From 947d35c210166725cb1372dabcde0aeb3f591c40 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 24 Apr 2015 13:10:00 -0700 Subject: [PATCH 1/7] Define additional ConfidentialStore around RSA key pair --- .../jenkins/security/RSAConfidentialKey.java | 88 +++++++++++++++++++ .../RSADigitalSignatureConfidentialKey.java | 36 ++++++++ .../security/RSAConfidentialKeyTest.groovy | 25 ++++++ 3 files changed, 149 insertions(+) create mode 100644 core/src/main/java/jenkins/security/RSAConfidentialKey.java create mode 100644 core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java create mode 100644 core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy diff --git a/core/src/main/java/jenkins/security/RSAConfidentialKey.java b/core/src/main/java/jenkins/security/RSAConfidentialKey.java new file mode 100644 index 0000000000..637609af04 --- /dev/null +++ b/core/src/main/java/jenkins/security/RSAConfidentialKey.java @@ -0,0 +1,88 @@ +package jenkins.security; + +import org.apache.commons.codec.binary.Base64; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.SecureRandom; +import java.security.interfaces.RSAPrivateCrtKey; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.RSAPublicKeySpec; + +/** + * RSA public/private key pair as {@link ConfidentialKey}. + * + *

+ * As per the design principle of {@link ConfidentialKey}, not exposing {@link PrivateKey} directly. + * Define subtypes for different use cases. + * + * @author Kohsuke Kawaguchi + */ +public abstract class RSAConfidentialKey extends ConfidentialKey { + private volatile RSAPrivateKey priv; + private volatile RSAPublicKey pub; + public RSAConfidentialKey(String id) { + super(id); + } + + public RSAConfidentialKey(Class owner, String shortName) { + this(owner.getName() + '.' + shortName); + } + + private RSAPrivateKey getKey() { + try { + if (priv ==null) { + synchronized (this) { + if (priv ==null) { + byte[] payload = load(); + if (payload==null) { + KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); + gen.initialize(2048,new SecureRandom()); // going beyond 2048 requires crypto extension + KeyPair keys = gen.generateKeyPair(); + priv = (RSAPrivateKey)keys.getPrivate(); + pub = (RSAPublicKey)keys.getPublic(); + store(priv.getEncoded()); + } else { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + priv = (RSAPrivateKey)keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); + + RSAPrivateCrtKey pks = (RSAPrivateCrtKey) priv; + pub = (RSAPublicKey)keyFactory.generatePublic( + new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent())); + } + } + } + } + return priv; + } catch (IOException e) { + throw new Error("Failed to load the key: "+getId(),e); + } catch (GeneralSecurityException e) { + throw new Error("Failed to load the key: "+getId(),e); + } + } + + /** + * Caller is responsible for using the private key appropriately to avoid compromise. + */ + protected RSAPrivateKey getPrivateKey() { + return getKey(); + } + + public RSAPublicKey getPublicKey() { + getKey(); + return pub; + } + + /** + * Gets base64-encoded public key. + */ + public String getEncodedPublicKey() { + return new String(Base64.encodeBase64(getPublicKey().getEncoded())); + } +} diff --git a/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java new file mode 100644 index 0000000000..6d5a88f107 --- /dev/null +++ b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java @@ -0,0 +1,36 @@ +package jenkins.security; + +import hudson.remoting.Base64; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.Signature; +import java.security.interfaces.RSAPrivateKey; + +/** + * RSA digital signature as {@link ConfidentialKey} to prevent accidental leak of private key. + * + * @author Kohsuke Kawaguchi + */ +public class RSADigitalSignatureConfidentialKey extends RSAConfidentialKey { + public RSADigitalSignatureConfidentialKey(String id) { + super(id); + } + + public RSADigitalSignatureConfidentialKey(Class owner, String shortName) { + super(owner, shortName); + } + + /** + * Sign a message and base64 encode the signature. + */ + public String sign(String msg) throws GeneralSecurityException, IOException { + RSAPrivateKey key = getPrivateKey(); + Signature sig = Signature.getInstance(SIGNING_ALGORITHM + "with" + key.getAlgorithm()); + sig.initSign(key); + sig.update(msg.getBytes("UTF8")); + return Base64.encode(sig.sign()); + } + + static final String SIGNING_ALGORITHM = "SHA256"; +} diff --git a/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy b/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy new file mode 100644 index 0000000000..f397a1cad7 --- /dev/null +++ b/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy @@ -0,0 +1,25 @@ +package jenkins.security + +import org.junit.Rule +import org.junit.Test + +/** + * + * + * @author Kohsuke Kawaguchi + */ +class RSAConfidentialKeyTest { + @Rule + public ConfidentialStoreRule store = new ConfidentialStoreRule() + + def key = new RSAConfidentialKey("test") {} + + @Test + void loadingExistingKey() { + // this second key of the same ID will cause it to load the key from the disk + def key2 = new RSAConfidentialKey("test") {} + + assert key.privateKey==key2.privateKey; + assert key.publicKey ==key2.publicKey; + } +} From 43a2f43a9f4b85aa17c80a3d9a0d51be3150452e Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 24 Apr 2015 13:18:54 -0700 Subject: [PATCH 2/7] These exceptions are pretty unlikely. --- .../RSADigitalSignatureConfidentialKey.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java index 6d5a88f107..69b6f5d64e 100644 --- a/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java @@ -1,8 +1,6 @@ package jenkins.security; -import hudson.remoting.Base64; - -import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; @@ -24,12 +22,18 @@ public class RSADigitalSignatureConfidentialKey extends RSAConfidentialKey { /** * Sign a message and base64 encode the signature. */ - public String sign(String msg) throws GeneralSecurityException, IOException { - RSAPrivateKey key = getPrivateKey(); - Signature sig = Signature.getInstance(SIGNING_ALGORITHM + "with" + key.getAlgorithm()); - sig.initSign(key); - sig.update(msg.getBytes("UTF8")); - return Base64.encode(sig.sign()); + public String sign(String msg) { + try { + RSAPrivateKey key = getPrivateKey(); + Signature sig = Signature.getInstance(SIGNING_ALGORITHM + "with" + key.getAlgorithm()); + sig.initSign(key); + sig.update(msg.getBytes("UTF-8")); + return hudson.remoting.Base64.encode(sig.sign()); + } catch (GeneralSecurityException e) { + throw new SecurityException(e); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); // UTF-8 is mandatory + } } static final String SIGNING_ALGORITHM = "SHA256"; From 0d41a2c3eb9274c1452b0644cc6cf96789fae031 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sun, 26 Apr 2015 11:26:02 -0700 Subject: [PATCH 3/7] whitespace reformatting --- .../jenkins/security/RSAConfidentialKey.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/jenkins/security/RSAConfidentialKey.java b/core/src/main/java/jenkins/security/RSAConfidentialKey.java index 637609af04..a2e7fa0bba 100644 --- a/core/src/main/java/jenkins/security/RSAConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSAConfidentialKey.java @@ -37,23 +37,23 @@ public abstract class RSAConfidentialKey extends ConfidentialKey { private RSAPrivateKey getKey() { try { - if (priv ==null) { + if (priv == null) { synchronized (this) { - if (priv ==null) { + if (priv == null) { byte[] payload = load(); - if (payload==null) { + if (payload == null) { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); - gen.initialize(2048,new SecureRandom()); // going beyond 2048 requires crypto extension + gen.initialize(2048, new SecureRandom()); // going beyond 2048 requires crypto extension KeyPair keys = gen.generateKeyPair(); - priv = (RSAPrivateKey)keys.getPrivate(); - pub = (RSAPublicKey)keys.getPublic(); + priv = (RSAPrivateKey) keys.getPrivate(); + pub = (RSAPublicKey) keys.getPublic(); store(priv.getEncoded()); } else { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - priv = (RSAPrivateKey)keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); + priv = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); RSAPrivateCrtKey pks = (RSAPrivateCrtKey) priv; - pub = (RSAPublicKey)keyFactory.generatePublic( + pub = (RSAPublicKey) keyFactory.generatePublic( new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent())); } } @@ -61,9 +61,9 @@ public abstract class RSAConfidentialKey extends ConfidentialKey { } return priv; } catch (IOException e) { - throw new Error("Failed to load the key: "+getId(),e); + throw new Error("Failed to load the key: " + getId(), e); } catch (GeneralSecurityException e) { - throw new Error("Failed to load the key: "+getId(),e); + throw new Error("Failed to load the key: " + getId(), e); } } From c6d9eb8af0b86aef4079258dda45c3693cbaf4f8 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sun, 26 Apr 2015 12:37:03 -0700 Subject: [PATCH 4/7] Making people happier --- .../jenkins/security/RSAConfidentialKey.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/jenkins/security/RSAConfidentialKey.java b/core/src/main/java/jenkins/security/RSAConfidentialKey.java index a2e7fa0bba..36449d5933 100644 --- a/core/src/main/java/jenkins/security/RSAConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSAConfidentialKey.java @@ -25,8 +25,8 @@ import java.security.spec.RSAPublicKeySpec; * @author Kohsuke Kawaguchi */ public abstract class RSAConfidentialKey extends ConfidentialKey { - private volatile RSAPrivateKey priv; - private volatile RSAPublicKey pub; + private RSAPrivateKey priv; + private RSAPublicKey pub; public RSAConfidentialKey(String id) { super(id); } @@ -35,28 +35,24 @@ public abstract class RSAConfidentialKey extends ConfidentialKey { this(owner.getName() + '.' + shortName); } - private RSAPrivateKey getKey() { + private synchronized RSAPrivateKey getKey() { try { if (priv == null) { - synchronized (this) { - if (priv == null) { - byte[] payload = load(); - if (payload == null) { - KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); - gen.initialize(2048, new SecureRandom()); // going beyond 2048 requires crypto extension - KeyPair keys = gen.generateKeyPair(); - priv = (RSAPrivateKey) keys.getPrivate(); - pub = (RSAPublicKey) keys.getPublic(); - store(priv.getEncoded()); - } else { - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - priv = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); + byte[] payload = load(); + if (payload == null) { + KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); + gen.initialize(2048, new SecureRandom()); // going beyond 2048 requires crypto extension + KeyPair keys = gen.generateKeyPair(); + priv = (RSAPrivateKey) keys.getPrivate(); + pub = (RSAPublicKey) keys.getPublic(); + store(priv.getEncoded()); + } else { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + priv = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); - RSAPrivateCrtKey pks = (RSAPrivateCrtKey) priv; - pub = (RSAPublicKey) keyFactory.generatePublic( - new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent())); - } - } + RSAPrivateCrtKey pks = (RSAPrivateCrtKey) priv; + pub = (RSAPublicKey) keyFactory.generatePublic( + new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent())); } } return priv; From ef7db48bf37fb3da63b0026a538b073690d8b9fc Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 28 Apr 2015 08:06:54 -0700 Subject: [PATCH 5/7] doc improvements --- .../jenkins/security/RSAConfidentialKey.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/jenkins/security/RSAConfidentialKey.java b/core/src/main/java/jenkins/security/RSAConfidentialKey.java index 36449d5933..892a200688 100644 --- a/core/src/main/java/jenkins/security/RSAConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSAConfidentialKey.java @@ -27,6 +27,7 @@ import java.security.spec.RSAPublicKeySpec; public abstract class RSAConfidentialKey extends ConfidentialKey { private RSAPrivateKey priv; private RSAPublicKey pub; + public RSAConfidentialKey(String id) { super(id); } @@ -35,7 +36,17 @@ public abstract class RSAConfidentialKey extends ConfidentialKey { this(owner.getName() + '.' + shortName); } - private synchronized RSAPrivateKey getKey() { + /** + * Obtains the private key (lazily.) + *

+ * This method is not publicly exposed as per the design principle of {@link ConfidentialKey}. + * Instead of exposing private key, define methods that use them in specific way, such as + * {@link RSADigitalSignatureConfidentialKey}. + * + * @throws Error + * If key cannot be loaded for some reasons, we fail. + */ + protected synchronized RSAPrivateKey getPrivateKey() { try { if (priv == null) { byte[] payload = load(); @@ -63,15 +74,8 @@ public abstract class RSAConfidentialKey extends ConfidentialKey { } } - /** - * Caller is responsible for using the private key appropriately to avoid compromise. - */ - protected RSAPrivateKey getPrivateKey() { - return getKey(); - } - public RSAPublicKey getPublicKey() { - getKey(); + getPrivateKey(); return pub; } From 31960d0de60033541c5ac5ce58b6faf87015b1fd Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 28 Apr 2015 08:07:41 -0700 Subject: [PATCH 6/7] Adding copyright header. --- .../jenkins/security/RSAConfidentialKey.java | 23 +++++++++++++++++++ .../RSADigitalSignatureConfidentialKey.java | 23 +++++++++++++++++++ .../security/RSAConfidentialKeyTest.groovy | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/core/src/main/java/jenkins/security/RSAConfidentialKey.java b/core/src/main/java/jenkins/security/RSAConfidentialKey.java index 892a200688..c8a065f10b 100644 --- a/core/src/main/java/jenkins/security/RSAConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSAConfidentialKey.java @@ -1,3 +1,26 @@ +/* + * The MIT License + * + * Copyright (c) 2015, CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package jenkins.security; import org.apache.commons.codec.binary.Base64; diff --git a/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java index 69b6f5d64e..5ff1b55bdc 100644 --- a/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java +++ b/core/src/main/java/jenkins/security/RSADigitalSignatureConfidentialKey.java @@ -1,3 +1,26 @@ +/* + * The MIT License + * + * Copyright (c) 2015, CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package jenkins.security; import java.io.UnsupportedEncodingException; diff --git a/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy b/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy index f397a1cad7..ed9ef183fe 100644 --- a/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy +++ b/core/src/test/groovy/jenkins/security/RSAConfidentialKeyTest.groovy @@ -1,3 +1,26 @@ +/* + * The MIT License + * + * Copyright (c) 2015, CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package jenkins.security import org.junit.Rule From aa50f169e4bdd101890f1a178092b8561b71b2f8 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Tue, 28 Apr 2015 08:37:52 -0700 Subject: [PATCH 7/7] Test for RSADigitalSignatureConfidentialKey --- ...DigitalSignatureConfidentialKeyTest.groovy | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 core/src/test/groovy/jenkins/security/RSADigitalSignatureConfidentialKeyTest.groovy diff --git a/core/src/test/groovy/jenkins/security/RSADigitalSignatureConfidentialKeyTest.groovy b/core/src/test/groovy/jenkins/security/RSADigitalSignatureConfidentialKeyTest.groovy new file mode 100644 index 0000000000..63df480550 --- /dev/null +++ b/core/src/test/groovy/jenkins/security/RSADigitalSignatureConfidentialKeyTest.groovy @@ -0,0 +1,54 @@ +/* + * The MIT License + * + * Copyright (c) 2015, CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package jenkins.security + +import hudson.remoting.Base64 +import org.junit.Rule +import org.junit.Test + +import java.security.Signature + +/** + * + * + * @author Kohsuke Kawaguchi + */ +class RSADigitalSignatureConfidentialKeyTest { + @Rule + public ConfidentialStoreRule store = new ConfidentialStoreRule() + + def key = new RSADigitalSignatureConfidentialKey("test"); + + @Test + void dsigSignAndVerify() { + def msg = key.sign("Hello world"); + println msg; + + def sig = Signature.getInstance("SHA256withRSA"); + sig.initVerify(key.publicKey); + sig.update(msg.getBytes("UTF-8")); + + assert sig.verify(Base64.decode(msg)) + } +}