• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / swtpm / #2824

13 May 2025 12:34PM UTC coverage: 72.964% (-0.5%) from 73.462%
#2824

push

travis-ci

web-flow
Merge c2b02e9b2 into 1544c99ca

7033 of 9639 relevant lines covered (72.96%)

13748.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.74
/src/swtpm_setup/swtpm_setup.c
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
/*
3
 * swtpm_setup.c: Tool to simulate TPM 1.2 & TPM 2 manufacturing
4
 *
5
 * Author: Stefan Berger, stefanb@linux.ibm.com
6
 *
7
 * Copyright (c) IBM Corporation, 2021
8
 */
9

10
#include "config.h"
11

12
#include <errno.h>
13
#include <getopt.h>
14
#include <grp.h>
15
#include <limits.h>
16
#include <pwd.h>
17
#include <stdlib.h>
18
#include <stdio.h>
19
#include <string.h>
20
#include <unistd.h>
21
#include <sys/stat.h>
22
#include <fcntl.h>
23
#include <sys/types.h>
24
#include <sys/wait.h>
25

26
#include <glib.h>
27
#include <glib/gstdio.h>
28
#include <glib/gprintf.h>
29

30
#include <glib-object.h>
31
#include <json-glib/json-glib.h>
32

33
#include <libtpms/tpm_nvfilename.h>
34

35
#include "swtpm.h"
36
#include "swtpm_setup_conf.h"
37
#include "swtpm_setup_utils.h"
38
#include "swtpm_utils.h"
39

40
#include <openssl/sha.h>
41

42
/* default values for passwords */
43
#define DEFAULT_OWNER_PASSWORD "ooo"
44
#define DEFAULT_SRK_PASSWORD   "sss"
45

46
#define SETUP_CREATE_EK_F           (1 << 0)
47
#define SETUP_TAKEOWN_F             (1 << 1)
48
#define SETUP_EK_CERT_F             (1 << 2)
49
#define SETUP_PLATFORM_CERT_F       (1 << 3)
50
#define SETUP_LOCK_NVRAM_F          (1 << 4)
51
#define SETUP_SRKPASS_ZEROS_F       (1 << 5)
52
#define SETUP_OWNERPASS_ZEROS_F     (1 << 6)
53
#define SETUP_STATE_OVERWRITE_F     (1 << 7)
54
#define SETUP_STATE_NOT_OVERWRITE_F (1 << 8)
55
#define SETUP_TPM2_F                (1 << 9)
56
#define SETUP_ALLOW_SIGNING_F       (1 << 10)
57
#define SETUP_TPM2_ECC_F            (1 << 11)
58
#define SETUP_CREATE_SPK_F          (1 << 12)
59
#define SETUP_DISPLAY_RESULTS_F     (1 << 13)
60
#define SETUP_DECRYPTION_F          (1 << 14)
61
#define SETUP_WRITE_EK_CERT_FILES_F (1 << 15)
62
#define SETUP_RECONFIGURE_F         (1 << 16)
63

64
/* default configuration file */
65
#define SWTPM_SETUP_CONF "swtpm_setup.conf"
66

67
/* Default logging goes to stderr */
68
gchar *gl_LOGFILE = NULL;
69

70
#define DEFAULT_RSA_KEYSIZE 2048
71

72

73
static const struct flag_to_certfile {
74
    unsigned long flag;
75
    const char *filename;
76
    const char *type;
77
} flags_to_certfiles[] = {
78
    {.flag = SETUP_EK_CERT_F      , .filename = "ek.cert",       .type = "ek" },
79
    {.flag = SETUP_PLATFORM_CERT_F, .filename = "platform.cert", .type = "platform" },
80
    {.flag = 0,                     .filename = NULL,            .type = NULL},
81
};
82

83
/* initialize the path of the config_file */
84
static int init(gchar **config_file)
83✔
85
{
86
    const gchar *configdir = g_get_user_config_dir();
83✔
87

88
    *config_file = g_build_filename(configdir, SWTPM_SETUP_CONF, NULL);
83✔
89
    if (access(*config_file, R_OK) != 0) {
83✔
90
        g_free(*config_file);
83✔
91
        *config_file = g_build_filename(SYSCONFDIR, SWTPM_SETUP_CONF, NULL);
83✔
92
    }
93

94
    return 0;
83✔
95
}
96

97
/* Get the spec and attributes parameters from swtpm */
98
static int tpm_get_specs_and_attributes(struct swtpm *swtpm, gchar ***params)
72✔
99
{
100
    int ret;
72✔
101
    g_autofree gchar *json = NULL;
144✔
102
    JsonParser *jp = NULL;
72✔
103
    GError *error = NULL;
72✔
104
    JsonReader *jr = NULL;
72✔
105
    JsonNode *root;
72✔
106
    static const struct parse_rule {
72✔
107
         const char *node1;
108
         const char *node2;
109
         gboolean is_int;
110
         const char *optname;
111
    } parser_rules[7] = {
112
         {"TPMSpecification", "family", FALSE, "--tpm-spec-family"},
113
         {"TPMSpecification", "level", TRUE, "--tpm-spec-level"},
114
         {"TPMSpecification", "revision", TRUE, "--tpm-spec-revision"},
115
         {"TPMAttributes", "manufacturer", FALSE, "--tpm-manufacturer"},
116
         {"TPMAttributes", "model", FALSE, "--tpm-model"},
117
         {"TPMAttributes", "version", FALSE, "--tpm-version"},
118
         {NULL, NULL, FALSE, NULL},
119
    };
120
    size_t idx;
72✔
121

122
    ret = swtpm->cops->ctrl_get_tpm_specs_and_attrs(swtpm, &json);
72✔
123
    if (ret != 0) {
72✔
124
        logerr(gl_LOGFILE, "Could not get the TPM spec and attribute parameters.\n");
×
125
        return 1;
×
126
    }
127

128
    jp = json_parser_new();
72✔
129

130
    if (!json_parser_load_from_data(jp, json, -1, &error)) {
72✔
131
        logerr(gl_LOGFILE, "JSON parser failed: %s\n", error->message);
×
132
        g_error_free(error);
×
133
        goto error;
×
134
    }
135

136
    *params = NULL;
72✔
137
    root = json_parser_get_root(jp);
72✔
138

139
    for (idx = 0; parser_rules[idx].node1 != NULL; idx++) {
576✔
140
        jr = json_reader_new(root);
432✔
141
        if (json_reader_read_member(jr, parser_rules[idx].node1) &&
864✔
142
            json_reader_read_member(jr, parser_rules[idx].node2)) {
432✔
143
            gchar *str;
432✔
144

145
            if (parser_rules[idx].is_int)
432✔
146
                str = g_strdup_printf("%ld", (long)json_reader_get_int_value(jr));
144✔
147
            else
148
                str = g_strdup(json_reader_get_string_value(jr));
288✔
149

150
            *params = concat_arrays(*params,
1,296✔
151
                                    (gchar*[]){
432✔
152
                                        g_strdup(parser_rules[idx].optname),
432✔
153
                                        str,
154
                                        NULL
155
                                    }, TRUE);
156
        } else {
157
            logerr(gl_LOGFILE, "Could not find [%s][%s] in '%s'\n",
×
158
                   parser_rules[idx].node1, parser_rules[idx].node2, json);
×
159
            ret = 1;
×
160
            break;
×
161
        }
162
        g_object_unref(jr);
432✔
163
        jr = NULL;
432✔
164
    }
165

166
    if (ret) {
×
167
        g_strfreev(*params);
×
168
        *params = NULL;
×
169
        g_object_unref(jr);
×
170
    }
171
error:
72✔
172
    g_object_unref(jp);
72✔
173

174
    return ret;
72✔
175
}
176

177
/* Call an external tool to create the certificates */
178
static int call_create_certs(unsigned long flags, const gchar *configfile, const gchar *certsdir,
72✔
179
                             const gchar *ekparam, const gchar *vmid, struct swtpm *swtpm)
180
{
181
    gchar **config_file_lines = NULL; /* must free */
72✔
182
    g_autofree gchar *create_certs_tool = NULL;
144✔
183
    g_autofree gchar *create_certs_tool_config = NULL;
72✔
184
    g_autofree gchar *create_certs_tool_options = NULL;
72✔
185
    g_autofree gchar **cmd = NULL;
72✔
186
    gchar **params = NULL; /* must free */
72✔
187
    g_autofree gchar *prgname = NULL;
72✔
188
    gboolean success;
72✔
189
    gint exit_status;
72✔
190
    size_t idx, j;
72✔
191
    gchar *s;
72✔
192
    int ret;
72✔
193

194
    ret = tpm_get_specs_and_attributes(swtpm, &params);
72✔
195
    if (ret != 0)
72✔
196
        goto error;
×
197

198
    ret = read_file_lines(configfile, &config_file_lines);
72✔
199
    if (ret != 0)
72✔
200
        goto error;
×
201

202
    create_certs_tool = get_config_value(config_file_lines, "create_certs_tool");
72✔
203
    create_certs_tool_config = get_config_value(config_file_lines, "create_certs_tool_config");
72✔
204
    create_certs_tool_options = get_config_value(config_file_lines, "create_certs_tool_options");
72✔
205

206
    ret = 0;
72✔
207

208
    if (create_certs_tool != NULL) {
72✔
209
        g_autofree gchar *create_certs_tool_path = g_find_program_in_path(create_certs_tool);
144✔
210
        if (create_certs_tool_path == NULL) {
72✔
211
            logerr(gl_LOGFILE, "Could not find %s in PATH.\n", create_certs_tool);
×
212
            ret = 1;
×
213
            goto error;
×
214
        }
215

216
        if (flags & SETUP_TPM2_F) {
72✔
217
            params = concat_arrays(params,
64✔
218
                                (gchar*[]){
64✔
219
                                    g_strdup("--tpm2"),
64✔
220
                                    NULL
221
                                }, TRUE);
222
        }
223
        cmd = concat_arrays((gchar*[]) {
72✔
224
                                create_certs_tool_path,
225
                                "--type", "_",  /* '_' must be at index '2' ! */
226
                                "--ek", (gchar *)ekparam,
227
                                "--dir", (gchar *)certsdir,
228
                                NULL
229
                            }, NULL, FALSE);
230
        if (gl_LOGFILE != NULL)
72✔
231
            cmd = concat_arrays(cmd, (gchar*[]){"--logfile", (gchar *)gl_LOGFILE, NULL}, TRUE);
7✔
232
        if (vmid != NULL)
72✔
233
            cmd = concat_arrays(cmd, (gchar*[]){"--vmid", (gchar *)vmid, NULL}, TRUE);
65✔
234
        cmd = concat_arrays(cmd, params, TRUE);
72✔
235
        if (create_certs_tool_config != NULL)
72✔
236
            cmd = concat_arrays(cmd, (gchar*[]){"--configfile", create_certs_tool_config, NULL}, TRUE);
7✔
237
        if (create_certs_tool_options != NULL)
72✔
238
            cmd = concat_arrays(cmd, (gchar*[]){"--optsfile", create_certs_tool_options, NULL}, TRUE);
7✔
239

240
        s = g_strrstr(create_certs_tool, G_DIR_SEPARATOR_S);
72✔
241
        if (s)
72✔
242
            prgname = strdup(&s[1]);
7✔
243
        else
244
            prgname = strdup(create_certs_tool);
65✔
245

246
        for (idx = 0; flags_to_certfiles[idx].filename != NULL; idx++) {
216✔
247
            if (flags & flags_to_certfiles[idx].flag) {
144✔
248
                g_autofree gchar *standard_output = NULL;
111✔
249
                g_autofree gchar *standard_error = NULL;
111✔
250
                GError *error = NULL;
111✔
251
                gchar **lines;
111✔
252

253
                cmd[2] = (gchar *)flags_to_certfiles[idx].type; /* replaces the "_" above */
111✔
254

255
                s = g_strjoinv(" ", cmd);
111✔
256
                logit(gl_LOGFILE, "  Invoking %s\n", s);
111✔
257
                g_free(s);
111✔
258

259
                success = g_spawn_sync(NULL, cmd, NULL, 0, NULL, NULL,
111✔
260
                                       &standard_output, &standard_error, &exit_status, &error);
261
                if (!success) {
111✔
262
                    logerr(gl_LOGFILE, "An error occurred running %s: %s\n",
×
263
                           create_certs_tool, error->message);
×
264
                    g_error_free(error);
×
265
                    ret = 1;
×
266
                    break;
×
267
                } else if (exit_status != 0) {
111✔
268
                    logerr(gl_LOGFILE, "%s exit with status %d: %s\n",
×
269
                           prgname, WEXITSTATUS(exit_status), standard_error);
×
270
                    ret = 1;
×
271
                    break;
×
272
                }
273

274
                lines = g_strsplit(standard_output, "\n", -1);
111✔
275
                for (j = 0; lines[j] != NULL; j++) {
222✔
276
                    if (strlen(lines[j]) > 0)
×
277
                        logit(gl_LOGFILE, "%s: %s\n", prgname, lines[j]);
×
278
                }
279
                g_strfreev(lines);
111✔
280

281
                g_free(standard_output);
111✔
282
                standard_output = NULL;
111✔
283
                g_free(standard_error);
111✔
284
                standard_error = NULL;
111✔
285
            }
286
        }
287
    }
288

289
error:
×
290
    g_strfreev(config_file_lines);
72✔
291
    g_strfreev(params);
72✔
292

293
    return ret;
72✔
294
}
295

296
static char *create_ek_certfile_name(const gchar *user_certsdir, const gchar *key_description)
7✔
297
{
298
    g_autofree gchar *filename = g_strdup_printf("ek-%s.crt", key_description);
7✔
299

300
    return g_strjoin(G_DIR_SEPARATOR_S, user_certsdir, filename, NULL);
7✔
301
}
302

303
/*
304
 * Remove the cert file unless the user wants a copy of it (EK only).
305
 */
306
static int certfile_move_or_delete(unsigned long flags, gboolean is_ek, const gchar *certfile,
111✔
307
                                   const gchar *user_certsdir, const gchar *key_description)
308
{
309
    g_autofree gchar *content = NULL;
222✔
310
    g_autofree gchar *cf = NULL;
111✔
311
    gsize content_length;
111✔
312
    GError *error = NULL;
111✔
313
    size_t offset = 0;
111✔
314

315
    if (is_ek && (flags & SETUP_WRITE_EK_CERT_FILES_F) && user_certsdir != NULL) {
111✔
316
        if (!g_file_get_contents(certfile, &content, &content_length, &error))
7✔
317
            goto error;
×
318

319
        cf = create_ek_certfile_name(user_certsdir, key_description);
7✔
320
        if (!(flags & SETUP_TPM2_F)) {
7✔
321
            /* A TPM 1.2 certificate has a 7 byte header at the beginning
322
             * that we now remove */
323
            if (content_length >= 8)
1✔
324
                offset = 7;
1✔
325
        }
326
        if (!g_file_set_contents(cf, &content[offset], content_length - offset,
7✔
327
                                 &error))
328
            goto error;
×
329
        if (g_chmod(cf, S_IRUSR | S_IWUSR | S_IRGRP) < 0) {
7✔
330
            logerr(gl_LOGFILE, "Failed to chmod file '%s': %s\n", cf, strerror(errno));
×
331
            goto error_unlink;
×
332
        }
333
    }
334
    unlink(certfile);
111✔
335

336
    return 0;
111✔
337

338
error:
×
339
    logerr(gl_LOGFILE, "%s\n", error->message);
×
340
    g_error_free(error);
×
341

342
error_unlink:
×
343
    unlink(certfile);
×
344

345
    return 1;
×
346
}
347

348
/* Create EK and certificate for a TPM 2 */
349
static int tpm2_create_ek_and_cert(unsigned long flags, const gchar *config_file,
80✔
350
                                   const gchar *certsdir, const gchar *vmid,
351
                                   unsigned int rsa_keysize, struct swtpm2 *swtpm2,
352
                                   const gchar *user_certsdir)
353
{
354
    g_autofree gchar *filecontent = NULL;
160✔
355
    size_t filecontent_len;
80✔
356
    g_autofree gchar *certfile = NULL;
80✔
357
    g_autofree gchar *ekparam = NULL;
80✔
358
    const char *key_description = "";
80✔
359
    size_t idx;
80✔
360
    int ret;
80✔
361

362
    if (flags & SETUP_CREATE_EK_F) {
80✔
363
        ret = swtpm2->ops->create_ek(&swtpm2->swtpm, !!(flags & SETUP_TPM2_ECC_F), rsa_keysize,
152✔
364
                                     !!(flags & SETUP_ALLOW_SIGNING_F),
76✔
365
                                     !!(flags & SETUP_DECRYPTION_F),
76✔
366
                                     !!(flags & SETUP_LOCK_NVRAM_F),
76✔
367
                                     &ekparam, &key_description);
368
        if (ret != 0)
76✔
369
            return 1;
370
    }
371

372
    if (flags & (SETUP_EK_CERT_F | SETUP_PLATFORM_CERT_F)) {
80✔
373
        ret = call_create_certs(flags, config_file, certsdir, ekparam, vmid, &swtpm2->swtpm);
64✔
374
        if (ret != 0)
64✔
375
            return 1;
376

377
        for (idx = 0; flags_to_certfiles[idx].filename; idx++) {
192✔
378
            if (flags & flags_to_certfiles[idx].flag) {
128✔
379
                g_free(certfile);
96✔
380
                certfile = g_strjoin(G_DIR_SEPARATOR_S, certsdir, flags_to_certfiles[idx].filename, NULL);
96✔
381

382
                g_free(filecontent);
96✔
383
                filecontent = NULL;
96✔
384
                ret = read_file(certfile, &filecontent, &filecontent_len);
96✔
385
                if (ret != 0)
96✔
386
                    return 1;
×
387

388
                if (flags_to_certfiles[idx].flag == SETUP_EK_CERT_F) {
96✔
389
                    ret = swtpm2->ops->write_ek_cert_nvram(&swtpm2->swtpm,
64✔
390
                                                   !!(flags & SETUP_TPM2_ECC_F), rsa_keysize,
64✔
391
                                                   !!(flags & SETUP_LOCK_NVRAM_F),
64✔
392
                                                   (const unsigned char*)filecontent, filecontent_len);
393
                } else {
394
                    ret = swtpm2->ops->write_platform_cert_nvram(&swtpm2->swtpm,
32✔
395
                                                     !!(flags & SETUP_LOCK_NVRAM_F),
32✔
396
                                                     (const unsigned char *)filecontent, filecontent_len);
397
                }
398

399
                if (ret != 0) {
96✔
400
                    unlink(certfile);
×
401
                    return 1;
×
402
                }
403

404
                if (certfile_move_or_delete(flags, !!(flags_to_certfiles[idx].flag & SETUP_EK_CERT_F),
96✔
405
                                            certfile, user_certsdir, key_description) != 0)
406
                    return 1;
×
407
            }
408
        }
409
    }
410

411
    return 0;
412
}
413

414
/* Create endorsement keys and certificates for a TPM 2 */
415
static int tpm2_create_eks_and_certs(unsigned long flags, const gchar *config_file,
40✔
416
                                     const gchar *certsdir, const gchar *vmid,
417
                                     unsigned int rsa_keysize, struct swtpm2 *swtpm2,
418
                                     const gchar *user_certsdir)
419
{
420
     int ret;
40✔
421

422
     /* 1st key will be RSA */
423
     flags = flags & ~SETUP_TPM2_ECC_F;
40✔
424
     ret = tpm2_create_ek_and_cert(flags, config_file, certsdir, vmid, rsa_keysize, swtpm2,
40✔
425
                                   user_certsdir);
426
     if (ret != 0)
40✔
427
         return 1;
428

429
     /* 2nd key will be an ECC; no more platform cert */
430
     flags = (flags & ~SETUP_PLATFORM_CERT_F) | SETUP_TPM2_ECC_F;
40✔
431
     return tpm2_create_ek_and_cert(flags, config_file, certsdir, vmid, rsa_keysize, swtpm2,
40✔
432
                                    user_certsdir);
433
}
434

435
/* Get the default PCR banks from the config file and if nothing can
436
   be found there use the DEFAULT_PCR_BANKS #define.
437
 */
438
static gchar *get_default_pcr_banks(const gchar *config_file)
66✔
439
{
440
    g_auto(GStrv) config_file_lines = NULL;
132✔
441
    gchar *pcr_banks;
66✔
442
    int ret;
66✔
443

444
    ret = read_file_lines(config_file, &config_file_lines);
66✔
445
    if (ret != 0)
66✔
446
        return NULL;
447

448
    pcr_banks = get_config_value(config_file_lines, "active_pcr_banks");
66✔
449
    if (pcr_banks)
66✔
450
        g_strstrip(pcr_banks);
26✔
451
    if (pcr_banks == NULL || strlen(pcr_banks) == 0) {
66✔
452
        g_free(pcr_banks);
40✔
453
        pcr_banks = g_strdup(DEFAULT_PCR_BANKS);
40✔
454
    }
455
    return pcr_banks;
456
}
457

458
/* Activate the given list of PCR banks. If pcr_banks is '-' then leave
459
 * the configuration as-is.
460
 */
461
static int tpm2_activate_pcr_banks(struct swtpm2 *swtpm2,
46✔
462
                                   const gchar *pcr_banks)
463
{
464
    g_autofree gchar *active_pcr_banks_join = NULL;
92✔
465
    g_autofree gchar *all_pcr_banks_join = NULL;
46✔
466
    g_auto(GStrv) active_pcr_banks = NULL;
46✔
467
    g_auto(GStrv) all_pcr_banks = NULL;
46✔
468
    g_auto(GStrv) pcr_banks_l = NULL;
46✔
469
    struct swtpm *swtpm = &swtpm2->swtpm;
46✔
470
    int ret = 0;
46✔
471

472
    if (g_str_equal(pcr_banks, "-"))
46✔
473
        return 0;
474

475
    ret = swtpm2->ops->get_all_pcr_banks(swtpm, &all_pcr_banks);
46✔
476
    if (ret != 0)
46✔
477
        return ret;
478

479
    pcr_banks_l = g_strsplit(pcr_banks, ",", -1);
46✔
480
    ret = swtpm2->ops->set_active_pcr_banks(swtpm, pcr_banks_l, all_pcr_banks,
46✔
481
                                            &active_pcr_banks);
482
    if (ret != 0)
46✔
483
        return ret;
484

485
    active_pcr_banks_join = g_strjoinv(",", active_pcr_banks);
46✔
486
    all_pcr_banks_join = g_strjoinv(",", all_pcr_banks);
46✔
487
    logit(gl_LOGFILE, "Successfully activated PCR banks %s among %s.\n",
46✔
488
          active_pcr_banks_join, all_pcr_banks_join);
489

490
    return 0;
46✔
491
}
492

493
/* Simulate manufacturing a TPM 2: create keys and certificates */
494
static int init_tpm2(unsigned long flags, gchar **swtpm_prg_l, const gchar *config_file,
46✔
495
                     const gchar *tpm2_state_path, const gchar *vmid, const gchar *pcr_banks,
496
                     const gchar *swtpm_keyopt, int *fds_to_pass, size_t n_fds_to_pass,
497
                     unsigned int rsa_keysize, const gchar *certsdir,
498
                     const gchar *user_certsdir)
499
{
500
    struct swtpm2 *swtpm2;
46✔
501
    struct swtpm *swtpm;
46✔
502
    int ret;
46✔
503

504
    swtpm2 = swtpm2_new(swtpm_prg_l, tpm2_state_path, swtpm_keyopt, gl_LOGFILE,
46✔
505
                        fds_to_pass, n_fds_to_pass);
506
    if (swtpm2 == NULL)
46✔
507
        return 1;
508
    swtpm = &swtpm2->swtpm;
46✔
509

510
    ret = swtpm->cops->start(swtpm);
46✔
511
    if (ret != 0) {
46✔
512
        logerr(gl_LOGFILE, "Could not start the TPM 2.\n");
×
513
        goto error;
×
514
    }
515

516
    if (!(flags & SETUP_RECONFIGURE_F)) {
46✔
517
        if ((flags & SETUP_CREATE_SPK_F)) {
40✔
518
            ret = swtpm2->ops->create_spk(swtpm, !!(flags & SETUP_TPM2_ECC_F), rsa_keysize);
3✔
519
            if (ret != 0)
3✔
520
                goto destroy;
×
521
        }
522

523
        ret = tpm2_create_eks_and_certs(flags, config_file, certsdir, vmid, rsa_keysize, swtpm2,
40✔
524
                                        user_certsdir);
525
        if (ret != 0)
40✔
526
            goto destroy;
×
527
    }
528

529
    ret = tpm2_activate_pcr_banks(swtpm2, pcr_banks);
46✔
530
    if (ret != 0)
46✔
531
        goto destroy;
×
532

533
    ret = swtpm2->ops->shutdown(swtpm);
46✔
534

535
destroy:
46✔
536
    swtpm->cops->destroy(swtpm);
46✔
537

538
error:
46✔
539
    swtpm_free(swtpm);
46✔
540

541
    return ret;
46✔
542
}
543

544
/* Create the owner password digest */
545
static void tpm12_get_ownerpass_digest(unsigned long flags, const gchar *ownerpass,
9✔
546
                                       unsigned char ownerpass_digest[SHA_DIGEST_LENGTH])
547
{
548
    const gchar zeros[SHA_DIGEST_LENGTH]= {0, };
9✔
549
    size_t len;
9✔
550

551
    if (ownerpass == NULL) {
9✔
552
        if (flags & SETUP_OWNERPASS_ZEROS_F) {
2✔
553
            ownerpass = zeros;
554
            len = sizeof(zeros);
555
        } else {
556
            ownerpass = DEFAULT_OWNER_PASSWORD;
×
557
            len = strlen(ownerpass);
×
558
        }
559
    } else {
560
        len = strlen(ownerpass);
7✔
561
    }
562
    SHA1((const unsigned char *)ownerpass, len, ownerpass_digest);
9✔
563
}
9✔
564

565
/* Create the SRK password digest */
566
static void tpm12_get_srkpass_digest(unsigned long flags, const gchar *srkpass,
9✔
567
                                     unsigned char srkpass_digest[SHA_DIGEST_LENGTH])
568
{
569
    const gchar zeros[SHA_DIGEST_LENGTH]= {0, };
9✔
570
    size_t len;
9✔
571

572
    if (srkpass == NULL) {
9✔
573
        if (flags & SETUP_SRKPASS_ZEROS_F) {
2✔
574
            srkpass = zeros;
575
            len = sizeof(zeros);
576
        } else {
577
            srkpass = DEFAULT_SRK_PASSWORD;
×
578
            len = strlen(srkpass);
×
579
        }
580
    } else {
581
        len = strlen(srkpass);
7✔
582
    }
583
    SHA1((const unsigned char *)srkpass, len, srkpass_digest);
9✔
584
}
9✔
585

586
/* Take ownership of a TPM 1.2 */
587
static int tpm12_take_ownership(unsigned long flags, const gchar *ownerpass,
9✔
588
                                const gchar *srkpass, gchar *pubek, size_t pubek_len,
589
                                struct swtpm12 *swtpm12)
590
{
591
    unsigned char ownerpass_digest[SHA_DIGEST_LENGTH];
9✔
592
    unsigned char srkpass_digest[SHA_DIGEST_LENGTH];
9✔
593

594
    tpm12_get_ownerpass_digest(flags, ownerpass, ownerpass_digest);
9✔
595
    tpm12_get_srkpass_digest(flags, srkpass, srkpass_digest);
9✔
596

597
    return swtpm12->ops->take_ownership(&swtpm12->swtpm, ownerpass_digest, srkpass_digest,
9✔
598
                                        (const unsigned char *)pubek, pubek_len);
599
}
600

601
/* Create the certificates for a TPM 1.2 */
602
static int tpm12_create_certs(unsigned long flags, const gchar *config_file,
8✔
603
                              const gchar *certsdir, const gchar *ekparam,
604
                              const gchar *vmid, struct swtpm12 *swtpm12,
605
                              const gchar *user_certsdir)
606
{
607
    g_autofree gchar *filecontent = NULL;
16✔
608
    g_autofree gchar *certfile = NULL;
8✔
609
    gsize filecontent_len;
8✔
610
    size_t idx;
8✔
611
    int ret;
8✔
612

613
    ret = call_create_certs(flags, config_file, certsdir, ekparam, vmid, &swtpm12->swtpm);
8✔
614
    if (ret != 0)
8✔
615
        return 1;
616

617
    for (idx = 0; flags_to_certfiles[idx].filename; idx++) {
24✔
618
        if (flags & flags_to_certfiles[idx].flag) {
16✔
619
            g_free(certfile);
15✔
620
            certfile = g_strjoin(G_DIR_SEPARATOR_S, certsdir,
15✔
621
                                 flags_to_certfiles[idx].filename, NULL);
622

623
            g_free(filecontent);
15✔
624
            filecontent = NULL;
15✔
625
            ret = read_file(certfile, &filecontent, &filecontent_len);
15✔
626
            if (ret != 0)
15✔
627
                return 1;
×
628

629
            if (flags_to_certfiles[idx].flag == SETUP_EK_CERT_F) {
15✔
630
                ret = swtpm12->ops->write_ek_cert_nvram(&swtpm12->swtpm,
8✔
631
                                                (const unsigned char*)filecontent, filecontent_len);
632
                if (ret == 0)
8✔
633
                    logit(gl_LOGFILE, "Successfully created NVRAM area for EK certificate.\n");
8✔
634
            } else {
635
                ret = swtpm12->ops->write_platform_cert_nvram(&swtpm12->swtpm,
7✔
636
                                                  (const unsigned char*)filecontent, filecontent_len);
637
                if (ret == 0)
7✔
638
                    logit(gl_LOGFILE, "Successfully created NVRAM area for Platform certificate.\n");
7✔
639
            }
640

641
            if (ret != 0) {
15✔
642
                unlink(certfile);
×
643
                return 1;
×
644
            }
645

646
            if (certfile_move_or_delete(flags, !!(flags_to_certfiles[idx].flag & SETUP_EK_CERT_F),
15✔
647
                                        certfile, user_certsdir, "rsa2048") != 0)
648
                return 1;
×
649
        }
650
    }
651

652
    return 0;
653
}
654

655
/* Simulate manufacturing a TPM 1.2: create keys and certificate and possibly take ownership */
656
static int init_tpm(unsigned long flags, gchar **swtpm_prg_l, const gchar *config_file,
26✔
657
                    const gchar *tpm_state_path, const gchar *ownerpass, const gchar *srkpass,
658
                    const gchar *vmid, const gchar *swtpm_keyopt,
659
                    int *fds_to_pass, size_t n_fds_to_pass, const gchar *certsdir,
660
                    const gchar *user_certsdir)
661
{
662
    struct swtpm12 *swtpm12;
26✔
663
    struct swtpm *swtpm;
26✔
664
    g_autofree gchar *pubek = NULL;
52✔
665
    size_t pubek_len = 0;
26✔
666
    int ret = 1;
26✔
667

668
    swtpm12 = swtpm12_new(swtpm_prg_l, tpm_state_path, swtpm_keyopt, gl_LOGFILE,
26✔
669
                          fds_to_pass, n_fds_to_pass);
670
    if (swtpm12 == NULL)
26✔
671
        return 1;
672
    swtpm = &swtpm12->swtpm;
26✔
673

674
    ret = swtpm->cops->start(swtpm);
26✔
675
    if (ret != 0) {
26✔
676
        logerr(gl_LOGFILE, "Could not start the TPM 1.2.\n");
×
677
        goto error;
×
678
    }
679

680
    ret = swtpm12->ops->run_swtpm_bios(swtpm);
26✔
681
    if (ret != 0)
26✔
682
         goto destroy;
×
683

684
    if ((flags & SETUP_CREATE_EK_F)) {
26✔
685
        ret = swtpm12->ops->create_endorsement_key_pair(swtpm, &pubek, &pubek_len);
19✔
686
        if (ret != 0)
19✔
687
            goto destroy;
×
688

689
        logit(gl_LOGFILE, "Successfully created EK.\n");
19✔
690

691
        /* can only take owernship if created an EK */
692
        if ((flags & SETUP_TAKEOWN_F)) {
19✔
693
            ret = tpm12_take_ownership(flags, ownerpass, srkpass, pubek, pubek_len, swtpm12);
9✔
694
            if (ret != 0)
9✔
695
                goto destroy;
×
696

697
            logit(gl_LOGFILE, "Successfully took ownership of the TPM.\n");
9✔
698
        }
699

700
        /* can only create EK cert if created an EK */
701
        if ((flags & SETUP_EK_CERT_F)) {
19✔
702
            g_autofree gchar *ekparam = print_as_hex((unsigned char *)pubek, pubek_len);
16✔
703

704
            ret = tpm12_create_certs(flags, config_file, certsdir, ekparam, vmid, swtpm12,
8✔
705
                                     user_certsdir);
706
            if (ret != 0)
8✔
707
                goto destroy;
×
708
        }
709
    }
710

711
    if ((flags & SETUP_LOCK_NVRAM_F)) {
26✔
712
        ret = swtpm12->ops->nv_lock(swtpm);
12✔
713
        if (ret == 0)
12✔
714
            logit(gl_LOGFILE, "Successfully locked NVRAM access.\n");
12✔
715
    }
716

717
destroy:
14✔
718
    swtpm->cops->destroy(swtpm);
26✔
719

720
error:
26✔
721
    swtpm_free(swtpm);
26✔
722

723
    return ret;
26✔
724
}
725

726
/* Check whether we are allowed to overwrite existing state.
727
 * This function returns 2 if the state exists but flag is set to not to overwrite it,
728
 * 0 in case we can overwrite it, 1 if the state exists.
729
 */
730
static int check_state_overwrite(gchar **swtpm_prg_l, unsigned int flags,
71✔
731
                                 const char *tpm_state_path)
732
{
733
    gboolean success;
71✔
734
    g_autofree gchar *standard_output = NULL;
142✔
735
    int exit_status = 0;
71✔
736
    g_autoptr(GError) error = NULL;
71✔
737
    g_autofree gchar **argv = NULL;
71✔
738
    g_autofree gchar *statearg = g_strdup_printf("backend-uri=%s", tpm_state_path);
142✔
739
    g_autofree gchar *logop = NULL;
71✔
740
    g_autofree gchar **my_argv = NULL;
71✔
741

742
    my_argv = concat_arrays((gchar*[]) {
71✔
743
                                "--print-states",
744
                                "--tpmstate",
745
                                statearg,
746
                                NULL
747
                            }, NULL, FALSE);
748

749
    if (flags & SETUP_TPM2_F)
71✔
750
        my_argv = concat_arrays(my_argv, (gchar*[]) { "--tpm2", NULL }, TRUE);
42✔
751

752
    if (gl_LOGFILE != NULL) {
71✔
753
        logop = g_strdup_printf("file=%s", gl_LOGFILE);
13✔
754
        my_argv = concat_arrays(my_argv, (gchar*[]){"--log", logop, NULL}, TRUE);
13✔
755
    }
756

757
    argv = concat_arrays(swtpm_prg_l, my_argv, FALSE);
71✔
758

759
    success = g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
71✔
760
                           &standard_output, NULL, &exit_status, &error);
761
    if (!success) {
71✔
762
        logerr(gl_LOGFILE, "Could not start swtpm '%s': %s\n", swtpm_prg_l[0], error->message);
×
763
        return 1;
×
764
    }
765

766
    if (exit_status != 0) {
71✔
767
        logerr(gl_LOGFILE, "%s exit with status %d: %s\n",
×
768
               swtpm_prg_l[0], exit_status, standard_output);
769
        return 1;
×
770
    }
771

772
    if (g_strstr_len(standard_output, -1, TPM_PERMANENT_ALL_NAME) != NULL) {
71✔
773
        /* State file exists */
774
        if (flags & SETUP_STATE_NOT_OVERWRITE_F) {
10✔
775
            logit(gl_LOGFILE, "Not overwriting existing state file.\n");
2✔
776
            return 2;
2✔
777
        }
778
        if (flags & SETUP_STATE_OVERWRITE_F)
8✔
779
            return 0;
780
        logerr(gl_LOGFILE, "Found existing TPM state '%s'.\n", TPM_PERMANENT_ALL_NAME);
3✔
781
        return 1;
3✔
782
    }
783

784
    return 0;
785
}
786

787
static void versioninfo(void)
788
{
789
    printf("TPM emulator setup tool version %d.%d.%d\n",
×
790
           SWTPM_VER_MAJOR, SWTPM_VER_MINOR, SWTPM_VER_MICRO);
791
}
792

793
static void usage(const char *prgname, const char *default_config_file)
794
{
795
    versioninfo();
×
796
    printf(
×
797
        "Usage: %s [options]\n"
798
        "\n"
799
        "The following options are supported:\n"
800
        "\n"
801
        "--runas <user>   : Run this program under the given user's account.\n"
802
        "\n"
803
        "--tpm-state <dir>: Path where the TPM's state will be written to;\n"
804
        "                   this is a mandatory argument. Prefix with dir:// to\n"
805
        "                   use directory backend, or file:// to use linear file.\n"
806
        "\n"
807
        "--tpmstate <dir> : This is an alias for --tpm-state <dir>.\n"
808
        "\n"
809
        "--tpm <executable>\n"
810
        "                 : Path to the TPM executable; this is an optional argument and\n"
811
        "                   by default 'swtpm' in the PATH is used.\n"
812
        "\n"
813
        "--swtpm_ioctl <executable>\n"
814
        "                 : Path to the swtpm_ioctl executable; this is deprecated\n"
815
        "                   argument.\n"
816
        "\n"
817
        "--tpm2           : Setup a TPM 2; by default a TPM 1.2 is setup.\n"
818
        "\n"
819
        "--createek       : Create the EK; for a TPM 2 an RSA and ECC EK will be\n"
820
        "                   created\n"
821
        "\n"
822
        "--allow-signing  : Create an EK that can be used for signing;\n"
823
        "                   this option requires --tpm2.\n"
824
        "                   Note: Careful, this option will create a non-standard EK!\n"
825
        "\n"
826
        "--decryption     : Create an EK that can be used for key encipherment;\n"
827
        "                   this is the default unless --allow-signing is given;\n"
828
        "                   this option requires --tpm2.\n"
829
        "\n"
830
        "--ecc            : This option allows to create a TPM 2's ECC key as storage\n"
831
        "                   primary key; a TPM 2 always gets an RSA and an ECC EK key.\n"
832
        "\n"
833
        "--take-ownership : Take ownership; this option implies --createek\n"
834
        "  --ownerpass  <password>\n"
835
        "                 : Provide custom owner password; default is %s\n"
836
        "  --owner-well-known:\n"
837
        "                 : Use an owner password of 20 zero bytes\n"
838
        "  --srkpass <password>\n"
839
        "                 : Provide custom SRK password; default is %s\n"
840
        "  --srk-well-known:\n"
841
        "                 : Use an SRK password of 20 zero bytes\n"
842
        "--create-ek-cert : Create an EK certificate; this implies --createek\n"
843
        "\n"
844
        "--create-platform-cert\n"
845
        "                 : Create a platform certificate; this implies --create-ek-cert\n"
846
        "\n"
847
        "--create-spk     : Create storage primary key; this requires --tpm2\n"
848
        "\n"
849
        "--lock-nvram     : Lock NVRAM access\n"
850
        "\n"
851
        "--display        : At the end display as much info as possible about the\n"
852
        "                   configuration of the TPM\n"
853
        "\n"
854
        "--config <config file>\n"
855
        "                 : Path to configuration file; default is %s\n"
856
        "\n"
857
        "--logfile <logfile>\n"
858
        "                 : Path to log file; default is logging to stderr\n"
859
        "\n"
860
        "--keyfile <keyfile>\n"
861
        "                 : Path to a key file containing the encryption key for the\n"
862
        "                   TPM to encrypt its persistent state with. The content\n"
863
        "                   must be a 32 hex digit number representing a 128bit AES key.\n"
864
        "                   This parameter will be passed to the TPM using\n"
865
        "                   '--key file=<file>'.\n"
866
        "\n"
867
        "--keyfile-fd <fd>: Like --keyfile but a file descriptor is given to read the\n"
868
        "                   encryption key from.\n"
869
        "\n"
870
        "--pwdfile <pwdfile>\n"
871
        "                 : Path to a file containing a passphrase from which the\n"
872
        "                   TPM will derive the 128bit AES key. The passphrase can be\n"
873
        "                   32 bytes long.\n"
874
        "                   This parameter will be passed to the TPM using\n"
875
        "                   '--key pwdfile=<file>'.\n"
876
        "\n"
877
        "--pwdfile-fd <fd>: Like --pwdfile but a file descriptor is given to to read\n"
878
        "                   the passphrase from.\n"
879
        "\n"
880
        "--cipher <cipher>: The cipher to use; either aes-128-cbc or aes-256-cbc;\n"
881
        "                   the default is aes-128-cbc; the same cipher must be\n"
882
        "                   used on the swtpm command line\n"
883
        "\n"
884
        "--overwrite      : Overwrite existing TPM state by re-initializing it; if this\n"
885
        "                   option is not given, this program will return an error if\n"
886
        "                   existing state is detected\n"
887
        "\n"
888
        "--not-overwrite  : Do not overwrite existing TPM state but silently end\n"
889
        "\n"
890
        "--vmid <vm id>   : Unique (VM) identifier to use as common name in certificate\n"
891
        "\n"
892
        "--pcr-banks <banks>\n"
893
        "                 : Set of PCR banks to activate. Provide a comma separated list\n"
894
        "                   like 'sha1,sha256'. '-' to skip and leave all banks active.\n"
895
        "                   Default: %s\n"
896
        "\n"
897
        "--rsa-keysize <keysize>\n"
898
        "                 : The RSA key size of the EK key; 3072 bits may be supported\n"
899
        "                   if libtpms supports it.\n"
900
        "                   Default: %u\n"
901
        "\n"
902
        "--write-ek-cert-files <directory>\n"
903
        "                 : Write EK cert files into the given directory\n"
904
        "\n"
905
        "--tcsd-system-ps-file <file>\n"
906
        "                 : This option is deprecated and has no effect.\n"
907
        "\n"
908
        "--print-capabilities\n"
909
        "                 : Print JSON formatted capabilites added after v0.1 and exit.\n"
910
        "\n"
911
        "--create-config-files [[overwrite][,root]]\n"
912
        "                 : Create swtpm_setup and swtpm-localca config files for a\n"
913
        "                   user account.\n"
914
        "                   overwrite: overwrite any existing files\n"
915
        "                   root: allow to create files under root's home directory\n"
916
        "                   skip-if-exist: if any file exists exit without error\n"
917
        "\n"
918
        "--reconfigure    : Reconfigure an existing swtpm by reusing existing state.\n"
919
        "                   The active PCR banks can be changed but no new keys will\n"
920
        "                   be created.\n"
921
        "\n"
922
        "--version        : Display version and exit\n"
923
        "\n"
924
        "--help,-h        : Display this help screen\n\n",
925
            prgname,
926
            DEFAULT_OWNER_PASSWORD,
927
            DEFAULT_SRK_PASSWORD,
928
            default_config_file,
929
            DEFAULT_PCR_BANKS,
930
            DEFAULT_RSA_KEYSIZE
931
        );
932
}
933

934
static int get_swtpm_capabilities(gchar **swtpm_prg_l, gboolean is_tpm2,
133✔
935
                                  gchar **standard_output)
936
{
937
    gchar *my_argv[] = { "--print-capabilities", is_tpm2 ? "--tpm2" : NULL, NULL };
133✔
938
    g_autofree gchar *logop = NULL;
266✔
939
    g_autoptr(GError) error = NULL;
133✔
940
    g_autofree gchar **argv = NULL;
133✔
941
    int exit_status = 0;
133✔
942
    gboolean success;
133✔
943
    int ret = 1;
133✔
944

945
    argv = concat_arrays(swtpm_prg_l, my_argv, FALSE);
133✔
946

947
    if (gl_LOGFILE != NULL) {
133✔
948
        logop = g_strdup_printf("file=%s", gl_LOGFILE);
29✔
949
        argv = concat_arrays(argv, (gchar*[]){"--log", logop, NULL}, TRUE);
29✔
950
    }
951

952
    success = g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL,
133✔
953
                           standard_output, NULL, &exit_status, &error);
954
    if (!success) {
133✔
955
        logerr(gl_LOGFILE, "Could not start swtpm '%s': %s\n", swtpm_prg_l[0], error->message);
×
956
        goto error;
×
957
    }
958
    ret = 0;
959

960
error:
133✔
961
    return ret;
133✔
962
}
963

964
static int get_supported_tpm_versions(gchar **swtpm_prg_l, gboolean *swtpm_has_tpm12,
82✔
965
                                      gboolean *swtpm_has_tpm2)
966
{
967
    g_autofree gchar *standard_output = NULL;
164✔
968
    int ret;
82✔
969

970
    ret = get_swtpm_capabilities(swtpm_prg_l, FALSE, &standard_output);
82✔
971
    if (ret)
82✔
972
        return ret;
973

974
    *swtpm_has_tpm12 = g_strstr_len(standard_output, -1, "\"tpm-1.2\"") != NULL;
82✔
975
    *swtpm_has_tpm2 = g_strstr_len(standard_output, -1, "\"tpm-2.0\"") != NULL;
82✔
976

977
    return 0;
82✔
978
}
979

980
/* Get the support RSA key sizes.
981
 *  This function returns an array of ints like the following
982
 *  - [ 1024, 2048, 3072 ]
983
 *  - [] (empty array, indicating only 2048 bit RSA keys are supported)
984
 */
985
static int get_rsa_keysizes(unsigned long flags, gchar **swtpm_prg_l,
77✔
986
                            unsigned int **keysizes, size_t *n_keysizes)
987
{
988
    g_autofree gchar *standard_output = NULL;
154✔
989
    const gchar *needle = "\"rsa-keysize-";
77✔
990
    unsigned int keysize;
77✔
991
    int ret = 1;
77✔
992
    char *p;
77✔
993
    int n;
77✔
994

995
    *n_keysizes = 0;
77✔
996

997
    if (flags & SETUP_TPM2_F) {
77✔
998
        ret = get_swtpm_capabilities(swtpm_prg_l, TRUE, &standard_output);
51✔
999
        if (ret)
51✔
1000
            goto error;
×
1001

1002
        p = standard_output;
51✔
1003
        /* A crude way of parsing the json output just looking for "rsa-keysize-%u" */
1004
        while ((p = g_strstr_len(p, -1, needle)) != NULL) {
255✔
1005
            p += strlen(needle);
204✔
1006
            n = sscanf(p, "%u\"", &keysize);
204✔
1007
            if (n == 1) {
204✔
1008
                *keysizes = g_realloc(*keysizes, (*n_keysizes + 1) * sizeof(unsigned int));
204✔
1009
                (*keysizes)[*n_keysizes] = keysize;
204✔
1010
                (*n_keysizes)++;
204✔
1011
            }
1012
        }
1013
    }
1014
    ret = 0;
1015

1016
error:
77✔
1017
    return ret;
77✔
1018
}
1019

1020
/* Return the RSA key size capabilities in a NULL-terminated array */
1021
static int get_rsa_keysize_caps(unsigned long flags, gchar **swtpm_prg_l,
5✔
1022
                                gchar ***keysize_strs)
1023
{
1024
    unsigned int *keysizes = NULL;
5✔
1025
    size_t n_keysizes = 0;
5✔
1026
    size_t i, j;
5✔
1027
    int ret = get_rsa_keysizes(flags, swtpm_prg_l, &keysizes, &n_keysizes);
5✔
1028
    if (ret)
5✔
1029
        return ret;
1030

1031
    *keysize_strs = g_malloc0(sizeof(char *) * (n_keysizes + 1));
5✔
1032
    for (i = 0, j = 0; i < n_keysizes; i++) {
25✔
1033
        if (keysizes[i] >= 2048)
20✔
1034
            (*keysize_strs)[j++] = g_strdup_printf("tpm2-rsa-keysize-%u", keysizes[i]);
15✔
1035
    }
1036

1037
    g_free(keysizes);
5✔
1038

1039
    return 0;
5✔
1040
}
1041

1042
/* Print teh JSON object of swtpm_setup's capabilities */
1043
static int print_capabilities(char **swtpm_prg_l, gboolean swtpm_has_tpm12,
5✔
1044
                              gboolean swtpm_has_tpm2)
1045
{
1046
    g_autofree gchar *param = g_strdup("");
5✔
1047
    gchar **keysize_strs = NULL;
5✔
1048
    gchar *tmp;
5✔
1049
    size_t i;
5✔
1050
    int ret = 0;
5✔
1051

1052
    ret = get_rsa_keysize_caps(SETUP_TPM2_F, swtpm_prg_l, &keysize_strs);
5✔
1053
    if (ret)
5✔
1054
        return 1;
1055

1056
    for (i = 0; keysize_strs[i] != NULL; i++) {
20✔
1057
        tmp = g_strdup_printf("%s, \"%s\"", param, keysize_strs[i]);
15✔
1058
        g_free(param);
15✔
1059
        param = tmp;
15✔
1060
    }
1061

1062
    printf("{ \"type\": \"swtpm_setup\", "
5✔
1063
           "\"features\": [ %s%s\"cmdarg-keyfile-fd\", \"cmdarg-pwdfile-fd\", \"tpm12-not-need-root\""
1064
           ", \"cmdarg-write-ek-cert-files\", \"cmdarg-create-config-files\""
1065
           ", \"cmdarg-reconfigure-pcr-banks\""
1066
           "%s ], "
1067
           "\"version\": \"" VERSION "\" "
1068
           "}\n",
1069
           swtpm_has_tpm12 ? "\"tpm-1.2\", " : "",
1070
           swtpm_has_tpm2  ? "\"tpm-2.0\", " : "",
1071
           param);
1072

1073
    g_strfreev(keysize_strs);
5✔
1074

1075
    return 0;
5✔
1076
}
1077

1078
static int change_process_owner(const char *user)
1079
{
1080
    char *endptr;
×
1081
    unsigned long long uid = strtoull(user, &endptr, 10);
×
1082
    gid_t gid;
×
1083
    struct passwd *passwd;
×
1084
    int ret = 1;
×
1085

1086
    if (*endptr != '\0') {
×
1087
        /* assuming a name */
1088
        passwd = getpwnam(user);
×
1089
        if (passwd == NULL) {
×
1090
            logerr(gl_LOGFILE, "Error: User '%s' does not exist.\n", user);
×
1091
            goto error;
×
1092
        }
1093

1094
        if (initgroups(passwd->pw_name, passwd->pw_gid) != 0) {
×
1095
            logerr(gl_LOGFILE, "Error: initgroups() failed: %s\n", strerror(errno));
×
1096
            goto error;
×
1097
        }
1098

1099
        gid = passwd->pw_gid;
×
1100
        uid = passwd->pw_uid;
×
1101
    } else {
1102
        if (uid > 0xffffffff) {
×
1103
            logerr(gl_LOGFILE, "Error: uid %s outside valid range.\n", user);
×
1104
            goto error;
×
1105
        }
1106
        gid = (gid_t)uid;
×
1107
    }
1108

1109
    if (setgid(gid) != 0) {
×
1110
        logerr(gl_LOGFILE, "Error: setgid(%d) failed: %s\n", gid, strerror(errno));
×
1111
        goto error;
×
1112
    }
1113

1114
    if (setuid(uid) != 0) {
×
1115
        logerr(gl_LOGFILE, "Error: setuid(%d) failed: %s\n", uid, strerror(errno));
×
1116
        goto error;
×
1117
    }
1118

1119
    ret = 0;
1120

1121
error:
×
1122
    return ret;
×
1123
}
1124

1125
static int handle_create_config_files(const char *optarg)
1126
{
1127
    g_auto(GStrv) tokens = NULL;
×
1128
    gboolean overwrite = FALSE;
×
1129
    gboolean root_flag = FALSE;
×
1130
    gboolean skip_if_exist = FALSE;
×
1131

1132
    if (optarg) {
×
1133
        tokens = g_strsplit_set(optarg, ", ", -1);
×
1134
        overwrite = g_strv_contains((const gchar **)tokens, "overwrite");
×
1135
        root_flag = g_strv_contains((const gchar **)tokens, "root");
×
1136
        skip_if_exist = g_strv_contains((const gchar **)tokens, "skip-if-exist");
×
1137
        if (overwrite && skip_if_exist) {
×
1138
            fprintf(stderr, "Error: overwrite and skip-if-exist cannot both be used\n");
×
1139
            return 1;
×
1140
        }
1141
    }
1142

1143
    return create_config_files(overwrite, root_flag, skip_if_exist);
×
1144
}
1145

1146
int main(int argc, char *argv[])
83✔
1147
{
1148
    int opt, option_index = 0;
83✔
1149
    static const struct option long_options[] = {
83✔
1150
        {"tpm-state", required_argument, NULL, 't'},
1151
        {"tpmstate", required_argument, NULL, 't'}, /* alias for tpm-state */
1152
        {"tpm", required_argument, NULL, 'T'},
1153
        {"swtpm_ioctl", required_argument, NULL, '_'},
1154
        {"tpm2", no_argument, NULL, '2'},
1155
        {"ecc", no_argument, NULL, 'e'},
1156
        {"createek", no_argument, NULL, 'c'},
1157
        {"create-spk", no_argument, NULL, 'C'},
1158
        {"take-ownership", no_argument, NULL, 'o'},
1159
        {"ownerpass", required_argument, NULL, 'O'},
1160
        {"owner-well-known", no_argument, NULL, 'w'},
1161
        {"srkpass", required_argument, NULL, 'S'},
1162
        {"srk-well-known", no_argument, NULL, 's'},
1163
        {"create-ek-cert", no_argument, NULL, 'E'},
1164
        {"create-platform-cert", no_argument, NULL, 'P'},
1165
        {"lock-nvram", no_argument, NULL, 'L'},
1166
        {"display", no_argument, NULL, 'i'},
1167
        {"config", required_argument, NULL, 'f'},
1168
        {"vmid", required_argument, NULL, 'm'},
1169
        {"keyfile", required_argument, NULL, 'x'},
1170
        {"keyfile-fd", required_argument, NULL, 'X'},
1171
        {"pwdfile", required_argument, NULL, 'k'},
1172
        {"pwdfile-fd", required_argument, NULL, 'K'},
1173
        {"cipher", required_argument, NULL, 'p'},
1174
        {"runas", required_argument, NULL, 'r'},
1175
        {"logfile", required_argument, NULL, 'l'},
1176
        {"overwrite", no_argument, NULL, 'v'},
1177
        {"not-overwrite", no_argument, NULL, 'V'},
1178
        {"allow-signing", no_argument, NULL, 'a'},
1179
        {"decryption", no_argument, NULL, 'd'},
1180
        {"pcr-banks", required_argument, NULL, 'b'},
1181
        {"rsa-keysize", required_argument, NULL, 'A'},
1182
        {"write-ek-cert-files", required_argument, NULL, '3'},
1183
        {"create-config-files", optional_argument, NULL, 'u'},
1184
        {"tcsd-system-ps-file", required_argument, NULL, 'F'},
1185
        {"version", no_argument, NULL, '1'},
1186
        {"print-capabilities", no_argument, NULL, 'y'},
1187
        {"reconfigure", no_argument, NULL, 'R'},
1188
        {"help", no_argument, NULL, 'h'},
1189
        {NULL, 0, NULL, 0}
1190
    };
1191
    unsigned long flags = 0;
83✔
1192
    g_autofree gchar *swtpm_prg = NULL;
83✔
1193
    g_autofree gchar *tpm_state_path = NULL;
83✔
1194
    struct swtpm_backend_ops *backend_ops = &swtpm_backend_dir;
83✔
1195
    void *backend_state = NULL;
83✔
1196
    g_autofree gchar *config_file = NULL;
83✔
1197
    g_autofree gchar *ownerpass = NULL;
83✔
1198
    gboolean got_ownerpass = FALSE;
83✔
1199
    g_autofree gchar *srkpass = NULL;
83✔
1200
    gboolean got_srkpass = FALSE;
83✔
1201
    g_autofree gchar *vmid = NULL;
83✔
1202
    g_autofree gchar *pcr_banks = NULL;
83✔
1203
    gboolean printcapabilities = FALSE;
83✔
1204
    g_autofree gchar *keyfile = NULL;
83✔
1205
    long int keyfile_fd = -1;
83✔
1206
    g_autofree gchar *pwdfile = NULL;
83✔
1207
    long int pwdfile_fd = -1;
83✔
1208
    g_autofree gchar *cipher = g_strdup("aes-128-cbc");
166✔
1209
    g_autofree gchar *rsa_keysize_str = g_strdup_printf("%d", DEFAULT_RSA_KEYSIZE);
166✔
1210
    unsigned int rsa_keysize;
83✔
1211
    g_autofree gchar *swtpm_keyopt = NULL;
83✔
1212
    g_autofree gchar *runas = NULL;
83✔
1213
    g_autofree gchar *certsdir = NULL;
83✔
1214
    g_autofree gchar *user_certsdir = NULL;
83✔
1215
    gchar *tmp;
83✔
1216
    gchar **swtpm_prg_l = NULL;
83✔
1217
    gchar **tmp_l = NULL;
83✔
1218
    size_t i, n;
83✔
1219
    struct stat statbuf;
83✔
1220
    const struct passwd *curr_user;
83✔
1221
    struct group *curr_grp;
83✔
1222
    char *endptr;
83✔
1223
    gboolean swtpm_has_tpm12 = FALSE;
83✔
1224
    gboolean swtpm_has_tpm2 = FALSE;
83✔
1225
    int fds_to_pass[1] = { -1 };
83✔
1226
    unsigned n_fds_to_pass = 0;
83✔
1227
    char tmpbuffer[200];
83✔
1228
    time_t now;
83✔
1229
    struct tm *tm;
83✔
1230
    int ret = 1;
83✔
1231
    g_autoptr(GError) error = NULL;
83✔
1232

1233
    if (init(&config_file) < 0)
83✔
1234
        goto error;
×
1235

1236
    swtpm_prg = g_find_program_in_path("swtpm");
83✔
1237
    if (swtpm_prg) {
83✔
1238
        tmp = g_strconcat(swtpm_prg, " socket", NULL);
63✔
1239
        g_free(swtpm_prg);
63✔
1240
        swtpm_prg = tmp;
63✔
1241
    }
1242

1243
    while ((opt = getopt_long(argc, argv, "h?",
1,560✔
1244
                              long_options, &option_index)) != -1) {
780✔
1245
        switch (opt) {
697✔
1246
        case 't': /* --tpmstate, --tpm-state */
77✔
1247
            g_free(tpm_state_path);
77✔
1248
            if (strncmp(optarg, "dir://", 6) == 0) {
77✔
1249
                tpm_state_path = g_strdup(optarg);
×
1250
            } else if (strncmp(optarg, "file://", 7) == 0) {
77✔
1251
                tpm_state_path = g_strdup(optarg);
3✔
1252
                backend_ops = &swtpm_backend_file;
3✔
1253
            } else {
1254
                /* always prefix with dir:// so we can pass verbatim to swtpm */
1255
                tpm_state_path = g_strconcat("dir://", optarg, NULL);
74✔
1256
            }
1257
            break;
1258
        case 'T': /* --tpm */
77✔
1259
            g_free(swtpm_prg);
77✔
1260
            swtpm_prg = g_strdup(optarg);
77✔
1261
            break;
77✔
1262
        case '_': /* --swtpm_ioctl */
×
1263
            fprintf(stdout, "Warning: --swtpm_ioctl is deprecated and has no effect.");
×
1264
            break;
1265
        case '2': /* --tpm2 */
53✔
1266
            flags |= SETUP_TPM2_F;
53✔
1267
            break;
53✔
1268
        case 'e': /* --ecc */
15✔
1269
            flags |= SETUP_TPM2_ECC_F;
15✔
1270
            break;
15✔
1271
        case 'c': /* --createek */
44✔
1272
            flags |= SETUP_CREATE_EK_F;
44✔
1273
            break;
44✔
1274
        case 'C': /* --create-spk */
3✔
1275
            flags |= SETUP_CREATE_SPK_F;
3✔
1276
            break;
3✔
1277
        case 'o': /* --take-ownership */
9✔
1278
            flags |= SETUP_CREATE_EK_F | SETUP_TAKEOWN_F;
9✔
1279
            break;
9✔
1280
        case 'O': /* --ownerpass */
2✔
1281
            g_free(ownerpass);
2✔
1282
            ownerpass = g_strdup(optarg);
2✔
1283
            got_ownerpass = TRUE;
2✔
1284
            break;
2✔
1285
        case 'w': /* --owner-well-known */
2✔
1286
            flags |= SETUP_OWNERPASS_ZEROS_F;
2✔
1287
            got_ownerpass = TRUE;
2✔
1288
            break;
2✔
1289
        case 'S': /* --srk-pass */
2✔
1290
            g_free(srkpass);
2✔
1291
            srkpass = g_strdup(optarg);
2✔
1292
            got_srkpass = TRUE;
2✔
1293
            break;
2✔
1294
        case 's': /* --srk-well-known */
2✔
1295
            flags |= SETUP_SRKPASS_ZEROS_F;
2✔
1296
            got_srkpass = TRUE;
2✔
1297
            break;
2✔
1298
        case 'E': /* --create-ek-cert */
40✔
1299
            flags |= SETUP_CREATE_EK_F | SETUP_EK_CERT_F;
40✔
1300
            break;
40✔
1301
        case 'P': /* --create-platform-cert */
39✔
1302
            flags |= SETUP_CREATE_EK_F | SETUP_PLATFORM_CERT_F;
39✔
1303
            break;
39✔
1304
        case 'L': /* --lock-nvram */
15✔
1305
            flags |= SETUP_LOCK_NVRAM_F;
15✔
1306
            break;
15✔
1307
        case 'i': /* --display */
36✔
1308
            flags |= SETUP_DISPLAY_RESULTS_F;
36✔
1309
            break;
36✔
1310
        case 'f': /* --config */
114✔
1311
            g_free(config_file);
114✔
1312
            config_file = g_strdup(optarg);
114✔
1313
            break;
114✔
1314
        case 'm': /* --vmid */
36✔
1315
            g_free(vmid);
36✔
1316
            vmid = g_strdup(optarg);
36✔
1317
            break;
36✔
1318
        case 'x': /* --keyfile */
11✔
1319
            g_free(keyfile);
11✔
1320
            keyfile = g_strdup(optarg);
11✔
1321
            break;
11✔
1322
        case 'X': /* --pwdfile-fd' */
2✔
1323
            keyfile_fd = strtoull(optarg, &endptr, 10);
2✔
1324
            if (*endptr != '\0' && keyfile_fd >= INT_MAX) {
2✔
1325
                fprintf(stderr, "Invalid file descriptor '%s'\n", optarg);
×
1326
                goto error;
×
1327
            }
1328
            break;
1329
        case 'k': /* --pwdfile */
15✔
1330
            g_free(pwdfile);
15✔
1331
            pwdfile = g_strdup(optarg);
15✔
1332
            break;
15✔
1333
        case 'K': /* --pwdfile-fd' */
2✔
1334
            pwdfile_fd = strtoull(optarg, &endptr, 10);
2✔
1335
            if (*endptr != '\0' || pwdfile_fd >= INT_MAX) {
2✔
1336
                fprintf(stderr, "Invalid file descriptor '%s'\n", optarg);
×
1337
                goto error;
×
1338
            }
1339
            break;
1340
        case 'p': /* --cipher */
12✔
1341
            g_free(cipher);
12✔
1342
            cipher = g_strdup(optarg);
12✔
1343
            break;
12✔
1344
        case 'r': /* --runas */
×
1345
            g_free(runas);
×
1346
            runas = g_strdup(optarg);
×
1347
            break;
×
1348
        case 'l': /* --logfile */
19✔
1349
            g_free(gl_LOGFILE);
19✔
1350
            gl_LOGFILE = g_strdup(optarg);
19✔
1351
            break;
19✔
1352
        case 'v': /* --overwrite */
6✔
1353
            flags |= SETUP_STATE_OVERWRITE_F;
6✔
1354
            break;
6✔
1355
        case 'V': /* --not-overwrite */
3✔
1356
            flags |= SETUP_STATE_NOT_OVERWRITE_F;
3✔
1357
            break;
3✔
1358
        case 'a': /* --allow-signing */
21✔
1359
            flags |= SETUP_ALLOW_SIGNING_F;
21✔
1360
            break;
21✔
1361
        case 'd': /* --decryption */
6✔
1362
            flags |= SETUP_DECRYPTION_F;
6✔
1363
            break;
6✔
1364
        case 'b': /* --pcr-banks */
6✔
1365
            tmp = g_strconcat(pcr_banks ? pcr_banks: "",
18✔
1366
                              pcr_banks ? "," : "", g_strstrip(optarg), NULL);
1367
            g_free(pcr_banks);
6✔
1368
            pcr_banks = tmp;
6✔
1369
            break;
6✔
1370
        case 'A': /* --rsa-keysize */
12✔
1371
            g_free(rsa_keysize_str);
12✔
1372
            rsa_keysize_str = strdup(optarg);
12✔
1373
            break;
12✔
1374
        case '3': /* --write-ek-cert-files */
4✔
1375
            g_free(user_certsdir);
4✔
1376
            user_certsdir = g_strdup(optarg);
4✔
1377
            flags |= SETUP_WRITE_EK_CERT_FILES_F;
4✔
1378
            break;
4✔
1379
        case 'u':
×
1380
            if (optarg == NULL && optind < argc && argv[optind][0] != '0')
×
1381
                optarg = argv[optind++];
×
1382
            ret = handle_create_config_files(optarg);
×
1383
            goto out;
×
1384
        case 'F': /* --tcsd-system-ps-file */
1385
            printf("Warning: --tcsd-system-ps-file is deprecated and has no effect.");
780✔
1386
            break;
1387
        case '1': /* --version */
×
1388
            versioninfo();
×
1389
            ret = 0;
×
1390
            goto out;
×
1391
        case 'y': /* --print-capabilities */
1392
            printcapabilities = TRUE;
1393
            break;
1394
        case 'R': /* --reconfigure */
6✔
1395
            flags |= SETUP_RECONFIGURE_F;
6✔
1396
            break;
6✔
1397
        case '?':
×
1398
        case 'h': /* --help */
1399
            usage(argv[0], config_file);
×
1400
            if (opt == 'h')
×
1401
                ret = 0;
×
1402
            goto out;
×
1403
        default:
×
1404
            fprintf(stderr, "Unknown option code %d\n", opt);
×
1405
            usage(argv[0], config_file);
×
1406
            goto error;
×
1407
        }
1408
    }
1409

1410
    if (swtpm_prg == NULL) {
83✔
1411
        logerr(gl_LOGFILE,
1✔
1412
               "Default TPM 'swtpm' could not be found and was not provided using --tpm.\n");
1413
        goto error;
1✔
1414
    }
1415

1416
    swtpm_prg_l = split_cmdline(swtpm_prg);
82✔
1417
    tmp = g_find_program_in_path(swtpm_prg_l[0]);
82✔
1418
    if (!tmp) {
82✔
1419
        logerr(gl_LOGFILE, "swtpm at %s is not an executable.\n", swtpm_prg_l[0]);
×
1420
        goto error;
×
1421
    }
1422
    g_free(tmp);
82✔
1423

1424

1425
    ret = get_supported_tpm_versions(swtpm_prg_l, &swtpm_has_tpm12, &swtpm_has_tpm2);
82✔
1426
    if (ret != 0)
82✔
1427
        goto error;
×
1428

1429
    if (printcapabilities) {
82✔
1430
        ret = print_capabilities(swtpm_prg_l, swtpm_has_tpm12, swtpm_has_tpm2);
5✔
1431
        goto out;
5✔
1432
    }
1433

1434
    if ((flags & SETUP_TPM2_F) != 0 && !swtpm_has_tpm2) {
77✔
1435
        logerr(gl_LOGFILE, "swtpm at %s does not support TPM 2\n", swtpm_prg_l[0]);
×
1436
        goto error;
×
1437
    } else if ((flags & SETUP_TPM2_F) == 0 && !swtpm_has_tpm12){
77✔
1438
        logerr(gl_LOGFILE, "swtpm at %s does not support TPM 1.2\n", swtpm_prg_l[0]);
×
1439
        goto error;
×
1440
    }
1441

1442
    if (runas) {
77✔
1443
        ret = change_process_owner(runas);
×
1444
        if (ret != 0)
×
1445
            goto error;
×
1446
    }
1447

1448
    if (!got_ownerpass)
77✔
1449
        ownerpass = g_strdup(DEFAULT_OWNER_PASSWORD);
73✔
1450
    if (!got_srkpass)
77✔
1451
        srkpass = g_strdup(DEFAULT_SRK_PASSWORD);
73✔
1452

1453
    if (gl_LOGFILE != NULL) {
77✔
1454
        FILE *tmpfile;
19✔
1455
        if (stat(gl_LOGFILE, &statbuf) == 0 &&
19✔
1456
            (statbuf.st_mode & S_IFMT) == S_IFLNK) {
19✔
1457
            fprintf(stderr, "Logfile must not be a symlink.\n");
×
1458
            goto error;
×
1459
        }
1460
        tmpfile = fopen(gl_LOGFILE, "a");
19✔
1461
        if (tmpfile == NULL) {
19✔
1462
            fprintf(stderr, "Cannot write to logfile %s.\n", gl_LOGFILE);
×
1463
            goto error;
×
1464
        }
1465
        fclose(tmpfile);
19✔
1466
    }
1467

1468
    curr_user = getpwuid(getuid());
77✔
1469

1470
    // Check tpm_state_path directory and access rights
1471
    if (tpm_state_path == NULL) {
77✔
1472
        logerr(gl_LOGFILE, "--tpm-state must be provided\n");
×
1473
        goto error;
×
1474
    }
1475

1476
    backend_state = backend_ops->parse_backend(tpm_state_path);
77✔
1477
    if (!backend_state)
77✔
1478
        goto error;
×
1479

1480
    if (backend_ops->check_access(backend_state, R_OK|W_OK, curr_user) != 0)
77✔
1481
        goto error;
×
1482

1483
    if ((flags & SETUP_WRITE_EK_CERT_FILES_F)) {
77✔
1484
        if (check_directory_access(user_certsdir, W_OK, curr_user) != 0)
4✔
1485
            goto error;
×
1486
    }
1487

1488
    if (flags & SETUP_TPM2_F) {
77✔
1489
        if (flags & SETUP_TAKEOWN_F) {
48✔
1490
            logerr(gl_LOGFILE, "Taking ownership is not supported for TPM 2.\n");
×
1491
            goto error;
×
1492
        }
1493
    } else {
1494
        if (flags & SETUP_TPM2_ECC_F) {
29✔
1495
            logerr(gl_LOGFILE, "--ecc requires --tpm2.\n");
×
1496
            goto error;
×
1497
        }
1498
        if (flags & SETUP_CREATE_SPK_F) {
29✔
1499
            logerr(gl_LOGFILE, "--create-spk requires --tpm2.\n");
×
1500
            goto error;
×
1501
        }
1502
        if (flags & SETUP_RECONFIGURE_F) {
29✔
1503
            logerr(gl_LOGFILE, "--reconfigure requires --tpm2.\n");
×
1504
            goto error;
×
1505
        }
1506
    }
1507

1508
    if (!(flags & SETUP_RECONFIGURE_F)) {
77✔
1509
        ret = check_state_overwrite(swtpm_prg_l, flags, tpm_state_path);
71✔
1510
        if (ret == 1) {
71✔
1511
            goto error;
3✔
1512
        } else if (ret == 2) {
68✔
1513
            ret = 0;
2✔
1514
            goto out;
2✔
1515
        }
1516

1517
        ret = backend_ops->delete_state(backend_state);
66✔
1518
        if (ret != 0)
66✔
1519
            goto error;
×
1520
    }
1521

1522
    if (access(config_file, R_OK) != 0) {
72✔
1523
        logerr(gl_LOGFILE, "User %s cannot read config file %s.\n",
×
1524
               curr_user ? curr_user->pw_name : "<unknown>", config_file);
1525
        goto error;
×
1526
    }
1527

1528
    /* check pcr_banks; read from config file if not given */
1529
    tmp_l = g_strsplit(pcr_banks ? pcr_banks : "", ",", -1);
72✔
1530
    for (i = 0, n = 0; tmp_l[i]; i++) {
156✔
1531
        g_strstrip(tmp_l[i]);
12✔
1532
        n += strlen(tmp_l[i]);
12✔
1533
    }
1534
    g_strfreev(tmp_l);
72✔
1535
    if (n == 0) {
72✔
1536
        g_free(pcr_banks);
66✔
1537
        pcr_banks = get_default_pcr_banks(config_file);
66✔
1538
    }
1539

1540
    if (cipher != NULL) {
72✔
1541
        if (strcmp(cipher, "aes-128-cbc") != 0 &&
72✔
1542
            strcmp(cipher, "aes-cbc") != 0 &&
12✔
1543
            strcmp(cipher, "aes-256-cbc") != 0) {
12✔
1544
            logerr(gl_LOGFILE, "Unsupported cipher %s.\n", cipher);
×
1545
            goto error;
×
1546
        }
1547
        tmp = g_strdup_printf(",mode=%s", cipher);
72✔
1548
        g_free(cipher);
72✔
1549
        cipher = tmp;
72✔
1550
    }
1551

1552
    if (keyfile != NULL) {
72✔
1553
        if (access(keyfile, R_OK) != 0) {
11✔
1554
            logerr(gl_LOGFILE, "User %s cannot read keyfile %s.\n",
×
1555
                   curr_user ? curr_user->pw_name : "<unknown>", keyfile);
1556
            goto error;
×
1557
        }
1558
        swtpm_keyopt = g_strdup_printf("file=%s%s", keyfile, cipher);
11✔
1559
        logit(gl_LOGFILE, "  The TPM's state will be encrypted with a provided key.\n");
11✔
1560
    } else if (pwdfile != NULL) {
61✔
1561
        if (access(pwdfile, R_OK) != 0) {
15✔
1562
            logerr(gl_LOGFILE, "User %s cannot read passphrase file %s.\n",
×
1563
                   curr_user ? curr_user->pw_name : "<unknown>", pwdfile);
1564
            goto error;
×
1565
        }
1566
        swtpm_keyopt = g_strdup_printf("pwdfile=%s%s", pwdfile, cipher);
15✔
1567
        logit(gl_LOGFILE, "  The TPM's state will be encrypted using a key derived from a passphrase.\n");
15✔
1568
    } else if (keyfile_fd >= 0) {
46✔
1569
        fds_to_pass[n_fds_to_pass++] = keyfile_fd;
2✔
1570
        swtpm_keyopt = g_strdup_printf("fd=%ld%s", keyfile_fd, cipher);
2✔
1571
        logit(gl_LOGFILE, "  The TPM's state will be encrypted with a provided key (fd).\n");
2✔
1572
    } else if (pwdfile_fd >= 0) {
44✔
1573
        fds_to_pass[n_fds_to_pass++] = pwdfile_fd;
2✔
1574
        swtpm_keyopt = g_strdup_printf("pwdfd=%ld%s", pwdfile_fd, cipher);
2✔
1575
        logit(gl_LOGFILE, "  The TPM's state will be encrypted using a key derived from a passphrase (fd).\n");
2✔
1576
    }
1577

1578
    if (strcmp(rsa_keysize_str, "max") == 0) {
72✔
1579
        unsigned int *keysizes = NULL;
×
1580
        size_t n_keysizes;
×
1581

1582
        ret = get_rsa_keysizes(flags, swtpm_prg_l, &keysizes, &n_keysizes);
×
1583
        if (ret)
×
1584
            goto error;
×
1585
        g_free(rsa_keysize_str);
×
1586
        if (n_keysizes > 0) {
×
1587
            /* last one is the biggest one */
1588
            rsa_keysize_str = g_strdup_printf("%u", keysizes[n_keysizes - 1]);
×
1589
        } else {
1590
            rsa_keysize_str = g_strdup("2048");
×
1591
        }
1592
        g_free(keysizes);
×
1593
    }
1594
    if (strcmp(rsa_keysize_str, "2048") == 0 || strcmp(rsa_keysize_str, "3072") == 0) {
72✔
1595
        unsigned int *keysizes = NULL;
72✔
1596
        size_t n_keysizes;
72✔
1597
        gboolean found = FALSE;
72✔
1598

1599
        ret = get_rsa_keysizes(flags, swtpm_prg_l, &keysizes, &n_keysizes);
72✔
1600
        if (ret)
72✔
1601
            goto error;
×
1602

1603
        rsa_keysize = strtoull(rsa_keysize_str, NULL, 10);
72✔
1604
        for (i = 0; i < n_keysizes && found == FALSE; i++)
175✔
1605
            found = (keysizes[i] == rsa_keysize);
103✔
1606
        if (!found && rsa_keysize != 2048) {
72✔
1607
            logerr(gl_LOGFILE, "%u bit RSA keys are not supported by libtpms.\n", rsa_keysize);
×
1608
            goto error;
×
1609
        }
1610
        g_free(keysizes);
72✔
1611
    } else {
1612
        logit(gl_LOGFILE, "Unsupported RSA key size %s.\n", rsa_keysize_str);
×
1613
        goto error;
×
1614
    }
1615

1616
    if (flags & SETUP_RECONFIGURE_F) {
72✔
1617
        if (flags & (SETUP_CREATE_EK_F | SETUP_EK_CERT_F | SETUP_PLATFORM_CERT_F)) {
6✔
1618
            logerr(gl_LOGFILE, "Reconfiguration is not supported with creation of EK or certificates\n");
×
1619
            goto error;
×
1620
        }
1621
    }
1622

1623
    now = time(NULL);
72✔
1624
    tm = localtime(&now);
72✔
1625
    if (strftime(tmpbuffer, sizeof(tmpbuffer), "%a %d %h %Y %I:%M:%S %p %Z", tm) == 0) {
72✔
1626
        logerr(gl_LOGFILE, "Could not format time/date string.\n");
×
1627
        goto error;
×
1628
    }
1629
    curr_grp = getgrgid(getgid());
72✔
1630
    logit(gl_LOGFILE, "Starting vTPM %s as %s:%s @ %s\n",
138✔
1631
          flags & SETUP_RECONFIGURE_F ? "reconfiguration" : "manufacturing",
1632
          curr_user ? curr_user->pw_name : "<unknown>",
1633
          curr_grp ? curr_grp->gr_name : "<unknown>",
1634
          tmpbuffer);
1635

1636
    if (flags & (SETUP_EK_CERT_F | SETUP_PLATFORM_CERT_F)) {
72✔
1637
        certsdir = g_dir_make_tmp("swtpm_setup.certs.XXXXXX", &error);
40✔
1638
        if (certsdir == NULL) {
40✔
1639
            logerr(gl_LOGFILE, "Could not create temporary directory for certs: %s\n",
×
1640
                   error->message);
×
1641
            goto error;
×
1642
        }
1643
    }
1644

1645
    if ((flags & SETUP_TPM2_F) == 0) {
72✔
1646
        ret = init_tpm(flags, swtpm_prg_l, config_file, tpm_state_path, ownerpass, srkpass, vmid,
26✔
1647
                       swtpm_keyopt, fds_to_pass, n_fds_to_pass, certsdir, user_certsdir);
1648
    } else {
1649
        ret = init_tpm2(flags, swtpm_prg_l, config_file, tpm_state_path, vmid, pcr_banks,
46✔
1650
                       swtpm_keyopt, fds_to_pass, n_fds_to_pass, rsa_keysize, certsdir,
1651
                       user_certsdir);
1652
    }
1653

1654
    if (ret == 0) {
72✔
1655
        logit(gl_LOGFILE, "Successfully authored TPM state.\n");
72✔
1656
    } else {
1657
        logerr(gl_LOGFILE, "An error occurred. Authoring the TPM state failed.\n");
×
1658
        backend_ops->delete_state(backend_state);
×
1659
    }
1660

1661
    now = time(NULL);
72✔
1662
    tm = localtime(&now);
72✔
1663
    if (strftime(tmpbuffer, sizeof(tmpbuffer), "%a %d %h %Y %I:%M:%S %p %Z", tm) == 0) {
72✔
1664
        logerr(gl_LOGFILE, "Could not format time/date string.\n");
×
1665
        goto error;
×
1666
    }
1667
    logit(gl_LOGFILE, "Ending vTPM manufacturing @ %s\n",
72✔
1668
          tmpbuffer);
1669

1670
out:
83✔
1671
    if (certsdir && g_rmdir(certsdir) != 0)
83✔
1672
        logerr(gl_LOGFILE, "Could not remove temporary directory for certs: %s\n",
×
1673
               strerror(errno));
×
1674

1675
    if (backend_ops && backend_state)
83✔
1676
        backend_ops->free_backend(backend_state);
77✔
1677
    g_strfreev(swtpm_prg_l);
83✔
1678
    g_free(gl_LOGFILE);
83✔
1679

1680
    return ret;
83✔
1681

1682
error:
4✔
1683
    ret = 1;
4✔
1684
    goto out;
4✔
1685
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc