/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 | 6.69k | #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 | 57.9M | { |
101 | 57.9M | unsigned short a; |
102 | 57.9M | a = (unsigned short)iobuf_get_noeof (inp) << 8; |
103 | 57.9M | a |= iobuf_get_noeof (inp); |
104 | 57.9M | return a; |
105 | 57.9M | } |
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 | 2.69M | { |
112 | 2.69M | unsigned long a; |
113 | 2.69M | a = (unsigned long)iobuf_get_noeof (inp) << 24; |
114 | 2.69M | a |= iobuf_get_noeof (inp) << 16; |
115 | 2.69M | a |= iobuf_get_noeof (inp) << 8; |
116 | 2.69M | a |= iobuf_get_noeof (inp); |
117 | 2.69M | return a; |
118 | 2.69M | } |
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 | 821k | { |
136 | 821k | int c, c1, c2, i; |
137 | 821k | unsigned int nmax = *ret_nread; |
138 | 821k | unsigned int nbits, nbytes; |
139 | 821k | size_t nread = 0; |
140 | 821k | gcry_mpi_t a = NULL; |
141 | 821k | byte *buf = NULL; |
142 | 821k | byte *p; |
143 | | |
144 | 821k | if (!nmax) |
145 | 1.31k | goto overflow; |
146 | | |
147 | 819k | if ((c = c1 = iobuf_get (inp)) == -1) |
148 | 882 | goto leave; |
149 | 818k | if (++nread == nmax) |
150 | 1.24k | goto overflow; |
151 | 817k | nbits = c << 8; |
152 | 817k | if ((c = c2 = iobuf_get (inp)) == -1) |
153 | 477 | goto leave; |
154 | 817k | ++nread; |
155 | 817k | nbits |= c; |
156 | 817k | if (nbits > MAX_EXTERN_MPI_BITS) |
157 | 4.29k | { |
158 | 4.29k | log_error ("mpi too large (%u bits)\n", nbits); |
159 | 4.29k | goto leave; |
160 | 4.29k | } |
161 | | |
162 | 812k | nbytes = (nbits + 7) / 8; |
163 | 812k | buf = secure ? gcry_xmalloc_secure (nbytes + 2) : gcry_xmalloc (nbytes + 2); |
164 | 812k | p = buf; |
165 | 812k | p[0] = c1; |
166 | 812k | p[1] = c2; |
167 | 21.6M | for (i = 0; i < nbytes; i++) |
168 | 20.8M | { |
169 | 20.8M | if (nread == nmax) |
170 | 5.15k | goto overflow; |
171 | | |
172 | 20.8M | c = iobuf_get (inp); |
173 | 20.8M | if (c == -1) |
174 | 979 | goto leave; |
175 | | |
176 | 20.8M | p[i + 2] = c; |
177 | 20.8M | nread ++; |
178 | 20.8M | } |
179 | | |
180 | 806k | if (gcry_mpi_scan (&a, GCRYMPI_FMT_PGP, buf, nread, &nread)) |
181 | 0 | a = NULL; |
182 | | |
183 | 806k | *ret_nread = nread; |
184 | 806k | gcry_free(buf); |
185 | 806k | return a; |
186 | | |
187 | 7.71k | overflow: |
188 | 7.71k | log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax); |
189 | 14.3k | leave: |
190 | 14.3k | *ret_nread = nread; |
191 | 14.3k | gcry_free(buf); |
192 | 14.3k | return a; |
193 | 7.71k | } |
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 | 821 | { |
213 | 821 | gpg_error_t err; |
214 | 821 | int c, i; |
215 | 821 | byte *buf = NULL; |
216 | 821 | byte *p; |
217 | | |
218 | 821 | *r_data = NULL; |
219 | | |
220 | 821 | if ((nbytes && nlength) |
221 | 821 | || (!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 | 821 | if (nlength) |
228 | 765 | { |
229 | 3.72k | for (i = 0; i < nlength; i++) |
230 | 2.97k | { |
231 | 2.97k | if (!*pktlen) |
232 | 3 | { |
233 | 3 | err = gpg_error (GPG_ERR_INV_PACKET); |
234 | 3 | goto leave; |
235 | 3 | } |
236 | 2.97k | c = iobuf_readbyte (inp); |
237 | 2.97k | if (c < 0) |
238 | 17 | { |
239 | 17 | err = gpg_error (GPG_ERR_INV_PACKET); |
240 | 17 | goto leave; |
241 | 17 | } |
242 | 2.95k | --*pktlen; |
243 | 2.95k | nbytes <<= 8; |
244 | 2.95k | nbytes |= c; |
245 | 2.95k | } |
246 | | |
247 | 745 | if (!nbytes) |
248 | 4 | { |
249 | 4 | err = gpg_error (GPG_ERR_INV_PACKET); |
250 | 4 | goto leave; |
251 | 4 | } |
252 | 745 | } |
253 | | |
254 | 797 | if (nbytes*8 > (nbytes==4? MAX_EXTERN_KEYPARM_BITS:MAX_EXTERN_MPI_BITS) |
255 | 312 | || (nbytes*8 < nbytes)) |
256 | 488 | { |
257 | 488 | log_error ("octet string too large (%u octets)\n", nbytes); |
258 | 488 | err = gpg_error (GPG_ERR_TOO_LARGE); |
259 | 488 | goto leave; |
260 | 488 | } |
261 | | |
262 | 309 | if (nbytes > *pktlen) |
263 | 133 | { |
264 | 133 | log_error ("octet string larger than packet (%u/%lu)\n", nbytes, *pktlen); |
265 | 133 | err = gpg_error (GPG_ERR_INV_PACKET); |
266 | 133 | goto leave; |
267 | 133 | } |
268 | | |
269 | 176 | buf = secure ? gcry_malloc_secure (nbytes) : gcry_malloc (nbytes); |
270 | 176 | if (!buf) |
271 | 0 | { |
272 | 0 | err = gpg_error_from_syserror (); |
273 | 0 | goto leave; |
274 | 0 | } |
275 | 176 | p = buf; |
276 | 6.49k | for (i = 0; i < nbytes; i++) |
277 | 6.38k | { |
278 | 6.38k | c = iobuf_get (inp); |
279 | 6.38k | if (c == -1) |
280 | 75 | { |
281 | 75 | err = gpg_error (GPG_ERR_INV_PACKET); |
282 | 75 | goto leave; |
283 | 75 | } |
284 | | |
285 | 6.31k | p[i] = c; |
286 | 6.31k | --*pktlen; |
287 | 6.31k | } |
288 | | |
289 | 101 | *r_data = gcry_mpi_set_opaque (NULL, buf, nbytes*8); |
290 | 101 | return 0; |
291 | | |
292 | 720 | leave: |
293 | 720 | gcry_free (buf); |
294 | 720 | return err; |
295 | 176 | } |
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 | 760 | { |
305 | 760 | gpg_error_t err; |
306 | | |
307 | 760 | err = read_raw_octet_string (inp, pktlen, nlength, nbytes, secure, r_data); |
308 | 760 | if (!err) |
309 | 91 | gcry_mpi_set_flag (*r_data, GCRYMPI_FLAG_USER2); |
310 | 760 | return err; |
311 | 760 | } |
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 | 57.9M | { |
328 | 57.9M | int c, c1, c2, i; |
329 | 57.9M | unsigned int nmax = *ret_nread; |
330 | 57.9M | unsigned int nbits, nbytes; |
331 | 57.9M | size_t nread = 0; |
332 | 57.9M | gcry_mpi_t a = NULL; |
333 | 57.9M | byte *buf = NULL; |
334 | 57.9M | byte *p; |
335 | | |
336 | 57.9M | if (!nmax) |
337 | 178 | goto overflow; |
338 | | |
339 | 57.9M | if ((c = c1 = iobuf_get (inp)) == -1) |
340 | 306 | goto leave; |
341 | 57.9M | if (++nread == nmax) |
342 | 110 | goto overflow; |
343 | 57.9M | nbits = c << 8; |
344 | 57.9M | if ((c = c2 = iobuf_get (inp)) == -1) |
345 | 171 | goto leave; |
346 | 57.9M | ++nread; |
347 | 57.9M | nbits |= c; |
348 | 57.9M | if (nbits > MAX_EXTERN_MPI_BITS) |
349 | 1.57k | { |
350 | 1.57k | log_error ("mpi too large (%u bits)\n", nbits); |
351 | 1.57k | goto leave; |
352 | 1.57k | } |
353 | | |
354 | 57.9M | nbytes = (nbits + 7) / 8; |
355 | 57.9M | buf = secure ? gcry_xmalloc_secure (nbytes) : gcry_xmalloc (nbytes); |
356 | 57.9M | p = buf; |
357 | 1.91G | for (i = 0; i < nbytes; i++) |
358 | 1.85G | { |
359 | 1.85G | if (nread == nmax) |
360 | 1.99k | goto overflow; |
361 | | |
362 | 1.85G | c = iobuf_get (inp); |
363 | 1.85G | if (c == -1) |
364 | 335 | goto leave; |
365 | | |
366 | 1.85G | p[i] = c; |
367 | 1.85G | nread ++; |
368 | 1.85G | } |
369 | | |
370 | 57.9M | a = gcry_mpi_set_opaque (NULL, buf, nbits); |
371 | 57.9M | gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2); |
372 | 57.9M | *ret_nread = nread; |
373 | 57.9M | return a; |
374 | | |
375 | 2.28k | overflow: |
376 | 2.28k | log_error ("mpi larger than indicated length (%u bits)\n", 8*nmax); |
377 | 4.67k | leave: |
378 | 4.67k | *ret_nread = nread; |
379 | 4.67k | gcry_free(buf); |
380 | 4.67k | return a; |
381 | 2.28k | } |
382 | | |
383 | | |
384 | | /* Register STRING as a known critical notation name. */ |
385 | | void |
386 | | register_known_notation (const char *string) |
387 | 935 | { |
388 | 935 | strlist_t sl; |
389 | | |
390 | 935 | if (!known_notations_list) |
391 | 4 | { |
392 | 4 | sl = add_to_strlist (&known_notations_list, |
393 | 4 | "preferred-email-encoding@pgp.com"); |
394 | 4 | sl->flags = 32; /* Length of the string. */ |
395 | 4 | } |
396 | 935 | if (!string) |
397 | 935 | 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 | 26.6k | { |
415 | 26.6k | int old = list_mode; |
416 | 26.6k | 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 | 26.6k | if (!listfp) |
436 | 3 | { |
437 | 3 | 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 | 3 | else |
444 | 3 | listfp = es_stderr; |
445 | | |
446 | 3 | if (DBG_MPI) |
447 | 0 | mpi_print_mode = 1; |
448 | 3 | } |
449 | 26.6k | return old; |
450 | 26.6k | } |
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 | 471k | { |
458 | 471k | static byte unknown_pubkey_algos[256]; |
459 | | |
460 | | /* First check whether the algorithm is usable but not suitable for |
461 | | encryption/signing. */ |
462 | 471k | if (pubkey_get_npkey (algo)) |
463 | 237k | { |
464 | 237k | 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 | 237k | } |
474 | 234k | else |
475 | 234k | { |
476 | 234k | algo &= 0xff; |
477 | 234k | if (!unknown_pubkey_algos[algo]) |
478 | 683 | { |
479 | 683 | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
480 | 683 | log_info (_("can't handle public key algorithm %d\n"), algo); |
481 | 683 | unknown_pubkey_algos[algo] = 1; |
482 | 683 | } |
483 | 234k | } |
484 | 471k | } |
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 | 33.1M | { |
492 | 33.1M | int skip, rc; |
493 | | |
494 | 33.1M | do |
495 | 33.1M | { |
496 | 33.1M | rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l); |
497 | 33.1M | } |
498 | 33.1M | while (skip && ! rc); |
499 | 33.1M | return rc; |
500 | 33.1M | } |
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 | 33.1M | { |
755 | 33.1M | int rc = 0; |
756 | 33.1M | iobuf_t inp; |
757 | 33.1M | int c, ctb, pkttype, lenbytes; |
758 | 33.1M | unsigned long pktlen; |
759 | 33.1M | byte hdr[8]; |
760 | 33.1M | int hdrlen; |
761 | 33.1M | int new_ctb = 0, partial = 0; |
762 | 33.1M | int with_uid = (onlykeypkts == 2); |
763 | 33.1M | off_t pos; |
764 | | |
765 | 33.1M | *skip = 0; |
766 | 33.1M | inp = ctx->inp; |
767 | | |
768 | 62.2M | again: |
769 | 62.2M | log_assert (!pkt->pkt.generic); |
770 | 62.2M | 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 | 62.2M | else |
777 | 62.2M | 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 | 62.2M | if ((ctb = iobuf_get (inp)) == -1) |
782 | 55.1k | { |
783 | 55.1k | rc = -1; |
784 | 55.1k | goto leave; |
785 | 55.1k | } |
786 | 62.1M | ctx->last_ctb = ctb; |
787 | 62.1M | hdrlen = 0; |
788 | 62.1M | hdr[hdrlen++] = ctb; |
789 | | |
790 | 62.1M | if (!(ctb & 0x80)) |
791 | 237k | { |
792 | 237k | log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp), ctb); |
793 | 237k | rc = gpg_error (GPG_ERR_INV_PACKET); |
794 | 237k | goto leave; |
795 | 237k | } |
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 | 61.9M | pktlen = 0; |
805 | 61.9M | new_ctb = !!(ctb & 0x40); |
806 | 61.9M | if (new_ctb) |
807 | 1.10M | pkttype = ctb & 0x3f; |
808 | 60.8M | else |
809 | 60.8M | pkttype = (ctb >> 2) & 0xf; |
810 | | |
811 | 61.9M | 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 | 61.9M | if (new_ctb) |
819 | 1.10M | { |
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 | 1.10M | if ((c = iobuf_get (inp)) == -1) |
825 | 1.38k | { |
826 | 1.38k | log_error ("%s: 1st length byte missing\n", iobuf_where (inp)); |
827 | 1.38k | rc = gpg_error (GPG_ERR_INV_PACKET); |
828 | 1.38k | goto leave; |
829 | 1.38k | } |
830 | | |
831 | | |
832 | 1.10M | hdr[hdrlen++] = c; |
833 | 1.10M | if (c < 192) |
834 | 854k | pktlen = c; |
835 | 252k | else if (c < 224) |
836 | 53.7k | { |
837 | 53.7k | pktlen = (c - 192) * 256; |
838 | 53.7k | if ((c = iobuf_get (inp)) == -1) |
839 | 609 | { |
840 | 609 | log_error ("%s: 2nd length byte missing\n", |
841 | 609 | iobuf_where (inp)); |
842 | 609 | rc = gpg_error (GPG_ERR_INV_PACKET); |
843 | 609 | goto leave; |
844 | 609 | } |
845 | 53.1k | hdr[hdrlen++] = c; |
846 | 53.1k | pktlen += c + 192; |
847 | 53.1k | } |
848 | 198k | else if (c == 255) |
849 | 4.74k | { |
850 | 4.74k | int i; |
851 | 4.74k | char value[4]; |
852 | | |
853 | 23.0k | for (i = 0; i < 4; i ++) |
854 | 18.7k | { |
855 | 18.7k | if ((c = iobuf_get (inp)) == -1) |
856 | 456 | { |
857 | 456 | log_error ("%s: 4 byte length invalid\n", iobuf_where (inp)); |
858 | 456 | rc = gpg_error (GPG_ERR_INV_PACKET); |
859 | 456 | goto leave; |
860 | 456 | } |
861 | 18.3k | value[i] = hdr[hdrlen++] = c; |
862 | 18.3k | } |
863 | | |
864 | 4.28k | pktlen = buf32_to_ulong (value); |
865 | 4.28k | } |
866 | 193k | else /* Partial body length. */ |
867 | 193k | { |
868 | 193k | switch (pkttype) |
869 | 193k | { |
870 | 13.2k | case PKT_PLAINTEXT: |
871 | 13.9k | case PKT_ENCRYPTED: |
872 | 19.5k | case PKT_ENCRYPTED_MDC: |
873 | 22.0k | case PKT_ENCRYPTED_OCB: |
874 | 190k | case PKT_COMPRESSED: |
875 | 190k | iobuf_set_partial_body_length_mode (inp, c & 0xff); |
876 | 190k | pktlen = 0; /* To indicate partial length. */ |
877 | 190k | partial = 1; |
878 | 190k | break; |
879 | | |
880 | 3.36k | default: |
881 | 3.36k | log_error ("%s: partial length invalid for" |
882 | 3.36k | " packet type %d\n", iobuf_where (inp), pkttype); |
883 | 3.36k | rc = gpg_error (GPG_ERR_INV_PACKET); |
884 | 3.36k | goto leave; |
885 | 193k | } |
886 | 193k | } |
887 | | |
888 | 1.10M | } |
889 | 60.8M | else /* This is an old format packet. */ |
890 | 60.8M | { |
891 | | /* The type of length encoding is encoded in bits 0-1 of the |
892 | | tag. */ |
893 | 60.8M | lenbytes = ((ctb & 3) == 3) ? 0 : (1 << (ctb & 3)); |
894 | 60.8M | if (!lenbytes) |
895 | 287k | { |
896 | 287k | 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 | 287k | partial = 1; |
900 | 287k | if (pkttype != PKT_ENCRYPTED && pkttype != PKT_PLAINTEXT |
901 | 286k | && pkttype != PKT_COMPRESSED) |
902 | 2.86k | { |
903 | 2.86k | log_error ("%s: indeterminate length for invalid" |
904 | 2.86k | " packet type %d\n", iobuf_where (inp), pkttype); |
905 | 2.86k | rc = gpg_error (GPG_ERR_INV_PACKET); |
906 | 2.86k | goto leave; |
907 | 2.86k | } |
908 | 287k | } |
909 | 60.5M | else |
910 | 60.5M | { |
911 | 121M | for (; lenbytes; lenbytes--) |
912 | 60.6M | { |
913 | 60.6M | pktlen <<= 8; |
914 | 60.6M | c = iobuf_get (inp); |
915 | 60.6M | if (c == -1) |
916 | 3.55k | { |
917 | 3.55k | log_error ("%s: length invalid\n", iobuf_where (inp)); |
918 | 3.55k | rc = gpg_error (GPG_ERR_INV_PACKET); |
919 | 3.55k | goto leave; |
920 | 3.55k | } |
921 | 60.6M | pktlen |= hdr[hdrlen++] = c; |
922 | 60.6M | } |
923 | 60.5M | } |
924 | 60.8M | } |
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 | 61.9M | 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 | 61.9M | 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 | 61.9M | 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 | 61.9M | else if (do_skip |
966 | | /* type==0 is not allowed. This is an invalid packet. */ |
967 | 61.9M | || !pkttype |
968 | | /* When ONLYKEYPKTS is set, we don't skip keys. */ |
969 | 61.8M | || (onlykeypkts && pkttype != PKT_PUBLIC_SUBKEY |
970 | 0 | && pkttype != PKT_PUBLIC_KEY |
971 | 0 | && pkttype != PKT_SECRET_SUBKEY && pkttype != PKT_SECRET_KEY)) |
972 | 17.2k | { |
973 | 17.2k | iobuf_skip_rest (inp, pktlen, partial); |
974 | 17.2k | *skip = 1; |
975 | 17.2k | rc = 0; |
976 | 17.2k | goto leave; |
977 | 17.2k | } |
978 | | |
979 | 61.8M | 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 | 61.8M | if (list_mode) |
993 | 61.8M | 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 | 61.8M | ctx->n_parsed_packets++; |
1000 | | |
1001 | 61.8M | pkt->pkttype = pkttype; |
1002 | 61.8M | rc = GPG_ERR_UNKNOWN_PACKET; /* default error */ |
1003 | 61.8M | switch (pkttype) |
1004 | 61.8M | { |
1005 | 91.6k | case PKT_PUBLIC_KEY: |
1006 | 1.96M | case PKT_PUBLIC_SUBKEY: |
1007 | 1.99M | case PKT_SECRET_KEY: |
1008 | 2.05M | case PKT_SECRET_SUBKEY: |
1009 | 2.05M | pkt->pkt.public_key = xmalloc_clear (sizeof *pkt->pkt.public_key); |
1010 | 2.05M | rc = parse_key (inp, pkttype, pktlen, hdr, hdrlen, pkt); |
1011 | 2.05M | break; |
1012 | 112k | case PKT_SYMKEY_ENC: |
1013 | 112k | rc = parse_symkeyenc (inp, pkttype, pktlen, pkt); |
1014 | 112k | break; |
1015 | 55.3k | case PKT_PUBKEY_ENC: |
1016 | 55.3k | rc = parse_pubkeyenc (inp, pkttype, pktlen, pkt); |
1017 | 55.3k | break; |
1018 | 29.4M | case PKT_SIGNATURE: |
1019 | 29.4M | pkt->pkt.signature = xmalloc_clear (sizeof *pkt->pkt.signature); |
1020 | 29.4M | rc = parse_signature (inp, pkttype, pktlen, pkt->pkt.signature); |
1021 | 29.4M | break; |
1022 | 39.5k | case PKT_ONEPASS_SIG: |
1023 | 39.5k | pkt->pkt.onepass_sig = xmalloc_clear (sizeof *pkt->pkt.onepass_sig); |
1024 | 39.5k | rc = parse_onepass_sig (inp, pkttype, pktlen, pkt->pkt.onepass_sig); |
1025 | 39.5k | break; |
1026 | 166k | case PKT_USER_ID: |
1027 | 166k | rc = parse_user_id (inp, pkttype, pktlen, pkt); |
1028 | 166k | break; |
1029 | 37.7k | case PKT_ATTRIBUTE: |
1030 | 37.7k | pkt->pkttype = pkttype = PKT_USER_ID; /* we store it in the userID */ |
1031 | 37.7k | rc = parse_attribute (inp, pkttype, pktlen, pkt); |
1032 | 37.7k | break; |
1033 | 12.6k | case PKT_OLD_COMMENT: |
1034 | 14.4k | case PKT_COMMENT: |
1035 | 14.4k | rc = parse_comment (inp, pkttype, pktlen, pkt); |
1036 | 14.4k | break; |
1037 | 29.0M | case PKT_RING_TRUST: |
1038 | 29.0M | { |
1039 | 29.0M | rc = parse_ring_trust (ctx, pktlen); |
1040 | 29.0M | if (!rc) |
1041 | 29.0M | goto again; /* Directly read the next packet. */ |
1042 | 29.0M | } |
1043 | 0 | break; |
1044 | 134k | case PKT_PLAINTEXT: |
1045 | 134k | rc = parse_plaintext (inp, pkttype, pktlen, pkt, new_ctb, partial); |
1046 | 134k | break; |
1047 | 482k | case PKT_COMPRESSED: |
1048 | 482k | rc = parse_compressed (inp, pkttype, pktlen, pkt, new_ctb); |
1049 | 482k | break; |
1050 | 44.8k | case PKT_ENCRYPTED: |
1051 | 54.7k | case PKT_ENCRYPTED_MDC: |
1052 | 54.7k | rc = parse_encrypted (inp, pkttype, pktlen, pkt, new_ctb, partial); |
1053 | 54.7k | break; |
1054 | 11.8k | case PKT_MDC: |
1055 | 11.8k | rc = parse_mdc (inp, pkttype, pktlen, pkt, new_ctb); |
1056 | 11.8k | break; |
1057 | 125k | case PKT_ENCRYPTED_OCB: |
1058 | 125k | rc = parse_encrypted_ocb (inp, pkttype, pktlen, pkt, partial); |
1059 | 125k | break; |
1060 | 20.5k | case PKT_GPG_CONTROL: |
1061 | 20.5k | rc = parse_gpg_control (inp, pkttype, pktlen, pkt, partial); |
1062 | 20.5k | break; |
1063 | 13.5k | case PKT_MARKER: |
1064 | 13.5k | rc = parse_marker (inp, pkttype, pktlen); |
1065 | 13.5k | break; |
1066 | 77.9k | default: |
1067 | | /* Unknown packet. Skip it. */ |
1068 | 77.9k | skip_packet (inp, pkttype, pktlen, partial); |
1069 | 77.9k | break; |
1070 | 61.8M | } |
1071 | | |
1072 | 32.8M | 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 | 32.8M | free_packet (NULL, ctx); |
1085 | 32.8M | if (!rc && (pkttype == PKT_PUBLIC_KEY |
1086 | 32.4M | || pkttype == PKT_SECRET_KEY |
1087 | 32.4M | || pkttype == PKT_USER_ID |
1088 | 32.2M | || pkttype == PKT_ATTRIBUTE |
1089 | 32.2M | || pkttype == PKT_SIGNATURE)) |
1090 | 29.6M | { |
1091 | 29.6M | ctx->last_pkt = *pkt; |
1092 | 29.6M | } |
1093 | | |
1094 | 33.1M | leave: |
1095 | | /* FIXME: We leak in case of an error (see the xmalloc's above). */ |
1096 | 33.1M | if (!rc && iobuf_error (inp)) |
1097 | 8.72k | 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 | 33.1M | return rc == -1? -1 : gpg_err_code (rc); |
1103 | 32.8M | } |
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 | 77.9k | { |
1182 | 77.9k | 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 | 77.9k | iobuf_skip_rest (inp, pktlen, partial); |
1209 | 77.9k | } |
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 | 445k | { |
1219 | 445k | int c; |
1220 | 445k | byte *buf, *p; |
1221 | | |
1222 | 445k | buf = xtrymalloc (pktlen); |
1223 | 445k | if (!buf) |
1224 | 301k | { |
1225 | 301k | gpg_error_t err = gpg_error_from_syserror (); |
1226 | 301k | log_error ("error reading rest of packet: %s\n", gpg_strerror (err)); |
1227 | 301k | return NULL; |
1228 | 301k | } |
1229 | 1.96M | for (p = buf; pktlen; pktlen--) |
1230 | 1.83M | { |
1231 | 1.83M | c = iobuf_get (inp); |
1232 | 1.83M | if (c == -1) |
1233 | 14.9k | { |
1234 | 14.9k | log_error ("premature eof while reading rest of packet\n"); |
1235 | 14.9k | xfree (buf); |
1236 | 14.9k | return NULL; |
1237 | 14.9k | } |
1238 | 1.81M | *p++ = c; |
1239 | 1.81M | } |
1240 | | |
1241 | 129k | return buf; |
1242 | 143k | } |
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 | 3.76M | { |
1253 | 3.76M | char buffer[256]; |
1254 | 3.76M | char *tmpbuf; |
1255 | 3.76M | int i, c, nbytes; |
1256 | | |
1257 | 3.76M | *r_data = NULL; |
1258 | | |
1259 | 3.76M | if (!*pktlen) |
1260 | 968 | return gpg_error (GPG_ERR_INV_PACKET); |
1261 | 3.76M | c = iobuf_readbyte (inp); |
1262 | 3.76M | if (c < 0) |
1263 | 135 | return gpg_error (GPG_ERR_INV_PACKET); |
1264 | 3.76M | --*pktlen; |
1265 | 3.76M | nbytes = c; |
1266 | 3.76M | if (nbytes < 2 || nbytes > 254) |
1267 | 1.35k | return gpg_error (GPG_ERR_INV_PACKET); |
1268 | 3.75M | if (nbytes > *pktlen) |
1269 | 892 | return gpg_error (GPG_ERR_INV_PACKET); |
1270 | | |
1271 | 3.75M | buffer[0] = nbytes; |
1272 | | |
1273 | 28.1M | for (i = 0; i < nbytes; i++) |
1274 | 24.4M | { |
1275 | 24.4M | c = iobuf_get (inp); |
1276 | 24.4M | if (c < 0) |
1277 | 208 | return gpg_error (GPG_ERR_INV_PACKET); |
1278 | 24.4M | --*pktlen; |
1279 | 24.4M | buffer[1+i] = c; |
1280 | 24.4M | } |
1281 | | |
1282 | 3.75M | tmpbuf = xtrymalloc (1 + nbytes); |
1283 | 3.75M | if (!tmpbuf) |
1284 | 0 | return gpg_error_from_syserror (); |
1285 | 3.75M | memcpy (tmpbuf, buffer, 1 + nbytes); |
1286 | 3.75M | *r_data = gcry_mpi_set_opaque (NULL, tmpbuf, 8 * (1 + nbytes)); |
1287 | 3.75M | if (!*r_data) |
1288 | 0 | { |
1289 | 0 | xfree (tmpbuf); |
1290 | 0 | return gpg_error_from_syserror (); |
1291 | 0 | } |
1292 | 3.75M | return 0; |
1293 | 3.75M | } |
1294 | | |
1295 | | |
1296 | | /* Parse a marker packet. */ |
1297 | | static int |
1298 | | parse_marker (IOBUF inp, int pkttype, unsigned long pktlen) |
1299 | 13.5k | { |
1300 | 13.5k | (void) pkttype; |
1301 | | |
1302 | 13.5k | if (pktlen != 3) |
1303 | 7.63k | goto fail; |
1304 | | |
1305 | 5.94k | if (iobuf_get (inp) != 'P') |
1306 | 2.03k | { |
1307 | 2.03k | pktlen--; |
1308 | 2.03k | goto fail; |
1309 | 2.03k | } |
1310 | | |
1311 | 3.91k | if (iobuf_get (inp) != 'G') |
1312 | 186 | { |
1313 | 186 | pktlen--; |
1314 | 186 | goto fail; |
1315 | 186 | } |
1316 | | |
1317 | 3.72k | if (iobuf_get (inp) != 'P') |
1318 | 984 | { |
1319 | 984 | pktlen--; |
1320 | 984 | goto fail; |
1321 | 984 | } |
1322 | | |
1323 | 2.74k | if (list_mode) |
1324 | 2.74k | es_fputs (":marker packet: PGP\n", listfp); |
1325 | | |
1326 | 2.74k | return 0; |
1327 | | |
1328 | 10.8k | fail: |
1329 | 10.8k | log_error ("invalid marker packet\n"); |
1330 | 10.8k | if (list_mode) |
1331 | 10.8k | es_fputs (":marker packet: [invalid]\n", listfp); |
1332 | 10.8k | iobuf_skip_rest (inp, pktlen, 0); |
1333 | 10.8k | return GPG_ERR_INV_PACKET; |
1334 | 3.72k | } |
1335 | | |
1336 | | |
1337 | | static int |
1338 | | parse_symkeyenc (IOBUF inp, int pkttype, unsigned long pktlen, |
1339 | | PACKET * packet) |
1340 | 112k | { |
1341 | 112k | PKT_symkey_enc *k; |
1342 | 112k | int rc = 0; |
1343 | 112k | int i, version, s2kmode, cipher_algo, aead_algo, hash_algo, seskeylen, minlen; |
1344 | | |
1345 | 112k | if (pktlen < 4) |
1346 | 14.1k | goto too_short; |
1347 | 98.6k | version = iobuf_get_noeof (inp); |
1348 | 98.6k | pktlen--; |
1349 | 98.6k | if (version == 4) |
1350 | 57.9k | ; |
1351 | 40.6k | else if (version == 5) |
1352 | 35.9k | ; |
1353 | 4.72k | else |
1354 | 4.72k | { |
1355 | 4.72k | log_error ("packet(%d) with unknown version %d\n", pkttype, version); |
1356 | 4.72k | if (list_mode) |
1357 | 4.72k | es_fprintf (listfp, ":symkey enc packet: [unknown version]\n"); |
1358 | 4.72k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1359 | 4.72k | goto leave; |
1360 | 4.72k | } |
1361 | 93.8k | if (pktlen > 200) |
1362 | 24.9k | { /* (we encode the seskeylen in a byte) */ |
1363 | 24.9k | log_error ("packet(%d) too large\n", pkttype); |
1364 | 24.9k | if (list_mode) |
1365 | 24.9k | es_fprintf (listfp, ":symkey enc packet: [too large]\n"); |
1366 | 24.9k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1367 | 24.9k | goto leave; |
1368 | 24.9k | } |
1369 | 68.8k | cipher_algo = iobuf_get_noeof (inp); |
1370 | 68.8k | pktlen--; |
1371 | 68.8k | if (version == 5) |
1372 | 11.1k | { |
1373 | 11.1k | aead_algo = iobuf_get_noeof (inp); |
1374 | 11.1k | pktlen--; |
1375 | 11.1k | } |
1376 | 57.7k | else |
1377 | 57.7k | aead_algo = 0; |
1378 | 68.8k | if (pktlen < 2) |
1379 | 1.51k | goto too_short; |
1380 | 67.3k | s2kmode = iobuf_get_noeof (inp); |
1381 | 67.3k | pktlen--; |
1382 | 67.3k | hash_algo = iobuf_get_noeof (inp); |
1383 | 67.3k | pktlen--; |
1384 | 67.3k | switch (s2kmode) |
1385 | 67.3k | { |
1386 | 35.6k | case 0: /* Simple S2K. */ |
1387 | 35.6k | minlen = 0; |
1388 | 35.6k | break; |
1389 | 2.37k | case 1: /* Salted S2K. */ |
1390 | 2.37k | minlen = 8; |
1391 | 2.37k | break; |
1392 | 8.17k | case 3: /* Iterated+salted S2K. */ |
1393 | 8.17k | minlen = 9; |
1394 | 8.17k | break; |
1395 | 21.1k | default: |
1396 | 21.1k | log_error ("unknown S2K mode %d\n", s2kmode); |
1397 | 21.1k | if (list_mode) |
1398 | 21.1k | es_fprintf (listfp, ":symkey enc packet: [unknown S2K mode]\n"); |
1399 | 21.1k | goto leave; |
1400 | 67.3k | } |
1401 | 46.2k | if (minlen > pktlen) |
1402 | 876 | { |
1403 | 876 | log_error ("packet with S2K %d too short\n", s2kmode); |
1404 | 876 | if (list_mode) |
1405 | 876 | es_fprintf (listfp, ":symkey enc packet: [too short]\n"); |
1406 | 876 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1407 | 876 | goto leave; |
1408 | 876 | } |
1409 | 45.3k | seskeylen = pktlen - minlen; |
1410 | 45.3k | k = packet->pkt.symkey_enc = xmalloc_clear (sizeof *packet->pkt.symkey_enc); |
1411 | 45.3k | k->version = version; |
1412 | 45.3k | k->cipher_algo = cipher_algo; |
1413 | 45.3k | k->aead_algo = aead_algo; |
1414 | 45.3k | k->s2k.mode = s2kmode; |
1415 | 45.3k | k->s2k.hash_algo = hash_algo; |
1416 | 45.3k | if (s2kmode == 1 || s2kmode == 3) |
1417 | 9.67k | { |
1418 | 87.0k | for (i = 0; i < 8 && pktlen; i++, pktlen--) |
1419 | 77.3k | k->s2k.salt[i] = iobuf_get_noeof (inp); |
1420 | 9.67k | } |
1421 | 45.3k | if (s2kmode == 3) |
1422 | 7.78k | { |
1423 | 7.78k | k->s2k.count = iobuf_get_noeof (inp); |
1424 | 7.78k | pktlen--; |
1425 | 7.78k | } |
1426 | 45.3k | k->seskeylen = seskeylen; |
1427 | 45.3k | if (k->seskeylen) |
1428 | 9.34k | { |
1429 | 9.34k | k->seskey = xcalloc (1, seskeylen); |
1430 | 103k | for (i = 0; i < seskeylen && pktlen; i++, pktlen--) |
1431 | 94.1k | 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 | 9.34k | if (s2kmode != 1 && s2kmode != 3) |
1437 | 9.34k | log_info (_("WARNING: potentially insecure symmetrically" |
1438 | 5.88k | " encrypted session key\n")); |
1439 | 9.34k | } |
1440 | 45.3k | log_assert (!pktlen); |
1441 | | |
1442 | 45.3k | 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 | 112k | leave: |
1473 | 112k | iobuf_skip_rest (inp, pktlen, 0); |
1474 | 112k | return rc; |
1475 | | |
1476 | 15.6k | too_short: |
1477 | 15.6k | log_error ("packet(%d) too short\n", pkttype); |
1478 | 15.6k | if (list_mode) |
1479 | 15.6k | es_fprintf (listfp, ":symkey enc packet: [too short]\n"); |
1480 | 15.6k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1481 | 15.6k | goto leave; |
1482 | 45.3k | } |
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 | 55.3k | { |
1490 | 55.3k | int rc = 0; |
1491 | 55.3k | int is_v6 = 0; |
1492 | 55.3k | int i, ndata; |
1493 | 55.3k | unsigned int n; |
1494 | 55.3k | PKT_pubkey_enc *k; |
1495 | | |
1496 | 55.3k | k = packet->pkt.pubkey_enc = xmalloc_clear (sizeof *packet->pkt.pubkey_enc); |
1497 | 55.3k | if (pktlen < 12) |
1498 | 6.74k | { |
1499 | 6.74k | log_error ("packet(%d) too short\n", pkttype); |
1500 | 6.74k | if (list_mode) |
1501 | 6.74k | es_fputs (":pubkey enc packet: [too short]\n", listfp); |
1502 | 6.74k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1503 | 6.74k | goto leave; |
1504 | 6.74k | } |
1505 | 48.5k | k->version = iobuf_get_noeof (inp); |
1506 | 48.5k | pktlen--; |
1507 | 48.5k | if (k->version == 2 || k->version == 3) |
1508 | 46.9k | ; |
1509 | 1.60k | else if (RFC9980 && k->version == 6) |
1510 | 9 | is_v6 = 1; |
1511 | 1.59k | else |
1512 | 1.59k | { |
1513 | 1.59k | log_error ("packet(%d) with unknown version %d\n", pkttype, k->version); |
1514 | 1.59k | if (list_mode) |
1515 | 1.59k | es_fputs (":pubkey enc packet: [unknown version]\n", listfp); |
1516 | 1.59k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1517 | 1.59k | goto leave; |
1518 | 1.59k | } |
1519 | | |
1520 | 46.9k | if (is_v6) |
1521 | 9 | { |
1522 | 9 | int keyver; |
1523 | | |
1524 | 9 | k->fprlen = iobuf_get_noeof (inp); /* Actually the 1 + fprlen. */ |
1525 | 9 | pktlen--; |
1526 | 9 | if (k->fprlen) |
1527 | 9 | { |
1528 | 9 | keyver = iobuf_get_noeof (inp); |
1529 | 9 | pktlen--; k->fprlen--; |
1530 | | /* fprlen has now the correct value. */ |
1531 | 9 | if (pktlen < k->fprlen || k->fprlen > MAX_FINGERPRINT_LEN) |
1532 | 7 | { |
1533 | 7 | log_error ("packet(%d) too short for fingerprint (%d)\n", |
1534 | 7 | pkttype, k->fprlen); |
1535 | 7 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1536 | 7 | goto leave; |
1537 | 7 | } |
1538 | 2 | if (iobuf_read (inp, k->fpr, k->fprlen) != k->fprlen) |
1539 | 0 | { |
1540 | 0 | log_error ("premature eof while reading " |
1541 | 0 | "fingerprint from packet(%d)\n", pkttype); |
1542 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1543 | 0 | goto leave; |
1544 | 0 | } |
1545 | 2 | pktlen -= k->fprlen; |
1546 | 2 | 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 | 2 | 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 | 2 | else |
1557 | 2 | { |
1558 | 2 | log_error ("packet(%d) inconsistent fingerprint (v=%d,n=%d)\n", |
1559 | 2 | pkttype, keyver, k->fprlen); |
1560 | 2 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1561 | 2 | goto leave; |
1562 | 2 | } |
1563 | 2 | } |
1564 | 9 | } |
1565 | 46.9k | else |
1566 | 46.9k | { |
1567 | 46.9k | k->keyid[0] = read_32 (inp); |
1568 | 46.9k | pktlen -= 4; |
1569 | 46.9k | k->keyid[1] = read_32 (inp); |
1570 | 46.9k | pktlen -= 4; |
1571 | 46.9k | } |
1572 | | |
1573 | 46.9k | 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 | 46.9k | k->pubkey_algo = iobuf_get_noeof (inp); |
1580 | 46.9k | pktlen--; |
1581 | 46.9k | k->throw_keyid = 0; /* Only used as flag for build_packet. */ |
1582 | 46.9k | if (list_mode) |
1583 | 46.9k | 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 | 46.9k | ndata = pubkey_get_nenc (k->pubkey_algo); |
1589 | 46.9k | if (!ndata) |
1590 | 37.3k | { |
1591 | 37.3k | if (list_mode) |
1592 | 37.3k | es_fprintf (listfp, "\tunsupported algorithm %d\n", k->pubkey_algo); |
1593 | 37.3k | unknown_pubkey_warning (k->pubkey_algo); |
1594 | 37.3k | k->data[0] = NULL; /* No need to store the encrypted data. */ |
1595 | 37.3k | } |
1596 | 9.62k | else if (k->pubkey_algo == PUBKEY_ALGO_ECDH) |
1597 | 5.78k | { |
1598 | 5.78k | log_assert (ndata == 2); |
1599 | | /* Get the ephemeral public key. */ |
1600 | 5.78k | n = pktlen; |
1601 | 5.78k | k->data[0] = sos_read (inp, &n, 0); |
1602 | 5.78k | pktlen -= n; |
1603 | 5.78k | if (!k->data[0]) |
1604 | 2.23k | { |
1605 | 2.23k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1606 | 2.23k | goto leave; |
1607 | 2.23k | } |
1608 | | /* Get the wrapped symmetric key. */ |
1609 | 3.55k | rc = read_sized_octet_string (inp, &pktlen, k->data + 1); |
1610 | 3.55k | if (rc) |
1611 | 3.06k | goto leave; |
1612 | 3.55k | } |
1613 | 3.84k | else if (k->pubkey_algo == PUBKEY_ALGO_X25519 && RFC9980) |
1614 | 1 | { |
1615 | 1 | log_assert (ndata == 2); |
1616 | | /* Get the ephemeral public key. */ |
1617 | 1 | rc = read_raw_octet_string (inp, &pktlen, 0, 32, 0, k->data + 0); |
1618 | 1 | if (rc) |
1619 | 1 | goto leave; |
1620 | 0 | if (pktlen < 2) |
1621 | 0 | { |
1622 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1623 | 0 | goto leave; |
1624 | 0 | } |
1625 | 0 | n = iobuf_get_noeof (inp); |
1626 | 0 | pktlen--; |
1627 | 0 | if (list_mode) |
1628 | 0 | es_fprintf (listfp, "\tlength octet: %u\n", n); |
1629 | 0 | if (!is_v6) |
1630 | 0 | { |
1631 | 0 | k->seskey_algo = iobuf_get_noeof (inp); |
1632 | 0 | if (!n || !pktlen) |
1633 | 0 | { |
1634 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1635 | 0 | goto leave; |
1636 | 0 | } |
1637 | 0 | pktlen--; |
1638 | 0 | n--; |
1639 | 0 | } |
1640 | | /* Get the encrypted symmetric key (keylen+8 due to AESWRAP). */ |
1641 | 0 | rc = read_raw_octet_string (inp, &pktlen, 0, n, 0, k->data + 1); |
1642 | 0 | if (rc) |
1643 | 0 | goto leave; |
1644 | 0 | } |
1645 | 3.83k | else if (k->pubkey_algo == PUBKEY_ALGO_KYBER) |
1646 | 127 | { |
1647 | 127 | log_assert (ndata == 3); |
1648 | | /* Get the ephemeral public key. */ |
1649 | 127 | n = pktlen; |
1650 | 127 | k->data[0] = sos_read (inp, &n, 0); |
1651 | 127 | pktlen -= n; |
1652 | 127 | if (!k->data[0]) |
1653 | 25 | { |
1654 | 25 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1655 | 25 | goto leave; |
1656 | 25 | } |
1657 | | /* Get the Kyber ciphertext. */ |
1658 | 102 | rc = read_sos_octet_string (inp, &pktlen, 4, 0, 0, k->data + 1); |
1659 | 102 | if (rc) |
1660 | 88 | goto leave; |
1661 | | /* Get the algorithm id for the session key. */ |
1662 | 14 | if (!pktlen) |
1663 | 0 | { |
1664 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1665 | 0 | goto leave; |
1666 | 0 | } |
1667 | 14 | k->seskey_algo = iobuf_get_noeof (inp); |
1668 | 14 | pktlen--; |
1669 | | /* Get the encrypted symmetric key. */ |
1670 | 14 | rc = read_sos_octet_string (inp, &pktlen, 1, 0, 0, k->data + 2); |
1671 | 14 | if (rc) |
1672 | 12 | goto leave; |
1673 | 14 | } |
1674 | 3.71k | else if (IS_PUBKEY_ALGO_MLK (k->pubkey_algo)) |
1675 | 6 | { |
1676 | 6 | log_assert (ndata == 3); |
1677 | | /* Get the ephemeral public key. */ |
1678 | 6 | rc = read_raw_octet_string |
1679 | 6 | (inp, &pktlen, 0, |
1680 | 6 | (k->pubkey_algo == PUBKEY_ALGO_MLK768_25519? 32 : |
1681 | 6 | k->pubkey_algo == PUBKEY_ALGO_MLK768_NP384? 97 : |
1682 | 5 | k->pubkey_algo == PUBKEY_ALGO_MLK768_BP384? 97 : |
1683 | 5 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_448? 56 : |
1684 | 2 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_NP521? 133 : |
1685 | 1 | k->pubkey_algo == PUBKEY_ALGO_MLK1024_BP512? 129 : 0), |
1686 | 6 | 0, k->data + 0); |
1687 | 6 | if (rc) |
1688 | 6 | goto leave; |
1689 | | |
1690 | | /* Get the Kyber ciphertext. */ |
1691 | 0 | rc = read_raw_octet_string (inp, &pktlen, 0, |
1692 | 0 | (IS_PUBKEY_ALGO_MLK768 (k->pubkey_algo)? |
1693 | 0 | 1088 : 1568), |
1694 | 0 | 0, k->data + 1); |
1695 | 0 | if (rc) |
1696 | 0 | goto leave; |
1697 | | /* Get the size octet and algo id for the session key. */ |
1698 | 0 | if (pktlen < 2) |
1699 | 0 | { |
1700 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1701 | 0 | goto leave; |
1702 | 0 | } |
1703 | 0 | n = iobuf_get_noeof (inp); |
1704 | 0 | pktlen--; |
1705 | 0 | if (list_mode) |
1706 | 0 | es_fprintf (listfp, "\tlength octet: %u\n", n); |
1707 | 0 | if (!is_v6) |
1708 | 0 | { |
1709 | 0 | k->seskey_algo = iobuf_get_noeof (inp); |
1710 | 0 | if (!n || !pktlen) |
1711 | 0 | { |
1712 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
1713 | 0 | goto leave; |
1714 | 0 | } |
1715 | 0 | pktlen--; |
1716 | 0 | n--; |
1717 | 0 | } |
1718 | | /* Get the encrypted symmetric key (keylen+8 due to AESWRAP). */ |
1719 | 0 | rc = read_raw_octet_string (inp, &pktlen, 0, n, 0, k->data + 2); |
1720 | 0 | if (rc) |
1721 | 0 | goto leave; |
1722 | 0 | } |
1723 | 3.70k | else |
1724 | 3.70k | { |
1725 | 10.0k | for (i = 0; i < ndata; i++) |
1726 | 6.32k | { |
1727 | 6.32k | n = pktlen; |
1728 | 6.32k | k->data[i] = mpi_read (inp, &n, 0); |
1729 | 6.32k | pktlen -= n; |
1730 | 6.32k | if (!k->data[i]) |
1731 | 2.36k | rc = gpg_error (GPG_ERR_INV_PACKET); |
1732 | 6.32k | } |
1733 | 3.70k | if (rc) |
1734 | 1.92k | goto leave; |
1735 | 3.70k | } |
1736 | 39.5k | 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 | 55.3k | leave: |
1750 | 55.3k | iobuf_skip_rest (inp, pktlen, 0); |
1751 | 55.3k | return rc; |
1752 | 39.5k | } |
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 | 45.6M | { |
2063 | 45.6M | switch (type) |
2064 | 45.6M | { |
2065 | 6.49k | case SIGSUBPKT_REV_KEY: |
2066 | 6.49k | if (n < 22) |
2067 | 1.34k | break; |
2068 | 5.15k | return 0; |
2069 | 16.1M | case SIGSUBPKT_SIG_CREATED: |
2070 | 16.1M | case SIGSUBPKT_SIG_EXPIRE: |
2071 | 16.1M | case SIGSUBPKT_KEY_EXPIRE: |
2072 | 16.1M | if (n < 4) |
2073 | 5.03k | break; |
2074 | 16.1M | return 0; |
2075 | 785k | case SIGSUBPKT_KEY_FLAGS: |
2076 | 788k | case SIGSUBPKT_KS_FLAGS: |
2077 | 792k | case SIGSUBPKT_PREF_SYM: |
2078 | 792k | case SIGSUBPKT_PREF_AEAD: |
2079 | 795k | case SIGSUBPKT_PREF_HASH: |
2080 | 799k | case SIGSUBPKT_PREF_COMPR: |
2081 | 802k | case SIGSUBPKT_POLICY: |
2082 | 810k | case SIGSUBPKT_PREF_KS: |
2083 | 815k | case SIGSUBPKT_FEATURES: |
2084 | 815k | case SIGSUBPKT_REGEXP: |
2085 | 815k | case SIGSUBPKT_ATTST_SIGS: |
2086 | 815k | return 0; |
2087 | 9.16k | case SIGSUBPKT_SIGNATURE: |
2088 | 37.2k | case SIGSUBPKT_EXPORTABLE: |
2089 | 39.9k | case SIGSUBPKT_REVOCABLE: |
2090 | 40.1k | case SIGSUBPKT_REVOC_REASON: |
2091 | 40.1k | if (!n) |
2092 | 1.88k | break; |
2093 | 38.2k | return 0; |
2094 | 10.2M | case SIGSUBPKT_ISSUER: /* issuer key ID */ |
2095 | 10.2M | if (n < 8) |
2096 | 3.84k | break; |
2097 | 10.2M | return 0; |
2098 | 18.3M | case SIGSUBPKT_ISSUER_FPR: /* issuer key fingerprint */ |
2099 | 18.3M | if (n < 21) |
2100 | 4.03k | break; |
2101 | 18.2M | return 0; |
2102 | 2.26k | 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 | 2.26k | if (n < 8 |
2107 | 981 | || 8 + ((buffer[4] << 8) | buffer[5]) + |
2108 | 981 | ((buffer[6] << 8) | buffer[7]) != n) |
2109 | 1.65k | break; |
2110 | 610 | return 0; |
2111 | 672 | case SIGSUBPKT_PRIMARY_UID: |
2112 | 672 | if (n != 1) |
2113 | 37 | break; |
2114 | 635 | return 0; |
2115 | 7.77k | case SIGSUBPKT_TRUST: |
2116 | 7.77k | if (n != 2) |
2117 | 6.90k | break; |
2118 | 877 | return 0; |
2119 | 2.61k | case SIGSUBPKT_KEY_BLOCK: |
2120 | 2.61k | if (n && buffer[0]) |
2121 | 1.20k | return -1; /* Unknown version - ignore. */ |
2122 | 1.40k | if (n < 50) |
2123 | 1.40k | break; /* Definitely too short to carry a key block. */ |
2124 | 2 | return 0; |
2125 | 25.5k | default: |
2126 | 25.5k | return 0; |
2127 | 45.6M | } |
2128 | 26.1k | return -2; |
2129 | 45.6M | } |
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 | 935 | { |
2136 | 935 | strlist_t sl; |
2137 | | |
2138 | 935 | register_known_notation (NULL); /* Make sure it is initialized. */ |
2139 | | |
2140 | 1.84k | for (sl = known_notations_list; sl; sl = sl->next) |
2141 | 935 | if (sl->flags == len && !memcmp (sl->d, name, len)) |
2142 | 29 | return 1; /* Known */ |
2143 | | |
2144 | 906 | 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 | 906 | return 0; /* Unknown. */ |
2152 | 935 | } |
2153 | | |
2154 | | |
2155 | | static int |
2156 | | can_handle_critical (const byte * buffer, size_t n, int type) |
2157 | 10.0M | { |
2158 | 10.0M | switch (type) |
2159 | 10.0M | { |
2160 | 2.38k | case SIGSUBPKT_NOTATION: |
2161 | 2.38k | if (n >= 8) |
2162 | 1.10k | { |
2163 | 1.10k | size_t notation_len = ((buffer[4] << 8) | buffer[5]); |
2164 | 1.10k | if (n - 8 >= notation_len) |
2165 | 935 | return can_handle_critical_notation (buffer + 8, notation_len); |
2166 | 1.10k | } |
2167 | 1.45k | return 0; |
2168 | 2.08k | case SIGSUBPKT_SIGNATURE: |
2169 | 5.09k | case SIGSUBPKT_SIG_CREATED: |
2170 | 6.72k | case SIGSUBPKT_SIG_EXPIRE: |
2171 | 322k | case SIGSUBPKT_KEY_EXPIRE: |
2172 | 324k | case SIGSUBPKT_EXPORTABLE: |
2173 | 328k | case SIGSUBPKT_REVOCABLE: |
2174 | 333k | case SIGSUBPKT_REV_KEY: |
2175 | 339k | case SIGSUBPKT_ISSUER: /* issuer key ID */ |
2176 | 342k | case SIGSUBPKT_ISSUER_FPR: /* issuer fingerprint */ |
2177 | 344k | case SIGSUBPKT_PREF_SYM: |
2178 | 345k | case SIGSUBPKT_PREF_AEAD: |
2179 | 346k | case SIGSUBPKT_PREF_HASH: |
2180 | 348k | case SIGSUBPKT_PREF_COMPR: |
2181 | 359k | case SIGSUBPKT_KEY_FLAGS: |
2182 | 360k | case SIGSUBPKT_PRIMARY_UID: |
2183 | 363k | case SIGSUBPKT_FEATURES: |
2184 | 366k | case SIGSUBPKT_TRUST: |
2185 | 366k | case SIGSUBPKT_REGEXP: |
2186 | 367k | case SIGSUBPKT_ATTST_SIGS: |
2187 | | /* Is it enough to show the policy or keyserver? */ |
2188 | 369k | case SIGSUBPKT_POLICY: |
2189 | 374k | case SIGSUBPKT_PREF_KS: |
2190 | 375k | case SIGSUBPKT_REVOC_REASON: /* At least we know about it. */ |
2191 | 375k | return 1; |
2192 | | |
2193 | 3.53k | case SIGSUBPKT_KEY_BLOCK: |
2194 | 3.53k | if (n && !buffer[0]) |
2195 | 222 | return 1; |
2196 | 3.31k | else |
2197 | 3.31k | return 0; |
2198 | | |
2199 | 9.63M | default: |
2200 | 9.63M | return 0; |
2201 | 10.0M | } |
2202 | 10.0M | } |
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 | 457M | { |
2209 | 457M | const byte *buffer; |
2210 | 457M | int buflen; |
2211 | 457M | int type; |
2212 | 457M | int critical_dummy; |
2213 | 457M | int offset; |
2214 | 457M | size_t n; |
2215 | 457M | const subpktarea_t *pktbuf = want_hashed? sig->hashed : sig->unhashed; |
2216 | 457M | int seq = 0; |
2217 | 457M | int reqseq = start ? *start : 0; |
2218 | | |
2219 | 457M | if (!critical) |
2220 | 457M | critical = &critical_dummy; |
2221 | | |
2222 | 457M | if (!pktbuf || reqseq == -1) |
2223 | 380k | { |
2224 | 380k | 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 | 380k | return reqtype == SIGSUBPKT_TEST_CRITICAL ? dummy : NULL; |
2228 | 380k | } |
2229 | 457M | buffer = pktbuf->data; |
2230 | 457M | buflen = pktbuf->len; |
2231 | 1.56G | while (buflen) |
2232 | 1.34G | { |
2233 | 1.34G | n = *buffer++; |
2234 | 1.34G | buflen--; |
2235 | 1.34G | if (n == 255) /* 4 byte length header. */ |
2236 | 432k | { |
2237 | 432k | if (buflen < 4) |
2238 | 200k | goto too_short; |
2239 | 232k | n = buf32_to_size_t (buffer); |
2240 | 232k | buffer += 4; |
2241 | 232k | buflen -= 4; |
2242 | 232k | } |
2243 | 1.34G | else if (n >= 192) /* 4 byte special encoded length header. */ |
2244 | 1.02M | { |
2245 | 1.02M | if (buflen < 2) |
2246 | 118k | goto too_short; |
2247 | 905k | n = ((n - 192) << 8) + *buffer + 192; |
2248 | 905k | buffer++; |
2249 | 905k | buflen--; |
2250 | 905k | } |
2251 | 1.34G | if (buflen < n) |
2252 | 178M | goto too_short; |
2253 | 1.16G | if (!buflen) |
2254 | 1.00M | goto no_type_byte; |
2255 | 1.16G | type = *buffer; |
2256 | 1.16G | if (type & 0x80) |
2257 | 146M | { |
2258 | 146M | type &= 0x7f; |
2259 | 146M | *critical = 1; |
2260 | 146M | } |
2261 | 1.02G | else |
2262 | 1.02G | *critical = 0; |
2263 | 1.16G | if (!(++seq > reqseq)) |
2264 | 59.0k | ; |
2265 | 1.16G | else if (reqtype == SIGSUBPKT_TEST_CRITICAL) |
2266 | 62.5M | { |
2267 | 62.5M | if (*critical) |
2268 | 10.0M | { |
2269 | 10.0M | if (n - 1 > buflen + 1) |
2270 | 18.0k | goto too_short; |
2271 | 10.0M | if (!can_handle_critical (buffer + 1, n - 1, type)) |
2272 | 9.63M | { |
2273 | 9.63M | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
2274 | 9.63M | log_info (_("subpacket of type %d has " |
2275 | 0 | "critical bit set\n"), type); |
2276 | 9.63M | if (start) |
2277 | 0 | *start = seq; |
2278 | 9.63M | return NULL; /* This is an error. */ |
2279 | 9.63M | } |
2280 | 10.0M | } |
2281 | 62.5M | } |
2282 | 1.10G | 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 | 1.10G | else if (type == reqtype) /* Found. */ |
2286 | 45.6M | { |
2287 | 45.6M | buffer++; |
2288 | 45.6M | n--; |
2289 | 45.6M | if (n > buflen) |
2290 | 9.27k | goto too_short; |
2291 | 45.6M | if (ret_n) |
2292 | 19.1M | *ret_n = n; |
2293 | 45.6M | offset = parse_one_sig_subpkt (buffer, n, type); |
2294 | 45.6M | switch (offset) |
2295 | 45.6M | { |
2296 | 26.1k | case -2: |
2297 | 26.1k | log_error ("subpacket of type %d too short\n", type); |
2298 | 26.1k | return NULL; |
2299 | 1.20k | case -1: |
2300 | 1.20k | return NULL; |
2301 | 45.5M | default: |
2302 | 45.5M | break; |
2303 | 45.6M | } |
2304 | 45.5M | if (start) |
2305 | 17.2k | *start = seq; |
2306 | 45.5M | return buffer + offset; |
2307 | 45.6M | } |
2308 | 1.11G | buffer += n; |
2309 | 1.11G | buflen -= n; |
2310 | 1.11G | } |
2311 | 222M | 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 | 31.9M | return buffer; |
2317 | | |
2318 | | /* Critical bit we don't understand. */ |
2319 | 190M | if (start) |
2320 | 1.57M | *start = -1; |
2321 | 190M | return NULL; /* End of packets; not found. */ |
2322 | | |
2323 | 178M | too_short: |
2324 | 178M | 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 | 178M | if (start) |
2333 | 10.9M | *start = -1; |
2334 | 178M | return NULL; |
2335 | | |
2336 | 1.00M | no_type_byte: |
2337 | 1.00M | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
2338 | 1.00M | log_info ("type octet missing in subpacket\n"); |
2339 | 1.00M | if (start) |
2340 | 1.17k | *start = -1; |
2341 | 1.00M | return NULL; |
2342 | 222M | } |
2343 | | |
2344 | | |
2345 | | const byte * |
2346 | | parse_sig_subpkt (PKT_signature *sig, int want_hashed, sigsubpkttype_t reqtype, |
2347 | | size_t *ret_n) |
2348 | 445M | { |
2349 | 445M | return enum_sig_subpkt (sig, want_hashed, reqtype, ret_n, NULL, NULL); |
2350 | 445M | } |
2351 | | |
2352 | | |
2353 | | const byte * |
2354 | | parse_sig_subpkt2 (PKT_signature *sig, sigsubpkttype_t reqtype) |
2355 | 39.8M | { |
2356 | 39.8M | const byte *p; |
2357 | | |
2358 | 39.8M | p = parse_sig_subpkt (sig, 1, reqtype, NULL); |
2359 | 39.8M | if (!p) |
2360 | 39.8M | p = parse_sig_subpkt (sig, 0, reqtype, NULL); |
2361 | 39.8M | return p; |
2362 | 39.8M | } |
2363 | | |
2364 | | |
2365 | | /* Find all revocation keys. Look in hashed area only. */ |
2366 | | void |
2367 | | parse_revkeys (PKT_signature * sig) |
2368 | 10.9M | { |
2369 | 10.9M | const byte *revkey; |
2370 | 10.9M | int seq = 0; |
2371 | 10.9M | size_t len; |
2372 | | |
2373 | 10.9M | if (sig->sig_class != 0x1F) |
2374 | 0 | return; |
2375 | | |
2376 | 10.9M | while ((revkey = enum_sig_subpkt (sig, 1, SIGSUBPKT_REV_KEY, |
2377 | 10.9M | &len, &seq, NULL))) |
2378 | 5.15k | { |
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 | 5.15k | if ((len == 22 || len == 34) |
2383 | 4.85k | && (revkey[0] & 0x80)) |
2384 | 2.00k | { |
2385 | 2.00k | sig->revkey = xrealloc (sig->revkey, |
2386 | 2.00k | sizeof (struct revocation_key) * |
2387 | 2.00k | (sig->numrevkeys + 1)); |
2388 | | |
2389 | 2.00k | sig->revkey[sig->numrevkeys].class = revkey[0]; |
2390 | 2.00k | sig->revkey[sig->numrevkeys].algid = revkey[1]; |
2391 | 2.00k | len -= 2; |
2392 | 2.00k | sig->revkey[sig->numrevkeys].fprlen = len; |
2393 | 2.00k | memcpy (sig->revkey[sig->numrevkeys].fpr, revkey+2, len); |
2394 | 2.00k | memset (sig->revkey[sig->numrevkeys].fpr+len, 0, |
2395 | 2.00k | sizeof (sig->revkey[sig->numrevkeys].fpr) - len); |
2396 | 2.00k | sig->numrevkeys++; |
2397 | 2.00k | } |
2398 | 5.15k | } |
2399 | 10.9M | } |
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 | 29.4M | { |
2410 | 29.4M | int md5_len = 0; |
2411 | 29.4M | unsigned int n; |
2412 | 29.4M | int is_v4plus = 0; |
2413 | 29.4M | int rc = 0; |
2414 | 29.4M | int i, ndata; |
2415 | | |
2416 | 29.4M | if (pktlen < 16) |
2417 | 327k | { |
2418 | 327k | log_error ("packet(%d) too short\n", pkttype); |
2419 | 327k | if (list_mode) |
2420 | 327k | es_fputs (":signature packet: [too short]\n", listfp); |
2421 | 327k | goto leave; |
2422 | 327k | } |
2423 | 29.1M | sig->version = iobuf_get_noeof (inp); |
2424 | 29.1M | pktlen--; |
2425 | 29.1M | if (sig->version == 4 || sig->version == 5 |
2426 | 112k | || (sig->version == 6 && RFC9980)) |
2427 | 28.9M | is_v4plus = 1; |
2428 | 112k | else if (sig->version != 2 && sig->version != 3) |
2429 | 1.90k | { |
2430 | 1.90k | log_error ("packet(%d) with unknown version %d\n", |
2431 | 1.90k | pkttype, sig->version); |
2432 | 1.90k | if (list_mode) |
2433 | 1.90k | es_fputs (":signature packet: [unknown version]\n", listfp); |
2434 | 1.90k | rc = gpg_error (GPG_ERR_INV_PACKET); |
2435 | 1.90k | goto leave; |
2436 | 1.90k | } |
2437 | | |
2438 | 29.1M | if (!is_v4plus) |
2439 | 110k | { |
2440 | 110k | if (pktlen == 0) |
2441 | 0 | goto underflow; |
2442 | 110k | md5_len = iobuf_get_noeof (inp); |
2443 | 110k | pktlen--; |
2444 | 110k | } |
2445 | 29.1M | if (pktlen == 0) |
2446 | 0 | goto underflow; |
2447 | 29.1M | sig->sig_class = iobuf_get_noeof (inp); |
2448 | 29.1M | pktlen--; |
2449 | 29.1M | if (!is_v4plus) |
2450 | 110k | { |
2451 | 110k | if (pktlen < 12) |
2452 | 0 | goto underflow; |
2453 | 110k | sig->timestamp = read_32 (inp); |
2454 | 110k | pktlen -= 4; |
2455 | 110k | sig->keyid[0] = read_32 (inp); |
2456 | 110k | pktlen -= 4; |
2457 | 110k | sig->keyid[1] = read_32 (inp); |
2458 | 110k | pktlen -= 4; |
2459 | 110k | } |
2460 | 29.1M | if (pktlen < 2) |
2461 | 64 | goto underflow; |
2462 | 29.1M | sig->pubkey_algo = iobuf_get_noeof (inp); |
2463 | 29.1M | pktlen--; |
2464 | 29.1M | sig->digest_algo = iobuf_get_noeof (inp); |
2465 | 29.1M | pktlen--; |
2466 | 29.1M | sig->flags.exportable = 1; |
2467 | 29.1M | sig->flags.revocable = 1; |
2468 | 29.1M | if (is_v4plus) /* Read subpackets. */ |
2469 | 28.9M | { |
2470 | 28.9M | if (sig->version == 6) |
2471 | 19 | { |
2472 | 19 | if (pktlen < 4) |
2473 | 0 | goto underflow; |
2474 | 19 | n = read_32 (inp); |
2475 | 19 | pktlen -= 4; /* Length of hashed data. */ |
2476 | 19 | } |
2477 | 28.9M | else |
2478 | 28.9M | { |
2479 | 28.9M | if (pktlen < 2) |
2480 | 0 | goto underflow; |
2481 | 28.9M | n = read_16 (inp); |
2482 | 28.9M | pktlen -= 2; /* Length of hashed data. */ |
2483 | 28.9M | } |
2484 | 28.9M | if (pktlen < n) |
2485 | 4.42k | goto underflow; |
2486 | 28.9M | if (n > 30000) |
2487 | 302 | { |
2488 | 302 | log_error ("signature packet: hashed data too long (%u)\n", n); |
2489 | 302 | if (list_mode) |
2490 | 302 | es_fprintf (listfp, |
2491 | 0 | ":signature packet: [hashed data too long (%u)]\n", n); |
2492 | 302 | rc = GPG_ERR_INV_PACKET; |
2493 | 302 | goto leave; |
2494 | 302 | } |
2495 | 28.9M | if (n) |
2496 | 28.9M | { |
2497 | 28.9M | sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1); |
2498 | 28.9M | sig->hashed->size = n; |
2499 | 28.9M | sig->hashed->len = n; |
2500 | 28.9M | if (iobuf_read (inp, sig->hashed->data, n) != n) |
2501 | 815 | { |
2502 | 815 | log_error ("premature eof while reading " |
2503 | 815 | "hashed signature data\n"); |
2504 | 815 | if (list_mode) |
2505 | 815 | es_fputs (":signature packet: [premature eof]\n", listfp); |
2506 | 815 | rc = -1; |
2507 | 815 | goto leave; |
2508 | 815 | } |
2509 | 28.9M | pktlen -= n; |
2510 | 28.9M | } |
2511 | 28.9M | if (sig->version == 6) |
2512 | 1 | { |
2513 | 1 | if (pktlen < 4) |
2514 | 0 | goto underflow; |
2515 | 1 | n = read_32 (inp); |
2516 | 1 | pktlen -= 4; /* Length of unhashed data. */ |
2517 | 1 | } |
2518 | 28.9M | else |
2519 | 28.9M | { |
2520 | 28.9M | if (pktlen < 2) |
2521 | 584 | goto underflow; |
2522 | 28.9M | n = read_16 (inp); |
2523 | 28.9M | pktlen -= 2; /* Length of unhashed data. */ |
2524 | 28.9M | } |
2525 | 28.9M | if (pktlen < n) |
2526 | 13.7k | goto underflow; |
2527 | 28.9M | if (n > 10000) |
2528 | 453 | { |
2529 | 453 | log_error ("signature packet: unhashed data too long (%u)\n", n); |
2530 | 453 | if (list_mode) |
2531 | 453 | es_fprintf (listfp, |
2532 | 0 | ":signature packet: [unhashed data too long (%u)]\n", |
2533 | 0 | n); |
2534 | 453 | rc = GPG_ERR_INV_PACKET; |
2535 | 453 | goto leave; |
2536 | 453 | } |
2537 | 28.9M | if (n) |
2538 | 28.8M | { |
2539 | 28.8M | sig->unhashed = xmalloc (sizeof (*sig->unhashed) + n - 1); |
2540 | 28.8M | sig->unhashed->size = n; |
2541 | 28.8M | sig->unhashed->len = n; |
2542 | 28.8M | if (iobuf_read (inp, sig->unhashed->data, n) != n) |
2543 | 253 | { |
2544 | 253 | log_error ("premature eof while reading " |
2545 | 253 | "unhashed signature data\n"); |
2546 | 253 | if (list_mode) |
2547 | 253 | es_fputs (":signature packet: [premature eof]\n", listfp); |
2548 | 253 | rc = -1; |
2549 | 253 | goto leave; |
2550 | 253 | } |
2551 | 28.8M | pktlen -= n; |
2552 | 28.8M | } |
2553 | 28.9M | } |
2554 | | |
2555 | 29.0M | if (pktlen < 2) |
2556 | 1.13k | goto underflow; |
2557 | 29.0M | sig->digest_start[0] = iobuf_get_noeof (inp); |
2558 | 29.0M | pktlen--; |
2559 | 29.0M | sig->digest_start[1] = iobuf_get_noeof (inp); |
2560 | 29.0M | pktlen--; |
2561 | | |
2562 | 29.0M | if (sig->version == 6) |
2563 | 0 | { |
2564 | 0 | int saltlen; |
2565 | | |
2566 | | /* Note that we allow for a zero length salt. */ |
2567 | 0 | saltlen = iobuf_get (inp); |
2568 | 0 | if (saltlen < 0 || !pktlen) |
2569 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2570 | 0 | else if (saltlen) |
2571 | 0 | rc = read_raw_octet_string (inp, &pktlen, 0, saltlen, 0, &sig->salt); |
2572 | 0 | pktlen--; /* Adjust for saltlen octet read above. */ |
2573 | |
|
2574 | 0 | if (rc) |
2575 | 0 | goto leave; |
2576 | 0 | } |
2577 | | |
2578 | 29.0M | if (is_v4plus && sig->pubkey_algo) /* Extract required information. */ |
2579 | 28.9M | { |
2580 | 28.9M | const byte *p; |
2581 | 28.9M | 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 | 28.9M | if (!parse_sig_subpkt (sig, 1, SIGSUBPKT_TEST_CRITICAL, NULL) |
2586 | 16.1M | || !parse_sig_subpkt (sig, 0, SIGSUBPKT_TEST_CRITICAL, NULL)) |
2587 | 13.1M | sig->flags.unknown_critical = 1; |
2588 | | |
2589 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_CREATED, NULL); |
2590 | 28.9M | if (p) |
2591 | 16.1M | sig->timestamp = buf32_to_u32 (p); |
2592 | 12.8M | else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110) |
2593 | 12.8M | && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2594 | 12.8M | 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 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_ISSUER_FPR, &len); |
2600 | 28.9M | if (p && len == 21 && p[0] == 4) |
2601 | 18.1M | { |
2602 | 18.1M | sig->keyid[0] = buf32_to_u32 (p + 1 + 12); |
2603 | 18.1M | sig->keyid[1] = buf32_to_u32 (p + 1 + 16); |
2604 | 18.1M | } |
2605 | 10.8M | else if (p && len == 33 && (p[0] == 5 || p[0] == 6)) |
2606 | 129 | { |
2607 | 129 | sig->keyid[0] = buf32_to_u32 (p + 1 ); |
2608 | 129 | sig->keyid[1] = buf32_to_u32 (p + 1 + 4); |
2609 | 129 | } |
2610 | 10.8M | else if ((p = parse_sig_subpkt2 (sig, SIGSUBPKT_ISSUER))) |
2611 | 10.2M | { |
2612 | 10.2M | sig->keyid[0] = buf32_to_u32 (p); |
2613 | 10.2M | sig->keyid[1] = buf32_to_u32 (p + 4); |
2614 | 10.2M | } |
2615 | 620k | else if (!(sig->pubkey_algo >= 100 && sig->pubkey_algo <= 110) |
2616 | 616k | && opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2617 | 620k | log_info ("signature packet without keyid\n"); |
2618 | | |
2619 | | /* Get the intended recipient (revocation subject) fpr. */ |
2620 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_INT_RCP_FPR, &len); |
2621 | 28.9M | if (p && len == 21 && p[0] == 4) |
2622 | 5 | { |
2623 | 5 | sig->rev_subject_info = xmalloc_clear (sizeof *sig->rev_subject_info); |
2624 | | |
2625 | 5 | sig->rev_subject_info->fprlen = 20; |
2626 | 5 | memcpy (sig->rev_subject_info->fpr, p + 1, 20); |
2627 | 5 | } |
2628 | 28.9M | 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 | 28.9M | else |
2636 | 28.9M | { |
2637 | 28.9M | sig->rev_subject_info = NULL; |
2638 | 28.9M | } |
2639 | | |
2640 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIG_EXPIRE, NULL); |
2641 | 28.9M | if (p && buf32_to_u32 (p)) |
2642 | 22.2k | sig->expiredate = sig->timestamp + buf32_to_u32 (p); |
2643 | 28.9M | if (sig->expiredate && sig->expiredate <= make_timestamp ()) |
2644 | 21.7k | sig->flags.expired = 1; |
2645 | | |
2646 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_POLICY, NULL); |
2647 | 28.9M | if (p) |
2648 | 3.01k | sig->flags.policy_url = 1; |
2649 | | |
2650 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_PREF_KS, NULL); |
2651 | 28.9M | if (p) |
2652 | 5.16k | sig->flags.pref_ks = 1; |
2653 | | |
2654 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_SIGNERS_UID, &len); |
2655 | 28.9M | if (p && len) |
2656 | 21.9k | { |
2657 | 21.9k | char *mbox; |
2658 | | |
2659 | 21.9k | sig->signers_uid = try_make_printable_string (p, len, 0); |
2660 | 21.9k | if (!sig->signers_uid) |
2661 | 0 | { |
2662 | 0 | rc = gpg_error_from_syserror (); |
2663 | 0 | goto leave; |
2664 | 0 | } |
2665 | 21.9k | mbox = mailbox_from_userid (sig->signers_uid, 0); |
2666 | 21.9k | if (mbox) |
2667 | 5.03k | { |
2668 | 5.03k | xfree (sig->signers_uid); |
2669 | 5.03k | sig->signers_uid = mbox; |
2670 | 5.03k | } |
2671 | 21.9k | } |
2672 | | |
2673 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_KEY_BLOCK, NULL); |
2674 | 28.9M | if (p) |
2675 | 2 | sig->flags.key_block = 1; |
2676 | | |
2677 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_NOTATION, NULL); |
2678 | 28.9M | if (p) |
2679 | 610 | sig->flags.notation = 1; |
2680 | | |
2681 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_REVOCABLE, NULL); |
2682 | 28.9M | if (p && *p == 0) |
2683 | 1.09k | sig->flags.revocable = 0; |
2684 | | |
2685 | 28.9M | p = parse_sig_subpkt (sig, 1, SIGSUBPKT_TRUST, &len); |
2686 | 28.9M | if (p && len == 2) |
2687 | 877 | { |
2688 | 877 | sig->trust_depth = p[0]; |
2689 | 877 | sig->trust_value = p[1]; |
2690 | | |
2691 | | /* Only look for a regexp if there is also a trust |
2692 | | subpacket. */ |
2693 | 877 | sig->trust_regexp = |
2694 | 877 | 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 | 877 | if (len == 0) |
2699 | 187 | sig->trust_regexp = NULL; |
2700 | 877 | } |
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 | 28.9M | p = parse_sig_subpkt2 (sig, SIGSUBPKT_EXPORTABLE); |
2708 | 28.9M | if (p && *p == 0) |
2709 | 1.72k | sig->flags.exportable = 0; |
2710 | | |
2711 | | /* Find all revocation keys. */ |
2712 | 28.9M | if (sig->sig_class == 0x1F) |
2713 | 10.9M | parse_revkeys (sig); |
2714 | 28.9M | } |
2715 | | |
2716 | 29.0M | 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 | 29.0M | ndata = pubkey_get_nsig (sig->pubkey_algo); |
2740 | 29.0M | if (!ndata) |
2741 | 386k | { |
2742 | 386k | if (list_mode) |
2743 | 386k | es_fprintf (listfp, "\tunknown algorithm %d\n", sig->pubkey_algo); |
2744 | 386k | 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 | 386k | if (pktlen > (5 * MAX_EXTERN_MPI_BITS / 8)) |
2749 | 171 | { |
2750 | | /* We include a limit to avoid too trivial DoS attacks by |
2751 | | having gpg allocate too much memory. */ |
2752 | 171 | log_error ("signature packet: too much data\n"); |
2753 | 171 | rc = GPG_ERR_INV_PACKET; |
2754 | 171 | } |
2755 | 386k | else |
2756 | 386k | { |
2757 | 386k | void *tmpp; |
2758 | | |
2759 | 386k | tmpp = read_rest (inp, pktlen); |
2760 | 386k | sig->data[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0); |
2761 | 386k | pktlen = 0; |
2762 | 386k | } |
2763 | 386k | } |
2764 | 28.7M | else |
2765 | 28.7M | { |
2766 | 85.4M | for (i = 0; i < ndata; i++) |
2767 | 56.7M | { |
2768 | 56.7M | 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 | 56.7M | else |
2775 | 56.7M | { |
2776 | 56.7M | n = pktlen; |
2777 | 56.7M | if (sig->pubkey_algo == PUBKEY_ALGO_ECDSA |
2778 | 55.6M | || sig->pubkey_algo == PUBKEY_ALGO_EDDSA) |
2779 | 56.0M | sig->data[i] = sos_read (inp, &n, 0); |
2780 | 677k | else |
2781 | 677k | sig->data[i] = mpi_read (inp, &n, 0); |
2782 | 56.7M | pktlen -= n; |
2783 | 56.7M | } |
2784 | 56.7M | 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 | 56.7M | if (!sig->data[i]) |
2791 | 3.83k | rc = GPG_ERR_INV_PACKET; |
2792 | 56.7M | } |
2793 | 28.7M | } |
2794 | | |
2795 | 29.4M | leave: |
2796 | 29.4M | iobuf_skip_rest (inp, pktlen, 0); |
2797 | 29.4M | return rc; |
2798 | | |
2799 | 19.9k | underflow: |
2800 | 19.9k | log_error ("packet(%d) too short\n", pkttype); |
2801 | 19.9k | if (list_mode) |
2802 | 19.9k | es_fputs (":signature packet: [too short]\n", listfp); |
2803 | | |
2804 | 19.9k | iobuf_skip_rest (inp, pktlen, 0); |
2805 | | |
2806 | 19.9k | return GPG_ERR_INV_PACKET; |
2807 | 29.0M | } |
2808 | | |
2809 | | |
2810 | | static int |
2811 | | parse_onepass_sig (IOBUF inp, int pkttype, unsigned long pktlen, |
2812 | | PKT_onepass_sig * ops) |
2813 | 39.5k | { |
2814 | 39.5k | int rc = 0; |
2815 | | |
2816 | 39.5k | if (pktlen < 13) |
2817 | 11.7k | { |
2818 | 11.7k | log_error ("packet(%d) too short\n", pkttype); |
2819 | 11.7k | if (list_mode) |
2820 | 11.7k | es_fputs (":onepass_sig packet: [too short]\n", listfp); |
2821 | 11.7k | rc = gpg_error (GPG_ERR_INV_PACKET); |
2822 | 11.7k | goto leave; |
2823 | 11.7k | } |
2824 | 27.7k | ops->version = iobuf_get_noeof (inp); |
2825 | 27.7k | pktlen--; |
2826 | 27.7k | if (!(ops->version == 3 || (RFC9980 && ops->version == 6))) |
2827 | 1.68k | { |
2828 | 1.68k | log_error ("onepass_sig with unknown version %d\n", ops->version); |
2829 | 1.68k | if (list_mode) |
2830 | 1.68k | es_fputs (":onepass_sig packet: [unknown version]\n", listfp); |
2831 | 1.68k | rc = gpg_error (GPG_ERR_INV_PACKET); |
2832 | 1.68k | goto leave; |
2833 | 1.68k | } |
2834 | 26.0k | ops->sig_class = iobuf_get_noeof (inp); |
2835 | 26.0k | pktlen--; |
2836 | 26.0k | ops->digest_algo = iobuf_get_noeof (inp); |
2837 | 26.0k | pktlen--; |
2838 | 26.0k | ops->pubkey_algo = iobuf_get_noeof (inp); |
2839 | 26.0k | pktlen--; |
2840 | | |
2841 | 26.0k | if (ops->version == 6) |
2842 | 5 | { |
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 | 5 | unsigned char fpr[32]; |
2847 | | |
2848 | 5 | rc = read_raw_octet_string (inp, &pktlen, 1, 0, 0, &ops->salt); |
2849 | 5 | if (rc) |
2850 | 4 | goto leave; |
2851 | | |
2852 | 1 | if (pktlen < sizeof fpr) |
2853 | 0 | { |
2854 | 0 | log_error ("packet(%d) too short for fingerprint (%lu/%zu)\n", |
2855 | 0 | pkttype, pktlen, sizeof fpr); |
2856 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2857 | 0 | goto leave; |
2858 | 0 | } |
2859 | 1 | if (iobuf_read (inp, fpr, sizeof fpr) != sizeof fpr) |
2860 | 0 | { |
2861 | 0 | log_error ("premature eof while reading " |
2862 | 0 | "fingerprint from packet(%d)\n", pkttype); |
2863 | 0 | rc = gpg_error (GPG_ERR_INV_PACKET); |
2864 | 0 | goto leave; |
2865 | 0 | } |
2866 | 1 | pktlen -= sizeof fpr; |
2867 | | |
2868 | 1 | ops->keyid[0] = buf32_to_u32 (fpr); |
2869 | 1 | ops->keyid[1] = buf32_to_u32 (fpr+4); |
2870 | 1 | } |
2871 | 26.0k | else |
2872 | 26.0k | { |
2873 | 26.0k | ops->keyid[0] = read_32 (inp); |
2874 | 26.0k | pktlen -= 4; |
2875 | 26.0k | ops->keyid[1] = read_32 (inp); |
2876 | 26.0k | pktlen -= 4; |
2877 | 26.0k | } |
2878 | | |
2879 | 26.0k | ops->last = iobuf_get_noeof (inp); |
2880 | 26.0k | pktlen--; |
2881 | 26.0k | 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 | 39.5k | leave: |
2899 | 39.5k | iobuf_skip_rest (inp, pktlen, 0); |
2900 | 39.5k | return rc; |
2901 | 26.0k | } |
2902 | | |
2903 | | |
2904 | | static int |
2905 | | parse_key (IOBUF inp, int pkttype, unsigned long pktlen, |
2906 | | byte * hdr, int hdrlen, PACKET * pkt) |
2907 | 2.05M | { |
2908 | 2.05M | gpg_error_t err = 0; |
2909 | 2.05M | int i, version, algorithm; |
2910 | 2.05M | unsigned long timestamp, expiredate, max_expiredate; |
2911 | 2.05M | int npkey, nskey; |
2912 | 2.05M | u32 keyid[2]; |
2913 | 2.05M | PKT_public_key *pk; |
2914 | 2.05M | int is_v5 = 0; |
2915 | 2.05M | int is_v6 = 0; |
2916 | 2.05M | unsigned int pkbytes; /* For v5 keys: Number of bytes in the public |
2917 | | * key material. For v4 keys: 0. */ |
2918 | | |
2919 | 2.05M | (void) hdr; |
2920 | | |
2921 | 2.05M | pk = pkt->pkt.public_key; /* PK has been cleared. */ |
2922 | | |
2923 | 2.05M | version = iobuf_get_noeof (inp); |
2924 | 2.05M | pktlen--; |
2925 | 2.05M | if (pkttype == PKT_PUBLIC_SUBKEY && version == '#') |
2926 | 7.87k | { |
2927 | | /* Early versions of G10 used the old PGP comments packets; |
2928 | | * luckily all those comments are started by a hash. */ |
2929 | 7.87k | 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 | 7.87k | iobuf_skip_rest (inp, pktlen, 0); |
2946 | 7.87k | return 0; |
2947 | 7.87k | } |
2948 | 2.04M | else if (version == 4) |
2949 | 2.01M | ; |
2950 | 39.2k | else if (version == 5) |
2951 | 13.9k | is_v5 = 1; |
2952 | 25.3k | else if (version == 6 && RFC9980) |
2953 | 18 | is_v6 = 1; |
2954 | 25.3k | else if (version == 2 || version == 3) |
2955 | 4.83k | { |
2956 | | /* Not anymore supported since 2.1. Use an older gpg version |
2957 | | * (i.e. gpg 1.4) to parse v3 packets. */ |
2958 | 4.83k | if (opt.verbose > 1 && !glo_ctrl.silence_parse_warnings) |
2959 | 4.83k | log_info ("packet(%d) with obsolete version %d\n", pkttype, version); |
2960 | 4.83k | if (list_mode) |
2961 | 4.83k | es_fprintf (listfp, ":key packet: [obsolete version %d]\n", version); |
2962 | 4.83k | pk->version = version; |
2963 | 4.83k | err = gpg_error (GPG_ERR_LEGACY_KEY); |
2964 | 4.83k | goto leave; |
2965 | 4.83k | } |
2966 | 20.5k | else |
2967 | 20.5k | { |
2968 | 20.5k | log_error ("packet(%d) with unknown version %d\n", pkttype, version); |
2969 | 20.5k | if (list_mode) |
2970 | 20.5k | es_fputs (":key packet: [unknown version]\n", listfp); |
2971 | 20.5k | err = gpg_error (GPG_ERR_INV_PACKET); |
2972 | 20.5k | goto leave; |
2973 | 20.5k | } |
2974 | | |
2975 | 2.02M | if (pktlen < (is_v5? 15:11)) |
2976 | 201 | { |
2977 | 201 | log_error ("packet(%d) too short\n", pkttype); |
2978 | 201 | if (list_mode) |
2979 | 201 | es_fputs (":key packet: [too short]\n", listfp); |
2980 | 201 | err = gpg_error (GPG_ERR_INV_PACKET); |
2981 | 201 | goto leave; |
2982 | 201 | } |
2983 | 2.02M | else if (pktlen > MAX_KEY_PACKET_LENGTH) |
2984 | 11.3k | { |
2985 | 11.3k | log_error ("packet(%d) too large\n", pkttype); |
2986 | 11.3k | if (list_mode) |
2987 | 11.3k | es_fputs (":key packet: [too large]\n", listfp); |
2988 | 11.3k | err = gpg_error (GPG_ERR_INV_PACKET); |
2989 | 11.3k | goto leave; |
2990 | 11.3k | } |
2991 | | |
2992 | 2.01M | timestamp = read_32 (inp); |
2993 | 2.01M | pktlen -= 4; |
2994 | 2.01M | expiredate = 0; /* have to get it from the selfsignature */ |
2995 | 2.01M | max_expiredate = 0; |
2996 | 2.01M | algorithm = iobuf_get_noeof (inp); |
2997 | 2.01M | pktlen--; |
2998 | 2.01M | if (is_v5 || is_v6) |
2999 | 13.4k | { |
3000 | 13.4k | pkbytes = read_32 (inp); |
3001 | 13.4k | pktlen -= 4; |
3002 | 13.4k | } |
3003 | 1.99M | else |
3004 | 1.99M | pkbytes = 0; |
3005 | | |
3006 | 2.01M | 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 | 2.01M | pk->timestamp = timestamp; |
3022 | 2.01M | pk->expiredate = expiredate; |
3023 | 2.01M | pk->max_expiredate = max_expiredate; |
3024 | 2.01M | pk->hdrbytes = hdrlen; |
3025 | 2.01M | pk->version = version; |
3026 | 2.01M | pk->flags.primary = (pkttype == PKT_PUBLIC_KEY || pkttype == PKT_SECRET_KEY); |
3027 | 2.01M | pk->pubkey_algo = algorithm; |
3028 | | |
3029 | 2.01M | nskey = pubkey_get_nskey (algorithm); |
3030 | 2.01M | npkey = pubkey_get_npkey (algorithm); |
3031 | 2.01M | if (!npkey) |
3032 | 48.0k | { |
3033 | 48.0k | if (list_mode) |
3034 | 48.0k | es_fprintf (listfp, "\tunknown algorithm %d\n", algorithm); |
3035 | 48.0k | unknown_pubkey_warning (algorithm); |
3036 | 48.0k | } |
3037 | | |
3038 | 2.01M | if (!npkey) |
3039 | 48.0k | { |
3040 | | /* Unknown algorithm - put data into an opaque MPI. */ |
3041 | 48.0k | 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 | 48.0k | pk->pkey[0] = gcry_mpi_set_opaque (NULL, tmpp, tmpp? pktlen * 8 : 0); |
3045 | 48.0k | pktlen = 0; |
3046 | 48.0k | goto leave; |
3047 | 48.0k | } |
3048 | 1.96M | else |
3049 | 1.96M | { |
3050 | 7.74M | for (i = 0; i < npkey; i++) |
3051 | 5.78M | { |
3052 | 5.78M | if ( (algorithm == PUBKEY_ALGO_ECDSA && (i == 0)) |
3053 | 5.78M | || (algorithm == PUBKEY_ALGO_EDDSA && (i == 0)) |
3054 | 5.74M | || (algorithm == PUBKEY_ALGO_ECDH && (i == 0 || i == 2)) |
3055 | 2.03M | || (algorithm == PUBKEY_ALGO_KYBER && (i == 0))) |
3056 | 3.75M | { |
3057 | | /* Read the OID (i==0) or the KDF params (i==2). */ |
3058 | 3.75M | err = read_sized_octet_string (inp, &pktlen, pk->pkey+i); |
3059 | 3.75M | } |
3060 | 2.03M | else if (algorithm == PUBKEY_ALGO_KYBER && i == 2) |
3061 | 640 | { |
3062 | | /* Read the four-octet count prefixed Kyber public key. */ |
3063 | 640 | err = read_sos_octet_string (inp, &pktlen, 4, 0, 0, pk->pkey+i); |
3064 | 640 | } |
3065 | 2.03M | else if (algorithm == PUBKEY_ALGO_X25519 && RFC9980) |
3066 | 7 | { |
3067 | 7 | err = read_raw_octet_string (inp, &pktlen, 0, 32, |
3068 | 7 | 0, pk->pkey+i); |
3069 | 7 | } |
3070 | 2.03M | else if (algorithm == PUBKEY_ALGO_MLK768_25519 && RFC9980) |
3071 | 23 | { |
3072 | 23 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 32 : 1184, |
3073 | 23 | 0, pk->pkey+i); |
3074 | 23 | } |
3075 | 2.03M | else if ((algorithm == PUBKEY_ALGO_MLK768_NP384 |
3076 | 2.03M | || 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 | 2.03M | else if (algorithm == PUBKEY_ALGO_MLK1024_448 && RFC9980) |
3082 | 7 | { |
3083 | 7 | if (is_v6) |
3084 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 56 : 1568, |
3085 | 0 | 0, pk->pkey+i); |
3086 | 7 | else |
3087 | 7 | err = gpg_error (GPG_ERR_INV_PACKET); |
3088 | 7 | } |
3089 | 2.03M | else if (algorithm == PUBKEY_ALGO_MLK1024_NP521 && RFC9980) |
3090 | 4 | { |
3091 | 4 | if (is_v6) |
3092 | 0 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 133 : 1568, |
3093 | 0 | 0, pk->pkey+i); |
3094 | 4 | else |
3095 | 4 | err = gpg_error (GPG_ERR_INV_PACKET); |
3096 | 4 | } |
3097 | 2.03M | else if (algorithm == PUBKEY_ALGO_MLK1024_BP512 && RFC9980) |
3098 | 7 | { |
3099 | 7 | if (is_v6) |
3100 | 2 | err = read_raw_octet_string (inp, &pktlen, 0, i==0? 129 : 1568, |
3101 | 2 | 0, pk->pkey+i); |
3102 | 5 | else |
3103 | 5 | err = gpg_error (GPG_ERR_INV_PACKET); |
3104 | 7 | } |
3105 | 2.03M | 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 | 2.03M | 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 | 2.03M | else |
3114 | 2.03M | { |
3115 | | /* Read MPI or SOS. */ |
3116 | 2.03M | unsigned int n = pktlen; |
3117 | 2.03M | if (algorithm == PUBKEY_ALGO_ECDSA |
3118 | 2.02M | || algorithm == PUBKEY_ALGO_EDDSA |
3119 | 1.99M | || algorithm == PUBKEY_ALGO_ECDH |
3120 | 131k | || algorithm == PUBKEY_ALGO_KYBER) |
3121 | 1.89M | pk->pkey[i] = sos_read (inp, &n, 0); |
3122 | 131k | else |
3123 | 131k | pk->pkey[i] = mpi_read (inp, &n, 0); |
3124 | 2.03M | pktlen -= n; |
3125 | 2.03M | if (!pk->pkey[i]) |
3126 | 7.68k | err = gpg_error (GPG_ERR_INV_PACKET); |
3127 | 2.03M | } |
3128 | 5.78M | if (err) |
3129 | 8.79k | goto leave; |
3130 | 5.78M | } |
3131 | 1.95M | 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 | 1.95M | } |
3160 | 1.95M | if (list_mode) |
3161 | 0 | keyid_from_pk (pk, keyid); |
3162 | | |
3163 | 1.95M | if (pkttype == PKT_SECRET_KEY || pkttype == PKT_SECRET_SUBKEY) |
3164 | 55.1k | { |
3165 | 55.1k | struct seckey_info *ski; |
3166 | 55.1k | byte temp[16]; |
3167 | 55.1k | size_t snlen = 0; |
3168 | 55.1k | unsigned int skbytes; |
3169 | | |
3170 | 55.1k | if (pktlen < 1) |
3171 | 363 | { |
3172 | 363 | err = gpg_error (GPG_ERR_INV_PACKET); |
3173 | 363 | goto leave; |
3174 | 363 | } |
3175 | | |
3176 | 54.7k | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
3177 | 54.7k | if (!pk->seckey_info) |
3178 | 0 | { |
3179 | 0 | err = gpg_error_from_syserror (); |
3180 | 0 | goto leave; |
3181 | 0 | } |
3182 | | |
3183 | 54.7k | ski->algo = iobuf_get_noeof (inp); |
3184 | 54.7k | pktlen--; |
3185 | | |
3186 | 54.7k | if (is_v5 || (is_v6 && ski->algo)) |
3187 | 2.96k | { |
3188 | 2.96k | 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 | 2.96k | if (!pktlen) |
3193 | 363 | { |
3194 | 363 | err = gpg_error (GPG_ERR_INV_PACKET); |
3195 | 363 | goto leave; |
3196 | 363 | } |
3197 | 2.60k | protcount = iobuf_get_noeof (inp); |
3198 | 2.60k | pktlen--; |
3199 | 2.60k | if (list_mode) |
3200 | 2.60k | es_fprintf (listfp, "\tprotbytes: %u\n", protcount); |
3201 | 2.60k | } |
3202 | | |
3203 | 54.3k | if (ski->algo) |
3204 | 49.6k | { |
3205 | 49.6k | ski->is_protected = 1; |
3206 | 49.6k | ski->s2k.count = 0; |
3207 | 49.6k | if (ski->algo == 253) |
3208 | 222 | { |
3209 | 222 | if (list_mode) |
3210 | 222 | es_fprintf (listfp, |
3211 | 0 | "\tS2K pseudo algo %d is not yet supported\n", |
3212 | 0 | ski->algo); |
3213 | 222 | err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
3214 | 222 | goto leave; |
3215 | 222 | } |
3216 | 49.4k | else if (ski->algo == 254 || ski->algo == 255) |
3217 | 41.4k | { |
3218 | 41.4k | if (pktlen < 3) |
3219 | 1.24k | { |
3220 | 1.24k | err = gpg_error (GPG_ERR_INV_PACKET); |
3221 | 1.24k | goto leave; |
3222 | 1.24k | } |
3223 | | |
3224 | 40.1k | ski->sha1chk = (ski->algo == 254); |
3225 | 40.1k | ski->algo = iobuf_get_noeof (inp); |
3226 | 40.1k | 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 | 40.1k | ski->s2k.mode = iobuf_get_noeof (inp); |
3231 | 40.1k | pktlen--; |
3232 | 40.1k | ski->s2k.hash_algo = iobuf_get_noeof (inp); |
3233 | 40.1k | pktlen--; |
3234 | | /* Check for the special GNU extension. */ |
3235 | 40.1k | if (ski->s2k.mode == 101) |
3236 | 30.9k | { |
3237 | 152k | for (i = 0; i < 4 && pktlen; i++, pktlen--) |
3238 | 121k | temp[i] = iobuf_get_noeof (inp); |
3239 | 30.9k | if (i < 4 || memcmp (temp, "GNU", 3)) |
3240 | 2.47k | { |
3241 | 2.47k | if (list_mode) |
3242 | 2.47k | es_fprintf (listfp, "\tunknown S2K %d\n", |
3243 | 0 | ski->s2k.mode); |
3244 | 2.47k | err = gpg_error (GPG_ERR_INV_PACKET); |
3245 | 2.47k | goto leave; |
3246 | 2.47k | } |
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 | 28.4k | ski->s2k.mode = 1000 + temp[3]; |
3252 | 28.4k | } |
3253 | | |
3254 | | /* Read the salt. */ |
3255 | 37.7k | if (ski->s2k.mode == 3 || ski->s2k.mode == 1) |
3256 | 5.51k | { |
3257 | 46.7k | for (i = 0; i < 8 && pktlen; i++, pktlen--) |
3258 | 41.2k | temp[i] = iobuf_get_noeof (inp); |
3259 | 5.51k | if (i < 8) |
3260 | 359 | { |
3261 | 359 | err = gpg_error (GPG_ERR_INV_PACKET); |
3262 | 359 | goto leave; |
3263 | 359 | } |
3264 | 5.15k | memcpy (ski->s2k.salt, temp, 8); |
3265 | 5.15k | } |
3266 | | |
3267 | | /* Check the mode. */ |
3268 | 37.3k | switch (ski->s2k.mode) |
3269 | 37.3k | { |
3270 | 1.19k | case 0: |
3271 | 1.19k | if (list_mode) |
3272 | 1.19k | es_fprintf (listfp, "\tsimple S2K"); |
3273 | 1.19k | break; |
3274 | 999 | case 1: |
3275 | 999 | if (list_mode) |
3276 | 999 | es_fprintf (listfp, "\tsalted S2K"); |
3277 | 999 | break; |
3278 | 4.15k | case 3: |
3279 | 4.15k | if (list_mode) |
3280 | 4.15k | es_fprintf (listfp, "\titer+salt S2K"); |
3281 | 4.15k | break; |
3282 | 1.32k | case 1001: |
3283 | 1.32k | if (list_mode) |
3284 | 1.32k | es_fprintf (listfp, "\tgnu-dummy"); |
3285 | 1.32k | break; |
3286 | 19.9k | case 1002: |
3287 | 19.9k | if (list_mode) |
3288 | 19.9k | es_fprintf (listfp, "\tgnu-divert-to-card"); |
3289 | 19.9k | break; |
3290 | 3.00k | case 1003: |
3291 | 3.00k | if (list_mode) |
3292 | 3.00k | es_fprintf (listfp, "\tgnu-mode1003"); |
3293 | 3.00k | break; |
3294 | 6.69k | default: |
3295 | 6.69k | if (list_mode) |
3296 | 6.69k | es_fprintf (listfp, "\tunknown %sS2K %d\n", |
3297 | 0 | ski->s2k.mode < 1000 ? "" : "GNU ", |
3298 | 0 | ski->s2k.mode); |
3299 | 6.69k | err = gpg_error (GPG_ERR_INV_PACKET); |
3300 | 6.69k | goto leave; |
3301 | 37.3k | } |
3302 | | |
3303 | | /* Print some info. */ |
3304 | 30.6k | 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 | 30.6k | if (list_mode) |
3317 | 30.6k | es_putc ('\n', listfp); |
3318 | | |
3319 | | /* Read remaining protection parameters. */ |
3320 | 30.6k | if (ski->s2k.mode == 3) |
3321 | 4.15k | { |
3322 | 4.15k | if (pktlen < 1) |
3323 | 840 | { |
3324 | 840 | err = gpg_error (GPG_ERR_INV_PACKET); |
3325 | 840 | goto leave; |
3326 | 840 | } |
3327 | 3.31k | ski->s2k.count = iobuf_get_noeof (inp); |
3328 | 3.31k | pktlen--; |
3329 | 3.31k | if (list_mode) |
3330 | 3.31k | 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 | 3.31k | } |
3334 | 26.4k | else if (ski->s2k.mode == 1002) |
3335 | 19.9k | { |
3336 | | /* Read the serial number. */ |
3337 | 19.9k | if (pktlen < 1) |
3338 | 5.10k | { |
3339 | 5.10k | err = gpg_error (GPG_ERR_INV_PACKET); |
3340 | 5.10k | goto leave; |
3341 | 5.10k | } |
3342 | 14.8k | snlen = iobuf_get (inp); |
3343 | 14.8k | pktlen--; |
3344 | 14.8k | if (pktlen < snlen || snlen == (size_t)(-1)) |
3345 | 2.37k | { |
3346 | 2.37k | err = gpg_error (GPG_ERR_INV_PACKET); |
3347 | 2.37k | goto leave; |
3348 | 2.37k | } |
3349 | 14.8k | } |
3350 | 30.6k | } |
3351 | 8.02k | else /* Old version; no S2K, so we set mode to 0, hash MD5. */ |
3352 | 8.02k | { |
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 | 8.02k | ski->s2k.mode = 0; |
3357 | 8.02k | ski->s2k.hash_algo = DIGEST_ALGO_MD5; |
3358 | 8.02k | if (list_mode) |
3359 | 8.02k | es_fprintf (listfp, "\tprotect algo: %d (hash algo: %d)\n", |
3360 | 0 | ski->algo, ski->s2k.hash_algo); |
3361 | 8.02k | } |
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 | 30.3k | ski->ivlen = openpgp_cipher_blocklen (ski->algo); |
3373 | 30.3k | log_assert (ski->ivlen <= sizeof (temp)); |
3374 | | |
3375 | 30.3k | if (ski->s2k.mode == 1001 || ski->s2k.mode == 1003) |
3376 | 4.32k | ski->ivlen = 0; |
3377 | 26.0k | else if (ski->s2k.mode == 1002) |
3378 | 12.4k | ski->ivlen = snlen < 16 ? snlen : 16; |
3379 | | |
3380 | 30.3k | if (pktlen < ski->ivlen) |
3381 | 1.75k | { |
3382 | 1.75k | err = gpg_error (GPG_ERR_INV_PACKET); |
3383 | 1.75k | goto leave; |
3384 | 1.75k | } |
3385 | 183k | for (i = 0; i < ski->ivlen; i++, pktlen--) |
3386 | 155k | temp[i] = iobuf_get_noeof (inp); |
3387 | 28.6k | 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 | 28.6k | memcpy (ski->iv, temp, ski->ivlen); |
3397 | 28.6k | } |
3398 | | |
3399 | | /* Skip count of secret key material. */ |
3400 | 33.3k | if (is_v5) |
3401 | 2.23k | { |
3402 | 2.23k | if (pktlen < 4) |
3403 | 440 | { |
3404 | 440 | err = gpg_error (GPG_ERR_INV_PACKET); |
3405 | 440 | goto leave; |
3406 | 440 | } |
3407 | 1.79k | skbytes = read_32 (inp); |
3408 | 1.79k | pktlen -= 4; |
3409 | 1.79k | if (list_mode) |
3410 | 1.79k | es_fprintf (listfp, "\tskbytes: %u\n", skbytes); |
3411 | 1.79k | } |
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 | 32.8k | if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002) |
3418 | 13.8k | { |
3419 | | /* Better set some dummy stuff here. */ |
3420 | 13.8k | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3421 | 13.8k | xstrdup ("dummydata"), |
3422 | 13.8k | 10 * 8); |
3423 | 13.8k | pktlen = 0; |
3424 | 13.8k | } |
3425 | 19.0k | else if (ski->s2k.mode == 1003) |
3426 | 3.00k | { |
3427 | 3.00k | void *tmpp; |
3428 | | |
3429 | 3.00k | if (pktlen < 2) /* At least two bytes for parenthesis. */ |
3430 | 1.41k | { |
3431 | 1.41k | err = gpg_error (GPG_ERR_INV_PACKET); |
3432 | 1.41k | goto leave; |
3433 | 1.41k | } |
3434 | | |
3435 | 1.58k | tmpp = read_rest (inp, pktlen); |
3436 | 1.58k | 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 | 1.58k | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3452 | 1.58k | tmpp, tmpp? pktlen * 8 : 0); |
3453 | 1.58k | pktlen = 0; |
3454 | 1.58k | } |
3455 | 16.0k | else if (ski->is_protected) |
3456 | 11.7k | { |
3457 | 11.7k | void *tmpp; |
3458 | | |
3459 | 11.7k | if (pktlen < 2) /* At least two bytes for the length. */ |
3460 | 2.39k | { |
3461 | 2.39k | err = gpg_error (GPG_ERR_INV_PACKET); |
3462 | 2.39k | goto leave; |
3463 | 2.39k | } |
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 | 9.39k | tmpp = read_rest (inp, pktlen); |
3471 | 9.39k | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, |
3472 | 9.39k | 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 | 9.39k | if (pk->pkey[npkey]) |
3477 | 9.39k | gcry_mpi_set_flag (pk->pkey[npkey], GCRYMPI_FLAG_USER1); |
3478 | 9.39k | pktlen = 0; |
3479 | 9.39k | if (list_mode) |
3480 | 9.39k | es_fprintf (listfp, "\tskey[%d]: [v4 protected]\n", npkey); |
3481 | 9.39k | } |
3482 | 4.25k | else |
3483 | 4.25k | { |
3484 | | /* Not encrypted. */ |
3485 | 12.0k | for (i = npkey; i < nskey; i++) |
3486 | 8.39k | { |
3487 | 8.39k | if (pktlen < 2) /* At least two bytes for the length. */ |
3488 | 642 | { |
3489 | 642 | err = gpg_error (GPG_ERR_INV_PACKET); |
3490 | 642 | goto leave; |
3491 | 642 | } |
3492 | 7.75k | if (algorithm == PUBKEY_ALGO_KYBER && i == npkey+1) |
3493 | 4 | { |
3494 | 4 | err = read_sos_octet_string (inp, &pktlen, 4, 0, |
3495 | 4 | 1, pk->pkey+i); |
3496 | 4 | if (err) |
3497 | 2 | goto leave; |
3498 | 4 | } |
3499 | 7.75k | 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 | 7.75k | 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 | 7.75k | else if ((algorithm == PUBKEY_ALGO_MLK768_NP384 |
3511 | 7.75k | || 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 | 7.75k | 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 | 7.75k | 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 | 7.75k | 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 | 7.75k | 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 | 7.75k | 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 | 7.75k | else |
3555 | 7.75k | { |
3556 | 7.75k | unsigned int n = pktlen; |
3557 | | |
3558 | 7.75k | if (algorithm == PUBKEY_ALGO_ECDSA |
3559 | 7.09k | || algorithm == PUBKEY_ALGO_EDDSA |
3560 | 6.56k | || algorithm == PUBKEY_ALGO_ECDH |
3561 | 6.21k | || algorithm == PUBKEY_ALGO_KYBER) |
3562 | 1.53k | pk->pkey[i] = sos_read (inp, &n, 0); |
3563 | 6.21k | else |
3564 | 6.21k | pk->pkey[i] = mpi_read (inp, &n, 0); |
3565 | 7.75k | pktlen -= n; |
3566 | 7.75k | } |
3567 | | |
3568 | 7.75k | 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 | 7.75k | if (!pk->pkey[i]) |
3576 | 2.88k | err = gpg_error (GPG_ERR_INV_PACKET); |
3577 | 7.75k | } |
3578 | 3.61k | if (err) |
3579 | 1.22k | goto leave; |
3580 | | |
3581 | 2.38k | if (!is_v6) |
3582 | 2.38k | { |
3583 | 2.38k | if (pktlen < 2) |
3584 | 81 | { |
3585 | 81 | if (opt.verbose) |
3586 | 81 | log_info ("checksum is missing (remaining bytes: %lu)\n", |
3587 | 0 | pktlen); |
3588 | 81 | err = gpg_error (GPG_ERR_INV_PACKET); |
3589 | 81 | goto leave; |
3590 | 81 | } |
3591 | 2.30k | ski->csum = read_16 (inp); |
3592 | 2.30k | pktlen -= 2; |
3593 | 2.30k | if (list_mode) |
3594 | 2.30k | es_fprintf (listfp, "\tchecksum: %04hx\n", ski->csum); |
3595 | 2.30k | } |
3596 | 2.38k | } |
3597 | 32.8k | } |
3598 | | |
3599 | | /* Note that KEYID below has been initialized above in list_mode. */ |
3600 | 1.92M | if (list_mode) |
3601 | 1.92M | es_fprintf (listfp, "\tkeyid: %08lX%08lX\n", |
3602 | 0 | (ulong) keyid[0], (ulong) keyid[1]); |
3603 | 1.92M | 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 | 2.04M | leave: |
3620 | 2.04M | iobuf_skip_rest (inp, pktlen, 0); |
3621 | 2.04M | return err; |
3622 | 1.92M | } |
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 | 37.5k | { |
3631 | 37.5k | size_t n; |
3632 | 37.5k | int count = 0; |
3633 | 37.5k | struct user_attribute *attribs = NULL; |
3634 | 37.5k | const byte *buffer = uid->attrib_data; |
3635 | 37.5k | int buflen = uid->attrib_len; |
3636 | 37.5k | byte type; |
3637 | | |
3638 | 37.5k | xfree (uid->attribs); |
3639 | | |
3640 | 58.1k | while (buflen) |
3641 | 31.7k | { |
3642 | 31.7k | n = *buffer++; |
3643 | 31.7k | buflen--; |
3644 | 31.7k | if (n == 255) /* 4 byte length header. */ |
3645 | 3.00k | { |
3646 | 3.00k | if (buflen < 4) |
3647 | 579 | goto too_short; |
3648 | 2.42k | n = buf32_to_size_t (buffer); |
3649 | 2.42k | buffer += 4; |
3650 | 2.42k | buflen -= 4; |
3651 | 2.42k | } |
3652 | 28.7k | else if (n >= 192) /* 2 byte special encoded length header. */ |
3653 | 5.19k | { |
3654 | 5.19k | if (buflen < 2) |
3655 | 3.84k | goto too_short; |
3656 | 1.35k | n = ((n - 192) << 8) + *buffer + 192; |
3657 | 1.35k | buffer++; |
3658 | 1.35k | buflen--; |
3659 | 1.35k | } |
3660 | 27.3k | if (buflen < n) |
3661 | 4.69k | goto too_short; |
3662 | | |
3663 | 22.6k | if (!n) |
3664 | 2.05k | { |
3665 | | /* Too short to encode the subpacket type. */ |
3666 | 2.05k | if (opt.verbose) |
3667 | 2.05k | log_info ("attribute subpacket too short\n"); |
3668 | 2.05k | break; |
3669 | 2.05k | } |
3670 | | |
3671 | 20.5k | attribs = xrealloc (attribs, |
3672 | 20.5k | (count + 1) * sizeof (struct user_attribute)); |
3673 | 20.5k | memset (&attribs[count], 0, sizeof (struct user_attribute)); |
3674 | | |
3675 | 20.5k | type = *buffer; |
3676 | 20.5k | buffer++; |
3677 | 20.5k | buflen--; |
3678 | 20.5k | n--; |
3679 | | |
3680 | 20.5k | attribs[count].type = type; |
3681 | 20.5k | attribs[count].data = buffer; |
3682 | 20.5k | attribs[count].len = n; |
3683 | 20.5k | buffer += n; |
3684 | 20.5k | buflen -= n; |
3685 | 20.5k | count++; |
3686 | 20.5k | } |
3687 | | |
3688 | 28.4k | uid->attribs = attribs; |
3689 | 28.4k | uid->numattribs = count; |
3690 | 28.4k | return count; |
3691 | | |
3692 | 9.11k | too_short: |
3693 | 9.11k | if (opt.verbose && !glo_ctrl.silence_parse_warnings) |
3694 | 9.11k | log_info ("buffer shorter than attribute subpacket\n"); |
3695 | 9.11k | uid->attribs = attribs; |
3696 | 9.11k | uid->numattribs = count; |
3697 | 9.11k | return count; |
3698 | 37.5k | } |
3699 | | |
3700 | | |
3701 | | static int |
3702 | | parse_user_id (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet) |
3703 | 166k | { |
3704 | 166k | 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 | 166k | if (pktlen > MAX_UID_PACKET_LENGTH) |
3714 | 1.22k | { |
3715 | 1.22k | log_error ("packet(%d) too large\n", pkttype); |
3716 | 1.22k | if (list_mode) |
3717 | 1.22k | es_fprintf (listfp, ":user ID packet: [too large]\n"); |
3718 | 1.22k | iobuf_skip_rest (inp, pktlen, 0); |
3719 | 1.22k | return GPG_ERR_INV_PACKET; |
3720 | 1.22k | } |
3721 | | |
3722 | 165k | packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id + pktlen); |
3723 | 165k | packet->pkt.user_id->len = pktlen; |
3724 | 165k | packet->pkt.user_id->ref = 1; |
3725 | | |
3726 | 165k | p = packet->pkt.user_id->name; |
3727 | 1.15M | for (; pktlen; pktlen--, p++) |
3728 | 985k | *p = iobuf_get_noeof (inp); |
3729 | 165k | *p = 0; |
3730 | | |
3731 | 165k | 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 | 165k | return 0; |
3746 | 166k | } |
3747 | | |
3748 | | |
3749 | | void |
3750 | | make_attribute_uidname (PKT_user_id * uid, size_t max_namelen) |
3751 | 37.5k | { |
3752 | 37.5k | log_assert (max_namelen > 70); |
3753 | 37.5k | if (uid->numattribs <= 0) |
3754 | 20.5k | sprintf (uid->name, "[bad attribute packet of size %lu]", |
3755 | 20.5k | uid->attrib_len); |
3756 | 17.0k | else if (uid->numattribs > 1) |
3757 | 800 | sprintf (uid->name, "[%d attributes of size %lu]", |
3758 | 800 | uid->numattribs, uid->attrib_len); |
3759 | 16.2k | else |
3760 | 16.2k | { |
3761 | | /* Only one attribute, so list it as the "user id" */ |
3762 | | |
3763 | 16.2k | if (uid->attribs->type == ATTRIB_IMAGE) |
3764 | 8.73k | { |
3765 | 8.73k | u32 len; |
3766 | 8.73k | byte type; |
3767 | | |
3768 | 8.73k | if (parse_image_header (uid->attribs, &type, &len)) |
3769 | 5.36k | sprintf (uid->name, "[%.20s image of size %lu]", |
3770 | 5.36k | image_type_to_string (type, 1), (ulong) len); |
3771 | 3.37k | else |
3772 | 3.37k | sprintf (uid->name, "[invalid image]"); |
3773 | 8.73k | } |
3774 | 7.48k | else |
3775 | 7.48k | sprintf (uid->name, "[unknown attribute of size %lu]", |
3776 | 7.48k | (ulong) uid->attribs->len); |
3777 | 16.2k | } |
3778 | | |
3779 | 37.5k | uid->len = strlen (uid->name); |
3780 | 37.5k | } |
3781 | | |
3782 | | |
3783 | | static int |
3784 | | parse_attribute (IOBUF inp, int pkttype, unsigned long pktlen, |
3785 | | PACKET * packet) |
3786 | 37.7k | { |
3787 | 37.7k | byte *p; |
3788 | | |
3789 | 37.7k | (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 | 37.7k | if (pktlen > MAX_ATTR_PACKET_LENGTH) |
3795 | 251 | { |
3796 | 251 | log_error ("packet(%d) too large\n", pkttype); |
3797 | 251 | if (list_mode) |
3798 | 251 | es_fprintf (listfp, ":attribute packet: [too large]\n"); |
3799 | 251 | iobuf_skip_rest (inp, pktlen, 0); |
3800 | 251 | return GPG_ERR_INV_PACKET; |
3801 | 251 | } |
3802 | | |
3803 | 37.5k | #define EXTRA_UID_NAME_SPACE 71 |
3804 | 37.5k | packet->pkt.user_id = xmalloc_clear (sizeof *packet->pkt.user_id |
3805 | 37.5k | + EXTRA_UID_NAME_SPACE); |
3806 | 37.5k | packet->pkt.user_id->ref = 1; |
3807 | 37.5k | packet->pkt.user_id->attrib_data = xmalloc (pktlen? pktlen:1); |
3808 | 37.5k | packet->pkt.user_id->attrib_len = pktlen; |
3809 | | |
3810 | 37.5k | p = packet->pkt.user_id->attrib_data; |
3811 | 1.81G | for (; pktlen; pktlen--, p++) |
3812 | 1.81G | *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 | 37.5k | parse_attribute_subpkts (packet->pkt.user_id); |
3818 | | |
3819 | 37.5k | make_attribute_uidname (packet->pkt.user_id, EXTRA_UID_NAME_SPACE); |
3820 | | |
3821 | 37.5k | if (list_mode) |
3822 | 0 | { |
3823 | 0 | es_fprintf (listfp, ":attribute packet: %s\n", packet->pkt.user_id->name); |
3824 | 0 | } |
3825 | 37.5k | return 0; |
3826 | 37.7k | } |
3827 | | |
3828 | | |
3829 | | static int |
3830 | | parse_comment (IOBUF inp, int pkttype, unsigned long pktlen, PACKET * packet) |
3831 | 14.4k | { |
3832 | 14.4k | 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 | 14.4k | if (pktlen > MAX_COMMENT_PACKET_LENGTH) |
3839 | 451 | { |
3840 | 451 | log_error ("packet(%d) too large\n", pkttype); |
3841 | 451 | if (list_mode) |
3842 | 451 | es_fprintf (listfp, ":%scomment packet: [too large]\n", |
3843 | 0 | pkttype == PKT_OLD_COMMENT ? "OpenPGP draft " : ""); |
3844 | 451 | iobuf_skip_rest (inp, pktlen, 0); |
3845 | 451 | return GPG_ERR_INV_PACKET; |
3846 | 451 | } |
3847 | 14.0k | packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1); |
3848 | 14.0k | packet->pkt.comment->len = pktlen; |
3849 | 14.0k | p = packet->pkt.comment->data; |
3850 | 2.14M | for (; pktlen; pktlen--, p++) |
3851 | 2.12M | *p = iobuf_get_noeof (inp); |
3852 | | |
3853 | 14.0k | 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 | 14.0k | return 0; |
3868 | 14.4k | } |
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 | 29.0M | { |
3878 | 29.0M | gpg_error_t err; |
3879 | 29.0M | iobuf_t inp = ctx->inp; |
3880 | 29.0M | PKT_ring_trust rt = {0}; |
3881 | 29.0M | int c; |
3882 | 29.0M | int not_gpg = 0; |
3883 | | |
3884 | 29.0M | if (!pktlen) |
3885 | 6.12k | { |
3886 | 6.12k | if (list_mode) |
3887 | 6.12k | es_fprintf (listfp, ":trust packet: empty\n"); |
3888 | 6.12k | err = 0; |
3889 | 6.12k | goto leave; |
3890 | 6.12k | } |
3891 | | |
3892 | 29.0M | c = iobuf_get_noeof (inp); |
3893 | 29.0M | pktlen--; |
3894 | 29.0M | rt.trustval = c; |
3895 | 29.0M | if (pktlen) |
3896 | 29.0M | { |
3897 | 29.0M | if (!c) |
3898 | 28.9M | { |
3899 | 28.9M | c = iobuf_get_noeof (inp); |
3900 | | /* We require that bit 7 of the sigcache is 0 (easier |
3901 | | * eof handling). */ |
3902 | 28.9M | if (!(c & 0x80)) |
3903 | 28.9M | rt.sigcache = c; |
3904 | 28.9M | } |
3905 | 62.5k | else |
3906 | 62.5k | iobuf_get_noeof (inp); /* Dummy read. */ |
3907 | 29.0M | pktlen--; |
3908 | 29.0M | } |
3909 | | |
3910 | | /* Next is the optional subtype. */ |
3911 | 29.0M | if (pktlen > 3) |
3912 | 29.0M | { |
3913 | 29.0M | char tmp[4]; |
3914 | 29.0M | tmp[0] = iobuf_get_noeof (inp); |
3915 | 29.0M | tmp[1] = iobuf_get_noeof (inp); |
3916 | 29.0M | tmp[2] = iobuf_get_noeof (inp); |
3917 | 29.0M | tmp[3] = iobuf_get_noeof (inp); |
3918 | 29.0M | pktlen -= 4; |
3919 | 29.0M | if (!memcmp (tmp, "gpg", 3)) |
3920 | 28.9M | rt.subtype = tmp[3]; |
3921 | 58.1k | else |
3922 | 58.1k | not_gpg = 1; |
3923 | 29.0M | } |
3924 | | /* If it is a key or uid subtype read the remaining data. */ |
3925 | 29.0M | if ((rt.subtype == RING_TRUST_KEY || rt.subtype == RING_TRUST_UID) |
3926 | 69.9k | && pktlen >= 6 ) |
3927 | 62.3k | { |
3928 | 62.3k | int i; |
3929 | 62.3k | unsigned int namelen; |
3930 | | |
3931 | 62.3k | rt.keyorg = iobuf_get_noeof (inp); |
3932 | 62.3k | pktlen--; |
3933 | 62.3k | rt.keyupdate = read_32 (inp); |
3934 | 62.3k | pktlen -= 4; |
3935 | 62.3k | namelen = iobuf_get_noeof (inp); |
3936 | 62.3k | pktlen--; |
3937 | 62.3k | if (namelen && pktlen) |
3938 | 15.5k | { |
3939 | 15.5k | rt.url = xtrymalloc (namelen + 1); |
3940 | 15.5k | if (!rt.url) |
3941 | 0 | { |
3942 | 0 | err = gpg_error_from_syserror (); |
3943 | 0 | goto leave; |
3944 | 0 | } |
3945 | 157k | for (i = 0; pktlen && i < namelen; pktlen--, i++) |
3946 | 141k | rt.url[i] = iobuf_get_noeof (inp); |
3947 | 15.5k | rt.url[i] = 0; |
3948 | 15.5k | } |
3949 | 62.3k | } |
3950 | | |
3951 | 29.0M | 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 | 29.0M | if (!ctx->last_pkt.pkt.generic || ctx->skip_meta) |
3987 | 28.3k | ; |
3988 | 29.0M | else if (rt.subtype == RING_TRUST_SIG |
3989 | 28.9M | && ctx->last_pkt.pkttype == PKT_SIGNATURE) |
3990 | 28.9M | { |
3991 | 28.9M | PKT_signature *sig = ctx->last_pkt.pkt.signature; |
3992 | | |
3993 | 28.9M | if ((rt.sigcache & 1)) |
3994 | 15.7M | { |
3995 | 15.7M | sig->flags.checked = 1; |
3996 | 15.7M | sig->flags.valid = !!(rt.sigcache & 2); |
3997 | 15.7M | } |
3998 | 28.9M | } |
3999 | 65.7k | else if (rt.subtype == RING_TRUST_UID |
4000 | 36.2k | && (ctx->last_pkt.pkttype == PKT_USER_ID |
4001 | 7.94k | || ctx->last_pkt.pkttype == PKT_ATTRIBUTE)) |
4002 | 28.3k | { |
4003 | 28.3k | PKT_user_id *uid = ctx->last_pkt.pkt.user_id; |
4004 | | |
4005 | 28.3k | uid->keyorg = rt.keyorg; |
4006 | 28.3k | uid->keyupdate = rt.keyupdate; |
4007 | 28.3k | uid->updateurl = rt.url; |
4008 | 28.3k | rt.url = NULL; |
4009 | 28.3k | } |
4010 | 37.3k | else if (rt.subtype == RING_TRUST_KEY |
4011 | 27.7k | && (ctx->last_pkt.pkttype == PKT_PUBLIC_KEY |
4012 | 12.3k | || ctx->last_pkt.pkttype == PKT_SECRET_KEY)) |
4013 | 21.9k | { |
4014 | 21.9k | PKT_public_key *pk = ctx->last_pkt.pkt.public_key; |
4015 | | |
4016 | 21.9k | pk->keyorg = rt.keyorg; |
4017 | 21.9k | pk->keyupdate = rt.keyupdate; |
4018 | 21.9k | pk->updateurl = rt.url; |
4019 | 21.9k | rt.url = NULL; |
4020 | 21.9k | } |
4021 | | |
4022 | 29.0M | err = 0; |
4023 | | |
4024 | 29.0M | leave: |
4025 | 29.0M | xfree (rt.url); |
4026 | 29.0M | free_packet (NULL, ctx); /* This sets ctx->last_pkt to NULL. */ |
4027 | 29.0M | iobuf_skip_rest (inp, pktlen, 0); |
4028 | 29.0M | return err; |
4029 | 29.0M | } |
4030 | | |
4031 | | |
4032 | | static int |
4033 | | parse_plaintext (IOBUF inp, int pkttype, unsigned long pktlen, |
4034 | | PACKET * pkt, int new_ctb, int partial) |
4035 | 134k | { |
4036 | 134k | int rc = 0; |
4037 | 134k | int mode, namelen; |
4038 | 134k | PKT_plaintext *pt; |
4039 | 134k | byte *p; |
4040 | 134k | int c, i; |
4041 | | |
4042 | 134k | if (!partial && pktlen < 6) |
4043 | 5.77k | { |
4044 | 5.77k | log_error ("packet(%d) too short (%lu)\n", pkttype, (ulong) pktlen); |
4045 | 5.77k | if (list_mode) |
4046 | 5.77k | es_fputs (":literal data packet: [too short]\n", listfp); |
4047 | 5.77k | rc = gpg_error (GPG_ERR_INV_PACKET); |
4048 | 5.77k | goto leave; |
4049 | 5.77k | } |
4050 | 128k | mode = iobuf_get_noeof (inp); |
4051 | 128k | if (pktlen) |
4052 | 113k | pktlen--; |
4053 | 128k | namelen = iobuf_get_noeof (inp); |
4054 | 128k | if (pktlen) |
4055 | 113k | pktlen--; |
4056 | | /* Note that namelen will never exceed 255 bytes. */ |
4057 | 128k | pt = pkt->pkt.plaintext = |
4058 | 128k | xmalloc (sizeof *pkt->pkt.plaintext + namelen - 1); |
4059 | 128k | pt->new_ctb = new_ctb; |
4060 | 128k | pt->mode = mode; |
4061 | 128k | pt->namelen = namelen; |
4062 | 128k | pt->is_partial = partial; |
4063 | 128k | if (pktlen) |
4064 | 113k | { |
4065 | 580k | for (i = 0; pktlen > 4 && i < namelen; pktlen--, i++) |
4066 | 467k | pt->name[i] = iobuf_get_noeof (inp); |
4067 | 113k | } |
4068 | 14.7k | else |
4069 | 14.7k | { |
4070 | 65.9k | for (i = 0; i < namelen; i++) |
4071 | 52.6k | if ((c = iobuf_get (inp)) == -1) |
4072 | 1.48k | break; |
4073 | 51.2k | else |
4074 | 51.2k | pt->name[i] = c; |
4075 | 14.7k | } |
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 | 6.16M | for (; i < namelen; i++) |
4080 | 6.03M | pt->name[i] = 0; |
4081 | | |
4082 | 128k | pt->timestamp = read_32 (inp); |
4083 | 128k | if (pktlen) |
4084 | 113k | pktlen -= 4; |
4085 | 128k | pt->len = pktlen; |
4086 | 128k | pt->buf = inp; |
4087 | | |
4088 | 128k | 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 | 134k | leave: |
4109 | 134k | return rc; |
4110 | 128k | } |
4111 | | |
4112 | | |
4113 | | static int |
4114 | | parse_compressed (IOBUF inp, int pkttype, unsigned long pktlen, |
4115 | | PACKET * pkt, int new_ctb) |
4116 | 482k | { |
4117 | 482k | 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 | 482k | (void) pkttype; |
4123 | 482k | (void) pktlen; |
4124 | | |
4125 | 482k | zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed); |
4126 | 482k | zd->algorithm = iobuf_get_noeof (inp); |
4127 | 482k | zd->len = 0; /* not used */ |
4128 | 482k | zd->new_ctb = new_ctb; |
4129 | 482k | zd->buf = inp; |
4130 | 482k | if (list_mode) |
4131 | 482k | es_fprintf (listfp, ":compressed packet: algo=%d\n", zd->algorithm); |
4132 | 482k | return 0; |
4133 | 482k | } |
4134 | | |
4135 | | |
4136 | | static int |
4137 | | parse_encrypted (IOBUF inp, int pkttype, unsigned long pktlen, |
4138 | | PACKET * pkt, int new_ctb, int partial) |
4139 | 54.7k | { |
4140 | 54.7k | int rc = 0; |
4141 | 54.7k | PKT_encrypted *ed; |
4142 | 54.7k | unsigned long orig_pktlen = pktlen; |
4143 | | |
4144 | 54.7k | ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted); |
4145 | | /* ed->len is set below. */ |
4146 | 54.7k | ed->extralen = 0; /* Unknown here; only used in build_packet. */ |
4147 | 54.7k | ed->buf = NULL; |
4148 | 54.7k | ed->new_ctb = new_ctb; |
4149 | 54.7k | ed->is_partial = partial; |
4150 | 54.7k | ed->seipd = 0; |
4151 | 54.7k | ed->version = 0; |
4152 | 54.7k | ed->aead_algo = 0; |
4153 | 54.7k | ed->mdc_method = 0; |
4154 | 54.7k | ed->cipher_algo = 0; /* Only used with AEAD. */ |
4155 | 54.7k | ed->chunkbyte = 0; /* Only used with AEAD. */ |
4156 | 54.7k | if (pkttype == PKT_ENCRYPTED_MDC) |
4157 | 9.84k | { |
4158 | 9.84k | ed->seipd = 1; |
4159 | 9.84k | ed->version = iobuf_get_noeof (inp); |
4160 | 9.84k | if (orig_pktlen) |
4161 | 2.91k | pktlen--; |
4162 | 9.84k | if (ed->version == 1) |
4163 | 5.53k | ed->mdc_method = DIGEST_ALGO_SHA1; |
4164 | 4.31k | else if (ed->version == 2 && RFC9980) |
4165 | 17 | { |
4166 | 17 | ed->cipher_algo = iobuf_get_noeof (inp); |
4167 | 17 | if (orig_pktlen) |
4168 | 8 | pktlen--; |
4169 | 17 | ed->aead_algo = iobuf_get_noeof (inp); |
4170 | 17 | if (orig_pktlen) |
4171 | 8 | pktlen--; |
4172 | 17 | ed->chunkbyte = iobuf_get_noeof (inp); |
4173 | 17 | if (orig_pktlen) |
4174 | 8 | pktlen--; |
4175 | 17 | } |
4176 | 4.29k | else |
4177 | 4.29k | { |
4178 | 4.29k | log_error ("encrypted_mdc packet with unknown version %d\n", |
4179 | 4.29k | ed->version); |
4180 | 4.29k | if (list_mode) |
4181 | 4.29k | es_fputs (":encrypted data packet: [unknown version]\n", listfp); |
4182 | | /*skip_rest(inp, pktlen); should we really do this? */ |
4183 | 4.29k | rc = gpg_error (GPG_ERR_INV_PACKET); |
4184 | 4.29k | goto leave; |
4185 | 4.29k | } |
4186 | 9.84k | } |
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 | 50.4k | if (orig_pktlen && pktlen < 10) |
4192 | 588 | { |
4193 | | /* Actually this is blocksize+2. */ |
4194 | 588 | log_error ("packet(%d) too short\n", pkttype); |
4195 | 588 | if (list_mode) |
4196 | 588 | es_fputs (":encrypted data packet: [too short]\n", listfp); |
4197 | 588 | rc = GPG_ERR_INV_PACKET; |
4198 | 588 | iobuf_skip_rest (inp, pktlen, partial); |
4199 | 588 | goto leave; |
4200 | 588 | } |
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 | 49.8k | ed->len = pktlen; |
4206 | | |
4207 | 49.8k | 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 | 49.8k | ed->buf = inp; |
4223 | | |
4224 | 54.7k | leave: |
4225 | 54.7k | return rc; |
4226 | 49.8k | } |
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 | 11.8k | { |
4236 | 11.8k | int rc = 0; |
4237 | 11.8k | PKT_mdc *mdc; |
4238 | 11.8k | byte *p; |
4239 | | |
4240 | 11.8k | (void) pkttype; |
4241 | | |
4242 | 11.8k | mdc = pkt->pkt.mdc = xmalloc (sizeof *pkt->pkt.mdc); |
4243 | 11.8k | if (list_mode) |
4244 | 11.8k | es_fprintf (listfp, ":mdc packet: length=%lu\n", pktlen); |
4245 | 11.8k | if (!new_ctb || pktlen != 20) |
4246 | 4.65k | { |
4247 | 4.65k | log_error ("mdc_packet with invalid encoding\n"); |
4248 | 4.65k | rc = gpg_error (GPG_ERR_INV_PACKET); |
4249 | 4.65k | goto leave; |
4250 | 4.65k | } |
4251 | 7.21k | p = mdc->hash; |
4252 | 151k | for (; pktlen; pktlen--, p++) |
4253 | 144k | *p = iobuf_get_noeof (inp); |
4254 | | |
4255 | 11.8k | leave: |
4256 | 11.8k | return rc; |
4257 | 7.21k | } |
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 | 125k | { |
4265 | 125k | int rc = 0; |
4266 | 125k | PKT_encrypted *ed; |
4267 | 125k | unsigned long orig_pktlen = pktlen; |
4268 | | |
4269 | 125k | ed = pkt->pkt.encrypted = xtrymalloc (sizeof *pkt->pkt.encrypted); |
4270 | 125k | if (!ed) |
4271 | 0 | return gpg_error_from_syserror (); |
4272 | 125k | ed->len = 0; |
4273 | 125k | ed->extralen = 0; /* (only used in build_packet.) */ |
4274 | 125k | ed->buf = NULL; |
4275 | 125k | ed->new_ctb = 1; /* (packet number requires a new CTB anyway.) */ |
4276 | 125k | ed->is_partial = partial; |
4277 | 125k | ed->seipd = 0; |
4278 | 125k | ed->version = 0; |
4279 | 125k | 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 | 125k | if (orig_pktlen && pktlen < 19) |
4283 | 1.02k | { |
4284 | 1.02k | log_error ("packet(%d) too short\n", pkttype); |
4285 | 1.02k | if (list_mode) |
4286 | 1.02k | es_fputs (":ocb encrypted packet: [too short]\n", listfp); |
4287 | 1.02k | rc = gpg_error (GPG_ERR_INV_PACKET); |
4288 | 1.02k | iobuf_skip_rest (inp, pktlen, partial); |
4289 | 1.02k | goto leave; |
4290 | 1.02k | } |
4291 | | |
4292 | 124k | ed->version = iobuf_get_noeof (inp); |
4293 | 124k | if (orig_pktlen) |
4294 | 1.70k | pktlen--; |
4295 | 124k | if (ed->version != 1) |
4296 | 7.76k | { |
4297 | 7.76k | log_error ("ocb encrypted packet with unknown version %d\n", |
4298 | 7.76k | ed->version); |
4299 | 7.76k | if (list_mode) |
4300 | 7.76k | es_fputs (":ocb encrypted packet: [unknown version]\n", listfp); |
4301 | | /*skip_rest(inp, pktlen); should we really do this? */ |
4302 | 7.76k | rc = gpg_error (GPG_ERR_INV_PACKET); |
4303 | 7.76k | goto leave; |
4304 | 7.76k | } |
4305 | | |
4306 | 116k | ed->cipher_algo = iobuf_get_noeof (inp); |
4307 | 116k | if (orig_pktlen) |
4308 | 326 | pktlen--; |
4309 | 116k | ed->aead_algo = iobuf_get_noeof (inp); |
4310 | 116k | if (orig_pktlen) |
4311 | 326 | pktlen--; |
4312 | 116k | ed->chunkbyte = iobuf_get_noeof (inp); |
4313 | 116k | if (orig_pktlen) |
4314 | 326 | pktlen--; |
4315 | | |
4316 | | /* Store the remaining length of the encrypted data. We read the |
4317 | | * rest during decryption. */ |
4318 | 116k | ed->len = pktlen; |
4319 | | |
4320 | 116k | 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 | 116k | ed->buf = inp; |
4331 | | |
4332 | 125k | leave: |
4333 | 125k | return rc; |
4334 | 116k | } |
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 | 20.5k | { |
4352 | 20.5k | byte *p; |
4353 | 20.5k | const byte *sesmark; |
4354 | 20.5k | size_t sesmarklen; |
4355 | 20.5k | int i; |
4356 | | |
4357 | 20.5k | (void) pkttype; |
4358 | | |
4359 | 20.5k | if (list_mode) |
4360 | 20.5k | es_fprintf (listfp, ":packet 63: length %lu ", pktlen); |
4361 | | |
4362 | 20.5k | sesmark = get_session_marker (&sesmarklen); |
4363 | 20.5k | if (pktlen < sesmarklen + 1) /* 1 is for the control bytes */ |
4364 | 5.40k | goto skipit; |
4365 | 203k | for (i = 0; i < sesmarklen; i++, pktlen--) |
4366 | 191k | { |
4367 | 191k | if (sesmark[i] != iobuf_get_noeof (inp)) |
4368 | 3.44k | goto skipit; |
4369 | 191k | } |
4370 | 11.7k | if (pktlen > 4096) |
4371 | 1 | goto skipit; /* Definitely too large. We skip it to avoid an |
4372 | | overflow in the malloc. */ |
4373 | 11.7k | if (list_mode) |
4374 | 11.7k | es_fputs ("- gpg control packet", listfp); |
4375 | | |
4376 | 11.7k | packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control |
4377 | 11.7k | + pktlen - 1); |
4378 | 11.7k | packet->pkt.gpg_control->control = iobuf_get_noeof (inp); |
4379 | 11.7k | pktlen--; |
4380 | 11.7k | packet->pkt.gpg_control->datalen = pktlen; |
4381 | 11.7k | p = packet->pkt.gpg_control->data; |
4382 | 38.4k | for (; pktlen; pktlen--, p++) |
4383 | 26.7k | *p = iobuf_get_noeof (inp); |
4384 | | |
4385 | 11.7k | return 0; |
4386 | | |
4387 | 8.84k | skipit: |
4388 | 8.84k | 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 | 8.84k | iobuf_skip_rest (inp, pktlen, 0); |
4411 | 8.84k | return gpg_error (GPG_ERR_INV_PACKET); |
4412 | 11.7k | } |
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 | 125k | { |
4419 | 125k | PACKET *packet; |
4420 | 125k | byte *p; |
4421 | | |
4422 | 125k | if (!data) |
4423 | 0 | datalen = 0; |
4424 | | |
4425 | 125k | packet = xmalloc (sizeof *packet); |
4426 | 125k | init_packet (packet); |
4427 | 125k | packet->pkttype = PKT_GPG_CONTROL; |
4428 | 125k | packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control + datalen); |
4429 | 125k | packet->pkt.gpg_control->control = type; |
4430 | 125k | packet->pkt.gpg_control->datalen = datalen; |
4431 | 125k | p = packet->pkt.gpg_control->data; |
4432 | 7.27M | for (; datalen; datalen--, p++) |
4433 | 7.14M | *p = *data++; |
4434 | | |
4435 | 125k | return packet; |
4436 | 125k | } |