1 /* parse-packet.c - read packets
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 * 2007, 2009 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
38 static int mpi_print_mode
;
42 static int parse (IOBUF inp
, PACKET
* pkt
, int onlykeypkts
,
43 off_t
* retpos
, int *skip
, IOBUF out
, int do_skip
44 #ifdef DEBUG_PARSE_PACKET
45 , const char *dbg_w
, const char *dbg_f
, int dbg_l
48 static int copy_packet (IOBUF inp
, IOBUF out
, int pkttype
,
49 unsigned long pktlen
, int partial
);
50 static void skip_packet (IOBUF inp
, int pkttype
,
51 unsigned long pktlen
, int partial
);
52 static void *read_rest (IOBUF inp
, size_t pktlen
, int partial
);
53 static int parse_marker (IOBUF inp
, int pkttype
, unsigned long pktlen
);
54 static int parse_symkeyenc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
56 static int parse_pubkeyenc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
58 static int parse_onepass_sig (IOBUF inp
, int pkttype
, unsigned long pktlen
,
59 PKT_onepass_sig
* ops
);
60 static int parse_key (IOBUF inp
, int pkttype
, unsigned long pktlen
,
61 byte
* hdr
, int hdrlen
, PACKET
* packet
);
62 static int parse_user_id (IOBUF inp
, int pkttype
, unsigned long pktlen
,
64 static int parse_attribute (IOBUF inp
, int pkttype
, unsigned long pktlen
,
66 static int parse_comment (IOBUF inp
, int pkttype
, unsigned long pktlen
,
68 static void parse_trust (IOBUF inp
, int pkttype
, unsigned long pktlen
,
70 static int parse_plaintext (IOBUF inp
, int pkttype
, unsigned long pktlen
,
71 PACKET
* packet
, int new_ctb
, int partial
);
72 static int parse_compressed (IOBUF inp
, int pkttype
, unsigned long pktlen
,
73 PACKET
* packet
, int new_ctb
);
74 static int parse_encrypted (IOBUF inp
, int pkttype
, unsigned long pktlen
,
75 PACKET
* packet
, int new_ctb
, int partial
);
76 static int parse_mdc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
77 PACKET
* packet
, int new_ctb
);
78 static int parse_gpg_control (IOBUF inp
, int pkttype
, unsigned long pktlen
,
79 PACKET
* packet
, int partial
);
85 a
= iobuf_get_noeof (inp
) << 8;
86 a
|= iobuf_get_noeof (inp
);
95 a
= iobuf_get_noeof (inp
) << 24;
96 a
|= iobuf_get_noeof (inp
) << 16;
97 a
|= iobuf_get_noeof (inp
) << 8;
98 a
|= iobuf_get_noeof (inp
);
103 /* Read an external representation of an mpi and return the MPI. The
104 * external format is a 16 bit unsigned value stored in network byte
105 * order, giving the number of bits for the following integer. The
106 * integer is stored with MSB first (left padded with zeroes to align
107 * on a byte boundary). */
109 mpi_read (iobuf_t inp
, unsigned int *ret_nread
, int secure
)
111 /*FIXME: Needs to be synced with gnupg14/mpi/mpicoder.c */
114 unsigned int nbits
, nbytes
;
120 if ((c
= c1
= iobuf_get (inp
)) == -1)
123 if ((c
= c2
= iobuf_get (inp
)) == -1)
126 if (nbits
> MAX_EXTERN_MPI_BITS
)
128 log_error ("mpi too large (%u bits)\n", nbits
);
132 nbytes
= (nbits
+ 7) / 8;
133 buf
= secure
? gcry_xmalloc_secure (nbytes
+ 2) : gcry_xmalloc (nbytes
+ 2);
137 for (i
= 0; i
< nbytes
; i
++)
139 p
[i
+ 2] = iobuf_get (inp
) & 0xff;
143 if (nread
>= 2 && !(buf
[0] << 8 | buf
[1]))
145 /* Libgcrypt < 1.5.0 accidently rejects zero-length (i.e. zero)
146 MPIs. We fix this here. */
147 a
= gcry_mpi_new (0);
151 if (gcry_mpi_scan (&a
, GCRYMPI_FMT_PGP
, buf
, nread
, &nread
))
157 if (nread
> *ret_nread
)
158 log_bug ("mpi larger than packet");
166 set_packet_list_mode (int mode
)
170 /* FIXME(gcrypt) mpi_print_mode = DBG_MPI; */
171 /* We use stdout print only if invoked by the --list-packets command
172 but switch to stderr in all otehr cases. This breaks the
173 previous behaviour but that seems to be more of a bug than
174 intentional. I don't believe that any application makes use of
175 this long standing annoying way of printing to stdout except when
176 doing a --list-packets. If this assumption fails, it will be easy
177 to add an option for the listing stream. Note that we initialize
178 it only once; mainly because some code may switch the option
179 value later back to 1 and we want to have all output to the same
182 Using stderr is not actually very clean because it bypasses the
183 logging code but it is a special thing anyay. I am not sure
184 whether using log_stream() would be better. Perhaps we should
185 enable the list mdoe only with a special option. */
187 listfp
= opt
.list_packets
== 2 ? stdout
: stderr
;
193 unknown_pubkey_warning (int algo
)
195 static byte unknown_pubkey_algos
[256];
198 if (!unknown_pubkey_algos
[algo
])
201 log_info (_("can't handle public key algorithm %d\n"), algo
);
202 unknown_pubkey_algos
[algo
] = 1;
207 /* Parse a packet and return it in packet structure.
208 * Returns: 0 := valid packet in pkt
209 * -1 := no more packets
211 * Note: The function may return an error and a partly valid packet;
212 * caller must free this packet. */
213 #ifdef DEBUG_PARSE_PACKET
215 dbg_parse_packet (IOBUF inp
, PACKET
*pkt
, const char *dbg_f
, int dbg_l
)
221 rc
= parse (inp
, pkt
, 0, NULL
, &skip
, NULL
, 0, "parse", dbg_f
, dbg_l
);
226 #else /*!DEBUG_PARSE_PACKET*/
228 parse_packet (IOBUF inp
, PACKET
* pkt
)
234 rc
= parse (inp
, pkt
, 0, NULL
, &skip
, NULL
, 0);
239 #endif /*!DEBUG_PARSE_PACKET*/
243 * Like parse packet, but only return secret or public (sub)key
246 #ifdef DEBUG_PARSE_PACKET
248 dbg_search_packet (IOBUF inp
, PACKET
* pkt
, off_t
* retpos
, int with_uid
,
249 const char *dbg_f
, int dbg_l
)
256 parse (inp
, pkt
, with_uid
? 2 : 1, retpos
, &skip
, NULL
, 0, "search",
262 #else /*!DEBUG_PARSE_PACKET*/
264 search_packet (IOBUF inp
, PACKET
* pkt
, off_t
* retpos
, int with_uid
)
270 rc
= parse (inp
, pkt
, with_uid
? 2 : 1, retpos
, &skip
, NULL
, 0);
275 #endif /*!DEBUG_PARSE_PACKET*/
279 * Copy all packets from INP to OUT, thereby removing unused spaces.
281 #ifdef DEBUG_PARSE_PACKET
283 dbg_copy_all_packets (IOBUF inp
, IOBUF out
, const char *dbg_f
, int dbg_l
)
293 parse (inp
, &pkt
, 0, NULL
, &skip
, out
, 0, "copy", dbg_f
, dbg_l
)));
296 #else /*!DEBUG_PARSE_PACKET*/
298 copy_all_packets (IOBUF inp
, IOBUF out
)
306 while (!(rc
= parse (inp
, &pkt
, 0, NULL
, &skip
, out
, 0)));
309 #endif /*!DEBUG_PARSE_PACKET*/
313 * Copy some packets from INP to OUT, thereby removing unused spaces.
314 * Stop at offset STOPoff (i.e. don't copy packets at this or later
317 #ifdef DEBUG_PARSE_PACKET
319 dbg_copy_some_packets (IOBUF inp
, IOBUF out
, off_t stopoff
,
320 const char *dbg_f
, int dbg_l
)
326 if (iobuf_tell (inp
) >= stopoff
)
330 while (!(rc
= parse (inp
, &pkt
, 0, NULL
, &skip
, out
, 0,
331 "some", dbg_f
, dbg_l
)));
334 #else /*!DEBUG_PARSE_PACKET*/
336 copy_some_packets (IOBUF inp
, IOBUF out
, off_t stopoff
)
342 if (iobuf_tell (inp
) >= stopoff
)
346 while (!(rc
= parse (inp
, &pkt
, 0, NULL
, &skip
, out
, 0)));
349 #endif /*!DEBUG_PARSE_PACKET*/
353 * Skip over N packets
355 #ifdef DEBUG_PARSE_PACKET
357 dbg_skip_some_packets (IOBUF inp
, unsigned n
, const char *dbg_f
, int dbg_l
)
362 for (; n
&& !rc
; n
--)
365 rc
= parse (inp
, &pkt
, 0, NULL
, &skip
, NULL
, 1, "skip", dbg_f
, dbg_l
);
369 #else /*!DEBUG_PARSE_PACKET*/
371 skip_some_packets (IOBUF inp
, unsigned n
)
376 for (; n
&& !rc
; n
--)
379 rc
= parse (inp
, &pkt
, 0, NULL
, &skip
, NULL
, 1);
383 #endif /*!DEBUG_PARSE_PACKET*/
387 * Parse packet. Stores 1 at SKIP 1 if the packet should be skipped;
388 * this is the case if either ONLYKEYPKTS is set and the parsed packet
389 * isn't a key packet or the packet-type is 0, indicating deleted
390 * stuff. If OUT is not NULL, a special copymode is used.
393 parse (IOBUF inp
, PACKET
* pkt
, int onlykeypkts
, off_t
* retpos
,
394 int *skip
, IOBUF out
, int do_skip
395 #ifdef DEBUG_PARSE_PACKET
396 , const char *dbg_w
, const char *dbg_f
, int dbg_l
400 int rc
= 0, c
, ctb
, pkttype
, lenbytes
;
401 unsigned long pktlen
;
404 int new_ctb
= 0, partial
= 0;
405 int with_uid
= (onlykeypkts
== 2);
408 assert (!pkt
->pkt
.generic
);
410 *retpos
= iobuf_tell (inp
);
412 if ((ctb
= iobuf_get (inp
)) == -1)
421 log_error ("%s: invalid packet (ctb=%02x)\n", iobuf_where (inp
), ctb
);
422 rc
= gpg_error (GPG_ERR_INV_PACKET
);
426 new_ctb
= !!(ctb
& 0x40);
429 pkttype
= ctb
& 0x3f;
430 if ((c
= iobuf_get (inp
)) == -1)
432 log_error ("%s: 1st length byte missing\n", iobuf_where (inp
));
433 rc
= gpg_error (GPG_ERR_INV_PACKET
);
443 pktlen
= (c
- 192) * 256;
444 if ((c
= iobuf_get (inp
)) == -1)
446 log_error ("%s: 2nd length byte missing\n",
448 rc
= gpg_error (GPG_ERR_INV_PACKET
);
456 pktlen
= (hdr
[hdrlen
++] = iobuf_get_noeof (inp
)) << 24;
457 pktlen
|= (hdr
[hdrlen
++] = iobuf_get_noeof (inp
)) << 16;
458 pktlen
|= (hdr
[hdrlen
++] = iobuf_get_noeof (inp
)) << 8;
459 if ((c
= iobuf_get (inp
)) == -1)
461 log_error ("%s: 4 byte length invalid\n", iobuf_where (inp
));
462 rc
= gpg_error (GPG_ERR_INV_PACKET
);
465 pktlen
|= (hdr
[hdrlen
++] = c
);
467 else /* Partial body length. */
473 case PKT_ENCRYPTED_MDC
:
475 iobuf_set_partial_block_mode (inp
, c
& 0xff);
476 pktlen
= 0; /* To indicate partial length. */
481 log_error ("%s: partial length for invalid"
482 " packet type %d\n", iobuf_where (inp
), pkttype
);
483 rc
= gpg_error (GPG_ERR_INV_PACKET
);
491 pkttype
= (ctb
>> 2) & 0xf;
492 lenbytes
= ((ctb
& 3) == 3) ? 0 : (1 << (ctb
& 3));
495 pktlen
= 0; /* Don't know the value. */
496 /* This isn't really partial, but we can treat it the same
497 in a "read until the end" sort of way. */
499 if (pkttype
!= PKT_ENCRYPTED
&& pkttype
!= PKT_PLAINTEXT
500 && pkttype
!= PKT_COMPRESSED
)
502 log_error ("%s: indeterminate length for invalid"
503 " packet type %d\n", iobuf_where (inp
), pkttype
);
504 rc
= gpg_error (GPG_ERR_INV_PACKET
);
510 for (; lenbytes
; lenbytes
--)
513 pktlen
|= hdr
[hdrlen
++] = iobuf_get_noeof (inp
);
518 if (pktlen
== (unsigned long) (-1))
520 /* With some probability this is caused by a problem in the
521 * the uncompressing layer - in some error cases it just loops
522 * and spits out 0xff bytes. */
523 log_error ("%s: garbled packet detected\n", iobuf_where (inp
));
529 rc
= iobuf_write (out
, hdr
, hdrlen
);
531 rc
= copy_packet (inp
, out
, pkttype
, pktlen
, partial
);
535 if (with_uid
&& pkttype
== PKT_USER_ID
)
539 || (onlykeypkts
&& pkttype
!= PKT_PUBLIC_SUBKEY
540 && pkttype
!= PKT_PUBLIC_KEY
541 && pkttype
!= PKT_SECRET_SUBKEY
&& pkttype
!= PKT_SECRET_KEY
))
543 iobuf_skip_rest (inp
, pktlen
, partial
);
551 #ifdef DEBUG_PARSE_PACKET
552 log_debug ("parse_packet(iob=%d): type=%d length=%lu%s (%s.%s.%d)\n",
553 iobuf_id (inp
), pkttype
, pktlen
, new_ctb
? " (new_ctb)" : "",
554 dbg_w
, dbg_f
, dbg_l
);
556 log_debug ("parse_packet(iob=%d): type=%d length=%lu%s\n",
557 iobuf_id (inp
), pkttype
, pktlen
,
558 new_ctb
? " (new_ctb)" : "");
562 pkt
->pkttype
= pkttype
;
563 rc
= G10ERR_UNKNOWN_PACKET
; /* default error */
567 case PKT_PUBLIC_SUBKEY
:
568 pkt
->pkt
.public_key
= xmalloc_clear (sizeof *pkt
->pkt
.public_key
);
569 rc
= parse_key (inp
, pkttype
, pktlen
, hdr
, hdrlen
, pkt
);
572 case PKT_SECRET_SUBKEY
:
573 pkt
->pkt
.secret_key
= xmalloc_clear (sizeof *pkt
->pkt
.secret_key
);
574 rc
= parse_key (inp
, pkttype
, pktlen
, hdr
, hdrlen
, pkt
);
577 rc
= parse_symkeyenc (inp
, pkttype
, pktlen
, pkt
);
580 rc
= parse_pubkeyenc (inp
, pkttype
, pktlen
, pkt
);
583 pkt
->pkt
.signature
= xmalloc_clear (sizeof *pkt
->pkt
.signature
);
584 rc
= parse_signature (inp
, pkttype
, pktlen
, pkt
->pkt
.signature
);
586 case PKT_ONEPASS_SIG
:
587 pkt
->pkt
.onepass_sig
= xmalloc_clear (sizeof *pkt
->pkt
.onepass_sig
);
588 rc
= parse_onepass_sig (inp
, pkttype
, pktlen
, pkt
->pkt
.onepass_sig
);
591 rc
= parse_user_id (inp
, pkttype
, pktlen
, pkt
);
594 pkt
->pkttype
= pkttype
= PKT_USER_ID
; /* we store it in the userID */
595 rc
= parse_attribute (inp
, pkttype
, pktlen
, pkt
);
597 case PKT_OLD_COMMENT
:
599 rc
= parse_comment (inp
, pkttype
, pktlen
, pkt
);
602 parse_trust (inp
, pkttype
, pktlen
, pkt
);
606 rc
= parse_plaintext (inp
, pkttype
, pktlen
, pkt
, new_ctb
, partial
);
609 rc
= parse_compressed (inp
, pkttype
, pktlen
, pkt
, new_ctb
);
612 case PKT_ENCRYPTED_MDC
:
613 rc
= parse_encrypted (inp
, pkttype
, pktlen
, pkt
, new_ctb
, partial
);
616 rc
= parse_mdc (inp
, pkttype
, pktlen
, pkt
, new_ctb
);
618 case PKT_GPG_CONTROL
:
619 rc
= parse_gpg_control (inp
, pkttype
, pktlen
, pkt
, partial
);
622 rc
= parse_marker (inp
, pkttype
, pktlen
);
625 skip_packet (inp
, pkttype
, pktlen
, partial
);
630 if (!rc
&& iobuf_error (inp
))
631 rc
= G10ERR_INV_KEYRING
;
637 dump_hex_line (int c
, int *i
)
641 if (*i
&& !(*i
% 24))
642 fprintf (listfp
, "\n%4d:", *i
);
647 fprintf (listfp
, " EOF");
649 fprintf (listfp
, " %02x", c
);
655 copy_packet (IOBUF inp
, IOBUF out
, int pkttype
,
656 unsigned long pktlen
, int partial
)
664 while ((n
= iobuf_read (inp
, buf
, 100)) != -1)
665 if ((rc
= iobuf_write (out
, buf
, n
)))
666 return rc
; /* write error */
668 else if (!pktlen
&& pkttype
== PKT_COMPRESSED
)
670 log_debug ("copy_packet: compressed!\n");
671 /* compressed packet, copy till EOF */
672 while ((n
= iobuf_read (inp
, buf
, 100)) != -1)
673 if ((rc
= iobuf_write (out
, buf
, n
)))
674 return rc
; /* write error */
678 for (; pktlen
; pktlen
-= n
)
680 n
= pktlen
> 100 ? 100 : pktlen
;
681 n
= iobuf_read (inp
, buf
, n
);
683 return gpg_error (GPG_ERR_EOF
);
684 if ((rc
= iobuf_write (out
, buf
, n
)))
685 return rc
; /* write error */
693 skip_packet (IOBUF inp
, int pkttype
, unsigned long pktlen
, int partial
)
697 fprintf (listfp
, ":unknown packet: type %2d, length %lu\n",
702 fputs ("dump:", listfp
);
705 while ((c
= iobuf_get (inp
)) != -1)
706 dump_hex_line (c
, &i
);
710 for (; pktlen
; pktlen
--)
712 dump_hex_line ((c
= iobuf_get (inp
)), &i
);
721 iobuf_skip_rest (inp
, pktlen
, partial
);
726 read_rest (IOBUF inp
, size_t pktlen
, int partial
)
733 log_error ("read_rest: can't store stream data\n");
738 p
= xmalloc (pktlen
);
739 for (i
= 0; pktlen
; pktlen
--, i
++)
740 p
[i
] = iobuf_get (inp
);
747 parse_marker (IOBUF inp
, int pkttype
, unsigned long pktlen
)
754 if (iobuf_get (inp
) != 'P')
760 if (iobuf_get (inp
) != 'G')
766 if (iobuf_get (inp
) != 'P')
773 fputs (":marker packet: PGP\n", listfp
);
778 log_error ("invalid marker packet\n");
779 iobuf_skip_rest (inp
, pktlen
, 0);
780 return G10ERR_INVALID_PACKET
;
785 parse_symkeyenc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
790 int i
, version
, s2kmode
, cipher_algo
, hash_algo
, seskeylen
, minlen
;
794 log_error ("packet(%d) too short\n", pkttype
);
795 rc
= gpg_error (GPG_ERR_INV_PACKET
);
798 version
= iobuf_get_noeof (inp
);
802 log_error ("packet(%d) with unknown version %d\n", pkttype
, version
);
803 rc
= gpg_error (GPG_ERR_INV_PACKET
);
807 { /* (we encode the seskeylen in a byte) */
808 log_error ("packet(%d) too large\n", pkttype
);
809 rc
= gpg_error (GPG_ERR_INV_PACKET
);
812 cipher_algo
= iobuf_get_noeof (inp
);
814 s2kmode
= iobuf_get_noeof (inp
);
816 hash_algo
= iobuf_get_noeof (inp
);
820 case 0: /* Simple S2K. */
823 case 1: /* Salted S2K. */
826 case 3: /* Iterated+salted S2K. */
830 log_error ("unknown S2K mode %d\n", s2kmode
);
835 log_error ("packet with S2K %d too short\n", s2kmode
);
836 rc
= gpg_error (GPG_ERR_INV_PACKET
);
839 seskeylen
= pktlen
- minlen
;
840 k
= packet
->pkt
.symkey_enc
= xmalloc_clear (sizeof *packet
->pkt
.symkey_enc
842 k
->version
= version
;
843 k
->cipher_algo
= cipher_algo
;
844 k
->s2k
.mode
= s2kmode
;
845 k
->s2k
.hash_algo
= hash_algo
;
846 if (s2kmode
== 1 || s2kmode
== 3)
848 for (i
= 0; i
< 8 && pktlen
; i
++, pktlen
--)
849 k
->s2k
.salt
[i
] = iobuf_get_noeof (inp
);
853 k
->s2k
.count
= iobuf_get (inp
);
856 k
->seskeylen
= seskeylen
;
859 for (i
= 0; i
< seskeylen
&& pktlen
; i
++, pktlen
--)
860 k
->seskey
[i
] = iobuf_get_noeof (inp
);
862 /* What we're watching out for here is a session key decryptor
863 with no salt. The RFC says that using salt for this is a
865 if (s2kmode
!= 1 && s2kmode
!= 3)
866 log_info (_("WARNING: potentially insecure symmetrically"
867 " encrypted session key\n"));
874 ":symkey enc packet: version %d, cipher %d, s2k %d, hash %d",
875 version
, cipher_algo
, s2kmode
, hash_algo
);
877 fprintf (listfp
, ", seskey %d bits", (seskeylen
- 1) * 8);
878 fprintf (listfp
, "\n");
879 if (s2kmode
== 1 || s2kmode
== 3)
881 fprintf (listfp
, "\tsalt ");
882 for (i
= 0; i
< 8; i
++)
883 fprintf (listfp
, "%02x", k
->s2k
.salt
[i
]);
885 fprintf (listfp
, ", count %lu (%lu)",
886 S2K_DECODE_COUNT ((ulong
) k
->s2k
.count
),
887 (ulong
) k
->s2k
.count
);
888 fprintf (listfp
, "\n");
893 iobuf_skip_rest (inp
, pktlen
, 0);
899 parse_pubkeyenc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
907 k
= packet
->pkt
.pubkey_enc
= xmalloc_clear (sizeof *packet
->pkt
.pubkey_enc
);
910 log_error ("packet(%d) too short\n", pkttype
);
911 rc
= gpg_error (GPG_ERR_INV_PACKET
);
914 k
->version
= iobuf_get_noeof (inp
);
916 if (k
->version
!= 2 && k
->version
!= 3)
918 log_error ("packet(%d) with unknown version %d\n", pkttype
, k
->version
);
919 rc
= gpg_error (GPG_ERR_INV_PACKET
);
922 k
->keyid
[0] = read_32 (inp
);
924 k
->keyid
[1] = read_32 (inp
);
926 k
->pubkey_algo
= iobuf_get_noeof (inp
);
928 k
->throw_keyid
= 0; /* Only used as flag for build_packet. */
931 ":pubkey enc packet: version %d, algo %d, keyid %08lX%08lX\n",
932 k
->version
, k
->pubkey_algo
, (ulong
) k
->keyid
[0],
933 (ulong
) k
->keyid
[1]);
935 ndata
= pubkey_get_nenc (k
->pubkey_algo
);
939 fprintf (listfp
, "\tunsupported algorithm %d\n", k
->pubkey_algo
);
940 unknown_pubkey_warning (k
->pubkey_algo
);
941 k
->data
[0] = NULL
; /* No need to store the encrypted data. */
945 for (i
= 0; i
< ndata
; i
++)
948 k
->data
[i
] = mpi_read (inp
, &n
, 0);
952 fprintf (listfp
, "\tdata: ");
953 mpi_print (listfp
, k
->data
[i
], mpi_print_mode
);
957 rc
= gpg_error (GPG_ERR_INV_PACKET
);
962 iobuf_skip_rest (inp
, pktlen
, 0);
968 dump_sig_subpkt (int hashed
, int type
, int critical
,
969 const byte
* buffer
, size_t buflen
, size_t length
)
971 const char *p
= NULL
;
974 /* The CERT has warning out with explains how to use GNUPG to detect
975 * the ARRs - we print our old message here when it is a faked ARR
976 * and add an additional notice. */
977 if (type
== SIGSUBPKT_ARR
&& !hashed
)
980 "\tsubpkt %d len %u (additional recipient request)\n"
981 "WARNING: PGP versions > 5.0 and < 6.5.8 will automagically "
982 "encrypt to this key and thereby reveal the plaintext to "
983 "the owner of this ARR key. Detailed info follows:\n",
984 type
, (unsigned) length
);
990 fprintf (listfp
, "\t%s%ssubpkt %d len %u (", /*) */
991 critical
? "critical " : "",
992 hashed
? "hashed " : "", type
, (unsigned) length
);
995 fprintf (listfp
, "too short: buffer is only %u)\n", (unsigned) buflen
);
1000 case SIGSUBPKT_SIG_CREATED
:
1002 fprintf (listfp
, "sig created %s",
1003 strtimestamp (buffer_to_u32 (buffer
)));
1005 case SIGSUBPKT_SIG_EXPIRE
:
1008 if (buffer_to_u32 (buffer
))
1009 fprintf (listfp
, "sig expires after %s",
1010 strtimevalue (buffer_to_u32 (buffer
)));
1012 fprintf (listfp
, "sig does not expire");
1015 case SIGSUBPKT_EXPORTABLE
:
1017 fprintf (listfp
, "%sexportable", *buffer
? "" : "not ");
1019 case SIGSUBPKT_TRUST
:
1021 p
= "[invalid trust subpacket]";
1023 fprintf (listfp
, "trust signature of depth %d, value %d", buffer
[0],
1026 case SIGSUBPKT_REGEXP
:
1028 p
= "[invalid regexp subpacket]";
1030 fprintf (listfp
, "regular expression: \"%s\"", buffer
);
1032 case SIGSUBPKT_REVOCABLE
:
1034 fprintf (listfp
, "%srevocable", *buffer
? "" : "not ");
1036 case SIGSUBPKT_KEY_EXPIRE
:
1039 if (buffer_to_u32 (buffer
))
1040 fprintf (listfp
, "key expires after %s",
1041 strtimevalue (buffer_to_u32 (buffer
)));
1043 fprintf (listfp
, "key does not expire");
1046 case SIGSUBPKT_PREF_SYM
:
1047 fputs ("pref-sym-algos:", listfp
);
1048 for (i
= 0; i
< length
; i
++)
1049 fprintf (listfp
, " %d", buffer
[i
]);
1051 case SIGSUBPKT_REV_KEY
:
1052 fputs ("revocation key: ", listfp
);
1057 fprintf (listfp
, "c=%02x a=%d f=", buffer
[0], buffer
[1]);
1058 for (i
= 2; i
< length
; i
++)
1059 fprintf (listfp
, "%02X", buffer
[i
]);
1062 case SIGSUBPKT_ISSUER
:
1064 fprintf (listfp
, "issuer key ID %08lX%08lX",
1065 (ulong
) buffer_to_u32 (buffer
),
1066 (ulong
) buffer_to_u32 (buffer
+ 4));
1068 case SIGSUBPKT_NOTATION
:
1070 fputs ("notation: ", listfp
);
1075 const byte
*s
= buffer
;
1078 n1
= (s
[4] << 8) | s
[5];
1079 n2
= (s
[6] << 8) | s
[7];
1081 if (8 + n1
+ n2
!= length
)
1085 print_string (listfp
, s
, n1
, ')');
1089 print_string (listfp
, s
+ n1
, n2
, ')');
1091 p
= "[not human readable]";
1096 case SIGSUBPKT_PREF_HASH
:
1097 fputs ("pref-hash-algos:", listfp
);
1098 for (i
= 0; i
< length
; i
++)
1099 fprintf (listfp
, " %d", buffer
[i
]);
1101 case SIGSUBPKT_PREF_COMPR
:
1102 fputs ("pref-zip-algos:", listfp
);
1103 for (i
= 0; i
< length
; i
++)
1104 fprintf (listfp
, " %d", buffer
[i
]);
1106 case SIGSUBPKT_KS_FLAGS
:
1107 fputs ("key server preferences:", listfp
);
1108 for (i
= 0; i
< length
; i
++)
1109 fprintf (listfp
, " %02X", buffer
[i
]);
1111 case SIGSUBPKT_PREF_KS
:
1112 fputs ("preferred key server: ", listfp
);
1113 print_string (listfp
, buffer
, length
, ')');
1115 case SIGSUBPKT_PRIMARY_UID
:
1116 p
= "primary user ID";
1118 case SIGSUBPKT_POLICY
:
1119 fputs ("policy: ", listfp
);
1120 print_string (listfp
, buffer
, length
, ')');
1122 case SIGSUBPKT_KEY_FLAGS
:
1123 fputs ("key flags:", listfp
);
1124 for (i
= 0; i
< length
; i
++)
1125 fprintf (listfp
, " %02X", buffer
[i
]);
1127 case SIGSUBPKT_SIGNERS_UID
:
1128 p
= "signer's user ID";
1130 case SIGSUBPKT_REVOC_REASON
:
1133 fprintf (listfp
, "revocation reason 0x%02x (", *buffer
);
1134 print_string (listfp
, buffer
+ 1, length
- 1, ')');
1139 fputs ("Big Brother's key (ignored): ", listfp
);
1144 fprintf (listfp
, "c=%02x a=%d f=", buffer
[0], buffer
[1]);
1145 for (i
= 2; i
< length
; i
++)
1146 fprintf (listfp
, "%02X", buffer
[i
]);
1149 case SIGSUBPKT_FEATURES
:
1150 fputs ("features:", listfp
);
1151 for (i
= 0; i
< length
; i
++)
1152 fprintf (listfp
, " %02x", buffer
[i
]);
1154 case SIGSUBPKT_SIGNATURE
:
1155 fputs ("signature: ", listfp
);
1159 fprintf (listfp
, "v%d, class 0x%02X, algo %d, digest algo %d",
1161 buffer
[0] == 3 ? buffer
[2] : buffer
[1],
1162 buffer
[0] == 3 ? buffer
[15] : buffer
[2],
1163 buffer
[0] == 3 ? buffer
[16] : buffer
[3]);
1166 if (type
>= 100 && type
<= 110)
1167 p
= "experimental / private subpacket";
1173 fprintf (listfp
, "%s)\n", p
? p
: "");
1178 * Returns: >= 0 use this offset into buffer
1179 * -1 explicitly reject returning this type
1180 * -2 subpacket too short
1183 parse_one_sig_subpkt (const byte
* buffer
, size_t n
, int type
)
1187 case SIGSUBPKT_REV_KEY
:
1191 case SIGSUBPKT_SIG_CREATED
:
1192 case SIGSUBPKT_SIG_EXPIRE
:
1193 case SIGSUBPKT_KEY_EXPIRE
:
1197 case SIGSUBPKT_KEY_FLAGS
:
1198 case SIGSUBPKT_KS_FLAGS
:
1199 case SIGSUBPKT_PREF_SYM
:
1200 case SIGSUBPKT_PREF_HASH
:
1201 case SIGSUBPKT_PREF_COMPR
:
1202 case SIGSUBPKT_POLICY
:
1203 case SIGSUBPKT_PREF_KS
:
1204 case SIGSUBPKT_FEATURES
:
1205 case SIGSUBPKT_REGEXP
:
1207 case SIGSUBPKT_SIGNATURE
:
1208 case SIGSUBPKT_EXPORTABLE
:
1209 case SIGSUBPKT_REVOCABLE
:
1210 case SIGSUBPKT_REVOC_REASON
:
1214 case SIGSUBPKT_ISSUER
: /* issuer key ID */
1218 case SIGSUBPKT_NOTATION
:
1219 /* minimum length needed, and the subpacket must be well-formed
1220 where the name length and value length all fit inside the
1223 || 8 + ((buffer
[4] << 8) | buffer
[5]) +
1224 ((buffer
[6] << 8) | buffer
[7]) != n
)
1227 case SIGSUBPKT_PRIMARY_UID
:
1231 case SIGSUBPKT_TRUST
:
1242 /* Return true if we understand the critical notation. */
1244 can_handle_critical_notation (const byte
* name
, size_t len
)
1246 if (len
== 32 && memcmp (name
, "preferred-email-encoding@pgp.com", 32) == 0)
1248 if (len
== 21 && memcmp (name
, "pka-address@gnupg.org", 21) == 0)
1256 can_handle_critical (const byte
* buffer
, size_t n
, int type
)
1260 case SIGSUBPKT_NOTATION
:
1262 return can_handle_critical_notation (buffer
+ 8,
1263 (buffer
[4] << 8) | buffer
[5]);
1266 case SIGSUBPKT_SIGNATURE
:
1267 case SIGSUBPKT_SIG_CREATED
:
1268 case SIGSUBPKT_SIG_EXPIRE
:
1269 case SIGSUBPKT_KEY_EXPIRE
:
1270 case SIGSUBPKT_EXPORTABLE
:
1271 case SIGSUBPKT_REVOCABLE
:
1272 case SIGSUBPKT_REV_KEY
:
1273 case SIGSUBPKT_ISSUER
: /* issuer key ID */
1274 case SIGSUBPKT_PREF_SYM
:
1275 case SIGSUBPKT_PREF_HASH
:
1276 case SIGSUBPKT_PREF_COMPR
:
1277 case SIGSUBPKT_KEY_FLAGS
:
1278 case SIGSUBPKT_PRIMARY_UID
:
1279 case SIGSUBPKT_FEATURES
:
1280 case SIGSUBPKT_TRUST
:
1281 case SIGSUBPKT_REGEXP
:
1282 /* Is it enough to show the policy or keyserver? */
1283 case SIGSUBPKT_POLICY
:
1284 case SIGSUBPKT_PREF_KS
:
1294 enum_sig_subpkt (const subpktarea_t
* pktbuf
, sigsubpkttype_t reqtype
,
1295 size_t * ret_n
, int *start
, int *critical
)
1304 int reqseq
= start
? *start
: 0;
1307 critical
= &critical_dummy
;
1309 if (!pktbuf
|| reqseq
== -1)
1311 /* return some value different from NULL to indicate that
1312 * there is no critical bit we do not understand. The caller
1313 * will never use the value. Yes I know, it is an ugly hack */
1315 SIGSUBPKT_TEST_CRITICAL
? (const byte
*) &pktbuf
: NULL
;
1317 buffer
= pktbuf
->data
;
1318 buflen
= pktbuf
->len
;
1323 if (n
== 255) /* 4 byte length header. */
1327 n
= (buffer
[0] << 24) | (buffer
[1] << 16)
1328 | (buffer
[2] << 8) | buffer
[3];
1332 else if (n
>= 192) /* 4 byte special encoded length header. */
1336 n
= ((n
- 192) << 8) + *buffer
+ 192;
1350 if (!(++seq
> reqseq
))
1352 else if (reqtype
== SIGSUBPKT_TEST_CRITICAL
)
1356 if (n
- 1 > buflen
+ 1)
1358 if (!can_handle_critical (buffer
+ 1, n
- 1, type
))
1361 log_info (_("subpacket of type %d has "
1362 "critical bit set\n"), type
);
1365 return NULL
; /* This is an error. */
1369 else if (reqtype
< 0) /* List packets. */
1370 dump_sig_subpkt (reqtype
== SIGSUBPKT_LIST_HASHED
,
1371 type
, *critical
, buffer
, buflen
, n
);
1372 else if (type
== reqtype
) /* Found. */
1380 offset
= parse_one_sig_subpkt (buffer
, n
, type
);
1384 log_error ("subpacket of type %d too short\n", type
);
1393 return buffer
+ offset
;
1398 if (reqtype
== SIGSUBPKT_TEST_CRITICAL
)
1399 return buffer
; /* Used as True to indicate that there is no. */
1401 /* Critical bit we don't understand. */
1404 return NULL
; /* End of packets; not found. */
1408 log_info ("buffer shorter than subpacket\n");
1416 parse_sig_subpkt (const subpktarea_t
* buffer
, sigsubpkttype_t reqtype
,
1419 return enum_sig_subpkt (buffer
, reqtype
, ret_n
, NULL
, NULL
);
1424 parse_sig_subpkt2 (PKT_signature
* sig
, sigsubpkttype_t reqtype
,
1429 p
= parse_sig_subpkt (sig
->hashed
, reqtype
, ret_n
);
1431 p
= parse_sig_subpkt (sig
->unhashed
, reqtype
, ret_n
);
1436 /* Find all revocation keys. Look in hashed area only. */
1438 parse_revkeys (PKT_signature
* sig
)
1440 struct revocation_key
*revkey
;
1444 if (sig
->sig_class
!= 0x1F)
1448 (struct revocation_key
*) enum_sig_subpkt (sig
->hashed
,
1452 if (len
== sizeof (struct revocation_key
)
1453 && (revkey
->class & 0x80)) /* 0x80 bit must be set. */
1455 sig
->revkey
= xrealloc (sig
->revkey
,
1456 sizeof (struct revocation_key
*) *
1457 (sig
->numrevkeys
+ 1));
1458 sig
->revkey
[sig
->numrevkeys
] = revkey
;
1466 parse_signature (IOBUF inp
, int pkttype
, unsigned long pktlen
,
1467 PKT_signature
* sig
)
1477 log_error ("packet(%d) too short\n", pkttype
);
1480 sig
->version
= iobuf_get_noeof (inp
);
1482 if (sig
->version
== 4)
1484 else if (sig
->version
!= 2 && sig
->version
!= 3)
1486 log_error ("packet(%d) with unknown version %d\n",
1487 pkttype
, sig
->version
);
1488 rc
= gpg_error (GPG_ERR_INV_PACKET
);
1494 md5_len
= iobuf_get_noeof (inp
);
1497 sig
->sig_class
= iobuf_get_noeof (inp
);
1501 sig
->timestamp
= read_32 (inp
);
1503 sig
->keyid
[0] = read_32 (inp
);
1505 sig
->keyid
[1] = read_32 (inp
);
1508 sig
->pubkey_algo
= iobuf_get_noeof (inp
);
1510 sig
->digest_algo
= iobuf_get_noeof (inp
);
1512 sig
->flags
.exportable
= 1;
1513 sig
->flags
.revocable
= 1;
1514 if (is_v4
) /* Read subpackets. */
1517 pktlen
-= 2; /* Length of hashed data. */
1520 log_error ("signature packet: hashed data too long\n");
1521 rc
= G10ERR_INVALID_PACKET
;
1526 sig
->hashed
= xmalloc (sizeof (*sig
->hashed
) + n
- 1);
1527 sig
->hashed
->size
= n
;
1528 sig
->hashed
->len
= n
;
1529 if (iobuf_read (inp
, sig
->hashed
->data
, n
) != n
)
1531 log_error ("premature eof while reading "
1532 "hashed signature data\n");
1539 pktlen
-= 2; /* Length of unhashed data. */
1542 log_error ("signature packet: unhashed data too long\n");
1543 rc
= G10ERR_INVALID_PACKET
;
1548 sig
->unhashed
= xmalloc (sizeof (*sig
->unhashed
) + n
- 1);
1549 sig
->unhashed
->size
= n
;
1550 sig
->unhashed
->len
= n
;
1551 if (iobuf_read (inp
, sig
->unhashed
->data
, n
) != n
)
1553 log_error ("premature eof while reading "
1554 "unhashed signature data\n");
1562 if (pktlen
< 5) /* Sanity check. */
1564 log_error ("packet(%d) too short\n", pkttype
);
1565 rc
= G10ERR_INVALID_PACKET
;
1569 sig
->digest_start
[0] = iobuf_get_noeof (inp
);
1571 sig
->digest_start
[1] = iobuf_get_noeof (inp
);
1574 if (is_v4
&& sig
->pubkey_algo
) /* Extract required information. */
1579 /* Set sig->flags.unknown_critical if there is a critical bit
1580 * set for packets which we do not understand. */
1581 if (!parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_TEST_CRITICAL
, NULL
)
1582 || !parse_sig_subpkt (sig
->unhashed
, SIGSUBPKT_TEST_CRITICAL
, NULL
))
1583 sig
->flags
.unknown_critical
= 1;
1585 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_SIG_CREATED
, NULL
);
1587 sig
->timestamp
= buffer_to_u32 (p
);
1588 else if (!(sig
->pubkey_algo
>= 100 && sig
->pubkey_algo
<= 110)
1590 log_info ("signature packet without timestamp\n");
1592 p
= parse_sig_subpkt2 (sig
, SIGSUBPKT_ISSUER
, NULL
);
1595 sig
->keyid
[0] = buffer_to_u32 (p
);
1596 sig
->keyid
[1] = buffer_to_u32 (p
+ 4);
1598 else if (!(sig
->pubkey_algo
>= 100 && sig
->pubkey_algo
<= 110)
1600 log_info ("signature packet without keyid\n");
1602 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_SIG_EXPIRE
, NULL
);
1603 if (p
&& buffer_to_u32 (p
))
1604 sig
->expiredate
= sig
->timestamp
+ buffer_to_u32 (p
);
1605 if (sig
->expiredate
&& sig
->expiredate
<= make_timestamp ())
1606 sig
->flags
.expired
= 1;
1608 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_POLICY
, NULL
);
1610 sig
->flags
.policy_url
= 1;
1612 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_PREF_KS
, NULL
);
1614 sig
->flags
.pref_ks
= 1;
1616 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_NOTATION
, NULL
);
1618 sig
->flags
.notation
= 1;
1620 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_REVOCABLE
, NULL
);
1622 sig
->flags
.revocable
= 0;
1624 p
= parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_TRUST
, &len
);
1627 sig
->trust_depth
= p
[0];
1628 sig
->trust_value
= p
[1];
1630 /* Only look for a regexp if there is also a trust
1633 parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_REGEXP
, &len
);
1635 /* If the regular expression is of 0 length, there is no
1636 regular expression. */
1638 sig
->trust_regexp
= NULL
;
1641 /* We accept the exportable subpacket from either the hashed or
1642 unhashed areas as older versions of gpg put it in the
1643 unhashed area. In theory, anyway, we should never see this
1644 packet off of a local keyring. */
1646 p
= parse_sig_subpkt2 (sig
, SIGSUBPKT_EXPORTABLE
, NULL
);
1648 sig
->flags
.exportable
= 0;
1650 /* Find all revocation keys. */
1651 if (sig
->sig_class
== 0x1F)
1652 parse_revkeys (sig
);
1657 fprintf (listfp
, ":signature packet: algo %d, keyid %08lX%08lX\n"
1658 "\tversion %d, created %lu, md5len %d, sigclass 0x%02x\n"
1659 "\tdigest algo %d, begin of digest %02x %02x\n",
1661 (ulong
) sig
->keyid
[0], (ulong
) sig
->keyid
[1],
1662 sig
->version
, (ulong
) sig
->timestamp
, md5_len
, sig
->sig_class
,
1663 sig
->digest_algo
, sig
->digest_start
[0], sig
->digest_start
[1]);
1666 parse_sig_subpkt (sig
->hashed
, SIGSUBPKT_LIST_HASHED
, NULL
);
1667 parse_sig_subpkt (sig
->unhashed
, SIGSUBPKT_LIST_UNHASHED
, NULL
);
1671 ndata
= pubkey_get_nsig (sig
->pubkey_algo
);
1675 fprintf (listfp
, "\tunknown algorithm %d\n", sig
->pubkey_algo
);
1676 unknown_pubkey_warning (sig
->pubkey_algo
);
1678 /* We store the plain material in data[0], so that we are able
1679 * to write it back with build_packet(). */
1680 if (pktlen
> (5 * MAX_EXTERN_MPI_BITS
/ 8))
1682 /* We include a limit to avoid too trivial DoS attacks by
1683 having gpg allocate too much memory. */
1684 log_error ("signature packet: too much data\n");
1685 rc
= G10ERR_INVALID_PACKET
;
1690 gcry_mpi_set_opaque (NULL
, read_rest (inp
, pktlen
, 0),
1697 for (i
= 0; i
< ndata
; i
++)
1700 sig
->data
[i
] = mpi_read (inp
, &n
, 0);
1704 fprintf (listfp
, "\tdata: ");
1705 mpi_print (listfp
, sig
->data
[i
], mpi_print_mode
);
1706 putc ('\n', listfp
);
1709 rc
= G10ERR_INVALID_PACKET
;
1714 iobuf_skip_rest (inp
, pktlen
, 0);
1720 parse_onepass_sig (IOBUF inp
, int pkttype
, unsigned long pktlen
,
1721 PKT_onepass_sig
* ops
)
1728 log_error ("packet(%d) too short\n", pkttype
);
1729 rc
= gpg_error (GPG_ERR_INV_PACKET
);
1732 version
= iobuf_get_noeof (inp
);
1736 log_error ("onepass_sig with unknown version %d\n", version
);
1737 rc
= gpg_error (GPG_ERR_INV_PACKET
);
1740 ops
->sig_class
= iobuf_get_noeof (inp
);
1742 ops
->digest_algo
= iobuf_get_noeof (inp
);
1744 ops
->pubkey_algo
= iobuf_get_noeof (inp
);
1746 ops
->keyid
[0] = read_32 (inp
);
1748 ops
->keyid
[1] = read_32 (inp
);
1750 ops
->last
= iobuf_get_noeof (inp
);
1754 ":onepass_sig packet: keyid %08lX%08lX\n"
1755 "\tversion %d, sigclass 0x%02x, digest %d, pubkey %d, "
1757 (ulong
) ops
->keyid
[0], (ulong
) ops
->keyid
[1],
1758 version
, ops
->sig_class
,
1759 ops
->digest_algo
, ops
->pubkey_algo
, ops
->last
);
1763 iobuf_skip_rest (inp
, pktlen
, 0);
1769 read_protected_v3_mpi (IOBUF inp
, unsigned long *length
)
1772 unsigned int nbits
, nbytes
;
1773 unsigned char *buf
, *p
;
1778 log_error ("mpi too small\n");
1782 if ((c
= iobuf_get (inp
)) == -1)
1786 if ((c
= iobuf_get (inp
)) == -1)
1793 log_error ("mpi too large (%u bits)\n", nbits
);
1796 nbytes
= (nbits
+ 7) / 8;
1797 buf
= p
= xmalloc (2 + nbytes
);
1800 for (; nbytes
&& *length
; nbytes
--, --*length
)
1801 *p
++ = iobuf_get (inp
);
1804 log_error ("packet shorter than mpi\n");
1809 /* Convert buffer into an opaque MPI. */
1810 val
= gcry_mpi_set_opaque (NULL
, buf
, (p
- buf
) * 8);
1816 parse_key (IOBUF inp
, int pkttype
, unsigned long pktlen
,
1817 byte
* hdr
, int hdrlen
, PACKET
* pkt
)
1819 int i
, version
, algorithm
;
1821 unsigned long timestamp
, expiredate
, max_expiredate
;
1829 version
= iobuf_get_noeof (inp
);
1831 if (pkttype
== PKT_PUBLIC_SUBKEY
&& version
== '#')
1833 /* Early versions of G10 used the old PGP comments packets;
1834 * luckily all those comments are started by a hash. */
1837 fprintf (listfp
, ":rfc1991 comment packet: \"");
1838 for (; pktlen
; pktlen
--)
1841 c
= iobuf_get_noeof (inp
);
1842 if (c
>= ' ' && c
<= 'z')
1845 fprintf (listfp
, "\\x%02x", c
);
1847 fprintf (listfp
, "\"\n");
1849 iobuf_skip_rest (inp
, pktlen
, 0);
1852 else if (version
== 4)
1854 else if (version
!= 2 && version
!= 3)
1856 log_error ("packet(%d) with unknown version %d\n", pkttype
, version
);
1857 rc
= gpg_error (GPG_ERR_INV_PACKET
);
1863 log_error ("packet(%d) too short\n", pkttype
);
1864 rc
= gpg_error (GPG_ERR_INV_PACKET
);
1868 timestamp
= read_32 (inp
);
1872 expiredate
= 0; /* have to get it from the selfsignature */
1877 unsigned short ndays
;
1878 ndays
= read_16 (inp
);
1881 expiredate
= timestamp
+ ndays
* 86400L;
1885 max_expiredate
= expiredate
;
1887 algorithm
= iobuf_get_noeof (inp
);
1890 fprintf (listfp
, ":%s key packet:\n"
1891 "\tversion %d, algo %d, created %lu, expires %lu\n",
1892 pkttype
== PKT_PUBLIC_KEY
? "public" :
1893 pkttype
== PKT_SECRET_KEY
? "secret" :
1894 pkttype
== PKT_PUBLIC_SUBKEY
? "public sub" :
1895 pkttype
== PKT_SECRET_SUBKEY
? "secret sub" : "??",
1896 version
, algorithm
, timestamp
, expiredate
);
1898 if (pkttype
== PKT_SECRET_KEY
|| pkttype
== PKT_SECRET_SUBKEY
)
1900 PKT_secret_key
*sk
= pkt
->pkt
.secret_key
;
1902 sk
->timestamp
= timestamp
;
1903 sk
->expiredate
= expiredate
;
1904 sk
->max_expiredate
= max_expiredate
;
1905 sk
->hdrbytes
= hdrlen
;
1906 sk
->version
= version
;
1907 sk
->is_primary
= pkttype
== PKT_SECRET_KEY
;
1908 sk
->pubkey_algo
= algorithm
;
1910 sk
->pubkey_usage
= 0; /* not yet used */
1914 PKT_public_key
*pk
= pkt
->pkt
.public_key
;
1916 pk
->timestamp
= timestamp
;
1917 pk
->expiredate
= expiredate
;
1918 pk
->max_expiredate
= max_expiredate
;
1919 pk
->hdrbytes
= hdrlen
;
1920 pk
->version
= version
;
1921 pk
->is_primary
= pkttype
== PKT_PUBLIC_KEY
;
1922 pk
->pubkey_algo
= algorithm
;
1924 pk
->pubkey_usage
= 0; /* not yet used */
1926 pk
->is_disabled
= 0;
1930 nskey
= pubkey_get_nskey (algorithm
);
1931 npkey
= pubkey_get_npkey (algorithm
);
1935 fprintf (listfp
, "\tunknown algorithm %d\n", algorithm
);
1936 unknown_pubkey_warning (algorithm
);
1940 if (pkttype
== PKT_SECRET_KEY
|| pkttype
== PKT_SECRET_SUBKEY
)
1942 PKT_secret_key
*sk
= pkt
->pkt
.secret_key
;
1948 sk
->skey
[0] = gcry_mpi_set_opaque (NULL
, read_rest (inp
, pktlen
, 0),
1954 for (i
= 0; i
< npkey
; i
++)
1957 sk
->skey
[i
] = mpi_read (inp
, &n
, 0);
1961 fprintf (listfp
, "\tskey[%d]: ", i
);
1962 mpi_print (listfp
, sk
->skey
[i
], mpi_print_mode
);
1963 putc ('\n', listfp
);
1966 rc
= G10ERR_INVALID_PACKET
;
1968 if (rc
) /* One of the MPIs were bad. */
1970 sk
->protect
.algo
= iobuf_get_noeof (inp
);
1972 sk
->protect
.sha1chk
= 0;
1973 if (sk
->protect
.algo
)
1975 sk
->is_protected
= 1;
1976 sk
->protect
.s2k
.count
= 0;
1977 if (sk
->protect
.algo
== 254 || sk
->protect
.algo
== 255)
1981 rc
= G10ERR_INVALID_PACKET
;
1984 sk
->protect
.sha1chk
= (sk
->protect
.algo
== 254);
1985 sk
->protect
.algo
= iobuf_get_noeof (inp
);
1987 /* Note that a sk->protect.algo > 110 is illegal, but
1988 I'm not erroring on it here as otherwise there would
1989 be no way to delete such a key. */
1990 sk
->protect
.s2k
.mode
= iobuf_get_noeof (inp
);
1992 sk
->protect
.s2k
.hash_algo
= iobuf_get_noeof (inp
);
1994 /* check for the special GNU extension */
1995 if (is_v4
&& sk
->protect
.s2k
.mode
== 101)
1997 for (i
= 0; i
< 4 && pktlen
; i
++, pktlen
--)
1998 temp
[i
] = iobuf_get_noeof (inp
);
1999 if (i
< 4 || memcmp (temp
, "GNU", 3))
2002 fprintf (listfp
, "\tunknown S2K %d\n",
2003 sk
->protect
.s2k
.mode
);
2004 rc
= G10ERR_INVALID_PACKET
;
2007 /* Here we know that it is a GNU extension. What
2008 * follows is the GNU protection mode: All values
2009 * have special meanings and they are mapped to MODE
2010 * with a base of 1000. */
2011 sk
->protect
.s2k
.mode
= 1000 + temp
[3];
2013 switch (sk
->protect
.s2k
.mode
)
2017 for (i
= 0; i
< 8 && pktlen
; i
++, pktlen
--)
2018 temp
[i
] = iobuf_get_noeof (inp
);
2019 memcpy (sk
->protect
.s2k
.salt
, temp
, 8);
2022 switch (sk
->protect
.s2k
.mode
)
2026 fprintf (listfp
, "\tsimple S2K");
2030 fprintf (listfp
, "\tsalted S2K");
2034 fprintf (listfp
, "\titer+salt S2K");
2038 fprintf (listfp
, "\tgnu-dummy S2K");
2042 fprintf (listfp
, "\tgnu-divert-to-card S2K");
2046 fprintf (listfp
, "\tunknown %sS2K %d\n",
2047 sk
->protect
.s2k
.mode
< 1000 ? "" : "GNU ",
2048 sk
->protect
.s2k
.mode
);
2049 rc
= G10ERR_INVALID_PACKET
;
2055 fprintf (listfp
, ", algo: %d,%s hash: %d",
2057 sk
->protect
.sha1chk
? " SHA1 protection,"
2058 : " simple checksum,", sk
->protect
.s2k
.hash_algo
);
2059 if (sk
->protect
.s2k
.mode
== 1 || sk
->protect
.s2k
.mode
== 3)
2061 fprintf (listfp
, ", salt: ");
2062 for (i
= 0; i
< 8; i
++)
2063 fprintf (listfp
, "%02x", sk
->protect
.s2k
.salt
[i
]);
2065 putc ('\n', listfp
);
2068 if (sk
->protect
.s2k
.mode
== 3)
2072 rc
= G10ERR_INVALID_PACKET
;
2075 sk
->protect
.s2k
.count
= iobuf_get (inp
);
2078 fprintf (listfp
, "\tprotect count: %lu\n",
2079 (ulong
) sk
->protect
.s2k
.count
);
2081 else if (sk
->protect
.s2k
.mode
== 1002)
2083 /* Read the serial number. */
2086 rc
= G10ERR_INVALID_PACKET
;
2089 snlen
= iobuf_get (inp
);
2091 if (pktlen
< snlen
|| snlen
== -1)
2093 rc
= G10ERR_INVALID_PACKET
;
2098 else /* Old version; no S2K, so we set mode to 0, hash MD5. */
2100 /* Note that a sk->protect.algo > 110 is illegal, but
2101 I'm not erroring on it here as otherwise there would
2102 be no way to delete such a key. */
2103 sk
->protect
.s2k
.mode
= 0;
2104 sk
->protect
.s2k
.hash_algo
= DIGEST_ALGO_MD5
;
2106 fprintf (listfp
, "\tprotect algo: %d (hash algo: %d)\n",
2107 sk
->protect
.algo
, sk
->protect
.s2k
.hash_algo
);
2110 /* It is really ugly that we don't know the size
2111 * of the IV here in cases we are not aware of the algorithm.
2113 * sk->protect.ivlen = cipher_get_blocksize(sk->protect.algo);
2114 * won't work. The only solution I see is to hardwire it.
2115 * NOTE: if you change the ivlen above 16, don't forget to
2117 sk
->protect
.ivlen
= openpgp_cipher_blocklen (sk
->protect
.algo
);
2118 assert (sk
->protect
.ivlen
<= sizeof (temp
));
2120 if (sk
->protect
.s2k
.mode
== 1001)
2121 sk
->protect
.ivlen
= 0;
2122 else if (sk
->protect
.s2k
.mode
== 1002)
2123 sk
->protect
.ivlen
= snlen
< 16 ? snlen
: 16;
2125 if (pktlen
< sk
->protect
.ivlen
)
2127 rc
= G10ERR_INVALID_PACKET
;
2130 for (i
= 0; i
< sk
->protect
.ivlen
&& pktlen
; i
++, pktlen
--)
2131 temp
[i
] = iobuf_get_noeof (inp
);
2135 sk
->protect
.s2k
.mode
== 1002 ? "\tserial-number: "
2136 : "\tprotect IV: ");
2137 for (i
= 0; i
< sk
->protect
.ivlen
; i
++)
2138 fprintf (listfp
, " %02x", temp
[i
]);
2139 putc ('\n', listfp
);
2141 memcpy (sk
->protect
.iv
, temp
, sk
->protect
.ivlen
);
2144 sk
->is_protected
= 0;
2146 /* It does not make sense to read it into secure memory.
2147 * If the user is so careless, not to protect his secret key,
2148 * we can assume, that he operates an open system :=(.
2149 * So we put the key into secure memory when we unprotect it. */
2150 if (sk
->protect
.s2k
.mode
== 1001 || sk
->protect
.s2k
.mode
== 1002)
2152 /* Better set some dummy stuff here. */
2153 sk
->skey
[npkey
] = gcry_mpi_set_opaque (NULL
,
2154 xstrdup ("dummydata"),
2158 else if (is_v4
&& sk
->is_protected
)
2160 /* Ugly: The length is encrypted too, so we read all stuff
2161 * up to the end of the packet into the first SKEY
2163 sk
->skey
[npkey
] = gcry_mpi_set_opaque (NULL
,
2164 read_rest (inp
, pktlen
, 0),
2169 fprintf (listfp
, "\tencrypted stuff follows\n");
2172 else /* The v3 method: The mpi length is not encrypted. */
2174 for (i
= npkey
; i
< nskey
; i
++)
2176 if (sk
->is_protected
)
2178 sk
->skey
[i
] = read_protected_v3_mpi (inp
, &pktlen
);
2180 fprintf (listfp
, "\tskey[%d]: [encrypted]\n", i
);
2185 sk
->skey
[i
] = mpi_read (inp
, &n
, 0);
2189 fprintf (listfp
, "\tskey[%d]: ", i
);
2190 mpi_print (listfp
, sk
->skey
[i
], mpi_print_mode
);
2191 putc ('\n', listfp
);
2196 rc
= G10ERR_INVALID_PACKET
;
2201 sk
->csum
= read_16 (inp
);
2205 fprintf (listfp
, "\tchecksum: %04hx\n", sk
->csum
);
2210 keyid_from_sk (sk
, keyid
);
2214 PKT_public_key
*pk
= pkt
->pkt
.public_key
;
2218 pk
->pkey
[0] = gcry_mpi_set_opaque (NULL
,
2219 read_rest (inp
, pktlen
, 0),
2225 for (i
= 0; i
< npkey
; i
++)
2228 pk
->pkey
[i
] = mpi_read (inp
, &n
, 0);
2232 fprintf (listfp
, "\tpkey[%d]: ", i
);
2233 mpi_print (listfp
, pk
->pkey
[i
], mpi_print_mode
);
2234 putc ('\n', listfp
);
2237 rc
= G10ERR_INVALID_PACKET
;
2242 keyid_from_pk (pk
, keyid
);
2246 fprintf (listfp
, "\tkeyid: %08lX%08lX\n",
2247 (ulong
) keyid
[0], (ulong
) keyid
[1]);
2250 iobuf_skip_rest (inp
, pktlen
, 0);
2255 /* Attribute subpackets have the same format as v4 signature
2256 subpackets. This is not part of OpenPGP, but is done in several
2257 versions of PGP nevertheless. */
2259 parse_attribute_subpkts (PKT_user_id
* uid
)
2263 struct user_attribute
*attribs
= NULL
;
2264 const byte
*buffer
= uid
->attrib_data
;
2265 int buflen
= uid
->attrib_len
;
2268 xfree (uid
->attribs
);
2274 if (n
== 255) /* 4 byte length header. */
2278 n
= (buffer
[0] << 24) | (buffer
[1] << 16)
2279 | (buffer
[2] << 8) | buffer
[3];
2283 else if (n
>= 192) /* 2 byte special encoded length header. */
2287 n
= ((n
- 192) << 8) + *buffer
+ 192;
2295 xrealloc (attribs
, (count
+ 1) * sizeof (struct user_attribute
));
2296 memset (&attribs
[count
], 0, sizeof (struct user_attribute
));
2303 attribs
[count
].type
= type
;
2304 attribs
[count
].data
= buffer
;
2305 attribs
[count
].len
= n
;
2311 uid
->attribs
= attribs
;
2312 uid
->numattribs
= count
;
2317 log_info ("buffer shorter than attribute subpacket\n");
2318 uid
->attribs
= attribs
;
2319 uid
->numattribs
= count
;
2325 parse_user_id (IOBUF inp
, int pkttype
, unsigned long pktlen
, PACKET
* packet
)
2329 /* Cap the size of a user ID at 2k: a value absurdly large enough
2330 that there is no sane user ID string (which is printable text
2331 as of RFC2440bis) that won't fit in it, but yet small enough to
2332 avoid allocation problems. A large pktlen may not be
2333 allocatable, and a very large pktlen could actually cause our
2334 allocation to wrap around in xmalloc to a small number. */
2338 log_error ("packet(%d) too large\n", pkttype
);
2339 iobuf_skip_rest (inp
, pktlen
, 0);
2340 return G10ERR_INVALID_PACKET
;
2343 packet
->pkt
.user_id
= xmalloc_clear (sizeof *packet
->pkt
.user_id
+ pktlen
);
2344 packet
->pkt
.user_id
->len
= pktlen
;
2345 packet
->pkt
.user_id
->ref
= 1;
2347 p
= packet
->pkt
.user_id
->name
;
2348 for (; pktlen
; pktlen
--, p
++)
2349 *p
= iobuf_get_noeof (inp
);
2354 int n
= packet
->pkt
.user_id
->len
;
2355 fprintf (listfp
, ":user ID packet: \"");
2356 /* fixme: Hey why don't we replace this with print_string?? */
2357 for (p
= packet
->pkt
.user_id
->name
; n
; p
++, n
--)
2359 if (*p
>= ' ' && *p
<= 'z')
2362 fprintf (listfp
, "\\x%02x", *p
);
2364 fprintf (listfp
, "\"\n");
2371 make_attribute_uidname (PKT_user_id
* uid
, size_t max_namelen
)
2373 assert (max_namelen
> 70);
2374 if (uid
->numattribs
<= 0)
2375 sprintf (uid
->name
, "[bad attribute packet of size %lu]",
2377 else if (uid
->numattribs
> 1)
2378 sprintf (uid
->name
, "[%d attributes of size %lu]",
2379 uid
->numattribs
, uid
->attrib_len
);
2382 /* Only one attribute, so list it as the "user id" */
2384 if (uid
->attribs
->type
== ATTRIB_IMAGE
)
2389 if (parse_image_header (uid
->attribs
, &type
, &len
))
2390 sprintf (uid
->name
, "[%.20s image of size %lu]",
2391 image_type_to_string (type
, 1), (ulong
) len
);
2393 sprintf (uid
->name
, "[invalid image]");
2396 sprintf (uid
->name
, "[unknown attribute of size %lu]",
2397 (ulong
) uid
->attribs
->len
);
2400 uid
->len
= strlen (uid
->name
);
2405 parse_attribute (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2412 #define EXTRA_UID_NAME_SPACE 71
2413 packet
->pkt
.user_id
= xmalloc_clear (sizeof *packet
->pkt
.user_id
2414 + EXTRA_UID_NAME_SPACE
);
2415 packet
->pkt
.user_id
->ref
= 1;
2416 packet
->pkt
.user_id
->attrib_data
= xmalloc (pktlen
);
2417 packet
->pkt
.user_id
->attrib_len
= pktlen
;
2419 p
= packet
->pkt
.user_id
->attrib_data
;
2420 for (; pktlen
; pktlen
--, p
++)
2421 *p
= iobuf_get_noeof (inp
);
2423 /* Now parse out the individual attribute subpackets. This is
2424 somewhat pointless since there is only one currently defined
2425 attribute type (jpeg), but it is correct by the spec. */
2426 parse_attribute_subpkts (packet
->pkt
.user_id
);
2428 make_attribute_uidname (packet
->pkt
.user_id
, EXTRA_UID_NAME_SPACE
);
2432 fprintf (listfp
, ":attribute packet: %s\n", packet
->pkt
.user_id
->name
);
2439 parse_comment (IOBUF inp
, int pkttype
, unsigned long pktlen
, PACKET
* packet
)
2443 /* Cap comment packet at a reasonable value to avoid an integer
2444 overflow in the malloc below. Comment packets are actually not
2445 anymore define my OpenPGP and we even stopped to use our
2446 private comment packet. */
2449 log_error ("packet(%d) too large\n", pkttype
);
2450 iobuf_skip_rest (inp
, pktlen
, 0);
2451 return G10ERR_INVALID_PACKET
;
2453 packet
->pkt
.comment
= xmalloc (sizeof *packet
->pkt
.comment
+ pktlen
- 1);
2454 packet
->pkt
.comment
->len
= pktlen
;
2455 p
= packet
->pkt
.comment
->data
;
2456 for (; pktlen
; pktlen
--, p
++)
2457 *p
= iobuf_get_noeof (inp
);
2461 int n
= packet
->pkt
.comment
->len
;
2462 fprintf (listfp
, ":%scomment packet: \"", pkttype
== PKT_OLD_COMMENT
?
2463 "OpenPGP draft " : "");
2464 for (p
= packet
->pkt
.comment
->data
; n
; p
++, n
--)
2466 if (*p
>= ' ' && *p
<= 'z')
2469 fprintf (listfp
, "\\x%02x", *p
);
2471 fprintf (listfp
, "\"\n");
2478 parse_trust (IOBUF inp
, int pkttype
, unsigned long pktlen
, PACKET
* pkt
)
2486 c
= iobuf_get_noeof (inp
);
2488 pkt
->pkt
.ring_trust
= xmalloc (sizeof *pkt
->pkt
.ring_trust
);
2489 pkt
->pkt
.ring_trust
->trustval
= c
;
2490 pkt
->pkt
.ring_trust
->sigcache
= 0;
2491 if (!c
&& pktlen
== 1)
2493 c
= iobuf_get_noeof (inp
);
2495 /* We require that bit 7 of the sigcache is 0 (easier eof
2498 pkt
->pkt
.ring_trust
->sigcache
= c
;
2501 fprintf (listfp
, ":trust packet: flag=%02x sigcache=%02x\n",
2502 pkt
->pkt
.ring_trust
->trustval
,
2503 pkt
->pkt
.ring_trust
->sigcache
);
2508 fprintf (listfp
, ":trust packet: empty\n");
2510 iobuf_skip_rest (inp
, pktlen
, 0);
2515 parse_plaintext (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2516 PACKET
* pkt
, int new_ctb
, int partial
)
2524 if (!partial
&& pktlen
< 6)
2526 log_error ("packet(%d) too short (%lu)\n", pkttype
, (ulong
) pktlen
);
2527 rc
= gpg_error (GPG_ERR_INV_PACKET
);
2530 mode
= iobuf_get_noeof (inp
);
2533 namelen
= iobuf_get_noeof (inp
);
2536 /* Note that namelen will never exceed 255 bytes. */
2537 pt
= pkt
->pkt
.plaintext
=
2538 xmalloc (sizeof *pkt
->pkt
.plaintext
+ namelen
- 1);
2539 pt
->new_ctb
= new_ctb
;
2541 pt
->namelen
= namelen
;
2542 pt
->is_partial
= partial
;
2545 for (i
= 0; pktlen
> 4 && i
< namelen
; pktlen
--, i
++)
2546 pt
->name
[i
] = iobuf_get_noeof (inp
);
2550 for (i
= 0; i
< namelen
; i
++)
2551 if ((c
= iobuf_get (inp
)) == -1)
2556 pt
->timestamp
= read_32 (inp
);
2565 fprintf (listfp
, ":literal data packet:\n"
2566 "\tmode %c (%X), created %lu, name=\"",
2567 mode
>= ' ' && mode
< 'z' ? mode
: '?', mode
,
2568 (ulong
) pt
->timestamp
);
2569 for (p
= pt
->name
, i
= 0; i
< namelen
; p
++, i
++)
2571 if (*p
>= ' ' && *p
<= 'z')
2574 fprintf (listfp
, "\\x%02x", *p
);
2576 fprintf (listfp
, "\",\n\traw data: ");
2578 fprintf (listfp
, "unknown length\n");
2580 fprintf (listfp
, "%lu bytes\n", (ulong
) pt
->len
);
2589 parse_compressed (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2590 PACKET
* pkt
, int new_ctb
)
2594 /* PKTLEN is here 0, but data follows (this should be the last
2595 object in a file or the compress algorithm should know the
2600 zd
= pkt
->pkt
.compressed
= xmalloc (sizeof *pkt
->pkt
.compressed
);
2601 zd
->algorithm
= iobuf_get_noeof (inp
);
2602 zd
->len
= 0; /* not used */
2603 zd
->new_ctb
= new_ctb
;
2606 fprintf (listfp
, ":compressed packet: algo=%d\n", zd
->algorithm
);
2612 parse_encrypted (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2613 PACKET
* pkt
, int new_ctb
, int partial
)
2617 unsigned long orig_pktlen
= pktlen
;
2619 ed
= pkt
->pkt
.encrypted
= xmalloc (sizeof *pkt
->pkt
.encrypted
);
2620 /* ed->len is set below. */
2621 ed
->extralen
= 0; /* Unknown here; only used in build_packet. */
2623 ed
->new_ctb
= new_ctb
;
2624 ed
->is_partial
= partial
;
2625 if (pkttype
== PKT_ENCRYPTED_MDC
)
2627 /* Fixme: add some pktlen sanity checks. */
2630 version
= iobuf_get_noeof (inp
);
2635 log_error ("encrypted_mdc packet with unknown version %d\n",
2637 /*skip_rest(inp, pktlen); should we really do this? */
2638 rc
= gpg_error (GPG_ERR_INV_PACKET
);
2641 ed
->mdc_method
= DIGEST_ALGO_SHA1
;
2646 /* A basic sanity check. We need at least an 8 byte IV plus the 2
2647 detection bytes. Note that we don't known the algorithm and thus
2648 we may only check against the minimum blocksize. */
2649 if (orig_pktlen
&& pktlen
< 10)
2651 /* Actually this is blocksize+2. */
2652 log_error ("packet(%d) too short\n", pkttype
);
2653 rc
= G10ERR_INVALID_PACKET
;
2654 iobuf_skip_rest (inp
, pktlen
, partial
);
2658 /* Store the remaining length of the encrypted data (i.e. without
2659 the MDC version number but with the IV etc.). This value is
2660 required during decryption. */
2666 fprintf (listfp
, ":encrypted data packet:\n\tlength: %lu\n",
2669 fprintf (listfp
, ":encrypted data packet:\n\tlength: unknown\n");
2671 fprintf (listfp
, "\tmdc_method: %d\n", ed
->mdc_method
);
2681 /* Note, that this code is not anymore used in real life because the
2682 MDC checking is now done right after the decryption in
2685 parse_mdc (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2686 PACKET
* pkt
, int new_ctb
)
2694 mdc
= pkt
->pkt
.mdc
= xmalloc (sizeof *pkt
->pkt
.mdc
);
2696 fprintf (listfp
, ":mdc packet: length=%lu\n", pktlen
);
2697 if (!new_ctb
|| pktlen
!= 20)
2699 log_error ("mdc_packet with invalid encoding\n");
2700 rc
= gpg_error (GPG_ERR_INV_PACKET
);
2704 for (; pktlen
; pktlen
--, p
++)
2705 *p
= iobuf_get_noeof (inp
);
2713 * This packet is internally generated by us (ibn armor.c) to transfer
2714 * some information to the lower layer. To make sure that this packet
2715 * is really a GPG faked one and not one comming from outside, we
2716 * first check that there is a unique tag in it.
2718 * The format of such a control packet is:
2719 * n byte session marker
2720 * 1 byte control type CTRLPKT_xxxxx
2721 * m byte control data
2724 parse_gpg_control (IOBUF inp
, int pkttype
, unsigned long pktlen
,
2725 PACKET
* packet
, int partial
)
2728 const byte
*sesmark
;
2735 fprintf (listfp
, ":packet 63: length %lu ", pktlen
);
2737 sesmark
= get_session_marker (&sesmarklen
);
2738 if (pktlen
< sesmarklen
+ 1) /* 1 is for the control bytes */
2740 for (i
= 0; i
< sesmarklen
; i
++, pktlen
--)
2742 if (sesmark
[i
] != iobuf_get_noeof (inp
))
2746 goto skipit
; /* Definitely too large. We skip it to avoid an
2747 overflow in the malloc. */
2749 puts ("- gpg control packet");
2751 packet
->pkt
.gpg_control
= xmalloc (sizeof *packet
->pkt
.gpg_control
2753 packet
->pkt
.gpg_control
->control
= iobuf_get_noeof (inp
);
2755 packet
->pkt
.gpg_control
->datalen
= pktlen
;
2756 p
= packet
->pkt
.gpg_control
->data
;
2757 for (; pktlen
; pktlen
--, p
++)
2758 *p
= iobuf_get_noeof (inp
);
2768 fprintf (listfp
, "- private (rest length %lu)\n", pktlen
);
2771 while ((c
= iobuf_get (inp
)) != -1)
2772 dump_hex_line (c
, &i
);
2776 for (; pktlen
; pktlen
--)
2778 dump_hex_line ((c
= iobuf_get (inp
)), &i
);
2783 putc ('\n', listfp
);
2785 iobuf_skip_rest (inp
, pktlen
, 0);
2786 return gpg_error (GPG_ERR_INV_PACKET
);
2790 /* Create a GPG control packet to be used internally as a placeholder. */
2792 create_gpg_control (ctrlpkttype_t type
, const byte
* data
, size_t datalen
)
2797 packet
= xmalloc (sizeof *packet
);
2798 init_packet (packet
);
2799 packet
->pkttype
= PKT_GPG_CONTROL
;
2800 packet
->pkt
.gpg_control
= xmalloc (sizeof *packet
->pkt
.gpg_control
2802 packet
->pkt
.gpg_control
->control
= type
;
2803 packet
->pkt
.gpg_control
->datalen
= datalen
;
2804 p
= packet
->pkt
.gpg_control
->data
;
2805 for (; datalen
; datalen
--, p
++)