[java-opensaml] 14/15: OSJ-75: Implement EC support for derivePublicKey in KeySupport

Brent Putman putmanb at georgetown.edu
Thu Jan 21 21:33:34 UTC 2021


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch dev/OSJ-82
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=eece3008a9b530dae1cd672fd58f0a3c9b91aac9

commit eece3008a9b530dae1cd672fd58f0a3c9b91aac9
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Sun Jan 17 20:40:50 2021 -0500

    OSJ-75: Implement EC support for derivePublicKey in KeySupport
---
 .../org/opensaml/security/crypto/KeySupport.java     | 20 +++++++++++++++++++-
 .../org/opensaml/security/crypto/KeySupportTest.java |  9 ++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java b/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java
index d774c7399..43044c97a 100644
--- a/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java
+++ b/opensaml-security-api/src/main/java/org/opensaml/security/crypto/KeySupport.java
@@ -45,6 +45,8 @@ import java.security.interfaces.RSAPrivateKey;
 import java.security.interfaces.RSAPublicKey;
 import java.security.spec.AlgorithmParameterSpec;
 import java.security.spec.DSAPublicKeySpec;
+import java.security.spec.ECPoint;
+import java.security.spec.ECPublicKeySpec;
 import java.security.spec.InvalidKeySpecException;
 import java.security.spec.KeySpec;
 import java.security.spec.RSAPublicKeySpec;
@@ -62,6 +64,7 @@ import net.shibboleth.utilities.java.support.codec.DecodingException;
 import net.shibboleth.utilities.java.support.collection.LazyMap;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
+import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util;
 import org.cryptacular.util.KeyPairUtil;
 import org.opensaml.security.SecurityException;
 import org.slf4j.Logger;
@@ -287,8 +290,23 @@ public final class KeySupport {
             } catch (final GeneralSecurityException e) {
                 throw new KeyException("Unable to derive public key from RSA private key", e);
             }
+        } else if (key instanceof ECPrivateKey) {
+            final ECPrivateKey ecKey = (ECPrivateKey) key;
+            // Let BC do the math, by converting to BC's ECPoint for the multiply(BigInteger),
+            // and then back to standard ECPoint
+            final ECPoint ecPointPublic = EC5Util.convertPoint(EC5Util.convertPoint(
+                    ecKey.getParams(), ecKey.getParams().getGenerator())
+                        .multiply(ecKey.getS()));
+            final ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(ecPointPublic, ecKey.getParams());
+
+            try {
+                factory = KeyFactory.getInstance(JCAConstants.KEY_ALGO_EC);
+                return factory.generatePublic(pubKeySpec);
+            } catch (final GeneralSecurityException e) {
+                throw new KeyException("Unable to derive public key from EC private key", e);
+            }
         } else {
-            throw new KeyException("Private key was not a DSA or RSA key");
+            throw new KeyException("Private key was not a DSA, RSA or EC key");
         }
     }
 
diff --git a/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java b/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java
index bff5778ad..e0f9a78cf 100644
--- a/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java
+++ b/opensaml-security-api/src/test/java/org/opensaml/security/crypto/KeySupportTest.java
@@ -142,7 +142,7 @@ public class KeySupportTest {
     
     
     /**
-     * Test deriving a public key from an RSA and DSA private key.
+     * Test deriving a public key from an RSA, DSA and EC private key.
      * 
      * @throws Exception if something goes wrong
      */
@@ -160,6 +160,13 @@ public class KeySupportTest {
         Assert.assertNotNull(pubKey);
         Assert.assertEquals(pubKey.getAlgorithm(), "DSA");
         Assert.assertTrue(KeySupport.matchKeyPair(pubKey, privKey));
+        
+        pubKey = null;
+        privKey = testPrivKey(ecPrivKeyPEMNoEncrypt, null, "EC");
+        pubKey = KeySupport.derivePublicKey(privKey);
+        Assert.assertNotNull(pubKey);
+        Assert.assertEquals(pubKey.getAlgorithm(), "EC");
+        Assert.assertTrue(KeySupport.matchKeyPair(pubKey, privKey));
     }
 
     

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list