Search in sources :

Example 26 with PrivateKey

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());
}
Also used : PrivateKey(java.security.PrivateKey) File(java.io.File) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 27 with PrivateKey

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);
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) PrivateKey(java.security.PrivateKey) FileChannel(java.nio.channels.FileChannel) File(java.io.File) Test(org.junit.Test)

Example 28 with PrivateKey

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);
}
Also used : PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) SSLException(javax.net.ssl.SSLException)

Example 29 with PrivateKey

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);
}
Also used : PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) SSLException(javax.net.ssl.SSLException)

Example 30 with PrivateKey

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);
}
Also used : PrivateKey(java.security.PrivateKey) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509v3CertificateBuilder(org.bouncycastle.cert.X509v3CertificateBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) ContentSigner(org.bouncycastle.operator.ContentSigner) BigInteger(java.math.BigInteger) X500Name(org.bouncycastle.asn1.x500.X500Name) X509Certificate(java.security.cert.X509Certificate)

Aggregations

PrivateKey (java.security.PrivateKey)517 X509Certificate (java.security.cert.X509Certificate)217 KeyFactory (java.security.KeyFactory)169 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)144 Certificate (java.security.cert.Certificate)127 PublicKey (java.security.PublicKey)120 ByteArrayInputStream (java.io.ByteArrayInputStream)118 KeyStore (java.security.KeyStore)93 CertificateFactory (java.security.cert.CertificateFactory)92 IOException (java.io.IOException)81 Key (java.security.Key)74 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)73 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)70 Entry (java.security.KeyStore.Entry)60 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)60 KeyPair (java.security.KeyPair)59 SecretKey (javax.crypto.SecretKey)48 InvalidKeyException (java.security.InvalidKeyException)47 KeyStoreException (java.security.KeyStoreException)46 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)46