use of org.bouncycastle.util.io.pem.PemObject 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.PemObject in project neo4j by neo4j.
the class Certificates method writePem.
private void writePem(String type, byte[] encodedContent, File path) throws IOException {
path.getParentFile().mkdirs();
try (PemWriter writer = new PemWriter(new FileWriter(path))) {
writer.writeObject(new PemObject(type, encodedContent));
writer.flush();
}
path.setReadable(false, false);
path.setWritable(false, false);
path.setReadable(true);
path.setWritable(true);
}
use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.
the class KeyParser method parse.
public Object parse(String s) {
Object key = null;
if (key == null) {
if (s.contains(BEGIN_PRIVATE_KEY)) {
String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
if (payload.contains(END_PRIVATE_KEY)) {
payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
key = tryParsePemFormat(payload);
}
}
}
if (key == null) {
try {
PemReader reader = new PemReader(new StringReader(s));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(keySpec);
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
PublicKey publicKey = kf.generatePublic(publicKeySpec);
key = new KeyPair(publicKey, privateKey);
} else {
key = privateKey;
}
} catch (Exception e) {
log.debug("Error reading pem data", e);
return null;
}
}
if (key == null) {
try {
// TODO: Check if looks like base64??
byte[] fromBase64 = Base64.decode(s);
key = parse(fromBase64);
} catch (Exception e) {
log.debug("Cannot decode as base64", e);
}
}
return key;
}
use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.
the class SimpleCertificateAuthority method parseCsr.
private static PKCS10CertificationRequest parseCsr(String csr) throws IOException {
PemReader reader = new PemReader(new StringReader(csr));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS10CertificationRequest csrHolder = new PKCS10CertificationRequest(pemObject.getContent());
return csrHolder;
}
use of org.bouncycastle.util.io.pem.PemObject in project keywhiz by square.
the class ExpirationExtractor method expirationFromEncodedCertificateChain.
@Nullable
public static Instant expirationFromEncodedCertificateChain(byte[] content) {
PemReader reader = new PemReader(new InputStreamReader(new ByteArrayInputStream(content), UTF_8));
PemObject object;
try {
object = reader.readPemObject();
} catch (IOException e) {
// Should never occur (reading form byte array)
throw Throwables.propagate(e);
}
Instant earliest = null;
while (object != null) {
if (object.getType().equals("CERTIFICATE")) {
Instant expiry = expirationFromRawCertificate(object.getContent());
if (earliest == null || expiry.isBefore(earliest)) {
earliest = expiry;
}
}
try {
object = reader.readPemObject();
} catch (IOException e) {
// Should never occur (reading form byte array)
throw Throwables.propagate(e);
}
}
return earliest;
}
Aggregations