-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.c
More file actions
658 lines (593 loc) · 19.6 KB
/
parse.c
File metadata and controls
658 lines (593 loc) · 19.6 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* Routines to parse pgp key/signature packets into a pgprItem
*/
#include <string.h>
#include "pgpr_internal.h"
typedef struct pgprPktKeyV3_s {
uint8_t version; /*!< version number (3). */
uint8_t time[4]; /*!< time that the key was created. */
uint8_t expdays[2]; /*!< time in days when the key expires */
uint8_t pubkey_algo; /*!< public key algorithm. */
} * pgprPktKeyV3;
typedef struct pgprPktKeyV4_s {
uint8_t version; /*!< version number (4). */
uint8_t time[4]; /*!< time that the key was created. */
uint8_t pubkey_algo; /*!< public key algorithm. */
} * pgprPktKeyV4;
typedef struct pgprPktKeyV56_s {
uint8_t version; /*!< version number (5, 6). */
uint8_t time[4]; /*!< time that the key was created. */
uint8_t pubkey_algo; /*!< public key algorithm. */
uint8_t pubkey_len[4]; /*!< public key material length. */
} * pgprPktKeyV56;
typedef struct pgprPktSigV3_s {
uint8_t version; /*!< version number (3). */
uint8_t hashlen; /*!< length of following hashed material. MUST be 5. */
uint8_t sigtype; /*!< signature type. */
uint8_t time[4]; /*!< time that the key was created. */
pgprKeyID_t keyid; /*!< key ID of signer. */
uint8_t pubkey_algo; /*!< public key algorithm. */
uint8_t hash_algo; /*!< hash algorithm. */
uint8_t signhash16[2]; /*!< left 16 bits of signed hash value. */
} * pgprPktSigV3;
typedef struct pgprPktSigV456_s {
uint8_t version; /*!< version number (4, 5, 6). */
uint8_t sigtype; /*!< signature type. */
uint8_t pubkey_algo; /*!< public key algorithm. */
uint8_t hash_algo; /*!< hash algorithm. */
uint8_t hashlen[2]; /*!< length of following hashed material (4 bytes for v6). */
} * pgprPktSigV456;
static inline unsigned int pgprGrab2(const uint8_t *s)
{
return s[0] << 8 | s[1];
}
static inline unsigned int pgprGrab4(const uint8_t *s)
{
return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
}
static uint8_t curve_oids[] = {
PGPRCURVE_NIST_P_256, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
PGPRCURVE_NIST_P_384, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22,
PGPRCURVE_NIST_P_521, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
PGPRCURVE_BRAINPOOL_P256R1, 0x09, 0x2b, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07,
PGPRCURVE_BRAINPOOL_P384R1, 0x09, 0x2b, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0b,
PGPRCURVE_BRAINPOOL_P512R1, 0x09, 0x2b, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0d,
PGPRCURVE_ED25519, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0xda, 0x47, 0x0f, 0x01,
PGPRCURVE_CURVE25519, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01,
PGPRCURVE_ED25519, 0x03, 0x2b, 0x65, 0x70, /* alternative oid */
PGPRCURVE_CURVE25519, 0x03, 0x2b, 0x65, 0x6e, /* alternative oid */
PGPRCURVE_ED448, 0x03, 0x2b, 0x65, 0x71,
PGPRCURVE_X448, 0x03, 0x2b, 0x65, 0x6f,
0,
};
static int pgprCurveByOid(const uint8_t *p, int l)
{
uint8_t *curve;
for (curve = curve_oids; *curve; curve += 2 + curve[1])
if (l == (int)curve[1] && !memcmp(p, curve + 2, l))
return (int)curve[0];
return 0;
}
/*
* Key/Signature algorithm parameter handling
*/
static pgprRC pgprParseKeyParams(pgprPkt *pkt, pgprItem item)
{
pgprRC rc;
const uint8_t *p;
int curve = 0;
if (item->tag != PGPRTAG_PUBLIC_KEY && item->tag != PGPRTAG_PUBLIC_SUBKEY)
return PGPR_ERROR_INTERNAL;
/* We can't handle more than one key at a time */
if (item->alg || !item->mpi_offset || item->mpi_offset > pkt->blen)
return PGPR_ERROR_INTERNAL;
p = pkt->body + item->mpi_offset;
if (item->pubkey_algo == PGPRPUBKEYALGO_EDDSA || item->pubkey_algo == PGPRPUBKEYALGO_ECDSA) {
size_t plen = pkt->blen - item->mpi_offset;
int len = plen > 0 ? p[0] : 0;
if (len == 0 || len == 0xff || len + 1 > plen)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
curve = pgprCurveByOid(p + 1, len);
if (!curve)
return PGPR_ERROR_UNSUPPORTED_CURVE;
p += len + 1;
}
item->alg = pgprAlgNew();
if (!item->alg)
return PGPR_ERROR_NO_MEMORY;
rc = pgprAlgSetupPubkey(item->alg, item->pubkey_algo, curve, p, pkt->body + pkt->blen);
return rc;
}
pgprRC pgprParseSigParams(pgprPkt *pkt, pgprItem item)
{
pgprRC rc;
if (item->tag != PGPRTAG_SIGNATURE)
return PGPR_ERROR_INTERNAL;
/* We can't handle more than one sig at a time */
if (item->alg || !item->mpi_offset || item->mpi_offset > pkt->blen)
return PGPR_ERROR_INTERNAL;
item->alg = pgprAlgNew();
if (!item->alg)
return PGPR_ERROR_NO_MEMORY;
rc = pgprAlgSetupSignature(item->alg, item->pubkey_algo, pkt->body + item->mpi_offset, pkt->body + pkt->blen);
if (rc != PGPR_OK)
item->alg = pgprAlgFree(item->alg);
return rc;
}
/** \ingroup pgpr
* Decode length from 1, 2, or 5 octet body length encoding, used in
* signature subpackets. Note that this is slightly different from
* the pgprNewLen function.
* @param s pointer to subpacket (including tag)
* @param slen buffer size
* @param[out] *lenp decoded length
* @return subpacket header length (excluding type), 0 on error
*/
static inline size_t pgprSubPktLen(const uint8_t *s, size_t slen, size_t *lenp)
{
size_t dlen, hlen;
if (slen > 0 && *s < 192) {
hlen = 1;
dlen = *s;
} else if (slen > 2 && *s < 255) {
hlen = 2;
dlen = (((s[0]) - 192) << 8) + s[1] + 192;
} else if (slen > 5 && *s == 255 && s[1] == 0) {
hlen = 5;
dlen = s[2] << 16 | s[3] << 8 | s[4];
} else {
return 0;
}
if (slen - hlen < dlen)
return 0;
*lenp = dlen;
return hlen;
}
static pgprRC pgprParseSigSubPkts(const uint8_t *s, size_t slen, pgprItem item, int hashed)
{
const uint8_t *p = s;
while (slen > 0) {
size_t blen = 0, hlen;
int understood = 0, tag;
hlen = pgprSubPktLen(p, slen, &blen);
if (hlen == 0 || blen < 1 || hlen + blen > slen)
break;
/* skip header */
p += hlen;
slen -= hlen;
/* remove tag from body */
tag = *p++;
blen--;
slen--;
switch (tag & ~PGPRSUBTYPE_CRITICAL) {
case PGPRSUBTYPE_SIG_CREATE_TIME:
if (!hashed)
break; /* RFC 4880 §5.2.3.4 creation time MUST be hashed */
if (blen != 4)
break; /* other lengths not understood */
if (item->saved & PGPRITEM_SAVED_TIME)
return PGPR_ERROR_DUPLICATE_DATA;
understood = 1;
item->time = pgprGrab4(p);
item->saved |= PGPRITEM_SAVED_TIME;
break;
case PGPRSUBTYPE_ISSUER_KEYID:
if (blen != sizeof(item->keyid))
break; /* other lengths not understood */
understood = 1;
if (!(item->saved & PGPRITEM_SAVED_ID)) {
memcpy(item->keyid, p, sizeof(item->keyid));
item->saved |= PGPRITEM_SAVED_ID;
}
break;
case PGPRSUBTYPE_ISSUER_FINGERPRINT:
if (blen < 17)
break;
understood = 1;
if (!(item->saved & PGPRITEM_SAVED_FP) && blen - 1 <= PGPR_MAX_FP_LENGTH) {
if ((p[0] == 4 && blen == 21) || ((p[0] == 5 || p[0] == 6) && blen == 33)) {
memcpy(item->fp, p + 1, blen - 1);
item->fp_len = blen - 1;
item->fp_version = p[0];
item->saved |= PGPRITEM_SAVED_FP;
}
}
if (p[0] == 4 && blen == 21 && !(item->saved & PGPRITEM_SAVED_ID)) {
memcpy(item->keyid, p + blen - sizeof(item->keyid), sizeof(item->keyid));
item->saved |= PGPRITEM_SAVED_ID;
}
if ((p[0] == 5 || p[0] == 6) && blen == 33 && !(item->saved & PGPRITEM_SAVED_ID)) {
memcpy(item->keyid, p + 1, sizeof(item->keyid));
item->saved |= PGPRITEM_SAVED_ID;
}
break;
case PGPRSUBTYPE_KEY_FLAGS:
if (!hashed)
break; /* Subpackets in the unhashed section cannot be trusted */
if (item->saved & PGPRITEM_SAVED_KEY_FLAGS)
return PGPR_ERROR_DUPLICATE_DATA;
understood = 1;
item->key_flags = blen >= 1 ? p[0] : 0; /* we're just storing the first 8 flags */
item->saved |= PGPRITEM_SAVED_KEY_FLAGS;
break;
case PGPRSUBTYPE_KEY_EXPIRE_TIME:
if (!hashed)
break; /* Subpackets in the unhashed section cannot be trusted */
if (blen != 4)
break; /* other lengths not understood */
if (item->saved & PGPRITEM_SAVED_KEY_EXPIRE)
return PGPR_ERROR_DUPLICATE_DATA;
understood = 1;
item->key_expire = pgprGrab4(p);
item->saved |= PGPRITEM_SAVED_KEY_EXPIRE;
break;
case PGPRSUBTYPE_SIG_EXPIRE_TIME:
if (!hashed)
break; /* RFC 4880 §5.2.3.4 creation time MUST be hashed */
if (blen != 4)
break; /* other lengths not understood */
if (item->saved & PGPRITEM_SAVED_SIG_EXPIRE)
return PGPR_ERROR_DUPLICATE_DATA;
understood = 1;
item->sig_expire = pgprGrab4(p);
item->saved |= PGPRITEM_SAVED_SIG_EXPIRE;
break;
case PGPRSUBTYPE_EMBEDDED_SIG:
if (item->sigtype != PGPRSIGTYPE_SUBKEY_BINDING)
break; /* do not bother for other types */
if (blen < 6)
break; /* obviously not a signature */
if (item->embedded_sig)
break; /* just store the first one. we may need to changed this to select the most recent. */
understood = 1;
item->embedded_sig_len = blen;
item->embedded_sig = pgprMemdup(p, blen);
if (!item->embedded_sig)
return PGPR_ERROR_NO_MEMORY;
break;
case PGPRSUBTYPE_PRIMARY_USERID:
if (!hashed)
break; /* Subpackets in the unhashed section cannot be trusted */
if (blen != 1)
break; /* other lengths not understood */
understood = 1;
if (p[0])
item->saved |= PGPRITEM_SAVED_PRIMARY;
break;
default:
break;
}
if (!understood && (tag & PGPRSUBTYPE_CRITICAL))
return PGPR_ERROR_UNKNOWN_CRITICAL_PKT;
p += blen;
slen -= blen;
}
if (slen != 0)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
return PGPR_OK;
}
/* create the trailer used in v4/5/6 signatures */
static pgprRC create_sig_trailer(pgprItem item, const uint8_t *p, size_t plen)
{
if (item->version == 4 || item->version == 6)
item->hashlen = plen + 6;
else if (item->version == 5)
item->hashlen = plen + (item->sigtype == PGPRSIGTYPE_BINARY || item->sigtype == PGPRSIGTYPE_TEXT ? 6 : 0) + 10;
else
return PGPR_ERROR_UNSUPPORTED_VERSION;
item->hash = pgprCalloc(1, item->hashlen);
if (!item->hash)
return PGPR_ERROR_NO_MEMORY;
memcpy(item->hash, p, plen);
if (item->version == 4 || item->version == 6) {
uint8_t *trailer = item->hash + item->hashlen - 6;
trailer[0] = item->version;
trailer[1] = 0xff;
trailer[2] = plen >> 24;
trailer[3] = plen >> 16;
trailer[4] = plen >> 8;
trailer[5] = plen;
} else if (item->version == 5) {
uint8_t *trailer = item->hash + item->hashlen - 10;
trailer[0] = 0x05;
trailer[1] = 0xff;
trailer[6] = plen >> 24;
trailer[7] = plen >> 16;
trailer[8] = plen >> 8;
trailer[9] = plen;
}
return PGPR_OK;
}
static pgprRC create_sig_salt(pgprItem item, const uint8_t *salt, size_t saltlen)
{
uint8_t *newhash = pgprRealloc(item->hash, item->hashlen + saltlen);
if (!newhash)
return PGPR_ERROR_NO_MEMORY;
item->hash = newhash;
memcpy(item->hash + item->hashlen, salt, saltlen);
item->saltlen = saltlen;
return PGPR_OK;
}
pgprRC pgprParseSigNoParams(pgprPkt *pkt, pgprItem item)
{
pgprRC rc = PGPR_ERROR_CORRUPT_PGP_PACKET; /* assume failure */
if (item->version || item->saved)
return PGPR_ERROR_INTERNAL;
if (item->tag != PGPRTAG_SIGNATURE || pkt->tag != item->tag)
return PGPR_ERROR_INTERNAL;
if (pkt->blen == 0)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->version = pkt->body[0];
switch (item->version) {
case 3:
{ pgprPktSigV3 v = (pgprPktSigV3)pkt->body;
if (pkt->blen <= sizeof(*v) || v->hashlen != 5)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->hashlen = v->hashlen;
item->sigtype = v->sigtype;
item->hash = pgprMemdup(&v->sigtype, v->hashlen);
if (!item->hash)
return PGPR_ERROR_NO_MEMORY;
item->time = pgprGrab4(v->time);
if (!item->time)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
memcpy(item->keyid, v->keyid, sizeof(item->keyid));
item->saved = PGPRITEM_SAVED_TIME | PGPRITEM_SAVED_ID;
item->pubkey_algo = v->pubkey_algo;
item->hash_algo = v->hash_algo;
memcpy(item->signhash16, v->signhash16, sizeof(item->signhash16));
item->mpi_offset = sizeof(*v);
rc = PGPR_OK;
} break;
case 4:
case 5:
case 6:
{ pgprPktSigV456 v = (pgprPktSigV456)pkt->body;
const uint8_t *const hend = pkt->body + pkt->blen;
const uint8_t *p;
size_t plen;
int hashed;
if (pkt->blen <= sizeof(*v))
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->sigtype = v->sigtype;
item->pubkey_algo = v->pubkey_algo;
item->hash_algo = v->hash_algo;
/* parse both the hashed and unhashed subpackets */
p = &v->hashlen[0];
for (hashed = 1; hashed >= 0; hashed--) {
if (item->version == 6) {
if (p > hend || hend - p < 4)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
plen = pgprGrab4(p);
p += 4;
} else {
if (p > hend || hend - p < 2)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
plen = pgprGrab2(p);
p += 2;
}
if (plen >= PGPR_MAX_OPENPGP_BYTES || hend - p < plen)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
if (hashed) {
rc = create_sig_trailer(item, pkt->body, sizeof(*v) + (item->version == 6 ? 2 : 0) + plen);
if (rc != PGPR_OK)
return rc;
}
rc = pgprParseSigSubPkts(p, plen, item, hashed);
if (rc != PGPR_OK)
return rc;
p += plen;
}
if (!(item->saved & PGPRITEM_SAVED_TIME))
return PGPR_ERROR_NO_CREATION_TIME; /* RFC 4880 §5.2.3.4 creation time MUST be present */
if (!item->time)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
if (p > hend || hend - p < 2)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
memcpy(item->signhash16, p, sizeof(item->signhash16));
p += 2;
if (item->version == 6) {
int saltlen;
if (p > hend || hend - p < 1)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
saltlen = p[0];
if (saltlen) {
if (hend - p < 1 + saltlen)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
rc = create_sig_salt(item, p + 1, saltlen);
if (rc != PGPR_OK)
return rc;
}
p += 1 + saltlen;
}
if (p > hend)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->mpi_offset = p - pkt->body;
rc = PGPR_OK;
} break;
default:
rc = PGPR_ERROR_UNSUPPORTED_VERSION;
break;
}
return rc;
}
pgprRC pgprParseSig(pgprPkt *pkt, pgprItem item)
{
pgprRC rc = pgprParseSigNoParams(pkt, item);
if (rc == PGPR_OK)
rc = pgprParseSigParams(pkt, item);
return rc;
}
static inline int pgprMpiLen(const uint8_t *p)
{
int mpi_bits = (p[0] << 8) | p[1];
return 2 + ((mpi_bits + 7) >> 3);
}
static pgprRC pgprParseKeyFp_V3(pgprPkt *pkt, pgprItem item)
{
pgprRC rc;
pgprDigCtx ctx = NULL;
uint8_t *out = NULL;
size_t outlen = 0;
const uint8_t *p = pkt->body;
size_t blen = pkt->blen;
int mpil1, mpil2;
/* make sure this is a v3 rsa key (4: 2 * mpi size) */
if (blen < sizeof(struct pgprPktKeyV3_s) + 2 + PGPR_KEYID_LEN + 2 || p[0] != 3 || p[7] != PGPRPUBKEYALGO_RSA)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
/* find the two rsa mpis */
p += sizeof(struct pgprPktKeyV3_s);
blen -= sizeof(struct pgprPktKeyV3_s);
mpil1 = pgprMpiLen(p);
if (mpil1 < 2 + PGPR_KEYID_LEN || blen < mpil1 + 2)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
mpil2 = pgprMpiLen(p + mpil1);
if (mpil2 < 2 + 1 || blen != mpil1 + mpil2)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
rc = pgprDigestInit(PGPRHASHALGO_MD5, &ctx);
if (rc != PGPR_OK)
return rc;
pgprDigestUpdate(ctx, p + 2, mpil1 - 2);
pgprDigestUpdate(ctx, p + mpil1 + 2, mpil2 - 2);
pgprDigestFinal(ctx, (void **)&out, &outlen);
if (outlen != 16) {
free(out);
return PGPR_ERROR_INTERNAL;
}
memcpy(item->fp, out, outlen);
item->fp_len = outlen;
item->fp_version = 3;
free(out);
/* calculate the keyid from the modulus */
memcpy(item->keyid, p + mpil1 - PGPR_KEYID_LEN, PGPR_KEYID_LEN);
item->saved |= PGPRITEM_SAVED_FP | PGPRITEM_SAVED_ID;
return PGPR_OK;
}
pgprRC pgprParseKeyFp(pgprPkt *pkt, pgprItem item)
{
pgprRC rc;
pgprDigCtx ctx = NULL;
uint8_t *out = NULL;
size_t outlen = 0;
int version;
size_t blen = pkt->blen;
if ((item->tag != PGPRTAG_PUBLIC_KEY && item->tag != PGPRTAG_PUBLIC_SUBKEY) || pkt->tag != item->tag)
return PGPR_ERROR_INTERNAL;
if (blen == 0)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
version = pkt->body[0];
if (version == 3)
return pgprParseKeyFp_V3(pkt, item);
if (version != 4 && version != 5 && version != 6)
return PGPR_ERROR_UNSUPPORTED_VERSION;
rc = pgprDigestInit(version == 4 ? PGPRHASHALGO_SHA1 : PGPRHASHALGO_SHA256, &ctx);
if (rc != PGPR_OK)
return rc;
if (version == 4) {
uint8_t in[3] = { 0x99, (blen >> 8), blen };
pgprDigestUpdate(ctx, in, 3);
} else {
uint8_t in[5] = { version == 6 ? 0x9b : 0x9a, (blen >> 24), (blen >> 16), (blen >> 8), blen };
pgprDigestUpdate(ctx, in, 5);
}
pgprDigestUpdate(ctx, pkt->body, blen);
pgprDigestFinal(ctx, (void **)&out, &outlen);
if (outlen != (version == 4 ? 20 : 32)) {
free(out);
return PGPR_ERROR_INTERNAL;
}
memcpy(item->fp, out, outlen);
item->fp_len = outlen;
item->fp_version = version;
free(out);
/* calculate the keyid from the fingerprint */
if (version == 4)
memcpy(item->keyid, (item->fp + (outlen - 8)), 8);
else
memcpy(item->keyid, item->fp, 8);
item->saved |= PGPRITEM_SAVED_FP | PGPRITEM_SAVED_ID;
return PGPR_OK;
}
pgprRC pgprParseKey(pgprPkt *pkt, pgprItem item)
{
pgprRC rc = PGPR_ERROR_CORRUPT_PGP_PACKET; /* assume failure */
if (item->version || item->saved)
return PGPR_ERROR_INTERNAL;
/* make sure that the item was initialized with the correct tag */
if ((item->tag != PGPRTAG_PUBLIC_KEY && item->tag != PGPRTAG_PUBLIC_SUBKEY) || pkt->tag != item->tag)
return PGPR_ERROR_INTERNAL;
if (pkt->blen == 0)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->version = pkt->body[0];
switch (item->version) {
case 3:
{ pgprPktKeyV3 v = (pgprPktKeyV3)pkt->body;
uint32_t expdays;
if (pkt->blen <= sizeof(*v) || v->pubkey_algo != PGPRPUBKEYALGO_RSA)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->time = pgprGrab4(v->time);
if (!item->time)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->saved |= PGPRITEM_SAVED_TIME;
item->pubkey_algo = v->pubkey_algo;
item->mpi_offset = sizeof(*v);
expdays = pgprGrab2(v->expdays);
if (expdays > 49710)
expdays = 49710; /* 130 years */
item->key_expire = expdays * 86400;
item->saved |= PGPRITEM_SAVED_KEY_EXPIRE;
rc = PGPR_OK;
} break;
case 4:
{ pgprPktKeyV4 v = (pgprPktKeyV4)pkt->body;
if (pkt->blen <= sizeof(*v))
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->time = pgprGrab4(v->time);
if (!item->time)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->saved |= PGPRITEM_SAVED_TIME;
item->pubkey_algo = v->pubkey_algo;
item->mpi_offset = sizeof(*v);
rc = PGPR_OK;
} break;
case 5:
case 6:
{ pgprPktKeyV56 v = (pgprPktKeyV56)pkt->body;
uint32_t pubkey_len;
if (pkt->blen <= sizeof(*v))
return PGPR_ERROR_CORRUPT_PGP_PACKET;
pubkey_len = pgprGrab4(v->pubkey_len);
if (pubkey_len > PGPR_MAX_OPENPGP_BYTES || pkt->blen != sizeof(*v) + pubkey_len)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->time = pgprGrab4(v->time);
if (!item->time)
return PGPR_ERROR_CORRUPT_PGP_PACKET;
item->saved |= PGPRITEM_SAVED_TIME;
item->pubkey_algo = v->pubkey_algo;
item->mpi_offset = sizeof(*v);
rc = PGPR_OK;
} break;
default:
rc = PGPR_ERROR_UNSUPPORTED_VERSION;
break;
}
/* read mpi data if there was no error */
if (rc == PGPR_OK)
rc = pgprParseKeyParams(pkt, item);
/* calculate the key fingerprint and key id if we could parse the key */
if (rc == PGPR_OK)
rc = pgprParseKeyFp(pkt, item);
return rc;
}
pgprRC pgprParseUserID(pgprPkt *pkt, pgprItem item)
{
if (item->tag != PGPRTAG_PUBLIC_KEY || pkt->tag != PGPRTAG_USER_ID)
return PGPR_ERROR_INTERNAL;
free(item->userid);
item->userid = pgprMalloc(pkt->blen + 1);
if (!item->userid)
return PGPR_ERROR_NO_MEMORY;
memcpy(item->userid, pkt->body, pkt->blen);
item->userid[pkt->blen] = 0;
return PGPR_OK;
}