-
Notifications
You must be signed in to change notification settings - Fork 2
/
sign.c
380 lines (314 loc) · 7.48 KB
/
sign.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
/* Copyright (c) 2019 by Erik Larsson
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <tss2/tss2_esys.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <stdlib.h>
#include <string.h>
#include "agent.h"
void dumpmessage(buffer_t *msg) {
uint32_t i;
for (i=0;i < msg->len;i++) {
printf("\\x%02x", msg->data[i]);
}
printf("\n");
}
const EVP_MD *hash_alg_to_evp(TPM2_ALG_ID alg) {
switch (alg) {
case TPM2_ALG_SHA1:
return EVP_sha1();
case TPM2_ALG_SHA256:
return EVP_sha256();
case TPM2_ALG_SHA384:
return EVP_sha384();
case TPM2_ALG_SHA512:
return EVP_sha512();
}
return NULL;
}
int hash(const EVP_MD *type, TPM2B_DIGEST *dig, uint8_t *data, size_t len) {
int r;
unsigned int mdsize;
EVP_MD_CTX *mdctx = NULL;
mdctx = EVP_MD_CTX_new();
if (!mdctx) {
return -1;
}
r = EVP_DigestInit(mdctx, type);
if (r != 1) {
r = -1;
goto out;
}
r = EVP_DigestUpdate(mdctx, data, len);
if (r != 1) {
r = -1;
goto out;
}
r = EVP_DigestFinal(mdctx, dig->buffer, &mdsize);
if (r != 1) {
r = -1;
goto out;
}
dig->size = mdsize;
out:
EVP_MD_CTX_free(mdctx);
return r;
}
TPM2_ALG_ID flag_to_hash_alg(uint32_t flags) {
switch (flags & 0x07) {
case 0:
return TPM2_ALG_SHA1;
case SSH_AGENT_RSA_SHA2_256:
return TPM2_ALG_SHA256;
case SSH_AGENT_RSA_SHA2_512:
return TPM2_ALG_SHA512;
}
return TPM2_ALG_NULL;
}
const char *rsa_hash_alg_to_string(TPM2_ALG_ID alg) {
switch (alg) {
case TPM2_ALG_SHA1:
return "ssh-rsa";
case TPM2_ALG_SHA256:
return "rsa-sha2-256";
case TPM2_ALG_SHA512:
return "rsa-sha2-512";
}
return NULL;
}
int append_digestinfo(buffer_t *buf, TPM2_ALG_ID alg, TPM2B_DIGEST *digest) {
int r;
static uint8_t oid_sha1[] =
{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
static uint8_t oid_sha256[] =
{ 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
static uint8_t oid_sha512[] =
{ 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
switch (alg) {
case TPM2_ALG_SHA1:
r = append_buffer(buf, oid_sha1, sizeof(oid_sha1));
break;
case TPM2_ALG_SHA256:
r = append_buffer(buf, oid_sha256, sizeof(oid_sha256));
break;
case TPM2_ALG_SHA512:
r = append_buffer(buf, oid_sha512, sizeof(oid_sha512));
break;
default:
return -1;
}
if (r) {
return r;
}
r = append_buffer(buf, digest->buffer, digest->size);
return r;
}
int add_padding(TPM2B_PUBLIC_KEY_RSA *msg, buffer_t *diginfo) {
int p, pssize;
msg->buffer[0] = 0;
msg->buffer[1] = 1;
p = 2;
pssize = msg->size - diginfo->len - 3;
memset(msg->buffer + p, 0xFF, pssize);
p = p + pssize;
msg->buffer[p] = 0;
p++;
memcpy(msg->buffer + p, diginfo->data, diginfo->len);
return 0;
}
int sign_rsa(ESYS_CONTEXT *ctx, tpm_key_t *key, uint32_t flags, uint8_t *data, size_t len, buffer_t *buf) {
int r = 0;
TPM2B_DIGEST dig = { .size = 0 };
TPM2B_PUBLIC_KEY_RSA *dmsg = NULL, msg = { .size = key->public.parameters.rsaDetail.keyBits / 8 };
TPMT_RSA_DECRYPT ds = { .scheme = TPM2_ALG_NULL };
TPM2B_DATA label = { .size = 0 };
TPM2_ALG_ID hashalg = TPM2_ALG_NULL;
buffer_t *msgbuf = NULL;
const EVP_MD *evp = NULL;
const char *algstr = NULL;
hashalg = flag_to_hash_alg(flags);
evp = hash_alg_to_evp(hashalg);
if (!evp) {
r = -1;
goto out;
}
r = hash(evp, &dig, data, len);
if (r != 1) {
r = -1;
goto out;
}
msgbuf = new_buffer();
if (!msgbuf) {
goto out;
}
r = append_digestinfo(msgbuf, hashalg, &dig);
if (r) {
goto out;
}
r = add_padding(&msg, msgbuf);
if (r) {
goto out;
}
r = Esys_RSA_Decrypt(ctx, key->handle, ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
&msg, &ds, &label, &dmsg);
algstr = rsa_hash_alg_to_string(hashalg);
if (!algstr) {
r = -1;
goto out;
}
r = buf_add_string(buf, algstr);
if (r) {
goto out;
}
r = buf_add_data(buf, dmsg->buffer, dmsg->size);
out:
free_buffer(msgbuf);
return r;
}
char *curve_to_string(TPMI_ECC_CURVE curveID) {
switch (curveID) {
case TPM2_ECC_NIST_P192:
return "ecdsa-sha2-nistp192";
case TPM2_ECC_NIST_P224:
return "ecdsa-sha2-nistp224";
case TPM2_ECC_NIST_P256:
return "ecdsa-sha2-nistp256";
case TPM2_ECC_NIST_P384:
return "ecdsa-sha2-nistp384";
case TPM2_ECC_NIST_P521:
return "ecdsa-sha2-nistp521";
}
return NULL;
}
TPM2_ALG_ID curve_to_hash_alg(TPMI_ECC_CURVE curveID) {
switch (curveID) {
case TPM2_ECC_NIST_P192:
case TPM2_ECC_NIST_P224:
case TPM2_ECC_NIST_P256:
return TPM2_ALG_SHA256;
case TPM2_ECC_NIST_P384:
return TPM2_ALG_SHA384;
case TPM2_ECC_NIST_P521:
return TPM2_ALG_SHA512;
}
return TPM2_ALG_NULL;
}
int sign_ecdsa(ESYS_CONTEXT *ctx, tpm_key_t *key, uint8_t *data, size_t len, buffer_t *buf) {
int r = 0;
TPM2B_DIGEST dig = { .size = 0 };
TPMT_SIG_SCHEME scheme = { .scheme = TPM2_ALG_ECDSA };
TPMT_TK_HASHCHECK hck = { .tag = TPM2_ST_HASHCHECK, .hierarchy = TPM2_RH_NULL, .digest.size = 0 };
TPMT_SIGNATURE *sig = NULL;
buffer_t *sigbuf = NULL;
char *curvestr = NULL;
const EVP_MD *evp = NULL;
sigbuf = new_buffer();
if (!sigbuf) {
r = -1;
goto out;
}
scheme.details.ecdsa.hashAlg = curve_to_hash_alg(key->public.parameters.eccDetail.curveID);
evp = hash_alg_to_evp(scheme.details.ecdsa.hashAlg);
if (!evp) {
r = -1;
goto out;
}
r = hash(evp, &dig, data, len);
if (r != 1) {
goto out;
}
r = Esys_Sign(ctx, key->handle, ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
&dig, &scheme, &hck, &sig);
if (r) {
goto out;
}
curvestr = curve_to_string(key->public.parameters.eccDetail.curveID);
if (!curvestr) {
r = -1;
goto out;
}
r = buf_add_string(buf, curvestr);
if (r) {
goto out;
}
r = buf_add_mpint(sigbuf, sig->signature.ecdsa.signatureR.buffer, sig->signature.ecdsa.signatureR.size);
if (r) {
goto out;
}
r = buf_add_mpint(sigbuf, sig->signature.ecdsa.signatureS.buffer, sig->signature.ecdsa.signatureS.size);
if (r) {
goto out;
}
r = buf_add_data(buf, sigbuf->data, sigbuf->len);
out:
if (sig) {
free(sig);
}
if (sigbuf) {
free_buffer(sigbuf);
}
return r;
}
int handle_signreq(context_t *ctx, buffer_t *msg, buffer_t *resp) {
int r;
uint32_t kblen, dlen, flags;
uint8_t *keyblob, *data = NULL;
buffer_t *sdata = NULL;
tpm_key_t *key = NULL;
r = buf_get_data(msg, &keyblob, &kblen);
if (r) {
goto out;
}
key = get_key_by_keyblob(ctx->keys, keyblob, kblen);
if (!key) {
goto out;
}
r = buf_get_data(msg, &data, &dlen);
if (r) {
goto out;
}
r = buf_get_uint32(msg, &flags);
if (r) {
goto out;
}
sdata = new_buffer();
if (!sdata) {
goto out;
}
switch (key->public.type) {
case TPM2_ALG_RSA:
INFO("RSA signing with %u as flags", flags);
r = sign_rsa(ctx->esys, key, flags, data, dlen, sdata);
if (r) {
goto out;
}
break;
case TPM2_ALG_ECC:
r = sign_ecdsa(ctx->esys, key, data, dlen, sdata);
if (r) {
goto out;
}
break;
default:
goto out;
}
r = buf_add_data(resp, sdata->data, sdata->len);
if (r) {
goto out;
}
out:
if (data) {
free(data);
}
if (keyblob) {
free(keyblob);
}
if (sdata) {
free_buffer(sdata);
}
return r;
}