use of org.bouncycastle.util.io.pem.PemReader in project neo4j by neo4j.
the class Certificates method loadCertificates.
public Certificate[] loadCertificates(File certFile) throws CertificateException, IOException {
CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_TYPE);
Collection<Certificate> certificates = new LinkedList<>();
try (PemReader r = new PemReader(new FileReader(certFile))) {
for (PemObject pemObject = r.readPemObject(); pemObject != null; pemObject = r.readPemObject()) {
byte[] encodedCert = pemObject.getContent();
certificates.addAll(certFactory.generateCertificates(new ByteArrayInputStream(encodedCert)));
}
}
if (certificates.size() == 0) {
// Ok, failed to read as PEM file, try and read it as raw binary certificate
try (FileInputStream in = new FileInputStream(certFile)) {
certificates = (Collection<Certificate>) certFactory.generateCertificates(in);
}
}
return certificates.toArray(new Certificate[certificates.size()]);
}
use of org.bouncycastle.util.io.pem.PemReader in project gocd by gocd.
the class RegistrationJSONizer method fromJson.
public static Registration fromJson(String json) {
Map map = GSON.fromJson(json, Map.class);
if (map.isEmpty()) {
return Registration.createNullPrivateKeyEntry();
}
List<Certificate> chain = new ArrayList<>();
try {
PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
PrivateKey privateKey = kf.generatePrivate(spec);
String agentCertificate = (String) map.get("agentCertificate");
PemReader certReader = new PemReader(new StringReader(agentCertificate));
while (true) {
PemObject obj = certReader.readPemObject();
if (obj == null) {
break;
}
chain.add(CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(obj.getContent())));
}
return new Registration(privateKey, chain.toArray(new Certificate[chain.size()]));
} catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
throw bomb(e);
}
}
use of org.bouncycastle.util.io.pem.PemReader in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method certificateToDER.
public byte[] certificateToDER(String certificatePEM) {
byte[] content = null;
try (PemReader pemReader = new PemReader(new StringReader(certificatePEM))) {
final PemObject pemObject = pemReader.readPemObject();
content = pemObject.getContent();
} catch (IOException e) {
// best attempt
}
return content;
}
use of org.bouncycastle.util.io.pem.PemReader in project stdlib by petergeneric.
the class PEMHelper method loadCertificates.
/**
* Load one or more X.509 Certificates from a PEM file
*
* @param pemFile
* A PKCS8 PEM file containing only <code>CERTIFICATE</code> / <code>X.509 CERTIFICATE</code> blocks
*
* @return a JKS KeyStore with the certificate aliases "cert<code>index</code>" where index is the 0-based index of the
* certificate in the PEM
*
* @throws RuntimeException
* if a problem occurs
*/
public static KeyStore loadCertificates(final File pemFile) {
try (final PemReader pem = new PemReader(new FileReader(pemFile))) {
final KeyStore ks = createEmptyKeyStore();
int certIndex = 0;
Object obj;
while ((obj = parse(pem.readPemObject())) != null) {
if (obj instanceof Certificate) {
final Certificate cert = (Certificate) obj;
ks.setCertificateEntry("cert" + Integer.toString(certIndex++), cert);
} else {
throw new RuntimeException("Unknown PEM contents: " + obj + ". Expected a Certificate");
}
}
return ks;
} catch (Exception e) {
throw new RuntimeException("Error parsing PEM " + pemFile, e);
}
}
use of org.bouncycastle.util.io.pem.PemReader in project azure-iot-sdk-java by Azure.
the class SecurityProviderX509CertTest method parsePublicKeyCertificateExceptionsWrappedInCertificateException.
// Tests_SRS_SecurityClientDiceEmulator_34_004: [If any exception is encountered while attempting to create the public key certificate instance, this function shall throw a CertificateException.]
@Test(expected = CertificateException.class)
public void parsePublicKeyCertificateExceptionsWrappedInCertificateException() throws CertificateException, IOException {
// arrange
new NonStrictExpectations() {
{
new PemReader(new StringReader(expectedPublicKeyCertificateString));
result = new IOException();
}
};
// act
X509Certificate actualPublicKeyCertificate = Deencapsulation.invoke(SecurityProviderX509Cert.class, "parsePublicKeyCertificate", new Class[] { String.class }, expectedPublicKeyCertificateString);
// assert
assertEquals(mockedX509Certificate, actualPublicKeyCertificate);
}
Aggregations