use of java.security.KeyPair in project azure-sdk-for-java by Azure.
the class CertificateOperationsTest method validatePem.
private void validatePem(CertificateBundle certificateBundle, String subjectName) throws CertificateException, IOException, KeyVaultErrorException, IllegalArgumentException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
// Load the CER part into X509Certificate object
X509Certificate x509Certificate = loadCerToX509Certificate(certificateBundle);
Assert.assertTrue(x509Certificate.getSubjectX500Principal().getName().equals(subjectName));
Assert.assertTrue(x509Certificate.getIssuerX500Principal().getName().equals(subjectName));
// Retrieve the secret backing the certificate
SecretIdentifier secretIdentifier = certificateBundle.secretIdentifier();
SecretBundle secret = keyVaultClient.getSecret(secretIdentifier.baseIdentifier());
Assert.assertTrue(secret.managed());
String secretValue = secret.value();
// Extract private key from PEM
PrivateKey secretPrivateKey = extractPrivateKeyFromPemContents(secretValue);
Assert.assertNotNull(secretPrivateKey);
// Extract certificates from PEM
List<X509Certificate> certificates = extractCertificatesFromPemContents(secretValue);
Assert.assertNotNull(certificates);
Assert.assertTrue(certificates.size() == 1);
// has the public key corresponding to the private key.
X509Certificate secretCertificate = certificates.get(0);
Assert.assertNotNull(secretCertificate);
Assert.assertTrue(secretCertificate.getSubjectX500Principal().getName().equals(x509Certificate.getSubjectX500Principal().getName()));
Assert.assertTrue(secretCertificate.getIssuerX500Principal().getName().equals(x509Certificate.getIssuerX500Principal().getName()));
Assert.assertTrue(secretCertificate.getSerialNumber().equals(x509Certificate.getSerialNumber()));
// Create a KeyPair with the private key from the KeyStore and public
// key from the certificate to verify they match
KeyPair keyPair = new KeyPair(secretCertificate.getPublicKey(), secretPrivateKey);
Assert.assertNotNull(keyPair);
verifyRSAKeyPair(keyPair);
}
use of java.security.KeyPair in project nhin-d by DirectProject.
the class CRLRevocationManager_getCrlFromUriTest method testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound.
public void testGetCrlFromUri_notInCache_loadFromCacheFile_assertCRLFound() throws Exception {
CRLRevocationManager.initCRLCacheLocation();
String uri = "http://localhost:8080/certs.crl";
X509CRL crl = (X509CRL) TestUtils.loadCRL("certs.crl");
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
KeyPair pair = kpGen.generateKeyPair();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 10);
X509V2CRLGenerator crlGen = new X509V2CRLGenerator();
crlGen.setIssuerDN(new X500Principal("CN=Test CRL"));
crlGen.setNextUpdate(cal.getTime());
crlGen.setSignatureAlgorithm("SHA256withRSAEncryption");
crlGen.setThisUpdate(Calendar.getInstance().getTime());
crlGen.addCRL(crl);
crl = crlGen.generate(pair.getPrivate(), "BC");
CRLRevocationManager.INSTANCE.writeCRLCacheFile(uri, crl);
X509CRL retCrl = CRLRevocationManager.getInstance().getCrlFromUri(uri);
assertNotNull(retCrl);
assertEquals(crl, retCrl);
}
use of java.security.KeyPair in project OpenAM by OpenRock.
the class AuthenticatorOathService method getEncryptionKeyPair.
private KeyPair getEncryptionKeyPair() {
try {
final KeyStore keyStore = new KeyStoreBuilder().withKeyStoreFile(new File(CollectionHelper.getMapAttr(options, OATH_KEYSTORE_FILE))).withPassword(CollectionHelper.getMapAttr(options, OATH_KEYSTORE_PASSWORD)).withKeyStoreType(KeyStoreType.valueOf(CollectionHelper.getMapAttr(options, OATH_KEYSTORE_TYPE))).build();
final Certificate cert = keyStore.getCertificate(CollectionHelper.getMapAttr(options, OATH_KEYSTORE_KEYPAIR_ALIAS));
final PublicKey publicKey = cert.getPublicKey();
final PrivateKey privateKey = (PrivateKey) keyStore.getKey(CollectionHelper.getMapAttr(options, OATH_KEYSTORE_KEYPAIR_ALIAS), CollectionHelper.getMapAttr(options, OATH_KEYSTORE_PRIVATEKEY_PASSWORD).toCharArray());
return new KeyPair(publicKey, privateKey);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("Invalid keystore location specified", e);
} catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException e) {
debug.error("AuthenticatorOathService.getEncryptionKeyPair(): Unable to load encryption key pair", e);
throw new IllegalStateException(e);
}
}
use of java.security.KeyPair in project GNS by MobilityFirst.
the class ByteificationComparisonFail method test_201_FromCommandPacket_128B_Signed.
/**
*
* @param byteificationComparison
* @throws UnsupportedEncodingException
* @throws JSONException
* @throws ClientException
* @throws NoSuchAlgorithmException
* @throws RequestParseException
*/
// FIXME: THIS TEST IS FAILING at new CommandPacket(bytes)
@Test
public void test_201_FromCommandPacket_128B_Signed(ByteificationComparisonFail byteificationComparison) throws UnsupportedEncodingException, JSONException, ClientException, NoSuchAlgorithmException, RequestParseException {
KeyPair keyPair = KeyPairGenerator.getInstance(GNSProtocol.RSA_ALGORITHM.toString()).generateKeyPair();
String guid = SharedGuidUtils.createGuidStringFromPublicKey(keyPair.getPublic().getEncoded());
// Squirrel this away now just in case the call below times out.
KeyPairUtils.saveKeyPair("gnsname", "alias", guid, keyPair);
GuidEntry querier = new GuidEntry("alias", guid, keyPair.getPublic(), keyPair.getPrivate());
CommandPacket packet = GNSCommand.fieldUpdate(querier, new String(Util.getRandomAlphanumericBytes(64)), new String(Util.getRandomAlphanumericBytes(64)));
String jsonBefore = packet.toJSONObject().toString();
byte[] bytes = packet.toBytes();
assert (jsonBefore.equals(packet.toJSONObject().toString()));
long startTime = System.nanoTime();
for (int i = 0; i < TEST_RUNS; i++) {
new CommandPacket(bytes);
}
long endTime = System.nanoTime();
double avg = (endTime - startTime) / (TEST_RUNS);
CommandPacket outputPacket = new CommandPacket(bytes);
System.out.println("Average time CommandPacket from bytes 128B Signed was " + avg + " nanoseconds.");
assert (Arrays.equals(bytes, outputPacket.toBytes()));
String canonicalJSON = CanonicalJSON.getCanonicalForm(jsonBefore);
String canonicalJSONOutput = CanonicalJSON.getCanonicalForm(outputPacket.toJSONObject());
//System.out.println(canonicalJSONOutput);
assert (canonicalJSON.equals(canonicalJSONOutput));
//CommandPacket outputPacket = CommandPacket.fromBytes(bytes);
//assert(packet.toJSONObject().toString().equals(outputPacket.toJSONObject().toString()));
}
use of java.security.KeyPair in project jdk8u_jdk by JetBrains.
the class SignatureTest method main.
public static void main(String[] args) throws Exception {
String testAlg = args[0];
int testSize = Integer.parseInt(args[1]);
byte[] data = new byte[100];
RandomFactory.getRandom().nextBytes(data);
// create a key pair
KeyPair kpair = generateKeys(KEYALG, testSize);
Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
// For signature algorithm, create and verify a signature
Arrays.stream(privs).forEach(priv -> Arrays.stream(pubs).forEach(pub -> {
try {
checkSignature(data, (PublicKey) pub, (PrivateKey) priv, testAlg);
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | NoSuchProviderException ex) {
throw new RuntimeException(ex);
}
}));
}
Aggregations