2017-02-10 17:36:36 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
2018-04-26 05:19:48 +01:00
|
|
|
|
2017-02-10 17:36:36 +00:00
|
|
|
let
|
|
|
|
cfg = config.security.dhparams;
|
2018-04-26 05:19:48 +01:00
|
|
|
|
|
|
|
paramsSubmodule = { name, config, ... }: {
|
|
|
|
options.bits = mkOption {
|
|
|
|
type = types.addCheck types.int (b: b >= 16) // {
|
|
|
|
name = "bits";
|
|
|
|
description = "integer of at least 16 bits";
|
|
|
|
};
|
|
|
|
default = 4096;
|
|
|
|
description = ''
|
|
|
|
The bit size for the prime that is used during a Diffie-Hellman
|
|
|
|
key exchange.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.path = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
readOnly = true;
|
|
|
|
description = ''
|
|
|
|
The resulting path of the generated Diffie-Hellman parameters
|
|
|
|
file for other services to reference. This could be either a
|
|
|
|
store path or a file inside the directory specified by
|
|
|
|
<option>security.dhparams.path</option>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
config.path = "${cfg.path}/${name}.pem";
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
2017-02-10 17:36:36 +00:00
|
|
|
options = {
|
|
|
|
security.dhparams = {
|
|
|
|
params = mkOption {
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Diffie-Hellman parameters to generate.
|
|
|
|
|
|
|
|
The value is the size (in bits) of the DH params to generate. The
|
|
|
|
generated DH params path can be found in
|
2018-04-26 05:19:48 +01:00
|
|
|
<literal>config.security.dhparams.params.<replaceable>name</replaceable>.path</literal>.
|
2017-02-10 17:36:36 +00:00
|
|
|
|
2018-04-26 05:19:48 +01:00
|
|
|
<note><para>The name of the DH params is taken as being the name of
|
|
|
|
the service it serves and the params will be generated before the
|
|
|
|
said service is started.</para></note>
|
2017-03-17 00:56:13 +00:00
|
|
|
|
2018-04-26 05:19:48 +01:00
|
|
|
<warning><para>If you are removing all dhparams from this list, you
|
|
|
|
have to leave <option>security.dhparams.enable</option> for at
|
|
|
|
least one activation in order to have them be cleaned up. This also
|
|
|
|
means if you rollback to a version without any dhparams the
|
|
|
|
existing ones won't be cleaned up.</para></warning>
|
2017-02-10 17:36:36 +00:00
|
|
|
'';
|
2018-04-26 05:19:48 +01:00
|
|
|
type = with types; let
|
|
|
|
coerce = bits: { inherit bits; };
|
|
|
|
in attrsOf (coercedTo types.int coerce (submodule paramsSubmodule));
|
2017-02-10 17:36:36 +00:00
|
|
|
default = {};
|
2018-04-26 05:19:48 +01:00
|
|
|
example = literalExample "{ nginx.bits = 3072; }";
|
2017-02-10 17:36:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOption {
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Path to the directory in which Diffie-Hellman parameters will be
|
|
|
|
stored.
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/dhparams";
|
|
|
|
};
|
2017-03-17 00:56:13 +00:00
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to generate new DH params and clean up old DH params.
|
|
|
|
'';
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
};
|
2017-02-10 17:36:36 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-17 00:56:13 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services = {
|
|
|
|
dhparams-init = {
|
|
|
|
description = "Cleanup old Diffie-Hellman parameters";
|
|
|
|
wantedBy = [ "multi-user.target" ]; # Clean up even when no DH params is set
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script =
|
|
|
|
# Create directory
|
|
|
|
''
|
|
|
|
if [ ! -d ${cfg.path} ]; then
|
|
|
|
mkdir -p ${cfg.path}
|
2017-02-10 17:36:36 +00:00
|
|
|
fi
|
2017-03-17 00:56:13 +00:00
|
|
|
'' +
|
|
|
|
# Remove old dhparams
|
|
|
|
''
|
|
|
|
for file in ${cfg.path}/*; do
|
|
|
|
if [ ! -f "$file" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
2018-04-26 05:19:48 +01:00
|
|
|
'' + concatStrings (mapAttrsToList (name: { bits, ... }:
|
2017-03-17 00:56:13 +00:00
|
|
|
''
|
|
|
|
if [ "$file" == "${cfg.path}/${name}.pem" ] && \
|
2018-04-26 05:19:48 +01:00
|
|
|
${pkgs.openssl}/bin/openssl dhparam -in "$file" -text | head -n 1 | grep "(${toString bits} bit)" > /dev/null; then
|
2017-03-17 00:56:13 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
) cfg.params) +
|
|
|
|
''
|
|
|
|
rm $file
|
|
|
|
done
|
2017-02-10 17:36:36 +00:00
|
|
|
|
2017-03-17 00:56:13 +00:00
|
|
|
# TODO: Ideally this would be removing the *former* cfg.path, though this
|
|
|
|
# does not seem really important as changes to it are quite unlikely
|
|
|
|
rmdir --ignore-fail-on-non-empty ${cfg.path}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
} //
|
2018-04-26 05:19:48 +01:00
|
|
|
mapAttrs' (name: { bits, ... }: nameValuePair "dhparams-gen-${name}" {
|
2017-03-17 00:56:13 +00:00
|
|
|
description = "Generate Diffie-Hellman parameters for ${name} if they don't exist yet";
|
|
|
|
after = [ "dhparams-init.service" ];
|
|
|
|
before = [ "${name}.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script =
|
|
|
|
''
|
|
|
|
mkdir -p ${cfg.path}
|
|
|
|
if [ ! -f ${cfg.path}/${name}.pem ]; then
|
2018-04-26 05:19:48 +01:00
|
|
|
${pkgs.openssl}/bin/openssl dhparam -out ${cfg.path}/${name}.pem ${toString bits}
|
2017-03-17 00:56:13 +00:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
}) cfg.params;
|
|
|
|
};
|
2017-02-10 17:36:36 +00:00
|
|
|
}
|