-
Notifications
You must be signed in to change notification settings - Fork 375
/
tpm2_import.c
569 lines (486 loc) · 16.9 KB
/
tpm2_import.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* SPDX-License-Identifier: BSD-3-Clause */
//**********************************************************************;
// Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
// Licensed under the Apache License 2.0 (the "License"). You may not use
// this file except in compliance with the License. You can obtain a copy
// in the file LICENSE in the source distribution or at
// https://www.openssl.org/source/license.html
//
// EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0)
//
// See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
// http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
// proof for the original OAEP scheme, which EME-OAEP is based on. A new
// proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
// "RSA-OEAP is Still Alive!", Dec. 2000, <URL:http://eprint.iacr.org/2000/061/>.
// The new proof has stronger requirements for the underlying permutation:
// "partial-one-wayness" instead of one-wayness. For the RSA function, this
// is an equivalent notion.
//**********************************************************************;
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <tss2/tss2_mu.h>
#include <openssl/rand.h>
#include "files.h"
#include "log.h"
#include "tpm2.h"
#include "tpm2_tool.h"
#include "tpm2_alg_util.h"
#include "tpm2_attr_util.h"
#include "tpm2_auth_util.h"
#include "tpm2_errata.h"
#include "tpm2_identity_util.h"
#include "tpm2_openssl.h"
#include "tpm2_options.h"
typedef struct tpm_import_ctx tpm_import_ctx;
struct tpm_import_ctx {
struct {
const char *ctx_path;
const char *auth_str;
tpm2_loaded_object object;
} parent;
char *input_key_file;
char *public_key_file;
char *private_key_file;
char *parent_key_public_file;
char *name_alg;
char *attrs; /* The attributes to use */
char *key_auth_str;
char *auth_key_file; /* an optional auth string for the input key file for OSSL */
char *input_seed_file;
char *input_enc_key_file;
char *policy;
bool import_tpm; /* Any param that is exclusively used by import tpm object sets this flag */
TPMI_ALG_PUBLIC key_type;
char *cp_hash_path;
};
static tpm_import_ctx ctx = {
.key_type = TPM2_ALG_ERROR,
.input_key_file = NULL,
};
static tool_rc readpublic(ESYS_CONTEXT *ectx, ESYS_TR handle,
TPM2B_PUBLIC **public) {
return tpm2_readpublic(ectx, handle, public, NULL, NULL);
}
static bool create_import_key_private_data(TPM2B_PRIVATE *private,
TPMI_ALG_HASH parent_name_alg,
TPM2B_MAX_BUFFER *encrypted_duplicate_sensitive,
TPM2B_DIGEST *outer_hmac) {
//UINT16 hash_size = tpm2_alg_util_get_hash_size(ctx.name_alg);
UINT16 parent_hash_size = tpm2_alg_util_get_hash_size(parent_name_alg);
private->size = sizeof(parent_hash_size) + parent_hash_size
+ encrypted_duplicate_sensitive->size;
size_t hmac_size_offset = 0;
TSS2_RC rval = Tss2_MU_UINT16_Marshal(parent_hash_size, private->buffer,
sizeof(parent_hash_size), &hmac_size_offset);
if (rval != TPM2_RC_SUCCESS)
{
LOG_ERR("Error serializing parent hash size");
return false;
}
memcpy(private->buffer + hmac_size_offset, outer_hmac->buffer,
parent_hash_size);
memcpy(private->buffer + hmac_size_offset + parent_hash_size,
encrypted_duplicate_sensitive->buffer,
encrypted_duplicate_sensitive->size);
return true;
}
static tool_rc key_import(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *parent_pub,
TPM2B_SENSITIVE *privkey, TPM2B_PUBLIC *pubkey,
TPM2B_ENCRYPTED_SECRET *encrypted_seed,
TPM2B_PRIVATE **imported_private) {
TPMI_ALG_HASH name_alg = pubkey->publicArea.nameAlg;
TPM2B_DIGEST *seed = &privkey->sensitiveArea.seedValue;
/*
* Create the protection encryption key that gets encrypted with the parents public key.
*/
TPM2B_DATA enc_sensitive_key = {
.size = parent_pub->publicArea.parameters.rsaDetail.symmetric.keyBits.sym / 8
};
memset(enc_sensitive_key.buffer, 0xFF, enc_sensitive_key.size);
/*
* Calculate the object name.
*/
TPM2B_NAME pubname = TPM2B_TYPE_INIT(TPM2B_NAME, name);
bool res = tpm2_identity_create_name(pubkey, &pubname);
if (!res) {
return false;
}
TPM2B_MAX_BUFFER hmac_key;
TPM2B_MAX_BUFFER enc_key;
tpm2_identity_util_calc_outer_integrity_hmac_key_and_dupsensitive_enc_key(
parent_pub, &pubname, seed, &hmac_key, &enc_key);
TPM2B_MAX_BUFFER encrypted_inner_integrity = TPM2B_EMPTY_INIT;
tpm2_identity_util_calculate_inner_integrity(name_alg, privkey, &pubname,
&enc_sensitive_key,
&parent_pub->publicArea.parameters.rsaDetail.symmetric,
&encrypted_inner_integrity);
TPM2B_DIGEST outer_hmac = TPM2B_EMPTY_INIT;
TPM2B_MAX_BUFFER encrypted_duplicate_sensitive = TPM2B_EMPTY_INIT;
tpm2_identity_util_calculate_outer_integrity(parent_pub->publicArea.nameAlg,
&pubname, &encrypted_inner_integrity, &hmac_key, &enc_key,
&parent_pub->publicArea.parameters.rsaDetail.symmetric,
&encrypted_duplicate_sensitive, &outer_hmac);
TPM2B_PRIVATE private = TPM2B_EMPTY_INIT;
res = create_import_key_private_data(&private, parent_pub->publicArea.nameAlg,
&encrypted_duplicate_sensitive, &outer_hmac);
if (!res) {
return false;
}
TPMT_SYM_DEF_OBJECT *sym_alg =
&parent_pub->publicArea.parameters.rsaDetail.symmetric;
if (!ctx.cp_hash_path) {
return tpm2_import(ectx, &ctx.parent.object, &enc_sensitive_key, pubkey,
&private, encrypted_seed, sym_alg, imported_private, NULL);
}
TPM2B_DIGEST cp_hash = { .size = 0 };
tool_rc rc = tpm2_import(ectx, &ctx.parent.object, &enc_sensitive_key, pubkey,
&private, encrypted_seed, sym_alg, imported_private, &cp_hash);
if (rc != tool_rc_success) {
return rc;
}
bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
if (!result) {
rc = tool_rc_general_error;
}
return rc;
}
static bool on_option(char key, char *value) {
switch (key) {
case 'P':
ctx.parent.auth_str = value;
break;
case 'p':
ctx.key_auth_str = value;
break;
case 'G':
ctx.key_type = tpm2_alg_util_from_optarg(value,
tpm2_alg_util_flags_asymmetric | tpm2_alg_util_flags_symmetric);
if (ctx.key_type == TPM2_ALG_ERROR) {
LOG_ERR("Unsupported key type");
return false;
}
return true;
case 'i':
ctx.input_key_file = value;
break;
case 'C':
ctx.parent.ctx_path = value;
break;
case 'U':
ctx.parent_key_public_file = value;
break;
case 'k':
ctx.import_tpm = true;
ctx.input_enc_key_file = value;
break;
case 'u':
ctx.public_key_file = value;
break;
case 'r':
ctx.private_key_file = value;
break;
case 'a':
ctx.attrs = value;
break;
case 'g':
ctx.name_alg = value;
break;
case 's':
ctx.import_tpm = true;
ctx.input_seed_file = value;
break;
case 'L':
ctx.policy = value;
break;
case 0:
ctx.auth_key_file = value;
break;
case 1:
ctx.cp_hash_path = value;
break;
default:
LOG_ERR("Invalid option");
return false;
}
return true;
}
static bool tpm2_tool_onstart(tpm2_options **opts) {
const struct option topts[] = {
{ "parent-auth", required_argument, NULL, 'P'},
{ "key-auth", required_argument, NULL, 'p'},
{ "key-algorithm", required_argument, NULL, 'G'},
{ "input", required_argument, NULL, 'i'},
{ "parent-context", required_argument, NULL, 'C'},
{ "parent-public", required_argument, NULL, 'U'},
{ "private", required_argument, NULL, 'r'},
{ "public", required_argument, NULL, 'u'},
{ "attributes", required_argument, NULL, 'a'},
{ "hash-algorithm", required_argument, NULL, 'g'},
{ "seed", required_argument, NULL, 's'},
{ "policy", required_argument, NULL, 'L'},
{ "encryption-key", required_argument, NULL, 'k'},
{ "passin", required_argument, NULL, 0 },
{ "cphash", required_argument, NULL, 1 },
};
*opts = tpm2_options_new("P:p:G:i:C:U:u:r:a:g:s:L:k:", ARRAY_LEN(topts),
topts, on_option, NULL, 0);
return *opts != NULL;
}
/**
* Check all options and report as many errors as possible via LOG_ERR.
* @return
* tool_rc indicating error.
*/
static tool_rc check_options(void) {
tool_rc rc = tool_rc_success;
/* Check the tpm import specific options */
if (ctx.import_tpm) {
if (!ctx.input_seed_file) {
LOG_ERR("Expected SymSeed to be specified via \"-s\","
" missing option.");
rc = tool_rc_option_error;
}
/* If a key file is specified we choose aes else null
for symmetricAlgdefinition */
if (!ctx.input_enc_key_file) {
ctx.key_type = TPM2_ALG_NULL;
} else {
ctx.key_type = TPM2_ALG_AES;
}
} else { /* Openssl specific option(s) */
if (!ctx.key_type) {
LOG_ERR("Expected key type to be specified via \"-G\","
" missing option.");
rc = tool_rc_option_error;
}
if (ctx.cp_hash_path) {
LOG_WARN("CAUTION CpHash calculation includes parameters that"
"have a derived/random seed!");
}
}
/* Common options */
if (!ctx.input_key_file) {
LOG_ERR("Expected to be imported key data to be specified via \"-i\","
" missing option.");
rc = tool_rc_option_error;
}
if (!ctx.public_key_file) {
LOG_ERR("Expected output public file missing, specify \"-u\","
" missing option.");
rc = tool_rc_option_error;
}
if (!ctx.private_key_file) {
LOG_ERR("Expected output private file missing, specify \"-r\","
" missing option.");
rc = tool_rc_option_error;
}
if (!ctx.parent.ctx_path) {
LOG_ERR("Expected parent key to be specified via \"-C\","
" missing option.");
rc = tool_rc_option_error;
}
return rc;
}
static tool_rc openssl_import(ESYS_CONTEXT *ectx) {
/*
* Load the parent public file, or read it from the TPM if not specified.
* We need this information for encrypting the protection seed.
*/
bool free_ppub = false;
tool_rc tmp_rc;
tool_rc rc = tool_rc_general_error;
TPM2B_PUBLIC ppub = TPM2B_EMPTY_INIT;
TPM2B_PUBLIC *parent_pub = NULL;
bool result;
tmp_rc = tool_rc_general_error;
if (ctx.parent_key_public_file) {
result = files_load_public(ctx.parent_key_public_file, &ppub);
parent_pub = &ppub;
} else {
tmp_rc = readpublic(ectx, ctx.parent.object.tr_handle, &parent_pub);
free_ppub = true;
result = tmp_rc == tool_rc_success;
}
if (!result) {
LOG_ERR("Failed loading parent key public.");
return tmp_rc;
}
TPM2B_SENSITIVE private = TPM2B_EMPTY_INIT;
TPM2B_PUBLIC public = TPM2B_EMPTY_INIT;
TPM2B_ENCRYPTED_SECRET encrypted_seed = TPM2B_EMPTY_INIT;
result = tpm2_openssl_import_keys(
parent_pub,
&private,
&public,
&encrypted_seed,
ctx.input_key_file,
ctx.key_type,
ctx.auth_key_file,
ctx.policy,
ctx.key_auth_str,
ctx.attrs,
ctx.name_alg
);
if (!result)
goto out;
TPM2B_PRIVATE *imported_private = NULL;
tmp_rc = key_import(ectx, parent_pub, &private, &public, &encrypted_seed,
&imported_private);
if (tmp_rc != tool_rc_success || ctx.cp_hash_path) {
rc = tmp_rc;
goto keyout;
}
/*
* Save the public and imported_private structure to disk
*/
result = files_save_public(&public, ctx.public_key_file);
if (!result) {
goto keyout;
}
result = files_save_private(imported_private, ctx.private_key_file);
if (!result) {
goto keyout;
}
/*
* Output the stats on the created object on Success.
*/
tpm2_util_public_to_yaml(&public, NULL);
rc = tool_rc_success;
keyout:
free(imported_private);
out:
if (free_ppub) {
free(parent_pub);
}
return rc;
}
static bool set_key_algorithm(TPMI_ALG_PUBLIC alg, TPMT_SYM_DEF_OBJECT * obj) {
bool result = true;
switch (alg) {
case TPM2_ALG_AES:
obj->algorithm = TPM2_ALG_AES;
obj->keyBits.aes = 128;
obj->mode.aes = TPM2_ALG_CFB;
break;
case TPM2_ALG_NULL:
obj->algorithm = TPM2_ALG_NULL;
break;
default:
LOG_ERR("The algorithm type input(0x%x) is not supported!", alg);
result = false;
break;
}
return result;
}
static tool_rc tpm_import(ESYS_CONTEXT *ectx) {
TPM2B_DATA enc_key = TPM2B_EMPTY_INIT;
TPM2B_PUBLIC public = TPM2B_EMPTY_INIT;
TPM2B_PRIVATE duplicate;
TPM2B_ENCRYPTED_SECRET encrypted_seed;
TPM2B_PRIVATE *imported_private = NULL;
TPMT_SYM_DEF_OBJECT sym_alg;
tool_rc rc;
bool result = set_key_algorithm(ctx.key_type, &sym_alg);
if (!result) {
return tool_rc_general_error;
}
/* Symmetric key */
if (ctx.input_enc_key_file) {
enc_key.size = 16;
result = files_load_bytes_from_path(ctx.input_enc_key_file,
enc_key.buffer, &enc_key.size);
if (!result) {
LOG_ERR("Failed to load symmetric encryption key\"%s\"",
ctx.input_enc_key_file);
return tool_rc_general_error;
}
if (enc_key.size != 16) {
LOG_ERR("Invalid AES key size, got %u bytes, expected 16",
enc_key.size);
return tool_rc_general_error;
}
}
/* Private key */
result = files_load_private(ctx.input_key_file, &duplicate);
if (!result) {
LOG_ERR("Failed to load duplicate \"%s\"", ctx.input_key_file);
return tool_rc_general_error;
}
/* Encrypted seed */
result = files_load_encrypted_seed(ctx.input_seed_file, &encrypted_seed);
if (!result) {
LOG_ERR("Failed to load encrypted seed \"%s\"", ctx.input_seed_file);
return tool_rc_general_error;
}
/* Public key */
result = files_load_public(ctx.public_key_file, &public);
if (!result) {
LOG_ERR(":( Failed to load public key \"%s\"", ctx.public_key_file);
return tool_rc_general_error;
}
if (ctx.policy) {
public.publicArea.authPolicy.size =
sizeof(public.publicArea.authPolicy.buffer);
result = files_load_bytes_from_path(ctx.policy,
public.publicArea.authPolicy.buffer,
&public.publicArea.authPolicy.size);
if (!result) {
LOG_ERR("Failed to copy over the auth policy to the public data");
return tool_rc_general_error;
}
}
if (!ctx.cp_hash_path) {
rc = tpm2_import(ectx, &ctx.parent.object, &enc_key, &public, &duplicate,
&encrypted_seed, &sym_alg, &imported_private, NULL);
if (rc != tool_rc_success) {
return rc;
}
assert(imported_private);
result = files_save_private(imported_private, ctx.private_key_file);
free(imported_private);
if (!result) {
LOG_ERR("Failed to save private key into file \"%s\"",
ctx.private_key_file);
return tool_rc_general_error;
}
return tool_rc_success;
}
TPM2B_DIGEST cp_hash = { .size = 0 };
rc = tpm2_import(ectx, &ctx.parent.object, &enc_key, &public, &duplicate,
&encrypted_seed, &sym_alg, &imported_private, &cp_hash);
if (rc != tool_rc_success) {
return rc;
}
result = files_save_digest(&cp_hash, ctx.cp_hash_path);
if (!result) {
rc = tool_rc_general_error;
}
return rc;
}
static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
UNUSED(flags);
tool_rc rc = check_options();
if (rc != tool_rc_success) {
return rc;
}
rc = tpm2_util_object_load_auth(ectx, ctx.parent.ctx_path,
ctx.parent.auth_str, &ctx.parent.object, false,
TPM2_HANDLE_ALL_W_NV);
if (rc != tool_rc_success) {
LOG_ERR("Invalid parent key authorization");
return rc;
}
return ctx.import_tpm ? tpm_import(ectx) : openssl_import(ectx);
}
static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
UNUSED(ectx);
if (!ctx.import_tpm) {
return tool_rc_success;
}
return tpm2_session_close(&ctx.parent.object.session);
}
// Register this tool with tpm2_tool.c
TPM2_TOOL_REGISTER("import", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)