1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
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>.)
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
51 (string->canonical-sexp
53 "(entry ~a (tag (guix import)))"
54 (canonical-sexp->string public-key))))
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))
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)))
70 (string-append %config-directory "/acl"))
72 (define %public-key-file
73 (string-append %config-directory "/signing-key.pub"))
75 (define %private-key-file
76 (string-append %config-directory "/signing-key.sec"))
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
86 (mkdir-p (dirname %acl-file))
87 (with-atomic-file-output %acl-file
89 (display (canonical-sexp->string
90 (public-keys->acl (list public-key)))
94 "Return the current ACL as a canonical sexp."
96 (if (file-exists? %acl-file)
97 (call-with-input-file %acl-file
98 (compose string->canonical-sexp
100 (public-keys->acl '()))) ; the empty ACL
102 (define (acl->public-keys acl)
103 "Return the public keys (as canonical sexps) listed in ACL with the '(guix
105 (match (canonical-sexp->sexp acl)
108 ('tag ('guix 'import)))
110 (map sexp->canonical-sexp subject-keys))
112 (error "invalid access-control list" acl))))
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)
122 ('tag ('guix 'import)))
124 (not (not (member key subject-keys))))
126 (error "invalid access-control list" acl)))))
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
134 "(signature ~a ~a ~a)"
135 (canonical-sexp->string data)
136 (canonical-sexp->string (sign data secret-key))
137 (canonical-sexp->string public-key))))
139 ;;; pki.scm ends here