Search in sources :

Example 6 with Certificate

use of org.gluu.oxauth.model.crypto.Certificate in project oxAuth by GluuFederation.

the class ECDSAKeyFactory method generateV3Certificate.

public Certificate generateV3Certificate(Date startDate, Date expirationDate, String dnName) throws CertificateEncodingException, InvalidKeyException, IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException {
    // Create certificate
    // serial number for certificate
    BigInteger serialNumber = new BigInteger(1024, new Random());
    X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
    X500Principal principal = new X500Principal(dnName);
    certGen.setSerialNumber(serialNumber);
    certGen.setIssuerDN(principal);
    certGen.setNotBefore(startDate);
    certGen.setNotAfter(expirationDate);
    // note: same as issuer
    certGen.setSubjectDN(principal);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm(signatureAlgorithm.getAlgorithm());
    X509Certificate x509Certificate = certGen.generate(keyPair.getPrivate(), "BC");
    return new Certificate(signatureAlgorithm, x509Certificate);
}
Also used : X509V3CertificateGenerator(org.bouncycastle.x509.X509V3CertificateGenerator) Random(java.util.Random) BigInteger(java.math.BigInteger) X500Principal(javax.security.auth.x500.X500Principal) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(org.gluu.oxauth.model.crypto.Certificate)

Example 7 with Certificate

use of org.gluu.oxauth.model.crypto.Certificate in project oxAuth by GluuFederation.

the class SignatureTest method generateES256Keys.

@Test
public void generateES256Keys() throws Exception {
    showTitle("TEST: generateES256Keys");
    KeyFactory<ECDSAPrivateKey, ECDSAPublicKey> keyFactory = new ECDSAKeyFactory(SignatureAlgorithm.ES256, "CN=Test CA Certificate");
    Key<ECDSAPrivateKey, ECDSAPublicKey> key = keyFactory.getKey();
    ECDSAPrivateKey privateKey = key.getPrivateKey();
    ECDSAPublicKey publicKey = key.getPublicKey();
    Certificate certificate = key.getCertificate();
    System.out.println(key);
    String signingInput = "Hello World!";
    ECDSASigner ecdsaSigner1 = new ECDSASigner(SignatureAlgorithm.ES256, privateKey);
    String signature = ecdsaSigner1.generateSignature(signingInput);
    ECDSASigner ecdsaSigner2 = new ECDSASigner(SignatureAlgorithm.ES256, publicKey);
    assertTrue(ecdsaSigner2.validateSignature(signingInput, signature));
    ECDSASigner ecdsaSigner3 = new ECDSASigner(SignatureAlgorithm.ES256, certificate);
    assertTrue(ecdsaSigner3.validateSignature(signingInput, signature));
}
Also used : ECDSAKeyFactory(org.gluu.oxauth.model.crypto.signature.ECDSAKeyFactory) ECDSASigner(org.gluu.oxauth.model.jws.ECDSASigner) ECDSAPrivateKey(org.gluu.oxauth.model.crypto.signature.ECDSAPrivateKey) ECDSAPublicKey(org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey) Certificate(org.gluu.oxauth.model.crypto.Certificate) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 8 with Certificate

use of org.gluu.oxauth.model.crypto.Certificate in project oxAuth by GluuFederation.

the class SignatureTest method generateES512Keys.

@Test
public void generateES512Keys() throws Exception {
    showTitle("TEST: generateES512Keys");
    KeyFactory<ECDSAPrivateKey, ECDSAPublicKey> keyFactory = new ECDSAKeyFactory(SignatureAlgorithm.ES512, "CN=Test CA Certificate");
    ECDSAPrivateKey privateKey = keyFactory.getPrivateKey();
    ECDSAPublicKey publicKey = keyFactory.getPublicKey();
    Certificate certificate = keyFactory.getCertificate();
    System.out.println("PRIVATE KEY");
    System.out.println(privateKey);
    System.out.println("PUBLIC KEY");
    System.out.println(publicKey);
    System.out.println("CERTIFICATE");
    System.out.println(certificate);
    String signingInput = "Hello World!";
    ECDSASigner ecdsaSigner1 = new ECDSASigner(SignatureAlgorithm.ES512, privateKey);
    String signature = ecdsaSigner1.generateSignature(signingInput);
    ECDSASigner ecdsaSigner2 = new ECDSASigner(SignatureAlgorithm.ES512, publicKey);
    assertTrue(ecdsaSigner2.validateSignature(signingInput, signature));
    ECDSASigner ecdsaSigner3 = new ECDSASigner(SignatureAlgorithm.ES512, certificate);
    assertTrue(ecdsaSigner3.validateSignature(signingInput, signature));
}
Also used : ECDSAKeyFactory(org.gluu.oxauth.model.crypto.signature.ECDSAKeyFactory) ECDSASigner(org.gluu.oxauth.model.jws.ECDSASigner) ECDSAPrivateKey(org.gluu.oxauth.model.crypto.signature.ECDSAPrivateKey) ECDSAPublicKey(org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey) Certificate(org.gluu.oxauth.model.crypto.Certificate) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 9 with Certificate

use of org.gluu.oxauth.model.crypto.Certificate in project oxAuth by GluuFederation.

the class SignatureTest method generateRS384Keys.

@Test
public void generateRS384Keys() throws Exception {
    showTitle("TEST: generateRS384Keys");
    KeyFactory<RSAPrivateKey, RSAPublicKey> keyFactory = new RSAKeyFactory(SignatureAlgorithm.RS384, "CN=Test CA Certificate");
    Key<RSAPrivateKey, RSAPublicKey> key = keyFactory.getKey();
    RSAPrivateKey privateKey = key.getPrivateKey();
    RSAPublicKey publicKey = key.getPublicKey();
    Certificate certificate = key.getCertificate();
    System.out.println(key);
    String signingInput = "Hello World!";
    RSASigner rsaSigner1 = new RSASigner(SignatureAlgorithm.RS384, privateKey);
    String signature = rsaSigner1.generateSignature(signingInput);
    RSASigner rsaSigner2 = new RSASigner(SignatureAlgorithm.RS384, publicKey);
    assertTrue(rsaSigner2.validateSignature(signingInput, signature));
    RSASigner rsaSigner3 = new RSASigner(SignatureAlgorithm.RS384, certificate);
    assertTrue(rsaSigner3.validateSignature(signingInput, signature));
}
Also used : RSAKeyFactory(org.gluu.oxauth.model.crypto.signature.RSAKeyFactory) RSAPublicKey(org.gluu.oxauth.model.crypto.signature.RSAPublicKey) RSASigner(org.gluu.oxauth.model.jws.RSASigner) RSAPrivateKey(org.gluu.oxauth.model.crypto.signature.RSAPrivateKey) Certificate(org.gluu.oxauth.model.crypto.Certificate) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Certificate (org.gluu.oxauth.model.crypto.Certificate)9 BaseTest (org.gluu.oxauth.BaseTest)6 Test (org.testng.annotations.Test)6 ECDSAPublicKey (org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey)5 ECDSAKeyFactory (org.gluu.oxauth.model.crypto.signature.ECDSAKeyFactory)4 ECDSAPrivateKey (org.gluu.oxauth.model.crypto.signature.ECDSAPrivateKey)4 RSAPublicKey (org.gluu.oxauth.model.crypto.signature.RSAPublicKey)4 RSAKeyFactory (org.gluu.oxauth.model.crypto.signature.RSAKeyFactory)3 RSAPrivateKey (org.gluu.oxauth.model.crypto.signature.RSAPrivateKey)3 ECDSASigner (org.gluu.oxauth.model.jws.ECDSASigner)3 RSASigner (org.gluu.oxauth.model.jws.RSASigner)3 BigInteger (java.math.BigInteger)2 X509Certificate (java.security.cert.X509Certificate)2 JSONObject (org.json.JSONObject)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Random (java.util.Random)1