Skip to content

Commit 4383ed5

Browse files
committed
[new] Add utils to de/serialize public part of KeyChains
1 parent b09bd37 commit 4383ed5

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/taoensso/tempel.clj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@
446446
?key-id (bytes/read-dynamic-?str in)]
447447
(enc/assoc-when
448448
{:kind :encrypted-keychain, :version 1,
449-
:keychain (keys/keychain-restore nil ba-kc-pub)}
449+
:keychain (keys/keychain-thaw nil ba-kc-pub)}
450450
:ba-aad ?ba-aad
451451
:key-id ?key-id
452452
:has-hmac? has-hmac?
@@ -464,6 +464,22 @@
464464
:aad (bytes/?utf8-ba->?str ba-aad)
465465
:cnt (bytes/?utf8-ba->?str ba-content))))
466466

467+
(defn keychain-freeze-public
468+
"Takes a `KeyChain` and serializes any public keys it contains
469+
to a storable byte[]. Returns the byte[].
470+
471+
Thaw (deserialize) output with: `keychain-thaw-public`."
472+
^bytes [keychain]
473+
(get (keys/keychain-freeze keychain) :ba-kc-pub))
474+
475+
(defn keychain-thaw-public
476+
"Complement of `keychain-freeze-public`.
477+
Takes a serialized byte[] of public keys and returns
478+
a `KeyChain` that contains those public keys."
479+
[ba-kc-pub] (keys/keychain-thaw nil ba-kc-pub))
480+
481+
(comment (let [kc (keychain)] (enc/submap? @kc @(keychain-thaw-public (keychain-freeze-public kc)))))
482+
467483
;;;; Cipher API
468484

469485
(defn- return-val [context return-kind ?ba-cnt ?ba-aad]

src/taoensso/tempel/keys.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
(delay (mkc-freeze m-keychain))
266266
?meta))
267267

268-
(defn keychain-restore
268+
(defn keychain-thaw
269269
"Thaws `KeyChain` from frozen byte[]s."
270270
([ba-kc-prv ba-kc-pub] (-keychain nil (mkc-thaw ba-kc-prv ba-kc-pub)))
271271
([ba-kc_ ] (-keychain nil (mkc-thaw ba-kc_))))
@@ -1162,7 +1162,7 @@
11621162
(let [?ba-kc-prv (bytes/read-dynamic-?ba in)
11631163
?ba-ucnt (bytes/read-dynamic-?ba in) ; User content
11641164
_ (df/read-resv! in)
1165-
keychain (keychain-restore ?ba-kc-prv ?ba-kc-pub)]
1165+
keychain (keychain-thaw ?ba-kc-prv ?ba-kc-pub)]
11661166

11671167
(case return
11681168
:keychain keychain

test/taoensso/tempel_tests.clj

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
(remove-ns 'taoensso.tempel-tests)
1717
(test/run-tests 'taoensso.tempel-tests))
1818

19+
;;;; Utils
20+
21+
(defn keychain->pub-keys [kc] (into #{} (comp (map :key-pub) (filter some?)) (vals (enc/force-ref kc))))
22+
(defn keychain->prv-keys [kc] (into #{} (comp (map #(or (:key-sym %) (:key-prv %))) (filter some?)) (vals (enc/force-ref kc))))
23+
24+
(comment (keychain->pub-keys (keys/keychain)))
25+
1926
;;;; Implementation
2027

2128
(deftest _headers
@@ -368,7 +375,18 @@
368375
(is (= (kci kc1) {:n-sym 1, :n-prv 2, :n-pub 2, :secret? true}))
369376
(is (= (kci kc3) {:n-sym 1, :n-prv 1, :n-pub 2, :secret? true}))])
370377

371-
(testing "Serialization and encryption"
378+
(testing "Serialization of public keys in KeyChains"
379+
(let [kc1 (keys/keychain {:empty? true})
380+
kc2 (keys/keychain {:symmetric-keys [#_1 :random]
381+
:asymmetric-keypairs [#_2 :rsa-1024 #_3 :dh-1024 #_4 :ec-secp256r1]})]
382+
(every? boolean
383+
(flatten
384+
(for [kc [kc1 kc2]]
385+
(let [kc-thawed (tempel/keychain-thaw-public (tempel/keychain-freeze-public kc))]
386+
[(is (empty? (keychain->prv-keys kc-thawed)) "No private keys in public KeyChain")
387+
(is (= (keychain->pub-keys kc-thawed) (keychain->pub-keys kc)) "All public keys in public KeyChain")]))))))
388+
389+
(testing "Serialization of encrypted KeyChains"
372390
(let [kc (->
373391
(keys/keychain {:symmetric-keys [#_1 :random]
374392
:asymmetric-keypairs [#_2 :rsa-1024 #_3 :dh-1024 #_4 :ec-secp256r1]})
@@ -405,10 +423,9 @@
405423
(is (= (set (keys (get @kc-dec "c"))) #{:key-prv :key-algo :priority}))
406424

407425
(is (= (keys/keychain-decrypt ba-enc !key-opts) nil) "Bad key")
408-
(is (= (kci (:keychain (pd ba-enc))) {:n-pub 4, :secret? false}) "Public keychain in public data")
426+
(is (= (kci (:keychain (pd ba-enc))) {:n-pub 4, :secret? false}) "Public KeyChain is in public data")
409427

410-
(is (every? nil? (mapv #(or (:key-sym %) (:key-prv %)) (vals @(:keychain (pd ba-enc)))))
411-
"No private data in public keychain")]))))))])
428+
(is (empty? (keychain->prv-keys (:keychain (pd ba-enc)))) "No private keys in public KeyChain")]))))))])
412429

413430
;;;; Core API
414431

0 commit comments

Comments
 (0)