pki: Keep ACL in native sexp format to speed up 'authorized-key?'.
[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
34 signature-sexp
35 signature-subject
36 signature-signed-data
37 valid-signature?
38 signature-case))
39
40 ;;; Commentary:
41 ;;;
42 ;;; Public key infrastructure for the authentication and authorization of
43 ;;; archive imports. This is essentially a subset of SPKI for our own
44 ;;; purposes (see <http://theworld.com/~cme/spki.txt> and
45 ;;; <http://www.ietf.org/rfc/rfc2693.txt>.)
46 ;;;
47 ;;; Code:
48
49 (define (public-keys->acl keys)
50 "Return an ACL that lists all of KEYS with a '(guix import)'
51 tag---meaning that all of KEYS are authorized for archive imports. Each
52 element in KEYS must be a canonical sexp with type 'public-key'."
53
54 ;; Use SPKI-style ACL entry sexp for PUBLIC-KEY, authorizing imports
55 ;; signed by the corresponding secret key (see the IETF draft at
56 ;; <http://theworld.com/~cme/spki.txt> for the ACL format.)
57 ;;
58 ;; Note: We always use PUBLIC-KEY to designate the subject. Someday we may
59 ;; want to have name certificates and to use subject names instead of
60 ;; complete keys.
61 `(acl ,@(map (lambda (key)
62 `(entry ,(canonical-sexp->sexp key)
63 (tag (guix import))))
64 keys)))
65
66 (define %acl-file
67 (string-append %config-directory "/acl"))
68
69 (define %public-key-file
70 (string-append %config-directory "/signing-key.pub"))
71
72 (define %private-key-file
73 (string-append %config-directory "/signing-key.sec"))
74
75 (define (ensure-acl)
76 "Make sure the ACL file exists, and create an initialized one if needed."
77 (unless (file-exists? %acl-file)
78 ;; If there's no public key file, don't attempt to create the ACL.
79 (when (file-exists? %public-key-file)
80 (let ((public-key (call-with-input-file %public-key-file
81 (compose string->canonical-sexp
82 get-string-all))))
83 (mkdir-p (dirname %acl-file))
84 (with-atomic-file-output %acl-file
85 (lambda (port)
86 (display (canonical-sexp->string
87 (public-keys->acl (list public-key)))
88 port)))))))
89
90 (define (current-acl)
91 "Return the current ACL."
92 (ensure-acl)
93 (if (file-exists? %acl-file)
94 (call-with-input-file %acl-file
95 (compose canonical-sexp->sexp
96 string->canonical-sexp
97 get-string-all))
98 (public-keys->acl '()))) ; the empty ACL
99
100 (define (acl->public-keys acl)
101 "Return the public keys (as canonical sexps) listed in ACL with the '(guix
102 import)' tag."
103 (match acl
104 (('acl
105 ('entry subject-keys
106 ('tag ('guix 'import)))
107 ...)
108 (map sexp->canonical-sexp subject-keys))
109 (_
110 (error "invalid access-control list" acl))))
111
112 (define* (authorized-key? key #:optional (acl (current-acl)))
113 "Return #t if KEY (a canonical sexp) is an authorized public key for archive
114 imports according to ACL."
115 ;; Note: ACL is kept in native sexp form to make 'authorized-key?' faster,
116 ;; by not having to convert it with 'canonical-sexp->sexp' on each call.
117 ;; TODO: We could use a better data type for ACLs.
118 (let ((key (canonical-sexp->sexp key)))
119 (match acl
120 (('acl
121 ('entry subject-keys
122 ('tag ('guix 'import)))
123 ...)
124 (not (not (member key subject-keys))))
125 (_
126 (error "invalid access-control list" acl)))))
127
128 (define (signature-sexp data secret-key public-key)
129 "Return a SPKI-style sexp for the signature of DATA with SECRET-KEY that
130 includes DATA, the actual signature value (with a 'sig-val' tag), and
131 PUBLIC-KEY (see <http://theworld.com/~cme/spki.txt> for examples.)"
132 (string->canonical-sexp
133 (format #f
134 "(signature ~a ~a ~a)"
135 (canonical-sexp->string data)
136 (canonical-sexp->string (sign data secret-key))
137 (canonical-sexp->string public-key))))
138
139 (define (signature-subject sig)
140 "Return the signer's public key for SIG."
141 (find-sexp-token sig 'public-key))
142
143 (define (signature-signed-data sig)
144 "Return the signed data from SIG, typically an sexp such as
145 (hash \"sha256\" #...#)."
146 (find-sexp-token sig 'data))
147
148 (define (valid-signature? sig)
149 "Return #t if SIG is valid."
150 (let* ((data (signature-signed-data sig))
151 (signature (find-sexp-token sig 'sig-val))
152 (public-key (signature-subject sig)))
153 (and data signature
154 (verify signature data public-key))))
155
156 (define* (%signature-status signature hash
157 #:optional (acl (current-acl)))
158 "Return a symbol denoting the status of SIGNATURE vs. HASH vs. ACL.
159
160 This procedure must only be used internally, because it would be easy to
161 forget some of the cases."
162 (let ((subject (signature-subject signature))
163 (data (signature-signed-data signature)))
164 (if (and data subject)
165 (if (authorized-key? subject acl)
166 (if (equal? (hash-data->bytevector data) hash)
167 (if (valid-signature? signature)
168 'valid-signature
169 'invalid-signature)
170 'hash-mismatch)
171 'unauthorized-key)
172 'corrupt-signature)))
173
174 (define-syntax signature-case
175 (syntax-rules (valid-signature invalid-signature
176 hash-mismatch unauthorized-key corrupt-signature
177 else)
178 "\
179 Match the cases of the verification of SIGNATURE against HASH and ACL:
180
181 - the 'valid-signature' case if SIGNATURE is indeed a signature of HASH with
182 a key present in ACL;
183 - 'invalid-signature' if SIGNATURE is incorrect;
184 - 'hash-mismatch' if the hash in SIGNATURE does not match HASH;
185 - 'unauthorized-key' if the public key in SIGNATURE is not listed in ACL;
186 - 'corrupt-signature' if SIGNATURE is not a valid signature sexp.
187
188 This macro guarantees at compile-time that all these cases are handled.
189
190 SIGNATURE, and ACL must be canonical sexps; HASH must be a bytevector."
191
192 ;; Simple case: we only care about valid signatures.
193 ((_ (signature hash acl)
194 (valid-signature valid-exp ...)
195 (else else-exp ...))
196 (case (%signature-status signature hash acl)
197 ((valid-signature) valid-exp ...)
198 (else else-exp ...)))
199
200 ;; Full case.
201 ((_ (signature hash acl)
202 (valid-signature valid-exp ...)
203 (invalid-signature invalid-exp ...)
204 (hash-mismatch mismatch-exp ...)
205 (unauthorized-key unauthorized-exp ...)
206 (corrupt-signature corrupt-exp ...))
207 (case (%signature-status signature hash acl)
208 ((valid-signature) valid-exp ...)
209 ((invalid-signature) invalid-exp ...)
210 ((hash-mismatch) mismatch-exp ...)
211 ((unauthorized-key) unauthorized-exp ...)
212 ((corrupt-signature) corrupt-exp ...)
213 (else (error "bogus signature status"))))))
214
215 ;;; pki.scm ends here