Download - Svetlin Nakov
Download - Svetlin Nakov
Download - Svetlin Nakov
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
private KeyStore loadKeyStoreFromPFXFile(String aFileName, String aKeyStorePass)<br />
throws GeneralSecurityException, IOException {<br />
KeyStore keyStore = KeyStore.getInstance(PKCS12_KEYSTORE_TYPE);<br />
FileInputStream keyStoreStream = new FileInputStream(aFileName);<br />
char[] password = aKeyStorePass.toCharArray();<br />
keyStore.load(keyStoreStream, password);<br />
return keyStore;<br />
}<br />
/**<br />
* @return private key and certification chain corresponding to it, extracted<br />
* from given keystore using given password to access the keystore and the same<br />
* password to access the private key in it. The keystore is considered to have<br />
* only one entry that contains both certification chain and the corresponding<br />
* private key.<br />
* If the certificate has no entries, an exception is trown. It the keystore has<br />
* several entries, the first is used.<br />
*/<br />
private PrivateKeyAndCertChain getPrivateKeyAndCertChain(<br />
KeyStore aKeyStore, String aKeyPassword)<br />
throws GeneralSecurityException {<br />
char[] password = aKeyPassword.toCharArray();<br />
Enumeration aliasesEnum = aKeyStore.aliases();<br />
if (aliasesEnum.hasMoreElements()) {<br />
String alias = (String)aliasesEnum.nextElement();<br />
Certificate[] certificationChain = aKeyStore.getCertificateChain(alias);<br />
PrivateKey privateKey = (PrivateKey) aKeyStore.getKey(alias, password);<br />
PrivateKeyAndCertChain result = new PrivateKeyAndCertChain();<br />
result.mPrivateKey = privateKey;<br />
result.mCertificationChain = certificationChain;<br />
return result;<br />
} else {<br />
throw new KeyStoreException("The keystore is empty!");<br />
}<br />
}<br />
/**<br />
* @return Base64-encoded ASN.1 DER representation of given X.509 certification<br />
* chain.<br />
*/<br />
private String encodeX509CertChainToBase64(Certificate[] aCertificationChain)<br />
throws CertificateException {<br />
List certList = Arrays.asList(aCertificationChain);<br />
CertificateFactory certFactory =<br />
CertificateFactory.getInstance(X509_CERTIFICATE_TYPE);<br />
CertPath certPath = certFactory.generateCertPath(certList);<br />
byte[] certPathEncoded = certPath.getEncoded(CERTIFICATION_CHAIN_ENCODING);<br />
String base64encodedCertChain = Base64Utils.base64Encode(certPathEncoded);<br />
return base64encodedCertChain;<br />
}<br />
/**<br />
* Reads the specified file into a byte array.<br />
*/<br />
private byte[] readFileInByteArray(String aFileName)<br />
throws IOException {<br />
File file = new File(aFileName);<br />
FileInputStream fileStream = new FileInputStream(file);<br />
try {<br />
int fileSize = (int) file.length();<br />
byte[] data = new byte[fileSize];<br />
int bytesRead = 0;<br />
while (bytesRead < fileSize) {<br />
bytesRead += fileStream.read(data, bytesRead, fileSize-bytesRead);<br />
80