1 package com.ca.commons.security.util; 2 3 import com.ca.commons.security.cert.extensions.*; 4 import com.ca.commons.security.asn1.*; 5 6 import java.io.*; 7 import java.util.*; 8 import java.security.cert.*; 9 import java.security.*; 10 11 import java.math.BigInteger ; 12 13 public class CertUtil 14 { 15 16 27 28 30 31 public static X509Certificate loadX509Certificate (String fileName) 32 { 33 return loadX509Certificate(new File(fileName)); 34 35 } 36 public static X509Certificate loadX509Certificate (File file) 37 { 38 try 39 { 40 FileInputStream inputStream = new FileInputStream (file); 41 42 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 43 X509Certificate cert = (X509Certificate)cf.generateCertificate(inputStream); 44 inputStream.close(); 45 46 return cert; 47 } 48 catch (Exception anyException) 49 { 50 anyException.printStackTrace (); 51 return null; 52 } 53 } 54 55 56 59 public static X509Certificate loadX509Certificate (byte[] certdata) 60 { 61 try 62 { 63 ByteArrayInputStream inputStream = new ByteArrayInputStream (certdata); 64 65 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 66 X509Certificate cert = (X509Certificate)cf.generateCertificate(inputStream); 67 inputStream.close(); 68 69 return cert; 70 } 71 catch (Exception e) 72 { 73 e.printStackTrace (); 74 return null; 75 } 76 } 77 78 79 83 public static void saveCertToDisk(byte[] certBytes, String outputDir) throws Exception 84 { 85 X509Certificate cert = loadX509Certificate(certBytes); 86 FileOutputStream fos = null; 87 String fileName = outputDir + "/" + cert.getSubjectDN().getName() + "_" + new Date().getTime() + ".der"; 88 try 89 { 90 fos = new FileOutputStream(fileName); 91 } 92 catch (Exception ex) 93 { 94 System.out.println("Could not open " + fileName + " for saving cert, now use " + 95 (fileName = new Date().getTime() + ".der")); 96 fos = new FileOutputStream(fileName); 97 } 98 99 fos.write(certBytes); 100 fos.close(); 101 } 102 103 104 public static AuthorityKeyIdentifier getAuthorityKeyIdentifier(X509Certificate theCert) { 105 AuthorityKeyIdentifier v3e = null; 106 byte [] aki = theCert.getExtensionValue(ASN1OID.authorityKeyIdentifier); 107 ASN1Object rext = decodeExtension(aki); 108 if ( rext != null ) { 109 v3e = new AuthorityKeyIdentifier(); 110 try { 111 v3e.init(rext); 112 } catch (Exception ex) { 113 ex.printStackTrace(); 114 } 115 } 116 return v3e; 117 } 118 119 120 public static byte [] getIssuerKeyId(X509Certificate theCert) { 121 AuthorityKeyIdentifier v3e = getAuthorityKeyIdentifier(theCert); 122 if ( v3e == null ) return null; 123 byte [] keyId = v3e.getKeyId(); 124 return keyId; 125 } 126 127 128 public static BigInteger getIssuerSerialNumber(X509Certificate theCert) { 129 AuthorityKeyIdentifier v3e = getAuthorityKeyIdentifier(theCert); 130 if ( v3e == null ) return null; 131 BigInteger issuerSerialNumber = v3e.getSerialNumber(); 132 return issuerSerialNumber; 133 } 134 135 136 public static ASN1Object decodeExtension(byte[] extvalue) { 137 ASN1Object rext = null; 138 if ( extvalue != null ) { 139 try { 140 DERCoder derCoder = new DERCoder(); 141 ASN1Object ext = derCoder.decode(extvalue); 142 143 if (ext.isASN1Type(ASN1Type.OCTET_STRING)) 144 rext = derCoder.decode((byte[])ext.getValue()); 145 } catch (Exception ex) { 146 ex.printStackTrace(); 147 } 148 } 149 return rext; 150 } 151 152 153 public static SubjectKeyIdentifier getSubjectKeyIdentifier(X509Certificate theCert) { 154 SubjectKeyIdentifier v3e = null; 155 byte [] ski = theCert.getExtensionValue(ASN1OID.subjectKeyIdentifier); 156 ASN1Object rext = decodeExtension(ski); 157 if ( rext != null ) { 158 v3e = new SubjectKeyIdentifier(); 159 try { 160 v3e.init(rext); 161 } catch (Exception ex) { 162 ex.printStackTrace(); 163 } 164 } 165 return v3e; 166 } 167 168 169 public static byte [] getSubjectKeyId(X509Certificate theCert) { 170 SubjectKeyIdentifier v3e = getSubjectKeyIdentifier(theCert); 171 if ( v3e == null ) return null; 172 byte [] keyId = v3e.getKeyId(); 173 return keyId; 174 } 175 176 177 184 public static int getPathLenConstraint(X509Certificate cert) 185 { 186 byte[] bs = cert.getExtensionValue(ASN1OID.basicConstraints); 188 if (bs != null) 189 { 190 try 191 { 192 DERCoder derCoder = new DERCoder(); 193 ASN1Object ext = derCoder.decode(bs); 194 ext = derCoder.decode((byte[])ext.getValue()); 195 196 BasicConstraints bsASN = new BasicConstraints(); 197 bsASN.init(ext); 198 199 return bsASN.pathLenConstraint; 200 } 201 catch (Exception ex) 202 { 203 ex.printStackTrace(); 204 } 206 } 207 208 return -1; 209 } 210 211 } | Popular Tags |