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
| | #!/usr/bin/env -S guix shell guile openssl -- guile \\
--no-auto-compile -e main -s
!#
;;;; cuirass.scm -- Cuirass public interface.
;;; Copyright © 2023 Ricardo Wurmus <rekado@elephly.net>
;;;
;;; This file is part of Cuirass.
;;;
;;; Cuirass is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Cuirass is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Cuirass. If not, see <http://www.gnu.org/licenses/>.
(use-modules (ice-9 match)
(guix build utils))
(define %CA-directory
"/etc/ssl-ca")
(define CA-key
(string-append %CA-directory "/private/ca.key"))
(define CA-cert
(string-append %CA-directory "/certs/ca.crt"))
(define* (output who file)
(string-append (getcwd) "/" who file))
(define (key-file who)
"Return the absolute file name of the key file for WHO."
(output who ".key"))
(define (csr-file who)
"Return the absolute file name of the CSR file for WHO."
(output who ".csr"))
(define (client-cert-file who)
"Return the absolute file name of the client certificate file for
WHO."
(output who ".crt"))
(define (exported-cert-file who)
"Return the absolute file name of the pkcs12 client certificate file
for WHO. This is the file that users should import into their
browsers."
(output who ".p12"))
(define (generate-csr! who)
"Generate a new certificate signing request and key for WHO."
(invoke "openssl" "req" "-newkey" "rsa:4096"
"-nodes" ;no password
"-subj"
(format #false "/C=DE/ST=Berlin/L=Berlin/O=GNU Guix/OU=Cuirass/CN=~a" who)
"-keyout" (key-file who)
"-out" (csr-file who)))
(define* (generate-client-certificate! who #:key (expiry 365))
"Generate a client certificate for WHO."
(invoke "openssl" "x509" "-req"
"-in" (csr-file who)
"-CA" CA-cert
"-CAkey" CA-key
"-out" (client-cert-file who)
"-days" (number->string expiry)))
(define (export-p12! who)
(invoke "openssl" "pkcs12" "-export"
"-in" (client-cert-file who)
"-inkey" (key-file who)
"-out" (exported-cert-file who)))
(define (main args)
(match (command-line)
((script)
(set-program-arguments (list script (or (getenv "SUDO_USER")
(getenv "USER"))))
(apply main args))
((script who)
(generate-csr! who)
(generate-client-certificate! who)
(export-p12! who))
((script . rest)
(format (current-error-port) "usage: ~a [name]~%" script))))
|