aboutsummaryrefslogtreecommitdiff
path: root/gnu/services/ca.scm
blob: 1b10ef3e1cfe115e7bd429912a4c833cda08c2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; SPDX-License-Identifier: CC0-1.0
;;;
;;; Copyright © 2023, 2024 Wojtek Kosior <koszko@koszko.org>

(define-module (gnu services ca)
  #:use-module ((srfi srfi-1) #:select (concatenate))
  #:use-module ((guix records) #:select (define-record-type* match-record))
  #:use-module ((guix gexp) #:select (gexp file-append))
  #:use-module ((gnu services) #:select
                (service-extension activation-service-type service-type))
  #:use-module ((gnu packages tls) #:select (openssl))
  #:export (snakeoil-service-type
            snakeoil-configuration
            snakeoil-configuration?
            snakeoil-cert-configuration))

(define-record-type* <snakeoil-cert-configuration> snakeoil-cert-configuration
  make-snakeoil-cert-configuration snakeoil-cert-configuration?
  (name            snakeoil-cert-configuration-name
                   (default #f))
  (domains         snakeoil-cert-configuration-domains
                   (default '()))
  (key-group-owner snakeoil-cert-configuration-key-group-owner
                   (default #f)))

(define-record-type* <snakeoil-configuration> snakeoil-configuration
  make-snakeoil-configuration snakeoil-configuration?
  (openssl      snakeoil-configuration-openssl
                (default openssl))
  (owner        snakeoil-configuration-owner
                (default "root"))
  (group-owner  snakeoil-configuration-group-owner
                (default #f))
  (rsa-key-size snakeoil-configuration-rsa-key-size
                (default 4096))
  (certificates snakeoil-configuration-certificates
                (default '())))

(define (snakeoil-activation config)
  (match-record config <snakeoil-configuration>
    (openssl owner group-owner rsa-key-size certificates)
    #~(begin
        (use-modules (ice-9 format)
                     (ice-9 textual-ports))

        (define openssl
          #$(file-append openssl "/bin/openssl"))

        (define pwnam
          (getpw #$owner))

        (define grnam
          (and=> #$group-owner getgr))

        (define (chown-file filename)
          (chown filename (passwd:uid pwnam) (or (and=> grnam group:gid) -1)))

        (define (generate-private-key path)
          (let ((initial-umask (umask))
                (tmp-path (string-append path ".tmp.pem")))
            (umask #o027)
            (system* openssl "genrsa" "-out" tmp-path
                     #$(number->string rsa-key-size))
            (rename-file tmp-path path)
            (umask initial-umask)))

        (mkdir-p "/etc/snakeoil/certs")

        (unless (false-if-exception
                 (stat "/etc/snakeoil/private/key.pem"))
          (mkdir-p "/etc/snakeoil/private/")
          (generate-private-key "/etc/snakeoil/private/key.pem"))

        (unless (false-if-exception
                 (stat "/etc/snakeoil/root-cert.pem"))
          ;; Self-sign a local certificate authority.
          (system* openssl "req"
                   "-x509" "-new" "-nodes" "-sha256"
                   "-days" (number->string (* 20 365))
                   "-out" "/etc/snakeoil/root-cert.pem"
                   "-key" "/etc/snakeoil/private/key.pem"
                   "-subj" "/CN=snakeoil-root"))

        (chown-file "/etc/snakeoil")
        (chown-file "/etc/snakeoil/certs")
        (chown-file "/etc/snakeoil/private")
        (chown-file "/etc/snakeoil/private/key.pem")
        (chown-file "/etc/snakeoil/root-cert.pem")

        (define (issue-cert cert-name domains key-group-owner)
          (let* ((cert-name* (or cert-name (car domains)))
                 (cert-dir (format #f "/etc/snakeoil/certs/~a" cert-name*))
                 (tmp-dir (format #f "/etc/snakeoil/certs-tmp/~a" cert-name*))
                 (cert-path (format #f "~a/cert.pem" tmp-dir))
                 (chain-link-path (format #f "~a/chain.pem" tmp-dir))
                 (key-path (format #f "~a/privkey.pem" tmp-dir))
                 (fullchain-path (format #f "~a/fullchain.pem" tmp-dir))
                 (csr-path (format #f "~a/csr.pem" tmp-dir)))
            (unless (false-if-exception (stat cert-dir))
              (system* "rm" "-rf" tmp-dir)
              (mkdir-p tmp-dir)

              (generate-private-key key-path)

              (system* openssl "req"
                       "-new" "-nodes" "-out" csr-path
                       "-key" key-path
                       "-subj" (format #f "/CN=~a" (car domains))
                       "-addext" (format #f "subjectAltName=~{DNS:~a~^,~}"
                                         domains))

              (system* openssl "x509"
                       "-req" "-sha256" "-CAcreateserial"
                       "-days" (number->string (* 20 365))
                       "-copy_extensions=copyall"
                       "-in" csr-path
                       "-CA" "/etc/snakeoil/root-cert.pem"
                       "-CAkey" "/etc/snakeoil/private/key.pem"
                       "-out" cert-path "-days" (number->string (* 20 365)))

              (symlink "../../../snakeoil/root-cert.pem" chain-link-path)

              ;; Concatenate cert.pem and chain.pem into fullchain.pem.
              (with-output-to-file fullchain-path
                (lambda _
                  (for-each (lambda (part)
                              (call-with-input-file part
                                (compose display get-string-all)))
                            (list cert-path chain-link-path))))

              (delete-file csr-path)

              (map chown-file
                   (list tmp-dir cert-path fullchain-path))

              (when key-group-owner
                (chown key-path -1  (if (integer? key-group-owner)
                                        key-group-owner
                                        (group:gid (getgr key-group-owner))))
                (chmod key-path #o640))

              (rename-file tmp-dir cert-dir))))

        (map issue-cert
             '#$(map snakeoil-cert-configuration-name certificates)
             '#$(map snakeoil-cert-configuration-domains certificates)
             '#$(map snakeoil-cert-configuration-key-group-owner
                     certificates)))))

(define snakeoil-service-type
  (service-type
   (name 'snakeoil)
   (extensions
    (list (service-extension activation-service-type snakeoil-activation)))
   (compose concatenate)
   (extend (lambda (config extra-certificates)
             (snakeoil-configuration
              (inherit config)
              (certificates (append
                             (snakeoil-configuration-certificates config)
                             extra-certificates)))))
   (default-value (snakeoil-configuration))
   (description "Generate self-issued TLS certificates.")))