-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.c
197 lines (164 loc) · 3.31 KB
/
list.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
/* Copyright (c) 2019 by Erik Larsson
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <tss2/tss2_tpm2_types.h>
#include <endian.h>
#include <string.h>
#include "agent.h"
int append_rsa_key(buffer_t *buf, TPMT_PUBLIC *public) {
int r;
buffer_t *tmp;
uint32_t e;
tmp = new_buffer();
if (!tmp) {
return -1;
}
r = buf_add_string(tmp, "ssh-rsa");
if (r) {
goto err_free;
}
e = public->parameters.rsaDetail.exponent;
if (!e) {
e = 65537;
}
e = htobe32(e);
r = buf_add_mpint(tmp, (void *) &e, 4);
if (r) {
goto err_free;
}
r = buf_add_mpint(tmp, public->unique.rsa.buffer, public->unique.rsa.size);
if (r) {
goto err_free;
}
r = buf_add_data(buf, tmp->data, tmp->len);
err_free:
free_buffer(tmp);
return r;
}
int append_ecc_key(buffer_t *buf, TPMT_PUBLIC *public) {
int r = 0;
buffer_t *tmp;
uint32_t xylen;
tmp = new_buffer();
if (!tmp) {
return -1;
}
switch (public->parameters.eccDetail.curveID) {
case TPM2_ECC_NIST_P256:
r = buf_add_string(tmp, "ecdsa-sha2-nistp256");
if (r) {
goto err_free;
}
r = buf_add_string(tmp, "nistp256");
if (r) {
goto err_free;
}
break;
default:
r = -1;
goto err_free;
}
xylen = public->unique.ecc.x.size + public->unique.ecc.y.size;
r = buf_add_uint32(tmp, xylen + 1);
if (r) {
goto err_free;
}
r = buf_add_byte(tmp, 4);
if (r) {
goto err_free;
}
r = append_buffer(tmp, public->unique.ecc.x.buffer, public->unique.ecc.x.size);
if (r) {
goto err_free;
}
r = append_buffer(tmp, public->unique.ecc.y.buffer, public->unique.ecc.y.size);
if (r) {
goto err_free;
}
r = buf_add_data(buf, tmp->data, tmp->len);
err_free:
free_buffer(tmp);
return r;
}
int handle_listreq(context_t *ctx, buffer_t *msg) {
int r;
uint32_t nkeys = 0;
tpm_key_t *key = ctx->keys;
buffer_t *kbuf = NULL;
while (key) {
nkeys++;
key = key->next;
}
r = buf_add_uint32(msg, nkeys);
if (r) {
return r;
}
key = ctx->keys;
while (key) {
kbuf = new_buffer();
if (!kbuf) {
return -1;
}
switch (key->public.type) {
case TPM2_ALG_RSA:
r = append_rsa_key(kbuf, &key->public);
if (r) {
return r;
}
break;
case TPM2_ALG_ECC:
r = append_ecc_key(kbuf, &key->public);
if (r) {
return r;
}
break;
default:
return -1;
}
r = append_buffer(msg, kbuf->data, kbuf->len);
if (r) {
return r;
}
r = buf_add_string(msg, "");
if (r) {
return r;
}
key = key->next;
free_buffer(kbuf);
}
return 0;
}
tpm_key_t *get_key_by_keyblob(tpm_key_t *keys, uint8_t *blob, uint32_t len) {
int r;
buffer_t *buf = NULL;
tpm_key_t *key = NULL;
while (keys) {
buf = new_buffer();
switch (keys->public.type) {
case TPM2_ALG_RSA:
r = append_rsa_key(buf, &keys->public);
if (r) {
goto out;
}
break;
case TPM2_ALG_ECC:
r = append_ecc_key(buf, &keys->public);
if (r) {
goto out;
}
break;
default:
break;
}
if ((buf->len - 4) == len && !memcmp(blob, buf->data + 4, len)) {
key = keys;
goto out;
}
free_buffer(buf);
buf = NULL;
keys = keys->next;
}
out:
free_buffer(buf);
return key;
}