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
|
/*
* Copyright (c) 2021 Yubico AB. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
* SPDX-License-Identifier: BSD-2-Clause
*/
/*
* Trusted Platform Module (TPM) 2.0 attestation support. Documentation
* references are relative to revision 01.38 of the TPM 2.0 specification.
*/
#include <openssl/sha.h>
#include "packed.h"
#include "fido.h"
/* Part 1, 4.89: TPM_GENERATED_VALUE */
#define TPM_MAGIC 0xff544347
/* Part 2, 6.3: TPM_ALG_ID */
#define TPM_ALG_RSA 0x0001
#define TPM_ALG_SHA256 0x000b
#define TPM_ALG_NULL 0x0010
#define TPM_ALG_ECC 0x0023
/* Part 2, 6.4: TPM_ECC_CURVE */
#define TPM_ECC_P256 0x0003
/* Part 2, 6.9: TPM_ST_ATTEST_CERTIFY */
#define TPM_ST_CERTIFY 0x8017
/* Part 2, 8.3: TPMA_OBJECT */
#define TPMA_RESERVED 0xfff8f309 /* reserved bits; must be zero */
#define TPMA_FIXED 0x00000002 /* object has fixed hierarchy */
#define TPMA_CLEAR 0x00000004 /* object persists */
#define TPMA_FIXED_P 0x00000010 /* object has fixed parent */
#define TPMA_SENSITIVE 0x00000020 /* data originates within tpm */
#define TPMA_SIGN 0x00040000 /* object may sign */
/* Part 2, 10.4.2: TPM2B_DIGEST */
PACKED_TYPE(tpm_sha256_digest_t,
struct tpm_sha256_digest {
uint16_t size; /* sizeof(body) */
uint8_t body[32];
})
/* Part 2, 10.4.3: TPM2B_DATA */
PACKED_TYPE(tpm_sha1_data_t,
struct tpm_sha1_data {
uint16_t size; /* sizeof(body) */
uint8_t body[20];
})
/* Part 2, 10.5.3: TPM2B_NAME */
PACKED_TYPE(tpm_sha256_name_t,
struct tpm_sha256_name {
uint16_t size; /* sizeof(alg) + sizeof(body) */
uint16_t alg; /* TPM_ALG_SHA256 */
uint8_t body[32];
})
/* Part 2, 10.11.1: TPMS_CLOCK_INFO */
PACKED_TYPE(tpm_clock_info_t,
struct tpm_clock_info {
uint64_t timestamp_ms;
uint32_t reset_count; /* obfuscated by tpm */
uint32_t restart_count; /* obfuscated by tpm */
uint8_t safe; /* 1 if timestamp_ms is current */
})
/* Part 2, 10.12.8 TPMS_ATTEST */
PACKED_TYPE(tpm_sha1_attest_t,
struct tpm_sha1_attest {
uint32_t magic; /* TPM_MAGIC */
uint16_t type; /* TPM_ST_ATTEST_CERTIFY */
tpm_sha256_name_t signer; /* full tpm path of signing key */
tpm_sha1_data_t data; /* signed sha1 */
tpm_clock_info_t clock;
uint64_t fwversion; /* obfuscated by tpm */
tpm_sha256_name_t name; /* sha256 of tpm_rs256_pubarea_t */
tpm_sha256_name_t qual_name; /* full tpm path of attested key */
})
/* Part 2, 11.2.4.5: TPM2B_PUBLIC_KEY_RSA */
PACKED_TYPE(tpm_rs256_key_t,
struct tpm_rs256_key {
uint16_t size; /* sizeof(body) */
uint8_t body[256];
})
/* Part 2, 11.2.5.1: TPM2B_ECC_PARAMETER */
PACKED_TYPE(tpm_es256_coord_t,
struct tpm_es256_coord {
uint16_t size; /* sizeof(body) */
uint8_t body[32];
})
/* Part 2, 11.2.5.2: TPMS_ECC_POINT */
PACKED_TYPE(tpm_es256_point_t,
struct tpm_es256_point {
tpm_es256_coord_t x;
tpm_es256_coord_t y;
})
/* Part 2, 12.2.3.5: TPMS_RSA_PARMS */
PACKED_TYPE(tpm_rs256_param_t,
struct tpm_rs256_param {
uint16_t symmetric; /* TPM_ALG_NULL */
uint16_t scheme; /* TPM_ALG_NULL */
uint16_t keybits; /* 2048 */
uint32_t exponent; /* zero (meaning 2^16 + 1) */
})
/* Part 2, 12.2.3.6: TPMS_ECC_PARMS */
PACKED_TYPE(tpm_es256_param_t,
struct tpm_es256_param {
uint16_t symmetric; /* TPM_ALG_NULL */
uint16_t scheme; /* TPM_ALG_NULL */
uint16_t curve_id; /* TPM_ECC_P256 */
uint16_t kdf; /* TPM_ALG_NULL */
})
/* Part 2, 12.2.4: TPMT_PUBLIC */
PACKED_TYPE(tpm_rs256_pubarea_t,
struct tpm_rs256_pubarea {
uint16_t alg; /* TPM_ALG_RSA */
uint16_t hash; /* TPM_ALG_SHA256 */
uint32_t attr;
tpm_sha256_digest_t policy; /* must be present? */
tpm_rs256_param_t param;
tpm_rs256_key_t key;
})
/* Part 2, 12.2.4: TPMT_PUBLIC */
PACKED_TYPE(tpm_es256_pubarea_t,
struct tpm_es256_pubarea {
uint16_t alg; /* TPM_ALG_ECC */
uint16_t hash; /* TPM_ALG_SHA256 */
uint32_t attr;
tpm_sha256_digest_t policy; /* must be present? */
tpm_es256_param_t param;
tpm_es256_point_t point;
})
static int
get_signed_sha1(tpm_sha1_data_t *dgst, const fido_blob_t *authdata,
const fido_blob_t *clientdata)
{
const EVP_MD *md = NULL;
EVP_MD_CTX *ctx = NULL;
int ok = -1;
if ((dgst->size = sizeof(dgst->body)) != SHA_DIGEST_LENGTH ||
(md = EVP_sha1()) == NULL ||
(ctx = EVP_MD_CTX_new()) == NULL ||
EVP_DigestInit_ex(ctx, md, NULL) != 1 ||
EVP_DigestUpdate(ctx, authdata->ptr, authdata->len) != 1 ||
EVP_DigestUpdate(ctx, clientdata->ptr, clientdata->len) != 1 ||
EVP_DigestFinal_ex(ctx, dgst->body, NULL) != 1) {
fido_log_debug("%s: sha1", __func__);
goto fail;
}
ok = 0;
fail:
EVP_MD_CTX_free(ctx);
return (ok);
}
static int
get_signed_name(tpm_sha256_name_t *name, const fido_blob_t *pubarea)
{
name->alg = TPM_ALG_SHA256;
name->size = sizeof(name->alg) + sizeof(name->body);
if (sizeof(name->body) != SHA256_DIGEST_LENGTH ||
SHA256(pubarea->ptr, pubarea->len, name->body) != name->body) {
fido_log_debug("%s: sha256", __func__);
return -1;
}
return 0;
}
static void
bswap_rs256_pubarea(tpm_rs256_pubarea_t *x)
{
x->alg = htobe16(x->alg);
x->hash = htobe16(x->hash);
x->attr = htobe32(x->attr);
x->policy.size = htobe16(x->policy.size);
x->param.symmetric = htobe16(x->param.symmetric);
x->param.scheme = htobe16(x->param.scheme);
x->param.keybits = htobe16(x->param.keybits);
x->key.size = htobe16(x->key.size);
}
static void
bswap_es256_pubarea(tpm_es256_pubarea_t *x)
{
x->alg = htobe16(x->alg);
x->hash = htobe16(x->hash);
x->attr = htobe32(x->attr);
x->policy.size = htobe16(x->policy.size);
x->param.symmetric = htobe16(x->param.symmetric);
x->param.scheme = htobe16(x->param.scheme);
x->param.curve_id = htobe16(x->param.curve_id);
x->param.kdf = htobe16(x->param.kdf);
x->point.x.size = htobe16(x->point.x.size);
x->point.y.size = htobe16(x->point.y.size);
}
static void
bswap_sha1_certinfo(tpm_sha1_attest_t *x)
{
x->magic = htobe32(x->magic);
x->type = htobe16(x->type);
x->signer.size = htobe16(x->signer.size);
x->data.size = htobe16(x->data.size);
x->name.alg = htobe16(x->name.alg);
x->name.size = htobe16(x->name.size);
}
static int
check_rs256_pubarea(const fido_blob_t *buf, const rs256_pk_t *pk)
{
const tpm_rs256_pubarea_t *actual;
tpm_rs256_pubarea_t expected;
int ok;
if (buf->len != sizeof(*actual)) {
fido_log_debug("%s: buf->len=%zu", __func__, buf->len);
return -1;
}
actual = (const void *)buf->ptr;
memset(&expected, 0, sizeof(expected));
expected.alg = TPM_ALG_RSA;
expected.hash = TPM_ALG_SHA256;
expected.attr = be32toh(actual->attr);
expected.attr &= ~(TPMA_RESERVED|TPMA_CLEAR);
expected.attr |= (TPMA_FIXED|TPMA_FIXED_P|TPMA_SENSITIVE|TPMA_SIGN);
expected.policy = actual->policy;
expected.policy.size = sizeof(expected.policy.body);
expected.param.symmetric = TPM_ALG_NULL;
expected.param.scheme = TPM_ALG_NULL;
expected.param.keybits = 2048;
expected.param.exponent = 0; /* meaning 2^16+1 */
expected.key.size = sizeof(expected.key.body);
memcpy(&expected.key.body, &pk->n, sizeof(expected.key.body));
bswap_rs256_pubarea(&expected);
ok = timingsafe_bcmp(&expected, actual, sizeof(expected));
explicit_bzero(&expected, sizeof(expected));
return ok != 0 ? -1 : 0;
}
static int
check_es256_pubarea(const fido_blob_t *buf, const es256_pk_t *pk)
{
const tpm_es256_pubarea_t *actual;
tpm_es256_pubarea_t expected;
int ok;
if (buf->len != sizeof(*actual)) {
fido_log_debug("%s: buf->len=%zu", __func__, buf->len);
return -1;
}
actual = (const void *)buf->ptr;
memset(&expected, 0, sizeof(expected));
expected.alg = TPM_ALG_ECC;
expected.hash = TPM_ALG_SHA256;
expected.attr = be32toh(actual->attr);
expected.attr &= ~(TPMA_RESERVED|TPMA_CLEAR);
expected.attr |= (TPMA_FIXED|TPMA_FIXED_P|TPMA_SENSITIVE|TPMA_SIGN);
expected.policy = actual->policy;
expected.policy.size = sizeof(expected.policy.body);
expected.param.symmetric = TPM_ALG_NULL;
expected.param.scheme = TPM_ALG_NULL; /* TCG Alg. Registry, 5.2.4 */
expected.param.curve_id = TPM_ECC_P256;
expected.param.kdf = TPM_ALG_NULL;
expected.point.x.size = sizeof(expected.point.x.body);
expected.point.y.size = sizeof(expected.point.y.body);
memcpy(&expected.point.x.body, &pk->x, sizeof(expected.point.x.body));
memcpy(&expected.point.y.body, &pk->y, sizeof(expected.point.y.body));
bswap_es256_pubarea(&expected);
ok = timingsafe_bcmp(&expected, actual, sizeof(expected));
explicit_bzero(&expected, sizeof(expected));
return ok != 0 ? -1 : 0;
}
static int
check_sha1_certinfo(const fido_blob_t *buf, const fido_blob_t *clientdata_hash,
const fido_blob_t *authdata_raw, const fido_blob_t *pubarea)
{
const tpm_sha1_attest_t *actual;
tpm_sha1_attest_t expected;
tpm_sha1_data_t signed_data;
tpm_sha256_name_t signed_name;
int ok = -1;
memset(&signed_data, 0, sizeof(signed_data));
memset(&signed_name, 0, sizeof(signed_name));
if (get_signed_sha1(&signed_data, authdata_raw, clientdata_hash) < 0 ||
get_signed_name(&signed_name, pubarea) < 0) {
fido_log_debug("%s: get_signed_sha1/name", __func__);
goto fail;
}
if (buf->len != sizeof(*actual)) {
fido_log_debug("%s: buf->len=%zu", __func__, buf->len);
goto fail;
}
actual = (const void *)buf->ptr;
memset(&expected, 0, sizeof(expected));
expected.magic = TPM_MAGIC;
expected.type = TPM_ST_CERTIFY;
expected.signer = actual->signer;
expected.signer.size = sizeof(expected.signer.alg) +
sizeof(expected.signer.body);
expected.data = signed_data;
expected.clock = actual->clock;
expected.clock.safe = 1;
expected.fwversion = actual->fwversion;
expected.name = signed_name;
expected.qual_name = actual->qual_name;
bswap_sha1_certinfo(&expected);
ok = timingsafe_bcmp(&expected, actual, sizeof(expected));
fail:
explicit_bzero(&expected, sizeof(expected));
explicit_bzero(&signed_data, sizeof(signed_data));
explicit_bzero(&signed_name, sizeof(signed_name));
return ok != 0 ? -1 : 0;
}
int
fido_get_signed_hash_tpm(fido_blob_t *dgst, const fido_blob_t *clientdata_hash,
const fido_blob_t *authdata_raw, const fido_attstmt_t *attstmt,
const fido_attcred_t *attcred)
{
const fido_blob_t *pubarea = &attstmt->pubarea;
const fido_blob_t *certinfo = &attstmt->certinfo;
if (attstmt->alg != COSE_RS1) {
fido_log_debug("%s: unsupported alg %d", __func__,
attstmt->alg);
return -1;
}
switch (attcred->type) {
case COSE_ES256:
if (check_es256_pubarea(pubarea, &attcred->pubkey.es256) < 0) {
fido_log_debug("%s: check_es256_pubarea", __func__);
return -1;
}
break;
case COSE_RS256:
if (check_rs256_pubarea(pubarea, &attcred->pubkey.rs256) < 0) {
fido_log_debug("%s: check_rs256_pubarea", __func__);
return -1;
}
break;
default:
fido_log_debug("%s: unsupported type %d", __func__,
attcred->type);
return -1;
}
if (check_sha1_certinfo(certinfo, clientdata_hash, authdata_raw,
pubarea) < 0) {
fido_log_debug("%s: check_sha1_certinfo", __func__);
return -1;
}
if (dgst->len < SHA_DIGEST_LENGTH ||
SHA1(certinfo->ptr, certinfo->len, dgst->ptr) != dgst->ptr) {
fido_log_debug("%s: sha1", __func__);
return -1;
}
dgst->len = SHA_DIGEST_LENGTH;
return 0;
}
|