-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpkey.lisp
38 lines (35 loc) · 1.29 KB
/
pkey.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(defpackage #:pem/pkey
(:use #:cl)
(:import-from #:pem/parser
#:parse-file)
(:import-from #:asn1
#:decode
#:rsa-public-key-info)
(:import-from #:trivia
#:match)
(:import-from #:cl-base64
#:base64-string-to-usb8-array)
(:import-from #:ironclad)
(:export #:read-from-file))
(in-package #:pem/pkey)
(defun read-public-key (key)
(let* ((der (base64:base64-string-to-usb8-array key))
(der (asn1:decode der)))
(match der
((asn1:rsa-public-key-info n e)
(ironclad:make-public-key :rsa :n n :e e))
(otherwise (error "Unexpected format: ~S" key)))))
(defun read-private-key (key)
(let* ((der (base64:base64-string-to-usb8-array key))
(der (asn1:decode der)))
(match der
((asn1:rsa-private-key :private-exponent d :modulus n)
(ironclad:make-private-key :rsa :d d :n n))
(otherwise (error "Unexpected format: ~S" key)))))
(defun read-from-file (pem)
(let ((data (pem/parser:parse-file pem)))
(let ((public-key (cdr (assoc "PUBLIC KEY" data :test #'string=)))
(private-key (cdr (assoc "RSA PRIVATE KEY" data :test #'string=))))
(cond
(public-key (read-public-key public-key))
(private-key (read-private-key private-key))))))