2013-12-20 11:17:42 -05:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2020-09-01 16:13:11 -04:00
|
|
|
|
;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020 Ludovic Courtès <ludo@gnu.org>
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix scripts authenticate)
|
2020-09-01 16:13:11 -04:00
|
|
|
|
#:use-module (guix scripts)
|
utils: Move base16 procedures to (guix base16).
* guix/utils.scm (bytevector->base16-string, base16-string->bytevector):
Move to...
* guix/base16.scm: ... here. New file.
* tests/utils.scm ("bytevector->base16-string->bytevector"): Move to...
* tests/base16.scm: ... here. New file.
* Makefile.am (MODULES): Add guix/base16.scm.
(SCM_TESTS): Add tests/base16.scm.
* build-aux/download.scm, guix/derivations.scm,
guix/docker.scm, guix/import/snix.scm, guix/pk-crypto.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/store.scm, tests/hash.scm,
tests/pk-crypto.scm: Adjust imports accordingly.
2017-03-15 16:54:34 -04:00
|
|
|
|
#:use-module (guix base16)
|
Switch to Guile-Gcrypt.
This removes (guix hash) and (guix pk-crypto), which now live as part of
Guile-Gcrypt (version 0.1.0.)
* guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm,
tests/hash.scm, tests/pk-crypto.scm: Remove.
* configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and
LIBGCRYPT_LIBDIR assignments.
* m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove.
* README: Add Guile-Gcrypt to the dependencies; move libgcrypt as
"required unless --disable-daemon".
* doc/guix.texi (Requirements): Likewise.
* gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm,
guix/git.scm, guix/http-client.scm, guix/import/cpan.scm,
guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm,
guix/import/gnu.scm, guix/import/hackage.scm,
guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm,
guix/pki.scm, guix/scripts/archive.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/scripts/pack.scm,
guix/scripts/publish.scm, guix/scripts/refresh.scm,
guix/scripts/substitute.scm, guix/store.scm,
guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm,
tests/builders.scm, tests/challenge.scm, tests/cpan.scm,
tests/crate.scm, tests/derivations.scm, tests/gem.scm,
tests/nar.scm, tests/opam.scm, tests/pki.scm,
tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm,
tests/store.scm, tests/substitute.scm: Adjust imports.
* gnu/system/vm.scm: Likewise.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(expression->derivation-in-linux-vm)[config]: Remove.
(iso9660-image)[config]: Remove.
(qemu-image)[config]: Remove.
(system-docker-image)[config]: Remove.
* guix/scripts/pack.scm: Adjust imports.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(self-contained-tarball)[build]: Call 'make-config.scm' without
#:libgcrypt argument.
(squashfs-image)[libgcrypt]: Remove.
[build]: Call 'make-config.scm' without #:libgcrypt.
(docker-image)[config, json]: Remove.
[build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from
the imported modules.
* guix/self.scm (specification->package): Remove "libgcrypt", add
"guile-gcrypt".
(compiled-guix): Remove #:libgcrypt.
[guile-gcrypt]: New variable.
[dependencies]: Add it.
[*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call.
Add #:extensions.
[*config*]: Remove #:libgcrypt from 'make-config.scm' call.
(%dependency-variables): Remove %libgcrypt.
(make-config.scm): Remove #:libgcrypt.
* build-aux/build-self.scm (guile-gcrypt): New variable.
(make-config.scm): Remove #:libgcrypt.
(build-program)[fake-gcrypt-hash]: New variable.
Add (gcrypt hash) to the imported modules. Adjust load path
assignments.
* gnu/packages/package-management.scm (guix)[propagated-inputs]: Add
GUILE-GCRYPT.
[arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search
path.
2018-08-31 11:07:07 -04:00
|
|
|
|
#:use-module (gcrypt pk-crypto)
|
2013-12-29 09:55:38 -05:00
|
|
|
|
#:use-module (guix pki)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
#:use-module (guix ui)
|
2020-09-11 06:51:36 -04:00
|
|
|
|
#:use-module (guix diagnostics)
|
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2020-09-11 08:35:07 -04:00
|
|
|
|
#:use-module (srfi srfi-71)
|
2020-09-11 06:51:36 -04:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
2016-10-19 08:28:56 -04:00
|
|
|
|
#:use-module (ice-9 binary-ports)
|
|
|
|
|
#:use-module (ice-9 rdelim)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
#:use-module (ice-9 match)
|
2020-09-11 08:35:07 -04:00
|
|
|
|
#:use-module (ice-9 vlist)
|
2020-09-15 08:24:05 -04:00
|
|
|
|
#:use-module (ice-9 iconv)
|
2013-12-20 11:17:42 -05:00
|
|
|
|
#:export (guix-authenticate))
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
|
|
|
|
;;; This program is used internally by the daemon to sign exported archive
|
|
|
|
|
;;; (the 'export-paths' RPC), and to authenticate imported archives (the
|
|
|
|
|
;;; 'import-paths' RPC.)
|
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2014-03-22 17:38:23 -04:00
|
|
|
|
(define read-canonical-sexp
|
|
|
|
|
;; Read a gcrypt sexp from a port and return it.
|
2016-10-19 08:28:56 -04:00
|
|
|
|
(compose string->canonical-sexp read-string))
|
2013-12-20 11:17:42 -05:00
|
|
|
|
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(define (load-key-pair key-file)
|
|
|
|
|
"Load the key pair whose secret key lives at KEY-FILE. Return a pair of
|
|
|
|
|
canonical sexps representing those keys."
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let* ((secret-key (call-with-input-file key-file read-canonical-sexp))
|
|
|
|
|
(public-key (call-with-input-file
|
2014-03-22 17:38:23 -04:00
|
|
|
|
(string-append (string-drop-right key-file 4)
|
|
|
|
|
".pub")
|
2020-09-11 08:35:07 -04:00
|
|
|
|
read-canonical-sexp)))
|
|
|
|
|
(cons public-key secret-key)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(let ((errno (system-error-errno args)))
|
|
|
|
|
(raise
|
|
|
|
|
(formatted-message
|
|
|
|
|
(G_ "failed to load key pair at '~a': ~a~%")
|
|
|
|
|
key-file (strerror errno)))))))
|
|
|
|
|
|
|
|
|
|
(define (sign-with-key public-key secret-key sha256)
|
|
|
|
|
"Sign the hash SHA256 (a bytevector) with SECRET-KEY (a canonical sexp), and
|
|
|
|
|
return the signature as a canonical sexp that includes SHA256, PUBLIC-KEY, and
|
|
|
|
|
the actual signature."
|
|
|
|
|
(let ((data (bytevector->hash-data sha256
|
|
|
|
|
#:key-type (key-type public-key))))
|
|
|
|
|
(signature-sexp data secret-key public-key)))
|
|
|
|
|
|
|
|
|
|
(define (validate-signature signature acl)
|
2020-09-08 09:00:29 -04:00
|
|
|
|
"Validate SIGNATURE, a canonical sexp. Check whether its public key is
|
2020-09-11 08:35:07 -04:00
|
|
|
|
authorized in ACL, verify the signature, and return the signed data (a
|
|
|
|
|
bytevector) upon success."
|
2020-09-08 09:00:29 -04:00
|
|
|
|
(let* ((subject (signature-subject signature))
|
|
|
|
|
(data (signature-signed-data signature)))
|
2014-03-22 17:38:23 -04:00
|
|
|
|
(if (and data subject)
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(if (authorized-key? subject acl)
|
2014-03-22 17:38:23 -04:00
|
|
|
|
(if (valid-signature? signature)
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(hash-data->bytevector data) ; success
|
|
|
|
|
(raise
|
|
|
|
|
(formatted-message (G_ "invalid signature: ~a")
|
|
|
|
|
(canonical-sexp->string signature))))
|
|
|
|
|
(raise
|
|
|
|
|
(formatted-message (G_ "unauthorized public key: ~a")
|
|
|
|
|
(canonical-sexp->string subject))))
|
|
|
|
|
(raise
|
|
|
|
|
(formatted-message (G_ "corrupt signature data: ~a")
|
|
|
|
|
(canonical-sexp->string signature))))))
|
|
|
|
|
|
|
|
|
|
(define (read-command port)
|
|
|
|
|
"Read a command from PORT and return the command and arguments as a list of
|
|
|
|
|
strings. Return the empty list when the end-of-file is reached.
|
|
|
|
|
|
|
|
|
|
Commands are newline-terminated and must look something like this:
|
|
|
|
|
|
|
|
|
|
COMMAND 3:abc 5:abcde 1:x
|
|
|
|
|
|
|
|
|
|
where COMMAND is an alphanumeric sequence and the remainder is the command
|
|
|
|
|
arguments. Each argument is written as its length (in characters), followed
|
|
|
|
|
by colon, followed by the given number of characters."
|
|
|
|
|
(define (consume-whitespace port)
|
|
|
|
|
(let ((chr (lookahead-u8 port)))
|
|
|
|
|
(when (eqv? chr (char->integer #\space))
|
|
|
|
|
(get-u8 port)
|
|
|
|
|
(consume-whitespace port))))
|
|
|
|
|
|
|
|
|
|
(match (read-delimited " \t\n\r" port)
|
|
|
|
|
((? eof-object?)
|
|
|
|
|
'())
|
|
|
|
|
(command
|
|
|
|
|
(let loop ((result (list command)))
|
|
|
|
|
(consume-whitespace port)
|
|
|
|
|
(let ((next (lookahead-u8 port)))
|
|
|
|
|
(cond ((eqv? next (char->integer #\newline))
|
|
|
|
|
(get-u8 port)
|
|
|
|
|
(reverse result))
|
|
|
|
|
((eof-object? next)
|
|
|
|
|
(reverse result))
|
|
|
|
|
(else
|
|
|
|
|
(let* ((len (string->number (read-delimited ":" port)))
|
2020-09-15 08:24:05 -04:00
|
|
|
|
(str (bytevector->string
|
|
|
|
|
(get-bytevector-n port len)
|
|
|
|
|
"ISO-8859-1" 'error)))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(loop (cons str result))))))))))
|
|
|
|
|
|
|
|
|
|
(define-syntax define-enumerate-type ;TODO: factorize
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_ name->int (name id) ...)
|
|
|
|
|
(define-syntax name->int
|
|
|
|
|
(syntax-rules (name ...)
|
|
|
|
|
((_ name) id) ...)))))
|
|
|
|
|
|
|
|
|
|
;; Codes used when reply to requests.
|
|
|
|
|
(define-enumerate-type reply-code
|
|
|
|
|
(success 0)
|
|
|
|
|
(command-not-found 404)
|
|
|
|
|
(command-failed 500))
|
2014-05-16 04:22:19 -04:00
|
|
|
|
|
2013-12-20 11:17:42 -05:00
|
|
|
|
|
|
|
|
|
;;;
|
2020-09-08 09:00:29 -04:00
|
|
|
|
;;; Entry point.
|
2013-12-20 11:17:42 -05:00
|
|
|
|
;;;
|
|
|
|
|
|
2020-09-01 16:13:11 -04:00
|
|
|
|
(define-command (guix-authenticate . args)
|
|
|
|
|
(category internal)
|
|
|
|
|
(synopsis "sign or verify signatures on normalized archives (nars)")
|
|
|
|
|
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(define (send-reply code str)
|
|
|
|
|
;; Send CODE and STR as a reply to our client.
|
2020-09-15 08:24:05 -04:00
|
|
|
|
(let ((bv (string->bytevector str "ISO-8859-1" 'error)))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(format #t "~a ~a:" code (bytevector-length bv))
|
|
|
|
|
(put-bytevector (current-output-port) bv)
|
|
|
|
|
(force-output (current-output-port))))
|
|
|
|
|
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(define (call-with-reply thunk)
|
|
|
|
|
;; Send a reply for the result of THUNK or for any exception raised during
|
|
|
|
|
;; its execution.
|
|
|
|
|
(guard (c ((formatted-message? c)
|
|
|
|
|
(send-reply (reply-code command-failed)
|
|
|
|
|
(apply format #f
|
|
|
|
|
(G_ (formatted-message-string c))
|
|
|
|
|
(formatted-message-arguments c)))))
|
|
|
|
|
(send-reply (reply-code success) (thunk))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (with-reply exp ...)
|
|
|
|
|
(call-with-reply (lambda () exp ...)))
|
|
|
|
|
|
2014-04-22 05:30:51 -04:00
|
|
|
|
;; Signature sexps written to stdout may contain binary data, so force
|
|
|
|
|
;; ISO-8859-1 encoding so that things are not mangled. See
|
|
|
|
|
;; <http://bugs.gnu.org/17312> for details.
|
|
|
|
|
(set-port-encoding! (current-output-port) "ISO-8859-1")
|
|
|
|
|
(set-port-conversion-strategy! (current-output-port) 'error)
|
|
|
|
|
|
|
|
|
|
;; Same goes for input ports.
|
|
|
|
|
(with-fluids ((%default-port-encoding "ISO-8859-1")
|
|
|
|
|
(%default-port-conversion-strategy 'error))
|
|
|
|
|
(match args
|
|
|
|
|
(("--help")
|
ui: Rename '_' to 'G_'.
This avoids collisions with '_' when the latter is used as a 'match'
pattern for instance. See
<https://lists.gnu.org/archive/html/guix-devel/2017-04/msg00464.html>.
* guix/ui.scm: Rename '_' to 'G_'.
* po/guix/Makevars (XGETTEXT_OPTIONS): Adjust accordingly.
* build-aux/compile-all.scm (warnings): Remove 'format'.
* gnu/packages.scm,
gnu/services.scm,
gnu/services/shepherd.scm,
gnu/system.scm,
gnu/system/shadow.scm,
guix/gnupg.scm,
guix/http-client.scm,
guix/import/cpan.scm,
guix/import/elpa.scm,
guix/import/pypi.scm,
guix/nar.scm,
guix/scripts.scm,
guix/scripts/archive.scm,
guix/scripts/authenticate.scm,
guix/scripts/build.scm,
guix/scripts/challenge.scm,
guix/scripts/container.scm,
guix/scripts/container/exec.scm,
guix/scripts/copy.scm,
guix/scripts/download.scm,
guix/scripts/edit.scm,
guix/scripts/environment.scm,
guix/scripts/gc.scm,
guix/scripts/graph.scm,
guix/scripts/hash.scm,
guix/scripts/import.scm,
guix/scripts/import/cpan.scm,
guix/scripts/import/cran.scm,
guix/scripts/import/crate.scm,
guix/scripts/import/elpa.scm,
guix/scripts/import/gem.scm,
guix/scripts/import/gnu.scm,
guix/scripts/import/hackage.scm,
guix/scripts/import/nix.scm,
guix/scripts/import/pypi.scm,
guix/scripts/import/stackage.scm,
guix/scripts/lint.scm,
guix/scripts/offload.scm,
guix/scripts/pack.scm,
guix/scripts/package.scm,
guix/scripts/perform-download.scm,
guix/scripts/publish.scm,
guix/scripts/pull.scm,
guix/scripts/refresh.scm,
guix/scripts/size.scm,
guix/scripts/substitute.scm,
guix/scripts/system.scm,
guix/ssh.scm,
guix/upstream.scm: Use 'G_' instead of '_'. Most of this change was
obtained by running: "sed -i -e's/(_ "/(G_ "/g' `find -name \*.scm`".
2017-05-03 09:57:02 -04:00
|
|
|
|
(display (G_ "Usage: guix authenticate OPTION...
|
2020-09-11 06:51:36 -04:00
|
|
|
|
Sign data or verify signatures. This tool is meant to be used internally by
|
|
|
|
|
'guix-daemon'.\n")))
|
2014-04-22 05:30:51 -04:00
|
|
|
|
(("--version")
|
|
|
|
|
(show-version-and-exit "guix authenticate"))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(()
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(let ((acl (current-acl)))
|
|
|
|
|
(let loop ((key-pairs vlist-null))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
;; Read a request on standard input and reply.
|
|
|
|
|
(match (read-command (current-input-port))
|
|
|
|
|
(("sign" signing-key (= base16-string->bytevector hash))
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(let* ((key-pairs keys
|
|
|
|
|
(match (vhash-assoc signing-key key-pairs)
|
|
|
|
|
((_ . keys)
|
|
|
|
|
(values key-pairs keys))
|
|
|
|
|
(#f
|
|
|
|
|
(let ((keys (load-key-pair signing-key)))
|
|
|
|
|
(values (vhash-cons signing-key keys
|
|
|
|
|
key-pairs)
|
|
|
|
|
keys))))))
|
|
|
|
|
(with-reply (canonical-sexp->string
|
|
|
|
|
(match keys
|
|
|
|
|
((public . secret)
|
|
|
|
|
(sign-with-key public secret hash)))))
|
|
|
|
|
(loop key-pairs)))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(("verify" signature)
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(with-reply (bytevector->base16-string
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(validate-signature
|
2020-09-11 08:35:07 -04:00
|
|
|
|
(string->canonical-sexp signature)
|
|
|
|
|
acl)))
|
|
|
|
|
(loop key-pairs))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(()
|
|
|
|
|
(exit 0))
|
|
|
|
|
(commands
|
|
|
|
|
(warning (G_ "~s: invalid command; ignoring~%") commands)
|
|
|
|
|
(send-reply (reply-code command-not-found)
|
2020-09-11 08:35:07 -04:00
|
|
|
|
"invalid command")
|
|
|
|
|
(loop key-pairs))))))
|
2020-09-11 06:51:36 -04:00
|
|
|
|
(_
|
|
|
|
|
(leave (G_ "wrong arguments~%"))))))
|
2013-12-20 11:17:42 -05:00
|
|
|
|
|
|
|
|
|
;;; authenticate.scm ends here
|