📄 mekeytool.java
字号:
/* * @(#)MEKeyTool.java 1.3 01/08/22 * * Copyright (c) 2001 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions */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.PublicKeyStoreBuilderBase;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"; /** read-writable ME keystore that does not depend on KSSL */ private PublicKeyStoreBuilderBase keystore; /** an enumeration for getFirstKey and getNextKey */ private Enumeration owners; /** * Performs the command specified in the first argument. * <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) * *Defaults: * * -MEkeystore appdir/main.ks * -keystore <user's home dir>/.keystore * -domain untrusted * </pre> * @param args command line arguments */ public static void main(String[] args) { File meKeystoreFile = null; if (args.length == 0 || args[0].equalsIgnoreCase("-help")) { // user just needs help with the arguments System.out.println("\n MEKeyTool argument combinations:\n\n" + " -delete [-MEkeystore <filename>] " + "-owner <owner name>\n" + " -help\n" + " -import [-MEkeystore <filename>] " + "[-keystore <filename>]\n" + " [-storepass <password>] -alias <key alias> " + "[-domain <domain>]\n" + " -list [-MEkeystore <filename>]\n"); return; } // start with the default keystore file meKeystoreFile = new File(defaultAppDir, defaultKeystoreFilename); try { if (args[0].equalsIgnoreCase("-import")) { importCommand(meKeystoreFile, args); return; } if (args[0].equalsIgnoreCase("-delete")) { deleteCommand(meKeystoreFile, args); return; } if (args[0].equalsIgnoreCase("-list")) { listCommand(meKeystoreFile, args); return; } System.out.println(" Invalid command: " + args[0]); return; } catch (Exception e) { System.out.println(" Error: " + e.getMessage()); } } /** * 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++) { if (args[i].equalsIgnoreCase("-MEkeystore")) { i++; meKeystoreFile = new File(args[i]); } else if (args[i].equalsIgnoreCase("-keystore")) { i++; jcaKeystoreFilename = args[i]; } else if (args[i].equalsIgnoreCase("-storepass")) { i++; keystorePassword = args[i]; } else if (args[i].equalsIgnoreCase("-alias")) { i++; alias = args[i]; } else if (args[i].equalsIgnoreCase("-domain")) { i++; domain = args[i]; } else { throw new Exception("Invalid argument for import command: " + args[i]); } } if (jcaKeystoreFilename == null) { jcaKeystoreFilename = System.getProperty("user.home") + File.separator + ".keystore"; } if (alias == null) { throw new Exception("SE key alias not specified"); } 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; MEKeyTool keyTool; for (int i = 1; i < args.length; i++) { if (args[i].equalsIgnoreCase("-MEkeystore")) { i++; meKeystoreFile = new File(args[i]); } else if (args[i].equalsIgnoreCase("-owner")) { i++; owner = args[i]; } else { throw new Exception("Invalid argument for delete command: " + args[i]); } } keyTool = new MEKeyTool(meKeystoreFile); if (owner == null) { throw new Exception("Key owner not specified"); } keyTool.deleteKey(owner); 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++) { if (args[i].equalsIgnoreCase("-MEkeystore")) { i++; meKeystoreFile = new File(args[i]); } else { throw new Exception("Invalid argument for list command: " + args[i]); } } System.out.println(""); 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(); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -