1 { pkgs, lib, config, ... }:
3 inherit (builtins) toString;
4 inherit (pkgs.lib) types unlines;
5 inherit (config) openssl;
7 generateCerts = certs: unlines (lib.mapAttrsToList generateCert certs);
8 generateKey = id: cert: ''
9 mkdir -p "${openssl.opensslHome}/${id}"
10 test -s "$PASSWORD_STORE_DIR/${cert.passPrefix}/${id}/key.pem.gpg" || {
11 info " generateKey: $PASSWORD_STORE_DIR/${cert.passPrefix}/${id}/key.pem.gpg"
12 ${pkgs.openssl}/bin/openssl genrsa \
14 ${toString cert.keySize} |
15 ${pkgs.pass}/bin/pass insert --multiline "${cert.passPrefix}/${id}/key.pem"
18 generateCert = id: cert:
25 info " generateCert: ${openssl.opensslHome}/${id}/cert.self-signed.pem"
26 ${pkgs.pass}/bin/pass "${cert.passPrefix}/${id}/key.pem" |
28 ${pkgs.openssl}/bin/openssl req \
34 -config ${cert.opensslConf} \
35 -extensions self_signed_extensions \
36 -reqexts self_signed_extensions \
40 ${lib.optionalString (cert.days != null) "-days ${toString cert.days}"} \
41 -set_serial 0x$(sleep 1; date '+%Y%m%d%H%M%S') \
42 -reqopt no_pubkey,no_sigdump \
44 -out "${openssl.opensslHome}/${id}/cert.self-signed.pem"
46 if test ! -s "${openssl.opensslHome}/${id}/cert.self-signed.pem"
49 if test ! "${openssl.opensslHome}/${id}/cert.self-signed.pem" -nt \
50 "$PASSWORD_STORE_DIR/${cert.passPrefix}/${id}/key.pem.gpg"
53 test ! "''${DEEPCHECK:+set}" || {
54 info " generateCert: checking wether key and certificate match"
56 ${pkgs.pass}/bin/pass "${cert.passPrefix}/${id}/key.pem" |
57 ${pkgs.openssl}/bin/openssl rsa -modulus -noout -in /dev/stdin)"
59 ${pkgs.openssl}/bin/openssl x509 -noout -modulus \
60 -in "${openssl.opensslHome}/${id}/cert.self-signed.pem")"
61 test "$key_mod_pub" = "$crt_mod_pub" ||
62 error "key and certificate do not match"
69 echo >&2 "openssl-init: ERROR: $*"
75 echo >&2 "openssl-init: $*"
78 # Initialize the keyring according to openssl.certificates.
79 openssl-init = pkgs.writeShellScriptBin "openssl-init" (''
84 generateCerts openssl.certificates
86 openssl-cert-iter = pkgs.writeScriptBin "openssl-cert-iter" ''
88 # SYNTAX: $command .. <cert.pem
90 begin="-----BEGIN CERTIFICATE-----"
91 end="-----END CERTIFICATE-----"
94 ${pkgs.gnused}/bin/sed -n -e "/^$begin/,/^$end/p" |
95 while IFS= read -r line
99 ("$end") printf '%s\n%s\n%s\n' "$begin" "$(printf %s "$buf" | ${pkgs.gnused}/bin/sed -e 's/\(.\{64\}\)/\1\n/g')" "$end" | "$@";;
100 (*) buf="$buf$line";;
104 openssl-cert-print = pkgs.writeScriptBin "openssl-cert-print" ''
107 ${openssl-cert-iter}/bin/openssl-cert-iter \
108 ${pkgs.openssl}/bin/openssl x509 \
109 -nameopt multiline,-esc_msb,utf8 \
110 -certopt no_pubkey,no_sigdump \
112 ''${x509_md:+-"$x509_md"} \
116 ''${OPENSSL_FLAGS-} ''${OPENSSL_X509_FLAGS-}
118 openssl-cert-fetch = pkgs.writeScriptBin "openssl-cert-fetch" ''
121 x509="''${x509:-x509}"
125 ${pkgs.gnused}/bin/sed \
126 -e 's,^[a-z][a-z]*://,,' \
130 ${pkgs.gnused}/bin/sed \
131 -e 's,:[0-9][0-9]*,,')
132 mkdir -p "x509/$host"
133 begin="-----BEGIN CERTIFICATE-----"
134 end="-----END CERTIFICATE-----"
137 ${pkgs.openssl}/bin/openssl s_client \
139 -servername "$servername" \
142 ${pkgs.gnused}/bin/sed -n \
143 -e "/^$begin/,/^$end/p" \
144 >"$x509/$host/crt.pem"
149 enable = lib.mkEnableOption "Configuration of X.509 certificates";
150 opensslHome = lib.mkOption {
152 default = "sec/openssl";
157 certificates = lib.mkOption {
162 host = "example.coop";
163 distributionPoint = "http://example.coop/x509";
164 domains = [ "example.org" ];
170 type = types.attrsOf (types.submodule ({name, ...}:
171 let cert = openssl.certificates."${name}"; in
173 #config.uid = lib.mkDefault uid;
175 host = lib.mkOption {
176 type = types.nullOr types.str;
177 example = "example.coop";
179 passPrefix = lib.mkOption {
184 distributionPoint = lib.mkOption {
185 type = types.nullOr types.str;
187 example = "http://example.coop/x509";
189 domains = lib.mkOption {
190 type = types.listOf types.str;
191 example = [ "example.coop" ];
194 days = lib.mkOption {
199 keySize = lib.mkOption {
204 digest = lib.mkOption {
208 description = "A digest from openssl list --digest-commands";
210 opensslConf = lib.mkOption {
211 type = types.attrsOf types.lines;
215 cert.opensslConf_common //
216 cert.opensslConf_self-signed //
217 cert.opensslConf_auth-signed //
218 cert.opensslConf_user-cert //
221 pkgs.writeText "openssl.conf"
224 (sectionHead: sectionBody:
225 (if sectionHead == "" then "" else "[ ${sectionHead} ]\n") +
229 opensslConf_common = lib.mkOption {
230 type = types.attrsOf types.lines;
233 RANDFILE = ${openssl.opensslHome}/openssl.rand
234 oid_section = extra_oids
235 default_md = ${cert.digest}
241 distinguished_name = distinguished_name
243 #x509_extensions = root_extensions
244 #req_extensions = extension
245 #attributes = req_attributes
247 distinguished_name = ''
248 commonName = ${cert.host}
250 #stateOrProvinceName =
252 #"0.organizationName" =
253 #organizationalUnitName =
258 opensslConf_self-signed = lib.mkOption {
259 type = types.attrsOf types.lines;
261 self_signed_extensions = ''
262 basicConstraints = critical,CA:TRUE,pathlen:0
263 keyUsage = keyCertSign,cRLSign,digitalSignature,keyEncipherment
264 subjectAltName = ${lib.concatMapStringsSep "," (dom: "DNS:${dom}") cert.domains}
265 subjectKeyIdentifier = hash
266 issuerAltName = issuer:copy
267 authorityKeyIdentifier = keyid:always,issuer:always
268 '' + lib.optionalString (cert.distributionPoint != null) ''
269 authorityInfoAccess = caIssuers;URI:${cert.distributionPoint}/${cert.host}.cert.pem
270 crlDistributionPoints = URI:${cert.distributionPoint}/${cert.host}.crl.self-signed.pem
274 dir = ${openssl.opensslHome}/${name}
275 private_key = $dir/key.pem
277 crlnumber = $dir/crl.self-signed.num
278 crl = $dir/crl.self-signed.pem
279 database = $dir/idx.self-signed.txt
284 opensslConf_auth-signed = lib.mkOption {
285 type = types.attrsOf types.lines;
287 auth_signed_extensions = ''
288 basicConstraints = critical,CA:FALSE
289 keyUsage = digitalSignature,keyEncipherment
290 subjectAltName = ${lib.concatMapStringsSep "," (dom: "DNS:${dom}") cert.domains}
291 subjectKeyIdentifier = hash
292 issuerAltName = issuer:copy
293 authorityKeyIdentifier = keyid:always,issuer:always
294 certificatePolicies = @certificate_policies
295 '' + lib.optionalString (cert.distributionPoint != null) ''
296 authorityInfoAccess = caIssuers;URI:${cert.distributionPoint}/${cert.host}.cert.pem
297 crlDistributionPoints = URI:${cert.distributionPoint}/${cert.host}.crl.pem
299 certificate_policies = lib.optionalString (cert.distributionPoint != null) ''
300 policyIdentifier = 1.2.250.1.42
301 CPS.1 = https://${cert.distributionPoint}/cps
305 dir = ${openssl.opensslHome}/${name}
306 private_key = $dir/key.pem
308 crlnumber = $dir/crl.num
310 database = $dir/idx.txt
315 opensslConf_user-cert = lib.mkOption {
316 type = types.attrsOf types.lines;
318 user_cert_extensions = ''
319 basicConstraints = critical,CA:FALSE,pathlen:0
320 keyUsage = digitalSignature,keyEncipherment
321 subjectAltName = email:$ENV::user@${cert.host}
322 subjectKeyIdentifier = hash
323 issuerAltName = issuer:copy
324 authorityKeyIdentifier = keyid:always,issuer:always
325 '' + lib.optionalString (cert.distributionPoint != null) ''
326 authorityInfoAccess = caIssuers;URI:${cert.distributionPoint}/${cert.host}.cert.pem
336 config = lib.mkIf openssl.enable {
337 nix-shell.buildInputs = [