cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5954] Minor updates for JWK to RSAPrivateKey conversions work with the spec examples
Date Fri, 05 Sep 2014 13:40:53 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 2c731ed48 -> 43df09350


[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/43df0935
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/43df0935
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/43df0935

Branch: refs/heads/3.0.x-fixes
Commit: 43df09350e7e6820558ccfdb419cc3f4df9bf569
Parents: 2c731ed
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Sep 5 14:24:57 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Sep 5 14:40:33 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/43df0935/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/43df0935/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 bffb504..721a149 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
@@ -43,6 +43,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;
@@ -292,6 +293,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));
@@ -415,30 +486,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));
     }


Mime
View raw message