Repository: cxf Updated Branches: refs/heads/master 98a578dbf -> b916b1b7a
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/b916b1b7 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/b916b1b7 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/b916b1b7 Branch: refs/heads/master Commit: b916b1b7a85c76b87ba162302b1442bd5a404e00 Parents: 98a578d Author: Sergey Beryozkin <[email protected]> Authored: Tue Nov 4 22:19:17 2014 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Tue Nov 4 22:19:17 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/b916b1b7/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); }
