summaryrefslogtreecommitdiff
path: root/disfluid/jwk.scm
blob: 6791e74a357326cef53ea843f1d07f28278b3645 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(define-module (disfluid jwk)
  #:use-module (disfluid i18n)
  #:use-module (json)
  #:use-module (gcrypt pk-crypto)
  #:use-module (gcrypt base64)
  #:use-module (gcrypt hash)
  #:use-module (oop goops)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 receive)
  #:use-module (rnrs bytevectors)
  #:declarative? #t
  #:export (<jwk>
            jwk->json
            jwk-public->json

            jkt

            &invalid-key-parameters))

(define-exception-type
  &invalid-key-parameters
  &error
  make-invalid-key-parameters
  invalid-key-parameters?)

(define-class <jwk> ()
  kid
  key)

(define-method (initialize (key <jwk>) initargs)
  (let-keywords
   initargs #t
   ((n-size #f)
    (e 0)
    (canonical-sexp #f)
    (kid #f))
   (when (string? e)
     (with-exception-handler
         (lambda (exn)
           (raise-exception
            (make-exception
             (make-invalid-key-parameters)
             (make-exception-with-message
              (format #f (G_ "the value of e could not be decoded from base64-url (~s)")
                      e))
             exn)))
       (lambda ()
         (let ((as-data (base64-decode e base64url-alphabet)))
           (set! e
                 (car (bytevector->uint-list
                       as-data
                       (endianness little)
                       (bytevector-length as-data))))))))
   (cond
    (canonical-sexp
     (slot-set! key 'key canonical-sexp)
     (unless kid
       (set! kid (jkt key)))
     (slot-set! key 'kid kid))
    (n-size
     (begin
       (with-exception-handler
           (lambda (exn)
             (raise-exception
              (make-exception
               (make-invalid-key-parameters)
               (make-exception-with-message
                (format #f (G_ "a key with n-size=~s (e=~s) could not be built")
                        n-size e))
               exn)))
         (lambda ()
           ;; generate-key will abort complaining that n is less than
           ;; 16 for n up to 64 (!) To avoid much trouble and unsafe
           ;; keys, I’ll just require them to be at least 1024 bits
           (unless (>= n-size 1024)
             (raise-exception
              (make-exception
               (make-error)
               (make-exception-with-message
                (format #f (G_ "the key size is too small"))))))
           (let ((parameters
                  (let ((n-size (number->string n-size))
                        (e (number->string e)))
                    (string->canonical-sexp
                     (format #f "(genkey (rsa (nbits ~a:~a) (rsa-use-e ~a:~a)))"
                             (string-length n-size) n-size
                             (string-length e) e)))))
             (let ((key-pair (generate-key parameters)))
               (slot-set! key 'key key-pair)))))
       (slot-set! key 'kid (jkt key))))
    (else 
     (raise-exception
      (make-exception
       (make-invalid-key-parameters)
       (make-exception-with-message
        (G_ "the key initialization requires at least #:n-size to generate it"))))))))

(define (encode bv)
  (base64-encode bv 0 (bytevector-length bv) #f #t base64url-alphabet))

(define-method (jkt (key <jwk>))
  (let* ((required-fields
          (let* ((sexp (slot-ref key 'key))
                 (public-sexp (or (find-sexp-token sexp 'public-key)
                                  (find-sexp-token sexp 'private-key))))
            (let ((rsa (find-sexp-token public-sexp 'rsa)))
              (cond
               (rsa
                (let ((e (cadr (canonical-sexp->sexp (find-sexp-token rsa 'e))))
                      (n (cadr (canonical-sexp->sexp (find-sexp-token rsa 'n)))))
                  `((kty . "rsa")
                    (e . ,(encode e))
                    (n . ,(encode n)))))
               (else
                (error "jkt: not implemented for this key type."))))))
         (as-string (scm->json-string required-fields))
         (as-data (string->utf8 as-string))
         (hash (sha256 as-data))
         (encoded (encode hash)))
    encoded))

(define-method (kid (key <jwk>))
  (slot-ref key 'kid))

(define-method (public-rsa-parameter (key <jwk>) name)
  (let* ((sexp (slot-ref key 'key))
         (public-sexp (or (find-sexp-token sexp 'public-key)
                          (find-sexp-token sexp 'private-key)))
         (public-rsa (find-sexp-token sexp 'rsa)))
    (and public-rsa
         (let ((as-data (cadr (canonical-sexp->sexp (find-sexp-token public-rsa name)))))
           (encode as-data)))))

(define-method (n (key <jwk>))
  (public-rsa-parameter key 'n))

(define-method (e (key <jwk>))
  (public-rsa-parameter key 'e))

(define-method (private-rsa-parameters (key <jwk>))
  (let* ((sexp (slot-ref key 'key))
         (private-sexp (find-sexp-token sexp 'private-key)))
    (if private-sexp
        (let ((rsa (find-sexp-token private-sexp 'rsa)))
          (if rsa
              (let ((d (canonical-sexp-nth-data (find-sexp-token rsa 'd) 1))
                    ;; What libgcrypt calls p is in fact q and vice
                    ;; versa
                    (p (canonical-sexp-nth-data (find-sexp-token rsa 'q) 1))
                    (q (canonical-sexp-nth-data (find-sexp-token rsa 'p) 1))
                    ;; If this is the case, then u is what we want
                    ;; as q^{-1} mod p
                    (qi (canonical-sexp-nth-data (find-sexp-token rsa 'u) 1)))
                ;; FIXME: gcrypt does not remember the dP and dQ
                ;; parameters.
                (values (encode d) (encode p) (encode q) (encode qi)))
              (values #f #f #f #f)))
        (values #f #f #f #f))))

(define-method (jwk-public->json (key <jwk>))
  (let ((kid (slot-ref key 'kid))
        (n (n key))
        (e (e key)))
    (cond
     ((and n e)
      `((kid . ,kid)
        (kty . "RSA")
        (n . ,n)
        (e . ,e)))
     (else
      (error "unsupported conversion to json")))))

(define-method (jwk->json (key <jwk>))
  ;; Make sure to only call this function if it doesn’t have a private
  ;; key
  (receive (d p q qi) (private-rsa-parameters key)
    (cond
     ((and d p q qi)
      `(,@(jwk-public->json key)
        (d . ,d)
        (p . ,p)
        (q . ,q)
        (qi . ,qi)))
     (else
      (error "unsupported conversion to json")))))