cl-rw

Layered streams for Common Lisp
git clone https://logand.com/git/cl-rw.git/
Log | Files | Refs

commit 33b17eb0e4d3a5d7505944c9a86410d20baace6f
parent ed055f88e82498a15a60ed42ada7a48f905b146c
Author: Tomas Hlavaty <tom@logand.com>
Date:   Sun,  7 Dec 2014 18:39:40 +0100

use next-der and write-der as usual instead of decode and encode

this should allow better integration with rw.wire

Diffstat:
Mder.lisp | 49+++++++++++++++++++++++++------------------------
Mtls.lisp | 4++--
2 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/der.lisp b/der.lisp @@ -22,8 +22,9 @@ (defpackage :rw.der (:use :cl) - (:export :decode - :encode)) + (:export :der + :next-der + :write-der)) (in-package :rw.der) @@ -40,7 +41,7 @@ ;; http://www.herongyang.com/Cryptography/Certificate-Format-PEM-on-Certificates.html ;; http://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file -(defun decode (reader) +(defun next-der (reader) (labels ((len () (let ((n (rw:next-u8 reader))) (if (logbitp 7 n) @@ -85,8 +86,8 @@ (r (rw:peek-reader (rw:shorter-reader reader n)))) (loop while (rw:peek r) - collect (decode r))) - ;;(decode (rw:shorter-reader reader (len))) + collect (next-der r))) + ;;(next-der (rw:shorter-reader reader (len))) ;;#+nil (let* ((n (len)) (z (make-array n @@ -147,26 +148,26 @@ (loop with r = (rw:peek-reader (rw:shorter-reader reader (len))) while (rw:peek r) - collect (decode r))) + collect (next-der r))) (49 ;; SET #x31 - (cons 'set (decode (rw:shorter-reader reader (len))))) + (cons 'set (next-der (rw:shorter-reader reader (len))))) (80 (list '???-key-identifier - (decode (rw:shorter-reader reader (len))))) + (next-der (rw:shorter-reader reader (len))))) (160 ;; ??? crl-extensions signed certificate version #xa0 ;; (int inside) 2 = signed certificate v3 (list '???-signed-certificate-version - (decode (rw:shorter-reader reader (len))))) + (next-der (rw:shorter-reader reader (len))))) (163 ;; ??? signed certificate extensions #xa3 (list '???-signed-certificate-extensions - (decode (rw:shorter-reader reader (len))))) + (next-der (rw:shorter-reader reader (len))))) ;; ;;;;;;;;;; #+nil (128 - (cons '???-128 (decode (rw:shorter-reader reader (len))))) + (cons '???-128 (next-der (rw:shorter-reader reader (len))))) )))) -(defun encode (writer x) +(defun write-der (writer x) (labels ((len (n) (assert (<= 0 n)) (if (< n #x80) @@ -188,7 +189,7 @@ (let* ((b (make-array 42 :fill-pointer 0 :adjustable t)) (w (rw:writer b))) (dolist (x x) - (encode w x)) + (write-der w x)) (len (length b)) (loop for x across b @@ -354,15 +355,15 @@ (dolist (test tests t) ;; (print (list :@@@ test)) ;; (finish-output) - (assert (equalp (car test) (decode (rw:reader (cadr test))))) + (assert (equalp (car test) (next-der (rw:reader (cadr test))))) (assert (equalp (cadr test) (let ((b (make-array 42 :fill-pointer 0 :adjustable t))) - (encode (rw:writer b) (car test)) + (write-der (rw:writer b) (car test)) (coerce b 'list)))))) -;;(decode (rw:reader '(6 6 #x2a #x86 #x48 #x86 #xf7 #x0d))) -;;(decode (rw:reader '(3 4 6 #x6e #x5d #xc0))) ; '(:bit-string "011011100101110111") -;;(encode w '(bit-string "011011100101110111")) ;; '(3 4 6 #x6e #x5d #xc0) +;;(next-der (rw:reader '(6 6 #x2a #x86 #x48 #x86 #xf7 #x0d))) +;;(next-der (rw:reader '(3 4 6 #x6e #x5d #xc0))) ; '(:bit-string "011011100101110111") +;;(write-der w '(bit-string "011011100101110111")) ;; '(3 4 6 #x6e #x5d #xc0) ;; http://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file @@ -377,7 +378,7 @@ #\P #\R #\I #\V #\A #\T #\E #\space #\K #\E #\Y #\- #\- #\- #\- #\-)) - (prog1 (list 'private-key (decode (rw.base64:decode-reader reader))) + (prog1 (list 'private-key (next-der (rw.base64:decode-reader reader))) ;;(rw:till (rw:peek-reader (rw.base64:decode-reader reader))) (rw:skip reader) (assert @@ -393,7 +394,7 @@ #\B #\E #\G #\I #\N #\space #\C #\E #\R #\T #\I #\F #\I #\C #\A #\T #\E #\- #\- #\- #\- #\-)) - (prog1 (list 'certificate (decode (rw.base64:decode-reader reader))) + (prog1 (list 'certificate (next-der (rw.base64:decode-reader reader))) ;;(rw:till (rw:peek-reader (rw.base64:decode-reader reader))) (rw:skip reader) (assert @@ -440,11 +441,11 @@ (defun decode-reader (reader) (lambda () - (decode reader))) + (next-der reader))) (defun encode-writer (writer) (lambda (x) - (encode writer x))) + (write-der writer x))) (defun encode-reader (reader &optional buffer) (let* (done @@ -462,7 +463,7 @@ (x (setq n 0) (setf (fill-pointer b) 0) - (encode w x) + (write-der w x) ;;(print b) (prog1 (aref b n) (incf n))) @@ -518,4 +519,4 @@ #+nil (with-open-file (s "/usr/share/doc/dirmngr/examples/extra-certs/S-TRUSTQualSigOCSP2008-022.final.v3.509.crt" :element-type '(unsigned-byte 8)) - (decode (rw:byte-reader s))) + (next-der (rw:byte-reader s))) diff --git a/tls.lisp b/tls.lisp @@ -292,7 +292,7 @@ (rw.wire:defstruc %$Certificate () (rw:u8 #+nil $ASN.1Cert data :length rw:u24be :min 0 :max #.(1- (expt 2 24))) - (rw.wire::computed der :next (rw.der:decode (rw:reader data)))) + (rw.wire::computed der :next (rw.der:next-der (rw:reader data)))) (rw.wire:defstruc $Certificate () (%$Certificate list :length rw:u24be :min 0 :max #.(1- (expt 2 24)) :size t)) @@ -666,7 +666,7 @@ (loop for i from (1- nbytes) downto 0 do (rw:write-u8 w (ldb (byte 8 (* 8 i)) x))) - (rw.der:decode (rw:reader b))))) + (rw.der:next-der (rw:reader b))))) (defun certificate-r-s (x) (labels ((rec (x)