-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniris-yubikey.c
414 lines (350 loc) · 11.4 KB
/
uniris-yubikey.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
/*******************************************************************************
* Archethic Yubikey Library
* (c) 2021 Varun Deshpande, Uniris
*
* Licensed under the GNU Affero General Public License, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/agpl-3.0.en.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ykpiv/ykpiv.h>
#include <openssl/x509.h>
#include "uniris-yubikey.h"
#define PRIME_LEN 32
ykpiv_rc rc;
static ykpiv_state *g_state;
static BYTE rsa_root_key[300] = {0};
static INT rsa_key_len;
static BYTE ecc_public_key[65] = {0};
static INT ecc_key_len;
static BYTE sigEccASN[2 + 2 + PRIME_LEN + 2 + PRIME_LEN + 2] = {0};
static size_t asnSignSize = sizeof(sigEccASN);
static BYTE ecdhPoint[PRIME_LEN] = {0};
static size_t ecdhPointLen = PRIME_LEN;
static BYTE ykCertificate[2048] = {0};
static size_t ykCertificateLen = 2048;
static INT _archEthicIndex = 0;
static BYTE _ykIndex = 0;
BYTE key_slots[] = {0x82, 0x83, 0x84, 0x85,
0x86, 0x87, 0x88, 0x89,
0x8a, 0x8b, 0x8c, 0x8d,
0x8e, 0x8f, 0x90, 0x91,
0x92, 0x93, 0x94, 0x95};
unsigned int key_certificates[] = {0x5fc10d, 0x5fc10e, 0x5fc10f, 0x5fc110,
0x5fc111, 0x5fc112, 0x5fc113, 0x5fc114,
0x5fc115, 0x5fc116, 0x5fc117, 0x5fc118,
0x5fc119, 0x5fc11a, 0x5fc11b, 0x5fc11c,
0x5fc11d, 0x5fc11e, 0x5fc11f, 0x5fc120};
void initializeYK()
{
/* Initialize */
rc = ykpiv_init(&g_state, true);
if (rc != 0)
{
printf("Initialization Failed, Error Code: %d\n", rc);
}
/* Connect */
rc = ykpiv_connect(g_state, NULL);
if (rc != 0)
{
printf("Connection Failed, Error Code: %d\n", rc);
}
_archEthicIndex = getArchEthicIndex();
_ykIndex = getYKIndex();
}
bool checkYK()
{
char list[100];
size_t list_len = 100;
ykpiv_list_readers(g_state, list, &list_len);
if (strstr(list, "Yubico YubiKey") != NULL)
return true;
else
return false;
}
void verifyPinYK()
{
rc = ykpiv_verify(g_state, "123456", NULL);
if (rc != 0)
{
printf("Pin Authentication Failed, Error Code: %d\n", rc);
}
}
void authenticateYK()
{
const char *mgm_key = "010203040506070801020304050607080102030405060708";
BYTE key[24];
size_t key_len = sizeof(key);
rc = ykpiv_hex_decode(mgm_key, strlen(mgm_key), key, &key_len);
if (rc != 0)
{
printf("Hex decode failed, Error Code: %d\n", rc);
}
/* Authenticate with the MGM Key */
rc = ykpiv_authenticate(g_state, key);
if (rc != 0)
{
printf("MGM Key Verification Failed, Error Code: %d\n", rc);
}
}
void fetchKey(BYTE ykIndex)
{
BYTE certi_yk[2048] = {0};
size_t yk_attest_len = sizeof(certi_yk);
ykpiv_fetch_object(g_state, key_certificates[ykIndex], certi_yk, &yk_attest_len);
const BYTE *data = certi_yk;
X509 *cert = d2i_X509(NULL, &data, yk_attest_len);
if (!cert)
{
printf("Error Parsing Certificate\n");
}
struct asn1_string_st *mykey = X509_get0_pubkey_bitstr(cert);
memcpy(ecc_public_key, mykey->data, mykey->length);
memcpy(&ecc_key_len, &mykey->length, sizeof(mykey->length));
X509_free(cert);
}
void generateKey(BYTE ykIndex)
{
/* ECC Public Point **/
uint8_t *point = NULL;
size_t point_len;
/* Key Generation */
rc = ykpiv_util_generate_key(g_state,
key_slots[ykIndex],
YKPIV_ALGO_ECCP256,
YKPIV_PINPOLICY_NEVER,
YKPIV_TOUCHPOLICY_NEVER,
NULL,
NULL,
NULL,
NULL,
&point,
&point_len);
if (rc != 0)
{
printf("Error generating key, Error Code: %d\n", rc);
}
printf("Saved %d : ", ykIndex);
for (int j = 0; j < point_len; j++)
{
printf("%02x", point[j]);
}
printf("\n");
ykpiv_util_free(g_state, point);
}
void generateCertificate(BYTE ykIndex)
{
/* Certificate Generation */
BYTE attest[2048] = {0};
size_t attest_len = sizeof(attest);
rc = ykpiv_attest(g_state, key_slots[ykIndex], attest, &attest_len);
rc = ykpiv_save_object(g_state, key_certificates[ykIndex], attest, attest_len);
if (rc != 0)
{
printf("Error saving certificate, Error Code: %d\n", rc);
}
}
BYTE getYKIndex()
{
// Check why extra 2 bytes are needed?
BYTE index_yk[5] = {0};
size_t index_length = sizeof(index_yk);
rc = ykpiv_fetch_object(g_state, YKPIV_OBJ_KEY_HISTORY, index_yk, &index_length);
if (rc != 0)
{
printf("Fetch Unsuccessful, Error Code: %d\n", rc);
}
return index_yk[0];
}
void saveIndex(BYTE ykIndex, INT archEthicIndex)
{
BYTE index_raw[3] = {0};
index_raw[0] = ykIndex;
// big endian
index_raw[1] = archEthicIndex >> 8;
index_raw[2] = archEthicIndex;
rc = ykpiv_save_object(g_state, YKPIV_OBJ_KEY_HISTORY, index_raw, sizeof(index_raw));
if (rc != 0)
{
printf("Error saving Index, Error Code: %d\n", rc);
}
printf("YK Index: %d, ArchEthic Index: %d\n", ykIndex, archEthicIndex);
}
void signECDSA(BYTE *hashToSign, BYTE ykIndex)
{
asnSignSize = 2 + 2 + PRIME_LEN + 2 + PRIME_LEN + 2;
/* Sign Data */
rc = ykpiv_sign_data(g_state, hashToSign, PRIME_LEN, sigEccASN, &asnSignSize, YKPIV_ALGO_ECCP256, key_slots[ykIndex]);
if (rc != 0)
{
printf("ECDSA Signing Failed, Error Code: %d\n", rc);
}
}
void getECDHPoint(BYTE ykIndex, BYTE *euphemeralKey)
{
ecdhPointLen = PRIME_LEN;
rc = ykpiv_decipher_data(g_state, euphemeralKey, 65, ecdhPoint, &ecdhPointLen, YKPIV_ALGO_ECCP256, key_slots[ykIndex]);
if (rc != 0)
{
printf("ECDH Exchange Failed, Error Code: %d\n", rc);
}
}
INT getArchEthicIndex()
{
// Check why extra 2 bytes are needed?
BYTE index_yk[5] = {0};
size_t index_length = sizeof(index_yk);
rc = ykpiv_fetch_object(g_state, YKPIV_OBJ_KEY_HISTORY, index_yk, &index_length);
if (rc != 0)
{
printf("Fetch Unsuccessful, Error Code: %d\n", rc);
}
INT archEthicIndex;
archEthicIndex = index_yk[1] << 8;
archEthicIndex += index_yk[2];
return archEthicIndex;
}
bool incrementIndex()
{
BYTE newYKIndex = (_ykIndex + 1) % 20;
INT newArchEthicIndex = _archEthicIndex + 1;
authenticateYK();
generateKey(newYKIndex);
generateCertificate(newYKIndex);
saveIndex(newYKIndex, newArchEthicIndex);
if (getYKIndex() == newYKIndex && getArchEthicIndex() == newArchEthicIndex)
{
_archEthicIndex = newArchEthicIndex;
_ykIndex = newYKIndex;
return true;
}
else
return false;
}
BYTE *getRootKey(INT *publicKeySize)
{
BYTE *pb = 0;
size_t pblen = 0;
ykpiv_util_read_cert(g_state, 0xf9, &pb, &pblen);
const BYTE *data = pb;
X509 *cert = d2i_X509(NULL, &data, pblen);
if (!cert)
{
printf("Error Parsing Certificate\n");
}
struct asn1_string_st *mykey = X509_get0_pubkey_bitstr(cert);
memcpy(rsa_root_key, mykey->data, mykey->length);
memcpy(&rsa_key_len, &mykey->length, sizeof(mykey->length));
X509_free(cert);
memcpy(publicKeySize, &rsa_key_len, sizeof(rsa_key_len));
ykpiv_util_free(g_state, pb);
return rsa_root_key;
}
BYTE *getCurrentKey(INT *publicKeySize)
{
INT previous_key_index = (_ykIndex - 1 + 20) % 20;
fetchKey(previous_key_index);
memcpy(publicKeySize, &ecc_key_len, sizeof(ecc_key_len));
return ecc_public_key;
}
BYTE *getNextKey(INT *publicKeySize)
{
fetchKey(_ykIndex);
memcpy(publicKeySize, &ecc_key_len, sizeof(ecc_key_len));
return ecc_public_key;
}
BYTE *getPastKey(INT archEthicIndex, INT *publicKeySize)
{
INT offset = _archEthicIndex - archEthicIndex;
if (offset > 19 || offset < 0)
return NULL;
INT slotPosition = (_ykIndex - offset + 20) % 20;
fetchKey(slotPosition);
memcpy(publicKeySize, &ecc_key_len, sizeof(ecc_key_len));
return ecc_public_key;
}
BYTE *getRootCertificate(INT *certificateSize)
{
BYTE *pb = 0;
ykCertificateLen = 2048;
ykpiv_util_read_cert(g_state, 0xf9, &pb, &ykCertificateLen);
memcpy(ykCertificate, pb, ykCertificateLen);
memcpy(certificateSize, &ykCertificateLen, sizeof(ykCertificateLen));
ykpiv_util_free(g_state, pb);
return ykCertificate;
}
BYTE *getCurrentCertificate(INT *certificateSize)
{
ykCertificateLen = 2048;
INT currentKeyIndex = (_ykIndex - 1 + 20) % 20;
ykpiv_fetch_object(g_state, key_certificates[currentKeyIndex], ykCertificate, &ykCertificateLen);
memcpy(certificateSize, &ykCertificateLen, sizeof(ykCertificateLen));
return ykCertificate;
}
BYTE *getNextCertificate(INT *certificateSize)
{
ykCertificateLen = 2048;
ykpiv_fetch_object(g_state, key_certificates[_ykIndex], ykCertificate, &ykCertificateLen);
memcpy(certificateSize, &ykCertificateLen, sizeof(ykCertificateLen));
return ykCertificate;
}
BYTE *getPastCertificate(INT archEthicIndex, INT *certificateSize)
{
ykCertificateLen = 2048;
INT offset = _archEthicIndex - archEthicIndex;
if (offset > 19 || offset < 0)
return NULL;
INT slotPosition = (_ykIndex - offset + 20) % 20;
ykpiv_fetch_object(g_state, key_certificates[slotPosition], ykCertificate, &ykCertificateLen);
memcpy(certificateSize, &ykCertificateLen, sizeof(ykCertificateLen));
return ykCertificate;
}
BYTE *signCurrentKey(BYTE *hashToSign, INT *eccSignSize)
{
INT currentKeyIndex = (_ykIndex - 1 + 20) % 20;
verifyPinYK();
signECDSA(hashToSign, currentKeyIndex);
// Prevent memory overwrites
INT signLength = asnSignSize;
memcpy(eccSignSize, &signLength, sizeof(signLength));
return sigEccASN;
}
BYTE *signPastKey(INT archEthicIndex, BYTE *hashToSign, INT *eccSignSize)
{
INT offset = _archEthicIndex - archEthicIndex;
if (offset > 19 || offset < 0)
return NULL;
INT slotPosition = (_ykIndex - offset + 20) % 20;
verifyPinYK();
signECDSA(hashToSign, slotPosition);
memcpy(eccSignSize, &asnSignSize, sizeof(asnSignSize));
return sigEccASN;
}
BYTE *ecdhCurrentKey(BYTE *euphemeralKey, INT *eccPointSize)
{
INT currentKeyIndex = (_ykIndex - 1 + 20) % 20;
verifyPinYK();
getECDHPoint(currentKeyIndex, euphemeralKey);
memcpy(eccPointSize, &ecdhPointLen, sizeof(ecdhPointLen));
return ecdhPoint;
}
BYTE *ecdhPastKey(INT archEthicIndex, BYTE *euphemeralKey, INT *eccPointSize)
{
INT offset = _archEthicIndex - archEthicIndex;
if (offset > 19 || offset < 0)
return NULL;
INT slotPosition = (_ykIndex - offset + 20) % 20;
verifyPinYK();
getECDHPoint(slotPosition, euphemeralKey);
memcpy(eccPointSize, &ecdhPointLen, sizeof(ecdhPointLen));
return ecdhPoint;
}