Advertisement
Guest User

Certificate Parser Java

a guest
Dec 9th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.20 KB | None | 0 0
  1. package javaCertificate;
  2.  
  3.  
  4. /*
  5.  * To change this template, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. /**
  9.  *
  10.  * @author Shuo
  11.  */
  12. import java.security.spec.InvalidKeySpecException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. import java.security.*;
  17. import java.io.*;
  18. import java.util.*;
  19. import java.security.cert.*;
  20. import java.security.cert.CertificateFactory;
  21. import java.security.interfaces.RSAPublicKey;
  22. import java.security.spec.X509EncodedKeySpec;
  23.  
  24. /**
  25.  * The application's main frame.
  26.  */
  27. public class ModifyCA {
  28.  
  29.     private String getKeyUsage(boolean b, int flag) {
  30.         Map<Integer, String> map = new HashMap<Integer, String>();
  31.         map.put(0, DIGITALSIGNATURE);
  32.         map.put(1, NONREPUDUATION);
  33.         map.put(2, KEYENCIPHERMENT);
  34.         map.put(3, DATAENCIPHERMENT);
  35.         map.put(4, KEYAGREEMENT);
  36.         map.put(5, KEYCERTSIGN);
  37.         map.put(6, CRLSIGN);
  38.         map.put(7, ENCIPHERONLY);
  39.         map.put(8, DECIPHERONLY);
  40.  
  41.         String s = "";
  42.         if (b) {
  43.             s = map.get(flag).toString() + ",";
  44.  
  45.         }
  46.         return s;
  47.     }
  48.  
  49.     private void parseCertificate(String argv, String cer) {
  50.  
  51.         CertificateFactory cf;
  52.         try {
  53.             cf = CertificateFactory.getInstance("X.509");
  54.             FileInputStream in;
  55.             try {
  56.                 in = new FileInputStream(cer);
  57.                 X509Certificate xc = (X509Certificate) cf.generateCertificate(in);
  58.  
  59.                 //     String s = xc.toString();
  60.                 if (argv.equals("-v") || argv.equals("-a")) {
  61.                     if (argv.equals("-a")) {
  62.                         System.out.print("Data:\n");
  63.                     }
  64.                     System.out.print("          Version: v" + xc.getVersion() + " ");
  65.                     System.out.print("(0x" + Integer.toHexString(xc.getVersion()) + ")\n");
  66.                 }
  67.                 if (argv.equals("-sn") || argv.equals("-a")) {
  68.                     System.out.print("          Serial Number: " + xc.getSerialNumber() + "\n");
  69.                 }
  70. //                    System.out.println("(0x"+Long.toHexString(xc.getSerialNumber().longValue())+")");
  71.                 if (argv.equals("-sa") || argv.equals("-a")) {
  72.                     System.out.print("          Signature Algorithm: " + xc.getSigAlgName() + "\n");
  73.                 }
  74.                 if (argv.equals("-is") || argv.equals("-a")) {
  75.                     System.out.print("          Issuer:" + xc.getIssuerDN().getName() + "\n");
  76.                 }
  77.                 if (argv.equals("-va") || argv.equals("-a")) {
  78.                     System.out.print("          Validity:\n");
  79.  
  80.                     System.out.print("              Not Before:" + xc.getNotBefore() + "\n");
  81.                     System.out.print("              Not  After:" + xc.getNotAfter() + "\n");
  82.                 }
  83.                 if (argv.equals("-sub") || argv.equals("-a")) {
  84.                     System.out.print("          Subject: " + xc.getSubjectDN().getName() + "\n");
  85.                 }
  86.                 if (argv.equals("-a")) {
  87.                     System.out.print("          Subject Public Key Info:\n");
  88.                 }
  89.                 if (argv.equals("-pa") || argv.equals("-a")) {
  90.                     System.out.print("              Algorithm: " + xc.getPublicKey().getAlgorithm() + "\n");
  91.                 }
  92.                 // if (argv.equals("-p") || argv.equals("-a")) {
  93.  
  94.  
  95.                 try {
  96.  
  97.                     X509EncodedKeySpec keySpec = new X509EncodedKeySpec(xc.getPublicKey().getEncoded());
  98.  
  99.                     KeyFactory kf = KeyFactory.getInstance("RSA");
  100.                     try {
  101.                         RSAPublicKey k = (RSAPublicKey) kf.generatePublic(keySpec);
  102.                         if (argv.equals("-pm") || argv.equals("-a")) {
  103.  
  104.  
  105.                             System.out.print("              Public Key:\n");
  106.                             System.out.print("                 Modulus:\n");
  107.  
  108.                             //     PublicKey pubKey = kf.generatePublic(keySpec);
  109.  
  110.  
  111.  
  112.  
  113.                             byte[] mb = k.getModulus().toByteArray();
  114.                             for (int i = 1; i <= mb.length - 2; i++) {
  115.                                 if (i == 1) {
  116.                                     System.out.print("                     ");
  117.                                 }
  118.                                 if (i % 18 == 0) {
  119.                                     String str = Integer.toHexString(mb[i]).replace("ffffff", "");
  120.                                     if (str.length() == 1 || str.length() == 0) {
  121.                                         String s1 = "0" + str + ":";
  122.                                         System.out.print(s1);
  123.                                     } else {
  124.  
  125.                                         System.out.print(str + ":");
  126.                                     }
  127.                                     System.out.print("\n");
  128.                                     System.out.print("                     ");
  129.                                 } else {
  130.                                     String str = Integer.toHexString(mb[i]).replace("ffffff", "");
  131.                                     if (str.length() == 1 || str.length() == 0) {
  132.                                         String s1 = "0" + str + ":";
  133.                                         System.out.print(s1);
  134.                                     } else {
  135.                                         System.out.print(str + ":");
  136.                                     }
  137.                                 }
  138.  
  139.  
  140.                             }
  141.                             String laststr = Integer.toHexString(mb[mb.length - 1]).replace("ffffff", "");
  142.                             if (laststr.length() == 1 || laststr.length() == 0) {
  143.                                 System.out.print("0" + laststr + "\n");
  144.                             } else {
  145.                                 System.out.print(laststr + "\n");
  146.                             }
  147.  
  148.  
  149.  
  150.                         }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.                         if (argv.equals("-pe") || argv.equals("-a")) {
  164.                             System.out.print("                 Public Exponent: " + k.getPublicExponent() + " ");
  165.                             System.out.print("(0x" + Long.toHexString(k.getPublicExponent().longValue()) + ")\n");
  166.                         }
  167.                         if (argv.equals("-a")) {
  168.                             System.out.print("          Extensions:\n");
  169.                             System.out.print("              Identifier: Certificate Type\n");
  170.  
  171.  
  172.                             Set<String> cno = xc.getNonCriticalExtensionOIDs();
  173.                             Iterator<String> it = cno.iterator();
  174.                             while (it.hasNext()) {
  175.                                 String cOID = it.next();
  176.                                 if (cOID.equals(CERTIFICATE_TYPE_IDENTIFIER_OID)) {
  177.                                     System.out.print("                  Critical: no\n");
  178.                                 }
  179.                             }
  180.                             Set<String> cyes = xc.getCriticalExtensionOIDs();
  181.                             Iterator<String> it1 = cyes.iterator();
  182.                             while (it1.hasNext()) {
  183.                                 String cOID = it1.next();
  184.                                 if (cOID.equals(CERTIFICATE_TYPE_IDENTIFIER_OID)) {
  185.                                     System.out.print("                  Critical: yes\n");
  186.                                 }
  187.                             }
  188.                         }
  189.                         if (argv.equals("-pe") || argv.equals("-a")) {
  190.                             System.out.print("                  Certified Usage:\n");
  191.  
  192.                             boolean[] keyUsage = xc.getKeyUsage();
  193.  
  194.  
  195.  
  196.                             StringBuilder sbUsage = new StringBuilder();
  197.  
  198.                             for (int i = 0; i < keyUsage.length; i++) {
  199.                                 //   getKeyUsage
  200.                                 sbUsage.append(getKeyUsage(keyUsage[i], i));
  201.                             }
  202.  
  203.                             System.out.print("                      " + sbUsage.substring(0, sbUsage.length() - 1) + "\n");
  204.                         }
  205.                         if (argv.equals("-a")) {
  206.  
  207.                             System.out.print("              Identifier: Authority Key Identifier\n");
  208.                             Set<String> cno1 = xc.getNonCriticalExtensionOIDs();
  209.                             Iterator<String> it2 = cno1.iterator();
  210.                             while (it2.hasNext()) {
  211.                                 String cOID = it2.next();
  212.                                 if (cOID.equals(AUTHORITY_KEY_IDENTIFIER_OID)) {
  213.                                     System.out.print("                  Critical: no\n");
  214.                                 }
  215.                             }
  216.                             Set<String> cyes1 = xc.getCriticalExtensionOIDs();
  217.                             Iterator<String> it3 = cyes1.iterator();
  218.                             while (it3.hasNext()) {
  219.                                 String cOID = it3.next();
  220.                                 if (cOID.equals(AUTHORITY_KEY_IDENTIFIER_OID)) {
  221.                                     System.out.print("                  Critical: yes\n");
  222.                                 }
  223.                             }
  224.                         }
  225.  
  226.                         if (argv.equals("-ki") || argv.equals("-a")) {
  227.                             System.out.print("                  Key Identifier:\n");    //?????????????????? 6 byte before identifier?  and  ffffff
  228.                             byte[] auKey = xc.getExtensionValue("2.5.29.35");
  229.                             StringBuilder sbid = new StringBuilder();
  230.                             for (int i = 6; i < auKey.length; i++) {
  231.  
  232.                                 String id = Integer.toHexString(auKey[i]).replace("ffffff", "");
  233.                                 if (id.length() == 1 || id.length() == 0) {
  234.                                     sbid.append("0").append(id).append(":");
  235.                                 } else {
  236.                                     sbid.append(id).append(":");
  237.                                 }
  238.                             }
  239.                             System.out.print("                      " + sbid.substring(0, sbid.length() - 1) + "\n");
  240.                             //           System.out.println("            "+X509Extensions.KeyUsage);
  241.                         }
  242.                         if (argv.equals("-sc") || argv.equals("-a")) {
  243.                             System.out.print("Signature:\n");
  244.                             System.out.print("          Algorithm:   " + xc.getSigAlgName() + "\n");
  245.                             System.out.print("          Signature:\n");
  246.                             //     xc.getSignature()
  247.  
  248.  
  249.  
  250.                             byte[] sig = xc.getSignature();
  251.                             for (int i = 1; i <= sig.length - 2; i++) {
  252.                                 if (i == 1) {
  253.                                     System.out.print("              ");
  254.                                 }
  255.                                 if (i % 21 == 0) {
  256.                                     String str = Integer.toHexString(sig[i]).replace("ffffff", "");
  257.                                     if (str.length() == 1 || str.length() == 0) {
  258.                                         String s1 = "0" + str + ":";
  259.                                         System.out.print(s1);
  260.                                     } else {
  261.  
  262.                                         System.out.print(str + ":");
  263.                                     }
  264.                                     System.out.print("\n");
  265.                                     System.out.print("              ");
  266.                                 } else {
  267.                                     String str = Integer.toHexString(sig[i]).replace("ffffff", "");
  268.                                     if (str.length() == 1 || str.length() == 0) {
  269.                                         String s1 = "0" + str + ":";
  270.                                         System.out.print(s1);
  271.                                     } else {
  272.                                         System.out.print(str + ":");
  273.                                     }
  274.                                 }
  275.  
  276.  
  277.                             }
  278.                             String laststr = Integer.toHexString(sig[sig.length - 1]).replace("ffffff", "");
  279.                             if (laststr.length() == 1 || laststr.length() == 0) {
  280.                                 System.out.print("0" + laststr + "\n");
  281.                             } else {
  282.                                 System.out.print(laststr + "\n");
  283.                             }
  284.                         }
  285.                     } catch (InvalidKeySpecException ex) {
  286.                         Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
  287.                     }
  288.  
  289.                 } catch (NoSuchAlgorithmException ex) {
  290.                     Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
  291.                 }
  292.  
  293.             } catch (FileNotFoundException ex) {
  294.                 System.out.print("No certification file found!");
  295.                 // Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
  296.             }
  297.  
  298.         } catch (CertificateException ex) {
  299.  
  300.             Logger.getLogger(ModifyCA.class.getName()).log(Level.SEVERE, null, ex);
  301.         }
  302.     }
  303.     // Variables declaration - do not modify                    
  304.     private String CERTIFICATE_TYPE_IDENTIFIER_OID = "2.5.29.14";
  305.     private String AUTHORITY_KEY_IDENTIFIER_OID = "2.5.29.35";
  306.     private String DIGITALSIGNATURE = "digitalSignature";
  307.     private String NONREPUDUATION = "nonRepudiation";
  308.     private String KEYENCIPHERMENT = "keyEncipherment";
  309.     private String DATAENCIPHERMENT = "dataEncipherment";
  310.     private String KEYAGREEMENT = "keyAgreement";
  311.     private String KEYCERTSIGN = "keyCertSign";
  312.     private String CRLSIGN = "cRLSign";
  313.     private String ENCIPHERONLY = "encipherOnly";
  314.     private String DECIPHERONLY = "decipherOnly";
  315.  
  316.     public static void main(String[] args) throws IOException {
  317.         ModifyCA mca = new ModifyCA();
  318.  
  319.  
  320.  
  321.         //   System.out.println(args.equals("null"));
  322.  
  323.         //   System.out.print(args.length + "is  11111111111111111111");
  324.  
  325.         try {
  326.             if (args.length == 0) {
  327.                 System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
  328.                 System.out.println("");
  329.                 System.out.println("-a   See all information");
  330.                 System.out.println("-h   Show hex result");
  331.                 System.out.println("-v   Version of certification");
  332.                 System.out.println("-sn  Serial Number of certification");
  333.                 System.out.println("-sa  Signature Algorithm");
  334.                 System.out.println("-is  Issuer");
  335.                 System.out.println("-va  Validity");
  336.                 System.out.println("-sub Subject");
  337.                 //  System.out.println("Subject Public Key Info:");
  338.                 System.out.println("-pa  Public Key Algorithm");
  339.                 System.out.println("-pm  Public Key Modulus");
  340.                 System.out.println("-pe  Public Exponent");
  341.                 System.out.println("-cu  Certified Usage");
  342.                 System.out.println("-ki  Key Identifier");
  343.                 System.out.println("-sc  Signature Content");
  344.             }
  345.  
  346.             if (args.length == 1) {
  347.                 if (args[0].equals("help") || args[0].equals("?")) {
  348.                     System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
  349.                     System.out.println("");
  350.                     System.out.println("-a   See all information");
  351.                     System.out.println("-h   Show hex result");
  352.                     System.out.println("-v   Version of certification");
  353.                     System.out.println("-sn  Serial Number of certification");
  354.                     System.out.println("-sa  Signature Algorithm");
  355.                     System.out.println("-is  Issuer");
  356.                     System.out.println("-va  Validity");
  357.                     System.out.println("-sub Subject");
  358.                     // System.out.println("Subject Public Key Info:");
  359.                     System.out.println("-pa  Public Key Algorithm");
  360.                     System.out.println("-pm  Public Key Modulus");
  361.                     System.out.println("-pe  Public Exponent");
  362.                     System.out.println("-cu  Certified Usage");
  363.                     System.out.println("-ki  Key Identifier");
  364.                     System.out.println("-sc  Signature Content");
  365.                 } else {
  366.                     // System.out.print("is here");
  367.                     mca.parseCertificate("-a", args[0]);
  368.                 }
  369.             }
  370.             if (args.length >= 2) {
  371.  
  372.                 if (args[1].equals("help") || args[1].equals("?")) {
  373.                     System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
  374.                     System.out.println("");
  375.                     System.out.println("-a   See all information");
  376.                     System.out.println("-h   Show hex result");
  377.                     System.out.println("-v   Version of certification");
  378.                     System.out.println("-sn  Serial Number of certification");
  379.                     System.out.println("-sa  Signature Algorithm");
  380.                     System.out.println("-is  Issuer");
  381.                     System.out.println("-va  Validity");
  382.                     System.out.println("-sub Subject");
  383.                     //  System.out.println("Subject Public Key Info:");
  384.                     System.out.println("-pa  Public Key Algorithm");
  385.                     System.out.println("-pm  Public Key Modulus");
  386.                     System.out.println("-pe  Public Exponent");
  387.                     System.out.println("-cu  Certified Usage");
  388.                     System.out.println("-ki  Key Identifier");
  389.                     System.out.println("-sc  Signature Content");
  390.                 }
  391.  
  392.                 if (args[0].equals("-h") || args[1].equals("-h")) {
  393.                     if (args[0].equals("-a")) {
  394.                         mca.parseCertificate("-a", args[1]);
  395.                     }
  396.                     if (args[0].equals("-v")) {
  397.                         mca.parseCertificate("-v", args[1]);
  398.                     }
  399.                     if (args[0].equals("-sn")) {
  400.                         mca.parseCertificate("-sn", args[1]);
  401.                     }
  402.                     if (args[0].equals("-sa")) {
  403.                         mca.parseCertificate("-sa", args[1]);
  404.                     }
  405.                     if (args[0].equals("-is")) {
  406.                         mca.parseCertificate("-is", args[1]);
  407.                     }
  408.                     if (args[0].equals("-va")) {
  409.                         mca.parseCertificate("-va", args[1]);
  410.                     }
  411.                     if (args[0].equals("-sub")) {
  412.                         mca.parseCertificate("-sub", args[1]);
  413.                     }
  414.                     if (args[0].equals("-pa")) {
  415.                         mca.parseCertificate("-pa", args[1]);
  416.                     }
  417.                     if (args[0].equals("-pm")) {
  418.                         mca.parseCertificate("-pm", args[1]);
  419.                     }
  420.                     if (args[0].equals("-pe")) {
  421.                         mca.parseCertificate("-pe", args[1]);
  422.                     }
  423.                     if (args[0].equals("-cu")) {
  424.                         mca.parseCertificate("-cu", args[1]);
  425.                     }
  426.                     if (args[0].equals("-ki")) {
  427.                         mca.parseCertificate("-ki", args[1]);
  428.                     }
  429.                     if (args[0].equals("-sc")) {
  430.                         mca.parseCertificate("-sc", args[1]);
  431.                     }
  432.                    
  433.                 } else {
  434.                     if (args[0].equals("-a")) {
  435.                         mca.parseCertificate("-a", args[1]);
  436.                     }
  437.                     if (args[0].equals("-v")) {
  438.                         mca.parseCertificate("-v", args[1]);
  439.                     }
  440.                     if (args[0].equals("-sn")) {
  441.                         mca.parseCertificate("-sn", args[1]);
  442.                     }
  443.                     if (args[0].equals("-sa")) {
  444.                         mca.parseCertificate("-sa", args[1]);
  445.                     }
  446.                     if (args[0].equals("-is")) {
  447.                         mca.parseCertificate("-is", args[1]);
  448.                     }
  449.                     if (args[0].equals("-va")) {
  450.                         mca.parseCertificate("-va", args[1]);
  451.                     }
  452.                     if (args[0].equals("-sub")) {
  453.                         mca.parseCertificate("-sub", args[1]);
  454.                     }
  455.                     if (args[0].equals("-pa")) {
  456.                         mca.parseCertificate("-pa", args[1]);
  457.                     }
  458.                     if (args[0].equals("-pm")) {
  459.                         mca.parseCertificate("-pm", args[1]);
  460.                     }
  461.                     if (args[0].equals("-pe")) {
  462.                         mca.parseCertificate("-pe", args[1]);
  463.                     }
  464.                     if (args[0].equals("-cu")) {
  465.                         mca.parseCertificate("-cu", args[1]);
  466.                     }
  467.                     if (args[0].equals("-ki")) {
  468.                         mca.parseCertificate("-ki", args[1]);
  469.                     }
  470.                     if (args[0].equals("-sc")) {
  471.                         mca.parseCertificate("-sc", args[1]);
  472.                     }
  473.                 }
  474.             }
  475.         } catch (Exception e) {
  476.             //  System.out.print("111111111111111111111111");
  477.             System.out.println("For more information on a specific command,type -command.Input type should be '-command cerPath' or 'cerPath'");
  478.             System.out.println("");
  479.             System.out.println("-a   See all information");
  480.             System.out.println("-h   Show hex result");
  481.             System.out.println("-v   Version of certification");
  482.             System.out.println("-sn  Serial Number of certification");
  483.             System.out.println("-sa  Signature Algorithm");
  484.             System.out.println("-is  Issuer");
  485.             System.out.println("-va  Validity");
  486.             System.out.println("-sub Subject");
  487.             //    System.out.println("Subject Public Key Info:");
  488.             System.out.println("-pa  Public Key Algorithm");
  489.             System.out.println("-pm  Public Key Modulus");
  490.             System.out.println("-pe  Public Exponent");
  491.             System.out.println("-cu  Certified Usage");
  492.             System.out.println("-ki  Key Identifier");
  493.             System.out.println("-sc  Signature Content");
  494.             // mca.parseCertificate("-a", args[0]);
  495.         }
  496.  
  497. //        finally {
  498. //            System.out.print("test...");    
  499. //        }
  500.  
  501.     }
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement