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) {
}
}
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());
}
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[]);
}
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) {
}
}
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);
}
}
Aggregations