cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: Prototyping few Java Key to JWK conversion methods with more to come
Date Tue, 04 Nov 2014 22:21:16 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 866b9cc09 -> cfde47b86


Prototyping few Java Key to JWK conversion methods with more to come


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/cfde47b8
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/cfde47b8
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/cfde47b8

Branch: refs/heads/3.0.x-fixes
Commit: cfde47b8640fb9f98d46387a58eeb300996e3aa5
Parents: 866b9cc
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Nov 4 22:19:17 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Nov 4 22:20:35 2014 +0000

----------------------------------------------------------------------
 .../cxf/rs/security/jose/jwk/JwkUtils.java      | 31 +++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/cfde47b8/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
index 883a2b4..6e4d4b2 100644
--- a/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
+++ b/rt/rs/security/jose/src/main/java/org/apache/cxf/rs/security/jose/jwk/JwkUtils.java
@@ -20,6 +20,7 @@ package org.apache.cxf.rs.security.jose.jwk;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigInteger;
 import java.net.URI;
 import java.security.interfaces.ECPrivateKey;
 import java.security.interfaces.ECPublicKey;
@@ -33,6 +34,7 @@ import java.util.Properties;
 import javax.crypto.SecretKey;
 
 import org.apache.cxf.Bus;
+import org.apache.cxf.common.util.Base64UrlUtility;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.crypto.CryptoUtils;
 import org.apache.cxf.helpers.IOUtils;
@@ -260,7 +262,7 @@ public final class JwkUtils {
         }
         return kid;
     }
-    public static PrivateKeyPasswordProvider loadPasswordProvider(Message m, Properties props,
String keyOper) {
+    private static PrivateKeyPasswordProvider loadPasswordProvider(Message m, Properties
props, String keyOper) {
         PrivateKeyPasswordProvider cb = 
             (PrivateKeyPasswordProvider)m.getContextualProperty(KeyManagementUtils.RSSEC_KEY_PSWD_PROVIDER);
         if (cb == null && keyOper != null) {
@@ -278,6 +280,12 @@ public final class JwkUtils {
         String encodedPublicExponent = (String)jwk.getProperty(JsonWebKey.RSA_PUBLIC_EXP);
         return CryptoUtils.getRSAPublicKey(encodedModulus, encodedPublicExponent);
     }
+    public static JsonWebKey fromRSAPublicKey(RSAPublicKey pk) {
+        JsonWebKey jwk = prepareRSAJwk(pk.getModulus());
+        String encodedPublicExponent = Base64UrlUtility.encode(pk.getPublicExponent().toByteArray());
+        jwk.setProperty(JsonWebKey.RSA_PUBLIC_EXP, encodedPublicExponent);
+        return jwk;
+    }
     public static RSAPrivateKey toRSAPrivateKey(JsonWebKey jwk) {
         String encodedModulus = (String)jwk.getProperty(JsonWebKey.RSA_MODULUS);
         String encodedPrivateExponent = (String)jwk.getProperty(JsonWebKey.RSA_PRIVATE_EXP);
@@ -300,6 +308,19 @@ public final class JwkUtils {
                                                 encodedCrtCoefficient);
         }
     }
+    public static JsonWebKey fromRSAPrivateKey(RSAPrivateKey pk) {
+        JsonWebKey jwk = prepareRSAJwk(pk.getModulus());
+        String encodedPrivateExponent = Base64UrlUtility.encode(pk.getPrivateExponent().toByteArray());
+        jwk.setProperty(JsonWebKey.RSA_PRIVATE_EXP, encodedPrivateExponent);
+        return jwk;
+    }
+    private static JsonWebKey prepareRSAJwk(BigInteger modulus) {
+        JsonWebKey jwk = new JsonWebKey();
+        jwk.setKeyType(JsonWebKey.KEY_TYPE_RSA);
+        String encodedModulus = Base64UrlUtility.encode(modulus.toByteArray());
+        jwk.setProperty(JsonWebKey.RSA_MODULUS, encodedModulus);
+        return jwk;
+    }
     public static ECPublicKey toECPublicKey(JsonWebKey jwk) {
         String eCurve = (String)jwk.getProperty(JsonWebKey.EC_CURVE);
         String encodedXCoord = (String)jwk.getProperty(JsonWebKey.EC_X_COORDINATE);
@@ -316,6 +337,14 @@ public final class JwkUtils {
         return CryptoUtils.createSecretKeySpec((String)jwk.getProperty(JsonWebKey.OCTET_KEY_VALUE),

                                                Algorithm.toJavaName(jwk.getAlgorithm()));
     }
+    public static JsonWebKey fromSecretKey(SecretKey secretKey) {
+        JsonWebKey jwk = new JsonWebKey();
+        jwk.setKeyType(JsonWebKey.KEY_TYPE_OCTET);
+        String encodedSecretKey = Base64UrlUtility.encode(secretKey.getEncoded());
+        jwk.setProperty(JsonWebKey.OCTET_KEY_VALUE, encodedSecretKey);
+        return jwk;
+    }
+    
     private static byte[] stringToBytes(String str) {
         return StringUtils.toBytesUTF8(str);
     }


Mime
View raw message