Search in sources :

Example 66 with PrivateKey

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

the class KSPrivateKeyEntryTest method testPrivateKeyEntry01.

/**
     * Test for <code>PrivateKeyEntry(PrivateKey privateKey, Certificate[] chain)</code>
     * constructor
     * Assertion: throws NullPointerException when privateKey is null
     */
public void testPrivateKeyEntry01() {
    //new Certificate[1];
    Certificate[] certs = new MyCertificate[1];
    PrivateKey pk = null;
    try {
        new KeyStore.PrivateKeyEntry(pk, certs);
        fail("NullPointerException must be thrown when privateKey is null");
    } catch (NullPointerException e) {
    }
}
Also used : MyCertificate(org.apache.harmony.security.tests.support.cert.MyCertificate) PrivateKey(java.security.PrivateKey) Certificate(java.security.cert.Certificate) MyCertificate(org.apache.harmony.security.tests.support.cert.MyCertificate)

Example 67 with PrivateKey

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

the class KeyFactory method engineGeneratePrivate.

protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof PKCS8EncodedKeySpec) {
        try {
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec) keySpec).getEncoded());
            PrivateKey key = BouncyCastleProvider.getPrivateKey(info);
            if (key != null) {
                return key;
            }
            throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm());
        } catch (Exception e) {
            throw new InvalidKeySpecException(e.toString());
        }
    }
    throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName());
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException)

Example 68 with PrivateKey

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

the class KeyStorePrivateKeyEntryTest method testGetCertificateChain.

public void testGetCertificateChain() throws Exception {
    String certificateData = "-----BEGIN CERTIFICATE-----\n" + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" + "-----END CERTIFICATE-----\n";
    ByteArrayInputStream certArray;
    {
        try {
            certArray = new ByteArrayInputStream(certificateData.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate certificate = cf.generateCertificate(certArray);
    assertTrue(certificate instanceof X509Certificate);
    String algorithm = certificate.getPublicKey().getAlgorithm();
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();
    // If all the certificate in the chain is X509Certificate,
    // KeyStore.PrivateKeyEntry will return a X509Certificate array.
    KeyStore.PrivateKeyEntry privateKeyEntry = new KeyStore.PrivateKeyEntry(privateKey, new Certificate[] { certificate });
    Certificate[] chain = privateKeyEntry.getCertificateChain();
    assertTrue(chain instanceof X509Certificate[]);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeyPairGenerator(java.security.KeyPairGenerator) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 69 with PrivateKey

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

the class KeyStore2Test method test_getCreationDate.

/**
     * java.security.KeyStore#getCreationDate(String)
     */
public void test_getCreationDate() throws Exception {
    String type = "DSA";
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCreationDate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, pssWord);
    assertNull(keyTest.getCreationDate(""));
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
    Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
    PrivateKey privateKey1 = KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0")));
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
    keyTest.setEntry("alias1", pke, pp);
    keyTest.setEntry("alias2", pke1, pp);
    Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int dayExpected = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthExpected = Calendar.getInstance().get(Calendar.MONTH);
    int yearExpected = Calendar.getInstance().get(Calendar.YEAR);
    int hourExpected = Calendar.getInstance().get(Calendar.HOUR);
    int minuteExpected = Calendar.getInstance().get(Calendar.MINUTE);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias1").getTime());
    int dayActual1 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual1 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual1 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual1 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual1 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual1);
    assertEquals(monthExpected, monthActual1);
    assertEquals(yearExpected, yearActual1);
    assertEquals(hourExpected, hourActual1);
    assertEquals(minuteExpected, minuteActual1);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias2").getTime());
    int dayActual2 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual2 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual2 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual2 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual2 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual2);
    assertEquals(monthExpected, monthActual2);
    assertEquals(yearExpected, yearActual2);
    assertEquals(hourExpected, hourActual2);
    assertEquals(minuteExpected, minuteActual2);
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 70 with PrivateKey

use of java.security.PrivateKey in project dbeaver by serge-rider.

the class DefaultCertificateStorage method addCertificate.

@Override
public void addCertificate(DBPDataSourceContainer dataSource, String certType, byte[] caCertData, byte[] clientCertData, byte[] keyData) throws DBException {
    final KeyStore keyStore = getKeyStore(dataSource, certType);
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        List<Certificate> certChain = new ArrayList<>();
        if (caCertData != null) {
            Certificate caCert = cf.generateCertificate(new ByteArrayInputStream(caCertData));
            keyStore.setCertificateEntry("ca-cert", caCert);
        //certChain.add(caCert);
        }
        if (clientCertData != null) {
            Certificate clientCert = cf.generateCertificate(new ByteArrayInputStream(clientCertData));
            keyStore.setCertificateEntry("client-cert", clientCert);
            certChain.add(clientCert);
        }
        if (keyData != null) {
            PrivateKey privateKey = loadPrivateKeyFromPEM(keyData);
            keyStore.setKeyEntry("key-cert", privateKey, DEFAULT_PASSWORD, certChain.toArray(new Certificate[certChain.size()]));
        }
        saveKeyStore(dataSource, certType, keyStore);
    } catch (Throwable e) {
        throw new DBException("Error adding certificate to keystore", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) Certificate(java.security.cert.Certificate)

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