Repository: cxf Updated Branches: refs/heads/3.1.x-fixes 3a14d3852 -> 15c7bcde4
[CXF-7300] Treating BigIntergers as positive values due to RSA and JWA spec backing it up, with thanks to Andriy Mahats Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/15c7bcde Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/15c7bcde Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/15c7bcde Branch: refs/heads/3.1.x-fixes Commit: 15c7bcde4ff8a741a79af1444e7d67442c48fd7a Parents: 3a14d38 Author: Sergey Beryozkin <[email protected]> Authored: Mon Mar 27 13:41:42 2017 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Mon Mar 27 13:46:27 2017 +0100 ---------------------------------------------------------------------- .../cxf/rs/security/jose/jwk/JwkUtilsTest.java | 43 ++++++++++++++++++++ .../cxf/rt/security/crypto/CryptoUtils.java | 24 ++++++++--- 2 files changed, 62 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/15c7bcde/rt/rs/security/jose-parent/jose/src/test/java/org/apache/cxf/rs/security/jose/jwk/JwkUtilsTest.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/jose-parent/jose/src/test/java/org/apache/cxf/rs/security/jose/jwk/JwkUtilsTest.java b/rt/rs/security/jose-parent/jose/src/test/java/org/apache/cxf/rs/security/jose/jwk/JwkUtilsTest.java index 762f7f8..99a930b 100644 --- a/rt/rs/security/jose-parent/jose/src/test/java/org/apache/cxf/rs/security/jose/jwk/JwkUtilsTest.java +++ b/rt/rs/security/jose-parent/jose/src/test/java/org/apache/cxf/rs/security/jose/jwk/JwkUtilsTest.java @@ -18,6 +18,7 @@ */ package org.apache.cxf.rs.security.jose.jwk; +import java.math.BigInteger; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; @@ -25,6 +26,7 @@ import org.apache.cxf.rs.security.jose.common.JoseException; import org.apache.cxf.rs.security.jose.common.JoseUtils; import org.apache.cxf.rs.security.jose.common.KeyManagementUtils; import org.apache.cxf.rs.security.jose.jwa.KeyAlgorithm; +import org.apache.cxf.rt.security.crypto.CryptoUtils; import org.junit.Assert; import org.junit.Test; @@ -90,11 +92,52 @@ public class JwkUtilsTest extends Assert { null); JsonWebKey jwk1 = JwkUtils.fromRSAPrivateKey(privateKey1, KeyAlgorithm.RSA_OAEP_256.getJwaName()); assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP)); + assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PRIVATE_EXP)); RSAPrivateKey privateKey2 = JwkUtils.toRSAPrivateKey(jwk1); assertEquals(privateKey2, privateKey1); } @Test + public void testFromToPublicRsaKey() throws Exception { + RSAPublicKey publicKey1 = + (RSAPublicKey)KeyManagementUtils.loadPublicKey("org/apache/cxf/rs/security/jose/jws/alice.jks", + "password", + "alice", + null); + JsonWebKey jwk1 = JwkUtils.fromRSAPublicKey(publicKey1, KeyAlgorithm.RSA_OAEP_256.getJwaName()); + assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP)); + assertNull(jwk1.getProperty(JsonWebKey.RSA_PRIVATE_EXP)); + RSAPublicKey publicKey2 = JwkUtils.toRSAPublicKey(jwk1); + assertEquals(publicKey2, publicKey1); + + } + @Test + public void testFromToPublicRsaKey2() throws Exception { + BigInteger n = new BigInteger( + "525569531153621228164069013206963023039121751335221395180741421479892725873020691336158448746650762107595" + + "8352148531548486906896903886764928450353366890712125983926472500064566992690642117517954169974907061547" + + "3353190040609042090075291281955112293781438730376121249764205272939686534594208819023639183157456093565" + + "4148815673814517535941780340023556224072529306118783149589148262622268860151306096159642808944513667279" + + "4704664637866917427597486905443676772669967766269923280637049233876979061993814679654208850149406432368" + + "2161337544093644200063709176660451323844399667162451308704624790051211834667782115390754507376506824717" + + "9938484919159962066058375588059543574624283546151162925649987580839763809787286157381728046746195701379" + + "0902293850442561995774628930418082115864728330723111110174368232384797709242627319756376556142528218939" + + "7783875183123336240582938265783686836202210705597100765098627429017295706176890505466946207401105614189" + + "2784165813507235148683348014201150784998715061575093867666453332433607035581378251824779499939486011300" + + "7245546797308586043310145338620953330797301627631794650975659295961069452157705404946866414340860434286" + + "65874725802069389719375237126155948350679342167596471110676954951640992376889874630989205394080379", + 10); + BigInteger e = new BigInteger("65537", 10); + RSAPublicKey publicKey = CryptoUtils.getRSAPublicKey(n, e); + + JsonWebKey jwk1 = JwkUtils.fromRSAPublicKey(publicKey, KeyAlgorithm.RSA_OAEP_256.getJwaName()); + assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP)); + assertNull(jwk1.getProperty(JsonWebKey.RSA_PRIVATE_EXP)); + RSAPublicKey privateKey2 = JwkUtils.toRSAPublicKey(jwk1); + assertEquals(privateKey2, publicKey); + + } + @Test public void testToPrivateRsaKeyWithoutE() throws Exception { RSAPrivateKey privateKey1 = (RSAPrivateKey)KeyManagementUtils.loadPrivateKey("org/apache/cxf/rs/security/jose/jws/alice.jks", http://git-wip-us.apache.org/repos/asf/cxf/blob/15c7bcde/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java ---------------------------------------------------------------------- diff --git a/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java b/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java index df3b19c..0b33150 100644 --- a/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java +++ b/rt/security/src/main/java/org/apache/cxf/rt/security/crypto/CryptoUtils.java @@ -131,6 +131,24 @@ public final class CryptoUtils { byte[] publicExponentBytes) { BigInteger modulus = toBigInteger(modulusBytes); BigInteger publicExponent = toBigInteger(publicExponentBytes); + return getRSAPublicKey(factory, modulus, publicExponent); + } + + public static RSAPublicKey getRSAPublicKey(BigInteger modulusBytes, + BigInteger publicExponentBytes) { + try { + return getRSAPublicKey(KeyFactory.getInstance("RSA"), + modulusBytes, + publicExponentBytes); + } catch (Exception ex) { + throw new SecurityException(ex); + } + } + + + public static RSAPublicKey getRSAPublicKey(KeyFactory factory, + BigInteger modulus, + BigInteger publicExponent) { try { return (RSAPublicKey)factory.generatePublic( new RSAPublicKeySpec(modulus, publicExponent)); @@ -281,11 +299,7 @@ public final class CryptoUtils { } } private static BigInteger toBigInteger(byte[] bytes) { - if (bytes[0] == -128) { - return new BigInteger(bytes); - } else { - return new BigInteger(1, bytes); - } + return new BigInteger(1, bytes); } public static AlgorithmParameterSpec getContentEncryptionCipherSpec(int authTagLength, byte[] iv) { if (authTagLength > 0) {
