fukamachi / pem

PEM parser.
14 stars 3 forks source link

PEM:READ-FROM-FILE returns NIL on PKCS#8 keys #1

Open phoe opened 6 years ago

phoe commented 6 years ago
CL-USER> (pem:read-from-file #P"~/priv4096.pem")
NIL

whenever the file contains a private key in form:

-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgk... (base64 encoded data)
-----END PRIVATE KEY-----
Junker commented 1 year ago

Yes, this function works only for PKCS#1 files. You need to convert PKCS#8 to PKCS#1 with openssl: openssl pkey -in pkcs8.pem -traditional > pkcs1.key

Junker commented 1 year ago

can extract private key from PKCS#8, example:

(let* ((pem-file #P"/var/tmp/KEY/rsa_private_pkcs8.pem")
       (pkcs8-der (asn1:decode (base64:base64-string-to-usb8-array
                                (cdar (pem:parse-file pem-file)))))
       (pkcs1-der (asn1:decode (cdr (fourth (car pkcs8-der))))))
  (trivia:match pkcs1-der
    ((asn1:rsa-private-key :private-exponent d :modulus n)
     (ironclad:make-private-key :rsa :d d :n n))))