narinfo.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix narinfo)
  21. #:use-module (guix pki)
  22. #:use-module (guix i18n)
  23. #:use-module (guix base32)
  24. #:use-module (guix base64)
  25. #:use-module (guix records)
  26. #:use-module (guix diagnostics)
  27. #:use-module (guix scripts substitute)
  28. #:use-module (gcrypt hash)
  29. #:use-module (gcrypt pk-crypto)
  30. #:use-module (rnrs bytevectors)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-9)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (ice-9 match)
  35. #:use-module (ice-9 binary-ports)
  36. #:use-module (web uri)
  37. #:export (narinfo-signature->canonical-sexp
  38. narinfo?
  39. narinfo-path
  40. narinfo-uris
  41. narinfo-uri-base
  42. narinfo-compressions
  43. narinfo-file-hashes
  44. narinfo-file-sizes
  45. narinfo-hash
  46. narinfo-size
  47. narinfo-references
  48. narinfo-deriver
  49. narinfo-system
  50. narinfo-signature
  51. narinfo-contents
  52. narinfo-hash-algorithm+value
  53. narinfo-hash->sha256
  54. narinfo-best-uri
  55. valid-narinfo?
  56. read-narinfo
  57. write-narinfo
  58. string->narinfo
  59. narinfo->string
  60. equivalent-narinfo?))
  61. (define-record-type <narinfo>
  62. (%make-narinfo path uri-base uris compressions file-sizes file-hashes
  63. nar-hash nar-size references deriver system
  64. signature contents)
  65. narinfo?
  66. (path narinfo-path)
  67. (uri-base narinfo-uri-base) ;URI of the cache it originates from
  68. (uris narinfo-uris) ;list of strings
  69. (compressions narinfo-compressions) ;list of strings
  70. (file-sizes narinfo-file-sizes) ;list of (integers | #f)
  71. (file-hashes narinfo-file-hashes)
  72. (nar-hash narinfo-hash)
  73. (nar-size narinfo-size)
  74. (references narinfo-references)
  75. (deriver narinfo-deriver)
  76. (system narinfo-system)
  77. (signature narinfo-signature) ; canonical sexp
  78. ;; The original contents of a narinfo file. This field is needed because we
  79. ;; want to preserve the exact textual representation for verification purposes.
  80. ;; See <https://lists.gnu.org/archive/html/guix-devel/2014-02/msg00340.html>
  81. ;; for more information.
  82. (contents narinfo-contents))
  83. (define (narinfo-hash-algorithm+value narinfo)
  84. "Return two values: the hash algorithm used by NARINFO and its value as a
  85. bytevector."
  86. (match (string-tokenize (narinfo-hash narinfo)
  87. (char-set-complement (char-set #\:)))
  88. ((algorithm base32)
  89. (values (lookup-hash-algorithm (string->symbol algorithm))
  90. (nix-base32-string->bytevector base32)))
  91. (_
  92. (raise (formatted-message
  93. (G_ "invalid narinfo hash: ~s") (narinfo-hash narinfo))))))
  94. (define (narinfo-hash->sha256 hash)
  95. "If the string HASH denotes a sha256 hash, return it as a bytevector.
  96. Otherwise return #f."
  97. (and (string-prefix? "sha256:" hash)
  98. (nix-base32-string->bytevector (string-drop hash 7))))
  99. (define (narinfo-signature->canonical-sexp str)
  100. "Return the value of a narinfo's 'Signature' field as a canonical sexp."
  101. (match (string-split str #\;)
  102. ((version host-name sig)
  103. (let ((maybe-number (string->number version)))
  104. (cond ((not (number? maybe-number))
  105. (leave (G_ "signature version must be a number: ~s~%")
  106. version))
  107. ;; Currently, there are no other versions.
  108. ((not (= 1 maybe-number))
  109. (leave (G_ "unsupported signature version: ~a~%")
  110. maybe-number))
  111. (else
  112. (let ((signature (utf8->string (base64-decode sig))))
  113. (catch 'gcry-error
  114. (lambda ()
  115. (string->canonical-sexp signature))
  116. (lambda (key proc err)
  117. (leave (G_ "signature is not a valid \
  118. s-expression: ~s~%")
  119. signature))))))))
  120. (x
  121. (leave (G_ "invalid format of the signature field: ~a~%") x))))
  122. (define (narinfo-maker str cache-url)
  123. "Return a narinfo constructor for narinfos originating from CACHE-URL. STR
  124. must contain the original contents of a narinfo file."
  125. (lambda (path urls compressions file-hashes file-sizes
  126. nar-hash nar-size references deriver system
  127. signature)
  128. "Return a new <narinfo> object."
  129. (define len (length urls))
  130. (%make-narinfo path cache-url
  131. ;; Handle the case where URL is a relative URL.
  132. (map (lambda (url)
  133. (or (string->uri url)
  134. (string->uri
  135. (string-append cache-url "/" url))))
  136. urls)
  137. compressions
  138. (match file-sizes
  139. (() (make-list len #f))
  140. ((lst ...) (map string->number lst)))
  141. (match file-hashes
  142. (() (make-list len #f))
  143. ((lst ...) (map string->number lst)))
  144. nar-hash
  145. (and=> nar-size string->number)
  146. (string-tokenize references)
  147. (match deriver
  148. ((or #f "") #f)
  149. (_ deriver))
  150. system
  151. (false-if-exception
  152. (and=> signature narinfo-signature->canonical-sexp))
  153. str)))
  154. (define fields->alist
  155. ;; The narinfo format is really just like recutils.
  156. recutils->alist)
  157. (define* (read-narinfo port #:optional url
  158. #:key size)
  159. "Read a narinfo from PORT. If URL is true, it must be a string used to
  160. build full URIs from relative URIs found while reading PORT. When SIZE is
  161. true, read at most SIZE bytes from PORT; otherwise, read as much as possible.
  162. No authentication and authorization checks are performed here!"
  163. (let ((str (utf8->string (if size
  164. (get-bytevector-n port size)
  165. (get-bytevector-all port)))))
  166. (alist->record (call-with-input-string str fields->alist)
  167. (narinfo-maker str url)
  168. '("StorePath" "URL" "Compression"
  169. "FileHash" "FileSize" "NarHash" "NarSize"
  170. "References" "Deriver" "System"
  171. "Signature")
  172. '("URL" "Compression" "FileSize" "FileHash"))))
  173. (define (narinfo-sha256 narinfo)
  174. "Return the sha256 hash of NARINFO as a bytevector, or #f if NARINFO lacks a
  175. 'Signature' field."
  176. (define %mandatory-fields
  177. ;; List of fields that must be signed. If they are not signed, the
  178. ;; narinfo is considered unsigned.
  179. '("StorePath" "NarHash" "References"))
  180. (let ((contents (narinfo-contents narinfo)))
  181. (match (string-contains contents "Signature:")
  182. (#f #f)
  183. (index
  184. (let* ((above-signature (string-take contents index))
  185. (signed-fields (match (call-with-input-string above-signature
  186. fields->alist)
  187. (((fields . values) ...) fields))))
  188. (and (every (cut member <> signed-fields) %mandatory-fields)
  189. (sha256 (string->utf8 above-signature))))))))
  190. (define* (valid-narinfo? narinfo #:optional (acl (current-acl))
  191. #:key verbose?)
  192. "Return #t if NARINFO's signature is not valid."
  193. (let ((hash (narinfo-sha256 narinfo))
  194. (signature (narinfo-signature narinfo))
  195. (uri (uri->string (first (narinfo-uris narinfo)))))
  196. (and hash signature
  197. (signature-case (signature hash acl)
  198. (valid-signature #t)
  199. (invalid-signature
  200. (when verbose?
  201. (format (current-error-port)
  202. "invalid signature for substitute at '~a'~%"
  203. uri))
  204. #f)
  205. (hash-mismatch
  206. (when verbose?
  207. (format (current-error-port)
  208. "hash mismatch for substitute at '~a'~%"
  209. uri))
  210. #f)
  211. (unauthorized-key
  212. (when verbose?
  213. (format (current-error-port)
  214. "substitute at '~a' is signed by an \
  215. unauthorized party~%"
  216. uri))
  217. #f)
  218. (corrupt-signature
  219. (when verbose?
  220. (format (current-error-port)
  221. "corrupt signature for substitute at '~a'~%"
  222. uri))
  223. #f)))))
  224. (define (write-narinfo narinfo port)
  225. "Write NARINFO to PORT."
  226. (put-bytevector port (string->utf8 (narinfo-contents narinfo))))
  227. (define (narinfo->string narinfo)
  228. "Return the external representation of NARINFO."
  229. (call-with-output-string (cut write-narinfo narinfo <>)))
  230. (define (string->narinfo str cache-uri)
  231. "Return the narinfo represented by STR. Assume CACHE-URI as the base URI of
  232. the cache STR originates form."
  233. (call-with-input-string str (cut read-narinfo <> cache-uri)))
  234. (define (equivalent-narinfo? narinfo1 narinfo2)
  235. "Return true if NARINFO1 and NARINFO2 are equivalent--i.e., if they describe
  236. the same store item. This ignores unnecessary metadata such as the Nar URL."
  237. (and (string=? (narinfo-hash narinfo1)
  238. (narinfo-hash narinfo2))
  239. ;; The following is not needed if all we want is to download a valid
  240. ;; nar, but it's necessary if we want valid narinfo.
  241. (string=? (narinfo-path narinfo1)
  242. (narinfo-path narinfo2))
  243. (equal? (narinfo-references narinfo1)
  244. (narinfo-references narinfo2))
  245. (= (narinfo-size narinfo1)
  246. (narinfo-size narinfo2))))
  247. (define %compression-methods
  248. ;; Known compression methods and a thunk to determine whether they're
  249. ;; supported. See 'decompressed-port' in (guix utils).
  250. `(("gzip" . ,(const #t))
  251. ("lzip" . ,(const #t))
  252. ("zstd" . ,(lambda ()
  253. (resolve-module '(zstd) #t #f #:ensure #f)))
  254. ("xz" . ,(const #t))
  255. ("bzip2" . ,(const #t))
  256. ("none" . ,(const #t))))
  257. (define (supported-compression? compression)
  258. "Return true if COMPRESSION, a string, denotes a supported compression
  259. method."
  260. (match (assoc-ref %compression-methods compression)
  261. (#f #f)
  262. (supported? (supported?))))
  263. (define (compresses-better? compression1 compression2)
  264. "Return true if COMPRESSION1 generally compresses better than COMPRESSION2;
  265. this is a rough approximation."
  266. (match compression1
  267. ("none" #f)
  268. ("gzip" (string=? compression2 "none"))
  269. ("lzip" #t)
  270. (_ (or (string=? compression2 "none")
  271. (string=? compression2 "gzip")))))
  272. (define (narinfo-best-uri narinfo)
  273. "Select the \"best\" URI to download NARINFO's nar, and return three values:
  274. the URI, its compression method (a string), and the compressed file size."
  275. (define choices
  276. (filter (match-lambda
  277. ((uri compression file-size)
  278. (supported-compression? compression)))
  279. (zip (narinfo-uris narinfo)
  280. (narinfo-compressions narinfo)
  281. (narinfo-file-sizes narinfo))))
  282. (define (file-size<? c1 c2)
  283. (match c1
  284. ((uri1 compression1 (? integer? file-size1))
  285. (match c2
  286. ((uri2 compression2 (? integer? file-size2))
  287. (< file-size1 file-size2))
  288. (_ #t)))
  289. ((uri compression1 #f)
  290. (match c2
  291. ((uri2 compression2 _)
  292. (compresses-better? compression1 compression2))))
  293. (_ #f))) ;we can't tell
  294. (match (sort choices file-size<?)
  295. (((uri compression file-size) _ ...)
  296. (values uri compression file-size))))