Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaCertificate;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Shuo
- */
- import java.security.spec.InvalidKeySpecException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.security.*;
- import java.io.*;
- import java.util.*;
- import java.security.cert.*;
- import java.security.cert.CertificateFactory;
- import java.security.interfaces.RSAPublicKey;
- import java.security.spec.X509EncodedKeySpec;
- /**
- * The application's main frame.
- */
- public class ModifyCA {
- private String getKeyUsage(boolean b, int flag) {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(0, DIGITALSIGNATURE);
- map.put(1, NONREPUDUATION);
- map.put(2, KEYENCIPHERMENT);
- map.put(3, DATAENCIPHERMENT);
- map.put(4, KEYAGREEMENT);
- map.put(5, KEYCERTSIGN);
- map.put(6, CRLSIGN);
- map.put(7, ENCIPHERONLY);
- map.put(8, DECIPHERONLY);
- String s = "";
- if (b) {
- s = map.get(flag).toString() + ",";
- }
- return s;
- }
- private void parseCertificate(String argv, String cer) {
- CertificateFactory cf;
- try {
- cf = CertificateFactory.getInstance("X.509");
- FileInputStream in;
- try {
- in = new FileInputStream(cer);
- X509Certificate xc = (X509Certificate) cf.generateCertificate(in);
- // String s = xc.toString();
- if (argv.equals("-v") || argv.equals("-a")) {
- if (argv.equals("-a")) {
- System.out.print("Data:\n");
- }
- System.out.print(" Version: v" + xc.getVersion() + " ");
- System.out.print("(0x" + Integer.toHexString(xc.getVersion()) + ")\n");
- }
- if (argv.equals("-sn") || argv.equals("-a")) {
- System.out.print(" Serial Number: " + xc.getSerialNumber() + "\n");
- }
- // System.out.println("(0x"+Long.toHexString(xc.getSerialNumber().longValue())+")");
- if (argv.equals("-sa") || argv.equals("-a")) {
- System.out.print(" Signature Algorithm: " + xc.getSigAlgName() + "\n");
- }
- if (argv.equals("-is") || argv.equals("-a")) {
- System.out.print(" Issuer:" + xc.getIssuerDN().getName() + "\n");
- }
- if (argv.equals("-va") || argv.equals("-a")) {
- System.out.print(" Validity:\n");
- System.out.print(" Not Before:" + xc.getNotBefore() + "\n");
- System.out.print(" Not After:" + xc.getNotAfter() + "\n");
- }
- if (argv.equals("-sub") || argv.equals("-a")) {
- System.out.print(" Subject: " + xc.getSubjectDN().getName() + "\n");
- }
- if (argv.equals("-a")) {
- System.out.print(" Subject Public Key Info:\n");
- }
- if (argv.equals("-pa") || argv.equals("-a")) {
- System.out.print(" Algorithm: " + xc.getPublicKey().getAlgorithm() + "\n");
- }
- // if (argv.equals("-p") || argv.equals("-a")) {
- try {
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(xc.getPublicKey().getEncoded());
- KeyFactory kf = KeyFactory.getInstance("RSA");
- try {
- RSAPublicKey k = (RSAPublicKey) kf.generatePublic(keySpec);
- if (argv.equals("-pm") || argv.equals("-a")) {
- System.out.print(" Public Key:\n");
- System.out.print(" Modulus:\n");
- // PublicKey pubKey = kf.generatePublic(keySpec);
- byte[] mb = k.getModulus().toByteArray();
- for (int i = 1; i <= mb.length - 2; i++) {
- if (i == 1) {
- System.out.print(" ");
- }
- if (i % 18 == 0) {
- String str = Integer.toHexString(mb[i]).replace("ffffff", "");
- if (str.length() == 1 || str.length() == 0) {
- String s1 = "0" + str + ":";
- System.out.print(s1);
- } else {
- System.out.print(str + ":");
- }
- System.out.print("\n");
- System.out.print(" ");
- } else {
- String str = Integer.toHexString(mb[i]).replace("ffffff", "");
- if (str.length() == 1 || str.length() == 0) {
- String s1 = "0" + str + ":";
- System.out.print(s1);
- } else {
- System.out.print(str + ":");
- }
- }
- }
- String laststr = Integer.toHexString(mb[mb.length - 1]).replace("ffffff", "");
- if (laststr.length() == 1 || laststr.length() == 0) {
- System.out.print("0" + laststr + "\n");
- } else {
- System.out.print(laststr + "\n");
- }
- }
- if (argv.equals("-pe") || argv.equals("-a")) {
- System.out.print(" Public Exponent: " + k.getPublicExponent() + " ");
- System.out.print("(0x" + Long.toHexString(k.getPublicExponent().longValue()) + ")\n");
- }
- if (argv.equals("-a")) {
- System.out.print(" Extensions:\n");
- System.out.print(" Identifier: Certificate Type\n");
- Set<String> cno = xc.getNonCriticalExtensionOIDs();
- Iterator<String> it = cno.iterator();
- while (it.hasNext()) {
- String cOID = it.next();
- if (cOID.equals(CERTIFICATE_TYPE_IDENTIFIER_OID)) {
- System.out.print(" Critical: no\n");
- }
- }
- Set<String> cyes = xc.getCriticalExtensionOIDs();
- Iterator<String> it1 = cyes.iterator();
- while (it1.hasNext()) {
- String cOID = it1.next();
- if (cOID.equals(CERTIFICATE_TYPE_IDENTIFIER_OID)) {
- System.out.print(" Critical: yes\n");
- }
- }
- }
- if (argv.equals("-pe") || argv.equals("-a")) {
- System.out.print(" Certified Usage:\n");
- boolean[] keyUsage = xc.getKeyUsage();
- StringBuilder sbUsage = new StringBuilder();
- for (int i = 0; i < keyUsage.length; i++) {
- // getKeyUsage
- sbUsage.append(getKeyUsage(keyUsage[i], i));
- }
- System.out.print(" " + sbUsage.substring(0, sbUsage.length() - 1) + "\n");
- }
- if (argv.equals("-a")) {
- System.out.print(" Identifier: Authority Key Identifier\n");
- Set<String> cno1 = xc.getNonCriticalExtensionOIDs();
- Iterator<String> it2 = cno1.iterator();
- while (it2.hasNext()) {
- String cOID = it2.next();
- if (cOID.equals(AUTHORITY_KEY_IDENTIFIER_OID)) {
- System.out.print(" Critical: no\n");
- }
- }
- Set<String> cyes1 = xc.getCriticalExtensionOIDs();
- Iterator<String> it3 = cyes1.iterator();
- while (it3.hasNext()) {
- String cOID = it3.next();
- if (cOID.equals(AUTHORITY_KEY_IDENTIFIER_OID)) {
- System.out.print(" Critical: yes\n");
- }
- }
- }
- if (argv.equals("-ki") || argv.equals("-a")) {
- System.out.print(" Key Identifier:\n"); //?????????????????? 6 byte before identifier? and ffffff
- byte[] auKey = xc.getExtensionValue("2.5.29.35");
- StringBuilder sbid = new StringBuilder();
- for (int i = 6; i < auKey.length; i++) {
- String id = Integer.toHexString(auKey[i]).replace("ffffff", "");
- if (id.length() == 1 || id.length() == 0) {
- sbid.append("0").append(id).append(":");
- } else {
- sbid.append(id).append(":");
- }
- }
- System.out.print(" " + sbid.substring(0, sbid.length() - 1) + "\n");
- // System.out.println(" "+X509Extensions.KeyUsage);
- }
- if (argv.equals("-sc") || argv.equals("-a")) {
- System.out.print("Signature:\n");
- System.out.print(" Algorithm: " + xc.getSigAlgName() + "\n");
- System.out.print(" Signature:\n");
- // xc.getSignature()
- byte[] sig = xc.getSignature();
- for (int i = 1; i <= sig.length - 2; i++) {
- if (i == 1) {
- System.out.print(" ");
- }
- if (i % 21 == 0) {
- String str = Integer.toHexString(sig[i]).replace("ffffff", "");
- if (str.length() == 1 || str.length() == 0) {
- String s1 = "0" + str + ":";
- System.out.print(s1);
- } else {
- System.out.print(str + ":");
- }
- System.out.print("\n");
- System.out.print(" ");
- } else {
- String str = Integer.toHexString(sig[i]).replace("ffffff", "");
- if (str.length() == 1 || str.length() == 0) {
- String s1 = "0" + str + ":";
- System.out.print(s1);
- } else {
- System.out.print(str + ":");
- }
- }
- }
- String laststr = Integer.toHexString(sig[sig.length - 1]).replace("ffffff", "");
- if (laststr.length() == 1 || laststr.length() == 0) {
- System.out.print("0" + laststr + "\n");
- } else {
- System.out.print(laststr + "\n");
- }
- }
- } catch (InvalidKeySpecException ex) {
- Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (NoSuchAlgorithmException ex) {
- Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (FileNotFoundException ex) {
- System.out.print("No certification file found!");
- // Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (CertificateException ex) {
- Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // Variables declaration - do not modify
- private String CERTIFICATE_TYPE_IDENTIFIER_OID = "2.5.29.14";
- private String AUTHORITY_KEY_IDENTIFIER_OID = "2.5.29.35";
- private String DIGITALSIGNATURE = "digitalSignature";
- private String NONREPUDUATION = "nonRepudiation";
- private String KEYENCIPHERMENT = "keyEncipherment";
- private String DATAENCIPHERMENT = "dataEncipherment";
- private String KEYAGREEMENT = "keyAgreement";
- private String KEYCERTSIGN = "keyCertSign";
- private String CRLSIGN = "cRLSign";
- private String ENCIPHERONLY = "encipherOnly";
- private String DECIPHERONLY = "decipherOnly";
- public static void main(String[] args) throws IOException {
- ModifyCA mca = new ModifyCA();
- // System.out.println(args.equals("null"));
- // System.out.print(args.length + "is 11111111111111111111");
- try {
- if (args.length == 0) {
- System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
- System.out.println("");
- System.out.println("-a See all information");
- System.out.println("-h Show hex result");
- System.out.println("-v Version of certification");
- System.out.println("-sn Serial Number of certification");
- System.out.println("-sa Signature Algorithm");
- System.out.println("-is Issuer");
- System.out.println("-va Validity");
- System.out.println("-sub Subject");
- // System.out.println("Subject Public Key Info:");
- System.out.println("-pa Public Key Algorithm");
- System.out.println("-pm Public Key Modulus");
- System.out.println("-pe Public Exponent");
- System.out.println("-cu Certified Usage");
- System.out.println("-ki Key Identifier");
- System.out.println("-sc Signature Content");
- }
- if (args.length == 1) {
- if (args[0].equals("help") || args[0].equals("?")) {
- System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
- System.out.println("");
- System.out.println("-a See all information");
- System.out.println("-h Show hex result");
- System.out.println("-v Version of certification");
- System.out.println("-sn Serial Number of certification");
- System.out.println("-sa Signature Algorithm");
- System.out.println("-is Issuer");
- System.out.println("-va Validity");
- System.out.println("-sub Subject");
- // System.out.println("Subject Public Key Info:");
- System.out.println("-pa Public Key Algorithm");
- System.out.println("-pm Public Key Modulus");
- System.out.println("-pe Public Exponent");
- System.out.println("-cu Certified Usage");
- System.out.println("-ki Key Identifier");
- System.out.println("-sc Signature Content");
- } else {
- // System.out.print("is here");
- mca.parseCertificate("-a", args[0]);
- }
- }
- if (args.length >= 2) {
- if (args[1].equals("help") || args[1].equals("?")) {
- System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
- System.out.println("");
- System.out.println("-a See all information");
- System.out.println("-h Show hex result");
- System.out.println("-v Version of certification");
- System.out.println("-sn Serial Number of certification");
- System.out.println("-sa Signature Algorithm");
- System.out.println("-is Issuer");
- System.out.println("-va Validity");
- System.out.println("-sub Subject");
- // System.out.println("Subject Public Key Info:");
- System.out.println("-pa Public Key Algorithm");
- System.out.println("-pm Public Key Modulus");
- System.out.println("-pe Public Exponent");
- System.out.println("-cu Certified Usage");
- System.out.println("-ki Key Identifier");
- System.out.println("-sc Signature Content");
- }
- if (args[0].equals("-h") || args[1].equals("-h")) {
- if (args[0].equals("-a")) {
- mca.parseCertificate("-a", args[1]);
- }
- if (args[0].equals("-v")) {
- mca.parseCertificate("-v", args[1]);
- }
- if (args[0].equals("-sn")) {
- mca.parseCertificate("-sn", args[1]);
- }
- if (args[0].equals("-sa")) {
- mca.parseCertificate("-sa", args[1]);
- }
- if (args[0].equals("-is")) {
- mca.parseCertificate("-is", args[1]);
- }
- if (args[0].equals("-va")) {
- mca.parseCertificate("-va", args[1]);
- }
- if (args[0].equals("-sub")) {
- mca.parseCertificate("-sub", args[1]);
- }
- if (args[0].equals("-pa")) {
- mca.parseCertificate("-pa", args[1]);
- }
- if (args[0].equals("-pm")) {
- mca.parseCertificate("-pm", args[1]);
- }
- if (args[0].equals("-pe")) {
- mca.parseCertificate("-pe", args[1]);
- }
- if (args[0].equals("-cu")) {
- mca.parseCertificate("-cu", args[1]);
- }
- if (args[0].equals("-ki")) {
- mca.parseCertificate("-ki", args[1]);
- }
- if (args[0].equals("-sc")) {
- mca.parseCertificate("-sc", args[1]);
- }
- } else {
- if (args[0].equals("-a")) {
- mca.parseCertificate("-a", args[1]);
- }
- if (args[0].equals("-v")) {
- mca.parseCertificate("-v", args[1]);
- }
- if (args[0].equals("-sn")) {
- mca.parseCertificate("-sn", args[1]);
- }
- if (args[0].equals("-sa")) {
- mca.parseCertificate("-sa", args[1]);
- }
- if (args[0].equals("-is")) {
- mca.parseCertificate("-is", args[1]);
- }
- if (args[0].equals("-va")) {
- mca.parseCertificate("-va", args[1]);
- }
- if (args[0].equals("-sub")) {
- mca.parseCertificate("-sub", args[1]);
- }
- if (args[0].equals("-pa")) {
- mca.parseCertificate("-pa", args[1]);
- }
- if (args[0].equals("-pm")) {
- mca.parseCertificate("-pm", args[1]);
- }
- if (args[0].equals("-pe")) {
- mca.parseCertificate("-pe", args[1]);
- }
- if (args[0].equals("-cu")) {
- mca.parseCertificate("-cu", args[1]);
- }
- if (args[0].equals("-ki")) {
- mca.parseCertificate("-ki", args[1]);
- }
- if (args[0].equals("-sc")) {
- mca.parseCertificate("-sc", args[1]);
- }
- }
- }
- } catch (Exception e) {
- // System.out.print("111111111111111111111111");
- System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
- System.out.println("");
- System.out.println("-a See all information");
- System.out.println("-h Show hex result");
- System.out.println("-v Version of certification");
- System.out.println("-sn Serial Number of certification");
- System.out.println("-sa Signature Algorithm");
- System.out.println("-is Issuer");
- System.out.println("-va Validity");
- System.out.println("-sub Subject");
- // System.out.println("Subject Public Key Info:");
- System.out.println("-pa Public Key Algorithm");
- System.out.println("-pm Public Key Modulus");
- System.out.println("-pe Public Exponent");
- System.out.println("-cu Certified Usage");
- System.out.println("-ki Key Identifier");
- System.out.println("-sc Signature Content");
- // mca.parseCertificate("-a", args[0]);
- }
- // finally {
- // System.out.print("test...");
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement