chromium-extension.scm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Marius Bakke <marius@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 (gnu build chromium-extension)
  19. #:use-module (gcrypt base16)
  20. #:use-module ((gcrypt hash) #:prefix hash:)
  21. #:use-module (ice-9 iconv)
  22. #:use-module (guix gexp)
  23. #:use-module (guix packages)
  24. #:use-module (gnu packages base)
  25. #:use-module (gnu packages check)
  26. #:use-module (gnu packages chromium)
  27. #:use-module (gnu packages gnupg)
  28. #:use-module (gnu packages tls)
  29. #:use-module (gnu packages xorg)
  30. #:use-module (guix build-system trivial)
  31. #:export (make-chromium-extension))
  32. ;;; Commentary:
  33. ;;;
  34. ;;; Tools to deal with Chromium extensions.
  35. ;;;
  36. ;;; Code:
  37. (define (make-signing-key seed)
  38. "Return a derivation for a deterministic PKCS #8 private key using SEED."
  39. (define sha256sum
  40. (bytevector->base16-string (hash:sha256 (string->bytevector seed "UTF-8"))))
  41. ;; certtool.c wants a 56 byte seed for a 2048 bit key.
  42. (define size 2048)
  43. (define normalized-seed (string-take sha256sum 56))
  44. (computed-file (string-append seed "-signing-key.pem")
  45. #~(system* #$(file-append gnutls "/bin/certtool")
  46. "--generate-privkey"
  47. "--key-type=rsa"
  48. "--pkcs8"
  49. ;; Use the provable FIPS-PUB186-4 algorithm for
  50. ;; deterministic results.
  51. "--provable"
  52. "--password="
  53. "--no-text"
  54. (string-append "--bits=" #$(number->string size))
  55. (string-append "--seed=" #$normalized-seed)
  56. "--outfile" #$output)
  57. #:local-build? #t))
  58. (define* (make-crx signing-key package #:optional (package-output "out"))
  59. "Create a signed \".crx\" file from the unpacked Chromium extension residing
  60. in PACKAGE-OUTPUT of PACKAGE. The extension will be signed with SIGNING-KEY."
  61. (define name (package-name package))
  62. (define version (package-version package))
  63. (with-imported-modules '((guix build utils))
  64. (computed-file
  65. (string-append name "-" version ".crx")
  66. #~(begin
  67. ;; This is not great. We pull Xorg and Chromium just to Zip and
  68. ;; sign an extension. This should be implemented with something
  69. ;; lighter. (TODO: where is the CRXv3 documentation..?)
  70. (use-modules (guix build utils))
  71. (let ((chromium #$(file-append ungoogled-chromium "/bin/chromium"))
  72. (xvfb #$(file-append xorg-server "/bin/Xvfb"))
  73. (packdir "/tmp/extension"))
  74. (mkdir-p (dirname packdir))
  75. (copy-recursively (ungexp package package-output) packdir)
  76. (system (string-append xvfb " :1 &"))
  77. (setenv "DISPLAY" ":1")
  78. (sleep 2) ;give Xorg some time to initialize...
  79. ;; Chromium stores the current time in the .crx Zip archive.
  80. ;; Use a fixed timestamp for deterministic behavior.
  81. ;; FIXME (core-updates): faketime is missing an absolute reference
  82. ;; to 'date', hence the need to set PATH.
  83. (setenv "PATH" #$(file-append coreutils "/bin"))
  84. (invoke #$(file-append libfaketime "/bin/faketime")
  85. "2000-01-01 00:00:00"
  86. chromium
  87. "--user-data-dir=/tmp/signing-profile"
  88. (string-append "--pack-extension=" packdir)
  89. (string-append "--pack-extension-key=" #$signing-key))
  90. (copy-file (string-append packdir ".crx") #$output)))
  91. #:local-build? #t)))
  92. (define* (crx->chromium-json crx version)
  93. "Return a derivation that creates a Chromium JSON settings file for the
  94. extension given as CRX. VERSION is used to signify the CRX version, and
  95. must match the version listed in the extension manifest.json."
  96. ;; See chrome/browser/extensions/external_provider_impl.cc and
  97. ;; extensions/common/extension.h for documentation on the JSON format.
  98. (computed-file "extension.json"
  99. #~(call-with-output-file #$output
  100. (lambda (port)
  101. (format port "{
  102. \"external_crx\": \"~a\",
  103. \"external_version\": \"~a\"
  104. }
  105. "
  106. #$crx #$version)))
  107. #:local-build? #t))
  108. (define (signing-key->public-der key)
  109. "Return a derivation for a file containing the public key of KEY in DER
  110. format."
  111. (computed-file "der"
  112. #~(system* #$(file-append gnutls "/bin/certtool")
  113. "--load-privkey" #$key
  114. "--pubkey-info"
  115. "--outfile" #$output
  116. "--outder")
  117. #:local-build? #t))
  118. (define (chromium-json->profile-object json signing-key)
  119. "Return a derivation that installs JSON to the directory searched by
  120. Chromium, using a file name (aka extension ID) derived from SIGNING-KEY."
  121. (define der (signing-key->public-der signing-key))
  122. (with-extensions (list guile-gcrypt)
  123. (with-imported-modules '((guix build utils))
  124. (computed-file
  125. "chromium-extension"
  126. #~(begin
  127. (use-modules (guix build utils)
  128. (gcrypt base16)
  129. (gcrypt hash))
  130. (define (base16-string->chromium-base16 str)
  131. ;; Translate STR, a hexadecimal string, to a Chromium-style
  132. ;; representation using the letters a-p (where a=0, p=15).
  133. (define s1 "0123456789abcdef")
  134. (define s2 "abcdefghijklmnop")
  135. (let loop ((chars (string->list str))
  136. (converted '()))
  137. (if (null? chars)
  138. (list->string (reverse converted))
  139. (loop (cdr chars)
  140. (cons (string-ref s2 (string-index s1 (car chars)))
  141. converted)))))
  142. (let* ((checksum (bytevector->base16-string (file-sha256 #$der)))
  143. (file-name (base16-string->chromium-base16
  144. (string-take checksum 32)))
  145. (extension-directory (string-append #$output
  146. "/share/chromium/extensions")))
  147. (mkdir-p extension-directory)
  148. (symlink #$json (string-append extension-directory "/"
  149. file-name ".json"))))
  150. #:local-build? #t))))
  151. (define* (make-chromium-extension p #:optional (output "out"))
  152. "Create a Chromium extension from package P and return a package that,
  153. when installed, will make the extension contained in P available as a
  154. Chromium browser extension. OUTPUT specifies which output of P to use."
  155. (let* ((pname (package-name p))
  156. (version (package-version p))
  157. (signing-key (make-signing-key pname)))
  158. (package
  159. (inherit p)
  160. (name (string-append pname "-chromium"))
  161. (source #f)
  162. (build-system trivial-build-system)
  163. (native-inputs '())
  164. (inputs
  165. `(("extension" ,(chromium-json->profile-object
  166. (crx->chromium-json (make-crx signing-key p output)
  167. version)
  168. signing-key))))
  169. (propagated-inputs '())
  170. (outputs '("out"))
  171. (arguments
  172. '(#:modules ((guix build utils))
  173. #:builder
  174. (begin
  175. (use-modules (guix build utils))
  176. (copy-recursively (assoc-ref %build-inputs "extension")
  177. (assoc-ref %outputs "out"))))))))