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
|
/* Support of OpenPGP certificates
* Copyright (C) 2002-2004 Andreas Steffen, Zuercher Hochschule Winterthur
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* RCSID $Id: pgp.c,v 1.7 2006/01/04 21:00:43 as Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <freeswan.h>
#include <freeswan/ipsec_policy.h>
#include "constants.h"
#include "defs.h"
#include "mp_defs.h"
#include "log.h"
#include "id.h"
#include "pgp.h"
#include "certs.h"
#include "md5.h"
#include "whack.h"
#include "pkcs1.h"
#include "keys.h"
/*
* chained list of OpenPGP end certificates
*/
static pgpcert_t *pgpcerts = NULL;
/*
* OpenPGP packet tags defined in section 4.3 of RFC 2440
*/
#define PGP_PKT_RESERVED 0
#define PGP_PKT_PUBKEY_ENC_SESSION_KEY 1
#define PGP_PKT_SIGNATURE 2
#define PGP_PKT_SYMKEY_ENC_SESSION_KEY 3
#define PGP_PKT_ONE_PASS_SIGNATURE_PKT 4
#define PGP_PKT_SECRET_KEY 5
#define PGP_PKT_PUBLIC_KEY 6
#define PGP_PKT_SECRET_SUBKEY 7
#define PGP_PKT_COMPRESSED_DATA 8
#define PGP_PKT_SYMKEY_ENC_DATA 9
#define PGP_PKT_MARKER 10
#define PGP_PKT_LITERAL_DATA 11
#define PGP_PKT_TRUST 12
#define PGP_PKT_USER_ID 13
#define PGP_PKT_PUBLIC_SUBKEY 14
#define PGP_PKT_ROOF 15
static const char *const pgp_packet_type_name[] = {
"Reserved",
"Public-Key Encrypted Session Key Packet",
"Signature Packet",
"Symmetric-Key Encrypted Session Key Packet",
"One-Pass Signature Packet",
"Secret Key Packet",
"Public Key Packet",
"Secret Subkey Packet",
"Compressed Data Packet",
"Symmetrically Encrypted Data Packet",
"Marker Packet",
"Literal Data Packet",
"Trust Packet",
"User ID Packet",
"Public Subkey Packet"
};
/*
* OpenPGP public key algorithms defined in section 9.1 of RFC 2440
*/
#define PGP_PUBKEY_ALG_RSA 1
#define PGP_PUBKEY_ALG_RSA_ENC_ONLY 2
#define PGP_PUBKEY_ALG_RSA_SIGN_ONLY 3
#define PGP_PUBKEY_ALG_ELGAMAL_ENC_ONLY 16
#define PGP_PUBKEY_ALG_DSA 17
#define PGP_PUBKEY_ALG_ECC 18
#define PGP_PUBKEY_ALG_ECDSA 19
#define PGP_PUBKEY_ALG_ELGAMAL 20
/*
* OpenPGP symmetric key algorithms defined in section 9.2 of RFC 2440
*/
#define PGP_SYM_ALG_PLAIN 0
#define PGP_SYM_ALG_IDEA 1
#define PGP_SYM_ALG_3DES 2
#define PGP_SYM_ALG_CAST5 3
#define PGP_SYM_ALG_BLOWFISH 4
#define PGP_SYM_ALG_SAFER 5
#define PGP_SYM_ALG_DES 6
#define PGP_SYM_ALG_AES 7
#define PGP_SYM_ALG_AES_192 8
#define PGP_SYM_ALG_AES_256 9
#define PGP_SYM_ALG_TWOFISH 10
#define PGP_SYM_ALG_ROOF 11
static const char *const pgp_sym_alg_name[] = {
"Plaintext",
"IDEA",
"3DES",
"CAST5",
"Blowfish",
"SAFER",
"DES",
"AES",
"AES-192",
"AES-256",
"Twofish"
};
/*
* Size of PGP Key ID
*/
#define PGP_KEYID_SIZE 8
const pgpcert_t empty_pgpcert = {
NULL , /* *next */
0 , /* installed */
0 , /* count */
{ NULL, 0 }, /* certificate */
0 , /* created */
0 , /* until */
0 , /* pubkeyAlgorithm */
{ NULL, 0 }, /* modulus */
{ NULL, 0 }, /* publicExponent */
"" /* fingerprint */
};
static size_t
pgp_size(chunk_t *blob, int len)
{
size_t size = 0;
blob->len -= len;
while (len-- > 0)
size = 256*size + *blob->ptr++;
return size;
}
/*
* extracts the length of a PGP packet
*/
static size_t
pgp_old_packet_length(chunk_t *blob)
{
/* bits 0 and 1 define the packet length type */
int len_type = 0x03 & *blob->ptr++;
blob->len--;
/* len_type: 0 -> 1 byte, 1 -> 2 bytes, 2 -> 4 bytes */
return pgp_size(blob, (len_type == 0)? 1: len_type << 1);
}
/*
* extracts PGP packet version (V3 or V4)
*/
static u_char
pgp_version(chunk_t *blob)
{
u_char version = *blob->ptr++;
blob->len--;
DBG(DBG_PARSING,
DBG_log("L3 - version:");
DBG_log(" V%d", version)
)
return version;
}
/*
* Parse OpenPGP public key packet defined in section 5.5.2 of RFC 2440
*/
static bool
parse_pgp_pubkey_packet(chunk_t *packet, pgpcert_t *cert)
{
u_char version = pgp_version(packet);
if (version < 3 || version > 4)
{
plog("PGP packet version V%d not supported", version);
return FALSE;
}
/* creation date - 4 bytes */
cert->created = (time_t)pgp_size(packet, 4);
DBG(DBG_PARSING,
DBG_log("L3 - created:");
DBG_log(" %s", timetoa(&cert->created, TRUE))
)
if (version == 3)
{
/* validity in days - 2 bytes */
cert->until = (time_t)pgp_size(packet, 2);
/* validity of 0 days means that the key never expires */
if (cert->until > 0)
cert->until = cert->created + 24*3600*cert->until;
DBG(DBG_PARSING,
DBG_log("L3 - until:");
DBG_log(" %s", timetoa(&cert->until, TRUE));
)
}
/* public key algorithm - 1 byte */
DBG(DBG_PARSING,
DBG_log("L3 - public key algorithm:")
)
switch (pgp_size(packet, 1))
{
case PGP_PUBKEY_ALG_RSA:
case PGP_PUBKEY_ALG_RSA_SIGN_ONLY:
cert->pubkeyAlg = PUBKEY_ALG_RSA;
DBG(DBG_PARSING,
DBG_log(" RSA")
)
/* modulus n */
cert->modulus.len = (pgp_size(packet, 2)+7) / BITS_PER_BYTE;
cert->modulus.ptr = packet->ptr;
packet->ptr += cert->modulus.len;
packet->len -= cert->modulus.len;
DBG(DBG_PARSING,
DBG_log("L3 - modulus:")
)
DBG_cond_dump_chunk(DBG_RAW, "", cert->modulus);
/* public exponent e */
cert->publicExponent.len = (pgp_size(packet, 2)+7) / BITS_PER_BYTE;
cert->publicExponent.ptr = packet->ptr;
packet->ptr += cert->publicExponent.len;
packet->len -= cert->publicExponent.len;
DBG(DBG_PARSING,
DBG_log("L3 - public exponent:")
)
DBG_cond_dump_chunk(DBG_RAW, "", cert->publicExponent);
if (version == 3)
{
/* a V3 fingerprint is the MD5 hash of modulus and public exponent */
MD5_CTX context;
MD5Init(&context);
MD5Update(&context, cert->modulus.ptr, cert->modulus.len);
MD5Update(&context, cert->publicExponent.ptr, cert->publicExponent.len);
MD5Final(cert->fingerprint, &context);
}
else
{
plog(" computation of V4 key ID not implemented yet");
}
break;
case PGP_PUBKEY_ALG_DSA:
cert->pubkeyAlg = PUBKEY_ALG_DSA;
DBG(DBG_PARSING,
DBG_log(" DSA")
)
plog(" DSA public keys not supported");
return FALSE;
default:
cert->pubkeyAlg = 0;
DBG(DBG_PARSING,
DBG_log(" other")
)
plog(" exotic not RSA public keys not supported");
return FALSE;
}
return TRUE;
}
/*
* Parse OpenPGP secret key packet defined in section 5.5.3 of RFC 2440
*/
static bool
parse_pgp_secretkey_packet(chunk_t *packet, RSA_private_key_t *key)
{
int i, s2k;
pgpcert_t cert = empty_pgpcert;
if (!parse_pgp_pubkey_packet(packet, &cert))
return FALSE;
init_RSA_public_key((RSA_public_key_t *)key, cert.publicExponent
, cert.modulus);
/* string-to-key usage */
s2k = pgp_size(packet, 1);
DBG(DBG_PARSING,
DBG_log("L3 - string-to-key: %d", s2k)
)
if (s2k == 255)
{
plog(" string-to-key specifiers not supported");
return FALSE;
}
if (s2k >= PGP_SYM_ALG_ROOF)
{
plog(" undefined symmetric key algorithm");
return FALSE;
}
/* a known symmetric key algorithm is specified*/
DBG(DBG_PARSING,
DBG_log(" %s", pgp_sym_alg_name[s2k])
)
/* private key is unencrypted */
if (s2k == PGP_SYM_ALG_PLAIN)
{
for (i = 2; i < RSA_PRIVATE_FIELD_ELEMENTS; i++)
{
mpz_t u; /* auxiliary variable */
/* compute offset to private key component i*/
MP_INT *n = (MP_INT*)((char *)key + RSA_private_field[i].offset);
switch (i)
{
case 2:
case 3:
case 4:
{
size_t len = (pgp_size(packet, 2)+7) / BITS_PER_BYTE;
n_to_mpz(n, packet->ptr, len);
DBG(DBG_PARSING,
DBG_log("L3 - %s:", RSA_private_field[i].name)
)
DBG_cond_dump(DBG_PRIVATE, "", packet->ptr, len);
packet->ptr += len;
packet->len -= len;
}
break;
case 5: /* dP = d mod (p-1) */
mpz_init(u);
mpz_sub_ui(u, &key->p, 1);
mpz_mod(n, &key->d, u);
mpz_clear(u);
break;
case 6: /* dQ = d mod (q-1) */
mpz_init(u);
mpz_sub_ui(u, &key->q, 1);
mpz_mod(n, &key->d, u);
mpz_clear(u);
break;
case 7: /* qInv = (q^-1) mod p */
mpz_invert(n, &key->q, &key->p);
if (mpz_cmp_ui(n, 0) < 0)
mpz_add(n, n, &key->p);
passert(mpz_cmp(n, &key->p) < 0);
break;
}
}
return TRUE;
}
plog(" %s encryption not supported", pgp_sym_alg_name[s2k]);
return FALSE;
}
/*
* Parse OpenPGP signature packet defined in section 5.2.2 of RFC 2440
*/
static bool
parse_pgp_signature_packet(chunk_t *packet, pgpcert_t *cert)
{
time_t created;
chunk_t keyid;
u_char sig_type;
u_char version = pgp_version(packet);
/* we parse only V3 signature packets */
if (version != 3)
return TRUE;
/* size byte must have the value 5 */
if (pgp_size(packet, 1) != 5)
{
plog(" size must be 5");
return FALSE;
}
/* signature type - 1 byte */
sig_type = (u_char)pgp_size(packet, 1);
DBG(DBG_PARSING,
DBG_log("L3 - signature type: 0x%2x", sig_type)
)
/* creation date - 4 bytes */
created = (time_t)pgp_size(packet, 4);
DBG(DBG_PARSING,
DBG_log("L3 - created:");
DBG_log(" %s", timetoa(&cert->created, TRUE))
)
/* key ID of signer - 8 bytes */
keyid.ptr = packet->ptr;
keyid.len = PGP_KEYID_SIZE;
DBG_cond_dump_chunk(DBG_PARSING, "L3 - key ID of signer", keyid);
return TRUE;
}
bool
parse_pgp(chunk_t blob, pgpcert_t *cert, RSA_private_key_t *key)
{
DBG(DBG_PARSING,
DBG_log("L0 - PGP file:")
)
DBG_cond_dump_chunk(DBG_RAW, "", blob);
if (cert != NULL)
{
/* parse a PGP certificate file */
cert->certificate = blob;
time(&cert->installed);
}
else if (key == NULL)
{
/* should not occur, nothing to parse */
return FALSE;
}
while (blob.len > 0)
{
chunk_t packet = empty_chunk;
u_char packet_tag = *blob.ptr;
DBG(DBG_PARSING,
DBG_log("L1 - PGP packet: tag= 0x%2x", packet_tag)
)
/* bit 7 must be set */
if (!(packet_tag & 0x80))
{
plog(" incorrect Packet Tag");
return FALSE;
}
/* bit 6 set defines new packet format */
if (packet_tag & 0x40)
{
plog(" new PGP packet format not supported");
return FALSE;
}
else
{
int packet_type = (packet_tag & 0x3C) >> 2;
packet.len = pgp_old_packet_length(&blob);
packet.ptr = blob.ptr;
blob.ptr += packet.len;
blob.len -= packet.len;
DBG(DBG_PARSING,
DBG_log(" %s (%d), old format, %d bytes",
(packet_type < PGP_PKT_ROOF) ?
pgp_packet_type_name[packet_type] :
"Undefined Packet Type", packet_type, (int)packet.len);
DBG_log("L2 - body:")
)
DBG_cond_dump_chunk(DBG_RAW, "", packet);
if (cert != NULL)
{
/* parse a PGP certificate */
switch (packet_type)
{
case PGP_PKT_PUBLIC_KEY:
if (!parse_pgp_pubkey_packet(&packet, cert))
return FALSE;
break;
case PGP_PKT_SIGNATURE:
if (!parse_pgp_signature_packet(&packet, cert))
return FALSE;
break;
case PGP_PKT_USER_ID:
DBG(DBG_PARSING,
DBG_log("L3 - user ID:");
DBG_log(" '%.*s'", (int)packet.len, packet.ptr)
)
break;
default:
break;
}
}
else
{
/* parse a PGP private key file */
switch (packet_type)
{
case PGP_PKT_SECRET_KEY:
if (!parse_pgp_secretkey_packet(&packet, key))
return FALSE;
break;
default:
break;
}
}
}
}
return TRUE;
}
/*
* compare two OpenPGP certificates
*/
static bool
same_pgpcert(pgpcert_t *a, pgpcert_t *b)
{
return a->certificate.len == b->certificate.len &&
memcmp(a->certificate.ptr, b->certificate.ptr, b->certificate.len) == 0;
}
/*
* for each link pointing to the certificate increase the count by one
*/
void
share_pgpcert(pgpcert_t *cert)
{
if (cert != NULL)
cert->count++;
}
/*
* select the OpenPGP keyid as ID
*/
void
select_pgpcert_id(pgpcert_t *cert, struct id *end_id)
{
end_id->kind = ID_KEY_ID;
end_id->name.len = PGP_FINGERPRINT_SIZE;
end_id->name.ptr = cert->fingerprint;
end_id->name.ptr = temporary_cyclic_buffer();
memcpy(end_id->name.ptr, cert->fingerprint, PGP_FINGERPRINT_SIZE);
}
/*
* add an OpenPGP user/host certificate to the chained list
*/
pgpcert_t*
add_pgpcert(pgpcert_t *cert)
{
pgpcert_t *c = pgpcerts;
while (c != NULL)
{
if (same_pgpcert(c, cert)) /* already in chain, free cert */
{
free_pgpcert(cert);
return c;
}
c = c->next;
}
/* insert new cert at the root of the chain */
cert->next = pgpcerts;
pgpcerts = cert;
DBG(DBG_CONTROL | DBG_PARSING,
DBG_log(" pgp cert inserted")
)
return cert;
}
/* release of a certificate decreases the count by one
" the certificate is freed when the counter reaches zero
*/
void
release_pgpcert(pgpcert_t *cert)
{
if (cert != NULL && --cert->count == 0)
{
pgpcert_t **pp = &pgpcerts;
while (*pp != cert)
pp = &(*pp)->next;
*pp = cert->next;
free_pgpcert(cert);
}
}
/*
* free a PGP certificate
*/
void
free_pgpcert(pgpcert_t *cert)
{
if (cert != NULL)
{
if (cert->certificate.ptr != NULL)
pfree(cert->certificate.ptr);
pfree(cert);
}
}
/*
* list all PGP end certificates in a chained list
*/
void
list_pgp_end_certs(bool utc)
{
pgpcert_t *cert = pgpcerts;
time_t now;
/* determine the current time */
time(&now);
if (cert != NULL)
{
whack_log(RC_COMMENT, " ");
whack_log(RC_COMMENT, "List of PGP End certificates:");
whack_log(RC_COMMENT, " ");
}
while (cert != NULL)
{
unsigned keysize;
char buf[BUF_LEN];
cert_t c;
c.type = CERT_PGP;
c.u.pgp = cert;
whack_log(RC_COMMENT, "%s, count: %d", timetoa(&cert->installed, utc), cert->count);
datatot(cert->fingerprint, PGP_FINGERPRINT_SIZE, 'x', buf, BUF_LEN);
whack_log(RC_COMMENT, " fingerprint: %s", buf);
form_keyid(cert->publicExponent, cert->modulus, buf, &keysize);
whack_log(RC_COMMENT, " pubkey: %4d RSA Key %s%s", 8*keysize, buf,
(has_private_key(c))? ", has private key" : "");
whack_log(RC_COMMENT, " created: %s", timetoa(&cert->created, utc));
whack_log(RC_COMMENT, " until: %s %s", timetoa(&cert->until, utc),
check_expiry(cert->until, CA_CERT_WARNING_INTERVAL, TRUE));
cert = cert->next;
}
}
|