📄 mekeytool.java
字号:
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */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.*;/** * Manages the initial public keystore needed to bootstrap this MIDP * security implementation. 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 MIDP implementation this is known * as the <i>ME keystore</i>. This tool does for the MIDP implementation * 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 can be set via this property */ private static final String DEFAULT_KEYSTORE_PROPERTY = "default.keystore"; /** default keystore can be set via this property */ private static final String DEFAULT_MEKEYSTORE_PROPERTY = "default.mekeystore"; /** 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. Common 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 <filename of the ME keystore> (optional for all) * -keystore <filename of the JCA keystore> (optional import) * -storepass <password for the JCA keystore> (optional import) * -alias <short string ID of a key in a JCA keystore> (import) * -domain <security domain of the ME key> (optional import) * -owner <name of the owner of a ME key> (delete) * -number <key number starting a 1 of a ME key> (delete) * *Defaults: * * -MEkeystore appdir/main.ks or set via -Ddefault.mekeystore= * -keystore <user's home dir>/.keystore or set via * -Ddefault.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 String defaultMeKeystore = System.getProperty(DEFAULT_MEKEYSTORE_PROPERTY); if (defaultMeKeystore != null) { meKeystoreFile = new File(defaultMeKeystore); } else { 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 \"" + System.getProperty(DEFAULT_MEKEYSTORE_PROPERTY, "appdb/_main.ks") + "\".\n" + " The default for -keystore is \"" + System.getProperty(DEFAULT_KEYSTORE_PROPERTY, "$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 = "identified"; 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( DEFAULT_KEYSTORE_PROPERTY, 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -