Codebase list pgpdump / 61382fd5-8056-448d-8b08-e9f518cd903a/main keys.c
61382fd5-8056-448d-8b08-e9f518cd903a/main

Tree @61382fd5-8056-448d-8b08-e9f518cd903a/main (Download .tar.gz)

keys.c @61382fd5-8056-448d-8b08-e9f518cd903a/mainraw · history · blame

  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
/*
 * keys.c
 */

#include "pgpdump.h"

private int PUBLIC;
private int VERSION;

private void old_Public_Key_Packet(void);
private void new_Public_Key_Packet(int);
private void IV(unsigned int);
private void plain_Secret_Key(int);
private void encrypted_Secret_Key(int, int);

public void
Public_Subkey_Packet(int len)
{
	Public_Key_Packet(len);
}

public void
Public_Key_Packet(int len)
{
	VERSION = Getc();
	printf("\tVer %d - ", VERSION);
	switch (VERSION) {
	case 2:
	case 3:
		printf("old\n");
		old_Public_Key_Packet();
		break;
	case 4:
		printf("new\n");
		new_Public_Key_Packet(len - 1);
		break;
	default:
		warn_exit("unknown version (%d).", VERSION);
		break;
	}
}

private void
old_Public_Key_Packet(void)
{
	int days;
	time4("Public key creation time");
	days = Getc() * 256;
	days += Getc();
	printf("\tValid days - %d[0 is forever]\n", days);
	PUBLIC = Getc();
	pub_algs(PUBLIC); /* PUBLIC should be 1 */
	multi_precision_integer("RSA n");
	multi_precision_integer("RSA e");
}

/* added: 2021-11-11; extended: 2022-02-21 (BrainPool 384,512; RFC5639)
 * Reference: draft-ietf-openpgp-crypto-refresh-04 (10/2021);section 9.2 ECC Curves for OpenPGP
 * https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-04.html
 * Note (2021-11-25): actual ECC curve hex OID padded to 10 to match incoming oid array length
 *                    so that memcmp will work properly (compare two values of the same size)
 */
private unsigned char BrainPool256r1_OID[10]={0x2B,0x24,0x3,0x3,0x2,0x8,0x1,0x1,0x7,0};
private unsigned char BrainPool384r1_OID[10]={0x2B,0x24,0x3,0x3,0x2,0x8,0x1,0x1,0x0b,0};
private unsigned char BrainPool512r1_OID[10]={0x2B,0x24,0x3,0x3,0x2,0x8,0x1,0x1,0x0d,0};
private unsigned char NIST_P256_OID[10]={0x2A,0x86,0x48,0xCE,0x3D,0x3,0x1,0x7,0,0};
private unsigned char NIST_P384_OID[10]={0x2B,0x81,0x04,0x00,0x22,0,0,0,0,0};
private unsigned char NIST_P521_OID[10]={0x2B,0x81,0x04,0x00,0x23,0,0,0,0,0};
private unsigned char Ed25519_OID[10]={0x2B,0x06,0x01,0x04,0x01,0xDA,0x47,0x0F,0x01,0};
private unsigned char Ed448_OID[10]={0x2B,0x65,0x71,0,0,0,0,0,0,0};
private unsigned char Curve25519_OID[10]={0x2B,0x06,0x01,0x04,0x01,0x97,0x55,0x01,0x05,0x01};
private unsigned char X448_OID[10]={0x2B,0x65,0x6F,0,0,0,0,0,0,0};

private unsigned char oid_input_HEX[10]={0,0,0,0,0,0,0,0,0,0};
#define oid_input_HEX_size sizeof(oid_input_HEX)
private size_t oidLEN;
private int FoundECC=NO;
private int jj;

private struct {
  const unsigned char *oidhex;
  const char *name;
  const char *oidstring;
} ELLIP_CURVES[] = {
  {NIST_P256_OID,"NIST P-256","0x2A 86 48 CE 3D 03 01 07"},
  {NIST_P384_OID,"NIST P-384","0x2B 81 04 00 22"},
  {NIST_P521_OID,"NIST P-521","0x2B 81 04 00 23"},
  {Ed25519_OID,"Ed25519","0x2B 06 01 04 01 DA 47 0F 01"},
  {Ed448_OID,"Ed448","0x2B 65 71"},
  {Curve25519_OID,"Curve25519","0x2B 06 01 04 01 97 55 01 05 01"},
  {X448_OID,"X448","0x2B 65 6F"},
  {BrainPool256r1_OID,"brainpoolP256r1","0x2B 24 03 03 02 08 01 01 07"},
  {BrainPool384r1_OID,"BrainPoolP384r1","0x2B 24 03 03 02 08 01 01 07 0b"},
  {BrainPool512r1_OID,"BrainPoolP512r1","0x2B 24 03 03 02 08 01 01 07 0d"}
};
#define ELLIP_CURVES_NUM 10

/* end 2021-11-11 */


private void
new_Public_Key_Packet(int len)
{
	key_creation_time4("Public key creation time");
	PUBLIC = Getc();
	pub_algs(PUBLIC);
	switch (PUBLIC) {
	case 1:
	case 2:
	case 3:
		multi_precision_integer("RSA n");
		multi_precision_integer("RSA e");
		break;
	case 16:
	case 20:
		multi_precision_integer("ElGamal p");
		multi_precision_integer("ElGamal g");
		multi_precision_integer("ElGamal y");
		break;
	case 17:
		multi_precision_integer("DSA p");
		multi_precision_integer("DSA q");
		multi_precision_integer("DSA g");
		multi_precision_integer("DSA y");
		break;
	case 18:/*ECDH*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
	        }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("ECDH Q");
/* note - what follows is most of what the "draft-ietf-openpgp-crypto-refresh-04"
 * specifies for "13.5 EC DH Algorithm (ECDH)" minus the following:
 * a) 'one-octet public key algorithm ID defined in Section 9.1'
 * b) '20 octets representing the UTF-8 encoding of the string "Anonymous Sender"'
 * c) '20 octets representing a recipient encryption subkey or a primary key fingerprint'
 * The end result is consonant with GnuPG-2.3.3 "list-packets" output in fields/bytes,
 * though gpg-2.3.3 displays "pkey[2]" [32 bits]" where the supposed KDF parameters exist.
 */
		unsigned int KDFparmsSize,KDFbits,KDFhashID,KDFsymAlgoID;
		KDFparmsSize=Getc();/*don't display*/
                KDFbits=(KDFparmsSize + 1)*8;
                Getc();/*bypass supposed KDF constant */
		KDFhashID=Getc();
		KDFsymAlgoID=Getc();
		printf("\tECDH KDF params(%d bits) - ...\n",KDFbits);
                printf("\t\t%s ","KDFhashID: ");
		hash_algs(KDFhashID);
                printf("\t\t%s ","KDFsymAlgoID: ");
		sym_algs(KDFsymAlgoID);
		break;
	case 19:/*ECDSA*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
                }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("ECDSA Q");
		break;
        case 22:/*EdDSA*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
                }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("EdDSA Q");
                break;
	default:
		printf("\tUnknown public key(pub %d)\n", PUBLIC);
		skip(len - 5);
		break;
	}
}

private void
IV(unsigned int len)
{
	printf("\tIV - ");
	dump(len);
	printf("\n");
}

public void
Secret_Subkey_Packet(int len)
{
	Secret_Key_Packet(len);
}

public void
Secret_Key_Packet(int len)
{
	int s2k, sym;

	Getc_resetlen();
	Public_Key_Packet(len);
	s2k = Getc();
	switch (s2k) {
	case 0:
		plain_Secret_Key(len - Getc_getlen());
		break;
	case 254:
		sym = Getc();
		sym_algs(sym);
		if (string_to_key() == YES)
			IV(iv_len(sym));
		encrypted_Secret_Key(len - Getc_getlen(), YES);
		break;
	case 255:
		sym = Getc();
		sym_algs(sym);
		if (string_to_key() == YES)
			IV(iv_len(sym));
		encrypted_Secret_Key(len - Getc_getlen(), NO);
		break;
	default:
		sym = s2k;
		sym_algs(sym);
		printf("\tSimple string-to-key for IDEA\n");
		IV(iv_len(sym));
		encrypted_Secret_Key(len - Getc_getlen(), NO);
		break;
	}
}

/*
 * 2021-11-29: added cases 18,19,22 (copied from Public key)
 */

private void
plain_Secret_Key(int len)
{
	switch (VERSION) {
	case 2:
	case 3:
		/* PUBLIC should be 1. */
		/* Tested by specifying a null passphrase. */
		multi_precision_integer("RSA d");
		multi_precision_integer("RSA p");
		multi_precision_integer("RSA q");
		multi_precision_integer("RSA u");
		printf("\tChecksum - ");
		dump(2);
		printf("\n");
		break;
	case 4:
		switch (PUBLIC) {
		case 1:
		case 2:
		case 3:
			multi_precision_integer("RSA d");
			multi_precision_integer("RSA p");
			multi_precision_integer("RSA q");
			multi_precision_integer("RSA u");
			break;
		case 16:
		case 20:
			multi_precision_integer("ElGamal x");
			break;
		case 17:
			multi_precision_integer("DSA x");
			break;
	case 18:/*ECDH*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
	        }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("ECDH Q");
/* note - what follows is most of what the "draft-ietf-openpgp-crypto-refresh-04"
 * specifies for "13.5 EC DH Algorithm (ECDH)" minus the following:
 * a) 'one-octet public key algorithm ID defined in Section 9.1'
 * b) '20 octets representing the UTF-8 encoding of the string "Anonymous Sender"'
 * c) '20 octets representing a recipient encryption subkey or a primary key fingerprint'
 * The end result is consonant with GnuPG-2.3.3 "list-packets" output in fields/bytes,
 * though gpg-2.3.3 displays "pkey[2]" [32 bits]" where the supposed KDF parameters exist.
 */
		unsigned int KDFparmsSize,KDFbits,KDFhashID,KDFsymAlgoID;
		KDFparmsSize=Getc();/*don't display*/
                KDFbits=(KDFparmsSize + 1)*8;
                Getc();/*bypass supposed KDF constant */
		KDFhashID=Getc();
		KDFsymAlgoID=Getc();
		printf("\tECDH KDF params(%d bits) - ...\n",KDFbits);
                printf("\t\t%s ","KDFhashID: ");
		hash_algs(KDFhashID);
                printf("\t\t%s ","KDFsymAlgoID: ");
		sym_algs(KDFsymAlgoID);
		break;
	case 19:/*ECDSA*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
                }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("ECDSA Q");
		break;
        case 22:/*EdDSA*/
		oidLEN = Getc();
		for(jj=0;jj<oidLEN;jj++){oid_input_HEX[jj]=Getc();}
	        for(jj=0;jj<ELLIP_CURVES_NUM;jj++){
		  if(memcmp(ELLIP_CURVES[jj].oidhex,oid_input_HEX,oid_input_HEX_size) == 0){
	            FoundECC=YES;
	            break;
	          }
                }
	        if(FoundECC){
	          printf("\tElliptic Curve - ");
	          printf("%s (%s)\n",ELLIP_CURVES[jj].name,ELLIP_CURVES[jj].oidstring);
	        }
	        else{
	          printf("\tunknown(elliptic curve - ");
	          for(jj=0;jj<oidLEN;jj++){
	            printf("%02hhu,%02x ",oid_input_HEX[jj],oid_input_HEX[jj]);
	          }
	          puts(")");
	        }
		multi_precision_integer("EdDSA Q");
                break;

		default:
			printf("\tUnknown secret key(pub %d)\n", PUBLIC);
			skip(len - 2);
			break;
		}
		printf("\tChecksum - ");
		dump(2);
		printf("\n");
		break;
	default:
		printf("\tunknown version (%d)\n", VERSION);
		skip(len);
		break;
	}
}

/*
 * 2021-11-29: Added cases 18,19,20
 */
private void
encrypted_Secret_Key(int len, int sha1)
{
	if (len == 0)
		return;

	switch (VERSION) {
	case 2:
	case 3:
		/* PUBLIC should be 1.
		   Printable since an MPI prefix count is not encrypted. */
		multi_precision_integer("Encrypted RSA d");
		multi_precision_integer("Encrypted RSA p");
		multi_precision_integer("Encrypted RSA q");
		multi_precision_integer("Encrypted RSA u");
		printf("\tChecksum - ");
		dump(2);
		printf("\n");
		break;
	case 4:
		switch (PUBLIC) {
		case 1:
		case 2:
		case 3:
			printf("\tEncrypted RSA d\n");
			printf("\tEncrypted RSA p\n");
			printf("\tEncrypted RSA q\n");
			printf("\tEncrypted RSA u\n");
			break;
		case 16:
		case 20:
			printf("\tEncrypted ElGamal x\n");
			break;
		case 17:
			printf("\tEncrypted DSA x\n");
			break;
                case 18:
                        printf("\tEncrypted ECDH x\n");
                        break;
                case 19:
                        printf("\tEncrypted ECDSA x\n");
                        break;
                case 22:
                        printf("\tEncrypted EdDSA x\n");
                        break;
		default:
			printf("\tUnknown encrypted key(pub %d)\n", PUBLIC);
			break;
		}
		if (sha1 == YES)
			printf("\tEncrypted SHA1 hash\n");
		else
			printf("\tEncrypted checksum\n");
		skip(len);
		break;
	default:
		printf("\tunknown version (%d)\n", VERSION);
		skip(len);
		break;
	}
}

/*
 * Copyright (C) 1998 Kazuhiko Yamamoto
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */