use of java.security.PrivateKey in project neo4j by neo4j.
the class TestSslCertificateFactory method shouldCreateASelfSignedCertificate.
@Test
public void shouldCreateASelfSignedCertificate() throws Exception {
// Given
Certificates sslFactory = new Certificates();
File cPath = new File(tmpDir.getRoot(), "certificate");
File pkPath = new File(tmpDir.getRoot(), "key");
// When
sslFactory.createSelfSignedCertificate(cPath, pkPath, "myhost");
// Then
// Attempt to load certificate
Certificate[] certificates = sslFactory.loadCertificates(cPath);
assertThat(certificates.length, is(greaterThan(0)));
// Attempt to load private key
PrivateKey pk = sslFactory.loadPrivateKey(pkPath);
assertThat(pk, notNullValue());
}
use of java.security.PrivateKey in project neo4j by neo4j.
the class TestSslCertificateFactory method shouldLoadBinaryPrivateKey.
/**
* For backwards-compatibility reasons, we support both PEM-encoded private keys *and* raw binary files containing
* the private key data
*
* @throws Throwable
*/
@Test
public void shouldLoadBinaryPrivateKey() throws Throwable {
// Given
SelfSignedCertificate cert = new SelfSignedCertificate("example.com");
Certificates certs = new Certificates();
File keyFile = tmpDir.newFile("certificate");
byte[] raw = certs.loadPrivateKey(cert.privateKey()).getEncoded();
try (FileChannel ch = FileChannel.open(keyFile.toPath(), WRITE)) {
FileUtils.writeAll(ch, ByteBuffer.wrap(raw));
}
// When
PrivateKey pk = certs.loadPrivateKey(keyFile);
// Then
assertNotNull(pk);
}
use of java.security.PrivateKey in project netty by netty.
the class SslContextBuilder method keyManager.
/**
* Identifying certificate for this host. {@code keyCertChainFile} and {@code keyFile} may
* be {@code null} for client contexts, which disables mutual authentication.
*
* @param keyCertChainFile an X.509 certificate chain file in PEM format
* @param keyFile a PKCS#8 private key file in PEM format
* @param keyPassword the password of the {@code keyFile}, or {@code null} if it's not
* password-protected
*/
public SslContextBuilder keyManager(File keyCertChainFile, File keyFile, String keyPassword) {
X509Certificate[] keyCertChain;
PrivateKey key;
try {
keyCertChain = SslContext.toX509Certificates(keyCertChainFile);
} catch (Exception e) {
throw new IllegalArgumentException("File does not contain valid certificates: " + keyCertChainFile, e);
}
try {
key = SslContext.toPrivateKey(keyFile, keyPassword);
} catch (Exception e) {
throw new IllegalArgumentException("File does not contain valid private key: " + keyFile, e);
}
return keyManager(key, keyPassword, keyCertChain);
}
use of java.security.PrivateKey in project netty by netty.
the class SslContextBuilder method keyManager.
/**
* Identifying certificate for this host. {@code keyCertChainInputStream} and {@code keyInputStream} may
* be {@code null} for client contexts, which disables mutual authentication.
*
* @param keyCertChainInputStream an input stream for an X.509 certificate chain in PEM format
* @param keyInputStream an input stream for a PKCS#8 private key in PEM format
* @param keyPassword the password of the {@code keyInputStream}, or {@code null} if it's not
* password-protected
*/
public SslContextBuilder keyManager(InputStream keyCertChainInputStream, InputStream keyInputStream, String keyPassword) {
X509Certificate[] keyCertChain;
PrivateKey key;
try {
keyCertChain = SslContext.toX509Certificates(keyCertChainInputStream);
} catch (Exception e) {
throw new IllegalArgumentException("Input stream not contain valid certificates.", e);
}
try {
key = SslContext.toPrivateKey(keyInputStream, keyPassword);
} catch (Exception e) {
throw new IllegalArgumentException("Input stream does not contain valid private key.", e);
}
return keyManager(key, keyPassword, keyCertChain);
}
use of java.security.PrivateKey in project netty by netty.
the class BouncyCastleSelfSignedCertGenerator method generate.
static String[] generate(String fqdn, KeyPair keypair, SecureRandom random, Date notBefore, Date notAfter) throws Exception {
PrivateKey key = keypair.getPrivate();
// Prepare the information required for generating an X.509 certificate.
X500Name owner = new X500Name("CN=" + fqdn);
X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(owner, new BigInteger(64, random), notBefore, notAfter, owner, keypair.getPublic());
ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(key);
X509CertificateHolder certHolder = builder.build(signer);
X509Certificate cert = new JcaX509CertificateConverter().setProvider(PROVIDER).getCertificate(certHolder);
cert.verify(keypair.getPublic());
return newSelfSignedCertificate(fqdn, key, cert);
}
Aggregations