unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 4fac7721847235f4b7ca28aad7c8c569f7305957 3933 bytes (raw)
name: etc/new-client-cert.scm 	 # note: path name is non-authoritative(*)

  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
 
#!/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>
;;; Copyright © 2023 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; 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 format)
             (ice-9 match)
             (guix build utils))

(define %user (or (getenv "SUDO_USER")
                  (getenv "USER")))

(define %user-id (passwd:uid (getpwnam %user)))

(define %group-id (passwd:gid (getpwnam %user)))

(define %CA-directory
  "/etc/ssl-ca")

(define subject-template
  "/C=DE/ST=Berlin/L=Berlin/O=GNU Guix/OU=Cuirass/CN=~a")

(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-ca!)
  "Generate a private certificate authority (CA) valid for 10 years."
  (mkdir-p (dirname CA-key))
  (mkdir-p (dirname CA-cert))
  (invoke "openssl" "req" "-newkey" "rsa" "-x509" "-days" "3650"
	  "-noenc"                      ;no password
	  "-subj" (format #false "~@?" subject-template "Cuirass CA")
          "-keyout" CA-key "-out" CA-cert))

(define (generate-csr! who)
  "Generate a new certificate signing request and key for WHO."
  (let ((key (key-file who))
        (csr (csr-file who)))
    (invoke "openssl" "req" "-newkey" "rsa"
	    "-noenc"                    ;no password
	    "-subj" (format #false "~@?" subject-template who)
            "-keyout" key
	    "-out" csr)
    (chown key %user-id %group-id)
    (chown csr %user-id %group-id)))

(define* (generate-client-certificate! who #:key (expiry 365))
  "Generate a client certificate for WHO."
  (let ((cert (client-cert-file who)))
    (invoke "openssl" "x509" "-req"
            "-in" (csr-file who)
            "-CA" CA-cert
            "-CAkey" CA-key
            "-out" cert
            "-days" (number->string expiry))
    (chown cert %user-id %group-id)))

(define (export-p12! who)
  (let ((key (key-file who))
        (exported-cert (exported-cert-file who)))
    (invoke "openssl" "pkcs12" "-export"
	    "-in" (client-cert-file who)
	    "-inkey" key
	    "-out" exported-cert)
    (chown key %user-id %group-id)
    (chown exported-cert %user-id %group-id)))

(define (main args)
  (match (command-line)
    ((script)
     (set-program-arguments (list script %user))
     (apply main args))
    ((script "--generate-ca")
     (generate-ca!))
    ((script who)
     (generate-csr! who)
     (generate-client-certificate! who)
     (export-p12! who))
    ((script . rest)
     (format (current-error-port) "usage: ~a [--generate-ca|name]~%" script))))

debug log:

solving 4fac772 ...
found 4fac772 in https://yhetil.org/guix-patches/20230511043452.14263-1-maxim.cournoyer@gmail.com/

applying [1/1] https://yhetil.org/guix-patches/20230511043452.14263-1-maxim.cournoyer@gmail.com/
diff --git a/etc/new-client-cert.scm b/etc/new-client-cert.scm
new file mode 100755
index 0000000..4fac772

Checking patch etc/new-client-cert.scm...
Applied patch etc/new-client-cert.scm cleanly.

index at:
100755 4fac7721847235f4b7ca28aad7c8c569f7305957	etc/new-client-cert.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).