Repository: cxf Updated Branches: refs/heads/master 667214435 -> 7d5f8b519
Update to OAuth2 CryptoUtils to get the authentication block correctly calculated if multi-part encryption is used Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/7d5f8b51 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7d5f8b51 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7d5f8b51 Branch: refs/heads/master Commit: 7d5f8b519fd02633eb0d40b017a747b3ce7b0321 Parents: 6672144 Author: Sergey Beryozkin <[email protected]> Authored: Fri Jun 20 12:46:18 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Jun 20 12:46:18 2014 +0100 ---------------------------------------------------------------------- .../oauth2/utils/crypto/CryptoUtils.java | 29 ++++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/7d5f8b51/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/crypto/CryptoUtils.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/crypto/CryptoUtils.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/crypto/CryptoUtils.java index e46409c..1d59467 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/crypto/CryptoUtils.java +++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/crypto/CryptoUtils.java @@ -497,9 +497,17 @@ public final class CryptoUtils { String wrappedKeyAlgo, Key unwrapperKey, KeyProperties keyProps) throws SecurityException { + return (SecretKey)unwrapKey(wrappedBytes, wrappedKeyAlgo, unwrapperKey, keyProps, Cipher.SECRET_KEY); + } + + public static Key unwrapKey(byte[] wrappedBytes, + String wrappedKeyAlgo, + Key unwrapperKey, + KeyProperties keyProps, + int wrappedKeyType) throws SecurityException { try { Cipher c = initCipher(unwrapperKey, keyProps, Cipher.UNWRAP_MODE); - return (SecretKey)c.unwrap(wrappedBytes, wrappedKeyAlgo, Cipher.SECRET_KEY); + return c.unwrap(wrappedBytes, wrappedKeyAlgo, wrappedKeyType); } catch (Exception ex) { throw new SecurityException(ex); } @@ -523,9 +531,12 @@ public final class CryptoUtils { if (blockSize == -1) { blockSize = secretKey instanceof PublicKey ? 117 : 128; } + boolean updateRequired = keyProps != null && keyProps.getAdditionalData() != null; int offset = 0; for (; offset + blockSize < bytes.length; offset += blockSize) { - result = addToResult(result, c.doFinal(bytes, offset, blockSize)); + byte[] next = !updateRequired ? c.doFinal(bytes, offset, blockSize) + : c.update(bytes, offset, blockSize); + result = addToResult(result, next); } if (offset < bytes.length) { result = addToResult(result, c.doFinal(bytes, offset, bytes.length - offset)); @@ -574,10 +585,16 @@ public final class CryptoUtils { } private static byte[] addToResult(byte[] prefix, byte[] suffix) { - byte[] result = new byte[prefix.length + suffix.length]; - System.arraycopy(prefix, 0, result, 0, prefix.length); - System.arraycopy(suffix, 0, result, prefix.length, suffix.length); - return result; + if (suffix == null || suffix.length == 0) { + return prefix; + } else if (prefix.length == 0) { + return suffix; + } else { + byte[] result = new byte[prefix.length + suffix.length]; + System.arraycopy(prefix, 0, result, 0, prefix.length); + System.arraycopy(suffix, 0, result, prefix.length, suffix.length); + return result; + } } public static SecretKey decodeSecretKey(String encodedSecretKey) throws SecurityException {
