use of org.bouncycastle.util.io.pem.PemObject in project cloudstack by apache.
the class CertificateHelper method parseChain.
public static List<Certificate> parseChain(final String chain) throws IOException, CertificateException {
Preconditions.checkNotNull(chain);
final List<Certificate> certs = new ArrayList<Certificate>();
try (final PemReader pemReader = new PemReader(new StringReader(chain))) {
final PemObject pemObject = pemReader.readPemObject();
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
final ByteArrayInputStream bais = new ByteArrayInputStream(pemObject.getContent());
for (final Certificate cert : certificateFactory.generateCertificates(bais)) {
if (cert instanceof X509Certificate) {
certs.add(cert);
}
}
if (certs.isEmpty()) {
throw new IllegalStateException("Unable to decode certificate chain");
}
}
return certs;
}
use of org.bouncycastle.util.io.pem.PemObject in project ddf by codice.
the class SimpleSignTest method setUp.
@Before
public void setUp() throws Exception {
encryptionService = mock(PasswordEncryptor.class);
systemCrypto = new SystemCrypto("encryption.properties", "signature.properties", encryptionService);
simpleSign = new SimpleSign(systemCrypto);
cannedResponse = Resources.toString(Resources.getResource(getClass(), "/SAMLResponse.xml"), Charsets.UTF_8);
//Normally you would have the cert in a string already but for this test we will have to pull it out of the jks file
Certificate cert = ((Merlin) systemCrypto.getSignatureCrypto()).getKeyStore().getCertificate("dsa");
StringWriter writer = new StringWriter();
PemWriter pemWriter = new PemWriter(writer);
pemWriter.writeObject(new PemObject("CERTIFICATE", cert.getEncoded()));
pemWriter.flush();
dsaCert = writer.toString().replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "");
}
use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.
the class CsrParser method parsePemFormat.
private PKCS10CertificationRequest parsePemFormat(String data) throws IOException {
PemReader reader = new PemReader(new StringReader(data));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS10CertificationRequest csr = new PKCS10CertificationRequest(pemObject.getContent());
return csr;
}
use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.
the class Csr method getEncoded.
public String getEncoded() {
StringWriter stringWriter = new StringWriter();
try {
PemWriter writer = new PemWriter(stringWriter);
PemObjectGenerator pemObject = new PemObject("CERTIFICATE REQUEST", csr.getEncoded());
writer.writeObject(pemObject);
writer.close();
} catch (IOException e) {
throw new IllegalArgumentException("Error generating PEM", e);
}
return stringWriter.toString();
}
use of org.bouncycastle.util.io.pem.PemObject in project neo4j by neo4j.
the class Certificates method loadPrivateKey.
public PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
try (PemReader r = new PemReader(new FileReader(privateKeyFile))) {
PemObject pemObject = r.readPemObject();
if (pemObject != null) {
byte[] encodedKey = pemObject.getContent();
KeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
try {
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
} catch (InvalidKeySpecException ignore) {
try {
return KeyFactory.getInstance("DSA").generatePrivate(keySpec);
} catch (InvalidKeySpecException ignore2) {
try {
return KeyFactory.getInstance("EC").generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e);
}
}
}
}
}
// Ok, failed to read as PEM file, try and read it as a raw binary private key
try (DataInputStream in = new DataInputStream(new FileInputStream(privateKeyFile))) {
byte[] keyBytes = new byte[(int) privateKeyFile.length()];
in.readFully(keyBytes);
KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
return KeyFactory.getInstance(DEFAULT_ENCRYPTION).generatePrivate(keySpec);
}
}
Aggregations