summaryrefslogtreecommitdiff
path: root/guile/email-key-rotation/dkim.scm
blob: f13d5b4346e14caeaa109334aa5a0e553733e3c6 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(define-module (email-key-rotation dkim)
  #:use-module ((email-key-rotation openssl) #:prefix openssl:)
  #:use-module ((email-key-rotation dns) #:prefix dns:)
  #:use-module (web uri)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web client)
  #:use-module (json)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 match)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 i18n)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (sxml match)
  #:export (<key>
	    make-key
	    key?
	    current-selector
	    current-private-key
	    expired-private-key
	    sxml->key
	    key->sxml
	    initialize
	    rotate
	    write-private-key
	    write-current-selector
	    dns-records)
  #:declarative? #t)

(define (G_ msg) (gettext msg "email-key-rotation"))

(define-immutable-record-type <key>
  (make-key current-selector next-selectors current-private-key expired-private-key)
  key?
  (current-selector current-selector set-current-selector)
  (next-selectors next-selectors set-next-selectors)
  (current-private-key current-private-key set-current-private-key)
  (expired-private-key expired-private-key set-expired-private-key))

(define (sxml->key sxml)
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-origin 'sxml->key)
	  (make-exception-with-irritants (list sxml))
	  (make-exception-with-message
	   (G_ "cannot read the XML fragment as a key."))
	  exn)))
    (lambda ()
      (sxml-match
       sxml
       ;; Guile removes newlines in attributes, so we preserve them by
       ;; saving the keys as uri-encoded strings.
       ((https://planete-kraus.eu/ns/email-key-rotation:key
	 (@ (current-selector ,current-selector)
	    (current-private-key ,current-private-key-encoded)
	    (expired-private-key ,expired-private-key-encoded))
	 (https://planete-kraus.eu/ns/email-key-rotation:next-selector
	  (@ (name ,next-selectors))) ...)
	(make-key (string->symbol current-selector)
		  (map string->symbol next-selectors)
		  (uri-decode current-private-key-encoded)
		  (uri-decode expired-private-key-encoded)))
       ((https://planete-kraus.eu/ns/email-key-rotation:key
	 (@ (current-selector ,current-selector)
	    (current-private-key ,current-private-key-encoded))
	 (https://planete-kraus.eu/ns/email-key-rotation:next-selector
	  (@ (name ,next-selectors))) ...)
	(make-key (string->symbol current-selector)
		  (map string->symbol next-selectors)
		  (uri-decode current-private-key-encoded)
		  #f))))))

(define (key->sxml key)
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'key->sxml)
	  (make-exception-with-irritants (list key))
	  (make-exception-with-message
	   (G_ "when converting a DKIM key to SXML:"))
	  exn)))
    (lambda ()
      (match key
	(($ <key> current-selector next-selectors current-private-key expired-private-key)
	 (let ((current-selector (symbol->string current-selector))
	       (next-selectors
		(map
		 (lambda (s)
		   `(next-selector (@ (name ,(symbol->string s)))))
		 next-selectors)))
	   `(key (@ (current-selector ,current-selector)
		    (current-private-key
		     ,(uri-encode current-private-key))
		    ,@(if expired-private-key
			  `((expired-private-key
			     ,(uri-encode expired-private-key)))
			  '()))
		 ,@next-selectors)))))))

(define (initialize selectors)
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'initialize)
	  (make-exception-with-irritants (list selectors))
	  (make-exception-with-message
	   (G_ "when initializing a new DKIM key:"))
	  exn)))
    (lambda ()
      (make-key (car selectors)
		(cdr selectors)
		(openssl:generate-key 2048)
		#f))))

(define (rotate key)
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'rotate)
	  (make-exception-with-irritants (list key))
	  (make-exception-with-message
	   (G_ "when rotating a DKIM key:"))
	  exn)))
    (lambda ()
      (match key
	(($ <key> current-selector next-selectors current-private-key expired-private-key)
	 (make-key (car next-selectors)
		   `(,@(cdr next-selectors) ,current-selector)
		   (openssl:generate-key 2048)
		   current-private-key))))))

(define* (write-current-selector key #:optional (port (current-output-port)))
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'write-current-selector)
	  (make-exception-with-irritants (list key port))
	  (make-exception-with-message
	   (G_ "when writing the DKIM selector to file:"))
	  exn)))
    (lambda ()
      (seek port 0 SEEK_SET)
      (format port "~a\n" (current-selector key))
      (truncate-file port))))

(define* (write-private-key key #:optional (port (current-output-port)))
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'write-private-key)
	  (make-exception-with-irritants (list key port))
	  (make-exception-with-message
	   (G_ "when saving a private DKIM key:"))
	  exn)))
    (lambda ()
      (unless (port? port)
	(raise-exception
	 (make-exception
	  (make-error)
	  (make-exception-with-message
	   (G_ "the port to write must be a port object.")))))
      (when (file-port? port)
	(chmod port #o600))
      (seek port 0 SEEK_SET)
      (match key
	(($ <key> _ _ key _)
	 (begin
	   (display key port)
	   (truncate-file port)))))))

(define (public-key->dkim-record public-key)
  ;; Discard the first and last lines and remove newlines
  (call-with-output-string
    (lambda (out)
      (format out "v=DKIM1; k=rsa; t=s; p=")
      (call-with-input-string public-key
	(lambda (port)
	  (let collect-lines ((carry #f)
			      (first? #t))
	    (let ((next (read-line port)))
	      (unless (eof-object? next)
		(when carry
		  (display carry out))
		(collect-lines (if first? #f next)
			       #f)))))))))

(define (private-key->dkim-record key)
  (public-key->dkim-record
   (openssl:private-key->public-key key)))

(define optional-private-key->dkim-record
  (match-lambda
    (#f "v=DKIM1; k=rsa; t=s; p=deleted")
    (key
     (private-key->dkim-record key))))

(define (dns-records key)
  (with-exception-handler
      (lambda (exn)
	(raise-exception
	 (make-exception
	  (make-exception-with-origin 'dns-records)
	  (make-exception-with-irritants (list key))
	  (make-exception-with-message
	   (G_ "when computing the DKIM DNS records:"))
	  exn)))
    (lambda ()
      (match key
	(($ <key>
	    (= symbol->string current-record)
	    (= reverse
	       (= car
		  (= symbol->string previous-record)))
	    (= private-key->dkim-record current)
	    (= optional-private-key->dkim-record expired))
	 (list
	  (dns:make-dns-record
	   (string-append current-record "._domainkey")
	   "TXT"
	   current)
	  (dns:make-dns-record
	   (string-append previous-record "._domainkey")
	   "TXT"
	   expired)))))))