⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mekeytool.java

📁 用于移动设备上的java虚拟机源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * @(#)MEKeyTool.java	1.18 02/09/18 @(#) * * Copyright (c) 2001-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package com.sun.midp.mekeytool;import java.util.*;import java.io.*;import java.security.*;import java.security.cert.*;import java.security.interfaces.RSAPublicKey;import java.math.BigInteger;import com.sun.midp.publickeystore.PublicKeyInfo;/** * Manages the initial public keystore needed to bootstrap the MIDP * security RI. It provides both a Java and a command line interface. * <p> * The anchor of trust on an ME (mobile equipment) are the public keys * loaded on it by the manufacturer, in RI this is known as the * <i>ME keystore</i>. This tool does for the RI what the manufacturer must * do for the ME so that trusted MIDP applications can be authenticated. * @see #main(String[]) */public class MEKeyTool {    /** default MIDP application directory, see Utility.c getStorageRoot() */    private final static String defaultAppDir = "appdb";    /** default ME keystore filename, see com.sun.midp.Main.java */    private final static String defaultKeystoreFilename = "_main.ks";    /**     * Maps byte codes that follow id-at (0x55 0x04) to corresponding name     * component tags (e.g. Commom Name, or CN, is 0x55, 0x04, 0x03 and     * Country, or C, is 0x55, 0x04, 0x06). See getName. See X.520 for     * the OIDs and RFC 1779 for the printable labels. Place holders for     * unknown labels have a -1 as the first byte.     */    private static final String[] AttrLabel = {        null,        null,        null,        "CN",     // Common name: id-at 3        "SN",     // Surname: id-at 4        null,        "C",      // Country: id-at 6        "L",      // Locality: id-at 7        "ST",      // State or province: id-at 8        "STREET", // Street address: id-at 9        "O",      // Organization: id-at 10        "OU",     // Organization unit: id-at 11    };    /** Email attribute label. */    private static final String EMAIL_ATTR_LABEL = "EmailAddress";    /** Email attribute object identifier. */    private static final byte[] EMAIL_ATTR_OID = {        (byte)0x2a, (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xf7,         (byte)0x0d, (byte)0x01, (byte)0x09, (byte)0x01    };    /** read-writable ME keystore that does not depend on SSL */    private PublicKeyStoreBuilderBase keystore;    /** the state for getFirstKey and getNextKey */    private int nextKeyToGet;    /**     * Performs the command specified in the first argument.     * <p>     * Exits with a 0 status if the command was successful.     * Exits and prints out an error message with a -1 status if the command     * failed.</p>     * <p><pre>     *MEKeyTool supports the following commands:     *     *  no args  - same has -help     *  -import  - import a public key from a JCE keystore     *              into a ME keystore     *  -delete  - delete a key from a ME keystore     *  -help    - print a usage summary     *  -list    - list the owner and validity period of each     *              key in a ME keystore     *     *Parameters for (commands):     *     *  -MEkeystore &lt;filename of the ME keystore&gt; (optional for all)     *  -keystore   &lt;filename of the JCA keystore&gt; (optional import)     *  -storepass  &lt;password for the JCA keystore&gt; (optional import)     *  -alias      &lt;short string ID of a key in a JCA keystore&gt; (import)     *  -domain     &lt;security domain of the ME key&gt; (optional import)     *  -owner      &lt;name of the owner of a ME key&gt; (delete)     *  -number     &lt;key number starting a 1 of a ME key&gt; (delete)     *     *Defaults:     *     *  -MEkeystore appdir/main.ks     *  -keystore   &lt;user's home dir&gt;/.keystore     *  -domain     untrusted     * </pre>     * @param args command line arguments     */    public static void main(String[] args) {        File meKeystoreFile = null;        if (args.length == 0) {            System.out.println("\n  Error: No command given");            displayUsage();            System.exit(-1);        }        if (args[0].equals("-help")) {            // user just needs help with the arguments            displayUsage();            System.exit(0);        }        // start with the default keystore file        meKeystoreFile = new File(defaultAppDir, defaultKeystoreFilename);        try {            if (args[0].equals("-import")) {                importCommand(meKeystoreFile, args);                System.exit(0);            }                        if (args[0].equals("-delete")) {                deleteCommand(meKeystoreFile, args);                System.exit(0);            }            if (args[0].equals("-list")) {                listCommand(meKeystoreFile, args);                System.exit(0);            }            throw new UsageException("  Invalid command: " + args[0]);        } catch (Exception e) {            System.out.println("\n  Error: " + e.getMessage());            if (e instanceof UsageException) {                displayUsage();            }            System.exit(-1);        }    }    /**     * Display the usage text to standard output.     */    private static void displayUsage() {        System.out.println("\n  MEKeyTool argument combinations:\n\n" +            "    -help\n" +            "    -import [-MEkeystore <filename>] " +            "[-keystore <filename>]\n" +            "            [-storepass <password>] -alias <key alias> " +            "[-domain <domain>]\n" +            "    -list [-MEkeystore <filename>]\n" +            "    -delete [-MEkeystore <filename>]\n" +            "            (-owner <owner name> | -number <key number>)\n" +            "\n" +            "  The default for -MEkeystore is \"appdb/_main.ks\".\n" +            "  The default for -keystore is \"$HOME/.keystore\".\n");    }    /**     * Process the command line arguments for the import command and     * then imports a public key from a JCA keystore to ME keystore.     * This method assumes the first argument is the import command     * and skips it.     * @param meKeystoreFile ME keystore abstract file name     * @param args command line arguments     * @exception Exception if an unrecoverable error occurs     */    private static void importCommand(File meKeystoreFile, String[] args)            throws Exception {        String jcaKeystoreFilename = null;        String keystorePassword = null;        String alias = null;        String domain = "untrusted";        MEKeyTool keyTool;        for (int i = 1; i < args.length; i++) {            try {                if (args[i].equals("-MEkeystore")) {                    i++;                    meKeystoreFile = new File(args[i]);                 } else if (args[i].equals("-keystore")) {                    i++;                    jcaKeystoreFilename = args[i];                 } else if (args[i].equals("-storepass")) {                    i++;                    keystorePassword = args[i];                 } else if (args[i].equals("-alias")) {                    i++;                    alias = args[i];                } else if (args[i].equals("-domain")) {                    i++;                    domain = args[i];                } else {                    throw new UsageException(                        "Invalid argument for import command: " + args[i]);                }            } catch (ArrayIndexOutOfBoundsException e) {                throw new UsageException("Missing value for " + args[--i]);            }        }        if (jcaKeystoreFilename == null) {            jcaKeystoreFilename = System.getProperty("user.home") +                                  File.separator + ".keystore";        }                if (alias == null) {            throw new Exception("J2SE key alias was not given");        }        try {            keyTool = new MEKeyTool(meKeystoreFile);        } catch (FileNotFoundException fnfe) {            keyTool = new MEKeyTool();        }        keyTool.importKeyFromJcaKeystore(jcaKeystoreFilename,                                      keystorePassword,                                      alias, domain);        keyTool.saveKeystore(meKeystoreFile);    }    /**     * Process the command line arguments for the delete command and     * then delete a public key from a ME keystore.     * This method assumes the first argument is the delete command     * and skips it.     * @param meKeystoreFile ME keystore abstract file name     * @param args command line arguments     * @exception Exception if an unrecoverable error occurs     */    private static void deleteCommand(File meKeystoreFile, String[] args)            throws Exception {        String owner = null;        int keyNumber = -1;        boolean keyNumberGiven = false;        MEKeyTool keyTool;        for (int i = 1; i < args.length; i++) {            try {                if (args[i].equals("-MEkeystore")) {                    i++;                    meKeystoreFile = new File(args[i]);                 } else if (args[i].equals("-owner")) {                    i++;                    owner = args[i];                } else if (args[i].equals("-number")) {                    keyNumberGiven = true;                    i++;                    try {                        keyNumber = Integer.parseInt(args[i]);                    } catch (NumberFormatException e) {                        throw new UsageException(                            "Invalid number for the -number argument: " +                            args[i]);                    }                } else {                    throw new UsageException(                        "Invalid argument for the delete command: " + args[i]);                }            } catch (ArrayIndexOutOfBoundsException e) {                throw new UsageException("Missing value for " + args[--i]);            }        }        if (owner == null && !keyNumberGiven) {            throw new UsageException(                "Neither key -owner or -number was not given");        }        if (owner != null && keyNumberGiven) {            throw new UsageException("-owner and -number cannot be used " +                "together");        }        keyTool = new MEKeyTool(meKeystoreFile);        if (owner != null) {            if (!keyTool.deleteKey(owner)) {                throw new UsageException("Key not found for: " + owner);            }        } else {            try {                keyTool.deleteKey(keyNumber - 1);            } catch (ArrayIndexOutOfBoundsException e) {                throw new UsageException("Invalid number for the -number " +                                    "delete option: " + keyNumber);            }                        }        keyTool.saveKeystore(meKeystoreFile);    }    /**     * Process the command line arguments for the list command and     * then list the public keys of a ME keystore.     * This method assumes the first argument is the list command     * and skips it.     * @param meKeystoreFile ME keystore abstract file name     * @param args command line arguments     * @exception Exception if an unrecoverable error occurs     */    private static void listCommand(File meKeystoreFile, String[] args)            throws Exception {        MEKeyTool keyTool;        PublicKeyInfo key;        for (int i = 1; i < args.length; i++) {            try {                if (args[i].equals("-MEkeystore")) {                    i++;                    meKeystoreFile = new File(args[i]);                 } else {                    throw new UsageException("Invalid argument for the list " +                                             "command: " + args[i]);                }            } catch (ArrayIndexOutOfBoundsException e) {                throw new UsageException("Missing value for " + args[--i]);            }        }        keyTool = new MEKeyTool(meKeystoreFile);        key = keyTool.getFirstKey();        for (int i = 1; key != null; i++) {            System.out.println("Key " + Integer.toString(i));            System.out.println(formatKeyInfo(key));            key = keyTool.getNextKey();        }        System.out.println("");    }    /**     * Constructs a MEKeyTool with an empty keystore.     */    public MEKeyTool() {        keystore = new PublicKeyStoreBuilderBase();    }    /**     * Constructs a MEKeyTool and loads its keystore using a filename.     * @param meKeystoreFilename serialized keystore file     * @exception FileNotFoundException if the file does not exist, is a     * directory rather than a regular file, or for some other reason     * cannot be opened for reading.     * @exception IOException if the key storage was corrupted     */    public MEKeyTool(String meKeystoreFilename)        throws FileNotFoundException, IOException {        FileInputStream input;        input = new FileInputStream(new File(meKeystoreFilename));        try {            keystore = new PublicKeyStoreBuilderBase(input);        } finally {            input.close();        }    };    /**     * Constructs a MEKeyTool and loads its keystore from a file.     * @param meKeystoreFile serialized keystore file     * @exception FileNotFoundException if the file does not exist, is a     * directory rather than a regular file, or for some other reason     * cannot be opened for reading.     * @exception IOException if the key storage was corrupted     */    public MEKeyTool(File meKeystoreFile)        throws FileNotFoundException, IOException {        FileInputStream input;        input = new FileInputStream(meKeystoreFile);        try {            keystore = new PublicKeyStoreBuilderBase(input);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -