You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2014/09/05 15:25:14 UTC
git commit: [CXF-5954] Minor updates for JWK to RSAPrivateKey
conversions work with the spec examples
Repository: cxf
Updated Branches:
refs/heads/master 279da3256 -> 2c2311afd
[CXF-5954] Minor updates for JWK to RSAPrivateKey conversions work with the spec examples
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/2c2311af
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/2c2311af
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/2c2311af
Branch: refs/heads/master
Commit: 2c2311afd7a03f01ec9316a803a26d94d56a9161
Parents: 279da32
Author: Sergey Beryozkin <sb...@talend.com>
Authored: Fri Sep 5 14:24:57 2014 +0100
Committer: Sergey Beryozkin <sb...@talend.com>
Committed: Fri Sep 5 14:24:57 2014 +0100
----------------------------------------------------------------------
.../cxf/rs/security/oauth2/jwk/JsonWebKey.java | 21 ++++-
.../oauth2/utils/crypto/CryptoUtils.java | 95 +++++++++++++++-----
2 files changed, 90 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf/blob/2c2311af/rt/rs/security/oauth-parent/oauth2-jwt/src/main/java/org/apache/cxf/rs/security/oauth2/jwk/JsonWebKey.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2-jwt/src/main/java/org/apache/cxf/rs/security/oauth2/jwk/JsonWebKey.java b/rt/rs/security/oauth-parent/oauth2-jwt/src/main/java/org/apache/cxf/rs/security/oauth2/jwk/JsonWebKey.java
index 93c3a14..7e3e7a1 100644
--- a/rt/rs/security/oauth-parent/oauth2-jwt/src/main/java/org/apache/cxf/rs/security/oauth2/jwk/JsonWebKey.java
+++ b/rt/rs/security/oauth-parent/oauth2-jwt/src/main/java/org/apache/cxf/rs/security/oauth2/jwk/JsonWebKey.java
@@ -166,9 +166,26 @@ public class JsonWebKey extends AbstractJwtObject {
return CryptoUtils.getRSAPublicKey(encodedModulus, encodedPublicExponent);
}
public RSAPrivateKey toRSAPrivateKey() {
- String encodedPublicExponent = (String)super.getValue(RSA_MODULUS);
+ String encodedModulus = (String)super.getValue(RSA_MODULUS);
String encodedPrivateExponent = (String)super.getValue(RSA_PRIVATE_EXP);
- return CryptoUtils.getRSAPrivateKey(encodedPublicExponent, encodedPrivateExponent);
+ String encodedPrimeP = (String)super.getValue(RSA_FIRST_PRIME_FACTOR);
+ if (encodedPrimeP == null) {
+ return CryptoUtils.getRSAPrivateKey(encodedModulus, encodedPrivateExponent);
+ } else {
+ String encodedPublicExponent = (String)super.getValue(RSA_PUBLIC_EXP);
+ String encodedPrimeQ = (String)super.getValue(RSA_SECOND_PRIME_FACTOR);
+ String encodedPrimeExpP = (String)super.getValue(RSA_FIRST_PRIME_CRT);
+ String encodedPrimeExpQ = (String)super.getValue(RSA_SECOND_PRIME_CRT);
+ String encodedCrtCoefficient = (String)super.getValue(RSA_FIRST_CRT_COEFFICIENT);
+ return CryptoUtils.getRSAPrivateKey(encodedModulus,
+ encodedPublicExponent,
+ encodedPrivateExponent,
+ encodedPrimeP,
+ encodedPrimeQ,
+ encodedPrimeExpP,
+ encodedPrimeExpQ,
+ encodedCrtCoefficient);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/cxf/blob/2c2311af/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 6b8c777..dc2fef3 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
@@ -41,6 +41,7 @@ import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
+import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Properties;
@@ -286,6 +287,76 @@ public final class CryptoUtils {
throw new SecurityException(ex);
}
}
+
+ public static RSAPrivateKey getRSAPrivateKey(byte[] modulusBytes,
+ byte[] privateExponentBytes) {
+ BigInteger modulus = new BigInteger(1, modulusBytes);
+ BigInteger privateExponent = new BigInteger(1, privateExponentBytes);
+ try {
+ KeyFactory factory = KeyFactory.getInstance("RSA");
+ return (RSAPrivateKey)factory.generatePrivate(
+ new RSAPrivateKeySpec(modulus, privateExponent));
+ } catch (Exception ex) {
+ throw new SecurityException(ex);
+ }
+ }
+ //CHECKSTYLE:OFF
+ public static RSAPrivateKey getRSAPrivateKey(String encodedModulus,
+ String encodedPublicExponent,
+ String encodedPrivateExponent,
+ String encodedPrimeP,
+ String encodedPrimeQ,
+ String encodedPrimeExpP,
+ String encodedPrimeExpQ,
+ String encodedCrtCoefficient) {
+ //CHECKSTYLE:ON
+ try {
+ return getRSAPrivateKey(Base64UrlUtility.decode(encodedModulus),
+ Base64UrlUtility.decode(encodedPublicExponent),
+ Base64UrlUtility.decode(encodedPrivateExponent),
+ Base64UrlUtility.decode(encodedPrimeP),
+ Base64UrlUtility.decode(encodedPrimeQ),
+ Base64UrlUtility.decode(encodedPrimeExpP),
+ Base64UrlUtility.decode(encodedPrimeExpQ),
+ Base64UrlUtility.decode(encodedCrtCoefficient));
+ } catch (Exception ex) {
+ throw new SecurityException(ex);
+ }
+ }
+ //CHECKSTYLE:OFF
+ public static RSAPrivateKey getRSAPrivateKey(byte[] modulusBytes,
+ byte[] publicExponentBytes,
+ byte[] privateExponentBytes,
+ byte[] primePBytes,
+ byte[] primeQBytes,
+ byte[] primeExpPBytes,
+ byte[] primeExpQBytes,
+ byte[] crtCoefficientBytes) {
+ //CHECKSTYLE:ON
+ BigInteger modulus = new BigInteger(1, modulusBytes);
+ BigInteger publicExponent = new BigInteger(1, publicExponentBytes);
+ BigInteger privateExponent = new BigInteger(1, privateExponentBytes);
+ BigInteger primeP = new BigInteger(1, primePBytes);
+ BigInteger primeQ = new BigInteger(1, primeQBytes);
+ BigInteger primeExpP = new BigInteger(1, primeExpPBytes);
+ BigInteger primeExpQ = new BigInteger(1, primeExpQBytes);
+ BigInteger crtCoefficient = new BigInteger(1, crtCoefficientBytes);
+ try {
+ KeyFactory factory = KeyFactory.getInstance("RSA");
+ return (RSAPrivateKey)factory.generatePrivate(
+ new RSAPrivateCrtKeySpec(modulus,
+ publicExponent,
+ privateExponent,
+ primeP,
+ primeQ,
+ primeExpP,
+ primeExpQ,
+ crtCoefficient));
+ } catch (Exception ex) {
+ throw new SecurityException(ex);
+ }
+ }
+
public static ECPrivateKey getECPrivateKey(String encodedPrivateKey) {
try {
return getECPrivateKey(Base64UrlUtility.decode(encodedPrivateKey));
@@ -401,30 +472,6 @@ public final class CryptoUtils {
}
}
- public static RSAPrivateKey getRSAPrivateKey(byte[] modulusBytes,
- byte[] privateExponentBytes) {
- try {
- return getRSAPrivateKey(KeyFactory.getInstance("RSA"),
- modulusBytes,
- privateExponentBytes);
- } catch (Exception ex) {
- throw new SecurityException(ex);
- }
- }
-
- public static RSAPrivateKey getRSAPrivateKey(KeyFactory factory,
- byte[] modulusBytes,
- byte[] privateExponentBytes) {
- BigInteger modulus = new BigInteger(1, modulusBytes);
- BigInteger privateExponent = new BigInteger(1, privateExponentBytes);
- try {
- return (RSAPrivateKey)factory.generatePrivate(
- new RSAPrivateKeySpec(modulus, privateExponent));
- } catch (Exception ex) {
- throw new SecurityException(ex);
- }
- }
-
public static SecretKey getSecretKey(String symEncAlgo) throws SecurityException {
return getSecretKey(new KeyProperties(symEncAlgo));
}