Search in sources :

Example 31 with PrivateKey

use of java.security.PrivateKey in project netty by netty.

the class SslContextTest method testUnencryptedEmptyPassword.

@Test(expected = IOException.class)
public void testUnencryptedEmptyPassword() throws Exception {
    PrivateKey key = SslContext.toPrivateKey(new File(getClass().getResource("test2_unencrypted.pem").getFile()), "");
    Assert.assertNotNull(key);
}
Also used : PrivateKey(java.security.PrivateKey) File(java.io.File) Test(org.junit.Test)

Example 32 with PrivateKey

use of java.security.PrivateKey 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;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeyFactory(java.security.KeyFactory)

Example 33 with PrivateKey

use of java.security.PrivateKey in project platformlayer by platformlayer.

the class KeyStoreEncryptionStore method main.

public static void main(String[] args) throws Exception {
    if (!args[0].equals("explode")) {
        throw new IllegalStateException();
    }
    char[] password = "notasecret".toCharArray();
    ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
    KeyStore keyStore = KeyStoreUtils.load(new File(args[1]));
    File dest = new File(args[2]);
    dest.mkdirs();
    Enumeration<String> aliases = keyStore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        if (keyStore.isKeyEntry(alias)) {
            Entry entry = keyStore.getEntry(alias, protParam);
            PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) entry;
            {
                X509Certificate[] certificateChain = toX509(privateKeyEntry.getCertificateChain());
                String encoded = CertificateUtils.toPem(certificateChain);
                File out = new File(dest, alias + ".crt");
                Files.write(encoded, out, Charsets.UTF_8);
            }
            {
                PrivateKey key = privateKeyEntry.getPrivateKey();
                String encoded = PrivateKeys.toPem(key);
                File out = new File(dest, alias + ".key");
                Files.write(encoded, out, Charsets.UTF_8);
            }
        }
        if (keyStore.isCertificateEntry(alias)) {
            Entry entry = keyStore.getEntry(alias, null);
            TrustedCertificateEntry trustedCertificateEntry = (TrustedCertificateEntry) entry;
            X509Certificate cert = (X509Certificate) trustedCertificateEntry.getTrustedCertificate();
            String encoded = CertificateUtils.toPem(cert);
            File out = new File(dest, alias + ".crt");
            Files.write(encoded, out, Charsets.UTF_8);
        }
    }
}
Also used : PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) Entry(java.security.KeyStore.Entry) File(java.io.File) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) ProtectionParameter(java.security.KeyStore.ProtectionParameter)

Example 34 with PrivateKey

use of java.security.PrivateKey in project platformlayer by platformlayer.

the class DirectoryEncryptionStore method getCertificateAndKey.

@Override
public CertificateAndKey getCertificateAndKey(String alias) {
    CertificateAndKey certificateAndKey;
    Preconditions.checkNotNull(alias);
    // Path to file
    File certPath = new File(base, alias + ".crt");
    List<X509Certificate> certificate;
    try {
        certificate = CertificateUtils.fromPem(certPath);
    } catch (IOException e) {
        throw new IllegalArgumentException("Error reading certificate: " + certPath, e);
    }
    File keyPath = new File(base, alias + ".key");
    PrivateKey privateKey;
    try {
        privateKey = PrivateKeys.fromPem(keyPath);
    } catch (IOException e) {
        throw new IllegalArgumentException("Error reading private key: " + keyPath, e);
    }
    certificateAndKey = new SimpleCertificateAndKey(certificate, privateKey);
    return certificateAndKey;
}
Also used : SimpleCertificateAndKey(com.fathomdb.crypto.SimpleCertificateAndKey) PrivateKey(java.security.PrivateKey) IOException(java.io.IOException) CertificateAndKey(com.fathomdb.crypto.CertificateAndKey) SimpleCertificateAndKey(com.fathomdb.crypto.SimpleCertificateAndKey) File(java.io.File) X509Certificate(java.security.cert.X509Certificate)

Example 35 with PrivateKey

use of java.security.PrivateKey in project platformlayer by platformlayer.

the class SecretStore method getSecretFromUser.

public CryptoKey getSecretFromUser(final UserEntity user) {
    SecretStoreDecoder visitor = new SecretStoreDecoder() {

        @Override
        public void visitUserKey(int userId, byte[] data) {
            if (userId == user.getId()) {
                setSecretKey(decryptSymetricKey(user.getUserSecret(), data));
            }
        }

        @Override
        public void visitAsymetricUserKey(int userId, byte[] data) {
            if (userId == user.getId()) {
                PrivateKey privateKey = user.getPrivateKey();
                setSecretKey(decryptAsymetricKey(privateKey, data));
            }
        }
    };
    try {
        read(encoded, visitor);
    } catch (IOException e) {
        throw new IllegalArgumentException("Secret data is corrupted", e);
    }
    return visitor.getSecretKey();
}
Also used : PrivateKey(java.security.PrivateKey) IOException(java.io.IOException)

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