From 4136f63123bc742d8ce6d45158cafa9ec5a57541 Mon Sep 17 00:00:00 2001 From: Dann Church Date: Tue, 8 Dec 2015 16:11:06 -0700 Subject: [PATCH 1/3] Construct privkey from string --- src/buddy/core/keys.clj | 7 +++++++ test/buddy/core/keys_tests.clj | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/buddy/core/keys.clj b/src/buddy/core/keys.clj index 74fa9e7..985f70d 100644 --- a/src/buddy/core/keys.clj +++ b/src/buddy/core/keys.clj @@ -89,6 +89,13 @@ (with-open [reader (StringReader. ^String keydata)] (read-pem->pubkey reader))) +(defn str->private-key + "Private key constructor from string." + [keydata] + (with-open [reader (StringReader. ^String keydata)] + (let [keypair (read-pem->keypair reader nil)] + (.getPrivate keypair)))) + (defn public-key? "Return true if key `k` is a public key." [k] diff --git a/test/buddy/core/keys_tests.clj b/test/buddy/core/keys_tests.clj index fa8c53c..1fd1e79 100644 --- a/test/buddy/core/keys_tests.clj +++ b/test/buddy/core/keys_tests.clj @@ -64,7 +64,19 @@ pkey (keys/str->public-key keystr)] (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)) (is (keys/public-key? pkey)))) - ) + + (testing "Read ecdsa priv key from string." + (let [keystr (slurp "test/_files/privkey.ecdsa.pem") + pkey (keys/str->private-key keystr)] + (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)) + (is (keys/private-key? pkey)))) + + (testing "Read rsa priv key from string." + (let [keystr (slurp "test/_files/privkey.rsa.pem") + pkey (keys/str->private-key keystr)] + (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey)) + (is (keys/private-key? pkey)))) +) (deftest key-wrapping-algorithms (let [secret16 (nonce/random-bytes 16) From 76e9a4eaf49a2dc4c241746156b66ad8b9f2278b Mon Sep 17 00:00:00 2001 From: Dann Church Date: Tue, 8 Dec 2015 17:17:50 -0700 Subject: [PATCH 2/3] include reading password protected keys from a string --- src/buddy/core/keys.clj | 8 +++++--- test/buddy/core/keys_tests.clj | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/buddy/core/keys.clj b/src/buddy/core/keys.clj index 985f70d..77e4ecd 100644 --- a/src/buddy/core/keys.clj +++ b/src/buddy/core/keys.clj @@ -91,10 +91,12 @@ (defn str->private-key "Private key constructor from string." - [keydata] + ([keydata] + (str->private-key keydata nil)) + ([keydata passphrase] (with-open [reader (StringReader. ^String keydata)] - (let [keypair (read-pem->keypair reader nil)] - (.getPrivate keypair)))) + (let [keypair (read-pem->keypair reader passphrase)] + (.getPrivate keypair))))) (defn public-key? "Return true if key `k` is a public key." diff --git a/test/buddy/core/keys_tests.clj b/test/buddy/core/keys_tests.clj index 1fd1e79..c5f6dea 100644 --- a/test/buddy/core/keys_tests.clj +++ b/test/buddy/core/keys_tests.clj @@ -71,11 +71,26 @@ (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)) (is (keys/private-key? pkey)))) - (testing "Read rsa priv key from string." + (testing "Read rsa priv key from string" + (let [keystr (slurp "test/_files/privkey.rsa.pem") + pkey (keys/str->private-key keystr "secret")] + (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey)) + (is (keys/private-key? pkey)))) + + (testing "Read rsa priv key from string without password." + (is (thrown? clojure.lang.ExceptionInfo + (let [keystr (slurp "test/_files/privkey.3des.rsa.pem") + pkey (keys/str->private-key keystr)]))) (let [keystr (slurp "test/_files/privkey.rsa.pem") pkey (keys/str->private-key keystr)] (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey)) (is (keys/private-key? pkey)))) + + (testing "Read rsa priv key from string with bad password" + (is (thrown? org.bouncycastle.openssl.EncryptionException + (let [keystr (slurp "test/_files/privkey.3des.rsa.pem") + pkey (keys/str->private-key keystr "secret2")] + (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey)))))) ) (deftest key-wrapping-algorithms From 091d64019eef009d11fc2649675af9abee9024a5 Mon Sep 17 00:00:00 2001 From: Dann Church Date: Tue, 8 Dec 2015 17:20:51 -0700 Subject: [PATCH 3/3] update test to use correct key --- test/buddy/core/keys_tests.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/buddy/core/keys_tests.clj b/test/buddy/core/keys_tests.clj index c5f6dea..07ed999 100644 --- a/test/buddy/core/keys_tests.clj +++ b/test/buddy/core/keys_tests.clj @@ -72,7 +72,7 @@ (is (keys/private-key? pkey)))) (testing "Read rsa priv key from string" - (let [keystr (slurp "test/_files/privkey.rsa.pem") + (let [keystr (slurp "test/_files/privkey.3des.rsa.pem") pkey (keys/str->private-key keystr "secret")] (is (= (type pkey) org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPrivateCrtKey)) (is (keys/private-key? pkey))))