1 18 19 package org.apache.jmeter.util.keystore; 20 21 import iaik.pkcs.pkcs12.PKCS12; 22 import iaik.pkcs.pkcs12.KeyBag; 23 import iaik.pkcs.pkcs12.CertificateBag; 24 import iaik.pkcs.PKCSParsingException; 25 import iaik.pkcs.PKCSException; 26 import iaik.x509.ChainVerifier; 27 import iaik.utils.Util; 28 29 import java.security.cert.X509Certificate ; 30 import java.security.cert.CertificateException ; 31 import java.security.PrivateKey ; 32 import java.io.InputStream ; 33 import java.io.IOException ; 34 import java.util.Arrays ; 35 36 42 public class PKCS12KeyStore extends JmeterKeyStore 43 { 44 45 private X509Certificate [] certChain; 46 47 48 private PrivateKey key; 49 50 51 private String alias; 52 53 public PKCS12KeyStore(String type) throws Exception 54 { 55 if (!"PKCS12".equalsIgnoreCase(type)) 56 { 57 throw new Exception ("Invalid keystore type"); 58 } 59 } 60 61 public final String getAlias() 62 { 63 return this.alias; 64 } 65 66 69 public void load(InputStream is, String pword) 70 throws IOException , PKCSException, CertificateException 71 { 72 PKCS12 p12 = new PKCS12(is); 73 is.close(); 74 75 p12.decrypt(pword.toCharArray()); 76 77 KeyBag keyBag = p12.getKeyBag(); 78 79 if (null == keyBag) 80 { 81 throw new PKCSException("No private key found"); 82 } 83 84 byte[] keyBagLocalKeyId = keyBag.getLocalKeyID(); 85 86 this.key = keyBag.getPrivateKey(); 87 88 CertificateBag[] certBags = p12.getCertificateBags(); 89 if ((null == certBags) || (certBags.length == 0)) 90 { 91 throw new PKCSException("No certificates found"); 92 } 93 94 this.alias = new String (keyBagLocalKeyId); 95 X509Certificate myCert = null; 96 97 for (int i = 0; i < certBags.length; i++) 98 { 99 byte[] certBagLocalKeyId = certBags[i].getLocalKeyID(); 100 if ((null != keyBagLocalKeyId) && (null != certBagLocalKeyId)) 101 { 102 if (Arrays.equals(certBagLocalKeyId, keyBagLocalKeyId)) 103 { 104 myCert = certBags[i].getCertificate(); 105 break; 106 } 107 } 108 } 109 110 if (null == myCert) 111 { 112 throw new PKCSException("No owner certificate found"); 113 } 114 115 iaik.x509.X509Certificate[] certChain = 116 CertificateBag.getCertificates(certBags); 117 this.certChain = Util.arrangeCertificateChain(certChain, false); 118 } 119 120 123 public final X509Certificate [] getCertificateChain() 124 { 125 return this.certChain; 126 } 127 128 131 public final PrivateKey getPrivateKey() 132 { 133 return this.key; 134 } 135 } | Popular Tags |