openpgp.scm 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. ;; -*- mode: scheme; coding: utf-8 -*-
  2. ;; Copyright © 2010, 2012 Göran Weinholt <goran@weinholt.se>
  3. ;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;; Permission is hereby granted, free of charge, to any person obtaining a
  5. ;; copy of this software and associated documentation files (the "Software"),
  6. ;; to deal in the Software without restriction, including without limitation
  7. ;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. ;; and/or sell copies of the Software, and to permit persons to whom the
  9. ;; Software is furnished to do so, subject to the following conditions:
  10. ;; The above copyright notice and this permission notice shall be included in
  11. ;; all copies or substantial portions of the Software.
  12. ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. ;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. ;; DEALINGS IN THE SOFTWARE.
  19. ;;; This code was originally written by Göran Weinholt for Industria and
  20. ;;; released under the Expat license shown above. It was then modified by
  21. ;;; Ludovic Courtès for use in GNU Guix: turned into a native Guile module,
  22. ;;; ported to Guile-Gcrypt, and extended and simplified in other ways.
  23. (define-module (guix openpgp)
  24. #:export (get-openpgp-detached-signature/ascii
  25. (get-packet . get-openpgp-packet)
  26. verify-openpgp-signature
  27. port-ascii-armored?
  28. openpgp-error?
  29. openpgp-unrecognized-packet-error?
  30. openpgp-unrecognized-packet-error-port
  31. openpgp-invalid-signature-error?
  32. openpgp-invalid-signature-error-port
  33. openpgp-signature?
  34. openpgp-signature-issuer-key-id
  35. openpgp-signature-issuer-fingerprint
  36. openpgp-signature-public-key-algorithm
  37. openpgp-signature-hash-algorithm
  38. openpgp-signature-creation-time
  39. openpgp-signature-expiration-time
  40. openpgp-user-id?
  41. openpgp-user-id-value
  42. openpgp-user-attribute?
  43. openpgp-public-key?
  44. openpgp-public-key-subkey?
  45. openpgp-public-key-value
  46. openpgp-public-key-fingerprint openpgp-format-fingerprint
  47. openpgp-public-key-id
  48. openpgp-keyring?
  49. %empty-keyring
  50. lookup-key-by-id
  51. lookup-key-by-fingerprint
  52. get-openpgp-keyring
  53. read-radix-64
  54. string->openpgp-packet)
  55. #:use-module (rnrs bytevectors)
  56. #:use-module (rnrs io ports)
  57. #:use-module (srfi srfi-1)
  58. #:use-module (srfi srfi-9)
  59. #:use-module (srfi srfi-11)
  60. #:use-module (srfi srfi-19)
  61. #:use-module (srfi srfi-26)
  62. #:use-module (srfi srfi-34)
  63. #:use-module (srfi srfi-35)
  64. #:use-module (srfi srfi-60)
  65. #:use-module (ice-9 match)
  66. #:use-module ((ice-9 rdelim) #:select (read-line))
  67. #:use-module (ice-9 vlist)
  68. #:use-module (gcrypt hash)
  69. #:use-module (gcrypt pk-crypto)
  70. #:use-module (gcrypt base64)
  71. #:use-module (gcrypt base16)
  72. #:use-module ((guix build utils) #:select (dump-port)))
  73. ;;; Commentary:
  74. ;;;
  75. ;;; This module contains code to read OpenPGP messages as described in
  76. ;;; <https://tools.ietf.org/html/rfc4880>, with extensions from
  77. ;;; <https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-06> (notably
  78. ;;; EdDSA support and extra signature sub-packets).
  79. ;;;
  80. ;;; Currently this module does enough to verify detached signatures of binary
  81. ;;; data. It does _not_ perform sanity checks on self-signatures, subkey
  82. ;;; binding signatures, etc., among others. Use only in a context where this
  83. ;;; limitations are acceptable!
  84. ;;;
  85. ;;; Code:
  86. (define-syntax print
  87. (syntax-rules ()
  88. ;; ((_ args ...) (pk 'openpgp args))
  89. ((_ args ...) (values))))
  90. (define-syntax-rule (define-alias new old)
  91. (define-syntax new (identifier-syntax old)))
  92. (define-alias fx+ +)
  93. (define-alias fx- -)
  94. (define-alias fx* *)
  95. (define-alias fx/ /)
  96. (define-alias fxdiv quotient)
  97. (define-alias fxand logand)
  98. (define-inlinable (fxbit-set? n index) (bit-set? index n))
  99. (define-alias fxbit-field bit-field)
  100. (define-alias bitwise-bit-field bit-field)
  101. (define-alias fxarithmetic-shift-left ash)
  102. (define-inlinable (fxarithmetic-shift-right i n) (ash i (- n)))
  103. (define-inlinable (port-eof? port) (eof-object? (lookahead-u8 port)))
  104. (define (string-hex-pad str)
  105. (if (odd? (string-length str))
  106. (string-append "0" str)
  107. str))
  108. (define (unixtime n)
  109. (time-monotonic->date (make-time 'time-monotonic 0 n)))
  110. ;; Root of the error hierarchy.
  111. (define-condition-type &openpgp-error &error
  112. openpgp-error?)
  113. ;; Error raised when reading an unsupported or unrecognized packet tag.
  114. (define-condition-type &openpgp-unrecognized-packet-error &openpgp-error
  115. openpgp-unrecognized-packet-error?
  116. (port openpgp-unrecognized-packet-error-port))
  117. ;; Error raised when reading an invalid signature packet.
  118. (define-condition-type &openpgp-invalid-signature-error &openpgp-error
  119. openpgp-invalid-signature-error?
  120. (port openpgp-invalid-signature-error-port))
  121. ;;;
  122. ;;; Bitwise I/O.
  123. ;;;
  124. ;;; TODO: Use Bytestructures instead.
  125. ;;;
  126. (define-syntax-rule (integer-read size)
  127. (lambda (port)
  128. "Read from PORT a big-endian integer of SIZE bytes. Return the EOF object
  129. on end-of-file."
  130. (let ((buf (make-bytevector size)))
  131. (match (get-bytevector-n! port buf 0 size)
  132. (size (bytevector-uint-ref buf 0 (endianness big) size))
  133. (_ (eof-object))))))
  134. (define get-u16 (integer-read 2))
  135. (define get-u32 (integer-read 4))
  136. (define get-u64 (integer-read 8))
  137. (define-syntax get-integers
  138. (syntax-rules ()
  139. "Read from PORT integers of the given TYPE, in big endian encoding. Each
  140. TYPE must be one of u8, u16, u32, u64, or _, as in this example:
  141. (get-integers port u8 _ _ _ u32 u16)
  142. In the case of _ (wildcard), one byte is read and discarded. Return as many
  143. values as there are TYPEs."
  144. ((_ port type ...)
  145. (letrec-syntax ((get-integer (syntax-rules (u8 u16 u32 u64)
  146. ((x u8) (get-u8 port))
  147. ((x u16) (get-u16 port))
  148. ((x u32) (get-u32 port))
  149. ((x u64) (get-u64 port))))
  150. (values* (syntax-rules (_)
  151. ((x (result (... ...)))
  152. (values result (... ...)))
  153. ((x (result (... ...)) _ rest (... ...))
  154. (let ((x (get-u8 port)))
  155. (values* (result (... ...))
  156. rest (... ...))))
  157. ((x (result (... ...)) t rest (... ...))
  158. (let ((x (get-integer t)))
  159. (values* (result (... ...) x)
  160. rest (... ...)))))))
  161. (values* () type ...)))))
  162. (define (bytevector->uint bv)
  163. (bytevector-uint-ref bv 0 (endianness big)
  164. (bytevector-length bv)))
  165. (define-syntax-rule (integer-write size)
  166. (lambda (port integer)
  167. "Write INTEGER to PORT as a SIZE-byte integer and as big endian."
  168. (let ((bv (make-bytevector size)))
  169. (bytevector-uint-set! bv 0 integer (endianness big) size)
  170. (put-bytevector port bv))))
  171. (define put-u16 (integer-write 2))
  172. (define put-u32 (integer-write 4))
  173. (define put-u64 (integer-write 8))
  174. (define-syntax put-integers
  175. (syntax-rules ()
  176. "Write the given integers as big endian to PORT. For example:
  177. (put-integers port u8 42 u32 #x7777)
  178. writes to PORT the value 42 as an 8-bit integer and the value #x7777 as a
  179. 32-bit integer."
  180. ((_ port)
  181. #t)
  182. ((_ port type value rest ...)
  183. (let-syntax ((put (syntax-rules (u8 u16 u32 u64)
  184. ((_ u8 port integer)
  185. (put-u8 port integer))
  186. ((_ u16 port integer)
  187. (put-u16 port integer))
  188. ((_ u32 port integer)
  189. (put-u32 port integer))
  190. ((_ u64 port integer)
  191. (put-u64 port integer)))))
  192. (begin
  193. (put type port value)
  194. (put-integers port rest ...))))))
  195. (define-syntax-rule (integers->bytevector type value rest ...)
  196. "Return the the TYPE/VALUE integers representation as a bytevector."
  197. (let-values (((port get) (open-bytevector-output-port)))
  198. (put-integers port type value rest ...)
  199. (force-output port)
  200. (get)))
  201. (define (bytevector->bitnames bv names)
  202. (define (bit-set? bv i)
  203. (let ((idx (fxarithmetic-shift-right i 3))
  204. (bit (fxand i #b111)))
  205. (and (< idx (bytevector-length bv))
  206. (fxbit-set? (bytevector-u8-ref bv idx) bit))))
  207. (do ((names names (cdr names))
  208. (i 0 (fx+ i 1))
  209. (bits '()
  210. (if (bit-set? bv i)
  211. (cons (car names) bits)
  212. bits)))
  213. ((null? names) (reverse bits))))
  214. (define (openpgp-format-fingerprint bv)
  215. "Return a string representing BV, a bytevector, in the conventional OpenPGP
  216. hexadecimal format for fingerprints."
  217. (define (h i)
  218. (string-pad (string-upcase
  219. (number->string
  220. (bytevector-u16-ref bv (* i 2) (endianness big))
  221. 16))
  222. 4 #\0))
  223. (string-append (h 0) " " (h 1) " " (h 2) " " (h 3) " " (h 4)
  224. " "
  225. (h 5) " " (h 6) " " (h 7) " " (h 8) " " (h 9)))
  226. ;;; Constants
  227. (define PACKET-SESSION-KEY 1)
  228. (define PACKET-SIGNATURE 2)
  229. (define PACKET-SYMMETRIC-SESSION-KEY 3)
  230. (define PACKET-ONE-PASS-SIGNATURE 4)
  231. (define PACKET-SECRET-KEY 5)
  232. (define PACKET-PUBLIC-KEY 6)
  233. (define PACKET-SECRET-SUBKEY 7)
  234. (define PACKET-COMPRESSED-DATA 8)
  235. (define PACKET-SYMMETRIC-ENCRYPTED-DATA 9)
  236. (define PACKET-MARKER 10)
  237. (define PACKET-LITERAL-DATA 11)
  238. (define PACKET-TRUST 12)
  239. (define PACKET-USER-ID 13)
  240. (define PACKET-PUBLIC-SUBKEY 14)
  241. (define PACKET-USER-ATTRIBUTE 17)
  242. (define PACKET-SYMMETRIC-ENCRYPTED/PROTECTED-DATA 18)
  243. (define PACKET-MDC 19)
  244. (define PUBLIC-KEY-RSA 1)
  245. (define PUBLIC-KEY-RSA-ENCRYPT-ONLY 2)
  246. (define PUBLIC-KEY-RSA-SIGN-ONLY 3)
  247. (define PUBLIC-KEY-ELGAMAL-ENCRYPT-ONLY 16)
  248. (define PUBLIC-KEY-DSA 17)
  249. (define PUBLIC-KEY-ECDH 18) ;RFC-6637
  250. (define PUBLIC-KEY-ECDSA 19) ;RFC-6639
  251. (define PUBLIC-KEY-ELGAMAL 20) ;encrypt + sign (legacy)
  252. (define PUBLIC-KEY-EDDSA 22) ;"not yet assigned" says GPG
  253. (define (public-key-algorithm id)
  254. (cond ((= id PUBLIC-KEY-RSA) 'rsa)
  255. ((= id PUBLIC-KEY-DSA) 'dsa)
  256. ((= id PUBLIC-KEY-ELGAMAL-ENCRYPT-ONLY) 'elgamal)
  257. ((= id PUBLIC-KEY-EDDSA) 'eddsa)
  258. (else id)))
  259. (define SYMMETRIC-KEY-PLAINTEXT 0)
  260. (define SYMMETRIC-KEY-IDEA 1)
  261. (define SYMMETRIC-KEY-TRIPLE-DES 2)
  262. (define SYMMETRIC-KEY-CAST5-128 3)
  263. (define SYMMETRIC-KEY-BLOWFISH-128 4)
  264. (define SYMMETRIC-KEY-AES-128 7)
  265. (define SYMMETRIC-KEY-AES-192 8)
  266. (define SYMMETRIC-KEY-AES-256 9)
  267. (define SYMMETRIC-KEY-TWOFISH-256 10)
  268. (define SYMMETRIC-KEY-CAMELLIA-128 11) ;RFC-5581
  269. (define SYMMETRIC-KEY-CAMELLIA-192 12)
  270. (define SYMMETRIC-KEY-CAMELLIA-256 13)
  271. (define (symmetric-key-algorithm id)
  272. (cond ((= id SYMMETRIC-KEY-PLAINTEXT) 'plaintext)
  273. ((= id SYMMETRIC-KEY-IDEA) 'idea)
  274. ((= id SYMMETRIC-KEY-TRIPLE-DES) 'tdea)
  275. ((= id SYMMETRIC-KEY-CAST5-128) 'cast5-128)
  276. ((= id SYMMETRIC-KEY-BLOWFISH-128) 'blowfish-128)
  277. ((= id SYMMETRIC-KEY-AES-128) 'aes-128)
  278. ((= id SYMMETRIC-KEY-AES-192) 'aes-192)
  279. ((= id SYMMETRIC-KEY-AES-256) 'aes-256)
  280. ((= id SYMMETRIC-KEY-TWOFISH-256) 'twofish-256)
  281. (else id)))
  282. (define HASH-MD5 1)
  283. (define HASH-SHA-1 2)
  284. (define HASH-RIPE-MD160 3)
  285. (define HASH-SHA-256 8)
  286. (define HASH-SHA-384 9)
  287. (define HASH-SHA-512 10)
  288. (define HASH-SHA-224 11)
  289. (define (openpgp-hash-algorithm id signature-port)
  290. (cond ((= id HASH-MD5) 'md5)
  291. ((= id HASH-SHA-1) 'sha1)
  292. ((= id HASH-RIPE-MD160) 'rmd160)
  293. ((= id HASH-SHA-256) 'sha256)
  294. ((= id HASH-SHA-384) 'sha384)
  295. ((= id HASH-SHA-512) 'sha512)
  296. ((= id HASH-SHA-224) 'sha224)
  297. (else
  298. (raise (condition
  299. (&openpgp-invalid-signature-error (port signature-port)))))))
  300. (define COMPRESSION-UNCOMPRESSED 0)
  301. (define COMPRESSION-ZIP 1) ;deflate
  302. (define COMPRESSION-ZLIB 2)
  303. (define COMPRESSION-BZIP2 3)
  304. (define (compression-algorithm id)
  305. (cond ((= id COMPRESSION-UNCOMPRESSED) 'uncompressed)
  306. ((= id COMPRESSION-ZIP) 'deflate)
  307. ((= id COMPRESSION-ZLIB) 'zlib)
  308. ((= id COMPRESSION-BZIP2) 'bzip2)
  309. (else id)))
  310. (define SUBPACKET-SIGNATURE-CTIME 2)
  311. (define SUBPACKET-SIGNATURE-ETIME 3)
  312. ;; 4 = Exportable Certification
  313. (define SUBPACKET-TRUST-SIGNATURE 5)
  314. ;; 6 = Regular Expression
  315. (define SUBPACKET-REVOCABLE 7)
  316. (define SUBPACKET-KEY-ETIME 9)
  317. (define SUBPACKET-PREFERRED-SYMMETRIC-ALGORITHMS 11)
  318. ;; 12 = Revocation Key
  319. (define SUBPACKET-ISSUER 16)
  320. (define SUBPACKET-NOTATION-DATA 20)
  321. (define SUBPACKET-PREFERRED-HASH-ALGORITHMS 21)
  322. (define SUBPACKET-PREFERRED-COMPRESSION-ALGORITHMS 22)
  323. (define SUBPACKET-KEY-SERVER-PREFERENCES 23)
  324. (define SUBPACKET-PREFERRED-KEY-SERVER 24)
  325. (define SUBPACKET-PRIMARY-USER-ID 25)
  326. (define SUBPACKET-POLICY-URI 26)
  327. (define SUBPACKET-KEY-FLAGS 27)
  328. (define SUBPACKET-SIGNER-USER-ID 28)
  329. (define SUBPACKET-REASON-FOR-REVOCATION 29)
  330. (define SUBPACKET-FEATURES 30)
  331. ;; 31 = Signature Target
  332. (define SUBPACKET-EMBEDDED-SIGNATURE 32)
  333. (define SUBPACKET-ISSUER-FINGERPRINT 33) ;defined in RFC4880bis
  334. (define SIGNATURE-BINARY #x00)
  335. (define SIGNATURE-TEXT #x01)
  336. (define SIGNATURE-STANDALONE #x02)
  337. (define SIGNATURE-GENERIC-CERT #x10)
  338. (define SIGNATURE-PERSONA-CERT #x11)
  339. (define SIGNATURE-CASUAL-CERT #x12)
  340. (define SIGNATURE-POSITIVE-CERT #x13)
  341. (define SIGNATURE-SUBKEY-BINDING #x18)
  342. (define SIGNATURE-PRIMARY-KEY-BINDING #x19)
  343. (define SIGNATURE-DIRECT #x1f)
  344. (define SIGNATURE-KEY-REVOCATION #x20)
  345. (define SIGNATURE-SUBKEY-REVOCATION #x28)
  346. (define SIGNATURE-CERT-REVOCATION #x30)
  347. (define SIGNATURE-TIMESTAMP #x40)
  348. (define SIGNATURE-THIRD-PARTY #x50)
  349. ;;; Parsing
  350. ;; Look at the tag byte and see if it looks reasonable, if it does
  351. ;; then the file is likely not armored. Does not move the port
  352. ;; position.
  353. (define (port-ascii-armored? p)
  354. (let ((tag (lookahead-u8 p)))
  355. (cond ((eof-object? tag) #f)
  356. ((not (fxbit-set? tag 7)) #t)
  357. (else
  358. (let ((type (if (fxbit-set? tag 6)
  359. (fxbit-field tag 0 6)
  360. (fxbit-field tag 2 6))))
  361. (not (<= PACKET-SESSION-KEY type PACKET-MDC)))))))
  362. (define (get-mpi/bytevector p)
  363. (let* ((bitlen (get-u16 p))
  364. (bytelen (fxdiv (fx+ bitlen 7) 8)))
  365. (get-bytevector-n p bytelen)))
  366. (define (get-mpi p)
  367. (bytevector->uint (get-mpi/bytevector p)))
  368. (define (get-v4-length p)
  369. ;; TODO: indeterminate length (only for data packets)
  370. (let ((o1 (get-u8 p)))
  371. (cond ((< o1 192) o1)
  372. ((< o1 255)
  373. (+ (fxarithmetic-shift-left (fx- o1 192) 8)
  374. (get-u8 p)
  375. 192))
  376. ((= o1 255)
  377. (get-u32 p)))))
  378. (define (get-packet p)
  379. (if (port-eof? p)
  380. (eof-object)
  381. (get-packet* p get-data)))
  382. (define (get-packet* p get-data)
  383. (let ((tag (get-u8 p)))
  384. ;; (unless (fxbit-set? tag 7) (error 'get-packet "Invalid tag" tag))
  385. (cond ((fxbit-set? tag 6) ;New packet format
  386. (let ((tag (fxbit-field tag 0 6))
  387. (len (get-v4-length p)))
  388. (get-data p tag len)))
  389. (else ;Old packet format
  390. (let ((tag (fxbit-field tag 2 6))
  391. (len (case (fxbit-field tag 0 2)
  392. ((0) (get-u8 p))
  393. ((1) (get-u16 p))
  394. ((2) (get-u32 p))
  395. ((3) #f))))
  396. (get-data p tag len))))))
  397. (define (get-data p tag len)
  398. (let ((pp (if len
  399. (open-bytevector-input-port (get-bytevector-n p len))
  400. p))) ;indeterminate length
  401. (cond
  402. ((= tag PACKET-SIGNATURE)
  403. (get-signature pp))
  404. ((= tag PACKET-PUBLIC-KEY)
  405. (get-public-key pp #f))
  406. ((= tag PACKET-TRUST)
  407. 'openpgp-trust) ;XXX: non-standard format?
  408. ((= tag PACKET-USER-ID)
  409. (get-user-id pp len))
  410. ((= tag PACKET-PUBLIC-SUBKEY)
  411. (get-public-key pp #t))
  412. ((= tag PACKET-USER-ATTRIBUTE)
  413. (get-user-attribute pp len))
  414. ((= tag PACKET-ONE-PASS-SIGNATURE)
  415. 'one-pass-signature) ;TODO: implement
  416. (else
  417. (raise (condition (&openpgp-unrecognized-packet-error (port p))))))))
  418. (define-record-type <openpgp-public-key>
  419. (make-openpgp-public-key version subkey? time value fingerprint)
  420. openpgp-public-key?
  421. (version openpgp-public-key-version)
  422. (subkey? openpgp-public-key-subkey?)
  423. (time openpgp-public-key-time)
  424. (value openpgp-public-key-value)
  425. (fingerprint openpgp-public-key-fingerprint))
  426. ;;; Signatures
  427. (define-record-type <openpgp-signature>
  428. (make-openpgp-signature version type pk-algorithm hash-algorithm hashl16
  429. append-data hashed-subpackets unhashed-subpackets
  430. value issuer issuer-fingerprint)
  431. openpgp-signature?
  432. (version openpgp-signature-version)
  433. (type openpgp-signature-type)
  434. (pk-algorithm openpgp-signature-public-key-algorithm)
  435. (hash-algorithm openpgp-signature-hash-algorithm)
  436. (hashl16 openpgp-signature-hashl16) ;left 16 bits of signed hash
  437. (append-data openpgp-signature-append-data) ;append to data when hashing
  438. (hashed-subpackets openpgp-signature-hashed-subpackets)
  439. (unhashed-subpackets openpgp-signature-unhashed-subpackets)
  440. (value openpgp-signature-value)
  441. (issuer openpgp-signature-issuer-key-id) ;integer | #f
  442. (issuer-fingerprint openpgp-signature-issuer-fingerprint)) ;bytevector | #f
  443. (define (openpgp-signature-creation-time sig)
  444. (cond ((assq 'signature-ctime (openpgp-signature-hashed-subpackets sig))
  445. => (lambda (x) (unixtime (cdr x))))
  446. ;; XXX: should be an error?
  447. (else #f)))
  448. (define (openpgp-signature-expiration-time sig)
  449. (cond ((assq 'signature-etime (openpgp-signature-hashed-subpackets sig))
  450. => (lambda (x)
  451. (unixtime (+ (cdr x)
  452. (openpgp-signature-creation-time sig)))))
  453. (else #f)))
  454. (define (get-openpgp-detached-signature/ascii port)
  455. "Read from PORT an ASCII-armored detached signature. Return an
  456. <openpgp-signature> record or the end-of-file object. Raise an error if the
  457. data read from PORT does is invalid or does not correspond to a detached
  458. signature."
  459. (let-values (((data type) (read-radix-64 port)))
  460. (cond ((eof-object? data) data)
  461. ((string=? type "PGP SIGNATURE")
  462. (get-packet (open-bytevector-input-port data)))
  463. (else
  464. (print "expected PGP SIGNATURE" type)
  465. (raise (condition
  466. (&openpgp-invalid-signature-error (port port))))))))
  467. (define (hash-algorithm-name algorithm) ;XXX: should be in Guile-Gcrypt
  468. "Return the name of ALGORITHM, a 'hash-algorithm' integer, as a symbol."
  469. (letrec-syntax ((->name (syntax-rules ()
  470. ((_) #f)
  471. ((_ name rest ...)
  472. (if (= algorithm (hash-algorithm name))
  473. 'name
  474. (->name rest ...))))))
  475. (->name sha1 sha256 sha384 sha512 sha224
  476. sha3-224 sha3-256 sha3-384 sha3-512)))
  477. (define (verify-openpgp-signature sig keyring dataport)
  478. "Verify that the data read from DATAPORT matches SIG, an
  479. <openpgp-signature>. Fetch the public key of the issuer of SIG from KEYRING,
  480. a keyring as returned by 'get-openpgp-keyring'. Return two values: a status
  481. symbol, such as 'bad-signature or 'missing-key, and additional info, such as
  482. the issuer's OpenPGP public key extracted from KEYRING."
  483. (define (check key sig)
  484. (let*-values (((hash-algorithm) (lookup-hash-algorithm
  485. (openpgp-signature-hash-algorithm sig)))
  486. ((port get-hash) (open-hash-port hash-algorithm)))
  487. (dump-port dataport port)
  488. ;; As per RFC4880 Section 5.2.4 ("Computing Signatures"), hash some of
  489. ;; the fields from the signature packet.
  490. (for-each (cut put-bytevector port <>)
  491. (openpgp-signature-append-data sig))
  492. (close-port port)
  493. (let* ((signature (openpgp-signature-value sig))
  494. (public-key (openpgp-public-key-value key))
  495. (hash (get-hash))
  496. (key-type (key-type public-key))
  497. (data
  498. ;; See "(gcrypt) Cryptographic Functions".
  499. (sexp->canonical-sexp
  500. (if (eq? key-type 'ecc)
  501. `(data
  502. (flags eddsa)
  503. (hash-algo sha512)
  504. (value ,hash))
  505. `(data
  506. (flags ,(match key-type
  507. ('rsa 'pkcs1)
  508. ('dsa 'rfc6979)))
  509. (hash ,(hash-algorithm-name hash-algorithm)
  510. ,hash))))))
  511. (values (if (verify signature data public-key)
  512. 'good-signature
  513. 'bad-signature)
  514. key))))
  515. ;; TODO: Support SIGNATURE-TEXT.
  516. (if (= (openpgp-signature-type sig) SIGNATURE-BINARY)
  517. (let* ((id (openpgp-signature-issuer-key-id sig))
  518. (fingerprint (openpgp-signature-issuer-fingerprint sig))
  519. (key (if fingerprint
  520. (lookup-key-by-fingerprint keyring fingerprint)
  521. (lookup-key-by-id keyring id))))
  522. (if key
  523. (check key sig)
  524. (values 'missing-key (or fingerprint id))))
  525. (values 'unsupported-signature sig)))
  526. (define (key-id-matches-fingerprint? key-id fingerprint)
  527. "Return true if KEY-ID, a number, corresponds to the low 8 bytes of
  528. FINGERPRINT, a bytevector."
  529. (let* ((len (bytevector-length fingerprint))
  530. (low (make-bytevector 8)))
  531. (bytevector-copy! fingerprint (- len 8) low 0 8)
  532. (= (bytevector->uint low) key-id)))
  533. (define (get-signature p)
  534. (define (->hex n)
  535. (string-hex-pad (number->string n 16)))
  536. (define (get-sig p pkalg)
  537. (cond ((= pkalg PUBLIC-KEY-RSA)
  538. (print "RSA signature")
  539. (string->canonical-sexp
  540. (format #f "(sig-val (rsa (s #~a#)))"
  541. (->hex (get-mpi p)))))
  542. ((= pkalg PUBLIC-KEY-DSA)
  543. (print "DSA signature")
  544. (let ((r (get-mpi p)) (s (get-mpi p)))
  545. (string->canonical-sexp
  546. (format #f "(sig-val (dsa (r #~a#) (s #~a#)))"
  547. (->hex r) (->hex s)))))
  548. ((= pkalg PUBLIC-KEY-EDDSA)
  549. (print "EdDSA signature")
  550. (let ((r (get-mpi/bytevector p))
  551. (s (get-mpi/bytevector p)))
  552. ;; XXX: 'verify' fails down the road with GPG_ERR_INV_LENGTH if
  553. ;; we provide a 31-byte R or S below, hence the second argument
  554. ;; to '->hex' ensuring the MPIs are represented as two-byte
  555. ;; multiples, with leading zeros.
  556. (define (bytevector->hex bv)
  557. (let ((str (bytevector->base16-string bv)))
  558. (if (odd? (bytevector-length bv))
  559. (string-append "00" str)
  560. str)))
  561. (string->canonical-sexp
  562. (format #f "(sig-val (eddsa (r #~a#) (s #~a#)))"
  563. (bytevector->hex r) (bytevector->hex s)))))
  564. (else
  565. (list 'unsupported-algorithm
  566. (public-key-algorithm pkalg)
  567. (get-bytevector-all p)))))
  568. (let ((version (get-u8 p)))
  569. (case version
  570. ((3)
  571. (let-values (((hmlen type ctime keyid pkalg halg hashl16)
  572. (get-integers p u8 u8 u32 u64 u8 u8 u16)))
  573. (unless (= hmlen 5)
  574. (raise (condition
  575. (&openpgp-invalid-signature-error (port p)))))
  576. (print "Signature type: " type " creation time: " (unixtime ctime))
  577. (print "Hash algorithm: " (openpgp-hash-algorithm halg p))
  578. (let ((value (get-sig p pkalg)))
  579. (unless (port-eof? p)
  580. (print "Trailing data in signature: " (get-bytevector-all p)))
  581. (make-openpgp-signature version type
  582. (public-key-algorithm pkalg)
  583. (openpgp-hash-algorithm halg p) hashl16
  584. (list (integers->bytevector u8 type
  585. u32 ctime))
  586. ;; Emulate hashed subpackets
  587. (list (cons 'signature-ctime ctime))
  588. ;; Unhashed subpackets
  589. (list (cons 'issuer keyid))
  590. value
  591. keyid #f))))
  592. ((4)
  593. (let*-values (((type pkalg halg) (get-integers p u8 u8 u8))
  594. ((hashed-subpackets)
  595. (get-bytevector-n p (get-u16 p)))
  596. ((unhashed-subpackets)
  597. (get-bytevector-n p (get-u16 p)))
  598. ((hashl16) (get-u16 p)))
  599. (print "Signature type: " type)
  600. (print "Hash algorithm: " (openpgp-hash-algorithm halg p))
  601. (let ((value (get-sig p pkalg)))
  602. (unless (port-eof? p)
  603. (print "Trailing data in signature: " (get-bytevector-all p)))
  604. (let* ((subpacket-len (bytevector-length hashed-subpackets))
  605. (append-data
  606. (list
  607. (integers->bytevector u8 version
  608. u8 type
  609. u8 pkalg
  610. u8 halg
  611. u16 subpacket-len)
  612. hashed-subpackets
  613. ;; http://www.rfc-editor.org/errata_search.php?rfc=4880
  614. ;; Errata ID: 2214.
  615. (integers->bytevector u8 #x04
  616. u8 #xff
  617. u32 (+ 6 subpacket-len))))
  618. (unhashed-subpackets
  619. (parse-subpackets unhashed-subpackets p))
  620. (hashed-subpackets (parse-subpackets hashed-subpackets p))
  621. (subpackets (append hashed-subpackets
  622. unhashed-subpackets))
  623. (issuer-key-id (assoc-ref subpackets 'issuer))
  624. (issuer (assoc-ref subpackets
  625. 'issuer-fingerprint)))
  626. (unless (or (not issuer) (not issuer-key-id)
  627. (key-id-matches-fingerprint? issuer-key-id issuer))
  628. (print "issuer key id does not match fingerprint"
  629. issuer-key-id issuer)
  630. (raise (condition
  631. (&openpgp-invalid-signature-error (port p)))))
  632. (make-openpgp-signature version type
  633. (public-key-algorithm pkalg)
  634. (openpgp-hash-algorithm halg p)
  635. hashl16
  636. append-data
  637. hashed-subpackets
  638. unhashed-subpackets
  639. value
  640. issuer-key-id issuer)))))
  641. (else
  642. (print "Unsupported signature version: " version)
  643. 'unsupported-signature-version))))
  644. (define (parse-subpackets bv signature-port)
  645. (define (parse tag data)
  646. (let ((type (fxbit-field tag 0 7))
  647. (critical? (fxbit-set? tag 7)))
  648. (cond
  649. ((= type SUBPACKET-SIGNATURE-CTIME)
  650. (cons 'signature-ctime
  651. (bytevector-u32-ref data 0 (endianness big))))
  652. ((= type SUBPACKET-SIGNATURE-ETIME)
  653. (cons 'signature-etime
  654. (bytevector-u32-ref data 0 (endianness big))))
  655. ((= type SUBPACKET-TRUST-SIGNATURE)
  656. (cons 'trust-signature
  657. (bytevector-u8-ref data 0)))
  658. ((= type SUBPACKET-REVOCABLE)
  659. (cons 'revocable
  660. (= (bytevector-u8-ref data 0) 1)))
  661. ((= type SUBPACKET-KEY-ETIME)
  662. (cons 'key-etime
  663. (bytevector-u32-ref data 0 (endianness big))))
  664. ((= type SUBPACKET-PREFERRED-SYMMETRIC-ALGORITHMS)
  665. (cons 'preferred-symmetric-algorithms
  666. (map symmetric-key-algorithm (bytevector->u8-list data))))
  667. ((= type SUBPACKET-ISSUER)
  668. (cons 'issuer
  669. (bytevector-u64-ref data 0 (endianness big))))
  670. ((= type SUBPACKET-ISSUER-FINGERPRINT) ;v4+ only, RFC4880bis
  671. (cons 'issuer-fingerprint
  672. (let* ((version (bytevector-u8-ref data 0))
  673. (len (match version (4 20) (5 32)) )
  674. (fingerprint (make-bytevector len)))
  675. (bytevector-copy! data 1 fingerprint 0 len)
  676. fingerprint)))
  677. ((= type SUBPACKET-NOTATION-DATA)
  678. (let ((p (open-bytevector-input-port data)))
  679. (let-values (((f1 nlen vlen)
  680. (get-integers p u8 _ _ _ u16 u16)))
  681. (let* ((name (get-bytevector-n p nlen))
  682. (value (get-bytevector-n p vlen)))
  683. (cons 'notation-data
  684. (list (utf8->string name)
  685. (if (fxbit-set? f1 7)
  686. (utf8->string value)
  687. value)))))))
  688. ((= type SUBPACKET-PREFERRED-HASH-ALGORITHMS)
  689. (cons 'preferred-hash-algorithms
  690. (map (cut openpgp-hash-algorithm <> signature-port)
  691. (bytevector->u8-list data))))
  692. ((= type SUBPACKET-PREFERRED-COMPRESSION-ALGORITHMS)
  693. (cons 'preferred-compression-algorithms
  694. (map compression-algorithm (bytevector->u8-list data))))
  695. ((= type SUBPACKET-KEY-SERVER-PREFERENCES)
  696. (cons 'key-server-preferences
  697. (if (and (>= (bytevector-length data) 1)
  698. (fxbit-set? (bytevector-u8-ref data 0) 7))
  699. (list 'no-modify)
  700. (list))))
  701. ((= type SUBPACKET-PREFERRED-KEY-SERVER)
  702. (cons 'preferred-key-server (utf8->string data)))
  703. ((= type SUBPACKET-PRIMARY-USER-ID)
  704. (cons 'primary-user-id (not (zero? (bytevector-u8-ref data 0)))))
  705. ((= type SUBPACKET-POLICY-URI)
  706. (cons 'policy-uri (utf8->string data)))
  707. ((= type SUBPACKET-KEY-FLAGS)
  708. (cons 'key-flags (bytevector->bitnames
  709. data
  710. '(certification sign-data
  711. communications-encryption
  712. storage-encryption
  713. split-key authentication
  714. group-key))))
  715. ((= type SUBPACKET-SIGNER-USER-ID)
  716. (cons 'signer-user-id (utf8->string data)))
  717. ((= type SUBPACKET-REASON-FOR-REVOCATION)
  718. (let* ((p (open-bytevector-input-port data))
  719. (revocation-code (get-u8 p)))
  720. (cons 'reason-for-revocation
  721. (list revocation-code
  722. (if (port-eof? p)
  723. ""
  724. (utf8->string (get-bytevector-all p)))))))
  725. ((= type SUBPACKET-FEATURES)
  726. (cons 'features (bytevector->bitnames
  727. data '(modification-detection))))
  728. ((= type SUBPACKET-EMBEDDED-SIGNATURE)
  729. (cons 'embedded-signature
  730. (get-signature (open-bytevector-input-port data))))
  731. (else
  732. ;; Unknown subpacket type. If it is critical, then the signature
  733. ;; should be considered invalid.
  734. (print "Unknown subpacket type: " type)
  735. (if critical?
  736. (raise (condition
  737. (&openpgp-unrecognized-packet-error
  738. (port signature-port))))
  739. (list 'unsupported-subpacket type data))))))
  740. (let ((p (open-bytevector-input-port bv)))
  741. (let lp ((subpackets '()))
  742. ;; In case of multiple subpackets of the same type, the last
  743. ;; one should be used. Therefore the list is not reversed
  744. ;; here.
  745. (if (port-eof? p)
  746. (reverse subpackets)
  747. (let* ((len (- (get-v4-length p) 1))
  748. (tag (get-u8 p))
  749. (sp (parse tag (get-bytevector-n p len))))
  750. (print "#;Subpacket " sp)
  751. (lp (cons sp subpackets)))))))
  752. ;;; Public keys
  753. (define (openpgp-public-key-id k)
  754. (let ((bv (openpgp-public-key-fingerprint k)))
  755. (bytevector-u64-ref bv
  756. (- (bytevector-length bv) 8)
  757. (endianness big))))
  758. (define (get-public-key p subkey?)
  759. (define (fingerprint p)
  760. (let ((len (port-position p)))
  761. (set-port-position! p 0)
  762. (let-values (((sha1-port get)
  763. (open-hash-port (hash-algorithm sha1))))
  764. (put-u8 sha1-port #x99)
  765. (put-u16 sha1-port len)
  766. (dump-port p sha1-port)
  767. (close-port sha1-port)
  768. (get))))
  769. (define (get-key p alg)
  770. (define (->hex n)
  771. (string-hex-pad (number->string n 16)))
  772. (cond ((= alg PUBLIC-KEY-RSA)
  773. (print "Public RSA key")
  774. (let* ((n (get-mpi p)) (e (get-mpi p)))
  775. (string->canonical-sexp
  776. (format #f "(public-key (rsa (n #~a#) (e #~a#)))"
  777. (->hex n) (->hex e)))))
  778. ((= alg PUBLIC-KEY-DSA)
  779. (print "Public DSA key")
  780. (let* ((p* (get-mpi p)) (q (get-mpi p))
  781. (g (get-mpi p)) (y (get-mpi p)))
  782. (string->canonical-sexp
  783. (format #f "(public-key (dsa (p #~a#)(q #~a#)(g #~a#)(y #~a#)))"
  784. (->hex p*) (->hex q) (->hex g) (->hex y)))))
  785. #;
  786. ((= alg PUBLIC-KEY-ELGAMAL-ENCRYPT-ONLY) ; ; ; ;
  787. (print "Public El-Gamal Key") ; ; ; ;
  788. (let* ((p* (get-mpi p)) (g (get-mpi p)) (y (get-mpi p))) ; ; ; ;
  789. (make-public-elgamal-key p* g y)))
  790. ((= alg PUBLIC-KEY-EDDSA)
  791. ;; See
  792. ;; <https://tools.ietf.org/html/draft-koch-eddsa-for-openpgp-04>
  793. ;; and openpgp-oid.c in GnuPG.
  794. (print "Public EdDSA key")
  795. (let* ((len (get-u8 p))
  796. (oid (bytevector->uint (get-bytevector-n p len)))
  797. (q (get-mpi p)))
  798. (define curve
  799. (match oid
  800. (#x2b06010401da470f01 'Ed25519)
  801. (#x2b060104019755010501 'Curve25519)))
  802. (string->canonical-sexp
  803. (format #f "(public-key (ecc (curve ~a)(flags ~a)(q #~a#)))"
  804. curve
  805. (if (eq? curve 'Curve25519) 'djb-tweak 'eddsa)
  806. (->hex q)))))
  807. (else
  808. (list 'unsupported-algorithm ;FIXME: throw
  809. (public-key-algorithm alg)
  810. (get-bytevector-all p)))))
  811. (let ((version (get-u8 p)))
  812. (case version
  813. ((4)
  814. (let-values (((ctime alg) (get-integers p u32 u8)))
  815. (print "Key creation time: " (unixtime ctime))
  816. (let ((key (get-key p alg)))
  817. (unless (port-eof? p)
  818. ;; Probably an error? Gonna cause trouble anyway.
  819. (print "Trailing data in public key: " (get-bytevector-all p)))
  820. (let ((digest (fingerprint p)))
  821. (make-openpgp-public-key version subkey? ctime key
  822. digest)))))
  823. (else
  824. (print "Unsupported public key version: " version)
  825. 'unsupported-public-key-version))))
  826. (define (openpgp-public-key-primary? key)
  827. (and (openpgp-public-key? key)
  828. (not (openpgp-public-key-subkey? key))))
  829. ;;; User IDs and User attributes
  830. (define-record-type <openpgp-user-id>
  831. (make-openpgp-user-id value unparsed)
  832. openpgp-user-id?
  833. (value openpgp-user-id-value)
  834. (unparsed openpgp-user-id-unparsed))
  835. (define (get-user-id p len)
  836. (let ((unparsed (get-bytevector-n p len)))
  837. (make-openpgp-user-id (utf8->string unparsed) unparsed)))
  838. (define-record-type <openpgp-user-attribute>
  839. (make-openpgp-user-attribute unparsed)
  840. openpgp-user-attribute?
  841. (unparsed openpgp-user-attribute-unparsed))
  842. (define (get-user-attribute p len)
  843. (let ((bv (get-bytevector-n p len)))
  844. ;; TODO: bv contains subpackets. Type 1 is JFIF.
  845. (make-openpgp-user-attribute bv)))
  846. ;;; Keyring management
  847. (define-record-type <openpgp-keyring>
  848. (openpgp-keyring ids fingerprints)
  849. openpgp-keyring?
  850. (ids openpgp-keyring-ids) ;vhash mapping key id to packets
  851. (fingerprints openpgp-keyring-fingerprints)) ;mapping fingerprint to packets
  852. (define* (keyring-insert key keyring #:optional (packets '()))
  853. "Insert the KEY/PACKETS association into KEYRING and return the resulting
  854. keyring. PACKETS typically contains KEY, an <openpgp-public-key>, alongside
  855. with additional <openpgp-public-key> records for sub-keys, <openpgp-user-id>
  856. records, and so on."
  857. (openpgp-keyring (vhash-consv (openpgp-public-key-id key)
  858. (cons key packets)
  859. (openpgp-keyring-ids keyring))
  860. (vhash-cons (openpgp-public-key-fingerprint key)
  861. (cons key packets)
  862. (openpgp-keyring-fingerprints keyring))))
  863. (define (lookup-key-by-id keyring id)
  864. "Return two values: the first key with ID in KEYRING, and a list of
  865. associated packets (user IDs, signatures, etc.). Return #f and the empty list
  866. of ID was not found. ID must be the 64-bit key ID of the key, an integer."
  867. (match (vhash-assv id (openpgp-keyring-ids keyring))
  868. ((_ key packets ...) (values key packets))
  869. (#f (values #f '()))))
  870. (define (lookup-key-by-fingerprint keyring fingerprint)
  871. "Return two values: the key with FINGERPRINT in KEYRING, and a list of
  872. associated packets (user IDs, signatures, etc.). Return #f and the empty list
  873. of FINGERPRINT was not found. FINGERPRINT must be a bytevector."
  874. (match (vhash-assoc fingerprint (openpgp-keyring-fingerprints keyring))
  875. ((_ key packets ...) (values key packets))
  876. (#f (values #f '()))))
  877. ;; Reads a keyring from the binary input port p. It must not be
  878. ;; ASCII armored.
  879. (define %empty-keyring
  880. ;; The empty keyring.
  881. (openpgp-keyring vlist-null vlist-null))
  882. (define* (get-openpgp-keyring port
  883. #:optional (keyring %empty-keyring)
  884. #:key (limit -1))
  885. "Read from PORT an OpenPGP keyring in binary format; return a keyring based
  886. on all the OpenPGP primary keys that were read. The returned keyring
  887. complements KEYRING. LIMIT is the maximum number of keys to read, or -1 if
  888. there is no limit."
  889. (let lp ((pkt (get-packet port))
  890. (limit limit)
  891. (keyring keyring))
  892. (print "#;key " pkt)
  893. (cond ((or (zero? limit) (eof-object? pkt))
  894. keyring)
  895. ((openpgp-public-key-primary? pkt)
  896. ;; Read signatures, user id's, subkeys
  897. (let lp* ((pkt (get-packet port))
  898. (pkts (list pkt))
  899. (keys (list pkt)))
  900. (print "#;keydata " pkt)
  901. (cond ((or (eof-object? pkt)
  902. (eq? pkt 'unsupported-public-key-version)
  903. (openpgp-public-key-primary? pkt))
  904. ;; KEYRING is indexed by key-id. Key ids for both the
  905. ;; primary key and subkeys all point to the list of
  906. ;; packets.
  907. (lp pkt
  908. (- limit 1)
  909. (fold (cute keyring-insert <> <> (reverse pkts))
  910. keyring keys)))
  911. ((openpgp-public-key? pkt) ;subkey
  912. (lp* (get-packet port) (cons pkt pkts)
  913. (cons pkt keys)))
  914. (else
  915. (lp* (get-packet port) (cons pkt pkts) keys)))))
  916. (else
  917. ;; Skip until there's a primary key. Ignore errors...
  918. (lp (get-packet port) limit keyring)))))
  919. ;;;
  920. ;;; Radix-64 (RFC4880).
  921. ;;;
  922. (define (crc24 bv)
  923. "Compute a CRC24 as described in RFC4880, Section 6.1."
  924. ;; We used to have it implemented in Scheme but the C version here makes
  925. ;; 'load-keyring-from-reference' 18% faster when loading the 72
  926. ;; ASCII-armored files of today's Guix keyring.
  927. (bytevector->uint (bytevector-hash bv (hash-algorithm crc24-rfc2440))))
  928. (define %begin-block-prefix "-----BEGIN ")
  929. (define %begin-block-suffix "-----")
  930. (define %end-block-prefix "-----END ")
  931. (define %end-block-suffix "-----")
  932. (define (read-radix-64 port)
  933. "Read from PORT an ASCII-armored Radix-64 stream, decode it, and return the
  934. result as a bytevector as well as the type, a string such as \"PGP MESSAGE\".
  935. Return #f if PORT does not contain a valid Radix-64 stream, and the
  936. end-of-file object if the Radix-64 sequence was truncated."
  937. ;; This is the same as 'get-delimited-base64', except that it implements the
  938. ;; CRC24 check.
  939. (define (skip-headers port)
  940. ;; Skip the Radix-64 "armor headers".
  941. (match (read-line port)
  942. ((? eof-object? eof) eof)
  943. ((= string-trim-both "") "")
  944. (_ (skip-headers port))))
  945. (let ((line (string-trim-right (read-line port))))
  946. (if (and (string-prefix? %begin-block-prefix line)
  947. (string-suffix? %begin-block-suffix line))
  948. (let* ((kind (string-drop-right
  949. (string-drop line (string-length %begin-block-prefix))
  950. (string-length %begin-block-suffix)))
  951. (end (string-append %end-block-prefix kind
  952. %end-block-suffix)))
  953. (skip-headers port)
  954. (let loop ((lines '()))
  955. (let ((line (read-line port)))
  956. (match line
  957. ((? eof-object? eof)
  958. (values eof kind))
  959. ((= string-trim-both "")
  960. (loop lines))
  961. ((= string-trim-both str)
  962. (if (string=? str end)
  963. (match lines
  964. ((crc lines ...)
  965. ;; The last line should be the CRC, starting with an
  966. ;; "=" sign.
  967. (let ((crc (and (string-prefix? "=" crc)
  968. (base64-decode (string-drop crc 1))))
  969. (data (base64-decode
  970. (string-concatenate-reverse lines))))
  971. (if (and crc (= (bytevector->uint crc) (crc24 data)))
  972. (values data kind)
  973. (values #f kind))))
  974. (_
  975. (values #f kind)))
  976. (loop (cons str lines))))))))
  977. (values #f #f))))
  978. (define (string->openpgp-packet str)
  979. "Read STR, an ASCII-armored OpenPGP packet, and return the corresponding
  980. OpenPGP record."
  981. (get-packet
  982. (open-bytevector-input-port (call-with-input-string str read-radix-64))))