📄 jadtool.java
字号:
* print the usage, and exit with a -1. * * @param args The command line arguments given to main. * * @exception Exception if there are any errors */ private void performAddCertCommand(String[] args) throws Exception { int i = 1; // change the default for cert number for this command certIndex = 0; try { for (i = 1; i < args.length; i++) { if (args[i].equals("-encoding")) { encoding = args[++i]; } else if (args[i].equals("-keystore")) { keystore = args[++i]; } else if (args[i].equals("-storepass")) { storepass = args[++i].toCharArray(); } else if (args[i].equals("-alias")) { alias = args[++i]; } else if (args[i].equals("-certnum")) { certnum = args[++i]; } else if (args[i].equals("-chainnum")) { chainNum = args[++i]; } else if (args[i].equals("-inputjad")) { infile = args[++i]; } else if (args[i].equals("-outputjad")) { outfile = args[++i]; } else { usageError("Illegal option for " + command + ": " + args[i]); } } } catch (ArrayIndexOutOfBoundsException aiobe) { usageError("Missing value for " + args[--i]); } // these methods will check for the presence of the args they need checkCertAndChainNum(); initJadUtil(); openKeystoreAndOutputJad(); try { appdesc.addCert(alias, chainIndex, certIndex); appdesc.store(outstream, encoding); return; } catch (Exception e) { throw new Exception(command + " failed: " + e.toString()); } } /** * Perform the -showcert command, including parsing the line arguments * for the -showcert command. * <p> * If there is a problem parsing an argument, print the error, * print the usage, and exit with a -1. * * @param args The command line arguments given to main. * * @exception Exception if there are any errors */ private void performShowCertCommand(String[] args) throws Exception { int i = 1; X509Certificate c; boolean listAll = false; try { for (i = 1; i < args.length; i++) { if (args[i].equals("-encoding")) { encoding = args[++i]; } else if (args[i].equals("-certnum")) { certnum = args[++i]; } else if (args[i].equals("-chainnum")) { chainNum = args[++i]; } else if (args[i].equals("-all")) { listAll = true; } else if (args[i].equals("-inputjad")) { infile = args[++i]; } else { usageError("Illegal option for " + command + ": " + args[i]); } } } catch (ArrayIndexOutOfBoundsException aiobe) { usageError("Missing value for " + args[--i]); } if (listAll && (chainNum != null || certnum != null)) { usageError("-all cannot be used with -certnum or -chainnum"); } // these methods will check for the presence of the args they need checkCertAndChainNum(); initJadUtil(); if (listAll) { Vector certs = appdesc.getAllCerts(); if (certs.size() == 0) { System.out.println("\nNo certificates found in JAD.\n"); return; } System.out.println(); for (i = 0; i < certs.size(); i++) { Object[] temp = (Object[])certs.elementAt(i); System.out.println((String)temp[AppDescriptor.KEY] + ":"); displayCert((X509Certificate)temp[AppDescriptor.CERT]); } return; } try { c = appdesc.getCert(chainIndex, certIndex); } catch (Exception e) { throw new Exception("-showcert failed: " + e.toString()); } if (c == null) { throw new Exception("Certificate " + chainIndex + "-" + certIndex + " not in JAD"); } try { displayCert(c); return; } catch (Exception e) { throw new Exception("-showcert failed: " + e.toString()); } } /** * Check the format of the certificate and chain numbers. If there is a * problem, print the error, print usage, and exit with -1. */ private void checkCertAndChainNum() { if (certnum != null) { try { certIndex = (Integer.valueOf(certnum)).intValue(); if (certIndex <= 0) { usageError("-certnum must be a positive number"); } } catch (NumberFormatException nfe) { usageError("-certnum must be a positive number"); } } if (chainNum != null) { try { chainIndex = (Integer.valueOf(chainNum)).intValue(); if (chainIndex <= 0) { usageError("-chainnum must be a positive number"); } } catch (NumberFormatException nfe) { usageError("-chainnum must be a positive number"); } } } /** * Initializes an instance of the AppDescriptor class. * <p> * If the input file has not been specified, print an error and the usage, * then exit with a -1 * * @exception Exception if there are any errors */ private void initJadUtil() throws Exception { InputStream instream; if (infile == null) { usageError(command + " requires an input JAD"); } try { FileInputStream fis = new FileInputStream(infile); instream = new BufferedInputStream(fis); } catch (FileNotFoundException fnfe) { throw new Exception("Input JAD does not exist: " + infile); } try { appdesc = new AppDescriptor(); appdesc.load(instream, encoding); } catch (UnsupportedEncodingException uee) { throw new Exception("Encoding type " + encoding + " not supported"); } catch (IOException ioe) { throw new Exception("Error parsing input JAD: " + infile); } finally { try { // close now so the input and output JAD can be the same. instream.close(); } catch (Exception e) { // ignore } } } /** * Open the keystore and output JAD file. * <p> * If the key alias or output file has not been specified, print an * error and the usage, then exit with a -1 * * @exception Exception if there are any errors */ private void openKeystoreAndOutputJad() throws Exception { File ksfile; FileInputStream ksstream; if (alias == null) { usageError(command + " requires -alias"); } if (outfile == null) { usageError(command + " requires an output JAD"); } if (keystore == null) { keystore = System.getProperty("user.home") + File.separator + ".keystore"; } try { ksfile = new File(keystore); // Check if keystore file is empty if (ksfile.exists() && ksfile.length() == 0) { throw new Exception("Keystore exists, but is empty: " + keystore); } ksstream = new FileInputStream(ksfile); } catch (FileNotFoundException fnfe) { throw new Exception("Keystore does not exist: " + keystore); } try { try { // the stream will be closed later outstream = new FileOutputStream(outfile); } catch (IOException ioe) { throw new Exception("Error opening output JAD: " + outfile); } try { // load the keystore into the AppDescriptor appdesc.loadKeyStore(ksstream, storepass); } catch (Exception e) { throw new Exception("Keystore could not be loaded: " + e.toString()); } } finally { try { ksstream.close(); } catch (IOException e) { // ignore } } } /** * Prints the usage cases of this tool and exits with 0 status. */ private void help() { usage(0); } /** * Prints the usage cases of this tool and exits with -1 status. * * @param error usage error message to print */ private void usageError(String error) { System.err.println("\n" + error); usage(-1); } /** * Prints the usage cases of this tool and exits. * * @param exitStatus status to exit with */ private void usage(int exitStatus) { System.out.println(USAGE); System.exit(exitStatus); } /** * Display a certficate. * * @param c certificate to display * * @exception Exception if there are any errors */ private void displayCert(X509Certificate c) throws Exception { String digest; System.out.println(); System.out.println("Subject: " + c.getSubjectDN().getName()); System.out.println("Issuer : " + c.getIssuerDN().getName()); System.out.println("Serial number: " + c.getSerialNumber().toString(16)); System.out.println("Valid from " + c.getNotBefore() + " to " + c.getNotAfter()); System.out.println("Certificate fingerprints:"); System.out.print(" MD5: "); digest = AppDescriptor.createFingerprint(c.getEncoded(), "MD5"); System.out.println(digest); System.out.print(" SHA: "); digest = AppDescriptor.createFingerprint(c.getEncoded(), "SHA"); System.out.println(digest); System.out.println(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -