2019-10-26 15:22:40 +00:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.security.tpm2;
|
|
|
|
|
|
|
|
# This snippet is taken from tpm2-tss/dist/tpm-udev.rules, but modified to allow custom user/groups
|
|
|
|
# The idea is that the tssUser is allowed to acess the TPM and kernel TPM resource manager, while
|
|
|
|
# the tssGroup is only allowed to access the kernel resource manager
|
|
|
|
# Therefore, if either of the two are null, the respective part isn't generated
|
|
|
|
udevRules = tssUser: tssGroup: ''
|
|
|
|
${lib.optionalString (tssUser != null) ''KERNEL=="tpm[0-9]*", MODE="0660", OWNER="${tssUser}"''}
|
|
|
|
${lib.optionalString (tssUser != null || tssGroup != null)
|
|
|
|
''KERNEL=="tpmrm[0-9]*", MODE="0660"''
|
|
|
|
+ lib.optionalString (tssUser != null) '', OWNER="${tssUser}"''
|
|
|
|
+ lib.optionalString (tssGroup != null) '', GROUP="${tssGroup}"''
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.security.tpm2 = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Trusted Platform Module 2 support");
|
2019-10-26 15:22:40 +00:00
|
|
|
|
|
|
|
tssUser = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Name of the tpm device-owner and service user, set if applyUdevRules is
|
|
|
|
set.
|
|
|
|
'';
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = if cfg.abrmd.enable then "tss" else "root";
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = lib.literalExpression ''if config.security.tpm2.abrmd.enable then "tss" else "root"'';
|
2019-10-26 15:22:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tssGroup = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Group of the tpm kernel resource manager (tpmrm) device-group, set if
|
|
|
|
applyUdevRules is set.
|
|
|
|
'';
|
|
|
|
type = lib.types.nullOr lib.types.str;
|
|
|
|
default = "tss";
|
|
|
|
};
|
|
|
|
|
|
|
|
applyUdevRules = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Whether to make the /dev/tpm[0-9] devices accessible by the tssUser, or
|
|
|
|
the /dev/tpmrm[0-9] by tssGroup respectively
|
|
|
|
'';
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
abrmd = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Trusted Platform 2 userspace resource manager daemon
|
2022-08-28 19:18:44 +00:00
|
|
|
'');
|
2019-10-26 15:22:40 +00:00
|
|
|
|
|
|
|
package = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "tpm2-abrmd package to use";
|
2019-10-26 15:22:40 +00:00
|
|
|
type = lib.types.package;
|
|
|
|
default = pkgs.tpm2-abrmd;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = lib.literalExpression "pkgs.tpm2-abrmd";
|
2019-10-26 15:22:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
pkcs11 = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
TPM2 PKCS#11 tool and shared library in system path
|
2022-08-28 19:18:44 +00:00
|
|
|
(`/run/current-system/sw/lib/libtpm2_pkcs11.so`)
|
|
|
|
'');
|
2019-10-26 15:22:40 +00:00
|
|
|
|
|
|
|
package = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "tpm2-pkcs11 package to use";
|
2019-10-26 15:22:40 +00:00
|
|
|
type = lib.types.package;
|
|
|
|
default = pkgs.tpm2-pkcs11;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = lib.literalExpression "pkgs.tpm2-pkcs11";
|
2019-10-26 15:22:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
tctiEnvironment = {
|
|
|
|
enable = lib.mkOption {
|
2022-08-29 21:34:22 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Set common TCTI environment variables to the specified value.
|
|
|
|
The variables are
|
2022-08-29 21:34:22 +00:00
|
|
|
- `TPM2TOOLS_TCTI`
|
|
|
|
- `TPM2_PKCS11_TCTI`
|
2019-10-26 15:22:40 +00:00
|
|
|
'';
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
The name of the TPM command transmission interface (TCTI) library to
|
|
|
|
use.
|
|
|
|
'';
|
|
|
|
type = lib.types.enum [ "tabrmd" "device" ];
|
|
|
|
default = "device";
|
|
|
|
};
|
|
|
|
|
|
|
|
deviceConf = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Configuration part of the device TCTI, e.g. the path to the TPM device.
|
|
|
|
Applies if interface is set to "device".
|
|
|
|
The format is specified in the
|
2022-07-28 21:19:15 +00:00
|
|
|
[
|
|
|
|
tpm2-tools repository](https://github.com/tpm2-software/tpm2-tools/blob/master/man/common/tcti.md#tcti-options).
|
2019-10-26 15:22:40 +00:00
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "/dev/tpmrm0";
|
|
|
|
};
|
|
|
|
|
|
|
|
tabrmdConf = lib.mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-26 15:22:40 +00:00
|
|
|
Configuration part of the tabrmd TCTI, like the D-Bus bus name.
|
|
|
|
Applies if interface is set to "tabrmd".
|
|
|
|
The format is specified in the
|
2022-07-28 21:19:15 +00:00
|
|
|
[
|
|
|
|
tpm2-tools repository](https://github.com/tpm2-software/tpm2-tools/blob/master/man/common/tcti.md#tcti-options).
|
2019-10-26 15:22:40 +00:00
|
|
|
'';
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "bus_name=com.intel.tss2.Tabrmd";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable (lib.mkMerge [
|
|
|
|
{
|
|
|
|
# PKCS11 tools and library
|
|
|
|
environment.systemPackages = lib.mkIf cfg.pkcs11.enable [
|
|
|
|
(lib.getBin cfg.pkcs11.package)
|
|
|
|
(lib.getLib cfg.pkcs11.package)
|
|
|
|
];
|
|
|
|
|
|
|
|
services.udev.extraRules = lib.mkIf cfg.applyUdevRules
|
|
|
|
(udevRules cfg.tssUser cfg.tssGroup);
|
|
|
|
|
|
|
|
# Create the tss user and group only if the default value is used
|
|
|
|
users.users.${cfg.tssUser} = lib.mkIf (cfg.tssUser == "tss") {
|
|
|
|
isSystemUser = true;
|
2021-09-19 19:40:54 +00:00
|
|
|
group = "tss";
|
2019-10-26 15:22:40 +00:00
|
|
|
};
|
|
|
|
users.groups.${cfg.tssGroup} = lib.mkIf (cfg.tssGroup == "tss") {};
|
|
|
|
|
|
|
|
environment.variables = lib.mkIf cfg.tctiEnvironment.enable (
|
|
|
|
lib.attrsets.genAttrs [
|
|
|
|
"TPM2TOOLS_TCTI"
|
|
|
|
"TPM2_PKCS11_TCTI"
|
|
|
|
] (_: ''${cfg.tctiEnvironment.interface}:${
|
|
|
|
if cfg.tctiEnvironment.interface == "tabrmd" then
|
|
|
|
cfg.tctiEnvironment.tabrmdConf
|
|
|
|
else
|
|
|
|
cfg.tctiEnvironment.deviceConf
|
|
|
|
}'')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
(lib.mkIf cfg.abrmd.enable {
|
|
|
|
systemd.services."tpm2-abrmd" = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "dbus";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 30;
|
|
|
|
BusName = "com.intel.tss2.Tabrmd";
|
|
|
|
ExecStart = "${cfg.abrmd.package}/bin/tpm2-abrmd";
|
|
|
|
User = "tss";
|
2021-09-19 19:40:54 +00:00
|
|
|
Group = "tss";
|
2019-10-26 15:22:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.dbus.packages = lib.singleton cfg.abrmd.package;
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
meta.maintainers = with lib.maintainers; [ lschuermann ];
|
|
|
|
}
|