Search in sources :

Example 1 with ECDSAPrivateKey

use of io.jans.as.model.crypto.signature.ECDSAPrivateKey in project jans by JanssenProject.

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(io.jans.as.model.crypto.signature.ECDSAKeyFactory) ECDSASigner(io.jans.as.model.jws.ECDSASigner) ECDSAPrivateKey(io.jans.as.model.crypto.signature.ECDSAPrivateKey) ECDSAPublicKey(io.jans.as.model.crypto.signature.ECDSAPublicKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Example 2 with ECDSAPrivateKey

use of io.jans.as.model.crypto.signature.ECDSAPrivateKey in project jans by JanssenProject.

the class ECDSASigner method generateSignature.

/**
 * Generating a signature, using URL safe based format.
 */
@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }
    if (ecdsaPrivateKey == null) {
        throw new SignatureException("The ECDSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }
    try {
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(getSignatureAlgorithm().getCurve().getName());
        ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
        Signature signer = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signer.initSign(privateKey);
        signer.update(signingInput.getBytes(StandardCharsets.UTF_8));
        byte[] signature = signer.sign();
        if (AlgorithmFamily.EC.equals(getSignatureAlgorithm().getFamily())) {
            int signatureLenght = ECDSA.getSignatureByteArrayLength(JWSAlgorithm.parse(getSignatureAlgorithm().getName()));
            signature = ECDSA.transcodeSignatureToConcat(signature, signatureLenght);
        }
        return Base64Util.base64urlencode(signature);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}
Also used : ECPrivateKeySpec(org.bouncycastle.jce.spec.ECPrivateKeySpec) ECDSAPrivateKey(io.jans.as.model.crypto.signature.ECDSAPrivateKey) PrivateKey(java.security.PrivateKey) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) Signature(java.security.Signature) SignatureException(java.security.SignatureException) KeyFactory(java.security.KeyFactory) ECPoint(org.bouncycastle.math.ec.ECPoint) SignatureException(java.security.SignatureException)

Example 3 with ECDSAPrivateKey

use of io.jans.as.model.crypto.signature.ECDSAPrivateKey in project jans by JanssenProject.

the class SignatureTest method generateES384Keys.

@Test
public void generateES384Keys() throws Exception {
    showTitle("TEST: generateES384Keys");
    KeyFactory<ECDSAPrivateKey, ECDSAPublicKey> keyFactory = new ECDSAKeyFactory(SignatureAlgorithm.ES384, "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.ES384, privateKey);
    String signature = ecdsaSigner1.generateSignature(signingInput);
    ECDSASigner ecdsaSigner2 = new ECDSASigner(SignatureAlgorithm.ES384, publicKey);
    assertTrue(ecdsaSigner2.validateSignature(signingInput, signature));
    ECDSASigner ecdsaSigner3 = new ECDSASigner(SignatureAlgorithm.ES384, certificate);
    assertTrue(ecdsaSigner3.validateSignature(signingInput, signature));
}
Also used : ECDSAKeyFactory(io.jans.as.model.crypto.signature.ECDSAKeyFactory) ECDSASigner(io.jans.as.model.jws.ECDSASigner) ECDSAPrivateKey(io.jans.as.model.crypto.signature.ECDSAPrivateKey) ECDSAPublicKey(io.jans.as.model.crypto.signature.ECDSAPublicKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Example 4 with ECDSAPrivateKey

use of io.jans.as.model.crypto.signature.ECDSAPrivateKey in project jans by JanssenProject.

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(io.jans.as.model.crypto.signature.ECDSAKeyFactory) ECDSASigner(io.jans.as.model.jws.ECDSASigner) ECDSAPrivateKey(io.jans.as.model.crypto.signature.ECDSAPrivateKey) ECDSAPublicKey(io.jans.as.model.crypto.signature.ECDSAPublicKey) Certificate(io.jans.as.model.crypto.Certificate) Test(org.testng.annotations.Test) BaseTest(io.jans.as.server.BaseTest)

Aggregations

ECDSAPrivateKey (io.jans.as.model.crypto.signature.ECDSAPrivateKey)4 Certificate (io.jans.as.model.crypto.Certificate)3 ECDSAKeyFactory (io.jans.as.model.crypto.signature.ECDSAKeyFactory)3 ECDSAPublicKey (io.jans.as.model.crypto.signature.ECDSAPublicKey)3 ECDSASigner (io.jans.as.model.jws.ECDSASigner)3 BaseTest (io.jans.as.server.BaseTest)3 Test (org.testng.annotations.Test)3 KeyFactory (java.security.KeyFactory)1 PrivateKey (java.security.PrivateKey)1 Signature (java.security.Signature)1 SignatureException (java.security.SignatureException)1 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)1 ECPrivateKeySpec (org.bouncycastle.jce.spec.ECPrivateKeySpec)1 ECPoint (org.bouncycastle.math.ec.ECPoint)1