Repository: cxf Updated Branches: refs/heads/master 5365fde2c -> b3a62ba7c
Minor updates to OAuth2 encryption utils Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/b3a62ba7 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/b3a62ba7 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/b3a62ba7 Branch: refs/heads/master Commit: b3a62ba7ca51e976b30d64103766907246cdd03b Parents: 5365fde Author: Sergey Beryozkin <[email protected]> Authored: Wed May 7 21:40:55 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Wed May 7 21:40:55 2014 +0100 ---------------------------------------------------------------------- .../oauth2/utils/EncryptionException.java | 4 ++ .../security/oauth2/utils/EncryptionUtils.java | 57 ++++++++++++++++++++ 2 files changed, 61 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/b3a62ba7/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionException.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionException.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionException.java index 5f16c78..279b400 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionException.java +++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionException.java @@ -21,6 +21,10 @@ package org.apache.cxf.rs.security.oauth2.utils; public class EncryptionException extends RuntimeException { private static final long serialVersionUID = -8231433265954055715L; + public EncryptionException(String message) { + super(message); + } + public EncryptionException(Throwable t) { super(t); } http://git-wip-us.apache.org/repos/asf/cxf/blob/b3a62ba7/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionUtils.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionUtils.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionUtils.java index fed8fc2..89589be 100644 --- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionUtils.java +++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/EncryptionUtils.java @@ -20,17 +20,24 @@ package org.apache.cxf.rs.security.oauth2.utils; import java.lang.reflect.Method; +import java.math.BigInteger; import java.security.Key; +import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.RSAPrivateKeySpec; +import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import org.apache.cxf.common.util.Base64Exception; import org.apache.cxf.common.util.CompressionUtils; import org.apache.cxf.helpers.IOUtils; @@ -60,6 +67,56 @@ public final class EncryptionUtils { return encodeBytes(encryptedBytes); } + public static RSAPublicKey getRsaPublicKey(KeyFactory factory, + String encodedModulus, + String encodedPublicExponent) { + try { + return getRSAPublicKey(factory, + Base64UrlUtility.decode(encodedModulus), + Base64UrlUtility.decode(encodedPublicExponent)); + } catch (Base64Exception ex) { + throw new EncryptionException(ex); + } + } + + public static RSAPublicKey getRSAPublicKey(KeyFactory factory, + byte[] modulusBytes, + byte[] publicExponentBytes) { + BigInteger modulus = new BigInteger(1, modulusBytes); + BigInteger publicExponent = new BigInteger(1, publicExponentBytes); + try { + return (RSAPublicKey)factory.generatePublic( + new RSAPublicKeySpec(modulus, publicExponent)); + } catch (Exception ex) { + throw new EncryptionException(ex); + } + } + + public static RSAPrivateKey getRSAPrivateKey(KeyFactory factory, + String encodedModulus, + String encodedPrivateExponent) { + try { + return getRSAPrivateKey(factory, + Base64UrlUtility.decode(encodedModulus), + Base64UrlUtility.decode(encodedPrivateExponent)); + } catch (Base64Exception ex) { + throw new EncryptionException(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 EncryptionException(ex); + } + } + public static SecretKey getSecretKey() throws Exception { return getSecretKey("AES"); }
