shrdlu68 / cl-tls

An implementation of TLS and related specifications in Common Lisp
BSD 3-Clause "New" or "Revised" License
53 stars 7 forks source link

How to generate public key base64-string? #8

Open anranyicheng opened 1 year ago

anranyicheng commented 1 year ago

Hi, if i have N and E with RSA, how to generate public key? is this library can do that?

(cl-tls:create-asn-sequence (list '(1 2 840 113549 1 1 1) :oid) (list n :integer) (list e :integer))

and result is correct oct-vector, can you help me?

adlai commented 1 year ago

Please start by enclosing code samples in markdown, so they are more easily readable; click the triple dots at the corner of this comment, and "Quote reply", to see the source of the following example:

(eval-when ()
  (throw :dice))
anranyicheng commented 1 year ago

Please start by enclosing code samples in markdown, so they are more easily readable; click the triple dots at the corner of this comment, and "Quote reply", to see the source of the following example:

(eval-when ()
  (throw :dice))

Yes,

(ql:quickload '(asn1 ironclad cl-tls cl-base64))
;; public-key, base64 string 
(defparameter *pub-key-str*
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHdsyxT66pDG4p73yope7jxA92c0AT4qIJ/xtbBcHkFPK77upnsfDTJiVEuQDH+MiMeb+XhCLNKZGp0yaUU6GlxZdp+nLW8b7Kmijr3iepaDhcbVTsYBWchaWUXauj9Lrhz58/6AE/NF0aMolxIGpsi+ST2hSHPu3GSXMdhPCkWQIDAQAB")

;; public-key octet-vect 
(defparameter *pub-key-oct* 
  (base64:base64-string-to-usb8-array *pub-key-str*))

(defparameter *rsa-pub-key*
  (trivia:match (asn1:decode *pub-key-oct*)
    ((asn1:rsa-public-key-info n e)
     (ironclad:make-public-key :rsa :n n :e e))))

(defparameter *n*
  (ironclad:rsa-key-modulus *rsa-pub-key*))
(defparameter *e*
  (ironclad:rsa-key-exponent *rsa-pub-key*))

(defparameter *generate-oct*
  (cl-tls:create-asn-sequence
   (list '(1 2 840 113549 1 1 1) :oid)
   (list '() :null)
   (list *n* :integer)
   (list *e* :integer)))

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL
adlai commented 1 year ago

and result is correct oct-vector, can you help me?

Probably; however...

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL

please link to the source from whence you took the failed test vectors; the only thing I hate worse than GitHub, is Google.

anranyicheng commented 1 year ago

and result is correct oct-vector, can you help me?

Probably; however...

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL

please link to the source from whence you took the failed test vectors; the only thing I hate worse than GitHub, is Google.

"and result is not correct oct-vector, can you help me?"

Sorry, the result is wrong, I originally wanted to express how to generate the same key(base64 string) as the original through N and E and I get publick_key_str from python:

from Crypto.PublicKey import RSA
cipher = RSA.generate(1024)
public = cipher.publickey()
public_key_str = public.exportKey().decode()
print(public_key_str)
# etc:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbtonF8bSV1oSHsc5hPsZlVDo6
LNld7cSc5lVkwdciwXTBPjO+YqK4IXMmyYRCnycUJsQRI7ceZTNCbb0QInzFURR5
tP8E09QrmbkN4qzD7dJarJnedxgHiCkjZV/QS0GfKhp07Dh5P4/+GVbgan+9vMkF
rhgjnwmI48+eZmAXXQIDAQAB
-----END PUBLIC KEY-----

I want:

(equalp *pub-key-oct* *generate-oct*)
;; -> T
adlai commented 1 year ago

[...]

I want:

(equalp *pub-key-oct* *generate-oct*)
;; -> T

Having re-read the code failing this test, I now understand what you're trying to do; not being familiar with the entire library, it'll take me a while to reply anything useful, so I recommend conducting your own study; mine could take a while.