pki: Introduce 'write-acl', and fix wrong conversion in 'ensure-acl'.
[jackhill/guix/guix.git] / guix / pki.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix pki)
20 #:use-module (guix config)
21 #:use-module (guix pk-crypto)
22 #:use-module ((guix utils) #:select (with-atomic-file-output))
23 #:use-module ((guix build utils) #:select (mkdir-p))
24 #:use-module (ice-9 match)
25 #:use-module (rnrs io ports)
26 #:export (%public-key-file
27 %private-key-file
28 %acl-file
29 current-acl
30 public-keys->acl
31 acl->public-keys
32 authorized-key?
33 write-acl
34
35 signature-sexp
36 signature-subject
37 signature-signed-data
38 valid-signature?
39 signature-case))
40
41 ;;; Commentary:
42 ;;;
43 ;;; Public key infrastructure for the authentication and authorization of
44 ;;; archive imports. This is essentially a subset of SPKI for our own
45 ;;; purposes (see <http://theworld.com/~cme/spki.txt> and
46 ;;; <http://www.ietf.org/rfc/rfc2693.txt>.)
47 ;;;
48 ;;; Code:
49
50 (define (public-keys->acl keys)
51 "Return an ACL that lists all of KEYS with a '(guix import)'
52 tag---meaning that all of KEYS are authorized for archive imports. Each
53 element in KEYS must be a canonical sexp with type 'public-key'."
54
55 ;; Use SPKI-style ACL entry sexp for PUBLIC-KEY, authorizing imports
56 ;; signed by the corresponding secret key (see the IETF draft at
57 ;; <http://theworld.com/~cme/spki.txt> for the ACL format.)
58 ;;
59 ;; Note: We always use PUBLIC-KEY to designate the subject. Someday we may
60 ;; want to have name certificates and to use subject names instead of
61 ;; complete keys.
62 `(acl ,@(map (lambda (key)
63 `(entry ,(canonical-sexp->sexp key)
64 (tag (guix import))))
65 keys)))
66
67 (define %acl-file
68 (string-append %config-directory "/acl"))
69
70 (define %public-key-file
71 (string-append %config-directory "/signing-key.pub"))
72
73 (define %private-key-file
74 (string-append %config-directory "/signing-key.sec"))
75
76 (define (ensure-acl)
77 "Make sure the ACL file exists, and create an initialized one if needed."
78 (unless (file-exists? %acl-file)
79 ;; If there's no public key file, don't attempt to create the ACL.
80 (when (file-exists? %public-key-file)
81 (let ((public-key (call-with-input-file %public-key-file
82 (compose string->canonical-sexp
83 get-string-all))))
84 (mkdir-p (dirname %acl-file))
85 (with-atomic-file-output %acl-file
86 (lambda (port)
87 (write-acl (public-keys->acl (list public-key))
88 port)))))))
89
90 (define (write-acl acl port)
91 "Write ACL to PORT in canonical-sexp format."
92 (let ((sexp (sexp->canonical-sexp acl)))
93 (display (canonical-sexp->string sexp) port)))
94
95 (define (current-acl)
96 "Return the current ACL."
97 (ensure-acl)
98 (if (file-exists? %acl-file)
99 (call-with-input-file %acl-file
100 (compose canonical-sexp->sexp
101 string->canonical-sexp
102 get-string-all))
103 (public-keys->acl '()))) ; the empty ACL
104
105 (define (acl->public-keys acl)
106 "Return the public keys (as canonical sexps) listed in ACL with the '(guix
107 import)' tag."
108 (match acl
109 (('acl
110 ('entry subject-keys
111 ('tag ('guix 'import)))
112 ...)
113 (map sexp->canonical-sexp subject-keys))
114 (_
115 (error "invalid access-control list" acl))))
116
117 (define* (authorized-key? key #:optional (acl (current-acl)))
118 "Return #t if KEY (a canonical sexp) is an authorized public key for archive
119 imports according to ACL."
120 ;; Note: ACL is kept in native sexp form to make 'authorized-key?' faster,
121 ;; by not having to convert it with 'canonical-sexp->sexp' on each call.
122 ;; TODO: We could use a better data type for ACLs.
123 (let ((key (canonical-sexp->sexp key)))
124 (match acl
125 (('acl
126 ('entry subject-keys
127 ('tag ('guix 'import)))
128 ...)
129 (not (not (member key subject-keys))))
130 (_
131 (error "invalid access-control list" acl)))))
132
133 (define (signature-sexp data secret-key public-key)
134 "Return a SPKI-style sexp for the signature of DATA with SECRET-KEY that
135 includes DATA, the actual signature value (with a 'sig-val' tag), and
136 PUBLIC-KEY (see <http://theworld.com/~cme/spki.txt> for examples.)"
137 (string->canonical-sexp
138 (format #f
139 "(signature ~a ~a ~a)"
140 (canonical-sexp->string data)
141 (canonical-sexp->string (sign data secret-key))
142 (canonical-sexp->string public-key))))
143
144 (define (signature-subject sig)
145 "Return the signer's public key for SIG."
146 (find-sexp-token sig 'public-key))
147
148 (define (signature-signed-data sig)
149 "Return the signed data from SIG, typically an sexp such as
150 (hash \"sha256\" #...#)."
151 (find-sexp-token sig 'data))
152
153 (define (valid-signature? sig)
154 "Return #t if SIG is valid."
155 (let* ((data (signature-signed-data sig))
156 (signature (find-sexp-token sig 'sig-val))
157 (public-key (signature-subject sig)))
158 (and data signature
159 (verify signature data public-key))))
160
161 (define* (%signature-status signature hash
162 #:optional (acl (current-acl)))
163 "Return a symbol denoting the status of SIGNATURE vs. HASH vs. ACL.
164
165 This procedure must only be used internally, because it would be easy to
166 forget some of the cases."
167 (let ((subject (signature-subject signature))
168 (data (signature-signed-data signature)))
169 (if (and data subject)
170 (if (authorized-key? subject acl)
171 (if (equal? (hash-data->bytevector data) hash)
172 (if (valid-signature? signature)
173 'valid-signature
174 'invalid-signature)
175 'hash-mismatch)
176 'unauthorized-key)
177 'corrupt-signature)))
178
179 (define-syntax signature-case
180 (syntax-rules (valid-signature invalid-signature
181 hash-mismatch unauthorized-key corrupt-signature
182 else)
183 "\
184 Match the cases of the verification of SIGNATURE against HASH and ACL:
185
186 - the 'valid-signature' case if SIGNATURE is indeed a signature of HASH with
187 a key present in ACL;
188 - 'invalid-signature' if SIGNATURE is incorrect;
189 - 'hash-mismatch' if the hash in SIGNATURE does not match HASH;
190 - 'unauthorized-key' if the public key in SIGNATURE is not listed in ACL;
191 - 'corrupt-signature' if SIGNATURE is not a valid signature sexp.
192
193 This macro guarantees at compile-time that all these cases are handled.
194
195 SIGNATURE, and ACL must be canonical sexps; HASH must be a bytevector."
196
197 ;; Simple case: we only care about valid signatures.
198 ((_ (signature hash acl)
199 (valid-signature valid-exp ...)
200 (else else-exp ...))
201 (case (%signature-status signature hash acl)
202 ((valid-signature) valid-exp ...)
203 (else else-exp ...)))
204
205 ;; Full case.
206 ((_ (signature hash acl)
207 (valid-signature valid-exp ...)
208 (invalid-signature invalid-exp ...)
209 (hash-mismatch mismatch-exp ...)
210 (unauthorized-key unauthorized-exp ...)
211 (corrupt-signature corrupt-exp ...))
212 (case (%signature-status signature hash acl)
213 ((valid-signature) valid-exp ...)
214 ((invalid-signature) invalid-exp ...)
215 ((hash-mismatch) mismatch-exp ...)
216 ((unauthorized-key) unauthorized-exp ...)
217 ((corrupt-signature) corrupt-exp ...)
218 (else (error "bogus signature status"))))))
219
220 ;;; pki.scm ends here