Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

222 рядки
8.0 KiB

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