use of com.github.zhenwei.pkix.openssl.PEMParser in project LinLong-Java by zhenwei1108.
the class JcaPKIXIdentityBuilder method build.
/**
* Build an identity from the passed in key and certificate stream in PEM format.
*
* @param keyStream the PEM stream containing the key
* @param certificateStream the PEM stream containing the certificate
* @return an identity object.
* @throws IOException on a general parsing error.
* @throws CertificateException on a certificate parsing error.
*/
public JcaPKIXIdentity build(InputStream keyStream, InputStream certificateStream) throws IOException, CertificateException {
PEMParser keyParser = new PEMParser(new InputStreamReader(keyStream));
PrivateKey privKey;
Object keyObj = keyParser.readObject();
if (keyObj instanceof PEMKeyPair) {
PEMKeyPair kp = (PEMKeyPair) keyObj;
privKey = keyConverter.getPrivateKey(kp.getPrivateKeyInfo());
} else if (keyObj instanceof PrivateKeyInfo) {
privKey = keyConverter.getPrivateKey((PrivateKeyInfo) keyObj);
} else {
// TODO: handle encrypted private keys
throw new IOException("unrecognised private key file");
}
PEMParser certParser = new PEMParser(new InputStreamReader(certificateStream));
List certs = new ArrayList();
Object certObj;
while ((certObj = certParser.readObject()) != null) {
certs.add(certConverter.getCertificate((X509CertificateHolder) certObj));
}
return new JcaPKIXIdentity(privKey, (X509Certificate[]) certs.toArray(new X509Certificate[certs.size()]));
}
Aggregations