You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
8.0 KiB

  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. (define-module (guix pki)
  19. #:use-module (guix config)
  20. #:use-module (guix pk-crypto)
  21. #:use-module ((guix utils) #:select (with-atomic-file-output))
  22. #:use-module ((guix build utils) #:select (mkdir-p))
  23. #:use-module (ice-9 match)
  24. #:use-module (rnrs io ports)
  25. #:export (%public-key-file
  26. %private-key-file
  27. %acl-file
  28. current-acl
  29. public-keys->acl
  30. acl->public-keys
  31. authorized-key?
  32. write-acl
  33. signature-sexp
  34. signature-subject
  35. signature-signed-data
  36. valid-signature?
  37. signature-case))
  38. ;;; Commentary:
  39. ;;;
  40. ;;; Public key infrastructure for the authentication and authorization of
  41. ;;; archive imports. This is essentially a subset of SPKI for our own
  42. ;;; purposes (see <http://theworld.com/~cme/spki.txt> and
  43. ;;; <http://www.ietf.org/rfc/rfc2693.txt>.)
  44. ;;;
  45. ;;; Code:
  46. (define (public-keys->acl keys)
  47. "Return an ACL that lists all of KEYS with a '(guix import)'
  48. tag---meaning that all of KEYS are authorized for archive imports. Each
  49. element in KEYS must be a canonical sexp with type 'public-key'."
  50. ;; Use SPKI-style ACL entry sexp for PUBLIC-KEY, authorizing imports
  51. ;; signed by the corresponding secret key (see the IETF draft at
  52. ;; <http://theworld.com/~cme/spki.txt> for the ACL format.)
  53. ;;
  54. ;; Note: We always use PUBLIC-KEY to designate the subject. Someday we may
  55. ;; want to have name certificates and to use subject names instead of
  56. ;; complete keys.
  57. `(acl ,@(map (lambda (key)
  58. `(entry ,(canonical-sexp->sexp key)
  59. (tag (guix import))))
  60. keys)))
  61. (define %acl-file
  62. (string-append %config-directory "/acl"))
  63. (define %public-key-file
  64. (string-append %config-directory "/signing-key.pub"))
  65. (define %private-key-file
  66. (string-append %config-directory "/signing-key.sec"))
  67. (define (ensure-acl)
  68. "Make sure the ACL file exists, and create an initialized one if needed."
  69. (unless (file-exists? %acl-file)
  70. ;; If there's no public key file, don't attempt to create the ACL.
  71. (when (file-exists? %public-key-file)
  72. (let ((public-key (call-with-input-file %public-key-file
  73. (compose string->canonical-sexp
  74. get-string-all))))
  75. (mkdir-p (dirname %acl-file))
  76. (with-atomic-file-output %acl-file
  77. (lambda (port)
  78. (write-acl (public-keys->acl (list public-key))
  79. port)))))))
  80. (define (write-acl acl port)
  81. "Write ACL to PORT in canonical-sexp format."
  82. (let ((sexp (sexp->canonical-sexp acl)))
  83. (display (canonical-sexp->string sexp) port)))
  84. (define (current-acl)
  85. "Return the current ACL."
  86. (ensure-acl)
  87. (if (file-exists? %acl-file)
  88. (call-with-input-file %acl-file
  89. (compose canonical-sexp->sexp
  90. string->canonical-sexp
  91. get-string-all))
  92. (public-keys->acl '()))) ; the empty ACL
  93. (define (acl->public-keys acl)
  94. "Return the public keys (as canonical sexps) listed in ACL with the '(guix
  95. import)' tag."
  96. (match acl
  97. (('acl
  98. ('entry subject-keys
  99. ('tag ('guix 'import)))
  100. ...)
  101. (map sexp->canonical-sexp subject-keys))
  102. (_
  103. (error "invalid access-control list" acl))))
  104. (define* (authorized-key? key #:optional (acl (current-acl)))
  105. "Return #t if KEY (a canonical sexp) is an authorized public key for archive
  106. imports according to ACL."
  107. ;; Note: ACL is kept in native sexp form to make 'authorized-key?' faster,
  108. ;; by not having to convert it with 'canonical-sexp->sexp' on each call.
  109. ;; TODO: We could use a better data type for ACLs.
  110. (let ((key (canonical-sexp->sexp key)))
  111. (match acl
  112. (('acl
  113. ('entry subject-keys
  114. ('tag ('guix 'import)))
  115. ...)
  116. (not (not (member key subject-keys))))
  117. (_
  118. (error "invalid access-control list" acl)))))
  119. (define (signature-sexp data secret-key public-key)
  120. "Return a SPKI-style sexp for the signature of DATA with SECRET-KEY that
  121. includes DATA, the actual signature value (with a 'sig-val' tag), and
  122. PUBLIC-KEY (see <http://theworld.com/~cme/spki.txt> for examples.)"
  123. (string->canonical-sexp
  124. (format #f
  125. "(signature ~a ~a ~a)"
  126. (canonical-sexp->string data)
  127. (canonical-sexp->string (sign data secret-key))
  128. (canonical-sexp->string public-key))))
  129. (define (signature-subject sig)
  130. "Return the signer's public key for SIG."
  131. (find-sexp-token sig 'public-key))
  132. (define (signature-signed-data sig)
  133. "Return the signed data from SIG, typically an sexp such as
  134. (hash \"sha256\" #...#)."
  135. (find-sexp-token sig 'data))
  136. (define (valid-signature? sig)
  137. "Return #t if SIG is valid."
  138. (let* ((data (signature-signed-data sig))
  139. (signature (find-sexp-token sig 'sig-val))
  140. (public-key (signature-subject sig)))
  141. (and data signature
  142. (verify signature data public-key))))
  143. (define* (%signature-status signature hash
  144. #:optional (acl (current-acl)))
  145. "Return a symbol denoting the status of SIGNATURE vs. HASH vs. ACL.
  146. This procedure must only be used internally, because it would be easy to
  147. forget some of the cases."
  148. (let ((subject (signature-subject signature))
  149. (data (signature-signed-data signature)))
  150. (if (and data subject)
  151. (if (authorized-key? subject acl)
  152. (if (equal? (hash-data->bytevector data) hash)
  153. (if (valid-signature? signature)
  154. 'valid-signature
  155. 'invalid-signature)
  156. 'hash-mismatch)
  157. 'unauthorized-key)
  158. 'corrupt-signature)))
  159. (define-syntax signature-case
  160. (syntax-rules (valid-signature invalid-signature
  161. hash-mismatch unauthorized-key corrupt-signature
  162. else)
  163. "\
  164. Match the cases of the verification of SIGNATURE against HASH and ACL:
  165. - the 'valid-signature' case if SIGNATURE is indeed a signature of HASH with
  166. a key present in ACL;
  167. - 'invalid-signature' if SIGNATURE is incorrect;
  168. - 'hash-mismatch' if the hash in SIGNATURE does not match HASH;
  169. - 'unauthorized-key' if the public key in SIGNATURE is not listed in ACL;
  170. - 'corrupt-signature' if SIGNATURE is not a valid signature sexp.
  171. This macro guarantees at compile-time that all these cases are handled.
  172. SIGNATURE, and ACL must be canonical sexps; HASH must be a bytevector."
  173. ;; Simple case: we only care about valid signatures.
  174. ((_ (signature hash acl)
  175. (valid-signature valid-exp ...)
  176. (else else-exp ...))
  177. (case (%signature-status signature hash acl)
  178. ((valid-signature) valid-exp ...)
  179. (else else-exp ...)))
  180. ;; Full case.
  181. ((_ (signature hash acl)
  182. (valid-signature valid-exp ...)
  183. (invalid-signature invalid-exp ...)
  184. (hash-mismatch mismatch-exp ...)
  185. (unauthorized-key unauthorized-exp ...)
  186. (corrupt-signature corrupt-exp ...))
  187. (case (%signature-status signature hash acl)
  188. ((valid-signature) valid-exp ...)
  189. ((invalid-signature) invalid-exp ...)
  190. ((hash-mismatch) mismatch-exp ...)
  191. ((unauthorized-key) unauthorized-exp ...)
  192. ((corrupt-signature) corrupt-exp ...)
  193. (else (error "bogus signature status"))))))
  194. ;;; pki.scm ends here