1 package net.sourceforge.jcetaglib.lib; 2 3 import net.sourceforge.jcetaglib.tools.Hex; 4 import net.sourceforge.jcetaglib.tools.KeyTools; 5 import org.bouncycastle.asn1.ASN1Sequence; 6 import org.bouncycastle.asn1.DERInputStream; 7 import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; 8 import org.bouncycastle.asn1.x509.SubjectKeyIdentifier; 9 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 10 import org.bouncycastle.util.encoders.Base64; 11 12 import java.io.*; 13 import java.security.*; 14 import java.security.cert.*; 15 import java.security.spec.InvalidKeySpecException ; 16 import java.security.spec.PKCS8EncodedKeySpec ; 17 import java.util.StringTokenizer ; 18 19 24 public class CertTools { 25 26 28 29 public CertTools() { 30 } 31 32 33 40 public static String getPartFromDN(String dn, String dnpart) { 41 String trimmeddn = dn.trim(); 42 String part = null, o = null; 43 StringTokenizer st = new StringTokenizer (trimmeddn, ",="); 44 while (st.hasMoreTokens()) { 45 o = st.nextToken(); 46 if (o.trim().equalsIgnoreCase(dnpart)) { 47 part = st.nextToken(); 48 } 49 } 50 return part; 51 } 53 public static PrivateKey getPrivatefromPEM(String keyFile, String keypwd) 54 throws IOException, NoSuchAlgorithmException, InvalidKeySpecException , Exception { 55 InputStream inStrm = new FileInputStream(keyFile); 56 PrivateKey privKey = getPrivatefromPEM(inStrm, keypwd); 57 return privKey; 58 } 59 60 public static PrivateKey getPrivatefromPEM(InputStream keystream, String keypwd) 61 throws IOException, NoSuchAlgorithmException, InvalidKeySpecException , Exception { 62 63 String beginKey; 64 String endKey; 65 66 if (keypwd == null || keypwd == "") { 67 beginKey = "-----BEGIN PRIVATE KEY-----"; 68 endKey = "-----END PRIVATE KEY-----"; 69 } else { 70 beginKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----"; 71 endKey = "-----END ENCRYPTED PRIVATE KEY-----"; 72 } 73 74 BufferedReader bufRdr = new BufferedReader(new InputStreamReader(keystream)); 75 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 76 PrintStream opstr = new PrintStream(ostr); 77 String temp; 78 while ((temp = bufRdr.readLine()) != null && 79 !temp.equals(beginKey)) 80 continue; 81 if (temp == null) 82 throw new IOException("Error in " + keystream.toString() + ", missing " + beginKey + " boundary"); 83 while ((temp = bufRdr.readLine()) != null && 84 !temp.equals(endKey)) 85 opstr.print(temp); 86 if (temp == null) 87 throw new IOException("Error in " + keystream.toString() + ", missing " + endKey + " boundary"); 88 89 opstr.close(); 90 91 byte[] keybuf = Base64.decode(ostr.toByteArray()); 92 93 PrivateKey privKey = null; 94 95 PKCS8EncodedKeySpec prvSpec = new PKCS8EncodedKeySpec (keybuf); 97 98 if (keypwd == null || keypwd == "") { 99 KeyFactory kf = KeyFactory.getInstance("RSA"); 100 privKey = kf.generatePrivate(prvSpec); 101 } else { 102 privKey = KeyTools.decryptPrivateKey(keybuf, keypwd); 103 } 104 105 return privKey; 106 } 107 108 109 118 public static X509Certificate getCertfromPEM(String certFile) throws IOException, CertificateException { 119 InputStream inStrm = new FileInputStream(certFile); 120 X509Certificate cert = getCertfromPEM(inStrm); 121 return cert; 122 } 123 124 133 public static X509Certificate getCertfromPEM(InputStream certstream) 134 throws IOException, CertificateException { 135 136 String beginKey = "-----BEGIN CERTIFICATE-----"; 137 String endKey = "-----END CERTIFICATE-----"; 138 BufferedReader bufRdr = new BufferedReader(new InputStreamReader(certstream)); 139 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 140 PrintStream opstr = new PrintStream(ostr); 141 String temp; 142 while ((temp = bufRdr.readLine()) != null && 143 !temp.equals(beginKey)) 144 continue; 145 if (temp == null) 146 throw new IOException("Error in " + certstream.toString() + ", missing " + beginKey + " boundary"); 147 while ((temp = bufRdr.readLine()) != null && 148 !temp.equals(endKey)) 149 opstr.print(temp); 150 if (temp == null) 151 throw new IOException("Error in " + certstream.toString() + ", missing " + endKey + " boundary"); 152 opstr.close(); 153 154 byte[] certbuf = Base64.decode(ostr.toByteArray()); 155 156 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 158 X509Certificate x509cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certbuf)); 159 160 return x509cert; 161 } 163 171 public static X509CRL getCRLfromByteArray(byte[] crl) 172 throws CertificateException, CRLException { 173 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 174 X509CRL x509crl = (X509CRL) cf.generateCRL(new ByteArrayInputStream(crl)); 175 return x509crl; 176 } 178 184 public static boolean isSelfSigned(X509Certificate cert) { 185 boolean ret = cert.getSubjectDN().equals(cert.getIssuerDN()); 186 return ret; 187 } 189 public static SubjectKeyIdentifier createSubjectKeyId(PublicKey pubKey) { 193 try { 194 ByteArrayInputStream bIn = new ByteArrayInputStream(pubKey.getEncoded()); 195 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new DERInputStream(bIn).readObject()); 196 197 return new SubjectKeyIdentifier(info); 198 } catch (Exception e) { 199 throw new RuntimeException ("error creating key"); 200 } 201 } 202 203 public static AuthorityKeyIdentifier createAuthorityKeyId(PublicKey pubKey) { 204 try { 205 ByteArrayInputStream bIn = new ByteArrayInputStream(pubKey.getEncoded()); 206 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new DERInputStream(bIn).readObject()); 207 208 return new AuthorityKeyIdentifier(info); 209 } catch (Exception e) { 210 throw new RuntimeException ("error creating AuthorityKeyId"); 211 } 212 } 213 214 215 221 public static String getFingerprintAsString(X509Certificate cert) { 222 223 try { 224 byte[] res = generateSHA1Fingerprint(cert.getEncoded()); 225 return Hex.encode(res); 226 } catch (CertificateEncodingException cee) { 227 System.out.println("Error encoding X509 certificate." + cee); 228 } 229 return null; 230 } 231 232 238 public static byte[] generateSHA1Fingerprint(byte[] ba) { 239 try { 240 MessageDigest md = MessageDigest.getInstance("SHA1"); 241 return md.digest(ba); 242 } catch (NoSuchAlgorithmException nsae) { 243 System.out.println("SHA1 algorithm not supported" + nsae); 244 } 245 return null; 246 } 248 } 250 | Popular Tags |