use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project athenz by yahoo.
the class CryptoTest method testGenerateX509CertificateReqPrivateKey.
@Test
public void testGenerateX509CertificateReqPrivateKey() throws IOException {
Path path = Paths.get("src/test/resources/valid.csr");
String certStr = new String(Files.readAllBytes(path));
PKCS10CertificationRequest certReq = Crypto.getPKCS10CertRequest(certStr);
X509Certificate caCertificate = Crypto.loadX509Certificate(ecPublicX509Cert);
PrivateKey caPrivateKey = Crypto.loadPrivateKey(rsaPrivateKey);
X509Certificate cert = Crypto.generateX509Certificate(certReq, caPrivateKey, caCertificate, 600, false);
assertNotNull(cert);
assertEquals(cert.getIssuerX500Principal().getName(), "CN=athenz.syncer,O=My Test Company,L=Sunnyvale,ST=CA,C=US");
}
use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project athenz by yahoo.
the class CryptoTest method testGenerateX509CertificateInvalid.
@Test
public void testGenerateX509CertificateInvalid() throws IOException {
Path path = Paths.get("src/test/resources/valid.csr");
String certStr = new String(Files.readAllBytes(path));
PKCS10CertificationRequest certReq = Crypto.getPKCS10CertRequest(certStr);
PrivateKey caPrivateKey = Crypto.loadPrivateKey(rsaPrivateKey);
try {
Crypto.generateX509Certificate(certReq, caPrivateKey, (X500Name) null, 600, true);
fail();
} catch (CryptoException ex) {
assertTrue(true, "Caught excepted exception");
}
}
use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project athenz by yahoo.
the class CryptoTest method validateJWSDocumentInvalidHeader.
@Test
public void validateJWSDocumentInvalidHeader() {
Function<String, PublicKey> keyGetter = (String keyId) -> null;
final Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding();
final String protectedHeader = "{\"alg\":\"ES256\"}";
final byte[] encodedHeader = encoder.encode(protectedHeader.getBytes(StandardCharsets.UTF_8));
final String payload = "{\"domainName\":\"athenz\"}";
final byte[] encodedPayload = encoder.encode(payload.getBytes(StandardCharsets.UTF_8));
PrivateKey privateKey = Crypto.loadPrivateKey(rsaPrivateKey);
final byte[] signature = encoder.encode(Crypto.sign(Bytes.concat(encodedHeader, PERIOD, encodedPayload), privateKey, Crypto.SHA256));
assertFalse(Crypto.validateJWSDocument("invalid-header", new String(encodedPayload), new String(signature), keyGetter));
}
use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project athenz by yahoo.
the class CryptoTest method testX509CSRrequestWithPrivateKeyOnly.
@Test(dataProvider = "x500Principal")
public void testX509CSRrequestWithPrivateKeyOnly(String x500Principal, boolean badRequest) {
PrivateKey privateKey = Crypto.loadPrivateKey(rsaPrivateKey);
String certRequest = null;
GeneralName otherName1 = new GeneralName(GeneralName.otherName, new DERIA5String("role1"));
GeneralName otherName2 = new GeneralName(GeneralName.otherName, new DERIA5String("role2"));
GeneralName[] sanArray = new GeneralName[] { otherName1, otherName2 };
try {
certRequest = Crypto.generateX509CSR(privateKey, x500Principal, sanArray);
} catch (Exception e) {
if (!badRequest) {
fail("Should not have failed to create csr");
}
}
if (!badRequest) {
// Now validate the csr
Crypto.getPKCS10CertRequest(certRequest);
}
}
use of org.bouncycastle.asn1.pkcs.RSAPrivateKey in project jruby-openssl by jruby.
the class SignerInfoWithPkey method set.
/* c: PKCS7_SIGNER_INFO_set
*
*/
public void set(X509AuxCertificate x509, PrivateKey pkey, MessageDigest dgst) throws PKCS7Exception {
boolean dsa = (pkey instanceof DSAPrivateKey) || (pkey instanceof ECPrivateKey);
version = new ASN1Integer(BigInteger.ONE);
X500Name issuer = X500Name.getInstance(x509.getIssuerX500Principal().getEncoded());
BigInteger serial = x509.getSerialNumber();
issuerAndSerialNumber = new IssuerAndSerialNumber(issuer, serial);
this.pkey = pkey;
if (dsa) {
digAlgorithm = new AlgorithmIdentifier(OID_sha1);
} else {
digAlgorithm = new AlgorithmIdentifier(ASN1Registry.nid2obj(EVP.type(dgst)));
}
if (pkey instanceof RSAPrivateKey) {
digEncryptionAlgorithm = new AlgorithmIdentifier(OID_rsaEncryption);
} else if (pkey instanceof DSAPrivateKey) {
digEncryptionAlgorithm = new AlgorithmIdentifier(OID_dsa);
} else if (pkey instanceof ECPrivateKey) {
digEncryptionAlgorithm = new AlgorithmIdentifier(OID_ecdsa_with_SHA1);
}
}
Aggregations