2013-04-24 23:48:36 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2019-12-18 17:19:00 +01:00
|
|
|
;;; Copyright © 2010, 2011, 2013, 2014, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
|
2013-06-10 09:46:13 +02:00
|
|
|
;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
|
2020-04-17 23:25:17 +02:00
|
|
|
;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
|
2021-11-14 03:43:45 +01:00
|
|
|
;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
|
2013-04-24 23:48:36 +02: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 gnupg)
|
|
|
|
#:use-module (ice-9 popen)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (ice-9 regex)
|
|
|
|
#:use-module (ice-9 rdelim)
|
2013-06-10 09:46:13 +02:00
|
|
|
#:use-module (ice-9 i18n)
|
2013-04-24 23:48:36 +02:00
|
|
|
#:use-module (srfi srfi-1)
|
2018-09-09 22:15:19 +02:00
|
|
|
#:use-module (guix i18n)
|
|
|
|
#:use-module ((guix utils) #:select (config-directory))
|
|
|
|
#:use-module ((guix build utils) #:select (mkdir-p))
|
2013-05-11 12:44:19 +02:00
|
|
|
#:export (%gpg-command
|
|
|
|
%openpgp-key-server
|
2018-09-09 22:15:19 +02:00
|
|
|
current-keyring
|
2013-05-11 12:44:19 +02:00
|
|
|
gnupg-verify
|
2013-04-24 23:48:36 +02:00
|
|
|
gnupg-verify*
|
|
|
|
gnupg-status-good-signature?
|
|
|
|
gnupg-status-missing-key?))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; GnuPG interface.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2013-05-11 12:44:19 +02:00
|
|
|
(define %gpg-command
|
|
|
|
;; The GnuPG 2.x command-line program name.
|
2016-09-06 09:23:43 +02:00
|
|
|
(make-parameter (or (getenv "GUIX_GPG_COMMAND") "gpg")))
|
2013-05-11 12:44:19 +02:00
|
|
|
|
2018-09-09 22:15:19 +02:00
|
|
|
(define %gpgv-command
|
|
|
|
;; The 'gpgv' program.
|
|
|
|
(make-parameter (or (getenv "GUIX_GPGV_COMMAND") "gpgv")))
|
|
|
|
|
|
|
|
(define current-keyring
|
|
|
|
;; The default keyring of "trusted keys".
|
|
|
|
(make-parameter (string-append (config-directory #:ensure? #f)
|
|
|
|
"/gpg/trustedkeys.kbx")))
|
|
|
|
|
2013-05-11 12:44:19 +02:00
|
|
|
(define %openpgp-key-server
|
2021-11-14 03:43:45 +01:00
|
|
|
;; The default key server. It defaults to #f, which causes GnuPG to use the
|
|
|
|
;; one it is configured with.
|
|
|
|
(make-parameter #f))
|
2013-04-24 23:48:36 +02:00
|
|
|
|
2019-12-19 22:16:50 +01:00
|
|
|
;; Regexps for status lines. See file `doc/DETAILS' in GnuPG.
|
|
|
|
|
|
|
|
(define sigid-rx
|
|
|
|
(make-regexp
|
|
|
|
"^\\[GNUPG:\\] SIG_ID ([A-Za-z0-9+/]+) ([[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}) ([[:digit:]]+)"))
|
|
|
|
(define goodsig-rx
|
|
|
|
(make-regexp "^\\[GNUPG:\\] GOODSIG ([[:xdigit:]]+) (.+)$"))
|
|
|
|
(define validsig-rx
|
|
|
|
(make-regexp
|
|
|
|
"^\\[GNUPG:\\] VALIDSIG ([[:xdigit:]]+) ([[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}) ([[:digit:]]+) .*$"))
|
|
|
|
(define expkeysig-rx ; good signature, but expired key
|
|
|
|
(make-regexp "^\\[GNUPG:\\] EXPKEYSIG ([[:xdigit:]]+) (.*)$"))
|
2020-04-17 23:25:17 +02:00
|
|
|
(define revkeysig-rx ; good signature, but revoked key
|
|
|
|
(make-regexp "^\\[GNUPG:\\] REVKEYSIG ([[:xdigit:]]+) (.*)$"))
|
2019-12-19 22:16:50 +01:00
|
|
|
(define errsig-rx
|
|
|
|
;; Note: The fingeprint part (the last element of the line) appeared in
|
|
|
|
;; GnuPG 2.2.7 according to 'doc/DETAILS', and it may be missing.
|
|
|
|
(make-regexp
|
|
|
|
"^\\[GNUPG:\\] ERRSIG ([[:xdigit:]]+) ([^ ]+) ([^ ]+) ([^ ]+) ([[:digit:]]+) ([[:digit:]]+)(.*)"))
|
|
|
|
|
|
|
|
|
2018-09-09 22:15:19 +02:00
|
|
|
(define* (gnupg-verify sig file
|
|
|
|
#:optional (keyring (current-keyring)))
|
|
|
|
"Verify signature SIG for FILE against the keys in KEYRING. All the keys in
|
|
|
|
KEYRING as assumed to be \"trusted\", whether or not they expired or were
|
|
|
|
revoked. Return a status s-exp if GnuPG failed."
|
2013-04-24 23:48:36 +02:00
|
|
|
|
2019-12-18 17:37:02 +01:00
|
|
|
(define (maybe-fingerprint str)
|
|
|
|
(match (string-trim-both str)
|
|
|
|
((or "-" "") #f)
|
|
|
|
(fpr fpr)))
|
|
|
|
|
2013-04-24 23:48:36 +02:00
|
|
|
(define (status-line->sexp line)
|
|
|
|
(cond ((regexp-exec sigid-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
|
|
|
`(signature-id ,(match:substring match 1) ; sig id
|
|
|
|
,(match:substring match 2) ; date
|
|
|
|
,(string->number ; timestamp
|
|
|
|
(match:substring match 3)))))
|
|
|
|
((regexp-exec goodsig-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
|
|
|
`(good-signature ,(match:substring match 1) ; key id
|
|
|
|
,(match:substring match 2)))) ; user name
|
|
|
|
((regexp-exec validsig-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
|
|
|
`(valid-signature ,(match:substring match 1) ; fingerprint
|
|
|
|
,(match:substring match 2) ; sig creation date
|
|
|
|
,(string->number ; timestamp
|
|
|
|
(match:substring match 3)))))
|
|
|
|
((regexp-exec expkeysig-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
|
|
|
`(expired-key-signature ,(match:substring match 1) ; fingerprint
|
|
|
|
,(match:substring match 2)))) ; user name
|
2020-04-17 23:25:17 +02:00
|
|
|
((regexp-exec revkeysig-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
|
|
|
`(revoked-key-signature ,(match:substring match 1) ; fingerprint
|
|
|
|
,(match:substring match 2)))) ; user name
|
2013-04-24 23:48:36 +02:00
|
|
|
((regexp-exec errsig-rx line)
|
|
|
|
=>
|
|
|
|
(lambda (match)
|
2019-12-18 17:37:02 +01:00
|
|
|
`(signature-error ,(match:substring match 1) ; key id
|
2013-04-24 23:48:36 +02:00
|
|
|
,(match:substring match 2) ; pubkey algo
|
|
|
|
,(match:substring match 3) ; hash algo
|
|
|
|
,(match:substring match 4) ; sig class
|
|
|
|
,(string->number ; timestamp
|
|
|
|
(match:substring match 5))
|
|
|
|
,(let ((rc
|
|
|
|
(string->number ; return code
|
|
|
|
(match:substring match 6))))
|
|
|
|
(case rc
|
|
|
|
((9) 'missing-key)
|
|
|
|
((4) 'unknown-algorithm)
|
2019-12-18 17:37:02 +01:00
|
|
|
(else rc)))
|
|
|
|
,(maybe-fingerprint ; fingerprint or #f
|
|
|
|
(match:substring match 7)))))
|
2013-04-24 23:48:36 +02:00
|
|
|
(else
|
|
|
|
`(unparsed-line ,line))))
|
|
|
|
|
|
|
|
(define (parse-status input)
|
|
|
|
(let loop ((line (read-line input))
|
|
|
|
(result '()))
|
|
|
|
(if (eof-object? line)
|
|
|
|
(reverse result)
|
|
|
|
(loop (read-line input)
|
|
|
|
(cons (status-line->sexp line) result)))))
|
|
|
|
|
2018-09-09 22:15:19 +02:00
|
|
|
(let* ((pipe (open-pipe* OPEN_READ (%gpgv-command) "--status-fd=1"
|
|
|
|
"--keyring" keyring sig file))
|
2013-04-24 23:48:36 +02:00
|
|
|
(status (parse-status pipe)))
|
|
|
|
;; Ignore PIPE's exit status since STATUS above should contain all the
|
|
|
|
;; info we need.
|
|
|
|
(close-pipe pipe)
|
|
|
|
status))
|
|
|
|
|
|
|
|
(define (gnupg-status-good-signature? status)
|
|
|
|
"If STATUS, as returned by `gnupg-verify', denotes a good signature, return
|
2019-12-18 17:19:00 +01:00
|
|
|
a fingerprint/user pair; return #f otherwise."
|
|
|
|
(match (assq 'valid-signature status)
|
|
|
|
(('valid-signature fingerprint date timestamp)
|
|
|
|
(match (or (assq 'good-signature status)
|
2020-04-17 23:25:17 +02:00
|
|
|
(assq 'expired-key-signature status)
|
|
|
|
(assq 'revoked-key-signature status))
|
2019-12-18 17:19:00 +01:00
|
|
|
((_ key-id user) (cons fingerprint user))
|
|
|
|
(_ #f)))
|
|
|
|
(_
|
|
|
|
#f)))
|
2013-04-24 23:48:36 +02:00
|
|
|
|
|
|
|
(define (gnupg-status-missing-key? status)
|
2019-12-18 17:37:02 +01:00
|
|
|
"If STATUS denotes a missing-key error, then return the fingerprint of the
|
|
|
|
missing key or its key id if the fingerprint is unavailable."
|
2013-04-24 23:48:36 +02:00
|
|
|
(any (lambda (sexp)
|
|
|
|
(match sexp
|
2019-12-18 17:37:02 +01:00
|
|
|
(('signature-error key-id _ ... 'missing-key fingerprint)
|
|
|
|
(or fingerprint key-id))
|
2013-04-24 23:48:36 +02:00
|
|
|
(_ #f)))
|
|
|
|
status))
|
|
|
|
|
2021-11-14 03:43:45 +01:00
|
|
|
(define* (gnupg-receive-keys fingerprint/key-id
|
|
|
|
#:key server (keyring (current-keyring)))
|
|
|
|
"Download FINGERPRINT/KEY-ID from SERVER if specified, otherwise from
|
|
|
|
GnuPG's default/configured one. The key is added to KEYRING."
|
2018-09-09 22:15:19 +02:00
|
|
|
(unless (file-exists? keyring)
|
|
|
|
(mkdir-p (dirname keyring))
|
2021-11-14 03:43:45 +01:00
|
|
|
(call-with-output-file keyring (const #t))) ;create an empty keybox
|
2018-09-09 22:15:19 +02:00
|
|
|
|
2021-11-14 03:43:45 +01:00
|
|
|
(zero? (apply system*
|
|
|
|
`(,(%gpg-command)
|
|
|
|
,@(if server
|
|
|
|
(list "--keyserver" server)
|
|
|
|
'())
|
|
|
|
"--no-default-keyring" "--keyring" ,keyring
|
|
|
|
"--recv-keys" ,fingerprint/key-id))))
|
2013-04-24 23:48:36 +02:00
|
|
|
|
2013-06-10 09:46:13 +02:00
|
|
|
(define* (gnupg-verify* sig file
|
2018-09-09 22:15:19 +02:00
|
|
|
#:key
|
|
|
|
(key-download 'interactive)
|
2021-11-14 03:43:45 +01:00
|
|
|
server
|
2018-09-09 22:15:19 +02:00
|
|
|
(keyring (current-keyring)))
|
2013-04-24 23:48:36 +02:00
|
|
|
"Like `gnupg-verify', but try downloading the public key if it's missing.
|
2019-12-20 21:49:43 +01:00
|
|
|
Return two values: 'valid-signature and a fingerprint/name pair upon success,
|
|
|
|
'missing-key and a fingerprint if the key could not be found, and
|
|
|
|
'invalid-signature with a fingerprint if the signature is invalid.
|
|
|
|
|
|
|
|
KEY-DOWNLOAD specifies a download policy for missing OpenPGP keys; allowed
|
|
|
|
values: 'always', 'never', and 'interactive' (default). Return a
|
|
|
|
fingerprint/user name pair on success and #f otherwise."
|
2013-04-24 23:48:36 +02:00
|
|
|
(let ((status (gnupg-verify sig file)))
|
2019-12-20 21:49:43 +01:00
|
|
|
(match (gnupg-status-good-signature? status)
|
|
|
|
((fingerprint . user)
|
|
|
|
(values 'valid-signature (cons fingerprint user)))
|
|
|
|
(#f
|
|
|
|
(let ((missing (gnupg-status-missing-key? status)))
|
|
|
|
(define (download-and-try-again)
|
|
|
|
;; Download the missing key and try again.
|
2021-11-14 03:43:45 +01:00
|
|
|
(if (gnupg-receive-keys missing #:server server #:keyring keyring)
|
2019-12-20 21:49:43 +01:00
|
|
|
(match (gnupg-status-good-signature?
|
|
|
|
(gnupg-verify sig file keyring))
|
|
|
|
(#f
|
|
|
|
(values 'invalid-signature missing))
|
|
|
|
((fingerprint . user)
|
|
|
|
(values 'valid-signature
|
|
|
|
(cons fingerprint user))))
|
|
|
|
(values 'missing-key missing)))
|
|
|
|
|
|
|
|
(define (receive?)
|
|
|
|
(let ((answer
|
|
|
|
(begin
|
|
|
|
(format #t (G_ "Would you like to add this key \
|
2018-09-09 22:15:19 +02:00
|
|
|
to keyring '~a'?~%")
|
2019-12-20 21:49:43 +01:00
|
|
|
keyring)
|
|
|
|
(read-line))))
|
|
|
|
(string-match (locale-yes-regexp) answer)))
|
|
|
|
|
|
|
|
(case key-download
|
|
|
|
((never)
|
|
|
|
(values 'missing-key missing))
|
|
|
|
((always)
|
|
|
|
(download-and-try-again))
|
|
|
|
(else
|
|
|
|
(if (receive?)
|
|
|
|
(download-and-try-again)
|
|
|
|
(values 'missing-key missing)))))))))
|
2013-04-24 23:48:36 +02:00
|
|
|
|
|
|
|
;;; gnupg.scm ends here
|