Search in sources :

Example 6 with ContentAlgorithm

use of org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm in project cxf by apache.

the class JweUtils method loadEncryptionProvider.

public static JweEncryptionProvider loadEncryptionProvider(Properties props, Message m, JweHeaders headers) {
    KeyEncryptionProvider keyEncryptionProvider = loadKeyEncryptionProvider(props, m, headers);
    ContentAlgorithm contentAlgo = getContentEncryptionAlgorithm(m, props, null, ContentAlgorithm.A128GCM);
    if (m != null) {
        m.put(JoseConstants.RSSEC_ENCRYPTION_CONTENT_ALGORITHM, contentAlgo.getJwaName());
    }
    ContentEncryptionProvider ctEncryptionProvider = null;
    if (KeyAlgorithm.DIRECT == keyEncryptionProvider.getAlgorithm()) {
        JsonWebKey jwk = JwkUtils.loadJsonWebKey(m, props, KeyOperation.ENCRYPT);
        if (jwk != null) {
            contentAlgo = getContentEncryptionAlgorithm(m, props, jwk.getAlgorithm() != null ? ContentAlgorithm.getAlgorithm(jwk.getAlgorithm()) : null, contentAlgo);
            ctEncryptionProvider = getContentEncryptionProvider(jwk, contentAlgo);
        }
    }
    String compression = props.getProperty(JoseConstants.RSSEC_ENCRYPTION_ZIP_ALGORITHM);
    return createJweEncryptionProvider(keyEncryptionProvider, ctEncryptionProvider, contentAlgo, compression, headers);
}
Also used : ContentAlgorithm(org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm) JsonWebKey(org.apache.cxf.rs.security.jose.jwk.JsonWebKey)

Example 7 with ContentAlgorithm

use of org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm in project cxf by apache.

the class JweUtils method getContentEncryptionProvider.

public static ContentEncryptionProvider getContentEncryptionProvider(JsonWebKey jwk, ContentAlgorithm defaultAlgorithm) {
    ContentAlgorithm ctAlgo = jwk.getAlgorithm() == null ? defaultAlgorithm : getContentAlgo(jwk.getAlgorithm());
    KeyType keyType = jwk.getKeyType();
    if (KeyType.OCTET == keyType) {
        return getContentEncryptionProvider(JwkUtils.toSecretKey(jwk), ctAlgo);
    }
    return null;
}
Also used : KeyType(org.apache.cxf.rs.security.jose.jwk.KeyType) ContentAlgorithm(org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm)

Example 8 with ContentAlgorithm

use of org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm in project cxf by apache.

the class JweCompactReaderWriterTest method decrypt.

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);
}
Also used : ContentAlgorithm(org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 9 with ContentAlgorithm

use of org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm in project cxf by apache.

the class JweJsonConsumerTest method doTestMultipleRecipients.

private void doTestMultipleRecipients(String jweJson) {
    final String text = "The true sign of intelligence is not knowledge but imagination.";
    SecretKey wrapperKey1 = CryptoUtils.createSecretKeySpec(JweJsonProducerTest.WRAPPER_BYTES1, "AES");
    SecretKey wrapperKey2 = CryptoUtils.createSecretKeySpec(JweJsonProducerTest.WRAPPER_BYTES2, "AES");
    JweJsonConsumer consumer = new JweJsonConsumer(jweJson);
    KeyAlgorithm keyAlgo = consumer.getSharedUnprotectedHeader().getKeyEncryptionAlgorithm();
    ContentAlgorithm ctAlgo = consumer.getProtectedHeader().getContentEncryptionAlgorithm();
    // Recipient 1
    JweDecryptionProvider jwe1 = JweUtils.createJweDecryptionProvider(wrapperKey1, keyAlgo, ctAlgo);
    JweDecryptionOutput out1 = consumer.decryptWith(jwe1, Collections.singletonMap("kid", "key1"));
    assertEquals(text, out1.getContentText());
    // Recipient 2
    JweDecryptionProvider jwe2 = JweUtils.createJweDecryptionProvider(wrapperKey2, keyAlgo, ctAlgo);
    JweDecryptionOutput out2 = consumer.decryptWith(jwe2, Collections.singletonMap("kid", "key2"));
    assertEquals(text, out2.getContentText());
    // Extra AAD
    assertEquals(JweJsonProducerTest.EXTRA_AAD_SOURCE, consumer.getAadText());
}
Also used : SecretKey(javax.crypto.SecretKey) ContentAlgorithm(org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm) KeyAlgorithm(org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm)

Example 10 with ContentAlgorithm

use of org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm in project cxf by apache.

the class EcdhDirectKeyJweDecryption method getDecryptedContentEncryptionKeyFromHeaders.

protected static byte[] getDecryptedContentEncryptionKeyFromHeaders(JweHeaders headers, ECPrivateKey privateKey) {
    ContentAlgorithm jwtAlgo = headers.getContentEncryptionAlgorithm();
    JsonWebKey publicJwk = headers.getJsonWebKey("epk");
    String apuHeader = (String) headers.getHeader("apu");
    byte[] apuBytes = apuHeader == null ? null : JoseUtils.decode(apuHeader);
    String apvHeader = (String) headers.getHeader("apv");
    byte[] apvBytes = apvHeader == null ? null : JoseUtils.decode(apvHeader);
    return JweUtils.getECDHKey(privateKey, JwkUtils.toECPublicKey(publicJwk), apuBytes, apvBytes, jwtAlgo.getJwaName(), jwtAlgo.getKeySizeBits());
}
Also used : ContentAlgorithm(org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm) JsonWebKey(org.apache.cxf.rs.security.jose.jwk.JsonWebKey)

Aggregations

ContentAlgorithm (org.apache.cxf.rs.security.jose.jwa.ContentAlgorithm)10 JsonWebKey (org.apache.cxf.rs.security.jose.jwk.JsonWebKey)5 Message (org.apache.cxf.message.Message)4 KeyAlgorithm (org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm)4 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 Properties (java.util.Properties)2 SecretKey (javax.crypto.SecretKey)2 KeyType (org.apache.cxf.rs.security.jose.jwk.KeyType)2 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 PrivateKeyPasswordProvider (org.apache.cxf.rs.security.jose.common.PrivateKeyPasswordProvider)1 ContentEncryptionProvider (org.apache.cxf.rs.security.jose.jwe.ContentEncryptionProvider)1 JweDecryptionOutput (org.apache.cxf.rs.security.jose.jwe.JweDecryptionOutput)1 JweDecryptionProvider (org.apache.cxf.rs.security.jose.jwe.JweDecryptionProvider)1 JweEncryption (org.apache.cxf.rs.security.jose.jwe.JweEncryption)1 JweEncryptionProvider (org.apache.cxf.rs.security.jose.jwe.JweEncryptionProvider)1