authenticate.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020 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. (define-module (guix scripts authenticate)
  19. #:use-module (guix scripts)
  20. #:use-module (guix base16)
  21. #:use-module (gcrypt pk-crypto)
  22. #:use-module (guix pki)
  23. #:use-module (guix ui)
  24. #:use-module (guix diagnostics)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-35)
  27. #:use-module (srfi srfi-71)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (ice-9 binary-ports)
  30. #:use-module (ice-9 rdelim)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 vlist)
  33. #:use-module (ice-9 iconv)
  34. #:export (guix-authenticate))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; This program is used internally by the daemon to sign exported archive
  38. ;;; (the 'export-paths' RPC), and to authenticate imported archives (the
  39. ;;; 'import-paths' RPC.)
  40. ;;;
  41. ;;; Code:
  42. (define read-canonical-sexp
  43. ;; Read a gcrypt sexp from a port and return it.
  44. (compose string->canonical-sexp read-string))
  45. (define (load-key-pair key-file)
  46. "Load the key pair whose secret key lives at KEY-FILE. Return a pair of
  47. canonical sexps representing those keys."
  48. (catch 'system-error
  49. (lambda ()
  50. (let* ((secret-key (call-with-input-file key-file read-canonical-sexp))
  51. (public-key (call-with-input-file
  52. (string-append (string-drop-right key-file 4)
  53. ".pub")
  54. read-canonical-sexp)))
  55. (cons public-key secret-key)))
  56. (lambda args
  57. (let ((errno (system-error-errno args)))
  58. (raise
  59. (formatted-message
  60. (G_ "failed to load key pair at '~a': ~a~%")
  61. key-file (strerror errno)))))))
  62. (define (sign-with-key public-key secret-key sha256)
  63. "Sign the hash SHA256 (a bytevector) with SECRET-KEY (a canonical sexp), and
  64. return the signature as a canonical sexp that includes SHA256, PUBLIC-KEY, and
  65. the actual signature."
  66. (let ((data (bytevector->hash-data sha256
  67. #:key-type (key-type public-key))))
  68. (signature-sexp data secret-key public-key)))
  69. (define (validate-signature signature acl)
  70. "Validate SIGNATURE, a canonical sexp. Check whether its public key is
  71. authorized in ACL, verify the signature, and return the signed data (a
  72. bytevector) upon success."
  73. (let* ((subject (signature-subject signature))
  74. (data (signature-signed-data signature)))
  75. (if (and data subject)
  76. (if (authorized-key? subject acl)
  77. (if (valid-signature? signature)
  78. (hash-data->bytevector data) ; success
  79. (raise
  80. (formatted-message (G_ "invalid signature: ~a")
  81. (canonical-sexp->string signature))))
  82. (raise
  83. (formatted-message (G_ "unauthorized public key: ~a")
  84. (canonical-sexp->string subject))))
  85. (raise
  86. (formatted-message (G_ "corrupt signature data: ~a")
  87. (canonical-sexp->string signature))))))
  88. (define (read-command port)
  89. "Read a command from PORT and return the command and arguments as a list of
  90. strings. Return the empty list when the end-of-file is reached.
  91. Commands are newline-terminated and must look something like this:
  92. COMMAND 3:abc 5:abcde 1:x
  93. where COMMAND is an alphanumeric sequence and the remainder is the command
  94. arguments. Each argument is written as its length (in characters), followed
  95. by colon, followed by the given number of characters."
  96. (define (consume-whitespace port)
  97. (let ((chr (lookahead-u8 port)))
  98. (when (eqv? chr (char->integer #\space))
  99. (get-u8 port)
  100. (consume-whitespace port))))
  101. (match (read-delimited " \t\n\r" port)
  102. ((? eof-object?)
  103. '())
  104. (command
  105. (let loop ((result (list command)))
  106. (consume-whitespace port)
  107. (let ((next (lookahead-u8 port)))
  108. (cond ((eqv? next (char->integer #\newline))
  109. (get-u8 port)
  110. (reverse result))
  111. ((eof-object? next)
  112. (reverse result))
  113. (else
  114. (let* ((len (string->number (read-delimited ":" port)))
  115. (str (bytevector->string
  116. (get-bytevector-n port len)
  117. "ISO-8859-1" 'error)))
  118. (loop (cons str result))))))))))
  119. (define-syntax define-enumerate-type ;TODO: factorize
  120. (syntax-rules ()
  121. ((_ name->int (name id) ...)
  122. (define-syntax name->int
  123. (syntax-rules (name ...)
  124. ((_ name) id) ...)))))
  125. ;; Codes used when reply to requests.
  126. (define-enumerate-type reply-code
  127. (success 0)
  128. (command-not-found 404)
  129. (command-failed 500))
  130. ;;;
  131. ;;; Entry point.
  132. ;;;
  133. (define-command (guix-authenticate . args)
  134. (category internal)
  135. (synopsis "sign or verify signatures on normalized archives (nars)")
  136. (define (send-reply code str)
  137. ;; Send CODE and STR as a reply to our client.
  138. (let ((bv (string->bytevector str "ISO-8859-1" 'error)))
  139. (format #t "~a ~a:" code (bytevector-length bv))
  140. (put-bytevector (current-output-port) bv)
  141. (force-output (current-output-port))))
  142. (define (call-with-reply thunk)
  143. ;; Send a reply for the result of THUNK or for any exception raised during
  144. ;; its execution.
  145. (guard (c ((formatted-message? c)
  146. (send-reply (reply-code command-failed)
  147. (apply format #f
  148. (G_ (formatted-message-string c))
  149. (formatted-message-arguments c)))))
  150. (send-reply (reply-code success) (thunk))))
  151. (define-syntax-rule (with-reply exp ...)
  152. (call-with-reply (lambda () exp ...)))
  153. ;; Signature sexps written to stdout may contain binary data, so force
  154. ;; ISO-8859-1 encoding so that things are not mangled. See
  155. ;; <http://bugs.gnu.org/17312> for details.
  156. (set-port-encoding! (current-output-port) "ISO-8859-1")
  157. (set-port-conversion-strategy! (current-output-port) 'error)
  158. ;; Same goes for input ports.
  159. (with-fluids ((%default-port-encoding "ISO-8859-1")
  160. (%default-port-conversion-strategy 'error))
  161. (match args
  162. (("--help")
  163. (display (G_ "Usage: guix authenticate OPTION...
  164. Sign data or verify signatures. This tool is meant to be used internally by
  165. 'guix-daemon'.\n")))
  166. (("--version")
  167. (show-version-and-exit "guix authenticate"))
  168. (()
  169. (let ((acl (current-acl)))
  170. (let loop ((key-pairs vlist-null))
  171. ;; Read a request on standard input and reply.
  172. (match (read-command (current-input-port))
  173. (("sign" signing-key (= base16-string->bytevector hash))
  174. (let* ((key-pairs keys
  175. (match (vhash-assoc signing-key key-pairs)
  176. ((_ . keys)
  177. (values key-pairs keys))
  178. (#f
  179. (let ((keys (load-key-pair signing-key)))
  180. (values (vhash-cons signing-key keys
  181. key-pairs)
  182. keys))))))
  183. (with-reply (canonical-sexp->string
  184. (match keys
  185. ((public . secret)
  186. (sign-with-key public secret hash)))))
  187. (loop key-pairs)))
  188. (("verify" signature)
  189. (with-reply (bytevector->base16-string
  190. (validate-signature
  191. (string->canonical-sexp signature)
  192. acl)))
  193. (loop key-pairs))
  194. (()
  195. (exit 0))
  196. (commands
  197. (warning (G_ "~s: invalid command; ignoring~%") commands)
  198. (send-reply (reply-code command-not-found)
  199. "invalid command")
  200. (loop key-pairs))))))
  201. (_
  202. (leave (G_ "wrong arguments~%"))))))
  203. ;;; authenticate.scm ends here