-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtpm2-provider-pkey.c
More file actions
404 lines (358 loc) · 13.1 KB
/
tpm2-provider-pkey.c
File metadata and controls
404 lines (358 loc) · 13.1 KB
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
/* SPDX-License-Identifier: BSD-3-Clause */
#include <string.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <tss2/tss2_mu.h>
#include "tpm2-provider-pkey.h"
typedef struct {
ASN1_OBJECT *type;
ASN1_BOOLEAN emptyAuth;
ASN1_INTEGER *parent;
ASN1_OCTET_STRING *pubkey;
ASN1_OCTET_STRING *privkey;
} TSSPRIVKEY;
ASN1_SEQUENCE(TSSPRIVKEY) = {
ASN1_SIMPLE(TSSPRIVKEY, type, ASN1_OBJECT),
ASN1_EXP_OPT(TSSPRIVKEY, emptyAuth, ASN1_BOOLEAN, 0),
ASN1_SIMPLE(TSSPRIVKEY, parent, ASN1_INTEGER),
ASN1_SIMPLE(TSSPRIVKEY, pubkey, ASN1_OCTET_STRING),
ASN1_SIMPLE(TSSPRIVKEY, privkey, ASN1_OCTET_STRING)
} ASN1_SEQUENCE_END(TSSPRIVKEY)
#define OID_loadableKey "2.23.133.10.1.3"
IMPLEMENT_ASN1_FUNCTIONS(TSSPRIVKEY);
IMPLEMENT_PEM_write_bio(TSSPRIVKEY, TSSPRIVKEY, TSSPRIVKEY_PEM_STRING, TSSPRIVKEY);
IMPLEMENT_PEM_read_bio(TSSPRIVKEY, TSSPRIVKEY, TSSPRIVKEY_PEM_STRING, TSSPRIVKEY);
TSSPRIVKEY *d2i_TSSPRIVKEY_bio(BIO *bp, TSSPRIVKEY **a)
{
return ASN1_d2i_bio_of(TSSPRIVKEY, TSSPRIVKEY_new, d2i_TSSPRIVKEY, bp, a);
}
int i2d_TSSPRIVKEY_bio(BIO *bp, const TSSPRIVKEY *a)
{
return ASN1_i2d_bio_of(TSSPRIVKEY, i2d_TSSPRIVKEY, bp, a);
}
/** Serialize TPM2_KEYDATA onto disk
*
* Write the tpm2tss key data into a file using PEM encoding.
* @param tpm2Data The data to be written to disk.
* @param filename The filename to write the data to.
* @retval 1 on success
* @retval 0 on failure
*/
int
tpm2_keydata_write(const TPM2_KEYDATA *keydata, BIO *bout, TPM2_PKEY_FORMAT format)
{
TSSPRIVKEY *tpk = NULL;
BIGNUM *bn_parent = NULL;
uint8_t privbuf[sizeof(keydata->priv)];
uint8_t pubbuf[sizeof(keydata->pub)];
size_t privbuf_len = 0, pubbuf_len = 0;
tpk = TSSPRIVKEY_new();
if (!tpk)
return 0;
if (Tss2_MU_TPM2B_PRIVATE_Marshal(&keydata->priv, &privbuf[0],
sizeof(privbuf), &privbuf_len))
goto error;
if (Tss2_MU_TPM2B_PUBLIC_Marshal(&keydata->pub, &pubbuf[0],
sizeof(pubbuf), &pubbuf_len))
goto error;
tpk->type = OBJ_txt2obj(OID_loadableKey, 1);
if (!tpk->type)
goto error;
tpk->emptyAuth = ! !keydata->emptyAuth;
// note the ASN1_INTEGER_set is not reliable for uin32_t on 32-bit machines
// instead we're using big nums by setting a word (which is unsigned)
bn_parent = BN_new();
if (!bn_parent)
goto error;
if (keydata->parent != 0)
BN_set_word(bn_parent, keydata->parent);
else
BN_set_word(bn_parent, TPM2_RH_OWNER);
if (!BN_to_ASN1_INTEGER(bn_parent, tpk->parent))
goto error;
ASN1_STRING_set(tpk->privkey, &privbuf[0], privbuf_len);
ASN1_STRING_set(tpk->pubkey, &pubbuf[0], pubbuf_len);
switch (format) {
case KEY_FORMAT_PEM:
PEM_write_bio_TSSPRIVKEY(bout, tpk);
break;
case KEY_FORMAT_DER:
i2d_TSSPRIVKEY_bio(bout, tpk);
break;
default:
goto error;
}
BN_free(bn_parent);
TSSPRIVKEY_free(tpk);
return 1;
error:
if (bn_parent)
BN_free(bn_parent);
TSSPRIVKEY_free(tpk);
return 0;
}
/** Deserialize TPM2_KEYDATA from disk
*
* Read the tpm2tss key data from a file using PEM encoding.
* @param filename The filename to read the data from.
* @param tpm2Datap The data after read.
* @retval 1 on success
* @retval 0 on EOF
* @retval -1 on failure
*/
int
tpm2_keydata_read(BIO *bin, TPM2_KEYDATA *keydata, TPM2_PKEY_FORMAT format)
{
BIGNUM *bn_parent = NULL;
BN_ULONG parent;
BN_ULONG all_bits_set = (BN_ULONG)~(BN_ULONG)0;
TSSPRIVKEY *tpk = NULL;
char type_oid[64];
switch (format) {
case KEY_FORMAT_PEM:
tpk = PEM_read_bio_TSSPRIVKEY(bin, NULL, NULL, NULL);
break;
case KEY_FORMAT_DER:
tpk = d2i_TSSPRIVKEY_bio(bin, NULL);
break;
default:
return 0;
}
if (tpk == NULL)
return 0;
keydata->privatetype = KEY_TYPE_BLOB;
keydata->emptyAuth = (tpk->emptyAuth != V_ASN1_UNDEF && tpk->emptyAuth);
// COMPATIBILITY:
// The parent handle of TPM2_TSS keys created with provider version < 1.2 or engine version <1.2.0-rc0 have been written using ASN1_INTEGER_set
// ASN1_INTEGER_set takes a (signed) long (which on 32-bit systems is 32-bit and on 64-bit systems is 64bit)
// As parent handles are in the range of 0x81000000 - 0x81FFFFFF the MSB on a 32-bit system is always set, and therefore is treated as negative.
// This won't be the case on 64 bit systems or in case the handle had been written using BN_set_word().
//
// The parent handle of TPM2_TSS keys create with provider verision 1.2 - 1.3 have been written using ASN1_INTEGER_set_uint64.
// These values can safely be read using BN_get_word, as the values written always were of type TPM2_HANDLE (uint32_t).
//
// | provider version | engine version | write method | read method |
// | tpm2-openssl | tpm2-tss-engine | according to provider/engine version | according to latest provider/engine implementation |
// | -----------------+-----------------+--------------------------------------+----------------------------------------------------|
// | <1.2.0 | <1.2.0-rc0 | ASN1_INTEGER_get | ASN1_integer_set |
// | 1.2.0 - 1.3.0 | | ASN1_INTEGER_set_uint64 | BN_get_word |
// | >1.3.0 | >=1.2.0-rc0 | BN_set_word | BN_get_word |
bn_parent = ASN1_INTEGER_to_BN(tpk->parent, NULL);
if (!bn_parent)
goto error;
if (BN_is_negative(bn_parent)) {
keydata->parent = ASN1_INTEGER_get(tpk->parent);
} else {
parent = BN_get_word(bn_parent);
// BN_get_words set's all bits in case of an error.
// BN_ULONG is 32-bits on 32-bit systems and 64-bits on 64-bit systems.
// Looking at the size of data types only UINT32_MAX (all_bits_set on a 32-bit system) would be a valid value to store in keydata->parent (which is of type TPM2_HANDLE).
// As parent handles are always in the range of 0x81000000 - 0x81FFFFFF it's okay to treat UINT32_MAX as error.
if (parent == all_bits_set)
goto error;
keydata->parent = parent;
}
if (keydata->parent == 0)
keydata->parent = TPM2_RH_OWNER;
if (!OBJ_obj2txt(type_oid, sizeof(type_oid), tpk->type, 1) ||
strcmp(type_oid, OID_loadableKey))
goto error;
if (Tss2_MU_TPM2B_PRIVATE_Unmarshal(tpk->privkey->data,
tpk->privkey->length, NULL,
&keydata->priv))
goto error;
if (Tss2_MU_TPM2B_PUBLIC_Unmarshal(tpk->pubkey->data,
tpk->pubkey->length, NULL,
&keydata->pub))
goto error;
BN_free(bn_parent);
TSSPRIVKEY_free(tpk);
return 1;
error:
if (bn_parent) {
BN_free(bn_parent);
}
TSSPRIVKEY_free(tpk);
return 0;
}
static const TPM2B_PUBLIC primaryRsaTemplate = {
.publicArea = {
.type = TPM2_ALG_RSA,
.nameAlg = ENGINE_HASH_ALG,
.objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
TPMA_OBJECT_RESTRICTED |
TPMA_OBJECT_DECRYPT |
TPMA_OBJECT_NODA |
TPMA_OBJECT_FIXEDTPM |
TPMA_OBJECT_FIXEDPARENT |
TPMA_OBJECT_SENSITIVEDATAORIGIN),
.authPolicy = {
.size = 0,
},
.parameters.rsaDetail = {
.symmetric = {
.algorithm = TPM2_ALG_AES,
.keyBits.aes = 128,
.mode.aes = TPM2_ALG_CFB,
},
.scheme = {
.scheme = TPM2_ALG_NULL,
.details = {}
},
.keyBits = 2048,
.exponent = 0,
},
.unique.rsa = {
.size = 0,
}
}
};
static const TPM2B_PUBLIC primaryEccTemplate = {
.publicArea = {
.type = TPM2_ALG_ECC,
.nameAlg = ENGINE_HASH_ALG,
.objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
TPMA_OBJECT_RESTRICTED |
TPMA_OBJECT_DECRYPT |
TPMA_OBJECT_NODA |
TPMA_OBJECT_FIXEDTPM |
TPMA_OBJECT_FIXEDPARENT |
TPMA_OBJECT_SENSITIVEDATAORIGIN),
.authPolicy = {
.size = 0,
},
.parameters.eccDetail = {
.symmetric = {
.algorithm = TPM2_ALG_AES,
.keyBits.aes = 128,
.mode.aes = TPM2_ALG_CFB,
},
.scheme = {
.scheme = TPM2_ALG_NULL,
.details = {}
},
.curveID = TPM2_ECC_NIST_P256,
.kdf = {
.scheme = TPM2_ALG_NULL,
.details = {}
},
},
.unique.ecc = {
.x.size = 0,
.y.size = 0
}
}
};
static const TPM2B_SENSITIVE_CREATE primarySensitive = {
.sensitive = {
.userAuth = {
.size = 0,
},
.data = {
.size = 0,
}
}
};
static const TPM2B_DATA allOutsideInfo = {
.size = 0,
};
static const TPML_PCR_SELECTION allCreationPCR = {
.count = 0,
};
int
tpm2_load_parent(const OSSL_CORE_HANDLE *core, tpm2_semaphore_t esys_lock, ESYS_CONTEXT *esys_ctx,
TPM2_HANDLE handle, TPM2B_DIGEST *auth, ESYS_TR *object)
{
TSS2_RC r;
/*
* Set the parent auth if specified. Their is no way to get the pkey parent-auth
* argument to this method, so we pull from the environment.
*/
if (auth->size == 0) {
const char *pauth = getenv("TPM2OPENSSL_PARENT_AUTH");
if (pauth) {
if (strlen(pauth) > sizeof(auth->buffer)) {
TPM2_ERROR_raise(core, TPM2_ERR_WRONG_DATA_LENGTH);
goto error1;
}
auth->size = strlen(pauth);
memcpy(auth->buffer, pauth, auth->size);
}
}
if (!tpm2_semaphore_lock(esys_lock))
goto error1;
r = Esys_TR_FromTPMPublic(esys_ctx, handle,
ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
object);
TPM2_CHECK_RC(core, r, TPM2_ERR_CANNOT_LOAD_PARENT, goto error2);
if (auth->size > 0) {
r = Esys_TR_SetAuth(esys_ctx, *object, auth);
TPM2_CHECK_RC(core, r, TPM2_ERR_CANNOT_LOAD_PARENT, goto error3);
}
tpm2_semaphore_unlock(esys_lock);
return 1;
error3:
Esys_FlushContext(esys_ctx, *object);
error2:
tpm2_semaphore_unlock(esys_lock);
error1:
return 0;
}
int
tpm2_build_primary(const OSSL_CORE_HANDLE *core, tpm2_semaphore_t esys_lock, ESYS_CONTEXT *esys_ctx,
const TPMS_CAPABILITY_DATA *algorithms, ESYS_TR hierarchy,
const TPM2B_DIGEST *auth, ESYS_TR *object)
{
const TPM2B_PUBLIC *primaryTemplate = NULL;
TSS2_RC r;
if (!tpm2_semaphore_lock(esys_lock))
return 0;
r = Esys_TR_SetAuth(esys_ctx, hierarchy, auth);
TPM2_CHECK_RC(core, r, TPM2_ERR_CANNOT_CREATE_PRIMARY, goto error);
if (tpm2_supports_algorithm(algorithms, TPM2_ALG_ECC))
primaryTemplate = &primaryEccTemplate;
else if (tpm2_supports_algorithm(algorithms, TPM2_ALG_RSA))
primaryTemplate = &primaryRsaTemplate;
if(!primaryTemplate) {
TPM2_ERROR_raise(core, TPM2_ERR_UNKNOWN_ALGORITHM);
goto error;
}
r = Esys_CreatePrimary(esys_ctx, hierarchy,
ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
&primarySensitive, primaryTemplate, &allOutsideInfo,
&allCreationPCR,
object, NULL, NULL, NULL, NULL);
if (r == 0x000009a2) {
TPM2_ERROR_raise(core, TPM2_ERR_AUTHORIZATION_FAILURE);
goto error;
}
TPM2_CHECK_RC(core, r, TPM2_ERR_CANNOT_CREATE_PRIMARY, goto error);
tpm2_semaphore_unlock(esys_lock);
return 1;
error:
tpm2_semaphore_unlock(esys_lock);
return 0;
}
const char *
tpm2_openssl_type(TPM2_KEYDATA *keydata)
{
if (keydata->pub.publicArea.type == TPM2_ALG_RSA) {
if (keydata->pub.publicArea.objectAttributes & TPMA_OBJECT_RESTRICTED) {
/* when it's a restricted key */
switch (keydata->pub.publicArea.parameters.rsaDetail.scheme.scheme) {
case TPM2_ALG_NULL:
case TPM2_ALG_RSASSA:
return "RSA";
case TPM2_ALG_RSAPSS:
/* if it is restricted to PSS, then it's a RSA-PSS key */
return "RSA-PSS";
default:
return NULL;
}
} else
return "RSA";
} else if (keydata->pub.publicArea.type == TPM2_ALG_ECC) {
return "EC";
} else
return NULL;
}