Java Code Examples for org.apache.cxf.rt.security.crypto.CryptoUtils#getRSAPrivateKey()

The following examples show how to use org.apache.cxf.rt.security.crypto.CryptoUtils#getRSAPrivateKey() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: cxf   File: JwkUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static RSAPrivateKey toRSAPrivateKey(JsonWebKey jwk) {
    String encodedModulus = (String)jwk.getProperty(JsonWebKey.RSA_MODULUS);
    String encodedPrivateExponent = (String)jwk.getProperty(JsonWebKey.RSA_PRIVATE_EXP);
    String encodedPrimeP = (String)jwk.getProperty(JsonWebKey.RSA_FIRST_PRIME_FACTOR);
    if (encodedPrimeP == null) {
        return CryptoUtils.getRSAPrivateKey(encodedModulus, encodedPrivateExponent);
    }
    String encodedPublicExponent = (String)jwk.getProperty(JsonWebKey.RSA_PUBLIC_EXP);
    if (encodedPublicExponent == null) {
        throw new JoseException("JWK without the public exponent can not be converted to RSAPrivateKey");
    }
    String encodedPrimeQ = (String)jwk.getProperty(JsonWebKey.RSA_SECOND_PRIME_FACTOR);
    String encodedPrimeExpP = (String)jwk.getProperty(JsonWebKey.RSA_FIRST_PRIME_CRT);
    String encodedPrimeExpQ = (String)jwk.getProperty(JsonWebKey.RSA_SECOND_PRIME_CRT);
    String encodedCrtCoefficient = (String)jwk.getProperty(JsonWebKey.RSA_FIRST_CRT_COEFFICIENT);
    return CryptoUtils.getRSAPrivateKey(encodedModulus,
                                        encodedPublicExponent,
                                        encodedPrivateExponent,
                                        encodedPrimeP,
                                        encodedPrimeQ,
                                        encodedPrimeExpP,
                                        encodedPrimeExpQ,
                                        encodedCrtCoefficient);
}
 
Example 2
Source Project: cxf   File: JwsCompactReaderWriterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testJwsPsSha() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    try {
        JwsHeaders outHeaders = new JwsHeaders();
        outHeaders.setSignatureAlgorithm(SignatureAlgorithm.PS256);
        JwsCompactProducer producer = initSpecJwtTokenWriter(outHeaders);
        PrivateKey privateKey = CryptoUtils.getRSAPrivateKey(RSA_MODULUS_ENCODED, RSA_PRIVATE_EXPONENT_ENCODED);
        String signed = producer.signWith(
            new PrivateKeyJwsSignatureProvider(privateKey, SignatureAlgorithm.PS256));

        JwsJwtCompactConsumer jws = new JwsJwtCompactConsumer(signed);
        RSAPublicKey key = CryptoUtils.getRSAPublicKey(RSA_MODULUS_ENCODED, RSA_PUBLIC_EXPONENT_ENCODED);
        assertTrue(jws.verifySignatureWith(new PublicKeyJwsSignatureVerifier(key, SignatureAlgorithm.PS256)));
        JwtToken token = jws.getJwtToken();
        JwsHeaders inHeaders = new JwsHeaders(token.getJwsHeaders());
        assertEquals(SignatureAlgorithm.PS256,
                     inHeaders.getSignatureAlgorithm());
        validateSpecClaim(token.getClaims());
    } finally {
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
    }
}
 
Example 3
Source Project: cxf   File: JweCompactReaderWriterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEncryptDecryptRSA15WrapA128CBCHS256() throws Exception {
    final String specPlainText = "Live long and prosper.";

    RSAPublicKey publicKey = CryptoUtils.getRSAPublicKey(RSA_MODULUS_ENCODED_A1,
                                                         RSA_PUBLIC_EXPONENT_ENCODED_A1);

    KeyEncryptionProvider keyEncryption = new RSAKeyEncryptionAlgorithm(publicKey,
                                                                         KeyAlgorithm.RSA1_5);

    JweEncryptionProvider encryption = new AesCbcHmacJweEncryption(ContentAlgorithm.A128CBC_HS256,
                                                       CONTENT_ENCRYPTION_KEY_A3,
                                                       INIT_VECTOR_A3,
                                                       keyEncryption);
    String jweContent = encryption.encrypt(specPlainText.getBytes(StandardCharsets.UTF_8), null);

    RSAPrivateKey privateKey = CryptoUtils.getRSAPrivateKey(RSA_MODULUS_ENCODED_A1,
                                                            RSA_PRIVATE_EXPONENT_ENCODED_A1);
    KeyDecryptionProvider keyDecryption = new RSAKeyDecryptionAlgorithm(privateKey,
                                                                         KeyAlgorithm.RSA1_5);
    JweDecryptionProvider decryption = new AesCbcHmacJweDecryption(keyDecryption);
    String decryptedText = decryption.decrypt(jweContent).getContentText();
    assertEquals(specPlainText, decryptedText);
}
 
Example 4
Source Project: cxf   File: JwsCompactReaderWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteJwsSignedByPrivateKey() throws Exception {
    JwsHeaders headers = new JwsHeaders();
    headers.setSignatureAlgorithm(SignatureAlgorithm.RS256);
    JwsCompactProducer jws = initSpecJwtTokenWriter(headers);
    PrivateKey key = CryptoUtils.getRSAPrivateKey(RSA_MODULUS_ENCODED, RSA_PRIVATE_EXPONENT_ENCODED);
    jws.signWith(new PrivateKeyJwsSignatureProvider(key, SignatureAlgorithm.RS256));

    assertEquals(ENCODED_TOKEN_SIGNED_BY_PRIVATE_KEY, jws.getSignedEncodedJws());
}
 
Example 5
Source Project: cxf   File: JweCompactReaderWriterTest.java    License: Apache License 2.0 5 votes vote down vote up
private void decrypt(String jweContent, String plainContent, boolean unwrap) throws Exception {
    RSAPrivateKey privateKey = CryptoUtils.getRSAPrivateKey(RSA_MODULUS_ENCODED_A1,
                                                            RSA_PRIVATE_EXPONENT_ENCODED_A1);
    ContentAlgorithm algo = Cipher.getMaxAllowedKeyLength("AES") > 128
        ? ContentAlgorithm.A256GCM : ContentAlgorithm.A128GCM;
    JweDecryptionProvider decryptor = new JweDecryption(new RSAKeyDecryptionAlgorithm(privateKey),
                                          new AesGcmContentDecryptionAlgorithm(algo));
    String decryptedText = decryptor.decrypt(jweContent).getContentText();
    assertEquals(decryptedText, plainContent);
}