summaryrefslogtreecommitdiff
path: root/pkgs/by-name/tp/tpm2-pkcs11/package.nix
blob: 33effd81306e460d96dbfd2b1107eabae3ee2e4c (about) (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{
  autoconf-archive,
  autoreconfHook,
  buildEnv,
  clangStdenv,
  cmocka,
  dbus,
  expect,
  fetchFromGitHub,
  glibc,
  gnutls,
  iproute2,
  lib,
  libyaml,
  makeWrapper,
  opensc,
  openssh,
  openssl,
  nss,
  p11-kit,
  patchelf,
  pkg-config,
  python3,
  stdenv,
  sqlite,
  swtpm,
  tpm2-abrmd,
  tpm2-openssl,
  tpm2-pkcs11, # for passthru abrmd tests
  tpm2-tools,
  tpm2-tss,
  which,
  xxd,
  abrmdSupport ? false,
  fapiSupport ? true,
  enableFuzzing ? false,
}:

let
  chosenStdenv = if enableFuzzing then clangStdenv else stdenv;
in
chosenStdenv.mkDerivation (finalAttrs: {
  pname = "tpm2-pkcs11";
  version = "1.9.1";

  src = fetchFromGitHub {
    owner = "tpm2-software";
    repo = "tpm2-pkcs11";
    tag = finalAttrs.version;
    hash = "sha256-W74ckrpK7ypny1L3Gn7nNbOVh8zbHavIk/TX3b8XbI8=";
  };

  # Disable Java‐based tests because of missing dependencies
  patches = [ ./disable-java-integration.patch ];

  postPatch = ''
    echo ${lib.escapeShellArg finalAttrs.version} >VERSION

    # Don't run git in the bootstrap
    substituteInPlace bootstrap --replace-warn "git" "# git"

    # Provide configuration file for D-Bus
    substituteInPlace Makefile.am --replace-fail \
      "dbus-run-session" \
      "dbus-run-session --config-file=${dbus}/share/dbus-1/session.conf"

    # Disable failing tests
    sed -E -i '/\<test\/integration\/(pkcs-crypt\.int|pkcs11-tool\.sh)\>/d' \
      Makefile-integration.am

    patchShebangs test tools

    # The preConfigure phase doesn't seem to be working here
    # ./bootstrap MUST be executed as the first step, before all
    # of the autoreconfHook stuff
    ./bootstrap
  '';

  configureFlags =
    [
      (lib.enableFeature finalAttrs.doCheck "unit")
      (lib.enableFeature finalAttrs.doCheck "integration")
    ]
    ++ lib.optionals enableFuzzing [
      "--enable-fuzzing"
      "--disable-hardening"
    ]
    ++ lib.optional fapiSupport "--with-fapi";

  strictDeps = true;

  nativeBuildInputs = [
    autoconf-archive
    autoreconfHook
    makeWrapper
    patchelf
    pkg-config
    (python3.withPackages (
      ps:
      with ps;
      [
        packaging
        pyyaml
        python-pkcs11
        cryptography
        pyasn1-modules
        tpm2-pytss
      ]
      ++ cryptography.optional-dependencies.ssh
    ))
  ];

  buildInputs = [
    libyaml
    opensc
    openssl
    sqlite
    tpm2-tools
    tpm2-tss
  ];

  nativeCheckInputs = [
    dbus
    expect
    gnutls
    iproute2
    nss.tools
    opensc
    openssh
    openssl
    p11-kit
    sqlite
    swtpm
    tpm2-abrmd
    tpm2-tools
    which
    xxd
  ];

  checkInputs = [
    cmocka
    tpm2-abrmd
  ];

  enableParallelBuilding = true;
  hardeningDisable = lib.optional enableFuzzing "all";

  outputs = [
    "out"
    "bin"
    "dev"
  ];

  doCheck = true;
  dontStrip = true;
  dontPatchELF = true;

  preCheck =
    let
      openssl-modules = buildEnv {
        name = "openssl-modules";
        pathsToLink = [ "/lib/ossl-modules" ];
        paths = map lib.getLib [
          openssl
          tpm2-openssl
        ];
      };
    in
    ''
      # Enable tests to load TCTI modules
      export LD_LIBRARY_PATH+=":${
        lib.makeLibraryPath [
          swtpm
          tpm2-tools
          tpm2-abrmd
        ]
      }"

      # Enable tests to load TPM2 OpenSSL module
      export OPENSSL_MODULES="${openssl-modules}/lib/ossl-modules"
    '';

  postInstall = ''
    mkdir -p $bin/bin/ $bin/share/tpm2_pkcs11/
    mv ./tools/* $bin/share/tpm2_pkcs11/
    makeWrapper $bin/share/tpm2_pkcs11/tpm2_ptool.py $bin/bin/tpm2_ptool \
      --prefix PATH : ${lib.makeBinPath [ tpm2-tools ]}
  '';

  # To be able to use the userspace resource manager, the RUNPATH must
  # explicitly include the tpm2-abrmd shared libraries.
  preFixup =
    let
      rpath = lib.makeLibraryPath (
        [
          glibc
          libyaml
          openssl
          sqlite
          tpm2-tss
        ]
        ++ (lib.optional abrmdSupport tpm2-abrmd)
      );
    in
    ''
      patchelf \
        --set-rpath ${rpath} \
        ${lib.optionalString abrmdSupport "--add-needed ${lib.makeLibraryPath [ tpm2-abrmd ]}/libtss2-tcti-tabrmd.so"} \
        --add-needed ${lib.makeLibraryPath [ tpm2-tss ]}/libtss2-tcti-device.so \
        $out/lib/libtpm2_pkcs11.so.0.0.0
    '';

  passthru = {
    tests.tpm2-pkcs11-abrmd = tpm2-pkcs11.override {
      abrmdSupport = true;
    };
  };

  meta = {
    description = "PKCS#11 interface for TPM2 hardware";
    homepage = "https://github.com/tpm2-software/tpm2-pkcs11";
    license = lib.licenses.bsd2;
    platforms = lib.platforms.linux;
    maintainers = with lib.maintainers; [ numinit ];
    mainProgram = "tpm2_ptool";
  };
})