/src/gnupg/g10/parse-packet.c
Line | Count | Source |
1 | | /* parse-packet.c - read packets |
2 | | * Copyright (C) 1998-2007, 2009-2010 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014, 2018 Werner Koch |
4 | | * Copyright (C) 2015 g10 Code GmbH |
5 | | * |
6 | | * This file is part of GnuPG. |
7 | | * |
8 | | * GnuPG is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * GnuPG is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
20 | | * SPDX-License-Identifier: GPL-3.0+ |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | #include "gpg.h" |
29 | | #include "../common/util.h" |
30 | | #include "packet.h" |
31 | | #include "../common/iobuf.h" |
32 | | #include "filter.h" |
33 | | #include "photoid.h" |
34 | | #include "options.h" |
35 | | #include "main.h" |
36 | | #include "../common/i18n.h" |
37 | | #include "../common/host2net.h" |
38 | | #include "../common/mbox-util.h" |
39 | | |
40 | | |
41 | | /* It is better to always allow parsing the 9980 packets but we keep |
42 | | * the macro for documentaion reasons. */ |
43 | | #undef RFC9980 |
44 | 4.43k | #define RFC9980 1 |
45 | | |
46 | | static int mpi_print_mode; |
47 | | static int list_mode; |
48 | | static estream_t listfp; |
49 | | |
50 | | /* A linked list of known notation names. Note that the FLAG is used |
51 | | * to store the length of the name to speed up the check. */ |
52 | | static strlist_t known_notations_list; |
53 | | |
54 | | |
55 | | static int parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, |
56 | | off_t * retpos, int *skip, IOBUF out, int do_skip |
57 | | #if DEBUG_PARSE_PACKET |
58 | | , const char *dbg_w, const char *dbg_f, int dbg_l |
59 | | #endif |
60 | | ); |
61 | | static int copy_packet (IOBUF inp, IOBUF out, int pkttype, |
62 | | unsigned long pktlen, int partial); |
63 | | static void skip_packet (IOBUF inp, int pkttype, |
64 | | unsigned long pktlen, int partial); |
65 | | static void *read_rest (IOBUF inp, size_t pktlen); |
66 | | static int parse_marker (IOBUF inp, int pkttype, unsigned long pktlen); |
67 | | static int parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen, |
68 | | PACKET * packet); |
69 | | static int parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen, |
70 | | PACKET * packet); |
71 | | static int parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen, |
72 | | PKT_onepass_sig * ops); |
73 | | static int parse_key (IOBUF inp, int pkttype, unsigned long pktlen, |
74 | | byte * hdr, int hdrlen, PACKET * packet); |
75 | | static int parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, |
76 | | PACKET * packet); |
77 | | static int parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen, |
78 | | PACKET * packet); |
79 | | static int parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, |
80 | | PACKET * packet); |
81 | | static gpg_error_t parse_ring_trust (parse_packet_ctx_t ctx, |
82 | | unsigned long pktlen); |
83 | | static int parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen, |
84 | | PACKET * packet, int new_ctb, int partial); |
85 | | static int parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen, |
86 | | PACKET * packet, int new_ctb); |
87 | | static int parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen, |
88 | | PACKET * packet, int new_ctb, int partial); |
89 | | static gpg_error_t parse_encrypted_ocb (iobuf_t inp, int pkttype, |
90 | | unsigned long pktlen, PACKET *packet, |
91 | | int partial); |
92 | | static int parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen, |
93 | | PACKET * packet, int new_ctb); |
94 | | static int parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen, |
95 | | PACKET * packet, int partial); |
96 | | |
97 | | /* Read a 16-bit value in MSB order (big endian) from an iobuf. */ |
98 | | static unsigned short |
99 | | read_16 (IOBUF inp) |
100 | 101k | { |
101 | 101k | unsigned short a; |
102 | 101k | a = (unsigned short)iobuf_get_noeof (inp) << 8; |
103 | 101k | a |= iobuf_get_noeof (inp); |
104 | 101k | return a; |
105 | 101k | } |
106 | | |
107 | | |
108 | | /* Read a 32-bit value in MSB order (big endian) from an iobuf. */ |
109 | | static unsigned long |
110 | | read_32 (IOBUF inp) |
111 | 129k | { |
112 | 129k | unsigned long a; |
113 | 129k | a = (unsigned long)iobuf_get_noeof (inp) << 24; |
114 | 129k | a |= iobuf_get_noeof (inp) << 16; |
115 | 129k | a |= iobuf_get_noeof (inp) << 8; |
116 | 129k | a |= iobuf_get_noeof (inp); |
117 | 129k | return a; |
118 | 129k | } |
119 | | |
120 | | |
121 | | /* Read an external representation of an MPI and return the MPI. The |
122 | | external format is a 16-bit unsigned value stored in network byte |
123 | | order giving the number of bits for the following integer. The |
124 | | integer is stored MSB first and is left padded with zero bits to |
125 | | align on a byte boundary. |
126 | | |
127 | | The caller must set *RET_NREAD to the maximum number of bytes to |
128 | | read from the pipeline INP. This function sets *RET_NREAD to be |
129 | | the number of bytes actually read from the pipeline. |
130 | | |
131 | | If SECURE is true, the integer is stored in secure memory |
132 | | (allocated using gcry_xmalloc_secure). */ |
133 | | static gcry_mpi_t |
134 | | mpi_read (iobuf_t inp, unsigned int *ret_nread, int secure) |
135 | 25.1k | { |
136 | 25.1k | int c, c1, c2, i; |
137 | 25.1k | unsigned int nmax = *ret_nread; |
138 | 25.1k | unsigned int nbits, nbytes; |
139 | 25.1k | size_t nread = 0; |
140 | 25.1k | gcry_mpi_t a = NULL; |
141 | 25.1k | byte *buf = NULL; |
142 | 25.1k | byte *p; |
143 | | |
144 | 25.1k | if (!nmax) |
145 | 8 | goto overflow; |
146 | | |
147 | 25.1k | if ((c = c1 = iobuf_get (inp)) == -1) |
148 | 180 | goto leave; |
149 | 24.9k | if (++nread == nmax) |
150 | 1 | goto overflow; |
151 | 24.9k | nbits = c << 8; |
152 | 24.9k | if ((c = c2 = iobuf_get (inp)) == -1) |
153 | 46 | goto leave; |
154 | 24.8k | ++nread; |
155 | 24.8k | nbits |= c; |
156 | 24.8k | if (nbits > MAX_EXTERN_MPI_BITS) |
157 | 66 | { |
158 | 66 | log_error ("mpi too large (%u bits)\n", nbits); |
159 | 66 | goto leave; |
160 | 66 | } |
161 | | |
162 | 24.8k | nbytes = (nbits + 7) / 8; |
163 | 24.8k | buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2); |
164 | 24.8k | p = buf; |
165 | 24.8k | p[0] = c1; |
166 | 24.8k | p[1] = c2; |
167 | 698k | for (i = 0; i < nbytes; i++) |
168 | 674k | { |
169 | 674k | if (nread == nmax) |
170 | 32 | goto overflow; |
171 | | |
172 | 674k | c = iobuf_get (inp); |
173 | 674k | if (c == -1) |
174 | 88 | goto leave; |
175 | | |
176 | 673k | p[i + 2] = c; |
177 | 673k | nread ++; |
178 | 673k | } |
179 | | |
180 | 24.6k | if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread)) |
181 | 0 | a = NULL; |
182 | | |
183 | 24.6k | *ret_nread = nread; |
184 | 24.6k | gcry_free(buf); |
185 | 24.6k | return a; |
186 | | |
187 | 41 | overflow: |
188 | 41 | log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax); |
189 | 421 | leave: |
190 | 421 | *ret_nread = nread; |
191 | 421 | gcry_free(buf); |
192 | 421 | return a; |
193 | 41 | } |
194 | | |
195 | | |
196 | | /* If NLENGTH is zero read an octet string of length NBYTES from INP |
197 | | * and return it at R_DATA. |
198 | | * |
199 | | * If NLENGTH is either 1, 2, or 4 and NLENGTH is zero read an |
200 | | * NLENGTH-octet count and use this count number octets from INP and |
201 | | * return it at R_DATA. |
202 | | * |
203 | | * On error return an error code and store NULL at R_DATA. PKTLEN |
204 | | * shall give the current length of the packet and is updated with |
205 | | * each read. If SECURE is true, the integer is stored in secure |
206 | | * memory (allocated using gcry_xmalloc_secure). |
207 | | */ |
208 | | static gpg_error_t |
209 | | read_raw_octet_string (iobuf_t inp, unsigned long *pktlen, |
210 | | unsigned int nlength, unsigned int nbytes, |
211 | | int secure, gcry_mpi_t *r_data) |
212 | 148 | { |
213 | 148 | gpg_error_t err; |
214 | 148 | int c, i; |
215 | 148 | byte *buf = NULL; |
216 | 148 | byte *p; |
217 | | |
218 | 148 | *r_data = NULL; |
219 | | |
220 | 148 | if ((nbytes && nlength) |
221 | 148 | || (!nbytes && !(nlength == 1 || nlength == 2 || nlength == 4))) |
222 | 0 | { |
223 | 0 | err = gpg_error (GPG_ERR_INV_ARG); |
224 | 0 | goto leave; |
225 | 0 | } |
226 | | |
227 | 148 | if (nlength) |
228 | 89 | { |
229 | 366 | for (i = 0; i < nlength; i++) |
230 | 286 | { |
231 | 286 | if (!*pktlen) |
232 | 0 | { |
233 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
234 | 0 | goto leave; |
235 | 0 | } |
236 | 286 | c = iobuf_readbyte (inp); |
237 | 286 | if (c < 0) |
238 | 9 | { |
239 | 9 | err = gpg_error (GPG_ERR_INV_PACKET); |
240 | 9 | goto leave; |
241 | 9 | } |
242 | 277 | --*pktlen; |
243 | 277 | nbytes <<= 8; |
244 | 277 | nbytes |= c; |
245 | 277 | } |
246 | | |
247 | 80 | if (!nbytes) |
248 | 1 | { |
249 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
250 | 1 | goto leave; |
251 | 1 | } |
252 | 80 | } |
253 | | |
254 | 138 | if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS) |
255 | 87 | || (nbytes*8 < nbytes)) |
256 | 54 | { |
257 | 54 | log_error ("octet string too large (%u octets)\n", nbytes); |
258 | 54 | err = gpg_error (GPG_ERR_TOO_LARGE); |
259 | 54 | goto leave; |
260 | 54 | } |
261 | | |
262 | 84 | if (nbytes > *pktlen) |
263 | 8 | { |
264 | 8 | log_error ("octet string larger than packet (%u/%lu)\n", nbytes, *pktlen); |
265 | 8 | err = gpg_error (GPG_ERR_INV_PACKET); |
266 | 8 | goto leave; |
267 | 8 | } |
268 | | |
269 | 76 | buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes); |
270 | 76 | if (!buf) |
271 | 0 | { |
272 | 0 | err = gpg_error_from_syserror (); |
273 | 0 | goto leave; |
274 | 0 | } |
275 | 76 | p = buf; |
276 | 8.34k | for (i = 0; i < nbytes; i++) |
277 | 8.31k | { |
278 | 8.31k | c = iobuf_get (inp); |
279 | 8.31k | if (c == -1) |
280 | 45 | { |
281 | 45 | err = gpg_error (GPG_ERR_INV_PACKET); |
282 | 45 | goto leave; |
283 | 45 | } |
284 | | |
285 | 8.27k | p[i] = c; |
286 | 8.27k | --*pktlen; |
287 | 8.27k | } |
288 | | |
289 | 31 | *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8); |
290 | 31 | return 0; |
291 | | |
292 | 117 | leave: |
293 | 117 | gcry_free (buf); |
294 | 117 | return err; |
295 | 76 | } |
296 | | |
297 | | |
298 | | /* Read a simple octet string (SOS) and mark the result as such a |
299 | | * value. For details see read_raw_octet_string. */ |
300 | | static gpg_error_t |
301 | | read_sos_octet_string (iobuf_t inp, unsigned long *pktlen, |
302 | | unsigned int nlength, unsigned int nbytes, |
303 | | int secure, gcry_mpi_t *r_data) |
304 | 68 | { |
305 | 68 | gpg_error_t err; |
306 | | |
307 | 68 | err = read_raw_octet_string (inp, pktlen, nlength, nbytes, secure, r_data); |
308 | 68 | if (!err) |
309 | 4 | gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2); |
310 | 68 | return err; |
311 | 68 | } |
312 | | |
313 | | |
314 | | /* Read an external representation of an SOS and return the opaque MPI |
315 | | with GCRYMPI_FLAG_USER2. The external format is a 16-bit unsigned |
316 | | value stored in network byte order giving information for the |
317 | | following octets. |
318 | | |
319 | | The caller must set *RET_NREAD to the maximum number of bytes to |
320 | | read from the pipeline INP. This function sets *RET_NREAD to be |
321 | | the number of bytes actually read from the pipeline. |
322 | | |
323 | | If SECURE is true, the integer is stored in secure memory |
324 | | (allocated using gcry_xmalloc_secure). */ |
325 | | static gcry_mpi_t |
326 | | sos_read (iobuf_t inp, unsigned int *ret_nread, int secure) |
327 | 20.8k | { |
328 | 20.8k | int c, c1, c2, i; |
329 | 20.8k | unsigned int nmax = *ret_nread; |
330 | 20.8k | unsigned int nbits, nbytes; |
331 | 20.8k | size_t nread = 0; |
332 | 20.8k | gcry_mpi_t a = NULL; |
333 | 20.8k | byte *buf = NULL; |
334 | 20.8k | byte *p; |
335 | | |
336 | 20.8k | if (!nmax) |
337 | 11 | goto overflow; |
338 | | |
339 | 20.7k | if ((c = c1 = iobuf_get (inp)) == -1) |
340 | 78 | goto leave; |
341 | 20.7k | if (++nread == nmax) |
342 | 41 | goto overflow; |
343 | 20.6k | nbits = c << 8; |
344 | 20.6k | if ((c = c2 = iobuf_get (inp)) == -1) |
345 | 34 | goto leave; |
346 | 20.6k | ++nread; |
347 | 20.6k | nbits |= c; |
348 | 20.6k | if (nbits > MAX_EXTERN_MPI_BITS) |
349 | 67 | { |
350 | 67 | log_error ("mpi too large (%u bits)\n", nbits); |
351 | 67 | goto leave; |
352 | 67 | } |
353 | | |
354 | 20.5k | nbytes = (nbits + 7) / 8; |
355 | 20.5k | buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes); |
356 | 20.5k | p = buf; |
357 | 211k | for (i = 0; i < nbytes; i++) |
358 | 190k | { |
359 | 190k | if (nread == nmax) |
360 | 19 | goto overflow; |
361 | | |
362 | 190k | c = iobuf_get (inp); |
363 | 190k | if (c == -1) |
364 | 75 | goto leave; |
365 | | |
366 | 190k | p[i] = c; |
367 | 190k | nread ++; |
368 | 190k | } |
369 | | |
370 | 20.4k | a = gcry_mpi_set_opaque (NULL, buf, nbits); |
371 | 20.4k | gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2); |
372 | 20.4k | *ret_nread = nread; |
373 | 20.4k | return a; |
374 | | |
375 | 71 | overflow: |
376 | 71 | log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax); |
377 | 325 | leave: |
378 | 325 | *ret_nread = nread; |
379 | 325 | gcry_free(buf); |
380 | 325 | return a; |
381 | 71 | } |
382 | | |
383 | | |
384 | | /* Register STRING as a known critical notation name. */ |
385 | | void |
386 | | register_known_notation (const char *string) |
387 | 359 | { |
388 | 359 | strlist_t sl; |
389 | | |
390 | 359 | if (!known_notations_list) |
391 | 1 | { |
392 | 1 | sl = add_to_strlist (&known_notations_list, |
393 | 1 | "preferred-email-encoding@pgp.com"); |
394 | 1 | sl->flags = 32; /* Length of the string. */ |
395 | 1 | } |
396 | 359 | if (!string) |
397 | 359 | return; /* Only initialized the default known notations. */ |
398 | | |
399 | | /* In --set-notation we use an exclamation mark to indicate a |
400 | | * critical notation. As a convenience skip this here. */ |
401 | 0 | if (*string == '!') |
402 | 0 | string++; |
403 | |
|
404 | 0 | if (!*string || strlist_find (known_notations_list, string)) |
405 | 0 | return; /* Empty string or already registered. */ |
406 | | |
407 | 0 | sl = add_to_strlist (&known_notations_list, string); |
408 | 0 | sl->flags = strlen (string); |
409 | 0 | } |
410 | | |
411 | | |
412 | | int |
413 | | set_packet_list_mode (int mode) |
414 | 492 | { |
415 | 492 | int old = list_mode; |
416 | 492 | list_mode = mode; |
417 | | |
418 | | /* We use stdout only if invoked by the --list-packets command |
419 | | but switch to stderr in all other cases. This breaks the |
420 | | previous behaviour but that seems to be more of a bug than |
421 | | intentional. I don't believe that any application makes use of |
422 | | this long standing annoying way of printing to stdout except when |
423 | | doing a --list-packets. If this assumption fails, it will be easy |
424 | | to add an option for the listing stream. Note that we initialize |
425 | | it only once; mainly because there is code which switches |
426 | | opt.list_mode back to 1 and we want to have all output to the |
427 | | same stream. The MPI_PRINT_MODE will be enabled if the |
428 | | corresponding debug flag is set or if we are in --list-packets |
429 | | and --verbose is given. |
430 | | |
431 | | Using stderr is not actually very clean because it bypasses the |
432 | | logging code but it is a special thing anyway. I am not sure |
433 | | whether using log_stream() would be better. Perhaps we should |
434 | | enable the list mode only with a special option. */ |
435 | 492 | if (!listfp) |
436 | 1 | { |
437 | 1 | if (opt.list_packets) |
438 | 0 | { |
439 | 0 | listfp = es_stdout; |
440 | 0 | if (opt.verbose) |
441 | 0 | mpi_print_mode = 1; |
442 | 0 | } |
443 | 1 | else |
444 | 1 | listfp = es_stderr; |
445 | | |
446 | 1 | if (DBG_MPI) |
447 | 0 | mpi_print_mode = 1; |
448 | 1 | } |
449 | 492 | return old; |
450 | 492 | } |
451 | | |
452 | | |
453 | | /* If OPT.VERBOSE is set, print a warning that the algorithm ALGO is |
454 | | not suitable for signing and encryption. */ |
455 | | static void |
456 | | unknown_pubkey_warning (int algo) |
457 | 66.9k | { |
458 | 66.9k | static byte unknown_pubkey_algos[256]; |
459 | | |
460 | | /* First check whether the algorithm is usable but not suitable for |
461 | | encryption/signing. */ |
462 | 66.9k | if (pubkey_get_npkey (algo)) |
463 | 14.2k | { |
464 | 14.2k | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
465 | 0 | { |
466 | 0 | if (!pubkey_get_nsig (algo)) |
467 | 0 | log_info ("public key algorithm %s not suitable for %s\n", |
468 | 0 | openpgp_pk_algo_name (algo), "signing"); |
469 | 0 | if (!pubkey_get_nenc (algo)) |
470 | 0 | log_info ("public key algorithm %s not suitable for %s\n", |
471 | 0 | openpgp_pk_algo_name (algo), "encryption"); |
472 | 0 | } |
473 | 14.2k | } |
474 | 52.6k | else |
475 | 52.6k | { |
476 | 52.6k | algo &= 0xff; |
477 | 52.6k | if (!unknown_pubkey_algos[algo]) |
478 | 197 | { |
479 | 197 | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
480 | 197 | log_info (_("can't handle public key algorithm %d\n"), algo); |
481 | 197 | unknown_pubkey_algos[algo] = 1; |
482 | 197 | } |
483 | 52.6k | } |
484 | 66.9k | } |
485 | | |
486 | | |
487 | | #if DEBUG_PARSE_PACKET |
488 | | int |
489 | | dbg_parse_packet (parse_packet_ctx_t ctx, PACKET *pkt, |
490 | | const char *dbg_f, int dbg_l) |
491 | 348k | { |
492 | 348k | int skip, rc; |
493 | | |
494 | 348k | do |
495 | 349k | { |
496 | 349k | rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l); |
497 | 349k | } |
498 | 349k | while (skip && ! rc); |
499 | 348k | return rc; |
500 | 348k | } |
501 | | #else /*!DEBUG_PARSE_PACKET*/ |
502 | | int |
503 | | parse_packet (parse_packet_ctx_t ctx, PACKET *pkt) |
504 | | { |
505 | | int skip, rc; |
506 | | |
507 | | do |
508 | | { |
509 | | rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0); |
510 | | } |
511 | | while (skip && ! rc); |
512 | | return rc; |
513 | | } |
514 | | #endif /*!DEBUG_PARSE_PACKET*/ |
515 | | |
516 | | |
517 | | /* |
518 | | * Like parse packet, but only return secret or public (sub)key |
519 | | * packets. |
520 | | */ |
521 | | #if DEBUG_PARSE_PACKET |
522 | | int |
523 | | dbg_search_packet (parse_packet_ctx_t ctx, PACKET *pkt, |
524 | | off_t * retpos, int with_uid, |
525 | | const char *dbg_f, int dbg_l) |
526 | 0 | { |
527 | 0 | int skip, rc; |
528 | |
|
529 | 0 | do |
530 | 0 | { |
531 | 0 | rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0, "search", |
532 | 0 | dbg_f, dbg_l); |
533 | 0 | } |
534 | 0 | while (skip && ! rc); |
535 | 0 | return rc; |
536 | 0 | } |
537 | | #else /*!DEBUG_PARSE_PACKET*/ |
538 | | int |
539 | | search_packet (parse_packet_ctx_t ctx, PACKET *pkt, |
540 | | off_t * retpos, int with_uid) |
541 | | { |
542 | | int skip, rc; |
543 | | |
544 | | do |
545 | | { |
546 | | rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0); |
547 | | } |
548 | | while (skip && ! rc); |
549 | | return rc; |
550 | | } |
551 | | #endif /*!DEBUG_PARSE_PACKET*/ |
552 | | |
553 | | |
554 | | /* |
555 | | * Copy all packets from INP to OUT, thereby removing unused spaces. |
556 | | */ |
557 | | #if DEBUG_PARSE_PACKET |
558 | | int |
559 | | dbg_copy_all_packets (iobuf_t inp, iobuf_t out, const char *dbg_f, int dbg_l) |
560 | 0 | { |
561 | 0 | PACKET pkt; |
562 | 0 | struct parse_packet_ctx_s parsectx; |
563 | 0 | int skip, rc = 0; |
564 | |
|
565 | 0 | if (! out) |
566 | 0 | log_bug ("copy_all_packets: OUT may not be NULL.\n"); |
567 | | |
568 | 0 | init_parse_packet (&parsectx, inp); |
569 | |
|
570 | 0 | do |
571 | 0 | { |
572 | 0 | init_packet (&pkt); |
573 | 0 | } |
574 | 0 | while (! |
575 | 0 | (rc = |
576 | 0 | parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, "copy", |
577 | 0 | dbg_f, dbg_l))); |
578 | |
|
579 | 0 | deinit_parse_packet (&parsectx); |
580 | |
|
581 | 0 | return rc; |
582 | 0 | } |
583 | | #else /*!DEBUG_PARSE_PACKET*/ |
584 | | int |
585 | | copy_all_packets (iobuf_t inp, iobuf_t out) |
586 | | { |
587 | | PACKET pkt; |
588 | | struct parse_packet_ctx_s parsectx; |
589 | | int skip, rc = 0; |
590 | | |
591 | | if (! out) |
592 | | log_bug ("copy_all_packets: OUT may not be NULL.\n"); |
593 | | |
594 | | init_parse_packet (&parsectx, inp); |
595 | | |
596 | | do |
597 | | { |
598 | | init_packet (&pkt); |
599 | | } |
600 | | while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0))); |
601 | | |
602 | | deinit_parse_packet (&parsectx); |
603 | | |
604 | | return rc; |
605 | | } |
606 | | #endif /*!DEBUG_PARSE_PACKET*/ |
607 | | |
608 | | |
609 | | /* |
610 | | * Copy some packets from INP to OUT, thereby removing unused spaces. |
611 | | * Stop at offset STOPoff (i.e. don't copy packets at this or later |
612 | | * offsets) |
613 | | */ |
614 | | #if DEBUG_PARSE_PACKET |
615 | | int |
616 | | dbg_copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff, |
617 | | const char *dbg_f, int dbg_l) |
618 | 0 | { |
619 | 0 | int rc = 0; |
620 | 0 | PACKET pkt; |
621 | 0 | int skip; |
622 | 0 | struct parse_packet_ctx_s parsectx; |
623 | |
|
624 | 0 | init_parse_packet (&parsectx, inp); |
625 | |
|
626 | 0 | do |
627 | 0 | { |
628 | 0 | if (iobuf_tell (inp) >= stopoff) |
629 | 0 | { |
630 | 0 | deinit_parse_packet (&parsectx); |
631 | 0 | return 0; |
632 | 0 | } |
633 | 0 | init_packet (&pkt); |
634 | 0 | } |
635 | 0 | while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, |
636 | 0 | "some", dbg_f, dbg_l))); |
637 | | |
638 | 0 | deinit_parse_packet (&parsectx); |
639 | |
|
640 | 0 | return rc; |
641 | 0 | } |
642 | | #else /*!DEBUG_PARSE_PACKET*/ |
643 | | int |
644 | | copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff) |
645 | | { |
646 | | int rc = 0; |
647 | | PACKET pkt; |
648 | | struct parse_packet_ctx_s parsectx; |
649 | | int skip; |
650 | | |
651 | | init_parse_packet (&parsectx, inp); |
652 | | |
653 | | do |
654 | | { |
655 | | if (iobuf_tell (inp) >= stopoff) |
656 | | { |
657 | | deinit_parse_packet (&parsectx); |
658 | | return 0; |
659 | | } |
660 | | init_packet (&pkt); |
661 | | } |
662 | | while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0))); |
663 | | |
664 | | deinit_parse_packet (&parsectx); |
665 | | |
666 | | return rc; |
667 | | } |
668 | | #endif /*!DEBUG_PARSE_PACKET*/ |
669 | | |
670 | | |
671 | | /* |
672 | | * Skip over N packets |
673 | | */ |
674 | | #if DEBUG_PARSE_PACKET |
675 | | int |
676 | | dbg_skip_some_packets (iobuf_t inp, unsigned n, const char *dbg_f, int dbg_l) |
677 | 0 | { |
678 | 0 | int rc = 0; |
679 | 0 | int skip; |
680 | 0 | PACKET pkt; |
681 | 0 | struct parse_packet_ctx_s parsectx; |
682 | |
|
683 | 0 | init_parse_packet (&parsectx, inp); |
684 | |
|
685 | 0 | for (; n && !rc; n--) |
686 | 0 | { |
687 | 0 | init_packet (&pkt); |
688 | 0 | rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1, "skip", |
689 | 0 | dbg_f, dbg_l); |
690 | 0 | } |
691 | |
|
692 | 0 | deinit_parse_packet (&parsectx); |
693 | |
|
694 | 0 | return rc; |
695 | 0 | } |
696 | | #else /*!DEBUG_PARSE_PACKET*/ |
697 | | int |
698 | | skip_some_packets (iobuf_t inp, unsigned int n) |
699 | | { |
700 | | int rc = 0; |
701 | | int skip; |
702 | | PACKET pkt; |
703 | | struct parse_packet_ctx_s parsectx; |
704 | | |
705 | | init_parse_packet (&parsectx, inp); |
706 | | |
707 | | for (; n && !rc; n--) |
708 | | { |
709 | | init_packet (&pkt); |
710 | | rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1); |
711 | | } |
712 | | |
713 | | deinit_parse_packet (&parsectx); |
714 | | |
715 | | return rc; |
716 | | } |
717 | | #endif /*!DEBUG_PARSE_PACKET*/ |
718 | | |
719 | | |
720 | | /* Parse a packet and save it in *PKT. |
721 | | |
722 | | If OUT is not NULL and the packet is valid (its type is not 0), |
723 | | then the header, the initial length field and the packet's contents |
724 | | are written to OUT. In this case, the packet is not saved in *PKT. |
725 | | |
726 | | ONLYKEYPKTS is a simple packet filter. If ONLYKEYPKTS is set to 1, |
727 | | then only public subkey packets, public key packets, private subkey |
728 | | packets and private key packets are parsed. The rest are skipped |
729 | | (i.e., the header and the contents are read from the pipeline and |
730 | | discarded). If ONLYKEYPKTS is set to 2, then in addition to the |
731 | | above 4 types of packets, user id packets are also accepted. |
732 | | |
733 | | DO_SKIP is a more coarse grained filter. Unless ONLYKEYPKTS is set |
734 | | to 2 and the packet is a user id packet, all packets are skipped. |
735 | | |
736 | | Finally, if a packet is invalid (it's type is 0), it is skipped. |
737 | | |
738 | | If a packet is skipped and SKIP is not NULL, then *SKIP is set to |
739 | | 1. |
740 | | |
741 | | Note: ONLYKEYPKTS and DO_SKIP are only respected if OUT is NULL, |
742 | | i.e., the packets are not simply being copied. |
743 | | |
744 | | If RETPOS is not NULL, then the position of CTX->INP (as returned by |
745 | | iobuf_tell) is saved there before any data is read from CTX->INP. |
746 | | */ |
747 | | static int |
748 | | parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, off_t * retpos, |
749 | | int *skip, IOBUF out, int do_skip |
750 | | #if DEBUG_PARSE_PACKET |
751 | | , const char *dbg_w, const char *dbg_f, int dbg_l |
752 | | #endif |
753 | | ) |
754 | 349k | { |
755 | 349k | int rc = 0; |
756 | 349k | iobuf_t inp; |
757 | 349k | int c, ctb, pkttype, lenbytes; |
758 | 349k | unsigned long pktlen; |
759 | 349k | byte hdr[8]; |
760 | 349k | int hdrlen; |
761 | 349k | int new_ctb = 0, partial = 0; |
762 | 349k | int with_uid = (onlykeypkts == 2); |
763 | 349k | off_t pos; |
764 | | |
765 | 349k | *skip = 0; |
766 | 349k | inp = ctx->inp; |
767 | | |
768 | 388k | again: |
769 | 388k | log_assert (!pkt->pkt.generic); |
770 | 388k | if (retpos || list_mode || opt.verbose > 1) |
771 | 0 | { |
772 | 0 | pos = iobuf_tell (inp); |
773 | 0 | if (retpos) |
774 | 0 | *retpos = pos; |
775 | 0 | } |
776 | 388k | else |
777 | 388k | pos = 0; /* (silence compiler warning) */ |
778 | | |
779 | | /* The first byte of a packet is the so-called tag. The highest bit |
780 | | must be set. */ |
781 | 388k | if ((ctb = iobuf_get (inp)) == -1) |
782 | 7.84k | { |
783 | 7.84k | rc = -1; |
784 | 7.84k | goto leave; |
785 | 7.84k | } |
786 | 380k | ctx->last_ctb = ctb; |
787 | 380k | hdrlen = 0; |
788 | 380k | hdr[hdrlen++] = ctb; |
789 | | |
790 | 380k | if (!(ctb & 0x80)) |
791 | 26.4k | { |
792 | 26.4k | log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb); |
793 | 26.4k | rc = gpg_error (GPG_ERR_INV_PACKET); |
794 | 26.4k | goto leave; |
795 | 26.4k | } |
796 | | |
797 | | /* Immediately following the header is the length. There are two |
798 | | * formats: the old format and the new format. If bit 6 (where the |
799 | | * least significant bit is bit 0) is set in the tag, then we are |
800 | | * dealing with a new format packet. Otherwise, it is an old format |
801 | | * packet. In the new format the packet's type is encoded in the 6 |
802 | | * least significant bits of the tag; in the old format it is |
803 | | * encoded in bits 2-5. */ |
804 | 354k | pktlen = 0; |
805 | 354k | new_ctb = !!(ctb & 0x40); |
806 | 354k | if (new_ctb) |
807 | 217k | pkttype = ctb & 0x3f; |
808 | 137k | else |
809 | 137k | pkttype = (ctb >> 2) & 0xf; |
810 | | |
811 | 354k | if (ctx->only_fookey_enc |
812 | 0 | && !(pkttype == PKT_SYMKEY_ENC || pkttype == PKT_PUBKEY_ENC)) |
813 | 0 | { |
814 | 0 | rc = gpg_error (GPG_ERR_TRUE); |
815 | 0 | goto leave; |
816 | 0 | } |
817 | | |
818 | 354k | if (new_ctb) |
819 | 217k | { |
820 | | /* Extract the packet's length. New format packets have 4 ways |
821 | | to encode the packet length. The value of the first byte |
822 | | determines the encoding and partially determines the length. |
823 | | See section 4.2.2 of RFC 4880 for details. */ |
824 | 217k | if ((c = iobuf_get (inp)) == -1) |
825 | 358 | { |
826 | 358 | log_error ("%s: 1st length byte missing\n", iobuf_where (inp)); |
827 | 358 | rc = gpg_error (GPG_ERR_INV_PACKET); |
828 | 358 | goto leave; |
829 | 358 | } |
830 | | |
831 | | |
832 | 216k | hdr[hdrlen++] = c; |
833 | 216k | if (c < 192) |
834 | 168k | pktlen = c; |
835 | 48.4k | else if (c < 224) |
836 | 22.1k | { |
837 | 22.1k | pktlen = (c - 192) * 256; |
838 | 22.1k | if ((c = iobuf_get (inp)) == -1) |
839 | 293 | { |
840 | 293 | log_error ("%s: 2nd length byte missing\n", |
841 | 293 | iobuf_where (inp)); |
842 | 293 | rc = gpg_error (GPG_ERR_INV_PACKET); |
843 | 293 | goto leave; |
844 | 293 | } |
845 | 21.9k | hdr[hdrlen++] = c; |
846 | 21.9k | pktlen += c + 192; |
847 | 21.9k | } |
848 | 26.2k | else if (c == 255) |
849 | 918 | { |
850 | 918 | int i; |
851 | 918 | char value[4]; |
852 | | |
853 | 4.49k | for (i = 0; i < 4; i ++) |
854 | 3.61k | { |
855 | 3.61k | if ((c = iobuf_get (inp)) == -1) |
856 | 33 | { |
857 | 33 | log_error ("%s: 4 byte length invalid\n", iobuf_where (inp)); |
858 | 33 | rc = gpg_error (GPG_ERR_INV_PACKET); |
859 | 33 | goto leave; |
860 | 33 | } |
861 | 3.57k | value[i] = hdr[hdrlen++] = c; |
862 | 3.57k | } |
863 | | |
864 | 885 | pktlen = buf32_to_ulong (value); |
865 | 885 | } |
866 | 25.3k | else /* Partial body length. */ |
867 | 25.3k | { |
868 | 25.3k | switch (pkttype) |
869 | 25.3k | { |
870 | 3.81k | case PKT_PLAINTEXT: |
871 | 4.50k | case PKT_ENCRYPTED: |
872 | 5.62k | case PKT_ENCRYPTED_MDC: |
873 | 5.75k | case PKT_ENCRYPTED_OCB: |
874 | 25.0k | case PKT_COMPRESSED: |
875 | 25.0k | iobuf_set_partial_body_length_mode (inp, c & 0xff); |
876 | 25.0k | pktlen = 0; /* To indicate partial length. */ |
877 | 25.0k | partial = 1; |
878 | 25.0k | break; |
879 | | |
880 | 260 | default: |
881 | 260 | log_error ("%s: partial length invalid for" |
882 | 260 | " packet type %d\n", iobuf_where (inp), pkttype); |
883 | 260 | rc = gpg_error (GPG_ERR_INV_PACKET); |
884 | 260 | goto leave; |
885 | 25.3k | } |
886 | 25.3k | } |
887 | | |
888 | 216k | } |
889 | 137k | else /* This is an old format packet. */ |
890 | 137k | { |
891 | | /* The type of length encoding is encoded in bits 0-1 of the |
892 | | tag. */ |
893 | 137k | lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3)); |
894 | 137k | if (!lenbytes) |
895 | 6.45k | { |
896 | 6.45k | pktlen = 0; /* Don't know the value. */ |
897 | | /* This isn't really partial, but we can treat it the same |
898 | | in a "read until the end" sort of way. */ |
899 | 6.45k | partial = 1; |
900 | 6.45k | if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT |
901 | 6.00k | && pkttype != PKT_COMPRESSED) |
902 | 428 | { |
903 | 428 | log_error ("%s: indeterminate length for invalid" |
904 | 428 | " packet type %d\n", iobuf_where (inp), pkttype); |
905 | 428 | rc = gpg_error (GPG_ERR_INV_PACKET); |
906 | 428 | goto leave; |
907 | 428 | } |
908 | 6.45k | } |
909 | 130k | else |
910 | 130k | { |
911 | 271k | for (; lenbytes; lenbytes--) |
912 | 141k | { |
913 | 141k | pktlen <<= 8; |
914 | 141k | c = iobuf_get (inp); |
915 | 141k | if (c == -1) |
916 | 758 | { |
917 | 758 | log_error ("%s: length invalid\n", iobuf_where (inp)); |
918 | 758 | rc = gpg_error (GPG_ERR_INV_PACKET); |
919 | 758 | goto leave; |
920 | 758 | } |
921 | 140k | pktlen |= hdr[hdrlen++] = c; |
922 | 140k | } |
923 | 130k | } |
924 | 137k | } |
925 | | |
926 | | /* Sometimes the decompressing layer enters an error state in which |
927 | | it simply outputs 0xff for every byte read. If we have a stream |
928 | | of 0xff bytes, then it will be detected as a new format packet |
929 | | with type 63 and a 4-byte encoded length that is 4G-1. Since |
930 | | packets with type 63 are private and we use them as a control |
931 | | packet, which won't be 4 GB, we reject such packets as |
932 | | invalid. */ |
933 | 351k | if (pkttype == 63 && pktlen == 0xFFFFFFFF) |
934 | 0 | { |
935 | | /* With some probability this is caused by a problem in the |
936 | | * the uncompressing layer - in some error cases it just loops |
937 | | * and spits out 0xff bytes. */ |
938 | 0 | log_error ("%s: garbled packet detected\n", iobuf_where (inp)); |
939 | 0 | g10_exit (2); |
940 | 0 | } |
941 | | |
942 | 351k | if (out && pkttype) |
943 | 0 | { |
944 | | /* This type of copying won't work if the packet uses a partial |
945 | | body length. (In other words, this only works if HDR is |
946 | | actually the length.) Currently, no callers require this |
947 | | functionality so we just log this as an error. */ |
948 | 0 | if (partial) |
949 | 0 | { |
950 | 0 | log_error ("parse: Can't copy partial packet. Aborting.\n"); |
951 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
952 | 0 | goto leave; |
953 | 0 | } |
954 | | |
955 | 0 | rc = iobuf_write (out, hdr, hdrlen); |
956 | 0 | if (!rc) |
957 | 0 | rc = copy_packet (inp, out, pkttype, pktlen, partial); |
958 | 0 | goto leave; |
959 | 0 | } |
960 | | |
961 | 351k | if (with_uid && pkttype == PKT_USER_ID) |
962 | | /* If ONLYKEYPKTS is set to 2, then we never skip user id packets, |
963 | | even if DO_SKIP is set. */ |
964 | 0 | ; |
965 | 351k | else if (do_skip |
966 | | /* type==0 is not allowed. This is an invalid packet. */ |
967 | 351k | || !pkttype |
968 | | /* When ONLYKEYPKTS is set, we don't skip keys. */ |
969 | 351k | || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY |
970 | 0 | && pkttype != PKT_PUBLIC_KEY |
971 | 0 | && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY)) |
972 | 893 | { |
973 | 893 | iobuf_skip_rest (inp, pktlen, partial); |
974 | 893 | *skip = 1; |
975 | 893 | rc = 0; |
976 | 893 | goto leave; |
977 | 893 | } |
978 | | |
979 | 351k | if (DBG_PACKET) |
980 | 0 | { |
981 | 0 | #if DEBUG_PARSE_PACKET |
982 | 0 | log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n", |
983 | 0 | iobuf_id (inp), pkttype, pktlen, new_ctb ? " (new_ctb)" : "", |
984 | 0 | dbg_w, dbg_f, dbg_l); |
985 | | #else |
986 | | log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n", |
987 | | iobuf_id (inp), pkttype, pktlen, |
988 | | new_ctb ? " (new_ctb)" : ""); |
989 | | #endif |
990 | 0 | } |
991 | | |
992 | 351k | if (list_mode) |
993 | 351k | es_fprintf (listfp, "# off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s\n", |
994 | 0 | (unsigned long)pos, ctb, pkttype, hdrlen, pktlen, |
995 | 0 | partial? (new_ctb ? " partial" : " indeterminate") :"", |
996 | 0 | new_ctb? " new-ctb":""); |
997 | | |
998 | | /* Count it. */ |
999 | 351k | ctx->n_parsed_packets++; |
1000 | | |
1001 | 351k | pkt->pkttype = pkttype; |
1002 | 351k | rc = GPG_ERR_UNKNOWN_PACKET; /* default error */ |
1003 | 351k | switch (pkttype) |
1004 | 351k | { |
1005 | 26.3k | case PKT_PUBLIC_KEY: |
1006 | 33.7k | case PKT_PUBLIC_SUBKEY: |
1007 | 35.7k | case PKT_SECRET_KEY: |
1008 | 41.0k | case PKT_SECRET_SUBKEY: |
1009 | 41.0k | pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key); |
1010 | 41.0k | rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt); |
1011 | 41.0k | break; |
1012 | 15.1k | case PKT_SYMKEY_ENC: |
1013 | 15.1k | rc = parse_symkeyenc (inp, pkttype, pktlen, pkt); |
1014 | 15.1k | break; |
1015 | 6.59k | case PKT_PUBKEY_ENC: |
1016 | 6.59k | rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt); |
1017 | 6.59k | break; |
1018 | 75.1k | case PKT_SIGNATURE: |
1019 | 75.1k | pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature); |
1020 | 75.1k | rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature); |
1021 | 75.1k | break; |
1022 | 7.25k | case PKT_ONEPASS_SIG: |
1023 | 7.25k | pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig); |
1024 | 7.25k | rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig); |
1025 | 7.25k | break; |
1026 | 31.3k | case PKT_USER_ID: |
1027 | 31.3k | rc = parse_user_id (inp, pkttype, pktlen, pkt); |
1028 | 31.3k | break; |
1029 | 10.2k | case PKT_ATTRIBUTE: |
1030 | 10.2k | pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */ |
1031 | 10.2k | rc = parse_attribute (inp, pkttype, pktlen, pkt); |
1032 | 10.2k | break; |
1033 | 4.55k | case PKT_OLD_COMMENT: |
1034 | 5.05k | case PKT_COMMENT: |
1035 | 5.05k | rc = parse_comment (inp, pkttype, pktlen, pkt); |
1036 | 5.05k | break; |
1037 | 38.9k | case PKT_RING_TRUST: |
1038 | 38.9k | { |
1039 | 38.9k | rc = parse_ring_trust (ctx, pktlen); |
1040 | 38.9k | if (!rc) |
1041 | 38.9k | goto again; /* Directly read the next packet. */ |
1042 | 38.9k | } |
1043 | 0 | break; |
1044 | 21.2k | case PKT_PLAINTEXT: |
1045 | 21.2k | rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial); |
1046 | 21.2k | break; |
1047 | 51.7k | case PKT_COMPRESSED: |
1048 | 51.7k | rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb); |
1049 | 51.7k | break; |
1050 | 10.1k | case PKT_ENCRYPTED: |
1051 | 11.8k | case PKT_ENCRYPTED_MDC: |
1052 | 11.8k | rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial); |
1053 | 11.8k | break; |
1054 | 961 | case PKT_MDC: |
1055 | 961 | rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb); |
1056 | 961 | break; |
1057 | 22.5k | case PKT_ENCRYPTED_OCB: |
1058 | 22.5k | rc = parse_encrypted_ocb (inp, pkttype, pktlen, pkt, partial); |
1059 | 22.5k | break; |
1060 | 4.29k | case PKT_GPG_CONTROL: |
1061 | 4.29k | rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial); |
1062 | 4.29k | break; |
1063 | 1.45k | case PKT_MARKER: |
1064 | 1.45k | rc = parse_marker (inp, pkttype, pktlen); |
1065 | 1.45k | break; |
1066 | 6.18k | default: |
1067 | | /* Unknown packet. Skip it. */ |
1068 | 6.18k | skip_packet (inp, pkttype, pktlen, partial); |
1069 | 6.18k | break; |
1070 | 351k | } |
1071 | | |
1072 | 312k | if (gpg_err_code (rc) == GPG_ERR_INV_PACKET && opt.verbose > 1) |
1073 | 0 | { |
1074 | 0 | log_error ("parse_packet: read error: %s\n", gpg_strerror (rc)); |
1075 | 0 | print_further_info |
1076 | 0 | ("packet %u off=%lu ctb=%02x tag=%d hlen=%d plen=%lu%s%s", |
1077 | 0 | ctx->n_parsed_packets, |
1078 | 0 | (unsigned long)pos, ctb, pkttype, hdrlen, pktlen, |
1079 | 0 | partial? (new_ctb ? " partial" : " indeterminate") :"", |
1080 | 0 | new_ctb? " new-ctb":""); |
1081 | 0 | } |
1082 | | |
1083 | | /* Store a shallow copy of certain packets in the context. */ |
1084 | 312k | free_packet (NULL, ctx); |
1085 | 312k | if (!rc && (pkttype == PKT_PUBLIC_KEY |
1086 | 258k | || pkttype == PKT_SECRET_KEY |
1087 | 256k | || pkttype == PKT_USER_ID |
1088 | 215k | || pkttype == PKT_ATTRIBUTE |
1089 | 215k | || pkttype == PKT_SIGNATURE)) |
1090 | 130k | { |
1091 | 130k | ctx->last_pkt = *pkt; |
1092 | 130k | } |
1093 | | |
1094 | 349k | leave: |
1095 | | /* FIXME: We leak in case of an error (see the xmalloc's above). */ |
1096 | 349k | if (!rc && iobuf_error (inp)) |
1097 | 599 | rc = GPG_ERR_INV_KEYRING; |
1098 | | |
1099 | | /* FIXME: We use only the error code for now to avoid problems with |
1100 | | callers which have not been checked to always use gpg_err_code() |
1101 | | when comparing error codes. */ |
1102 | 349k | return rc == -1? -1 : gpg_err_code (rc); |
1103 | 312k | } |
1104 | | |
1105 | | |
1106 | | static void |
1107 | | dump_hex_line (int c, int *i) |
1108 | 0 | { |
1109 | 0 | if (*i && !(*i % 8)) |
1110 | 0 | { |
1111 | 0 | if (*i && !(*i % 24)) |
1112 | 0 | es_fprintf (listfp, "\n%4d:", *i); |
1113 | 0 | else |
1114 | 0 | es_putc (' ', listfp); |
1115 | 0 | } |
1116 | 0 | if (c == -1) |
1117 | 0 | es_fprintf (listfp, " EOF"); |
1118 | 0 | else |
1119 | 0 | es_fprintf (listfp, " %02x", c); |
1120 | 0 | ++*i; |
1121 | 0 | } |
1122 | | |
1123 | | |
1124 | | /* Copy the contents of a packet from the pipeline IN to the pipeline |
1125 | | OUT. |
1126 | | |
1127 | | The header and length have already been read from INP and the |
1128 | | decoded values are given as PKGTYPE and PKTLEN. |
1129 | | |
1130 | | If the packet is a partial body length packet (RFC 4880, Section |
1131 | | 4.2.2.4), then iobuf_set_partial_block_modeiobuf_set_partial_block_mode |
1132 | | should already have been called on INP and PARTIAL should be set. |
1133 | | |
1134 | | If PARTIAL is set or PKTLEN is 0 and PKTTYPE is PKT_COMPRESSED, |
1135 | | copy until the first EOF is encountered on INP. |
1136 | | |
1137 | | Returns 0 on success and an error code if an error occurs. */ |
1138 | | static int |
1139 | | copy_packet (IOBUF inp, IOBUF out, int pkttype, |
1140 | | unsigned long pktlen, int partial) |
1141 | 0 | { |
1142 | 0 | int rc; |
1143 | 0 | int n; |
1144 | 0 | char buf[100]; |
1145 | |
|
1146 | 0 | if (partial) |
1147 | 0 | { |
1148 | 0 | while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1) |
1149 | 0 | if ((rc = iobuf_write (out, buf, n))) |
1150 | 0 | return rc; /* write error */ |
1151 | 0 | } |
1152 | 0 | else if (!pktlen && pkttype == PKT_COMPRESSED) |
1153 | 0 | { |
1154 | 0 | log_debug ("copy_packet: compressed!\n"); |
1155 | | /* compressed packet, copy till EOF */ |
1156 | 0 | while ((n = iobuf_read (inp, buf, sizeof (buf))) != -1) |
1157 | 0 | if ((rc = iobuf_write (out, buf, n))) |
1158 | 0 | return rc; /* write error */ |
1159 | 0 | } |
1160 | 0 | else |
1161 | 0 | { |
1162 | 0 | for (; pktlen; pktlen -= n) |
1163 | 0 | { |
1164 | 0 | n = pktlen > sizeof (buf) ? sizeof (buf) : pktlen; |
1165 | 0 | n = iobuf_read (inp, buf, n); |
1166 | 0 | if (n == -1) |
1167 | 0 | return gpg_error (GPG_ERR_EOF); |
1168 | 0 | if ((rc = iobuf_write (out, buf, n))) |
1169 | 0 | return rc; /* write error */ |
1170 | 0 | } |
1171 | 0 | } |
1172 | 0 | return 0; |
1173 | 0 | } |
1174 | | |
1175 | | |
1176 | | /* Skip an unknown packet. PKTTYPE is the packet's type, PKTLEN is |
1177 | | the length of the packet's content and PARTIAL is whether partial |
1178 | | body length encoding in used (in this case PKTLEN is ignored). */ |
1179 | | static void |
1180 | | skip_packet (IOBUF inp, int pkttype, unsigned long pktlen, int partial) |
1181 | 6.18k | { |
1182 | 6.18k | if (list_mode) |
1183 | 0 | { |
1184 | 0 | es_fprintf (listfp, ":unknown packet: type %2d, length %lu\n", |
1185 | 0 | pkttype, pktlen); |
1186 | 0 | if (pkttype) |
1187 | 0 | { |
1188 | 0 | int c, i = 0; |
1189 | 0 | es_fputs ("dump:", listfp); |
1190 | 0 | if (partial) |
1191 | 0 | { |
1192 | 0 | while ((c = iobuf_get (inp)) != -1) |
1193 | 0 | dump_hex_line (c, &i); |
1194 | 0 | } |
1195 | 0 | else |
1196 | 0 | { |
1197 | 0 | for (; pktlen; pktlen--) |
1198 | 0 | { |
1199 | 0 | dump_hex_line ((c = iobuf_get (inp)), &i); |
1200 | 0 | if (c == -1) |
1201 | 0 | break; |
1202 | 0 | } |
1203 | 0 | } |
1204 | 0 | es_putc ('\n', listfp); |
1205 | 0 | return; |
1206 | 0 | } |
1207 | 0 | } |
1208 | 6.18k | iobuf_skip_rest (inp, pktlen, partial); |
1209 | 6.18k | } |
1210 | | |
1211 | | |
1212 | | /* Read PKTLEN bytes from INP and return them in a newly allocated |
1213 | | * buffer. In case of an error (including reading fewer than PKTLEN |
1214 | | * bytes from INP before EOF is returned), NULL is returned and an |
1215 | | * error message is logged. */ |
1216 | | static void * |
1217 | | read_rest (IOBUF inp, size_t pktlen) |
1218 | 63.4k | { |
1219 | 63.4k | int c; |
1220 | 63.4k | byte *buf, *p; |
1221 | | |
1222 | 63.4k | buf = xtrymalloc (pktlen); |
1223 | 63.4k | if (!buf) |
1224 | 35.8k | { |
1225 | 35.8k | gpg_error_t err = gpg_error_from_syserror (); |
1226 | 35.8k | log_error ("error reading rest of packet: %s\n", gpg_strerror (err)); |
1227 | 35.8k | return NULL; |
1228 | 35.8k | } |
1229 | 268k | for (p = buf; pktlen; pktlen--) |
1230 | 242k | { |
1231 | 242k | c = iobuf_get (inp); |
1232 | 242k | if (c == -1) |
1233 | 1.33k | { |
1234 | 1.33k | log_error ("premature eof while reading rest of packet\n"); |
1235 | 1.33k | xfree (buf); |
1236 | 1.33k | return NULL; |
1237 | 1.33k | } |
1238 | 241k | *p++ = c; |
1239 | 241k | } |
1240 | | |
1241 | 26.2k | return buf; |
1242 | 27.6k | } |
1243 | | |
1244 | | |
1245 | | /* Read a special size+body from INP. On success store an opaque MPI |
1246 | | * with it at R_DATA. The caller shall store the remaining size of |
1247 | | * the packet at PKTLEN. On error return an error code and store NULL |
1248 | | * at R_DATA. Even in the error case store the number of read bytes |
1249 | | * at PKTLEN is updated. */ |
1250 | | static gpg_error_t |
1251 | | read_sized_octet_string (iobuf_t inp, unsigned long *pktlen, gcry_mpi_t *r_data) |
1252 | 17.3k | { |
1253 | 17.3k | char buffer[256]; |
1254 | 17.3k | char *tmpbuf; |
1255 | 17.3k | int i, c, nbytes; |
1256 | | |
1257 | 17.3k | *r_data = NULL; |
1258 | | |
1259 | 17.3k | if (!*pktlen) |
1260 | 1 | return gpg_error (GPG_ERR_INV_PACKET); |
1261 | 17.3k | c = iobuf_readbyte (inp); |
1262 | 17.3k | if (c < 0) |
1263 | 29 | return gpg_error (GPG_ERR_INV_PACKET); |
1264 | 17.3k | --*pktlen; |
1265 | 17.3k | nbytes = c; |
1266 | 17.3k | if (nbytes < 2 || nbytes > 254) |
1267 | 4 | return gpg_error (GPG_ERR_INV_PACKET); |
1268 | 17.3k | if (nbytes > *pktlen) |
1269 | 15 | return gpg_error (GPG_ERR_INV_PACKET); |
1270 | | |
1271 | 17.3k | buffer[0] = nbytes; |
1272 | | |
1273 | 63.0k | for (i = 0; i < nbytes; i++) |
1274 | 45.7k | { |
1275 | 45.7k | c = iobuf_get (inp); |
1276 | 45.7k | if (c < 0) |
1277 | 34 | return gpg_error (GPG_ERR_INV_PACKET); |
1278 | 45.7k | --*pktlen; |
1279 | 45.7k | buffer[1+i] = c; |
1280 | 45.7k | } |
1281 | | |
1282 | 17.2k | tmpbuf = xtrymalloc (1 + nbytes); |
1283 | 17.2k | if (!tmpbuf) |
1284 | 0 | return gpg_error_from_syserror (); |
1285 | 17.2k | memcpy (tmpbuf, buffer, 1 + nbytes); |
1286 | 17.2k | *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes)); |
1287 | 17.2k | if (!*r_data) |
1288 | 0 | { |
1289 | 0 | xfree (tmpbuf); |
1290 | 0 | return gpg_error_from_syserror (); |
1291 | 0 | } |
1292 | 17.2k | return 0; |
1293 | 17.2k | } |
1294 | | |
1295 | | |
1296 | | /* Parse a marker packet. */ |
1297 | | static int |
1298 | | parse_marker (IOBUF inp, int pkttype, unsigned long pktlen) |
1299 | 1.45k | { |
1300 | 1.45k | (void) pkttype; |
1301 | | |
1302 | 1.45k | if (pktlen != 3) |
1303 | 187 | goto fail; |
1304 | | |
1305 | 1.26k | if (iobuf_get (inp) != 'P') |
1306 | 10 | { |
1307 | 10 | pktlen--; |
1308 | 10 | goto fail; |
1309 | 10 | } |
1310 | | |
1311 | 1.25k | if (iobuf_get (inp) != 'G') |
1312 | 18 | { |
1313 | 18 | pktlen--; |
1314 | 18 | goto fail; |
1315 | 18 | } |
1316 | | |
1317 | 1.23k | if (iobuf_get (inp) != 'P') |
1318 | 13 | { |
1319 | 13 | pktlen--; |
1320 | 13 | goto fail; |
1321 | 13 | } |
1322 | | |
1323 | 1.22k | if (list_mode) |
1324 | 1.22k | es_fputs (":marker packet: PGP\n", listfp); |
1325 | | |
1326 | 1.22k | return 0; |
1327 | | |
1328 | 228 | fail: |
1329 | 228 | log_error ("invalid marker packet\n"); |
1330 | 228 | if (list_mode) |
1331 | 228 | es_fputs (":marker packet: [invalid]\n", listfp); |
1332 | 228 | iobuf_skip_rest (inp, pktlen, 0); |
1333 | 228 | return GPG_ERR_INV_PACKET; |
1334 | 1.23k | } |
1335 | | |
1336 | | |
1337 | | static int |
1338 | | parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen, |
1339 | | PACKET * packet) |
1340 | 15.1k | { |
1341 | 15.1k | PKT_symkey_enc *k; |
1342 | 15.1k | int rc = 0; |
1343 | 15.1k | int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen; |
1344 | | |
1345 | 15.1k | if (pktlen < 4) |
1346 | 483 | goto too_short; |
1347 | 14.6k | version = iobuf_get_noeof (inp); |
1348 | 14.6k | pktlen--; |
1349 | 14.6k | if (version == 4) |
1350 | 11.7k | ; |
1351 | 2.87k | else if (version == 5) |
1352 | 2.61k | ; |
1353 | 267 | else |
1354 | 267 | { |
1355 | 267 | log_error ("packet(%d) with unknown version %d\n", pkttype, version); |
1356 | 267 | if (list_mode) |
1357 | 267 | es_fprintf (listfp, ":symkey enc packet: [unknown version]\n"); |
1358 | 267 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1359 | 267 | goto leave; |
1360 | 267 | } |
1361 | 14.3k | if (pktlen > 200) |
1362 | 19 | { /* (we encode the seskeylen in a byte) */ |
1363 | 19 | log_error ("packet(%d) too large\n", pkttype); |
1364 | 19 | if (list_mode) |
1365 | 19 | es_fprintf (listfp, ":symkey enc packet: [too large]\n"); |
1366 | 19 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1367 | 19 | goto leave; |
1368 | 19 | } |
1369 | 14.3k | cipher_algo = iobuf_get_noeof (inp); |
1370 | 14.3k | pktlen--; |
1371 | 14.3k | if (version == 5) |
1372 | 2.60k | { |
1373 | 2.60k | aead_algo = iobuf_get_noeof (inp); |
1374 | 2.60k | pktlen--; |
1375 | 2.60k | } |
1376 | 11.7k | else |
1377 | 11.7k | aead_algo = 0; |
1378 | 14.3k | if (pktlen < 2) |
1379 | 552 | goto too_short; |
1380 | 13.7k | s2kmode = iobuf_get_noeof (inp); |
1381 | 13.7k | pktlen--; |
1382 | 13.7k | hash_algo = iobuf_get_noeof (inp); |
1383 | 13.7k | pktlen--; |
1384 | 13.7k | switch (s2kmode) |
1385 | 13.7k | { |
1386 | 8.01k | case 0: /* Simple S2K. */ |
1387 | 8.01k | minlen = 0; |
1388 | 8.01k | break; |
1389 | 1.99k | case 1: /* Salted S2K. */ |
1390 | 1.99k | minlen = 8; |
1391 | 1.99k | break; |
1392 | 2.65k | case 3: /* Iterated+salted S2K. */ |
1393 | 2.65k | minlen = 9; |
1394 | 2.65k | break; |
1395 | 1.12k | default: |
1396 | 1.12k | log_error ("unknown S2K mode %d\n", s2kmode); |
1397 | 1.12k | if (list_mode) |
1398 | 1.12k | es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n"); |
1399 | 1.12k | goto leave; |
1400 | 13.7k | } |
1401 | 12.6k | if (minlen > pktlen) |
1402 | 3 | { |
1403 | 3 | log_error ("packet with S2K %d too short\n", s2kmode); |
1404 | 3 | if (list_mode) |
1405 | 3 | es_fprintf (listfp, ":symkey enc packet: [too short]\n"); |
1406 | 3 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1407 | 3 | goto leave; |
1408 | 3 | } |
1409 | 12.6k | seskeylen = pktlen - minlen; |
1410 | 12.6k | k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc); |
1411 | 12.6k | k->version = version; |
1412 | 12.6k | k->cipher_algo = cipher_algo; |
1413 | 12.6k | k->aead_algo = aead_algo; |
1414 | 12.6k | k->s2k.mode = s2kmode; |
1415 | 12.6k | k->s2k.hash_algo = hash_algo; |
1416 | 12.6k | if (s2kmode == 1 || s2kmode == 3) |
1417 | 4.64k | { |
1418 | 41.8k | for (i = 0; i < 8 && pktlen; i++, pktlen--) |
1419 | 37.1k | k->s2k.salt[i] = iobuf_get_noeof (inp); |
1420 | 4.64k | } |
1421 | 12.6k | if (s2kmode == 3) |
1422 | 2.65k | { |
1423 | 2.65k | k->s2k.count = iobuf_get_noeof (inp); |
1424 | 2.65k | pktlen--; |
1425 | 2.65k | } |
1426 | 12.6k | k->seskeylen = seskeylen; |
1427 | 12.6k | if (k->seskeylen) |
1428 | 3.11k | { |
1429 | 3.11k | k->seskey = xcalloc (1, seskeylen); |
1430 | 40.7k | for (i = 0; i < seskeylen && pktlen; i++, pktlen--) |
1431 | 37.6k | k->seskey[i] = iobuf_get_noeof (inp); |
1432 | | |
1433 | | /* What we're watching out for here is a session key decryptor |
1434 | | with no salt. The RFC says that using salt for this is a |
1435 | | MUST. */ |
1436 | 3.11k | if (s2kmode != 1 && s2kmode != 3) |
1437 | 3.11k | log_info (_("WARNING: potentially insecure symmetrically" |
1438 | 1.23k | " encrypted session key\n")); |
1439 | 3.11k | } |
1440 | 12.6k | log_assert (!pktlen); |
1441 | | |
1442 | 12.6k | if (list_mode) |
1443 | 0 | { |
1444 | 0 | es_fprintf (listfp, |
1445 | 0 | ":symkey enc packet: version %d, cipher %d, aead %d," |
1446 | 0 | " s2k %d, hash %d", |
1447 | 0 | version, cipher_algo, aead_algo, s2kmode, hash_algo); |
1448 | 0 | if (seskeylen) |
1449 | 0 | { |
1450 | | /* To compute the size of the session key we need to know |
1451 | | * the size of the AEAD nonce which we may not know. Thus |
1452 | | * we show only the size of the entire encrypted session |
1453 | | * key. */ |
1454 | 0 | if (aead_algo) |
1455 | 0 | es_fprintf (listfp, ", encrypted seskey %d bytes", seskeylen); |
1456 | 0 | else |
1457 | 0 | es_fprintf (listfp, ", seskey %d bits", (seskeylen - 1) * 8); |
1458 | 0 | } |
1459 | 0 | es_fprintf (listfp, "\n"); |
1460 | 0 | if (s2kmode == 1 || s2kmode == 3) |
1461 | 0 | { |
1462 | 0 | es_fprintf (listfp, "\tsalt "); |
1463 | 0 | es_write_hexstring (listfp, k->s2k.salt, 8, 0, NULL); |
1464 | 0 | if (s2kmode == 3) |
1465 | 0 | es_fprintf (listfp, ", count %lu (%lu)", |
1466 | 0 | S2K_DECODE_COUNT ((ulong) k->s2k.count), |
1467 | 0 | (ulong) k->s2k.count); |
1468 | 0 | es_fprintf (listfp, "\n"); |
1469 | 0 | } |
1470 | 0 | } |
1471 | | |
1472 | 15.1k | leave: |
1473 | 15.1k | iobuf_skip_rest (inp, pktlen, 0); |
1474 | 15.1k | return rc; |
1475 | | |
1476 | 1.03k | too_short: |
1477 | 1.03k | log_error ("packet(%d) too short\n", pkttype); |
1478 | 1.03k | if (list_mode) |
1479 | 1.03k | es_fprintf (listfp, ":symkey enc packet: [too short]\n"); |
1480 | 1.03k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1481 | 1.03k | goto leave; |
1482 | 12.6k | } |
1483 | | |
1484 | | |
1485 | | /* Parse a public key encrypted packet (Tag 1). */ |
1486 | | static int |
1487 | | parse_pubkeyenc (IOBUF inp, int pkttype, unsigned long pktlen, |
1488 | | PACKET * packet) |
1489 | 6.59k | { |
1490 | 6.59k | int rc = 0; |
1491 | 6.59k | int is_v6 = 0; |
1492 | 6.59k | int i, ndata; |
1493 | 6.59k | unsigned int n; |
1494 | 6.59k | PKT_pubkey_enc *k; |
1495 | | |
1496 | 6.59k | k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc); |
1497 | 6.59k | if (pktlen < 12) |
1498 | 319 | { |
1499 | 319 | log_error ("packet(%d) too short\n", pkttype); |
1500 | 319 | if (list_mode) |
1501 | 319 | es_fputs (":pubkey enc packet: [too short]\n", listfp); |
1502 | 319 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1503 | 319 | goto leave; |
1504 | 319 | } |
1505 | 6.27k | k->version = iobuf_get_noeof (inp); |
1506 | 6.27k | pktlen--; |
1507 | 6.27k | if (k->version == 2 || k->version == 3) |
1508 | 6.07k | ; |
1509 | 206 | else if (RFC9980 && k->version == 6) |
1510 | 29 | is_v6 = 1; |
1511 | 177 | else |
1512 | 177 | { |
1513 | 177 | log_error ("packet(%d) with unknown version %d\n", pkttype, k->version); |
1514 | 177 | if (list_mode) |
1515 | 177 | es_fputs (":pubkey enc packet: [unknown version]\n", listfp); |
1516 | 177 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1517 | 177 | goto leave; |
1518 | 177 | } |
1519 | | |
1520 | 6.10k | if (is_v6) |
1521 | 29 | { |
1522 | 29 | int keyver; |
1523 | | |
1524 | 29 | k->fprlen = iobuf_get_noeof (inp); /* Actually the 1 + fprlen. */ |
1525 | 29 | pktlen--; |
1526 | 29 | if (k->fprlen) |
1527 | 28 | { |
1528 | 28 | keyver = iobuf_get_noeof (inp); |
1529 | 28 | pktlen--; k->fprlen--; |
1530 | | /* fprlen has now the correct value. */ |
1531 | 28 | if (pktlen < k->fprlen || k->fprlen > MAX_FINGERPRINT_LEN) |
1532 | 13 | { |
1533 | 13 | log_error ("packet(%d) too short for fingerprint (%d)\n", |
1534 | 13 | pkttype, k->fprlen); |
1535 | 13 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1536 | 13 | goto leave; |
1537 | 13 | } |
1538 | 15 | if (iobuf_read (inp, k->fpr, k->fprlen) != k->fprlen) |
1539 | 5 | { |
1540 | 5 | log_error ("premature eof while reading " |
1541 | 5 | "fingerprint from packet(%d)\n", pkttype); |
1542 | 5 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1543 | 5 | goto leave; |
1544 | 5 | } |
1545 | 10 | pktlen -= k->fprlen; |
1546 | 10 | if (k->fprlen == 20 && keyver == 4) |
1547 | 0 | { |
1548 | 0 | k->keyid[0] = buf32_to_u32 (k->fpr+12); |
1549 | 0 | k->keyid[1] = buf32_to_u32 (k->fpr+16); |
1550 | 0 | } |
1551 | 10 | else if (k->fprlen == 32 && keyver >= 5) |
1552 | 0 | { |
1553 | 0 | k->keyid[0] = buf32_to_u32 (k->fpr); |
1554 | 0 | k->keyid[1] = buf32_to_u32 (k->fpr+4); |
1555 | 0 | } |
1556 | 10 | else |
1557 | 10 | { |
1558 | 10 | log_error ("packet(%d) inconsistent fingerprint (v=%d,n=%d)\n", |
1559 | 10 | pkttype, keyver, k->fprlen); |
1560 | 10 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1561 | 10 | goto leave; |
1562 | 10 | } |
1563 | 10 | } |
1564 | 29 | } |
1565 | 6.07k | else |
1566 | 6.07k | { |
1567 | 6.07k | k->keyid[0] = read_32 (inp); |
1568 | 6.07k | pktlen -= 4; |
1569 | 6.07k | k->keyid[1] = read_32 (inp); |
1570 | 6.07k | pktlen -= 4; |
1571 | 6.07k | } |
1572 | | |
1573 | 6.07k | if (!pktlen) |
1574 | 0 | { |
1575 | 0 | log_error ("packet(%d) too short for public-key algo\n", pkttype); |
1576 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1577 | 0 | goto leave; |
1578 | 0 | } |
1579 | 6.07k | k->pubkey_algo = iobuf_get_noeof (inp); |
1580 | 6.07k | pktlen--; |
1581 | 6.07k | k->throw_keyid = 0; /* Only used as flag for build_packet. */ |
1582 | 6.07k | if (list_mode) |
1583 | 6.07k | es_fprintf (listfp, |
1584 | 0 | ":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n", |
1585 | 0 | k->version, k->pubkey_algo, (ulong) k->keyid[0], |
1586 | 0 | (ulong) k->keyid[1]); |
1587 | | |
1588 | 6.07k | ndata = pubkey_get_nenc (k->pubkey_algo); |
1589 | 6.07k | if (!ndata) |
1590 | 5.10k | { |
1591 | 5.10k | if (list_mode) |
1592 | 5.10k | es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo); |
1593 | 5.10k | unknown_pubkey_warning (k->pubkey_algo); |
1594 | 5.10k | k->data[0] = NULL; /* No need to store the encrypted data. */ |
1595 | 5.10k | } |
1596 | 963 | else if (k->pubkey_algo == PUBKEY_ALGO_ECDH) |
1597 | 351 | { |
1598 | 351 | log_assert (ndata == 2); |
1599 | | /* Get the ephemeral public key. */ |
1600 | 351 | n = pktlen; |
1601 | 351 | k->data[0] = sos_read (inp, &n, 0); |
1602 | 351 | pktlen -= n; |
1603 | 351 | if (!k->data[0]) |
1604 | 31 | { |
1605 | 31 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1606 | 31 | goto leave; |
1607 | 31 | } |
1608 | | /* Get the wrapped symmetric key. */ |
1609 | 320 | rc = read_sized_octet_string (inp, &pktlen, k->data + 1); |
1610 | 320 | if (rc) |
1611 | 12 | goto leave; |
1612 | 320 | } |
1613 | 612 | else if (k->pubkey_algo == PUBKEY_ALGO_X25519 && RFC9980) |
1614 | 6 | { |
1615 | 6 | log_assert (ndata == 2); |
1616 | | /* Get the ephemeral public key. */ |
1617 | 6 | rc = read_raw_octet_string (inp, &pktlen, 0, 32, 0, k->data + 0); |
1618 | 6 | if (rc) |
1619 | 1 | goto leave; |
1620 | 5 | if (pktlen < 2) |
1621 | 0 | { |
1622 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1623 | 0 | goto leave; |
1624 | 0 | } |
1625 | 5 | n = iobuf_get_noeof (inp); |
1626 | 5 | pktlen--; |
1627 | 5 | if (list_mode) |
1628 | 5 | es_fprintf (listfp, "\tlength octet: %u\n", n); |
1629 | 5 | if (!is_v6) |
1630 | 5 | { |
1631 | 5 | k->seskey_algo = iobuf_get_noeof (inp); |
1632 | 5 | if (!n || !pktlen) |
1633 | 1 | { |
1634 | 1 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1635 | 1 | goto leave; |
1636 | 1 | } |
1637 | 4 | pktlen--; |
1638 | 4 | n--; |
1639 | 4 | } |
1640 | | /* Get the encrypted symmetric key (keylen+8 due to AESWRAP). */ |
1641 | 4 | rc = read_raw_octet_string (inp, &pktlen, 0, n, 0, k->data + 1); |
1642 | 4 | if (rc) |
1643 | 3 | goto leave; |
1644 | 4 | } |
1645 | 606 | else if (k->pubkey_algo == PUBKEY_ALGO_KYBER) |
1646 | 34 | { |
1647 | 34 | log_assert (ndata == 3); |
1648 | | /* Get the ephemeral public key. */ |
1649 | 34 | n = pktlen; |
1650 | 34 | k->data[0] = sos_read (inp, &n, 0); |
1651 | 34 | pktlen -= n; |
1652 | 34 | if (!k->data[0]) |
1653 | 11 | { |
1654 | 11 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1655 | 11 | goto leave; |
1656 | 11 | } |
1657 | | /* Get the Kyber ciphertext. */ |
1658 | 23 | rc = read_sos_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1); |
1659 | 23 | if (rc) |
1660 | 23 | goto leave; |
1661 | | /* Get the algorithm id for the session key. */ |
1662 | 0 | if (!pktlen) |
1663 | 0 | { |
1664 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1665 | 0 | goto leave; |
1666 | 0 | } |
1667 | 0 | k->seskey_algo = iobuf_get_noeof (inp); |
1668 | 0 | pktlen--; |
1669 | | /* Get the encrypted symmetric key. */ |
1670 | 0 | rc = read_sos_octet_string (inp, &pktlen, 1, 0, 0, k->data + 2); |
1671 | 0 | if (rc) |
1672 | 0 | goto leave; |
1673 | 0 | } |
1674 | 572 | else if (IS_PUBKEY_ALGO_MLK (k->pubkey_algo)) |
1675 | 14 | { |
1676 | 14 | log_assert (ndata == 3); |
1677 | | /* Get the ephemeral public key. */ |
1678 | 14 | rc = read_raw_octet_string |
1679 | 14 | (inp, &pktlen, 0, |
1680 | 14 | (k->pubkey_algo == PUBKEY_ALGO_MLK768_25519? 32 : |
1681 | 14 | k->pubkey_algo == PUBKEY_ALGO_MLK768_NP384? 97 : |
1682 | 12 | k->pubkey_algo == PUBKEY_ALGO_MLK768_BP384? 97 : |
1683 | 6 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_448? 56 : |
1684 | 5 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_NP521? 133 : |
1685 | 3 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_BP512? 129 : 0), |
1686 | 14 | 0, k->data + 0); |
1687 | 14 | if (rc) |
1688 | 8 | goto leave; |
1689 | | |
1690 | | /* Get the Kyber ciphertext. */ |
1691 | 6 | rc = read_raw_octet_string (inp, &pktlen, 0, |
1692 | 6 | (IS_PUBKEY_ALGO_MLK768 (k->pubkey_algo)? |
1693 | 5 | 1088 : 1568), |
1694 | 6 | 0, k->data + 1); |
1695 | 6 | if (rc) |
1696 | 3 | goto leave; |
1697 | | /* Get the size octet and algo id for the session key. */ |
1698 | 3 | if (pktlen < 2) |
1699 | 0 | { |
1700 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1701 | 0 | goto leave; |
1702 | 0 | } |
1703 | 3 | n = iobuf_get_noeof (inp); |
1704 | 3 | pktlen--; |
1705 | 3 | if (list_mode) |
1706 | 3 | es_fprintf (listfp, "\tlength octet: %u\n", n); |
1707 | 3 | if (!is_v6) |
1708 | 3 | { |
1709 | 3 | k->seskey_algo = iobuf_get_noeof (inp); |
1710 | 3 | if (!n || !pktlen) |
1711 | 0 | { |
1712 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1713 | 0 | goto leave; |
1714 | 0 | } |
1715 | 3 | pktlen--; |
1716 | 3 | n--; |
1717 | 3 | } |
1718 | | /* Get the encrypted symmetric key (keylen+8 due to AESWRAP). */ |
1719 | 3 | rc = read_raw_octet_string (inp, &pktlen, 0, n, 0, k->data + 2); |
1720 | 3 | if (rc) |
1721 | 1 | goto leave; |
1722 | 3 | } |
1723 | 558 | else |
1724 | 558 | { |
1725 | 1.53k | for (i = 0; i < ndata; i++) |
1726 | 977 | { |
1727 | 977 | n = pktlen; |
1728 | 977 | k->data[i] = mpi_read (inp, &n, 0); |
1729 | 977 | pktlen -= n; |
1730 | 977 | if (!k->data[i]) |
1731 | 46 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1732 | 977 | } |
1733 | 558 | if (rc) |
1734 | 39 | goto leave; |
1735 | 558 | } |
1736 | 5.93k | if (list_mode) |
1737 | 0 | { |
1738 | 0 | if (k->seskey_algo) |
1739 | 0 | es_fprintf (listfp, "\tsession key algo: %d\n", k->seskey_algo); |
1740 | 0 | for (i = 0; i < ndata; i++) |
1741 | 0 | { |
1742 | 0 | es_fprintf (listfp, "\tdata: "); |
1743 | 0 | mpi_print (listfp, k->data[i], mpi_print_mode); |
1744 | 0 | es_putc ('\n', listfp); |
1745 | 0 | } |
1746 | 0 | } |
1747 | | |
1748 | | |
1749 | 6.59k | leave: |
1750 | 6.59k | iobuf_skip_rest (inp, pktlen, 0); |
1751 | 6.59k | return rc; |
1752 | 5.93k | } |
1753 | | |
1754 | | |
1755 | | /* Dump a subpacket to LISTFP. BUFFER contains the subpacket in |
1756 | | * question and points to the type field in the subpacket header (not |
1757 | | * the start of the header). TYPE is the subpacket's type with the |
1758 | | * critical bit cleared. CRITICAL is the value of the CRITICAL bit. |
1759 | | * BUFLEN is the length of the buffer and LENGTH is the length of the |
1760 | | * subpacket according to the subpacket's header. DIGEST_ALGO is the |
1761 | | * digest algo of the signature. */ |
1762 | | static void |
1763 | | dump_sig_subpkt (int hashed, int type, int critical, |
1764 | | const byte * buffer, size_t buflen, size_t length, |
1765 | | int digest_algo) |
1766 | 0 | { |
1767 | 0 | const char *p = NULL; |
1768 | 0 | int i; |
1769 | 0 | int nprinted; |
1770 | | |
1771 | | /* The CERT has warning out with explains how to use GNUPG to detect |
1772 | | * the ARRs - we print our old message here when it is a faked ARR |
1773 | | * and add an additional notice. */ |
1774 | 0 | if (type == SIGSUBPKT_ARR && !hashed) |
1775 | 0 | { |
1776 | 0 | es_fprintf (listfp, |
1777 | 0 | "\tsubpkt %d len %u (additional recipient request)\n" |
1778 | 0 | "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically " |
1779 | 0 | "encrypt to this key and thereby reveal the plaintext to " |
1780 | 0 | "the owner of this ARR key. Detailed info follows:\n", |
1781 | 0 | type, (unsigned) length); |
1782 | 0 | } |
1783 | |
|
1784 | 0 | buffer++; |
1785 | 0 | length--; |
1786 | |
|
1787 | 0 | nprinted = es_fprintf (listfp, "\t%s%ssubpkt %d len %u (", /*) */ |
1788 | 0 | critical ? "critical " : "", |
1789 | 0 | hashed ? "hashed " : "", type, (unsigned) length); |
1790 | 0 | if (nprinted < 1) |
1791 | 0 | nprinted = 1; /*(we use (nprinted-1) later.)*/ |
1792 | 0 | if (length > buflen) |
1793 | 0 | { |
1794 | 0 | es_fprintf (listfp, "too short: buffer is only %u)\n", (unsigned) buflen); |
1795 | 0 | return; |
1796 | 0 | } |
1797 | 0 | switch (type) |
1798 | 0 | { |
1799 | 0 | case SIGSUBPKT_SIG_CREATED: |
1800 | 0 | if (length >= 4) |
1801 | 0 | es_fprintf (listfp, "sig created %s", |
1802 | 0 | strtimestamp (buf32_to_u32 (buffer))); |
1803 | 0 | break; |
1804 | 0 | case SIGSUBPKT_SIG_EXPIRE: |
1805 | 0 | if (length >= 4) |
1806 | 0 | { |
1807 | 0 | if (buf32_to_u32 (buffer)) |
1808 | 0 | es_fprintf (listfp, "sig expires after %s", |
1809 | 0 | strtimevalue (buf32_to_u32 (buffer))); |
1810 | 0 | else |
1811 | 0 | es_fprintf (listfp, "sig does not expire"); |
1812 | 0 | } |
1813 | 0 | break; |
1814 | 0 | case SIGSUBPKT_EXPORTABLE: |
1815 | 0 | if (length) |
1816 | 0 | es_fprintf (listfp, "%sexportable", *buffer ? "" : "not "); |
1817 | 0 | break; |
1818 | 0 | case SIGSUBPKT_TRUST: |
1819 | 0 | if (length != 2) |
1820 | 0 | p = "[invalid trust subpacket]"; |
1821 | 0 | else |
1822 | 0 | es_fprintf (listfp, "trust signature of depth %d, value %d", buffer[0], |
1823 | 0 | buffer[1]); |
1824 | 0 | break; |
1825 | 0 | case SIGSUBPKT_REGEXP: |
1826 | 0 | if (!length) |
1827 | 0 | p = "[invalid regexp subpacket]"; |
1828 | 0 | else |
1829 | 0 | { |
1830 | 0 | es_fprintf (listfp, "regular expression: \""); |
1831 | 0 | es_write_sanitized (listfp, buffer, length, "\"", NULL); |
1832 | 0 | p = "\""; |
1833 | 0 | } |
1834 | 0 | break; |
1835 | 0 | case SIGSUBPKT_REVOCABLE: |
1836 | 0 | if (length) |
1837 | 0 | es_fprintf (listfp, "%srevocable", *buffer ? "" : "not "); |
1838 | 0 | break; |
1839 | 0 | case SIGSUBPKT_KEY_EXPIRE: |
1840 | 0 | if (length >= 4) |
1841 | 0 | { |
1842 | 0 | if (buf32_to_u32 (buffer)) |
1843 | 0 | es_fprintf (listfp, "key expires after %s", |
1844 | 0 | strtimevalue (buf32_to_u32 (buffer))); |
1845 | 0 | else |
1846 | 0 | es_fprintf (listfp, "key does not expire"); |
1847 | 0 | } |
1848 | 0 | break; |
1849 | 0 | case SIGSUBPKT_PREF_SYM: |
1850 | 0 | es_fputs ("pref-sym-algos:", listfp); |
1851 | 0 | for (i = 0; i < length; i++) |
1852 | 0 | es_fprintf (listfp, " %d", buffer[i]); |
1853 | 0 | break; |
1854 | 0 | case SIGSUBPKT_PREF_AEAD: |
1855 | 0 | es_fputs ("pref-aead-algos:", listfp); |
1856 | 0 | for (i = 0; i < length; i++) |
1857 | 0 | es_fprintf (listfp, " %d", buffer[i]); |
1858 | 0 | break; |
1859 | 0 | case SIGSUBPKT_REV_KEY: |
1860 | 0 | es_fputs ("revocation key: ", listfp); |
1861 | 0 | if (length < 22) |
1862 | 0 | p = "[too short]"; |
1863 | 0 | else |
1864 | 0 | { |
1865 | 0 | es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]); |
1866 | 0 | for (i = 2; i < length; i++) |
1867 | 0 | es_fprintf (listfp, "%02X", buffer[i]); |
1868 | 0 | } |
1869 | 0 | break; |
1870 | 0 | case SIGSUBPKT_ISSUER: |
1871 | 0 | if (length >= 8) |
1872 | 0 | es_fprintf (listfp, "issuer key ID %08lX%08lX", |
1873 | 0 | (ulong) buf32_to_u32 (buffer), |
1874 | 0 | (ulong) buf32_to_u32 (buffer + 4)); |
1875 | 0 | break; |
1876 | 0 | case SIGSUBPKT_ISSUER_FPR: |
1877 | 0 | if (length >= 21) |
1878 | 0 | { |
1879 | 0 | char *tmp; |
1880 | 0 | es_fprintf (listfp, "issuer fpr v%d ", buffer[0]); |
1881 | 0 | tmp = bin2hex (buffer+1, length-1, NULL); |
1882 | 0 | if (tmp) |
1883 | 0 | { |
1884 | 0 | es_fputs (tmp, listfp); |
1885 | 0 | xfree (tmp); |
1886 | 0 | } |
1887 | 0 | } |
1888 | 0 | break; |
1889 | 0 | case SIGSUBPKT_INT_RCP_FPR: |
1890 | 0 | if (length >= 21) |
1891 | 0 | { |
1892 | 0 | char *tmp; |
1893 | 0 | es_fprintf (listfp, "intended recipient (revocation subject) fpr v%d ", |
1894 | 0 | buffer[0]); |
1895 | 0 | tmp = bin2hex (buffer+1, length -1, NULL); |
1896 | 0 | if (tmp) |
1897 | 0 | { |
1898 | 0 | es_fputs (tmp, listfp); |
1899 | 0 | xfree (tmp); |
1900 | 0 | } |
1901 | 0 | } |
1902 | 0 | break; |
1903 | 0 | case SIGSUBPKT_NOTATION: |
1904 | 0 | { |
1905 | 0 | es_fputs ("notation: ", listfp); |
1906 | 0 | if (length < 8) |
1907 | 0 | p = "[too short]"; |
1908 | 0 | else |
1909 | 0 | { |
1910 | 0 | const byte *s = buffer; |
1911 | 0 | size_t n1, n2; |
1912 | |
|
1913 | 0 | n1 = (s[4] << 8) | s[5]; |
1914 | 0 | n2 = (s[6] << 8) | s[7]; |
1915 | 0 | s += 8; |
1916 | 0 | if (8 + n1 + n2 != length) |
1917 | 0 | p = "[error]"; |
1918 | 0 | else |
1919 | 0 | { |
1920 | 0 | es_write_sanitized (listfp, s, n1, ")", NULL); |
1921 | 0 | es_putc ('=', listfp); |
1922 | |
|
1923 | 0 | if (*buffer & 0x80) |
1924 | 0 | es_write_sanitized (listfp, s + n1, n2, ")", NULL); |
1925 | 0 | else |
1926 | 0 | p = "[not human readable]"; |
1927 | 0 | } |
1928 | 0 | } |
1929 | 0 | } |
1930 | 0 | break; |
1931 | 0 | case SIGSUBPKT_PREF_HASH: |
1932 | 0 | es_fputs ("pref-hash-algos:", listfp); |
1933 | 0 | for (i = 0; i < length; i++) |
1934 | 0 | es_fprintf (listfp, " %d", buffer[i]); |
1935 | 0 | break; |
1936 | 0 | case SIGSUBPKT_PREF_COMPR: |
1937 | 0 | es_fputs ("pref-zip-algos:", listfp); |
1938 | 0 | for (i = 0; i < length; i++) |
1939 | 0 | es_fprintf (listfp, " %d", buffer[i]); |
1940 | 0 | break; |
1941 | 0 | case SIGSUBPKT_KS_FLAGS: |
1942 | 0 | es_fputs ("keyserver preferences:", listfp); |
1943 | 0 | for (i = 0; i < length; i++) |
1944 | 0 | es_fprintf (listfp, " %02X", buffer[i]); |
1945 | 0 | break; |
1946 | 0 | case SIGSUBPKT_PREF_KS: |
1947 | 0 | es_fputs ("preferred keyserver: ", listfp); |
1948 | 0 | es_write_sanitized (listfp, buffer, length, ")", NULL); |
1949 | 0 | break; |
1950 | 0 | case SIGSUBPKT_PRIMARY_UID: |
1951 | 0 | p = "primary user ID"; |
1952 | 0 | break; |
1953 | 0 | case SIGSUBPKT_POLICY: |
1954 | 0 | es_fputs ("policy: ", listfp); |
1955 | 0 | es_write_sanitized (listfp, buffer, length, ")", NULL); |
1956 | 0 | break; |
1957 | 0 | case SIGSUBPKT_KEY_FLAGS: |
1958 | 0 | es_fputs ("key flags:", listfp); |
1959 | 0 | for (i = 0; i < length; i++) |
1960 | 0 | es_fprintf (listfp, " %02X", buffer[i]); |
1961 | 0 | break; |
1962 | 0 | case SIGSUBPKT_SIGNERS_UID: |
1963 | 0 | p = "signer's user ID"; |
1964 | 0 | break; |
1965 | 0 | case SIGSUBPKT_REVOC_REASON: |
1966 | 0 | if (length) |
1967 | 0 | { |
1968 | 0 | es_fprintf (listfp, "revocation reason 0x%02x (", *buffer); |
1969 | 0 | es_write_sanitized (listfp, buffer + 1, length - 1, ")", NULL); |
1970 | 0 | p = ")"; |
1971 | 0 | } |
1972 | 0 | break; |
1973 | 0 | case SIGSUBPKT_ARR: |
1974 | 0 | es_fputs ("Big Brother's key (ignored): ", listfp); |
1975 | 0 | if (length < 22) |
1976 | 0 | p = "[too short]"; |
1977 | 0 | else |
1978 | 0 | { |
1979 | 0 | es_fprintf (listfp, "c=%02x a=%d f=", buffer[0], buffer[1]); |
1980 | 0 | if (length > 2) |
1981 | 0 | es_write_hexstring (listfp, buffer+2, length-2, 0, NULL); |
1982 | 0 | } |
1983 | 0 | break; |
1984 | 0 | case SIGSUBPKT_FEATURES: |
1985 | 0 | es_fputs ("features:", listfp); |
1986 | 0 | for (i = 0; i < length; i++) |
1987 | 0 | es_fprintf (listfp, " %02x", buffer[i]); |
1988 | 0 | break; |
1989 | 0 | case SIGSUBPKT_SIGNATURE: |
1990 | 0 | es_fputs ("signature: ", listfp); |
1991 | 0 | if (length < 17) |
1992 | 0 | p = "[too short]"; |
1993 | 0 | else |
1994 | 0 | es_fprintf (listfp, "v%d, class 0x%02X, algo %d, digest algo %d", |
1995 | 0 | buffer[0], |
1996 | 0 | buffer[0] == 3 ? buffer[2] : buffer[1], |
1997 | 0 | buffer[0] == 3 ? buffer[15] : buffer[2], |
1998 | 0 | buffer[0] == 3 ? buffer[16] : buffer[3]); |
1999 | 0 | break; |
2000 | | |
2001 | 0 | case SIGSUBPKT_ATTST_SIGS: |
2002 | 0 | { |
2003 | 0 | unsigned int hlen; |
2004 | |
|
2005 | 0 | es_fputs ("attst-sigs: ", listfp); |
2006 | 0 | hlen = gcry_md_get_algo_dlen (map_md_openpgp_to_gcry (digest_algo)); |
2007 | 0 | if (!hlen) |
2008 | 0 | p = "[unknown digest algo]"; |
2009 | 0 | else if ((length % hlen)) |
2010 | 0 | p = "[invalid length]"; |
2011 | 0 | else |
2012 | 0 | { |
2013 | 0 | es_fprintf (listfp, "%u", (unsigned int)length/hlen); |
2014 | 0 | while (length) |
2015 | 0 | { |
2016 | 0 | es_fprintf (listfp, "\n\t%*s", nprinted-1, ""); |
2017 | 0 | es_write_hexstring (listfp, buffer, hlen, 0, NULL); |
2018 | 0 | buffer += hlen; |
2019 | 0 | length -= hlen; |
2020 | 0 | } |
2021 | 0 | } |
2022 | 0 | } |
2023 | 0 | break; |
2024 | | |
2025 | 0 | case SIGSUBPKT_KEY_BLOCK: |
2026 | 0 | es_fputs ("key-block: ", listfp); |
2027 | 0 | if (length && buffer[0]) |
2028 | 0 | p = "[unknown reserved octet]"; |
2029 | 0 | else if (length < 50) /* 50 is an arbitrary min. length. */ |
2030 | 0 | p = "[invalid subpacket]"; |
2031 | 0 | else |
2032 | 0 | { |
2033 | | /* estream_t fp; */ |
2034 | | /* fp = es_fopen ("a.key-block", "wb"); */ |
2035 | | /* log_assert (fp); */ |
2036 | | /* es_fwrite ( buffer+1, length-1, 1, fp); */ |
2037 | | /* es_fclose (fp); */ |
2038 | 0 | es_fprintf (listfp, "[%u octets]", (unsigned int)length-1); |
2039 | 0 | } |
2040 | 0 | break; |
2041 | | |
2042 | | |
2043 | 0 | default: |
2044 | 0 | if (type >= 100 && type <= 110) |
2045 | 0 | p = "experimental / private subpacket"; |
2046 | 0 | else |
2047 | 0 | p = "?"; |
2048 | 0 | break; |
2049 | 0 | } |
2050 | | |
2051 | 0 | es_fprintf (listfp, "%s)\n", p ? p : ""); |
2052 | 0 | } |
2053 | | |
2054 | | |
2055 | | /* |
2056 | | * Returns: >= 0 use this offset into buffer |
2057 | | * -1 explicitly reject returning this type |
2058 | | * -2 subpacket too short |
2059 | | */ |
2060 | | int |
2061 | | parse_one_sig_subpkt (const byte * buffer, size_t n, int type) |
2062 | 32.4k | { |
2063 | 32.4k | switch (type) |
2064 | 32.4k | { |
2065 | 1.69k | case SIGSUBPKT_REV_KEY: |
2066 | 1.69k | if (n < 22) |
2067 | 23 | break; |
2068 | 1.66k | return 0; |
2069 | 4.47k | case SIGSUBPKT_SIG_CREATED: |
2070 | 5.44k | case SIGSUBPKT_SIG_EXPIRE: |
2071 | 5.60k | case SIGSUBPKT_KEY_EXPIRE: |
2072 | 5.60k | if (n < 4) |
2073 | 932 | break; |
2074 | 4.67k | return 0; |
2075 | 655 | case SIGSUBPKT_KEY_FLAGS: |
2076 | 1.30k | case SIGSUBPKT_KS_FLAGS: |
2077 | 2.07k | case SIGSUBPKT_PREF_SYM: |
2078 | 2.08k | case SIGSUBPKT_PREF_AEAD: |
2079 | 2.84k | case SIGSUBPKT_PREF_HASH: |
2080 | 3.61k | case SIGSUBPKT_PREF_COMPR: |
2081 | 4.54k | case SIGSUBPKT_POLICY: |
2082 | 5.23k | case SIGSUBPKT_PREF_KS: |
2083 | 5.77k | case SIGSUBPKT_FEATURES: |
2084 | 5.77k | case SIGSUBPKT_REGEXP: |
2085 | 5.77k | case SIGSUBPKT_ATTST_SIGS: |
2086 | 5.77k | return 0; |
2087 | 247 | case SIGSUBPKT_SIGNATURE: |
2088 | 1.42k | case SIGSUBPKT_EXPORTABLE: |
2089 | 2.30k | case SIGSUBPKT_REVOCABLE: |
2090 | 2.30k | case SIGSUBPKT_REVOC_REASON: |
2091 | 2.30k | if (!n) |
2092 | 747 | break; |
2093 | 1.55k | return 0; |
2094 | 3.15k | case SIGSUBPKT_ISSUER: /* issuer key ID */ |
2095 | 3.15k | if (n < 8) |
2096 | 533 | break; |
2097 | 2.62k | return 0; |
2098 | 4.73k | case SIGSUBPKT_ISSUER_FPR: /* issuer key fingerprint */ |
2099 | 4.73k | if (n < 21) |
2100 | 1.25k | break; |
2101 | 3.47k | return 0; |
2102 | 313 | case SIGSUBPKT_NOTATION: |
2103 | | /* minimum length needed, and the subpacket must be well-formed |
2104 | | where the name length and value length all fit inside the |
2105 | | packet. */ |
2106 | 313 | if (n < 8 |
2107 | 278 | || 8 + ((buffer[4] << 8) | buffer[5]) + |
2108 | 278 | ((buffer[6] << 8) | buffer[7]) != n) |
2109 | 149 | break; |
2110 | 164 | return 0; |
2111 | 4 | case SIGSUBPKT_PRIMARY_UID: |
2112 | 4 | if (n != 1) |
2113 | 0 | break; |
2114 | 4 | return 0; |
2115 | 769 | case SIGSUBPKT_TRUST: |
2116 | 769 | if (n != 2) |
2117 | 735 | break; |
2118 | 34 | return 0; |
2119 | 181 | case SIGSUBPKT_KEY_BLOCK: |
2120 | 181 | if (n && buffer[0]) |
2121 | 67 | return -1; /* Unknown version - ignore. */ |
2122 | 114 | if (n < 50) |
2123 | 114 | break; /* Definitely too short to carry a key block. */ |
2124 | 0 | return 0; |
2125 | 7.96k | default: |
2126 | 7.96k | return 0; |
2127 | 32.4k | } |
2128 | 4.49k | return -2; |
2129 | 32.4k | } |
2130 | | |
2131 | | |
2132 | | /* Return true if we understand the critical notation. */ |
2133 | | static int |
2134 | | can_handle_critical_notation (const byte *name, size_t len) |
2135 | 359 | { |
2136 | 359 | strlist_t sl; |
2137 | | |
2138 | 359 | register_known_notation (NULL); /* Make sure it is initialized. */ |
2139 | | |
2140 | 696 | for (sl = known_notations_list; sl; sl = sl->next) |
2141 | 359 | if (sl->flags == len && !memcmp (sl->d, name, len)) |
2142 | 22 | return 1; /* Known */ |
2143 | | |
2144 | 337 | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
2145 | 0 | { |
2146 | 0 | log_info(_("Unknown critical signature notation: ") ); |
2147 | 0 | print_utf8_buffer (log_get_stream(), name, len); |
2148 | 0 | log_printf ("\n"); |
2149 | 0 | } |
2150 | | |
2151 | 337 | return 0; /* Unknown. */ |
2152 | 359 | } |
2153 | | |
2154 | | |
2155 | | static int |
2156 | | can_handle_critical (const byte * buffer, size_t n, int type) |
2157 | 18.6k | { |
2158 | 18.6k | switch (type) |
2159 | 18.6k | { |
2160 | 419 | case SIGSUBPKT_NOTATION: |
2161 | 419 | if (n >= 8) |
2162 | 386 | { |
2163 | 386 | size_t notation_len = ((buffer[4] << 8) | buffer[5]); |
2164 | 386 | if (n - 8 >= notation_len) |
2165 | 359 | return can_handle_critical_notation (buffer + 8, notation_len); |
2166 | 386 | } |
2167 | 60 | return 0; |
2168 | 129 | case SIGSUBPKT_SIGNATURE: |
2169 | 722 | case SIGSUBPKT_SIG_CREATED: |
2170 | 1.45k | case SIGSUBPKT_SIG_EXPIRE: |
2171 | 1.65k | case SIGSUBPKT_KEY_EXPIRE: |
2172 | 2.41k | case SIGSUBPKT_EXPORTABLE: |
2173 | 2.42k | case SIGSUBPKT_REVOCABLE: |
2174 | 3.77k | case SIGSUBPKT_REV_KEY: |
2175 | 4.28k | case SIGSUBPKT_ISSUER: /* issuer key ID */ |
2176 | 5.29k | case SIGSUBPKT_ISSUER_FPR: /* issuer fingerprint */ |
2177 | 5.41k | case SIGSUBPKT_PREF_SYM: |
2178 | 5.42k | case SIGSUBPKT_PREF_AEAD: |
2179 | 5.61k | case SIGSUBPKT_PREF_HASH: |
2180 | 5.72k | case SIGSUBPKT_PREF_COMPR: |
2181 | 6.31k | case SIGSUBPKT_KEY_FLAGS: |
2182 | 6.58k | case SIGSUBPKT_PRIMARY_UID: |
2183 | 6.72k | case SIGSUBPKT_FEATURES: |
2184 | 7.73k | case SIGSUBPKT_TRUST: |
2185 | 7.82k | case SIGSUBPKT_REGEXP: |
2186 | 7.93k | case SIGSUBPKT_ATTST_SIGS: |
2187 | | /* Is it enough to show the policy or keyserver? */ |
2188 | 9.00k | case SIGSUBPKT_POLICY: |
2189 | 9.72k | case SIGSUBPKT_PREF_KS: |
2190 | 10.4k | case SIGSUBPKT_REVOC_REASON: /* At least we know about it. */ |
2191 | 10.4k | return 1; |
2192 | | |
2193 | 181 | case SIGSUBPKT_KEY_BLOCK: |
2194 | 181 | if (n && !buffer[0]) |
2195 | 73 | return 1; |
2196 | 108 | else |
2197 | 108 | return 0; |
2198 | | |
2199 | 7.60k | default: |
2200 | 7.60k | return 0; |
2201 | 18.6k | } |
2202 | 18.6k | } |
2203 | | |
2204 | | |
2205 | | const byte * |
2206 | | enum_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype, |
2207 | | size_t *ret_n, int *start, int *critical) |
2208 | 694k | { |
2209 | 694k | const byte *buffer; |
2210 | 694k | int buflen; |
2211 | 694k | int type; |
2212 | 694k | int critical_dummy; |
2213 | 694k | int offset; |
2214 | 694k | size_t n; |
2215 | 694k | const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed; |
2216 | 694k | int seq = 0; |
2217 | 694k | int reqseq = start ? *start : 0; |
2218 | | |
2219 | 694k | if (!critical) |
2220 | 694k | critical = &critical_dummy; |
2221 | | |
2222 | 694k | if (!pktbuf || reqseq == -1) |
2223 | 75.3k | { |
2224 | 75.3k | static char dummy[] = "x"; |
2225 | | /* Return a value different from NULL to indicate that |
2226 | | * there is no critical bit we do not understand. */ |
2227 | 75.3k | return reqtype == SIGSUBPKT_TEST_CRITICAL ? dummy : NULL; |
2228 | 75.3k | } |
2229 | 619k | buffer = pktbuf->data; |
2230 | 619k | buflen = pktbuf->len; |
2231 | 1.29M | while (buflen) |
2232 | 1.15M | { |
2233 | 1.15M | n = *buffer++; |
2234 | 1.15M | buflen--; |
2235 | 1.15M | if (n == 255) /* 4 byte length header. */ |
2236 | 36.2k | { |
2237 | 36.2k | if (buflen < 4) |
2238 | 1.07k | goto too_short; |
2239 | 35.1k | n = buf32_to_size_t (buffer); |
2240 | 35.1k | buffer += 4; |
2241 | 35.1k | buflen -= 4; |
2242 | 35.1k | } |
2243 | 1.11M | else if (n >= 192) /* 4 byte special encoded length header. */ |
2244 | 309k | { |
2245 | 309k | if (buflen < 2) |
2246 | 11.2k | goto too_short; |
2247 | 298k | n = ((n - 192) << 8) + *buffer + 192; |
2248 | 298k | buffer++; |
2249 | 298k | buflen--; |
2250 | 298k | } |
2251 | 1.13M | if (buflen < n) |
2252 | 406k | goto too_short; |
2253 | 732k | if (!buflen) |
2254 | 11.8k | goto no_type_byte; |
2255 | 720k | type = *buffer; |
2256 | 720k | if (type & 0x80) |
2257 | 280k | { |
2258 | 280k | type &= 0x7f; |
2259 | 280k | *critical = 1; |
2260 | 280k | } |
2261 | 439k | else |
2262 | 439k | *critical = 0; |
2263 | 720k | if (!(++seq > reqseq)) |
2264 | 9.12k | ; |
2265 | 710k | else if (reqtype == SIGSUBPKT_TEST_CRITICAL) |
2266 | 48.2k | { |
2267 | 48.2k | if (*critical) |
2268 | 19.0k | { |
2269 | 19.0k | if (n - 1 > buflen + 1) |
2270 | 411 | goto too_short; |
2271 | 18.6k | if (!can_handle_critical (buffer + 1, n - 1, type)) |
2272 | 8.11k | { |
2273 | 8.11k | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
2274 | 8.11k | log_info (_("subpacket of type %d has " |
2275 | 0 | "critical bit set\n"), type); |
2276 | 8.11k | if (start) |
2277 | 0 | *start = seq; |
2278 | 8.11k | return NULL; /* This is an error. */ |
2279 | 8.11k | } |
2280 | 18.6k | } |
2281 | 48.2k | } |
2282 | 662k | else if (reqtype < 0) /* List packets. */ |
2283 | 0 | dump_sig_subpkt (reqtype == SIGSUBPKT_LIST_HASHED, |
2284 | 0 | type, *critical, buffer, buflen, n, sig->digest_algo); |
2285 | 662k | else if (type == reqtype) /* Found. */ |
2286 | 34.8k | { |
2287 | 34.8k | buffer++; |
2288 | 34.8k | n--; |
2289 | 34.8k | if (n > buflen) |
2290 | 2.34k | goto too_short; |
2291 | 32.4k | if (ret_n) |
2292 | 19.5k | *ret_n = n; |
2293 | 32.4k | offset = parse_one_sig_subpkt (buffer, n, type); |
2294 | 32.4k | switch (offset) |
2295 | 32.4k | { |
2296 | 4.49k | case -2: |
2297 | 4.49k | log_error ("subpacket of type %d too short\n", type); |
2298 | 4.49k | return NULL; |
2299 | 67 | case -1: |
2300 | 67 | return NULL; |
2301 | 27.9k | default: |
2302 | 27.9k | break; |
2303 | 32.4k | } |
2304 | 27.9k | if (start) |
2305 | 1.91k | *start = seq; |
2306 | 27.9k | return buffer + offset; |
2307 | 32.4k | } |
2308 | 676k | buffer += n; |
2309 | 676k | buflen -= n; |
2310 | 676k | } |
2311 | 145k | if (reqtype == SIGSUBPKT_TEST_CRITICAL) |
2312 | | /* Returning NULL means we found a subpacket with the critical bit |
2313 | | set that we don't grok. We've iterated over all the subpackets |
2314 | | and haven't found such a packet so we need to return a non-NULL |
2315 | | value. */ |
2316 | 12.9k | return buffer; |
2317 | | |
2318 | | /* Critical bit we don't understand. */ |
2319 | 132k | if (start) |
2320 | 1.65k | *start = -1; |
2321 | 132k | return NULL; /* End of packets; not found. */ |
2322 | | |
2323 | 421k | too_short: |
2324 | 421k | if (opt.debug && !glo_ctrl.silence_parse_warnings) |
2325 | 0 | { |
2326 | 0 | es_fflush (es_stdout); |
2327 | 0 | log_printhex (pktbuf->data, pktbuf->len > 16? 16 : pktbuf->len, |
2328 | 0 | "buffer shorter than subpacket (%zu/%d/%zu); dump:", |
2329 | 0 | pktbuf->len, buflen, n); |
2330 | 0 | } |
2331 | | |
2332 | 421k | if (start) |
2333 | 671 | *start = -1; |
2334 | 421k | return NULL; |
2335 | | |
2336 | 11.8k | no_type_byte: |
2337 | 11.8k | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
2338 | 11.8k | log_info ("type octet missing in subpacket\n"); |
2339 | 11.8k | if (start) |
2340 | 344 | *start = -1; |
2341 | 11.8k | return NULL; |
2342 | 145k | } |
2343 | | |
2344 | | |
2345 | | const byte * |
2346 | | parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype, |
2347 | | size_t *ret_n) |
2348 | 689k | { |
2349 | 689k | return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL); |
2350 | 689k | } |
2351 | | |
2352 | | |
2353 | | const byte * |
2354 | | parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype) |
2355 | 79.5k | { |
2356 | 79.5k | const byte *p; |
2357 | | |
2358 | 79.5k | p = parse_sig_subpkt (sig, 1, reqtype, NULL); |
2359 | 79.5k | if (!p) |
2360 | 78.3k | p = parse_sig_subpkt (sig, 0, reqtype, NULL); |
2361 | 79.5k | return p; |
2362 | 79.5k | } |
2363 | | |
2364 | | |
2365 | | /* Find all revocation keys. Look in hashed area only. */ |
2366 | | void |
2367 | | parse_revkeys (PKT_signature * sig) |
2368 | 1.24k | { |
2369 | 1.24k | const byte *revkey; |
2370 | 1.24k | int seq = 0; |
2371 | 1.24k | size_t len; |
2372 | | |
2373 | 1.24k | if (sig->sig_class != 0x1F) |
2374 | 0 | return; |
2375 | | |
2376 | 2.91k | while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY, |
2377 | 2.91k | &len, &seq, NULL))) |
2378 | 1.66k | { |
2379 | | /* Consider only valid packets. They must have a length of |
2380 | | * either 2+20 or 2+32 octets and bit 7 of the class octet must |
2381 | | * be set. */ |
2382 | 1.66k | if ((len == 22 || len == 34) |
2383 | 1.60k | && (revkey[0] & 0x80)) |
2384 | 635 | { |
2385 | 635 | sig->revkey = xrealloc (sig->revkey, |
2386 | 635 | sizeof (struct revocation_key) * |
2387 | 635 | (sig->numrevkeys + 1)); |
2388 | | |
2389 | 635 | sig->revkey[sig->numrevkeys].class = revkey[0]; |
2390 | 635 | sig->revkey[sig->numrevkeys].algid = revkey[1]; |
2391 | 635 | len -= 2; |
2392 | 635 | sig->revkey[sig->numrevkeys].fprlen = len; |
2393 | 635 | memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len); |
2394 | 635 | memset (sig->revkey[sig->numrevkeys].fpr+len, 0, |
2395 | 635 | sizeof (sig->revkey[sig->numrevkeys].fpr) - len); |
2396 | 635 | sig->numrevkeys++; |
2397 | 635 | } |
2398 | 1.66k | } |
2399 | 1.24k | } |
2400 | | |
2401 | | |
2402 | | /* Note that the function returns -1 to indicate an EOF (which also |
2403 | | * indicates a broken packet in this case. In most other cases |
2404 | | * GPG_ERR_INV_PACKET is returned and callers of parse_packet will |
2405 | | * usually skipt this packet then. */ |
2406 | | int |
2407 | | parse_signature (IOBUF inp, int pkttype, unsigned long pktlen, |
2408 | | PKT_signature * sig) |
2409 | 75.3k | { |
2410 | 75.3k | int md5_len = 0; |
2411 | 75.3k | unsigned int n; |
2412 | 75.3k | int is_v4plus = 0; |
2413 | 75.3k | int rc = 0; |
2414 | 75.3k | int i, ndata; |
2415 | | |
2416 | 75.3k | if (pktlen < 16) |
2417 | 6.49k | { |
2418 | 6.49k | log_error ("packet(%d) too short\n", pkttype); |
2419 | 6.49k | if (list_mode) |
2420 | 6.49k | es_fputs (":signature packet: [too short]\n", listfp); |
2421 | 6.49k | goto leave; |
2422 | 6.49k | } |
2423 | 68.8k | sig->version = iobuf_get_noeof (inp); |
2424 | 68.8k | pktlen--; |
2425 | 68.8k | if (sig->version == 4 || sig->version == 5 |
2426 | 16.5k | || (sig->version == 6 && RFC9980)) |
2427 | 52.3k | is_v4plus = 1; |
2428 | 16.5k | else if (sig->version != 2 && sig->version != 3) |
2429 | 273 | { |
2430 | 273 | log_error ("packet(%d) with unknown version %d\n", |
2431 | 273 | pkttype, sig->version); |
2432 | 273 | if (list_mode) |
2433 | 273 | es_fputs (":signature packet: [unknown version]\n", listfp); |
2434 | 273 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2435 | 273 | goto leave; |
2436 | 273 | } |
2437 | | |
2438 | 68.5k | if (!is_v4plus) |
2439 | 16.2k | { |
2440 | 16.2k | if (pktlen == 0) |
2441 | 0 | goto underflow; |
2442 | 16.2k | md5_len = iobuf_get_noeof (inp); |
2443 | 16.2k | pktlen--; |
2444 | 16.2k | } |
2445 | 68.5k | if (pktlen == 0) |
2446 | 0 | goto underflow; |
2447 | 68.5k | sig->sig_class = iobuf_get_noeof (inp); |
2448 | 68.5k | pktlen--; |
2449 | 68.5k | if (!is_v4plus) |
2450 | 16.2k | { |
2451 | 16.2k | if (pktlen < 12) |
2452 | 0 | goto underflow; |
2453 | 16.2k | sig->timestamp = read_32 (inp); |
2454 | 16.2k | pktlen -= 4; |
2455 | 16.2k | sig->keyid[0] = read_32 (inp); |
2456 | 16.2k | pktlen -= 4; |
2457 | 16.2k | sig->keyid[1] = read_32 (inp); |
2458 | 16.2k | pktlen -= 4; |
2459 | 16.2k | } |
2460 | 68.5k | if (pktlen < 2) |
2461 | 4 | goto underflow; |
2462 | 68.5k | sig->pubkey_algo = iobuf_get_noeof (inp); |
2463 | 68.5k | pktlen--; |
2464 | 68.5k | sig->digest_algo = iobuf_get_noeof (inp); |
2465 | 68.5k | pktlen--; |
2466 | 68.5k | sig->flags.exportable = 1; |
2467 | 68.5k | sig->flags.revocable = 1; |
2468 | 68.5k | if (is_v4plus) /* Read subpackets. */ |
2469 | 52.3k | { |
2470 | 52.3k | if (sig->version == 6) |
2471 | 35 | { |
2472 | 35 | if (pktlen < 4) |
2473 | 0 | goto underflow; |
2474 | 35 | n = read_32 (inp); |
2475 | 35 | pktlen -= 4; /* Length of hashed data. */ |
2476 | 35 | } |
2477 | 52.2k | else |
2478 | 52.2k | { |
2479 | 52.2k | if (pktlen < 2) |
2480 | 0 | goto underflow; |
2481 | 52.2k | n = read_16 (inp); |
2482 | 52.2k | pktlen -= 2; /* Length of hashed data. */ |
2483 | 52.2k | } |
2484 | 52.3k | if (pktlen < n) |
2485 | 3.96k | goto underflow; |
2486 | 48.3k | if (n > 30000) |
2487 | 30 | { |
2488 | 30 | log_error ("signature packet: hashed data too long (%u)\n", n); |
2489 | 30 | if (list_mode) |
2490 | 30 | es_fprintf (listfp, |
2491 | 0 | ":signature packet: [hashed data too long (%u)]\n", n); |
2492 | 30 | rc = GPG_ERR_INV_PACKET; |
2493 | 30 | goto leave; |
2494 | 30 | } |
2495 | 48.3k | if (n) |
2496 | 47.7k | { |
2497 | 47.7k | sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1); |
2498 | 47.7k | sig->hashed->size = n; |
2499 | 47.7k | sig->hashed->len = n; |
2500 | 47.7k | if (iobuf_read (inp, sig->hashed->data, n) != n) |
2501 | 134 | { |
2502 | 134 | log_error ("premature eof while reading " |
2503 | 134 | "hashed signature data\n"); |
2504 | 134 | if (list_mode) |
2505 | 134 | es_fputs (":signature packet: [premature eof]\n", listfp); |
2506 | 134 | rc = -1; |
2507 | 134 | goto leave; |
2508 | 134 | } |
2509 | 47.6k | pktlen -= n; |
2510 | 47.6k | } |
2511 | 48.1k | if (sig->version == 6) |
2512 | 3 | { |
2513 | 3 | if (pktlen < 4) |
2514 | 0 | goto underflow; |
2515 | 3 | n = read_32 (inp); |
2516 | 3 | pktlen -= 4; /* Length of unhashed data. */ |
2517 | 3 | } |
2518 | 48.1k | else |
2519 | 48.1k | { |
2520 | 48.1k | if (pktlen < 2) |
2521 | 1 | goto underflow; |
2522 | 48.1k | n = read_16 (inp); |
2523 | 48.1k | pktlen -= 2; /* Length of unhashed data. */ |
2524 | 48.1k | } |
2525 | 48.1k | if (pktlen < n) |
2526 | 6.00k | goto underflow; |
2527 | 42.1k | if (n > 10000) |
2528 | 12 | { |
2529 | 12 | log_error ("signature packet: unhashed data too long (%u)\n", n); |
2530 | 12 | if (list_mode) |
2531 | 12 | es_fprintf (listfp, |
2532 | 0 | ":signature packet: [unhashed data too long (%u)]\n", |
2533 | 0 | n); |
2534 | 12 | rc = GPG_ERR_INV_PACKET; |
2535 | 12 | goto leave; |
2536 | 12 | } |
2537 | 42.1k | if (n) |
2538 | 9.88k | { |
2539 | 9.88k | sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1); |
2540 | 9.88k | sig->unhashed->size = n; |
2541 | 9.88k | sig->unhashed->len = n; |
2542 | 9.88k | if (iobuf_read (inp, sig->unhashed->data, n) != n) |
2543 | 31 | { |
2544 | 31 | log_error ("premature eof while reading " |
2545 | 31 | "unhashed signature data\n"); |
2546 | 31 | if (list_mode) |
2547 | 31 | es_fputs (":signature packet: [premature eof]\n", listfp); |
2548 | 31 | rc = -1; |
2549 | 31 | goto leave; |
2550 | 31 | } |
2551 | 9.85k | pktlen -= n; |
2552 | 9.85k | } |
2553 | 42.1k | } |
2554 | | |
2555 | 58.3k | if (pktlen < 2) |
2556 | 834 | goto underflow; |
2557 | 57.5k | sig->digest_start[0] = iobuf_get_noeof (inp); |
2558 | 57.5k | pktlen--; |
2559 | 57.5k | sig->digest_start[1] = iobuf_get_noeof (inp); |
2560 | 57.5k | pktlen--; |
2561 | | |
2562 | 57.5k | if (sig->version == 6) |
2563 | 2 | { |
2564 | 2 | int saltlen; |
2565 | | |
2566 | | /* Note that we allow for a zero length salt. */ |
2567 | 2 | saltlen = iobuf_get (inp); |
2568 | 2 | if (saltlen < 0 || !pktlen) |
2569 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2570 | 2 | else if (saltlen) |
2571 | 0 | rc = read_raw_octet_string (inp, &pktlen, 0, saltlen, 0, &sig->salt); |
2572 | 2 | pktlen--; /* Adjust for saltlen octet read above. */ |
2573 | | |
2574 | 2 | if (rc) |
2575 | 0 | goto leave; |
2576 | 2 | } |
2577 | | |
2578 | 57.5k | if (is_v4plus && sig->pubkey_algo) /* Extract required information. */ |
2579 | 40.6k | { |
2580 | 40.6k | const byte *p; |
2581 | 40.6k | size_t len; |
2582 | | |
2583 | | /* Set sig->flags.unknown_critical if there is a critical bit |
2584 | | * set for packets which we do not understand. */ |
2585 | 40.6k | if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL) |
2586 | 8.85k | || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL)) |
2587 | 32.7k | sig->flags.unknown_critical = 1; |
2588 | | |
2589 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL); |
2590 | 40.6k | if (p) |
2591 | 4.08k | sig->timestamp = buf32_to_u32 (p); |
2592 | 36.5k | else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110) |
2593 | 35.5k | && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2594 | 36.5k | log_info ("signature packet without timestamp\n"); |
2595 | | |
2596 | | /* Set the key id. We first try the issuer fingerprint and if |
2597 | | * it is a v4 signature the fallback to the issuer. Note that |
2598 | | * only the issuer packet is also searched in the unhashed area. */ |
2599 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len); |
2600 | 40.6k | if (p && len == 21 && p[0] == 4) |
2601 | 1.73k | { |
2602 | 1.73k | sig->keyid[0] = buf32_to_u32 (p + 1 + 12); |
2603 | 1.73k | sig->keyid[1] = buf32_to_u32 (p + 1 + 16); |
2604 | 1.73k | } |
2605 | 38.8k | else if (p && len == 33 && (p[0] == 5 || p[0] == 6)) |
2606 | 16 | { |
2607 | 16 | sig->keyid[0] = buf32_to_u32 (p + 1 ); |
2608 | 16 | sig->keyid[1] = buf32_to_u32 (p + 1 + 4); |
2609 | 16 | } |
2610 | 38.8k | else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER))) |
2611 | 2.62k | { |
2612 | 2.62k | sig->keyid[0] = buf32_to_u32 (p); |
2613 | 2.62k | sig->keyid[1] = buf32_to_u32 (p + 4); |
2614 | 2.62k | } |
2615 | 36.2k | else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110) |
2616 | 35.2k | && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2617 | 36.2k | log_info ("signature packet without keyid\n"); |
2618 | | |
2619 | | /* Get the intended recipient (revocation subject) fpr. */ |
2620 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_INT_RCP_FPR, &len); |
2621 | 40.6k | if (p && len == 21 && p[0] == 4) |
2622 | 0 | { |
2623 | 0 | sig->rev_subject_info = xmalloc_clear (sizeof *sig->rev_subject_info); |
2624 | |
|
2625 | 0 | sig->rev_subject_info->fprlen = 20; |
2626 | 0 | memcpy (sig->rev_subject_info->fpr, p + 1, 20); |
2627 | 0 | } |
2628 | 40.6k | else if (p && len == 33 && p[0] == 5) |
2629 | 0 | { |
2630 | 0 | sig->rev_subject_info = xmalloc_clear (sizeof *sig->rev_subject_info); |
2631 | |
|
2632 | 0 | sig->rev_subject_info->fprlen = 32; |
2633 | 0 | memcpy (sig->rev_subject_info->fpr, p + 1, 32); |
2634 | 0 | } |
2635 | 40.6k | else |
2636 | 40.6k | { |
2637 | 40.6k | sig->rev_subject_info = NULL; |
2638 | 40.6k | } |
2639 | | |
2640 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL); |
2641 | 40.6k | if (p && buf32_to_u32 (p)) |
2642 | 410 | sig->expiredate = sig->timestamp + buf32_to_u32 (p); |
2643 | 40.6k | if (sig->expiredate && sig->expiredate <= make_timestamp ()) |
2644 | 201 | sig->flags.expired = 1; |
2645 | | |
2646 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL); |
2647 | 40.6k | if (p) |
2648 | 931 | sig->flags.policy_url = 1; |
2649 | | |
2650 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL); |
2651 | 40.6k | if (p) |
2652 | 686 | sig->flags.pref_ks = 1; |
2653 | | |
2654 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len); |
2655 | 40.6k | if (p && len) |
2656 | 7.63k | { |
2657 | 7.63k | char *mbox; |
2658 | | |
2659 | 7.63k | sig->signers_uid = try_make_printable_string (p, len, 0); |
2660 | 7.63k | if (!sig->signers_uid) |
2661 | 0 | { |
2662 | 0 | rc = gpg_error_from_syserror (); |
2663 | 0 | goto leave; |
2664 | 0 | } |
2665 | 7.63k | mbox = mailbox_from_userid (sig->signers_uid, 0); |
2666 | 7.63k | if (mbox) |
2667 | 3.61k | { |
2668 | 3.61k | xfree (sig->signers_uid); |
2669 | 3.61k | sig->signers_uid = mbox; |
2670 | 3.61k | } |
2671 | 7.63k | } |
2672 | | |
2673 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL); |
2674 | 40.6k | if (p) |
2675 | 0 | sig->flags.key_block = 1; |
2676 | | |
2677 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL); |
2678 | 40.6k | if (p) |
2679 | 164 | sig->flags.notation = 1; |
2680 | | |
2681 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL); |
2682 | 40.6k | if (p && *p == 0) |
2683 | 78 | sig->flags.revocable = 0; |
2684 | | |
2685 | 40.6k | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len); |
2686 | 40.6k | if (p && len == 2) |
2687 | 34 | { |
2688 | 34 | sig->trust_depth = p[0]; |
2689 | 34 | sig->trust_value = p[1]; |
2690 | | |
2691 | | /* Only look for a regexp if there is also a trust |
2692 | | subpacket. */ |
2693 | 34 | sig->trust_regexp = |
2694 | 34 | parse_sig_subpkt (sig, 1, SIGSUBPKT_REGEXP, &len); |
2695 | | |
2696 | | /* If the regular expression is of 0 length, there is no |
2697 | | regular expression. */ |
2698 | 34 | if (len == 0) |
2699 | 0 | sig->trust_regexp = NULL; |
2700 | 34 | } |
2701 | | |
2702 | | /* We accept the exportable subpacket from either the hashed or |
2703 | | unhashed areas as older versions of gpg put it in the |
2704 | | unhashed area. In theory, anyway, we should never see this |
2705 | | packet off of a local keyring. */ |
2706 | | |
2707 | 40.6k | p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE); |
2708 | 40.6k | if (p && *p == 0) |
2709 | 179 | sig->flags.exportable = 0; |
2710 | | |
2711 | | /* Find all revocation keys. */ |
2712 | 40.6k | if (sig->sig_class == 0x1F) |
2713 | 1.24k | parse_revkeys (sig); |
2714 | 40.6k | } |
2715 | | |
2716 | 57.5k | if (list_mode) |
2717 | 0 | { |
2718 | 0 | es_fprintf (listfp, ":signature packet: algo %d, keyid %08lX%08lX\n" |
2719 | 0 | "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n" |
2720 | 0 | "\tdigest algo %d, begin of digest %02x %02x\n", |
2721 | 0 | sig->pubkey_algo, |
2722 | 0 | (ulong) sig->keyid[0], (ulong) sig->keyid[1], |
2723 | 0 | sig->version, (ulong) sig->timestamp, md5_len, sig->sig_class, |
2724 | 0 | sig->digest_algo, sig->digest_start[0], sig->digest_start[1]); |
2725 | 0 | if (sig->version == 6 && sig->salt) |
2726 | 0 | { |
2727 | 0 | es_fprintf (listfp, "\tsalt: "); |
2728 | 0 | mpi_print (listfp, sig->salt, 1 /* Always print the salt. */); |
2729 | 0 | es_putc ('\n', listfp); |
2730 | 0 | } |
2731 | |
|
2732 | 0 | if (is_v4plus) |
2733 | 0 | { |
2734 | 0 | parse_sig_subpkt (sig, 1, SIGSUBPKT_LIST_HASHED, NULL); |
2735 | 0 | parse_sig_subpkt (sig, 0, SIGSUBPKT_LIST_UNHASHED, NULL); |
2736 | 0 | } |
2737 | 0 | } |
2738 | | |
2739 | 57.5k | ndata = pubkey_get_nsig (sig->pubkey_algo); |
2740 | 57.5k | if (!ndata) |
2741 | 51.8k | { |
2742 | 51.8k | if (list_mode) |
2743 | 51.8k | es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo); |
2744 | 51.8k | unknown_pubkey_warning (sig->pubkey_algo); |
2745 | | |
2746 | | /* We store the plain material in data[0], so that we are able |
2747 | | * to write it back with build_packet(). */ |
2748 | 51.8k | if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8)) |
2749 | 51 | { |
2750 | | /* We include a limit to avoid too trivial DoS attacks by |
2751 | | having gpg allocate too much memory. */ |
2752 | 51 | log_error ("signature packet: too much data\n"); |
2753 | 51 | rc = GPG_ERR_INV_PACKET; |
2754 | 51 | } |
2755 | 51.8k | else |
2756 | 51.8k | { |
2757 | 51.8k | void *tmpp; |
2758 | | |
2759 | 51.8k | tmpp = read_rest (inp, pktlen); |
2760 | 51.8k | sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0); |
2761 | 51.8k | pktlen = 0; |
2762 | 51.8k | } |
2763 | 51.8k | } |
2764 | 5.70k | else |
2765 | 5.70k | { |
2766 | 15.2k | for (i = 0; i < ndata; i++) |
2767 | 9.50k | { |
2768 | 9.50k | if (sig->pubkey_algo == PUBKEY_ALGO_ED25519 && RFC9980) |
2769 | 1 | { |
2770 | 1 | rc = read_raw_octet_string (inp, &pktlen, 0, 64, 0, sig->data+i); |
2771 | 1 | if (rc) |
2772 | 1 | goto leave; |
2773 | 1 | } |
2774 | 9.50k | else |
2775 | 9.50k | { |
2776 | 9.50k | n = pktlen; |
2777 | 9.50k | if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA |
2778 | 8.97k | || sig->pubkey_algo == PUBKEY_ALGO_EDDSA) |
2779 | 3.54k | sig->data[i] = sos_read (inp, &n, 0); |
2780 | 5.95k | else |
2781 | 5.95k | sig->data[i] = mpi_read (inp, &n, 0); |
2782 | 9.50k | pktlen -= n; |
2783 | 9.50k | } |
2784 | 9.50k | if (list_mode) |
2785 | 0 | { |
2786 | 0 | es_fprintf (listfp, "\tdata: "); |
2787 | 0 | mpi_print (listfp, sig->data[i], mpi_print_mode); |
2788 | 0 | es_putc ('\n', listfp); |
2789 | 0 | } |
2790 | 9.50k | if (!sig->data[i]) |
2791 | 286 | rc = GPG_ERR_INV_PACKET; |
2792 | 9.50k | } |
2793 | 5.70k | } |
2794 | | |
2795 | 64.5k | leave: |
2796 | 64.5k | iobuf_skip_rest (inp, pktlen, 0); |
2797 | 64.5k | return rc; |
2798 | | |
2799 | 10.8k | underflow: |
2800 | 10.8k | log_error ("packet(%d) too short\n", pkttype); |
2801 | 10.8k | if (list_mode) |
2802 | 10.8k | es_fputs (":signature packet: [too short]\n", listfp); |
2803 | | |
2804 | 10.8k | iobuf_skip_rest (inp, pktlen, 0); |
2805 | | |
2806 | 10.8k | return GPG_ERR_INV_PACKET; |
2807 | 57.5k | } |
2808 | | |
2809 | | |
2810 | | static int |
2811 | | parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen, |
2812 | | PKT_onepass_sig * ops) |
2813 | 7.25k | { |
2814 | 7.25k | int rc = 0; |
2815 | | |
2816 | 7.25k | if (pktlen < 13) |
2817 | 1.62k | { |
2818 | 1.62k | log_error ("packet(%d) too short\n", pkttype); |
2819 | 1.62k | if (list_mode) |
2820 | 1.62k | es_fputs (":onepass_sig packet: [too short]\n", listfp); |
2821 | 1.62k | rc = gpg_error (GPG_ERR_INV_PACKET); |
2822 | 1.62k | goto leave; |
2823 | 1.62k | } |
2824 | 5.63k | ops->version = iobuf_get_noeof (inp); |
2825 | 5.63k | pktlen--; |
2826 | 5.63k | if (!(ops->version == 3 || (RFC9980 && ops->version == 6))) |
2827 | 1.93k | { |
2828 | 1.93k | log_error ("onepass_sig with unknown version %d\n", ops->version); |
2829 | 1.93k | if (list_mode) |
2830 | 1.93k | es_fputs (":onepass_sig packet: [unknown version]\n", listfp); |
2831 | 1.93k | rc = gpg_error (GPG_ERR_INV_PACKET); |
2832 | 1.93k | goto leave; |
2833 | 1.93k | } |
2834 | 3.69k | ops->sig_class = iobuf_get_noeof (inp); |
2835 | 3.69k | pktlen--; |
2836 | 3.69k | ops->digest_algo = iobuf_get_noeof (inp); |
2837 | 3.69k | pktlen--; |
2838 | 3.69k | ops->pubkey_algo = iobuf_get_noeof (inp); |
2839 | 3.69k | pktlen--; |
2840 | | |
2841 | 3.69k | if (ops->version == 6) |
2842 | 21 | { |
2843 | | /* FIXME: We should save the fingerprint and check it against |
2844 | | * the regular packet. Howeever, we have not done that for |
2845 | | * keyid either. */ |
2846 | 21 | unsigned char fpr[32]; |
2847 | | |
2848 | 21 | rc = read_raw_octet_string (inp, &pktlen, 1, 0, 0, &ops->salt); |
2849 | 21 | if (rc) |
2850 | 19 | goto leave; |
2851 | | |
2852 | 2 | if (pktlen < sizeof fpr) |
2853 | 1 | { |
2854 | 1 | log_error ("packet(%d) too short for fingerprint (%lu/%zu)\n", |
2855 | 1 | pkttype, pktlen, sizeof fpr); |
2856 | 1 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2857 | 1 | goto leave; |
2858 | 1 | } |
2859 | 1 | if (iobuf_read (inp, fpr, sizeof fpr) != sizeof fpr) |
2860 | 1 | { |
2861 | 1 | log_error ("premature eof while reading " |
2862 | 1 | "fingerprint from packet(%d)\n", pkttype); |
2863 | 1 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2864 | 1 | goto leave; |
2865 | 1 | } |
2866 | 0 | pktlen -= sizeof fpr; |
2867 | |
|
2868 | 0 | ops->keyid[0] = buf32_to_u32 (fpr); |
2869 | 0 | ops->keyid[1] = buf32_to_u32 (fpr+4); |
2870 | 0 | } |
2871 | 3.67k | else |
2872 | 3.67k | { |
2873 | 3.67k | ops->keyid[0] = read_32 (inp); |
2874 | 3.67k | pktlen -= 4; |
2875 | 3.67k | ops->keyid[1] = read_32 (inp); |
2876 | 3.67k | pktlen -= 4; |
2877 | 3.67k | } |
2878 | | |
2879 | 3.67k | ops->last = iobuf_get_noeof (inp); |
2880 | 3.67k | pktlen--; |
2881 | 3.67k | if (list_mode) |
2882 | 0 | { |
2883 | 0 | es_fprintf (listfp, |
2884 | 0 | ":onepass_sig packet: keyid %08lX%08lX\n" |
2885 | 0 | "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, " |
2886 | 0 | "last=%d\n", |
2887 | 0 | (ulong) ops->keyid[0], (ulong) ops->keyid[1], |
2888 | 0 | ops->version, ops->sig_class, |
2889 | 0 | ops->digest_algo, ops->pubkey_algo, ops->last); |
2890 | 0 | if (ops->version == 6 && ops->salt) |
2891 | 0 | { |
2892 | 0 | es_fprintf (listfp, "\tsalt: "); |
2893 | 0 | mpi_print (listfp, ops->salt, 1); |
2894 | 0 | es_putc ('\n', listfp); |
2895 | 0 | } |
2896 | 0 | } |
2897 | | |
2898 | 7.25k | leave: |
2899 | 7.25k | iobuf_skip_rest (inp, pktlen, 0); |
2900 | 7.25k | return rc; |
2901 | 3.67k | } |
2902 | | |
2903 | | |
2904 | | static int |
2905 | | parse_key (IOBUF inp, int pkttype, unsigned long pktlen, |
2906 | | byte * hdr, int hdrlen, PACKET * pkt) |
2907 | 41.0k | { |
2908 | 41.0k | gpg_error_t err = 0; |
2909 | 41.0k | int i, version, algorithm; |
2910 | 41.0k | unsigned long timestamp, expiredate, max_expiredate; |
2911 | 41.0k | int npkey, nskey; |
2912 | 41.0k | u32 keyid[2]; |
2913 | 41.0k | PKT_public_key *pk; |
2914 | 41.0k | int is_v5 = 0; |
2915 | 41.0k | int is_v6 = 0; |
2916 | 41.0k | unsigned int pkbytes; /* For v5 keys: Number of bytes in the public |
2917 | | * key material. For v4 keys: 0. */ |
2918 | | |
2919 | 41.0k | (void) hdr; |
2920 | | |
2921 | 41.0k | pk = pkt->pkt.public_key; /* PK has been cleared. */ |
2922 | | |
2923 | 41.0k | version = iobuf_get_noeof (inp); |
2924 | 41.0k | pktlen--; |
2925 | 41.0k | if (pkttype == PKT_PUBLIC_SUBKEY && version == '#') |
2926 | 4.37k | { |
2927 | | /* Early versions of G10 used the old PGP comments packets; |
2928 | | * luckily all those comments are started by a hash. */ |
2929 | 4.37k | if (list_mode) |
2930 | 0 | { |
2931 | 0 | es_fprintf (listfp, ":rfc1991 comment packet: \""); |
2932 | 0 | for (; pktlen; pktlen--) |
2933 | 0 | { |
2934 | 0 | int c; |
2935 | 0 | c = iobuf_get (inp); |
2936 | 0 | if (c == -1) |
2937 | 0 | break; /* Ooops: shorter than indicated. */ |
2938 | 0 | if (c >= ' ' && c <= 'z') |
2939 | 0 | es_putc (c, listfp); |
2940 | 0 | else |
2941 | 0 | es_fprintf (listfp, "\\x%02x", c); |
2942 | 0 | } |
2943 | 0 | es_fprintf (listfp, "\"\n"); |
2944 | 0 | } |
2945 | 4.37k | iobuf_skip_rest (inp, pktlen, 0); |
2946 | 4.37k | return 0; |
2947 | 4.37k | } |
2948 | 36.6k | else if (version == 4) |
2949 | 34.5k | ; |
2950 | 2.10k | else if (version == 5) |
2951 | 340 | is_v5 = 1; |
2952 | 1.76k | else if (version == 6 && RFC9980) |
2953 | 22 | is_v6 = 1; |
2954 | 1.74k | else if (version == 2 || version == 3) |
2955 | 483 | { |
2956 | | /* Not anymore supported since 2.1. Use an older gpg version |
2957 | | * (i.e. gpg 1.4) to parse v3 packets. */ |
2958 | 483 | if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2959 | 483 | log_info ("packet(%d) with obsolete version %d\n", pkttype, version); |
2960 | 483 | if (list_mode) |
2961 | 483 | es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version); |
2962 | 483 | pk->version = version; |
2963 | 483 | err = gpg_error (GPG_ERR_LEGACY_KEY); |
2964 | 483 | goto leave; |
2965 | 483 | } |
2966 | 1.26k | else |
2967 | 1.26k | { |
2968 | 1.26k | log_error ("packet(%d) with unknown version %d\n", pkttype, version); |
2969 | 1.26k | if (list_mode) |
2970 | 1.26k | es_fputs (":key packet: [unknown version]\n", listfp); |
2971 | 1.26k | err = gpg_error (GPG_ERR_INV_PACKET); |
2972 | 1.26k | goto leave; |
2973 | 1.26k | } |
2974 | | |
2975 | 34.9k | if (pktlen < (is_v5? 15:11)) |
2976 | 577 | { |
2977 | 577 | log_error ("packet(%d) too short\n", pkttype); |
2978 | 577 | if (list_mode) |
2979 | 577 | es_fputs (":key packet: [too short]\n", listfp); |
2980 | 577 | err = gpg_error (GPG_ERR_INV_PACKET); |
2981 | 577 | goto leave; |
2982 | 577 | } |
2983 | 34.3k | else if (pktlen > MAX_KEY_PACKET_LENGTH) |
2984 | 23 | { |
2985 | 23 | log_error ("packet(%d) too large\n", pkttype); |
2986 | 23 | if (list_mode) |
2987 | 23 | es_fputs (":key packet: [too large]\n", listfp); |
2988 | 23 | err = gpg_error (GPG_ERR_INV_PACKET); |
2989 | 23 | goto leave; |
2990 | 23 | } |
2991 | | |
2992 | 34.3k | timestamp = read_32 (inp); |
2993 | 34.3k | pktlen -= 4; |
2994 | 34.3k | expiredate = 0; /* have to get it from the selfsignature */ |
2995 | 34.3k | max_expiredate = 0; |
2996 | 34.3k | algorithm = iobuf_get_noeof (inp); |
2997 | 34.3k | pktlen--; |
2998 | 34.3k | if (is_v5 || is_v6) |
2999 | 356 | { |
3000 | 356 | pkbytes = read_32 (inp); |
3001 | 356 | pktlen -= 4; |
3002 | 356 | } |
3003 | 33.9k | else |
3004 | 33.9k | pkbytes = 0; |
3005 | | |
3006 | 34.3k | if (list_mode) |
3007 | 0 | { |
3008 | 0 | es_fprintf (listfp, ":%s key packet:\n" |
3009 | 0 | "\tversion %d, algo %d, created %lu, expires %lu", |
3010 | 0 | pkttype == PKT_PUBLIC_KEY ? "public" : |
3011 | 0 | pkttype == PKT_SECRET_KEY ? "secret" : |
3012 | 0 | pkttype == PKT_PUBLIC_SUBKEY ? "public sub" : |
3013 | 0 | pkttype == PKT_SECRET_SUBKEY ? "secret sub" : "??", |
3014 | 0 | version, algorithm, timestamp, expiredate); |
3015 | 0 | if (is_v5 || is_v6) |
3016 | 0 | es_fprintf (listfp, ", pkbytes %u\n", pkbytes); |
3017 | 0 | else |
3018 | 0 | es_fprintf (listfp, "\n"); |
3019 | 0 | } |
3020 | | |
3021 | 34.3k | pk->timestamp = timestamp; |
3022 | 34.3k | pk->expiredate = expiredate; |
3023 | 34.3k | pk->max_expiredate = max_expiredate; |
3024 | 34.3k | pk->hdrbytes = hdrlen; |
3025 | 34.3k | pk->version = version; |
3026 | 34.3k | pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY); |
3027 | 34.3k | pk->pubkey_algo = algorithm; |
3028 | | |
3029 | 34.3k | nskey = pubkey_get_nskey (algorithm); |
3030 | 34.3k | npkey = pubkey_get_npkey (algorithm); |
3031 | 34.3k | if (!npkey) |
3032 | 9.97k | { |
3033 | 9.97k | if (list_mode) |
3034 | 9.97k | es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm); |
3035 | 9.97k | unknown_pubkey_warning (algorithm); |
3036 | 9.97k | } |
3037 | | |
3038 | 34.3k | if (!npkey) |
3039 | 9.97k | { |
3040 | | /* Unknown algorithm - put data into an opaque MPI. */ |
3041 | 9.97k | void *tmpp = read_rest (inp, pktlen); |
3042 | | /* Current gcry_mpi_cmp does not handle a (NULL,n>0) nicely and |
3043 | | * thus we avoid to create such an MPI. */ |
3044 | 9.97k | pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0); |
3045 | 9.97k | pktlen = 0; |
3046 | 9.97k | goto leave; |
3047 | 9.97k | } |
3048 | 24.3k | else |
3049 | 24.3k | { |
3050 | 74.7k | for (i = 0; i < npkey; i++) |
3051 | 50.7k | { |
3052 | 50.7k | if ( (algorithm == PUBKEY_ALGO_ECDSA && (i == 0)) |
3053 | 48.9k | || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0)) |
3054 | 34.4k | || (algorithm == PUBKEY_ALGO_ECDH && (i == 0 || i == 2)) |
3055 | 33.8k | || (algorithm == PUBKEY_ALGO_KYBER && (i == 0))) |
3056 | 17.0k | { |
3057 | | /* Read the OID (i==0) or the KDF params (i==2). */ |
3058 | 17.0k | err = read_sized_octet_string (inp, &pktlen, pk->pkey+i); |
3059 | 17.0k | } |
3060 | 33.7k | else if (algorithm == PUBKEY_ALGO_KYBER && i == 2) |
3061 | 43 | { |
3062 | | /* Read the four-octet count prefixed Kyber public key. */ |
3063 | 43 | err = read_sos_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i); |
3064 | 43 | } |
3065 | 33.7k | else if (algorithm == PUBKEY_ALGO_X25519 && RFC9980) |
3066 | 1 | { |
3067 | 1 | err = read_raw_octet_string (inp, &pktlen, 0, 32, |
3068 | 1 | 0, pk->pkey+i); |
3069 | 1 | } |
3070 | 33.7k | else if (algorithm == PUBKEY_ALGO_MLK768_25519 && RFC9980) |
3071 | 5 | { |
3072 | 5 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 32 : 1184, |
3073 | 5 | 0, pk->pkey+i); |
3074 | 5 | } |
3075 | 33.7k | else if ((algorithm == PUBKEY_ALGO_MLK768_NP384 |
3076 | 33.6k | || algorithm == PUBKEY_ALGO_MLK768_BP384) && RFC9980) |
3077 | 13 | { |
3078 | 13 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 97 : 1184, |
3079 | 13 | 0, pk->pkey+i); |
3080 | 13 | } |
3081 | 33.6k | else if (algorithm == PUBKEY_ALGO_MLK1024_448 && RFC9980) |
3082 | 1 | { |
3083 | 1 | if (is_v6) |
3084 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 56 : 1568, |
3085 | 0 | 0, pk->pkey+i); |
3086 | 1 | else |
3087 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3088 | 1 | } |
3089 | 33.6k | else if (algorithm == PUBKEY_ALGO_MLK1024_NP521 && RFC9980) |
3090 | 1 | { |
3091 | 1 | if (is_v6) |
3092 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 133 : 1568, |
3093 | 0 | 0, pk->pkey+i); |
3094 | 1 | else |
3095 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3096 | 1 | } |
3097 | 33.6k | else if (algorithm == PUBKEY_ALGO_MLK1024_BP512 && RFC9980) |
3098 | 4 | { |
3099 | 4 | if (is_v6) |
3100 | 3 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 129 : 1568, |
3101 | 3 | 0, pk->pkey+i); |
3102 | 1 | else |
3103 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3104 | 4 | } |
3105 | 33.6k | else if (algorithm == PUBKEY_ALGO_X25519 && RFC9980) |
3106 | 0 | { |
3107 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, 32, 0, pk->pkey+i); |
3108 | 0 | } |
3109 | 33.6k | else if (algorithm == PUBKEY_ALGO_ED25519 && RFC9980) |
3110 | 3 | { |
3111 | 3 | err = read_raw_octet_string (inp, &pktlen, 0, 32, 0, pk->pkey+i); |
3112 | 3 | } |
3113 | 33.6k | else |
3114 | 33.6k | { |
3115 | | /* Read MPI or SOS. */ |
3116 | 33.6k | unsigned int n = pktlen; |
3117 | 33.6k | if (algorithm == PUBKEY_ALGO_ECDSA |
3118 | 31.8k | || algorithm == PUBKEY_ALGO_EDDSA |
3119 | 17.3k | || algorithm == PUBKEY_ALGO_ECDH |
3120 | 17.0k | || algorithm == PUBKEY_ALGO_KYBER) |
3121 | 16.6k | pk->pkey[i] = sos_read (inp, &n, 0); |
3122 | 17.0k | else |
3123 | 17.0k | pk->pkey[i] = mpi_read (inp, &n, 0); |
3124 | 33.6k | pktlen -= n; |
3125 | 33.6k | if (!pk->pkey[i]) |
3126 | 220 | err = gpg_error (GPG_ERR_INV_PACKET); |
3127 | 33.6k | } |
3128 | 50.7k | if (err) |
3129 | 351 | goto leave; |
3130 | 50.7k | } |
3131 | 23.9k | if (list_mode) |
3132 | 0 | { /* Again so that we have all parameters in pkey[] and can |
3133 | | * do a look forward. We use a hack for Kyber because the |
3134 | | * commonly used function pubkey_string requires an extra |
3135 | | * buffer and, more important, its result depends on an |
3136 | | * configure option. */ |
3137 | 0 | for (i = 0; i < npkey; i++) |
3138 | 0 | { |
3139 | 0 | es_fprintf (listfp, "\tpkey[%d]: ", i); |
3140 | 0 | mpi_print (listfp, pk->pkey[i], mpi_print_mode); |
3141 | 0 | if ((algorithm == PUBKEY_ALGO_ECDSA |
3142 | 0 | || algorithm == PUBKEY_ALGO_EDDSA |
3143 | 0 | || algorithm == PUBKEY_ALGO_ECDH |
3144 | 0 | || algorithm == PUBKEY_ALGO_KYBER) && i==0) |
3145 | 0 | { |
3146 | 0 | char *curve = openpgp_oid_to_str (pk->pkey[0]); |
3147 | 0 | const char *name = openpgp_oid_to_curve (curve, 2); |
3148 | |
|
3149 | 0 | if (algorithm == PUBKEY_ALGO_KYBER) |
3150 | 0 | es_fprintf (listfp, " ky%u_%s (%s)", |
3151 | 0 | nbits_from_pk (pk), name?name:"", curve); |
3152 | 0 | else |
3153 | 0 | es_fprintf (listfp, " %s (%s)", name?name:"", curve); |
3154 | 0 | xfree (curve); |
3155 | 0 | } |
3156 | 0 | es_putc ('\n', listfp); |
3157 | 0 | } |
3158 | 0 | } |
3159 | 23.9k | } |
3160 | 23.9k | if (list_mode) |
3161 | 0 | keyid_from_pk (pk, keyid); |
3162 | | |
3163 | 23.9k | if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY) |
3164 | 5.94k | { |
3165 | 5.94k | struct seckey_info *ski; |
3166 | 5.94k | byte temp[16]; |
3167 | 5.94k | size_t snlen = 0; |
3168 | 5.94k | unsigned int skbytes; |
3169 | | |
3170 | 5.94k | if (pktlen < 1) |
3171 | 1 | { |
3172 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3173 | 1 | goto leave; |
3174 | 1 | } |
3175 | | |
3176 | 5.94k | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
3177 | 5.94k | if (!pk->seckey_info) |
3178 | 0 | { |
3179 | 0 | err = gpg_error_from_syserror (); |
3180 | 0 | goto leave; |
3181 | 0 | } |
3182 | | |
3183 | 5.94k | ski->algo = iobuf_get_noeof (inp); |
3184 | 5.94k | pktlen--; |
3185 | | |
3186 | 5.94k | if (is_v5 || (is_v6 && ski->algo)) |
3187 | 227 | { |
3188 | 227 | unsigned int protcount = 0; |
3189 | | |
3190 | | /* Read the one octet count of the following key-protection |
3191 | | * material. Only required in case of unknown values. */ |
3192 | 227 | if (!pktlen) |
3193 | 0 | { |
3194 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3195 | 0 | goto leave; |
3196 | 0 | } |
3197 | 227 | protcount = iobuf_get_noeof (inp); |
3198 | 227 | pktlen--; |
3199 | 227 | if (list_mode) |
3200 | 227 | es_fprintf (listfp, "\tprotbytes: %u\n", protcount); |
3201 | 227 | } |
3202 | | |
3203 | 5.94k | if (ski->algo) |
3204 | 5.20k | { |
3205 | 5.20k | ski->is_protected = 1; |
3206 | 5.20k | ski->s2k.count = 0; |
3207 | 5.20k | if (ski->algo == 253) |
3208 | 45 | { |
3209 | 45 | if (list_mode) |
3210 | 45 | es_fprintf (listfp, |
3211 | 0 | "\tS2K pseudo algo %d is not yet supported\n", |
3212 | 0 | ski->algo); |
3213 | 45 | err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
3214 | 45 | goto leave; |
3215 | 45 | } |
3216 | 5.16k | else if (ski->algo == 254 || ski->algo == 255) |
3217 | 4.75k | { |
3218 | 4.75k | if (pktlen < 3) |
3219 | 1 | { |
3220 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3221 | 1 | goto leave; |
3222 | 1 | } |
3223 | | |
3224 | 4.75k | ski->sha1chk = (ski->algo == 254); |
3225 | 4.75k | ski->algo = iobuf_get_noeof (inp); |
3226 | 4.75k | pktlen--; |
3227 | | /* Note that a ski->algo > 110 is illegal, but I'm not |
3228 | | * erroring out here as otherwise there would be no way |
3229 | | * to delete such a key. */ |
3230 | 4.75k | ski->s2k.mode = iobuf_get_noeof (inp); |
3231 | 4.75k | pktlen--; |
3232 | 4.75k | ski->s2k.hash_algo = iobuf_get_noeof (inp); |
3233 | 4.75k | pktlen--; |
3234 | | /* Check for the special GNU extension. */ |
3235 | 4.75k | if (ski->s2k.mode == 101) |
3236 | 3.51k | { |
3237 | 17.5k | for (i = 0; i < 4 && pktlen; i++, pktlen--) |
3238 | 14.0k | temp[i] = iobuf_get_noeof (inp); |
3239 | 3.51k | if (i < 4 || memcmp (temp, "GNU", 3)) |
3240 | 61 | { |
3241 | 61 | if (list_mode) |
3242 | 61 | es_fprintf (listfp, "\tunknown S2K %d\n", |
3243 | 0 | ski->s2k.mode); |
3244 | 61 | err = gpg_error (GPG_ERR_INV_PACKET); |
3245 | 61 | goto leave; |
3246 | 61 | } |
3247 | | /* Here we know that it is a GNU extension. What |
3248 | | * follows is the GNU protection mode: All values |
3249 | | * have special meanings and they are mapped to MODE |
3250 | | * with a base of 1000. */ |
3251 | 3.45k | ski->s2k.mode = 1000 + temp[3]; |
3252 | 3.45k | } |
3253 | | |
3254 | | /* Read the salt. */ |
3255 | 4.69k | if (ski->s2k.mode == 3 || ski->s2k.mode == 1) |
3256 | 1.03k | { |
3257 | 9.25k | for (i = 0; i < 8 && pktlen; i++, pktlen--) |
3258 | 8.22k | temp[i] = iobuf_get_noeof (inp); |
3259 | 1.03k | if (i < 8) |
3260 | 6 | { |
3261 | 6 | err = gpg_error (GPG_ERR_INV_PACKET); |
3262 | 6 | goto leave; |
3263 | 6 | } |
3264 | 1.02k | memcpy (ski->s2k.salt, temp, 8); |
3265 | 1.02k | } |
3266 | | |
3267 | | /* Check the mode. */ |
3268 | 4.68k | switch (ski->s2k.mode) |
3269 | 4.68k | { |
3270 | 165 | case 0: |
3271 | 165 | if (list_mode) |
3272 | 165 | es_fprintf (listfp, "\tsimple S2K"); |
3273 | 165 | break; |
3274 | 385 | case 1: |
3275 | 385 | if (list_mode) |
3276 | 385 | es_fprintf (listfp, "\tsalted S2K"); |
3277 | 385 | break; |
3278 | 640 | case 3: |
3279 | 640 | if (list_mode) |
3280 | 640 | es_fprintf (listfp, "\titer+salt S2K"); |
3281 | 640 | break; |
3282 | 294 | case 1001: |
3283 | 294 | if (list_mode) |
3284 | 294 | es_fprintf (listfp, "\tgnu-dummy"); |
3285 | 294 | break; |
3286 | 3.10k | case 1002: |
3287 | 3.10k | if (list_mode) |
3288 | 3.10k | es_fprintf (listfp, "\tgnu-divert-to-card"); |
3289 | 3.10k | break; |
3290 | 51 | case 1003: |
3291 | 51 | if (list_mode) |
3292 | 51 | es_fprintf (listfp, "\tgnu-mode1003"); |
3293 | 51 | break; |
3294 | 52 | default: |
3295 | 52 | if (list_mode) |
3296 | 52 | es_fprintf (listfp, "\tunknown %sS2K %d\n", |
3297 | 0 | ski->s2k.mode < 1000 ? "" : "GNU ", |
3298 | 0 | ski->s2k.mode); |
3299 | 52 | err = gpg_error (GPG_ERR_INV_PACKET); |
3300 | 52 | goto leave; |
3301 | 4.68k | } |
3302 | | |
3303 | | /* Print some info. */ |
3304 | 4.63k | if (list_mode && ski->s2k.mode != 1003) |
3305 | 0 | { |
3306 | 0 | es_fprintf (listfp, ", algo: %d,%s hash: %d", |
3307 | 0 | ski->algo, |
3308 | 0 | ski->sha1chk ? " SHA1 protection," |
3309 | 0 | : " simple checksum,", ski->s2k.hash_algo); |
3310 | 0 | if (ski->s2k.mode == 1 || ski->s2k.mode == 3) |
3311 | 0 | { |
3312 | 0 | es_fprintf (listfp, ", salt: "); |
3313 | 0 | es_write_hexstring (listfp, ski->s2k.salt, 8, 0, NULL); |
3314 | 0 | } |
3315 | 0 | } |
3316 | 4.63k | if (list_mode) |
3317 | 4.63k | es_putc ('\n', listfp); |
3318 | | |
3319 | | /* Read remaining protection parameters. */ |
3320 | 4.63k | if (ski->s2k.mode == 3) |
3321 | 640 | { |
3322 | 640 | if (pktlen < 1) |
3323 | 1 | { |
3324 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3325 | 1 | goto leave; |
3326 | 1 | } |
3327 | 639 | ski->s2k.count = iobuf_get_noeof (inp); |
3328 | 639 | pktlen--; |
3329 | 639 | if (list_mode) |
3330 | 639 | es_fprintf (listfp, "\tprotect count: %lu (%lu)\n", |
3331 | 0 | (ulong)S2K_DECODE_COUNT ((ulong)ski->s2k.count), |
3332 | 0 | (ulong) ski->s2k.count); |
3333 | 639 | } |
3334 | 3.99k | else if (ski->s2k.mode == 1002) |
3335 | 3.10k | { |
3336 | | /* Read the serial number. */ |
3337 | 3.10k | if (pktlen < 1) |
3338 | 0 | { |
3339 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3340 | 0 | goto leave; |
3341 | 0 | } |
3342 | 3.10k | snlen = iobuf_get (inp); |
3343 | 3.10k | pktlen--; |
3344 | 3.10k | if (pktlen < snlen || snlen == (size_t)(-1)) |
3345 | 9 | { |
3346 | 9 | err = gpg_error (GPG_ERR_INV_PACKET); |
3347 | 9 | goto leave; |
3348 | 9 | } |
3349 | 3.10k | } |
3350 | 4.63k | } |
3351 | 408 | else /* Old version; no S2K, so we set mode to 0, hash MD5. */ |
3352 | 408 | { |
3353 | | /* Note that a ski->algo > 110 is illegal, but I'm not |
3354 | | erroring on it here as otherwise there would be no |
3355 | | way to delete such a key. */ |
3356 | 408 | ski->s2k.mode = 0; |
3357 | 408 | ski->s2k.hash_algo = DIGEST_ALGO_MD5; |
3358 | 408 | if (list_mode) |
3359 | 408 | es_fprintf (listfp, "\tprotect algo: %d (hash algo: %d)\n", |
3360 | 0 | ski->algo, ski->s2k.hash_algo); |
3361 | 408 | } |
3362 | | |
3363 | | /* It is really ugly that we don't know the size |
3364 | | * of the IV here in cases we are not aware of the algorithm. |
3365 | | * so a |
3366 | | * ski->ivlen = cipher_get_blocksize (ski->algo); |
3367 | | * won't work. The only solution I see is to hardwire it. |
3368 | | * NOTE: if you change the ivlen above 16, don't forget to |
3369 | | * enlarge temp. |
3370 | | * FIXME: For v5 keys we can deduce this info! |
3371 | | */ |
3372 | 5.03k | ski->ivlen = openpgp_cipher_blocklen (ski->algo); |
3373 | 5.03k | log_assert (ski->ivlen <= sizeof (temp)); |
3374 | | |
3375 | 5.03k | if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003) |
3376 | 345 | ski->ivlen = 0; |
3377 | 4.68k | else if (ski->s2k.mode == 1002) |
3378 | 3.09k | ski->ivlen = snlen < 16 ? snlen : 16; |
3379 | | |
3380 | 5.03k | if (pktlen < ski->ivlen) |
3381 | 8 | { |
3382 | 8 | err = gpg_error (GPG_ERR_INV_PACKET); |
3383 | 8 | goto leave; |
3384 | 8 | } |
3385 | 43.4k | for (i = 0; i < ski->ivlen; i++, pktlen--) |
3386 | 38.4k | temp[i] = iobuf_get_noeof (inp); |
3387 | 5.02k | if (list_mode && ski->s2k.mode != 1003) |
3388 | 0 | { |
3389 | 0 | es_fprintf (listfp, |
3390 | 0 | ski->s2k.mode == 1002 ? "\tserial-number: " |
3391 | 0 | : "\tprotect IV: "); |
3392 | 0 | for (i = 0; i < ski->ivlen; i++) |
3393 | 0 | es_fprintf (listfp, " %02x", temp[i]); |
3394 | 0 | es_putc ('\n', listfp); |
3395 | 0 | } |
3396 | 5.02k | memcpy (ski->iv, temp, ski->ivlen); |
3397 | 5.02k | } |
3398 | | |
3399 | | /* Skip count of secret key material. */ |
3400 | 5.76k | if (is_v5) |
3401 | 218 | { |
3402 | 218 | if (pktlen < 4) |
3403 | 0 | { |
3404 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3405 | 0 | goto leave; |
3406 | 0 | } |
3407 | 218 | skbytes = read_32 (inp); |
3408 | 218 | pktlen -= 4; |
3409 | 218 | if (list_mode) |
3410 | 218 | es_fprintf (listfp, "\tskbytes: %u\n", skbytes); |
3411 | 218 | } |
3412 | | |
3413 | | /* It does not make sense to read it into secure memory. |
3414 | | * If the user is so careless, not to protect his secret key, |
3415 | | * we can assume, that he operates an open system :=(. |
3416 | | * So we put the key into secure memory when we unprotect it. */ |
3417 | 5.76k | if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002) |
3418 | 3.38k | { |
3419 | | /* Better set some dummy stuff here. */ |
3420 | 3.38k | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3421 | 3.38k | xstrdup ("dummydata"), |
3422 | 3.38k | 10 * 8); |
3423 | 3.38k | pktlen = 0; |
3424 | 3.38k | } |
3425 | 2.38k | else if (ski->s2k.mode == 1003) |
3426 | 51 | { |
3427 | 51 | void *tmpp; |
3428 | | |
3429 | 51 | if (pktlen < 2) /* At least two bytes for parenthesis. */ |
3430 | 1 | { |
3431 | 1 | err = gpg_error (GPG_ERR_INV_PACKET); |
3432 | 1 | goto leave; |
3433 | 1 | } |
3434 | | |
3435 | 50 | tmpp = read_rest (inp, pktlen); |
3436 | 50 | if (list_mode) |
3437 | 0 | { |
3438 | 0 | if (mpi_print_mode) |
3439 | 0 | { |
3440 | 0 | char *tmpsxp = canon_sexp_to_string (tmpp, pktlen); |
3441 | |
|
3442 | 0 | es_fprintf (listfp, "\tskey[%d]: %s\n", npkey, |
3443 | 0 | tmpsxp? trim_trailing_spaces (tmpsxp) |
3444 | 0 | /* */: "[invalid S-expression]"); |
3445 | 0 | xfree (tmpsxp); |
3446 | 0 | } |
3447 | 0 | else |
3448 | 0 | es_fprintf (listfp, "\tskey[%d]: [s-expression %lu octets]\n", |
3449 | 0 | npkey, pktlen); |
3450 | 0 | } |
3451 | 50 | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3452 | 50 | tmpp, tmpp? pktlen * 8 : 0); |
3453 | 50 | pktlen = 0; |
3454 | 50 | } |
3455 | 2.32k | else if (ski->is_protected) |
3456 | 1.58k | { |
3457 | 1.58k | void *tmpp; |
3458 | | |
3459 | 1.58k | if (pktlen < 2) /* At least two bytes for the length. */ |
3460 | 2 | { |
3461 | 2 | err = gpg_error (GPG_ERR_INV_PACKET); |
3462 | 2 | goto leave; |
3463 | 2 | } |
3464 | | |
3465 | | /* Ugly: The length is encrypted too, so we read all stuff |
3466 | | * up to the end of the packet into the first SKEY |
3467 | | * element. |
3468 | | * FIXME: We can do better for v5 keys. */ |
3469 | | |
3470 | 1.58k | tmpp = read_rest (inp, pktlen); |
3471 | 1.58k | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3472 | 1.58k | tmpp, tmpp? pktlen * 8 : 0); |
3473 | | /* Mark that MPI as protected - we need this information for |
3474 | | * importing a key. The OPAQUE flag can't be used because |
3475 | | * we also store public EdDSA values in opaque MPIs. */ |
3476 | 1.58k | if (pk->pkey[npkey]) |
3477 | 1.58k | gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1); |
3478 | 1.58k | pktlen = 0; |
3479 | 1.58k | if (list_mode) |
3480 | 1.58k | es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey); |
3481 | 1.58k | } |
3482 | 740 | else |
3483 | 740 | { |
3484 | | /* Not encrypted. */ |
3485 | 2.10k | for (i = npkey; i < nskey; i++) |
3486 | 1.37k | { |
3487 | 1.37k | if (pktlen < 2) /* At least two bytes for the length. */ |
3488 | 6 | { |
3489 | 6 | err = gpg_error (GPG_ERR_INV_PACKET); |
3490 | 6 | goto leave; |
3491 | 6 | } |
3492 | 1.36k | if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1) |
3493 | 2 | { |
3494 | 2 | err = read_sos_octet_string (inp, &pktlen, 4, 0, |
3495 | 2 | 1, pk->pkey+i); |
3496 | 2 | if (err) |
3497 | 1 | goto leave; |
3498 | 2 | } |
3499 | 1.36k | else if (algorithm == PUBKEY_ALGO_X25519 && RFC9980) |
3500 | 0 | { |
3501 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, 32, |
3502 | 0 | 0, pk->pkey+i); |
3503 | 0 | } |
3504 | 1.36k | else if (algorithm == PUBKEY_ALGO_MLK768_25519 && RFC9980) |
3505 | 0 | { |
3506 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, |
3507 | 0 | i == npkey? 32 : 64, |
3508 | 0 | 0, pk->pkey+i); |
3509 | 0 | } |
3510 | 1.36k | else if ((algorithm == PUBKEY_ALGO_MLK768_NP384 |
3511 | 1.36k | || algorithm == PUBKEY_ALGO_MLK768_BP384) && RFC9980) |
3512 | 0 | { |
3513 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, |
3514 | 0 | i == npkey? 48 : 64, |
3515 | 0 | 0, pk->pkey+i); |
3516 | 0 | } |
3517 | 1.36k | else if (algorithm == PUBKEY_ALGO_MLK1024_448 && RFC9980) |
3518 | 0 | { |
3519 | 0 | if (is_v6) |
3520 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, |
3521 | 0 | i == npkey? 56 : 64, |
3522 | 0 | 0, pk->pkey+i); |
3523 | 0 | else |
3524 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3525 | 0 | } |
3526 | 1.36k | else if (algorithm == PUBKEY_ALGO_MLK1024_NP521 && RFC9980) |
3527 | 0 | { |
3528 | 0 | if (is_v6) |
3529 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, |
3530 | 0 | i == npkey? 66 : 64, |
3531 | 0 | 0, pk->pkey+i); |
3532 | 0 | else |
3533 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3534 | 0 | } |
3535 | 1.36k | else if (algorithm == PUBKEY_ALGO_MLK1024_BP512 && RFC9980) |
3536 | 0 | { |
3537 | 0 | if (is_v6) |
3538 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, |
3539 | 0 | i == npkey? 64 : 64, |
3540 | 0 | 0, pk->pkey+i); |
3541 | 0 | else |
3542 | 0 | err = gpg_error (GPG_ERR_INV_PACKET); |
3543 | 0 | } |
3544 | 1.36k | else if (algorithm == PUBKEY_ALGO_X25519 && RFC9980) |
3545 | 0 | { |
3546 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, 32, |
3547 | 0 | 0, pk->pkey+i); |
3548 | 0 | } |
3549 | 1.36k | else if (algorithm == PUBKEY_ALGO_ED25519 && RFC9980) |
3550 | 0 | { |
3551 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, 32, |
3552 | 0 | 0, pk->pkey+i); |
3553 | 0 | } |
3554 | 1.36k | else |
3555 | 1.36k | { |
3556 | 1.36k | unsigned int n = pktlen; |
3557 | | |
3558 | 1.36k | if (algorithm == PUBKEY_ALGO_ECDSA |
3559 | 1.34k | || algorithm == PUBKEY_ALGO_EDDSA |
3560 | 1.30k | || algorithm == PUBKEY_ALGO_ECDH |
3561 | 1.17k | || algorithm == PUBKEY_ALGO_KYBER) |
3562 | 188 | pk->pkey[i] = sos_read (inp, &n, 0); |
3563 | 1.17k | else |
3564 | 1.17k | pk->pkey[i] = mpi_read (inp, &n, 0); |
3565 | 1.36k | pktlen -= n; |
3566 | 1.36k | } |
3567 | | |
3568 | 1.36k | if (list_mode) |
3569 | 0 | { |
3570 | 0 | es_fprintf (listfp, "\tskey[%d]: ", i); |
3571 | 0 | mpi_print (listfp, pk->pkey[i], mpi_print_mode); |
3572 | 0 | es_putc ('\n', listfp); |
3573 | 0 | } |
3574 | | |
3575 | 1.36k | if (!pk->pkey[i]) |
3576 | 152 | err = gpg_error (GPG_ERR_INV_PACKET); |
3577 | 1.36k | } |
3578 | 733 | if (err) |
3579 | 52 | goto leave; |
3580 | | |
3581 | 681 | if (!is_v6) |
3582 | 680 | { |
3583 | 680 | if (pktlen < 2) |
3584 | 2 | { |
3585 | 2 | if (opt.verbose) |
3586 | 2 | log_info ("checksum is missing (remaining bytes: %lu)\n", |
3587 | 0 | pktlen); |
3588 | 2 | err = gpg_error (GPG_ERR_INV_PACKET); |
3589 | 2 | goto leave; |
3590 | 2 | } |
3591 | 678 | ski->csum = read_16 (inp); |
3592 | 678 | pktlen -= 2; |
3593 | 678 | if (list_mode) |
3594 | 678 | es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum); |
3595 | 678 | } |
3596 | 681 | } |
3597 | 5.76k | } |
3598 | | |
3599 | | /* Note that KEYID below has been initialized above in list_mode. */ |
3600 | 23.7k | if (list_mode) |
3601 | 23.7k | es_fprintf (listfp, "\tkeyid: %08lX%08lX\n", |
3602 | 0 | (ulong) keyid[0], (ulong) keyid[1]); |
3603 | 23.7k | if (list_mode && (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY) |
3604 | 0 | && (opt.list_options & LIST_DEBUG_SHOW_SEXP)) |
3605 | 0 | { |
3606 | 0 | nvc_t nvc = seckey_packet_to_nvc (pk); |
3607 | |
|
3608 | 0 | es_fputs ("\ts-exp: ", listfp); |
3609 | 0 | if (!nvc) |
3610 | 0 | es_fputs ("[error getting key]\n", listfp); |
3611 | 0 | else |
3612 | 0 | { |
3613 | 0 | nvc_write (nvc, listfp); |
3614 | 0 | nvc_release (nvc); |
3615 | 0 | } |
3616 | 0 | } |
3617 | | |
3618 | | |
3619 | 36.6k | leave: |
3620 | 36.6k | iobuf_skip_rest (inp, pktlen, 0); |
3621 | 36.6k | return err; |
3622 | 23.7k | } |
3623 | | |
3624 | | |
3625 | | /* Attribute subpackets have the same format as v4 signature |
3626 | | subpackets. This is not part of OpenPGP, but is done in several |
3627 | | versions of PGP nevertheless. */ |
3628 | | int |
3629 | | parse_attribute_subpkts (PKT_user_id * uid) |
3630 | 10.1k | { |
3631 | 10.1k | size_t n; |
3632 | 10.1k | int count = 0; |
3633 | 10.1k | struct user_attribute *attribs = NULL; |
3634 | 10.1k | const byte *buffer = uid->attrib_data; |
3635 | 10.1k | int buflen = uid->attrib_len; |
3636 | 10.1k | byte type; |
3637 | | |
3638 | 10.1k | xfree (uid->attribs); |
3639 | | |
3640 | 17.5k | while (buflen) |
3641 | 12.5k | { |
3642 | 12.5k | n = *buffer++; |
3643 | 12.5k | buflen--; |
3644 | 12.5k | if (n == 255) /* 4 byte length header. */ |
3645 | 2.96k | { |
3646 | 2.96k | if (buflen < 4) |
3647 | 205 | goto too_short; |
3648 | 2.76k | n = buf32_to_size_t (buffer); |
3649 | 2.76k | buffer += 4; |
3650 | 2.76k | buflen -= 4; |
3651 | 2.76k | } |
3652 | 9.61k | else if (n >= 192) /* 2 byte special encoded length header. */ |
3653 | 920 | { |
3654 | 920 | if (buflen < 2) |
3655 | 651 | goto too_short; |
3656 | 269 | n = ((n - 192) << 8) + *buffer + 192; |
3657 | 269 | buffer++; |
3658 | 269 | buflen--; |
3659 | 269 | } |
3660 | 11.7k | if (buflen < n) |
3661 | 3.20k | goto too_short; |
3662 | | |
3663 | 8.52k | if (!n) |
3664 | 1.08k | { |
3665 | | /* Too short to encode the subpacket type. */ |
3666 | 1.08k | if (opt.verbose) |
3667 | 1.08k | log_info ("attribute subpacket too short\n"); |
3668 | 1.08k | break; |
3669 | 1.08k | } |
3670 | | |
3671 | 7.44k | attribs = xrealloc (attribs, |
3672 | 7.44k | (count + 1) * sizeof (struct user_attribute)); |
3673 | 7.44k | memset (&attribs[count], 0, sizeof (struct user_attribute)); |
3674 | | |
3675 | 7.44k | type = *buffer; |
3676 | 7.44k | buffer++; |
3677 | 7.44k | buflen--; |
3678 | 7.44k | n--; |
3679 | | |
3680 | 7.44k | attribs[count].type = type; |
3681 | 7.44k | attribs[count].data = buffer; |
3682 | 7.44k | attribs[count].len = n; |
3683 | 7.44k | buffer += n; |
3684 | 7.44k | buflen -= n; |
3685 | 7.44k | count++; |
3686 | 7.44k | } |
3687 | | |
3688 | 6.07k | uid->attribs = attribs; |
3689 | 6.07k | uid->numattribs = count; |
3690 | 6.07k | return count; |
3691 | | |
3692 | 4.06k | too_short: |
3693 | 4.06k | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
3694 | 4.06k | log_info ("buffer shorter than attribute subpacket\n"); |
3695 | 4.06k | uid->attribs = attribs; |
3696 | 4.06k | uid->numattribs = count; |
3697 | 4.06k | return count; |
3698 | 10.1k | } |
3699 | | |
3700 | | |
3701 | | static int |
3702 | | parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet) |
3703 | 31.3k | { |
3704 | 31.3k | byte *p; |
3705 | | |
3706 | | /* Cap the size of a user ID at 2k: a value absurdly large enough |
3707 | | that there is no sane user ID string (which is printable text |
3708 | | as of RFC2440bis) that won't fit in it, but yet small enough to |
3709 | | avoid allocation problems. A large pktlen may not be |
3710 | | allocatable, and a very large pktlen could actually cause our |
3711 | | allocation to wrap around in xmalloc to a small number. */ |
3712 | | |
3713 | 31.3k | if (pktlen > MAX_UID_PACKET_LENGTH) |
3714 | 508 | { |
3715 | 508 | log_error ("packet(%d) too large\n", pkttype); |
3716 | 508 | if (list_mode) |
3717 | 508 | es_fprintf (listfp, ":user ID packet: [too large]\n"); |
3718 | 508 | iobuf_skip_rest (inp, pktlen, 0); |
3719 | 508 | return GPG_ERR_INV_PACKET; |
3720 | 508 | } |
3721 | | |
3722 | 30.8k | packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen); |
3723 | 30.8k | packet->pkt.user_id->len = pktlen; |
3724 | 30.8k | packet->pkt.user_id->ref = 1; |
3725 | | |
3726 | 30.8k | p = packet->pkt.user_id->name; |
3727 | 273k | for (; pktlen; pktlen--, p++) |
3728 | 243k | *p = iobuf_get_noeof (inp); |
3729 | 30.8k | *p = 0; |
3730 | | |
3731 | 30.8k | if (list_mode) |
3732 | 0 | { |
3733 | 0 | int n = packet->pkt.user_id->len; |
3734 | 0 | es_fprintf (listfp, ":user ID packet: \""); |
3735 | | /* fixme: Hey why don't we replace this with es_write_sanitized?? */ |
3736 | 0 | for (p = packet->pkt.user_id->name; n; p++, n--) |
3737 | 0 | { |
3738 | 0 | if (*p >= ' ' && *p <= 'z') |
3739 | 0 | es_putc (*p, listfp); |
3740 | 0 | else |
3741 | 0 | es_fprintf (listfp, "\\x%02x", *p); |
3742 | 0 | } |
3743 | 0 | es_fprintf (listfp, "\"\n"); |
3744 | 0 | } |
3745 | 30.8k | return 0; |
3746 | 31.3k | } |
3747 | | |
3748 | | |
3749 | | void |
3750 | | make_attribute_uidname (PKT_user_id * uid, size_t max_namelen) |
3751 | 10.1k | { |
3752 | 10.1k | log_assert (max_namelen > 70); |
3753 | 10.1k | if (uid->numattribs <= 0) |
3754 | 7.00k | sprintf (uid->name, "[bad attribute packet of size %lu]", |
3755 | 7.00k | uid->attrib_len); |
3756 | 3.12k | else if (uid->numattribs > 1) |
3757 | 221 | sprintf (uid->name, "[%d attributes of size %lu]", |
3758 | 221 | uid->numattribs, uid->attrib_len); |
3759 | 2.90k | else |
3760 | 2.90k | { |
3761 | | /* Only one attribute, so list it as the "user id" */ |
3762 | | |
3763 | 2.90k | if (uid->attribs->type == ATTRIB_IMAGE) |
3764 | 2.36k | { |
3765 | 2.36k | u32 len; |
3766 | 2.36k | byte type; |
3767 | | |
3768 | 2.36k | if (parse_image_header (uid->attribs, &type, &len)) |
3769 | 1.91k | sprintf (uid->name, "[%.20s image of size %lu]", |
3770 | 1.91k | image_type_to_string (type, 1), (ulong) len); |
3771 | 449 | else |
3772 | 449 | sprintf (uid->name, "[invalid image]"); |
3773 | 2.36k | } |
3774 | 545 | else |
3775 | 545 | sprintf (uid->name, "[unknown attribute of size %lu]", |
3776 | 545 | (ulong) uid->attribs->len); |
3777 | 2.90k | } |
3778 | | |
3779 | 10.1k | uid->len = strlen (uid->name); |
3780 | 10.1k | } |
3781 | | |
3782 | | |
3783 | | static int |
3784 | | parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen, |
3785 | | PACKET * packet) |
3786 | 10.2k | { |
3787 | 10.2k | byte *p; |
3788 | | |
3789 | 10.2k | (void) pkttype; |
3790 | | |
3791 | | /* We better cap the size of an attribute packet to make DoS not too |
3792 | | easy. 16MB should be more then enough for one attribute packet |
3793 | | (ie. a photo). */ |
3794 | 10.2k | if (pktlen > MAX_ATTR_PACKET_LENGTH) |
3795 | 161 | { |
3796 | 161 | log_error ("packet(%d) too large\n", pkttype); |
3797 | 161 | if (list_mode) |
3798 | 161 | es_fprintf (listfp, ":attribute packet: [too large]\n"); |
3799 | 161 | iobuf_skip_rest (inp, pktlen, 0); |
3800 | 161 | return GPG_ERR_INV_PACKET; |
3801 | 161 | } |
3802 | | |
3803 | 10.1k | #define EXTRA_UID_NAME_SPACE 71 |
3804 | 10.1k | packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id |
3805 | 10.1k | + EXTRA_UID_NAME_SPACE); |
3806 | 10.1k | packet->pkt.user_id->ref = 1; |
3807 | 10.1k | packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1); |
3808 | 10.1k | packet->pkt.user_id->attrib_len = pktlen; |
3809 | | |
3810 | 10.1k | p = packet->pkt.user_id->attrib_data; |
3811 | 622M | for (; pktlen; pktlen--, p++) |
3812 | 622M | *p = iobuf_get_noeof (inp); |
3813 | | |
3814 | | /* Now parse out the individual attribute subpackets. This is |
3815 | | somewhat pointless since there is only one currently defined |
3816 | | attribute type (jpeg), but it is correct by the spec. */ |
3817 | 10.1k | parse_attribute_subpkts (packet->pkt.user_id); |
3818 | | |
3819 | 10.1k | make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE); |
3820 | | |
3821 | 10.1k | if (list_mode) |
3822 | 0 | { |
3823 | 0 | es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name); |
3824 | 0 | } |
3825 | 10.1k | return 0; |
3826 | 10.2k | } |
3827 | | |
3828 | | |
3829 | | static int |
3830 | | parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet) |
3831 | 5.05k | { |
3832 | 5.05k | byte *p; |
3833 | | |
3834 | | /* Cap comment packet at a reasonable value to avoid an integer |
3835 | | overflow in the malloc below. Comment packets are actually not |
3836 | | anymore define my OpenPGP and we even stopped to use our |
3837 | | private comment packet. */ |
3838 | 5.05k | if (pktlen > MAX_COMMENT_PACKET_LENGTH) |
3839 | 18 | { |
3840 | 18 | log_error ("packet(%d) too large\n", pkttype); |
3841 | 18 | if (list_mode) |
3842 | 18 | es_fprintf (listfp, ":%scomment packet: [too large]\n", |
3843 | 0 | pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : ""); |
3844 | 18 | iobuf_skip_rest (inp, pktlen, 0); |
3845 | 18 | return GPG_ERR_INV_PACKET; |
3846 | 18 | } |
3847 | 5.03k | packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1); |
3848 | 5.03k | packet->pkt.comment->len = pktlen; |
3849 | 5.03k | p = packet->pkt.comment->data; |
3850 | 661k | for (; pktlen; pktlen--, p++) |
3851 | 656k | *p = iobuf_get_noeof (inp); |
3852 | | |
3853 | 5.03k | if (list_mode) |
3854 | 0 | { |
3855 | 0 | int n = packet->pkt.comment->len; |
3856 | 0 | es_fprintf (listfp, ":%scomment packet: \"", pkttype == PKT_OLD_COMMENT ? |
3857 | 0 | "OpenPGP draft " : ""); |
3858 | 0 | for (p = packet->pkt.comment->data; n; p++, n--) |
3859 | 0 | { |
3860 | 0 | if (*p >= ' ' && *p <= 'z') |
3861 | 0 | es_putc (*p, listfp); |
3862 | 0 | else |
3863 | 0 | es_fprintf (listfp, "\\x%02x", *p); |
3864 | 0 | } |
3865 | 0 | es_fprintf (listfp, "\"\n"); |
3866 | 0 | } |
3867 | 5.03k | return 0; |
3868 | 5.05k | } |
3869 | | |
3870 | | |
3871 | | /* Parse a ring trust packet RFC4880 (5.10). |
3872 | | * |
3873 | | * This parser is special in that the packet is not stored as a packet |
3874 | | * but its content is merged into the previous packet. */ |
3875 | | static gpg_error_t |
3876 | | parse_ring_trust (parse_packet_ctx_t ctx, unsigned long pktlen) |
3877 | 38.9k | { |
3878 | 38.9k | gpg_error_t err; |
3879 | 38.9k | iobuf_t inp = ctx->inp; |
3880 | 38.9k | PKT_ring_trust rt = {0}; |
3881 | 38.9k | int c; |
3882 | 38.9k | int not_gpg = 0; |
3883 | | |
3884 | 38.9k | if (!pktlen) |
3885 | 258 | { |
3886 | 258 | if (list_mode) |
3887 | 258 | es_fprintf (listfp, ":trust packet: empty\n"); |
3888 | 258 | err = 0; |
3889 | 258 | goto leave; |
3890 | 258 | } |
3891 | | |
3892 | 38.7k | c = iobuf_get_noeof (inp); |
3893 | 38.7k | pktlen--; |
3894 | 38.7k | rt.trustval = c; |
3895 | 38.7k | if (pktlen) |
3896 | 38.3k | { |
3897 | 38.3k | if (!c) |
3898 | 1.07k | { |
3899 | 1.07k | c = iobuf_get_noeof (inp); |
3900 | | /* We require that bit 7 of the sigcache is 0 (easier |
3901 | | * eof handling). */ |
3902 | 1.07k | if (!(c & 0x80)) |
3903 | 731 | rt.sigcache = c; |
3904 | 1.07k | } |
3905 | 37.2k | else |
3906 | 37.2k | iobuf_get_noeof (inp); /* Dummy read. */ |
3907 | 38.3k | pktlen--; |
3908 | 38.3k | } |
3909 | | |
3910 | | /* Next is the optional subtype. */ |
3911 | 38.7k | if (pktlen > 3) |
3912 | 37.8k | { |
3913 | 37.8k | char tmp[4]; |
3914 | 37.8k | tmp[0] = iobuf_get_noeof (inp); |
3915 | 37.8k | tmp[1] = iobuf_get_noeof (inp); |
3916 | 37.8k | tmp[2] = iobuf_get_noeof (inp); |
3917 | 37.8k | tmp[3] = iobuf_get_noeof (inp); |
3918 | 37.8k | pktlen -= 4; |
3919 | 37.8k | if (!memcmp (tmp, "gpg", 3)) |
3920 | 11.5k | rt.subtype = tmp[3]; |
3921 | 26.2k | else |
3922 | 26.2k | not_gpg = 1; |
3923 | 37.8k | } |
3924 | | /* If it is a key or uid subtype read the remaining data. */ |
3925 | 38.7k | if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID) |
3926 | 10.9k | && pktlen >= 6 ) |
3927 | 5.70k | { |
3928 | 5.70k | int i; |
3929 | 5.70k | unsigned int namelen; |
3930 | | |
3931 | 5.70k | rt.keyorg = iobuf_get_noeof (inp); |
3932 | 5.70k | pktlen--; |
3933 | 5.70k | rt.keyupdate = read_32 (inp); |
3934 | 5.70k | pktlen -= 4; |
3935 | 5.70k | namelen = iobuf_get_noeof (inp); |
3936 | 5.70k | pktlen--; |
3937 | 5.70k | if (namelen && pktlen) |
3938 | 5.27k | { |
3939 | 5.27k | rt.url = xtrymalloc (namelen + 1); |
3940 | 5.27k | if (!rt.url) |
3941 | 0 | { |
3942 | 0 | err = gpg_error_from_syserror (); |
3943 | 0 | goto leave; |
3944 | 0 | } |
3945 | 14.7k | for (i = 0; pktlen && i < namelen; pktlen--, i++) |
3946 | 9.44k | rt.url[i] = iobuf_get_noeof (inp); |
3947 | 5.27k | rt.url[i] = 0; |
3948 | 5.27k | } |
3949 | 5.70k | } |
3950 | | |
3951 | 38.7k | if (list_mode) |
3952 | 0 | { |
3953 | 0 | if (rt.subtype == RING_TRUST_SIG) |
3954 | 0 | es_fprintf (listfp, ":trust packet: sig flag=%02x sigcache=%02x\n", |
3955 | 0 | rt.trustval, rt.sigcache); |
3956 | 0 | else if (rt.subtype == RING_TRUST_UID || rt.subtype == RING_TRUST_KEY) |
3957 | 0 | { |
3958 | 0 | unsigned char *p; |
3959 | |
|
3960 | 0 | es_fprintf (listfp, ":trust packet: %s upd=%lu src=%d%s", |
3961 | 0 | (rt.subtype == RING_TRUST_UID? "uid" : "key"), |
3962 | 0 | (unsigned long)rt.keyupdate, |
3963 | 0 | rt.keyorg, |
3964 | 0 | (rt.url? " url=":"")); |
3965 | 0 | if (rt.url) |
3966 | 0 | { |
3967 | 0 | for (p = rt.url; *p; p++) |
3968 | 0 | { |
3969 | 0 | if (*p >= ' ' && *p <= 'z') |
3970 | 0 | es_putc (*p, listfp); |
3971 | 0 | else |
3972 | 0 | es_fprintf (listfp, "\\x%02x", *p); |
3973 | 0 | } |
3974 | 0 | } |
3975 | 0 | es_putc ('\n', listfp); |
3976 | 0 | } |
3977 | 0 | else if (not_gpg) |
3978 | 0 | es_fprintf (listfp, ":trust packet: not created by gpg\n"); |
3979 | 0 | else |
3980 | 0 | es_fprintf (listfp, ":trust packet: subtype=%02x\n", |
3981 | 0 | rt.subtype); |
3982 | 0 | } |
3983 | | |
3984 | | /* Now transfer the data to the respective packet. Do not do this |
3985 | | * if SKIP_META is set. */ |
3986 | 38.7k | if (!ctx->last_pkt.pkt.generic || ctx->skip_meta) |
3987 | 10.0k | ; |
3988 | 28.7k | else if (rt.subtype == RING_TRUST_SIG |
3989 | 20.3k | && ctx->last_pkt.pkttype == PKT_SIGNATURE) |
3990 | 20.0k | { |
3991 | 20.0k | PKT_signature *sig = ctx->last_pkt.pkt.signature; |
3992 | | |
3993 | 20.0k | if ((rt.sigcache & 1)) |
3994 | 123 | { |
3995 | 123 | sig->flags.checked = 1; |
3996 | 123 | sig->flags.valid = !!(rt.sigcache & 2); |
3997 | 123 | } |
3998 | 20.0k | } |
3999 | 8.68k | else if (rt.subtype == RING_TRUST_UID |
4000 | 3.91k | && (ctx->last_pkt.pkttype == PKT_USER_ID |
4001 | 318 | || ctx->last_pkt.pkttype == PKT_ATTRIBUTE)) |
4002 | 3.59k | { |
4003 | 3.59k | PKT_user_id *uid = ctx->last_pkt.pkt.user_id; |
4004 | | |
4005 | 3.59k | uid->keyorg = rt.keyorg; |
4006 | 3.59k | uid->keyupdate = rt.keyupdate; |
4007 | 3.59k | uid->updateurl = rt.url; |
4008 | 3.59k | rt.url = NULL; |
4009 | 3.59k | } |
4010 | 5.08k | else if (rt.subtype == RING_TRUST_KEY |
4011 | 4.15k | && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY |
4012 | 1.52k | || ctx->last_pkt.pkttype == PKT_SECRET_KEY)) |
4013 | 2.64k | { |
4014 | 2.64k | PKT_public_key *pk = ctx->last_pkt.pkt.public_key; |
4015 | | |
4016 | 2.64k | pk->keyorg = rt.keyorg; |
4017 | 2.64k | pk->keyupdate = rt.keyupdate; |
4018 | 2.64k | pk->updateurl = rt.url; |
4019 | 2.64k | rt.url = NULL; |
4020 | 2.64k | } |
4021 | | |
4022 | 38.7k | err = 0; |
4023 | | |
4024 | 38.9k | leave: |
4025 | 38.9k | xfree (rt.url); |
4026 | 38.9k | free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL. */ |
4027 | 38.9k | iobuf_skip_rest (inp, pktlen, 0); |
4028 | 38.9k | return err; |
4029 | 38.7k | } |
4030 | | |
4031 | | |
4032 | | static int |
4033 | | parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen, |
4034 | | PACKET * pkt, int new_ctb, int partial) |
4035 | 21.2k | { |
4036 | 21.2k | int rc = 0; |
4037 | 21.2k | int mode, namelen; |
4038 | 21.2k | PKT_plaintext *pt; |
4039 | 21.2k | byte *p; |
4040 | 21.2k | int c, i; |
4041 | | |
4042 | 21.2k | if (!partial && pktlen < 6) |
4043 | 485 | { |
4044 | 485 | log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen); |
4045 | 485 | if (list_mode) |
4046 | 485 | es_fputs (":literal data packet: [too short]\n", listfp); |
4047 | 485 | rc = gpg_error (GPG_ERR_INV_PACKET); |
4048 | 485 | goto leave; |
4049 | 485 | } |
4050 | 20.7k | mode = iobuf_get_noeof (inp); |
4051 | 20.7k | if (pktlen) |
4052 | 16.6k | pktlen--; |
4053 | 20.7k | namelen = iobuf_get_noeof (inp); |
4054 | 20.7k | if (pktlen) |
4055 | 16.6k | pktlen--; |
4056 | | /* Note that namelen will never exceed 255 bytes. */ |
4057 | 20.7k | pt = pkt->pkt.plaintext = |
4058 | 20.7k | xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1); |
4059 | 20.7k | pt->new_ctb = new_ctb; |
4060 | 20.7k | pt->mode = mode; |
4061 | 20.7k | pt->namelen = namelen; |
4062 | 20.7k | pt->is_partial = partial; |
4063 | 20.7k | if (pktlen) |
4064 | 16.6k | { |
4065 | 65.6k | for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++) |
4066 | 49.0k | pt->name[i] = iobuf_get_noeof (inp); |
4067 | 16.6k | } |
4068 | 4.14k | else |
4069 | 4.14k | { |
4070 | 8.63k | for (i = 0; i < namelen; i++) |
4071 | 4.79k | if ((c = iobuf_get (inp)) == -1) |
4072 | 309 | break; |
4073 | 4.48k | else |
4074 | 4.48k | pt->name[i] = c; |
4075 | 4.14k | } |
4076 | | /* Fill up NAME so that a check with valgrind won't complain about |
4077 | | * reading from uninitialized memory. This case may be triggred by |
4078 | | * corrupted packets. */ |
4079 | 779k | for (; i < namelen; i++) |
4080 | 758k | pt->name[i] = 0; |
4081 | | |
4082 | 20.7k | pt->timestamp = read_32 (inp); |
4083 | 20.7k | if (pktlen) |
4084 | 16.6k | pktlen -= 4; |
4085 | 20.7k | pt->len = pktlen; |
4086 | 20.7k | pt->buf = inp; |
4087 | | |
4088 | 20.7k | if (list_mode) |
4089 | 0 | { |
4090 | 0 | es_fprintf (listfp, ":literal data packet:\n" |
4091 | 0 | "\tmode %c (%X), created %lu, name=\"", |
4092 | 0 | mode >= ' ' && mode < 'z' ? mode : '?', mode, |
4093 | 0 | (ulong) pt->timestamp); |
4094 | 0 | for (p = pt->name, i = 0; i < namelen; p++, i++) |
4095 | 0 | { |
4096 | 0 | if (*p >= ' ' && *p <= 'z') |
4097 | 0 | es_putc (*p, listfp); |
4098 | 0 | else |
4099 | 0 | es_fprintf (listfp, "\\x%02x", *p); |
4100 | 0 | } |
4101 | 0 | es_fprintf (listfp, "\",\n\traw data: "); |
4102 | 0 | if (partial) |
4103 | 0 | es_fprintf (listfp, "unknown length\n"); |
4104 | 0 | else |
4105 | 0 | es_fprintf (listfp, "%lu bytes\n", (ulong) pt->len); |
4106 | 0 | } |
4107 | | |
4108 | 21.2k | leave: |
4109 | 21.2k | return rc; |
4110 | 20.7k | } |
4111 | | |
4112 | | |
4113 | | static int |
4114 | | parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen, |
4115 | | PACKET * pkt, int new_ctb) |
4116 | 51.7k | { |
4117 | 51.7k | PKT_compressed *zd; |
4118 | | |
4119 | | /* PKTLEN is here 0, but data follows (this should be the last |
4120 | | object in a file or the compress algorithm should know the |
4121 | | length). */ |
4122 | 51.7k | (void) pkttype; |
4123 | 51.7k | (void) pktlen; |
4124 | | |
4125 | 51.7k | zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed); |
4126 | 51.7k | zd->algorithm = iobuf_get_noeof (inp); |
4127 | 51.7k | zd->len = 0; /* not used */ |
4128 | 51.7k | zd->new_ctb = new_ctb; |
4129 | 51.7k | zd->buf = inp; |
4130 | 51.7k | if (list_mode) |
4131 | 51.7k | es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm); |
4132 | 51.7k | return 0; |
4133 | 51.7k | } |
4134 | | |
4135 | | |
4136 | | static int |
4137 | | parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen, |
4138 | | PACKET * pkt, int new_ctb, int partial) |
4139 | 11.8k | { |
4140 | 11.8k | int rc = 0; |
4141 | 11.8k | PKT_encrypted *ed; |
4142 | 11.8k | unsigned long orig_pktlen = pktlen; |
4143 | | |
4144 | 11.8k | ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted); |
4145 | | /* ed->len is set below. */ |
4146 | 11.8k | ed->extralen = 0; /* Unknown here; only used in build_packet. */ |
4147 | 11.8k | ed->buf = NULL; |
4148 | 11.8k | ed->new_ctb = new_ctb; |
4149 | 11.8k | ed->is_partial = partial; |
4150 | 11.8k | ed->seipd = 0; |
4151 | 11.8k | ed->version = 0; |
4152 | 11.8k | ed->aead_algo = 0; |
4153 | 11.8k | ed->mdc_method = 0; |
4154 | 11.8k | ed->cipher_algo = 0; /* Only used with AEAD. */ |
4155 | 11.8k | ed->chunkbyte = 0; /* Only used with AEAD. */ |
4156 | 11.8k | if (pkttype == PKT_ENCRYPTED_MDC) |
4157 | 1.66k | { |
4158 | 1.66k | ed->seipd = 1; |
4159 | 1.66k | ed->version = iobuf_get_noeof (inp); |
4160 | 1.66k | if (orig_pktlen) |
4161 | 343 | pktlen--; |
4162 | 1.66k | if (ed->version == 1) |
4163 | 1.20k | ed->mdc_method = DIGEST_ALGO_SHA1; |
4164 | 461 | else if (ed->version == 2 && RFC9980) |
4165 | 25 | { |
4166 | 25 | ed->cipher_algo = iobuf_get_noeof (inp); |
4167 | 25 | if (orig_pktlen) |
4168 | 9 | pktlen--; |
4169 | 25 | ed->aead_algo = iobuf_get_noeof (inp); |
4170 | 25 | if (orig_pktlen) |
4171 | 9 | pktlen--; |
4172 | 25 | ed->chunkbyte = iobuf_get_noeof (inp); |
4173 | 25 | if (orig_pktlen) |
4174 | 9 | pktlen--; |
4175 | 25 | } |
4176 | 436 | else |
4177 | 436 | { |
4178 | 436 | log_error ("encrypted_mdc packet with unknown version %d\n", |
4179 | 436 | ed->version); |
4180 | 436 | if (list_mode) |
4181 | 436 | es_fputs (":encrypted data packet: [unknown version]\n", listfp); |
4182 | | /*skip_rest(inp, pktlen); should we really do this? */ |
4183 | 436 | rc = gpg_error (GPG_ERR_INV_PACKET); |
4184 | 436 | goto leave; |
4185 | 436 | } |
4186 | 1.66k | } |
4187 | | |
4188 | | /* A basic sanity check. We need at least an 8 byte IV plus the 2 |
4189 | | detection bytes. Note that we don't known the algorithm and thus |
4190 | | we may only check against the minimum blocksize. */ |
4191 | 11.3k | if (orig_pktlen && pktlen < 10) |
4192 | 178 | { |
4193 | | /* Actually this is blocksize+2. */ |
4194 | 178 | log_error ("packet(%d) too short\n", pkttype); |
4195 | 178 | if (list_mode) |
4196 | 178 | es_fputs (":encrypted data packet: [too short]\n", listfp); |
4197 | 178 | rc = GPG_ERR_INV_PACKET; |
4198 | 178 | iobuf_skip_rest (inp, pktlen, partial); |
4199 | 178 | goto leave; |
4200 | 178 | } |
4201 | | |
4202 | | /* Store the remaining length of the encrypted data (i.e. without |
4203 | | the MDC version number but with the IV etc.). This value is |
4204 | | required during decryption. */ |
4205 | 11.1k | ed->len = pktlen; |
4206 | | |
4207 | 11.1k | if (list_mode) |
4208 | 0 | { |
4209 | 0 | es_fprintf (listfp, ":encrypted data packet:\n\tversion: %d\n", |
4210 | 0 | ed->version); |
4211 | 0 | if (ed->mdc_method) |
4212 | 0 | es_fprintf (listfp, "\t mdc: %d\n", ed->mdc_method); |
4213 | 0 | if (ed->version == 2) |
4214 | 0 | es_fprintf (listfp, "\t cipher: %u\n\t aead: %u\n\t cbyte: %u\n", |
4215 | 0 | ed->cipher_algo, ed->aead_algo, ed->chunkbyte); |
4216 | 0 | if (orig_pktlen) |
4217 | 0 | es_fprintf (listfp, "\t length: %lu\n", orig_pktlen); |
4218 | 0 | else |
4219 | 0 | es_fprintf (listfp, "\t length: unknown\n"); |
4220 | 0 | } |
4221 | | |
4222 | 11.1k | ed->buf = inp; |
4223 | | |
4224 | 11.8k | leave: |
4225 | 11.8k | return rc; |
4226 | 11.1k | } |
4227 | | |
4228 | | |
4229 | | /* Note, that this code is not anymore used in real life because the |
4230 | | MDC checking is now done right after the decryption in |
4231 | | decrypt_data. */ |
4232 | | static int |
4233 | | parse_mdc (IOBUF inp, int pkttype, unsigned long pktlen, |
4234 | | PACKET * pkt, int new_ctb) |
4235 | 961 | { |
4236 | 961 | int rc = 0; |
4237 | 961 | PKT_mdc *mdc; |
4238 | 961 | byte *p; |
4239 | | |
4240 | 961 | (void) pkttype; |
4241 | | |
4242 | 961 | mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc); |
4243 | 961 | if (list_mode) |
4244 | 961 | es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen); |
4245 | 961 | if (!new_ctb || pktlen != 20) |
4246 | 281 | { |
4247 | 281 | log_error ("mdc_packet with invalid encoding\n"); |
4248 | 281 | rc = gpg_error (GPG_ERR_INV_PACKET); |
4249 | 281 | goto leave; |
4250 | 281 | } |
4251 | 680 | p = mdc->hash; |
4252 | 14.2k | for (; pktlen; pktlen--, p++) |
4253 | 13.6k | *p = iobuf_get_noeof (inp); |
4254 | | |
4255 | 961 | leave: |
4256 | 961 | return rc; |
4257 | 680 | } |
4258 | | |
4259 | | |
4260 | | /* Note that PKTLEN may be 0 to indicate partial length encoding. */ |
4261 | | static gpg_error_t |
4262 | | parse_encrypted_ocb (iobuf_t inp, int pkttype, unsigned long pktlen, |
4263 | | PACKET *pkt, int partial) |
4264 | 22.5k | { |
4265 | 22.5k | int rc = 0; |
4266 | 22.5k | PKT_encrypted *ed; |
4267 | 22.5k | unsigned long orig_pktlen = pktlen; |
4268 | | |
4269 | 22.5k | ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted); |
4270 | 22.5k | if (!ed) |
4271 | 0 | return gpg_error_from_syserror (); |
4272 | 22.5k | ed->len = 0; |
4273 | 22.5k | ed->extralen = 0; /* (only used in build_packet.) */ |
4274 | 22.5k | ed->buf = NULL; |
4275 | 22.5k | ed->new_ctb = 1; /* (packet number requires a new CTB anyway.) */ |
4276 | 22.5k | ed->is_partial = partial; |
4277 | 22.5k | ed->seipd = 0; |
4278 | 22.5k | ed->version = 0; |
4279 | 22.5k | ed->mdc_method = 0; |
4280 | | /* A basic sanity check. We need one version byte, one algo byte, |
4281 | | * one aead algo byte, one chunkbyte, at least 15 byte IV. */ |
4282 | 22.5k | if (orig_pktlen && pktlen < 19) |
4283 | 10 | { |
4284 | 10 | log_error ("packet(%d) too short\n", pkttype); |
4285 | 10 | if (list_mode) |
4286 | 10 | es_fputs (":ocb encrypted packet: [too short]\n", listfp); |
4287 | 10 | rc = gpg_error (GPG_ERR_INV_PACKET); |
4288 | 10 | iobuf_skip_rest (inp, pktlen, partial); |
4289 | 10 | goto leave; |
4290 | 10 | } |
4291 | | |
4292 | 22.5k | ed->version = iobuf_get_noeof (inp); |
4293 | 22.5k | if (orig_pktlen) |
4294 | 390 | pktlen--; |
4295 | 22.5k | if (ed->version != 1) |
4296 | 192 | { |
4297 | 192 | log_error ("ocb encrypted packet with unknown version %d\n", |
4298 | 192 | ed->version); |
4299 | 192 | if (list_mode) |
4300 | 192 | es_fputs (":ocb encrypted packet: [unknown version]\n", listfp); |
4301 | | /*skip_rest(inp, pktlen); should we really do this? */ |
4302 | 192 | rc = gpg_error (GPG_ERR_INV_PACKET); |
4303 | 192 | goto leave; |
4304 | 192 | } |
4305 | | |
4306 | 22.3k | ed->cipher_algo = iobuf_get_noeof (inp); |
4307 | 22.3k | if (orig_pktlen) |
4308 | 335 | pktlen--; |
4309 | 22.3k | ed->aead_algo = iobuf_get_noeof (inp); |
4310 | 22.3k | if (orig_pktlen) |
4311 | 335 | pktlen--; |
4312 | 22.3k | ed->chunkbyte = iobuf_get_noeof (inp); |
4313 | 22.3k | if (orig_pktlen) |
4314 | 335 | pktlen--; |
4315 | | |
4316 | | /* Store the remaining length of the encrypted data. We read the |
4317 | | * rest during decryption. */ |
4318 | 22.3k | ed->len = pktlen; |
4319 | | |
4320 | 22.3k | if (list_mode) |
4321 | 0 | { |
4322 | 0 | es_fprintf (listfp, ":ocb encrypted packet: cipher=%u aead=%u cb=%u\n", |
4323 | 0 | ed->cipher_algo, ed->aead_algo, ed->chunkbyte); |
4324 | 0 | if (orig_pktlen) |
4325 | 0 | es_fprintf (listfp, "\tlength: %lu\n", orig_pktlen); |
4326 | 0 | else |
4327 | 0 | es_fprintf (listfp, "\tlength: unknown\n"); |
4328 | 0 | } |
4329 | | |
4330 | 22.3k | ed->buf = inp; |
4331 | | |
4332 | 22.5k | leave: |
4333 | 22.5k | return rc; |
4334 | 22.3k | } |
4335 | | |
4336 | | |
4337 | | /* |
4338 | | * This packet is internally generated by us (in armor.c) to transfer |
4339 | | * some information to the lower layer. To make sure that this packet |
4340 | | * is really a GPG faked one and not one coming from outside, we |
4341 | | * first check that there is a unique tag in it. |
4342 | | * |
4343 | | * The format of such a control packet is: |
4344 | | * n byte session marker |
4345 | | * 1 byte control type CTRLPKT_xxxxx |
4346 | | * m byte control data |
4347 | | */ |
4348 | | static int |
4349 | | parse_gpg_control (IOBUF inp, int pkttype, unsigned long pktlen, |
4350 | | PACKET * packet, int partial) |
4351 | 4.29k | { |
4352 | 4.29k | byte *p; |
4353 | 4.29k | const byte *sesmark; |
4354 | 4.29k | size_t sesmarklen; |
4355 | 4.29k | int i; |
4356 | | |
4357 | 4.29k | (void) pkttype; |
4358 | | |
4359 | 4.29k | if (list_mode) |
4360 | 4.29k | es_fprintf (listfp, ":packet 63: length %lu ", pktlen); |
4361 | | |
4362 | 4.29k | sesmark = get_session_marker (&sesmarklen); |
4363 | 4.29k | if (pktlen < sesmarklen + 1) /* 1 is for the control bytes */ |
4364 | 279 | goto skipit; |
4365 | 64.1k | for (i = 0; i < sesmarklen; i++, pktlen--) |
4366 | 60.4k | { |
4367 | 60.4k | if (sesmark[i] != iobuf_get_noeof (inp)) |
4368 | 251 | goto skipit; |
4369 | 60.4k | } |
4370 | 3.76k | if (pktlen > 4096) |
4371 | 2 | goto skipit; /* Definitely too large. We skip it to avoid an |
4372 | | overflow in the malloc. */ |
4373 | 3.75k | if (list_mode) |
4374 | 3.75k | es_fputs ("- gpg control packet", listfp); |
4375 | | |
4376 | 3.75k | packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control |
4377 | 3.75k | + pktlen - 1); |
4378 | 3.75k | packet->pkt.gpg_control->control = iobuf_get_noeof (inp); |
4379 | 3.75k | pktlen--; |
4380 | 3.75k | packet->pkt.gpg_control->datalen = pktlen; |
4381 | 3.75k | p = packet->pkt.gpg_control->data; |
4382 | 13.0k | for (; pktlen; pktlen--, p++) |
4383 | 9.33k | *p = iobuf_get_noeof (inp); |
4384 | | |
4385 | 3.75k | return 0; |
4386 | | |
4387 | 532 | skipit: |
4388 | 532 | if (list_mode) |
4389 | 0 | { |
4390 | 0 | int c; |
4391 | |
|
4392 | 0 | i = 0; |
4393 | 0 | es_fprintf (listfp, "- private (rest length %lu)\n", pktlen); |
4394 | 0 | if (partial) |
4395 | 0 | { |
4396 | 0 | while ((c = iobuf_get (inp)) != -1) |
4397 | 0 | dump_hex_line (c, &i); |
4398 | 0 | } |
4399 | 0 | else |
4400 | 0 | { |
4401 | 0 | for (; pktlen; pktlen--) |
4402 | 0 | { |
4403 | 0 | dump_hex_line ((c = iobuf_get (inp)), &i); |
4404 | 0 | if (c == -1) |
4405 | 0 | break; |
4406 | 0 | } |
4407 | 0 | } |
4408 | 0 | es_putc ('\n', listfp); |
4409 | 0 | } |
4410 | 532 | iobuf_skip_rest (inp, pktlen, 0); |
4411 | 532 | return gpg_error (GPG_ERR_INV_PACKET); |
4412 | 3.76k | } |
4413 | | |
4414 | | |
4415 | | /* Create a GPG control packet to be used internally as a placeholder. */ |
4416 | | PACKET * |
4417 | | create_gpg_control (ctrlpkttype_t type, const byte * data, size_t datalen) |
4418 | 20.4k | { |
4419 | 20.4k | PACKET *packet; |
4420 | 20.4k | byte *p; |
4421 | | |
4422 | 20.4k | if (!data) |
4423 | 0 | datalen = 0; |
4424 | | |
4425 | 20.4k | packet = xmalloc (sizeof *packet); |
4426 | 20.4k | init_packet (packet); |
4427 | 20.4k | packet->pkttype = PKT_GPG_CONTROL; |
4428 | 20.4k | packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen); |
4429 | 20.4k | packet->pkt.gpg_control->control = type; |
4430 | 20.4k | packet->pkt.gpg_control->datalen = datalen; |
4431 | 20.4k | p = packet->pkt.gpg_control->data; |
4432 | 947k | for (; datalen; datalen--, p++) |
4433 | 927k | *p = *data++; |
4434 | | |
4435 | 20.4k | return packet; |
4436 | 20.4k | } |