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);
}
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;
}
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);
}
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());
}
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());
}
Aggregations