archive: Make sure $sysconfdir/guix exists.
[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 signature-sexp
33 authorized-key?))
34
35 ;;; Commentary:
36 ;;;
37 ;;; Public key infrastructure for the authentication and authorization of
38 ;;; archive imports. This is essentially a subset of SPKI for our own
39 ;;; purposes (see <http://theworld.com/~cme/spki.txt> and
40 ;;; <http://www.ietf.org/rfc/rfc2693.txt>.)
41 ;;;
42 ;;; Code:
43
44 (define (acl-entry-sexp public-key)
45 "Return a SPKI-style ACL entry sexp for PUBLIC-KEY, authorizing imports
46 signed by the corresponding secret key (see the IETF draft at
47 <http://theworld.com/~cme/spki.txt> for the ACL format.)"
48 ;; Note: We always use PUBLIC-KEY to designate the subject. Someday we may
49 ;; want to have name certificates and to use subject names instead of
50 ;; complete keys.
51 (string->canonical-sexp
52 (format #f
53 "(entry ~a (tag (guix import)))"
54 (canonical-sexp->string public-key))))
55
56 (define (acl-sexp entries)
57 "Return an ACL sexp from ENTRIES, a list of 'entry' sexps."
58 (string->canonical-sexp
59 (string-append "(acl "
60 (string-join (map canonical-sexp->string entries))
61 ")")))
62
63 (define (public-keys->acl keys)
64 "Return an ACL canonical sexp that lists all of KEYS with a '(guix import)'
65 tag---meaning that all of KEYS are authorized for archive imports. Each
66 element in KEYS must be a canonical sexp with type 'public-key'."
67 (acl-sexp (map acl-entry-sexp keys)))
68
69 (define %acl-file
70 (string-append %config-directory "/acl"))
71
72 (define %public-key-file
73 (string-append %config-directory "/signing-key.pub"))
74
75 (define %private-key-file
76 (string-append %config-directory "/signing-key.sec"))
77
78 (define (ensure-acl)
79 "Make sure the ACL file exists, and create an initialized one if needed."
80 (unless (file-exists? %acl-file)
81 ;; If there's no public key file, don't attempt to create the ACL.
82 (when (file-exists? %public-key-file)
83 (let ((public-key (call-with-input-file %public-key-file
84 (compose string->canonical-sexp
85 get-string-all))))
86 (mkdir-p (dirname %acl-file))
87 (with-atomic-file-output %acl-file
88 (lambda (port)
89 (display (canonical-sexp->string
90 (public-keys->acl (list public-key)))
91 port)))))))
92
93 (define (current-acl)
94 "Return the current ACL as a canonical sexp."
95 (ensure-acl)
96 (if (file-exists? %acl-file)
97 (call-with-input-file %acl-file
98 (compose string->canonical-sexp
99 get-string-all))
100 (public-keys->acl '()))) ; the empty ACL
101
102 (define (acl->public-keys acl)
103 "Return the public keys (as canonical sexps) listed in ACL with the '(guix
104 import)' tag."
105 (match (canonical-sexp->sexp acl)
106 (('acl
107 ('entry subject-keys
108 ('tag ('guix 'import)))
109 ...)
110 (map sexp->canonical-sexp subject-keys))
111 (_
112 (error "invalid access-control list" acl))))
113
114 (define* (authorized-key? key
115 #:optional (acl (current-acl)))
116 "Return #t if KEY (a canonical sexp) is an authorized public key for archive
117 imports according to ACL."
118 (let ((key (canonical-sexp->sexp key)))
119 (match (canonical-sexp->sexp 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 ;;; pki.scm ends here