1 /* build-packet.c - assemble packets and write them
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 * 2006 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
39 static int do_user_id( IOBUF out
, int ctb
, PKT_user_id
*uid
);
40 static int do_public_key( IOBUF out
, int ctb
, PKT_public_key
*pk
);
41 static int do_secret_key( IOBUF out
, int ctb
, PKT_secret_key
*pk
);
42 static int do_symkey_enc( IOBUF out
, int ctb
, PKT_symkey_enc
*enc
);
43 static int do_pubkey_enc( IOBUF out
, int ctb
, PKT_pubkey_enc
*enc
);
44 static u32
calc_plaintext( PKT_plaintext
*pt
);
45 static int do_plaintext( IOBUF out
, int ctb
, PKT_plaintext
*pt
);
46 static int do_encrypted( IOBUF out
, int ctb
, PKT_encrypted
*ed
);
47 static int do_encrypted_mdc( IOBUF out
, int ctb
, PKT_encrypted
*ed
);
48 static int do_compressed( IOBUF out
, int ctb
, PKT_compressed
*cd
);
49 static int do_signature( IOBUF out
, int ctb
, PKT_signature
*sig
);
50 static int do_onepass_sig( IOBUF out
, int ctb
, PKT_onepass_sig
*ops
);
52 static int calc_header_length( u32 len
, int new_ctb
);
53 static int write_16(IOBUF inp
, u16 a
);
54 static int write_32(IOBUF inp
, u32 a
);
55 static int write_header( IOBUF out
, int ctb
, u32 len
);
56 static int write_sign_packet_header( IOBUF out
, int ctb
, u32 len
);
57 static int write_header2( IOBUF out
, int ctb
, u32 len
, int hdrlen
);
58 static int write_new_header( IOBUF out
, int ctb
, u32 len
, int hdrlen
);
59 static int write_version( IOBUF out
, int ctb
);
62 * Build a packet and write it to INP
65 * Note: Caller must free the packet
68 build_packet( IOBUF out
, PACKET
*pkt
)
70 int new_ctb
=0, rc
=0, ctb
;
74 log_debug("build_packet() type=%d\n", pkt
->pkttype
);
75 assert( pkt
->pkt
.generic
);
77 switch( (pkttype
= pkt
->pkttype
) )
79 case PKT_PLAINTEXT
: new_ctb
= pkt
->pkt
.plaintext
->new_ctb
; break;
81 case PKT_ENCRYPTED_MDC
: new_ctb
= pkt
->pkt
.encrypted
->new_ctb
; break;
82 case PKT_COMPRESSED
:new_ctb
= pkt
->pkt
.compressed
->new_ctb
; break;
84 if( pkt
->pkt
.user_id
->attrib_data
)
85 pkttype
= PKT_ATTRIBUTE
;
90 if( new_ctb
|| pkttype
> 15 ) /* new format */
91 ctb
= 0xc0 | (pkttype
& 0x3f);
93 ctb
= 0x80 | ((pkttype
& 15)<<2);
98 rc
= do_user_id( out
, ctb
, pkt
->pkt
.user_id
);
100 case PKT_OLD_COMMENT
:
103 Ignore these. Theoretically, this will never be called as
104 we have no way to output comment packets any longer, but
105 just in case there is some code path that would end up
106 outputting a comment that was written before comments were
107 dropped (in the public key?) this is a no-op.
110 case PKT_PUBLIC_SUBKEY
:
112 rc
= do_public_key( out
, ctb
, pkt
->pkt
.public_key
);
114 case PKT_SECRET_SUBKEY
:
116 rc
= do_secret_key( out
, ctb
, pkt
->pkt
.secret_key
);
119 rc
= do_symkey_enc( out
, ctb
, pkt
->pkt
.symkey_enc
);
122 rc
= do_pubkey_enc( out
, ctb
, pkt
->pkt
.pubkey_enc
);
125 rc
= do_plaintext( out
, ctb
, pkt
->pkt
.plaintext
);
128 rc
= do_encrypted( out
, ctb
, pkt
->pkt
.encrypted
);
130 case PKT_ENCRYPTED_MDC
:
131 rc
= do_encrypted_mdc( out
, ctb
, pkt
->pkt
.encrypted
);
134 rc
= do_compressed( out
, ctb
, pkt
->pkt
.compressed
);
137 rc
= do_signature( out
, ctb
, pkt
->pkt
.signature
);
139 case PKT_ONEPASS_SIG
:
140 rc
= do_onepass_sig( out
, ctb
, pkt
->pkt
.onepass_sig
);
143 break; /* ignore it (keyring.c does write it directly)*/
144 case PKT_MDC
: /* we write it directly, so we should never see it here. */
146 log_bug("invalid packet type in build_packet()\n");
155 * Write the mpi A to OUT.
158 mpi_write (iobuf_t out
, gcry_mpi_t a
)
160 char buffer
[(MAX_EXTERN_MPI_BITS
+7)/8];
164 nbytes
= (MAX_EXTERN_MPI_BITS
+7)/8;
165 rc
= gcry_mpi_print (GCRYMPI_FMT_PGP
, buffer
, nbytes
, &nbytes
, a
);
167 rc
= iobuf_write( out
, buffer
, nbytes
);
175 * calculate the length of a packet described by PKT
178 calc_packet_length( PACKET
*pkt
)
183 assert( pkt
->pkt
.generic
);
184 switch( pkt
->pkttype
) {
186 n
= calc_plaintext( pkt
->pkt
.plaintext
);
187 new_ctb
= pkt
->pkt
.plaintext
->new_ctb
;
198 case PKT_ONEPASS_SIG
:
202 log_bug("invalid packet type in calc_packet_length()");
206 n
+= calc_header_length(n
, new_ctb
);
211 write_fake_data (IOBUF out
, gcry_mpi_t a
)
218 p
= gcry_mpi_get_opaque ( a
, &n
);
219 iobuf_write (out
, p
, (n
+7)/8 );
224 do_user_id( IOBUF out
, int ctb
, PKT_user_id
*uid
)
228 if( uid
->attrib_data
)
230 write_header(out
, ctb
, uid
->attrib_len
);
231 rc
= iobuf_write( out
, uid
->attrib_data
, uid
->attrib_len
);
235 write_header2( out
, ctb
, uid
->len
, 2 );
236 rc
= iobuf_write( out
, uid
->name
, uid
->len
);
242 do_public_key( IOBUF out
, int ctb
, PKT_public_key
*pk
)
246 IOBUF a
= iobuf_temp();
251 iobuf_put( a
, pk
->version
);
252 write_32(a
, pk
->timestamp
);
253 if( pk
->version
< 4 ) {
256 ndays
= (u16
)((pk
->expiredate
- pk
->timestamp
) / 86400L);
261 iobuf_put(a
, pk
->pubkey_algo
);
262 n
= pubkey_get_npkey( pk
->pubkey_algo
);
264 write_fake_data( a
, pk
->pkey
[0] );
265 for(i
=0; i
< n
; i
++ )
266 mpi_write(a
, pk
->pkey
[i
] );
268 write_header2(out
, ctb
, iobuf_get_temp_length(a
), pk
->hdrbytes
);
269 rc
= iobuf_write_temp( out
, a
);
277 do_secret_key( IOBUF out
, int ctb
, PKT_secret_key
*sk
)
281 IOBUF a
= iobuf_temp(); /* build in a self-enlarging buffer */
283 /* Write the version number - if none is specified, use 3 */
287 iobuf_put( a
, sk
->version
);
288 write_32(a
, sk
->timestamp
);
290 /* v3 needs the expiration time */
291 if( sk
->version
< 4 ) {
294 ndays
= (u16
)((sk
->expiredate
- sk
->timestamp
) / 86400L);
300 iobuf_put(a
, sk
->pubkey_algo
);
302 /* get number of secret and public parameters. They are held in
303 one array first the public ones, then the secret ones */
304 nskey
= pubkey_get_nskey( sk
->pubkey_algo
);
305 npkey
= pubkey_get_npkey( sk
->pubkey_algo
);
307 /* If we don't have any public parameters - which is the case if
308 we don't know the algorithm used - the parameters are stored as
309 one blob in a faked (opaque) MPI */
311 write_fake_data( a
, sk
->skey
[0] );
314 assert( npkey
< nskey
);
316 /* Writing the public parameters is easy */
317 for(i
=0; i
< npkey
; i
++ )
318 mpi_write(a
, sk
->skey
[i
] );
320 /* build the header for protected (encrypted) secret parameters */
321 if( sk
->is_protected
) {
322 if( is_RSA(sk
->pubkey_algo
) && sk
->version
< 4
323 && !sk
->protect
.s2k
.mode
) {
324 /* the simple rfc1991 (v3) way */
325 iobuf_put(a
, sk
->protect
.algo
);
326 iobuf_write(a
, sk
->protect
.iv
, sk
->protect
.ivlen
);
329 /* OpenPGP protection according to rfc2440 */
330 iobuf_put(a
, sk
->protect
.sha1chk
? 0xfe : 0xff );
331 iobuf_put(a
, sk
->protect
.algo
);
332 if( sk
->protect
.s2k
.mode
>= 1000 ) {
333 /* These modes are not possible in OpenPGP, we use them
334 to implement our extensions, 101 can be seen as a
335 private/experimental extension (this is not
336 specified in rfc2440 but the same scheme is used
337 for all other algorithm identifiers) */
339 iobuf_put(a
, sk
->protect
.s2k
.hash_algo
);
340 iobuf_write(a
, "GNU", 3 );
341 iobuf_put(a
, sk
->protect
.s2k
.mode
- 1000 );
344 iobuf_put(a
, sk
->protect
.s2k
.mode
);
345 iobuf_put(a
, sk
->protect
.s2k
.hash_algo
);
347 if( sk
->protect
.s2k
.mode
== 1
348 || sk
->protect
.s2k
.mode
== 3 )
349 iobuf_write(a
, sk
->protect
.s2k
.salt
, 8 );
350 if( sk
->protect
.s2k
.mode
== 3 )
351 iobuf_put(a
, sk
->protect
.s2k
.count
);
353 /* For out special modes 1001, 1002 we do not need an IV */
354 if( sk
->protect
.s2k
.mode
!= 1001
355 && sk
->protect
.s2k
.mode
!= 1002 )
356 iobuf_write(a
, sk
->protect
.iv
, sk
->protect
.ivlen
);
362 if( sk
->protect
.s2k
.mode
== 1001 )
363 ; /* GnuPG extension - don't write a secret key at all */
364 else if( sk
->protect
.s2k
.mode
== 1002 )
365 { /* GnuPG extension - divert to OpenPGP smartcard. */
366 iobuf_put(a
, sk
->protect
.ivlen
); /* length of the serial
367 number or 0 for no serial
369 /* The serial number gets stored in the IV field. */
370 iobuf_write(a
, sk
->protect
.iv
, sk
->protect
.ivlen
);
372 else if( sk
->is_protected
&& sk
->version
>= 4 ) {
373 /* The secret key is protected - write it out as it is */
375 unsigned int ndatabits
;
377 assert (gcry_mpi_get_flag (sk
->skey
[npkey
], GCRYMPI_FLAG_OPAQUE
));
378 p
= gcry_mpi_get_opaque (sk
->skey
[npkey
], &ndatabits
);
379 iobuf_write (a
, p
, (ndatabits
+7)/8 );
381 else if( sk
->is_protected
) {
382 /* The secret key is protected te old v4 way. */
383 for( ; i
< nskey
; i
++ ) {
385 unsigned int ndatabits
;
387 assert (gcry_mpi_get_flag (sk
->skey
[i
], GCRYMPI_FLAG_OPAQUE
));
388 p
= gcry_mpi_get_opaque (sk
->skey
[i
], &ndatabits
);
389 iobuf_write (a
, p
, (ndatabits
+7)/8);
391 write_16(a
, sk
->csum
);
394 /* non-protected key */
395 for( ; i
< nskey
; i
++ )
396 mpi_write(a
, sk
->skey
[i
] );
397 write_16(a
, sk
->csum
);
401 /* Build the header of the packet - which we must do after writing all
402 the other stuff, so that we know the length of the packet */
403 write_header2(out
, ctb
, iobuf_get_temp_length(a
), sk
->hdrbytes
);
404 /* And finally write it out the real stream */
405 rc
= iobuf_write_temp( out
, a
);
407 iobuf_close(a
); /* close the remporary buffer */
412 do_symkey_enc( IOBUF out
, int ctb
, PKT_symkey_enc
*enc
)
415 IOBUF a
= iobuf_temp();
417 assert( enc
->version
== 4 );
418 switch( enc
->s2k
.mode
) {
419 case 0: case 1: case 3: break;
420 default: log_bug("do_symkey_enc: s2k=%d\n", enc
->s2k
.mode
);
422 iobuf_put( a
, enc
->version
);
423 iobuf_put( a
, enc
->cipher_algo
);
424 iobuf_put( a
, enc
->s2k
.mode
);
425 iobuf_put( a
, enc
->s2k
.hash_algo
);
426 if( enc
->s2k
.mode
== 1 || enc
->s2k
.mode
== 3 ) {
427 iobuf_write(a
, enc
->s2k
.salt
, 8 );
428 if( enc
->s2k
.mode
== 3 )
429 iobuf_put(a
, enc
->s2k
.count
);
432 iobuf_write(a
, enc
->seskey
, enc
->seskeylen
);
434 write_header(out
, ctb
, iobuf_get_temp_length(a
) );
435 rc
= iobuf_write_temp( out
, a
);
443 do_pubkey_enc( IOBUF out
, int ctb
, PKT_pubkey_enc
*enc
)
447 IOBUF a
= iobuf_temp();
449 write_version( a
, ctb
);
450 if( enc
->throw_keyid
) {
451 write_32(a
, 0 ); /* don't tell Eve who can decrypt the message */
455 write_32(a
, enc
->keyid
[0] );
456 write_32(a
, enc
->keyid
[1] );
458 iobuf_put(a
,enc
->pubkey_algo
);
459 n
= pubkey_get_nenc( enc
->pubkey_algo
);
461 write_fake_data( a
, enc
->data
[0] );
462 for(i
=0; i
< n
; i
++ )
463 mpi_write(a
, enc
->data
[i
] );
465 write_header(out
, ctb
, iobuf_get_temp_length(a
) );
466 rc
= iobuf_write_temp( out
, a
);
474 calc_plaintext( PKT_plaintext
*pt
)
476 /* Truncate namelen to the maximum 255 characters. Note this means
477 that a function that calls build_packet with an illegal literal
478 packet will get it back legalized. */
483 return pt
->len
? (1 + 1 + pt
->namelen
+ 4 + pt
->len
) : 0;
487 do_plaintext( IOBUF out
, int ctb
, PKT_plaintext
*pt
)
491 byte buf
[1000]; /* this buffer has the plaintext! */
494 write_header(out
, ctb
, calc_plaintext( pt
) );
495 iobuf_put(out
, pt
->mode
);
496 iobuf_put(out
, pt
->namelen
);
497 for(i
=0; i
< pt
->namelen
; i
++ )
498 iobuf_put(out
, pt
->name
[i
] );
499 rc
= write_32(out
, pt
->timestamp
);
504 while( (nbytes
=iobuf_read(pt
->buf
, buf
, 1000)) != -1 ) {
505 rc
= iobuf_write (out
, buf
, nbytes
);
510 wipememory(buf
,1000); /* burn the buffer */
511 if( (ctb
&0x40) && !pt
->len
)
512 iobuf_set_partial_block_mode(out
, 0 ); /* turn off partial */
513 if( pt
->len
&& n
!= pt
->len
)
514 log_error("do_plaintext(): wrote %lu bytes but expected %lu bytes\n",
515 (ulong
)n
, (ulong
)pt
->len
);
523 do_encrypted( IOBUF out
, int ctb
, PKT_encrypted
*ed
)
528 n
= ed
->len
? (ed
->len
+ ed
->extralen
) : 0;
529 write_header(out
, ctb
, n
);
531 /* This is all. The caller has to write the real data */
537 do_encrypted_mdc( IOBUF out
, int ctb
, PKT_encrypted
*ed
)
542 assert( ed
->mdc_method
);
544 /* Take version number and the following MDC packet in account. */
545 n
= ed
->len
? (ed
->len
+ ed
->extralen
+ 1 + 22) : 0;
546 write_header(out
, ctb
, n
);
547 iobuf_put(out
, 1 ); /* version */
549 /* This is all. The caller has to write the real data */
556 do_compressed( IOBUF out
, int ctb
, PKT_compressed
*cd
)
560 /* We must use the old convention and don't use blockmode for tyhe
561 sake of PGP 2 compatibility. However if the new_ctb flag was
562 set, CTB is already formatted as new style and write_header2
563 does create a partial length encoding using new the new
565 write_header2(out
, ctb
, 0, 0);
566 iobuf_put(out
, cd
->algorithm
);
568 /* This is all. The caller has to write the real data */
575 * Delete all subpackets of type REQTYPE and return a bool whether a packet
579 delete_sig_subpkt (subpktarea_t
*area
, sigsubpkttype_t reqtype
)
582 sigsubpkttype_t type
;
583 byte
*buffer
, *bufstart
;
598 n
= *buffer
++; buflen
--;
602 n
= (buffer
[0] << 24) | (buffer
[1] << 16)
603 | (buffer
[2] << 8) | buffer
[3];
607 else if( n
>= 192 ) {
610 n
= (( n
- 192 ) << 8) + *buffer
+ 192;
617 type
= *buffer
& 0x7f;
618 if( type
== reqtype
) {
624 buffer
+= n
; /* point to next subpkt */
626 memmove (bufstart
, buffer
, buflen
); /* shift */
627 unused
+= buffer
- bufstart
;
631 buffer
+= n
; buflen
-=n
;
636 log_error ("delete_subpkt: buffer shorter than subpacket\n");
637 assert (unused
<= area
->len
);
644 * Create or update a signature subpacket for SIG of TYPE. This
645 * functions knows where to put the data (hashed or unhashed). The
646 * function may move data from the unhashed part to the hashed one.
647 * Note: All pointers into sig->[un]hashed (e.g. returned by
648 * parse_sig_subpkt) are not valid after a call to this function. The
649 * data to put into the subpaket should be in a buffer with a length
653 build_sig_subpkt (PKT_signature
*sig
, sigsubpkttype_t type
,
654 const byte
*buffer
, size_t buflen
)
657 int critical
, hashed
;
658 subpktarea_t
*oldarea
, *newarea
;
661 critical
= (type
& SIGSUBPKT_FLAG_CRITICAL
);
662 type
&= ~SIGSUBPKT_FLAG_CRITICAL
;
664 /* Sanity check buffer sizes */
665 if(parse_one_sig_subpkt(buffer
,buflen
,type
)<0)
670 case SIGSUBPKT_NOTATION
:
671 case SIGSUBPKT_POLICY
:
672 case SIGSUBPKT_REV_KEY
:
673 case SIGSUBPKT_SIGNATURE
:
674 /* we do allow multiple subpackets */
678 /* we don't allow multiple subpackets */
679 delete_sig_subpkt(sig
->hashed
,type
);
680 delete_sig_subpkt(sig
->unhashed
,type
);
684 /* Any special magic that needs to be done for this type so the
685 packet doesn't need to be reparsed? */
688 case SIGSUBPKT_NOTATION
:
689 sig
->flags
.notation
=1;
692 case SIGSUBPKT_POLICY
:
693 sig
->flags
.policy_url
=1;
696 case SIGSUBPKT_PREF_KS
:
697 sig
->flags
.pref_ks
=1;
700 case SIGSUBPKT_EXPORTABLE
:
702 sig
->flags
.exportable
=1;
704 sig
->flags
.exportable
=0;
707 case SIGSUBPKT_REVOCABLE
:
709 sig
->flags
.revocable
=1;
711 sig
->flags
.revocable
=0;
714 case SIGSUBPKT_TRUST
:
715 sig
->trust_depth
=buffer
[0];
716 sig
->trust_value
=buffer
[1];
719 case SIGSUBPKT_REGEXP
:
720 sig
->trust_regexp
=buffer
;
723 /* This should never happen since we don't currently allow
724 creating such a subpacket, but just in case... */
725 case SIGSUBPKT_SIG_EXPIRE
:
726 if(buffer_to_u32(buffer
)+sig
->timestamp
<=make_timestamp())
727 sig
->flags
.expired
=1;
729 sig
->flags
.expired
=0;
736 if( (buflen
+1) >= 8384 )
737 nlen
= 5; /* write 5 byte length header */
738 else if( (buflen
+1) >= 192 )
739 nlen
= 2; /* write 2 byte length header */
741 nlen
= 1; /* just a 1 byte length header */
745 /* The issuer being unhashed is a historical oddity. It
746 should work equally as well hashed. Of course, if even an
747 unhashed issuer is tampered with, it makes it awfully hard
748 to verify the sig... */
749 case SIGSUBPKT_ISSUER
:
750 case SIGSUBPKT_SIGNATURE
:
759 type
|= SIGSUBPKT_FLAG_CRITICAL
;
761 oldarea
= hashed
? sig
->hashed
: sig
->unhashed
;
763 /* Calculate new size of the area and allocate */
764 n0
= oldarea
? oldarea
->len
: 0;
765 n
= n0
+ nlen
+ 1 + buflen
; /* length, type, buffer */
766 if (oldarea
&& n
<= oldarea
->size
) { /* fits into the unused space */
768 /*log_debug ("updating area for type %d\n", type );*/
771 newarea
= xrealloc (oldarea
, sizeof (*newarea
) + n
- 1);
773 /*log_debug ("reallocating area for type %d\n", type );*/
776 newarea
= xmalloc (sizeof (*newarea
) + n
- 1);
778 /*log_debug ("allocating area for type %d\n", type );*/
782 p
= newarea
->data
+ n0
;
785 *p
++ = (buflen
+1) >> 24;
786 *p
++ = (buflen
+1) >> 16;
787 *p
++ = (buflen
+1) >> 8;
790 memcpy (p
, buffer
, buflen
);
792 else if (nlen
== 2) {
793 *p
++ = (buflen
+1-192) / 256 + 192;
794 *p
++ = (buflen
+1-192) % 256;
796 memcpy (p
, buffer
, buflen
);
801 memcpy (p
, buffer
, buflen
);
805 sig
->hashed
= newarea
;
807 sig
->unhashed
= newarea
;
811 * Put all the required stuff from SIG into subpackets of sig.
812 * Hmmm, should we delete those subpackets which are in a wrong area?
815 build_sig_subpkt_from_sig( PKT_signature
*sig
)
821 buf
[0] = (u
>> 24) & 0xff;
822 buf
[1] = (u
>> 16) & 0xff;
823 buf
[2] = (u
>> 8) & 0xff;
826 buf
[4] = (u
>> 24) & 0xff;
827 buf
[5] = (u
>> 16) & 0xff;
828 buf
[6] = (u
>> 8) & 0xff;
830 build_sig_subpkt( sig
, SIGSUBPKT_ISSUER
, buf
, 8 );
833 buf
[0] = (u
>> 24) & 0xff;
834 buf
[1] = (u
>> 16) & 0xff;
835 buf
[2] = (u
>> 8) & 0xff;
837 build_sig_subpkt( sig
, SIGSUBPKT_SIG_CREATED
, buf
, 4 );
841 if(sig
->expiredate
>sig
->timestamp
)
842 u
=sig
->expiredate
-sig
->timestamp
;
844 u
=1; /* A 1-second expiration time is the shortest one
847 buf
[0] = (u
>> 24) & 0xff;
848 buf
[1] = (u
>> 16) & 0xff;
849 buf
[2] = (u
>> 8) & 0xff;
852 /* Mark this CRITICAL, so if any implementation doesn't
853 understand sigs that can expire, it'll just disregard this
856 build_sig_subpkt( sig
, SIGSUBPKT_SIG_EXPIRE
| SIGSUBPKT_FLAG_CRITICAL
,
862 build_attribute_subpkt(PKT_user_id
*uid
,byte type
,
863 const void *buf
,u32 buflen
,
864 const void *header
,u32 headerlen
)
869 if(1+headerlen
+buflen
>8383)
871 else if(1+headerlen
+buflen
>191)
876 /* realloc uid->attrib_data to the right size */
878 uid
->attrib_data
=xrealloc(uid
->attrib_data
,
879 uid
->attrib_len
+idx
+1+headerlen
+buflen
);
881 attrib
=&uid
->attrib_data
[uid
->attrib_len
];
886 attrib
[1]=(1+headerlen
+buflen
) >> 24;
887 attrib
[2]=(1+headerlen
+buflen
) >> 16;
888 attrib
[3]=(1+headerlen
+buflen
) >> 8;
889 attrib
[4]=1+headerlen
+buflen
;
893 attrib
[0]=(1+headerlen
+buflen
-192) / 256 + 192;
894 attrib
[1]=(1+headerlen
+buflen
-192) % 256;
897 attrib
[0]=1+headerlen
+buflen
; /* Good luck finding a JPEG this small! */
901 /* Tack on our data at the end */
904 memcpy(&attrib
[idx
],header
,headerlen
);
905 memcpy(&attrib
[idx
+headerlen
],buf
,buflen
);
906 uid
->attrib_len
+=idx
+headerlen
+buflen
;
910 string_to_notation(const char *string
,int is_utf8
)
914 struct notation
*notation
;
916 notation
=xmalloc_clear(sizeof(*notation
));
920 notation
->flags
.ignore
=1;
926 notation
->flags
.critical
=1;
930 /* If and when the IETF assigns some official name tags, we'll have
933 for( s
=string
; *s
!= '='; s
++ )
938 /* -notationname is legal without an = sign */
939 if(!*s
&& notation
->flags
.ignore
)
942 if( !*s
|| !isascii (*s
) || (!isgraph(*s
) && !isspace(*s
)) )
944 log_error(_("a notation name must have only printable characters"
945 " or spaces, and end with an '='\n") );
950 notation
->name
=xmalloc((s
-string
)+1);
951 strncpy(notation
->name
,string
,s
-string
);
952 notation
->name
[s
-string
]='\0';
954 if(!saw_at
&& !opt
.expert
)
956 log_error(_("a user notation name must contain the '@' character\n"));
962 log_error(_("a notation name must not contain more than"
963 " one '@' character\n"));
972 /* we only support printable text - therefore we enforce the use
973 of only printable characters (an empty value is valid) */
978 else if (iscntrl(*s
))
980 log_error(_("a notation value must not use any"
981 " control characters\n"));
986 if(!highbit
|| is_utf8
)
987 notation
->value
=xstrdup(i
);
989 notation
->value
=native_to_utf8(i
);
995 free_notation(notation
);
1000 sig_to_notation(PKT_signature
*sig
)
1005 struct notation
*list
=NULL
;
1007 while((p
=enum_sig_subpkt(sig
->hashed
,SIGSUBPKT_NOTATION
,&len
,&seq
,&crit
)))
1010 struct notation
*n
=NULL
;
1014 log_info(_("WARNING: invalid notation data found\n"));
1023 log_info(_("WARNING: invalid notation data found\n"));
1027 n
=xmalloc_clear(sizeof(*n
));
1028 n
->name
=xmalloc(n1
+1);
1030 memcpy(n
->name
,&p
[8],n1
);
1035 n
->value
=xmalloc(n2
+1);
1036 memcpy(n
->value
,&p
[8+n1
],n2
);
1041 n
->bdat
=xmalloc(n2
);
1043 memcpy(n
->bdat
,&p
[8+n1
],n2
);
1045 n
->value
=xmalloc(2+strlen(_("not human readable"))+2+1);
1046 strcpy(n
->value
,"[ ");
1047 strcat(n
->value
,_("not human readable"));
1048 strcat(n
->value
," ]");
1051 n
->flags
.critical
=crit
;
1061 free_notation(struct notation
*notation
)
1065 struct notation
*n
=notation
;
1077 do_signature( IOBUF out
, int ctb
, PKT_signature
*sig
)
1081 IOBUF a
= iobuf_temp();
1086 iobuf_put( a
, sig
->version
);
1087 if( sig
->version
< 4 )
1088 iobuf_put(a
, 5 ); /* constant */
1089 iobuf_put(a
, sig
->sig_class
);
1090 if( sig
->version
< 4 ) {
1091 write_32(a
, sig
->timestamp
);
1092 write_32(a
, sig
->keyid
[0] );
1093 write_32(a
, sig
->keyid
[1] );
1095 iobuf_put(a
, sig
->pubkey_algo
);
1096 iobuf_put(a
, sig
->digest_algo
);
1097 if( sig
->version
>= 4 ) {
1099 /* timestamp and keyid must have been packed into the
1100 * subpackets prior to the call of this function, because
1101 * these subpackets are hashed */
1102 nn
= sig
->hashed
? sig
->hashed
->len
: 0;
1105 iobuf_write( a
, sig
->hashed
->data
, nn
);
1106 nn
= sig
->unhashed
? sig
->unhashed
->len
: 0;
1109 iobuf_write( a
, sig
->unhashed
->data
, nn
);
1111 iobuf_put(a
, sig
->digest_start
[0] );
1112 iobuf_put(a
, sig
->digest_start
[1] );
1113 n
= pubkey_get_nsig( sig
->pubkey_algo
);
1115 write_fake_data( a
, sig
->data
[0] );
1116 for(i
=0; i
< n
; i
++ )
1117 mpi_write(a
, sig
->data
[i
] );
1119 if( is_RSA(sig
->pubkey_algo
) && sig
->version
< 4 )
1120 write_sign_packet_header(out
, ctb
, iobuf_get_temp_length(a
) );
1122 write_header(out
, ctb
, iobuf_get_temp_length(a
) );
1123 rc
= iobuf_write_temp( out
, a
);
1131 do_onepass_sig( IOBUF out
, int ctb
, PKT_onepass_sig
*ops
)
1134 IOBUF a
= iobuf_temp();
1136 write_version( a
, ctb
);
1137 iobuf_put(a
, ops
->sig_class
);
1138 iobuf_put(a
, ops
->digest_algo
);
1139 iobuf_put(a
, ops
->pubkey_algo
);
1140 write_32(a
, ops
->keyid
[0] );
1141 write_32(a
, ops
->keyid
[1] );
1142 iobuf_put(a
, ops
->last
);
1144 write_header(out
, ctb
, iobuf_get_temp_length(a
) );
1145 rc
= iobuf_write_temp( out
, a
);
1153 write_16(IOBUF out
, u16 a
)
1155 iobuf_put(out
, a
>>8);
1156 if( iobuf_put(out
,a
) )
1162 write_32(IOBUF out
, u32 a
)
1164 iobuf_put(out
, a
>> 24);
1165 iobuf_put(out
, a
>> 16);
1166 iobuf_put(out
, a
>> 8);
1167 return iobuf_put(out
, a
);
1172 * calculate the length of a header
1175 calc_header_length( u32 len
, int new_ctb
)
1178 return 1; /* only the ctb */
1197 * Write the CTB and the packet length
1200 write_header( IOBUF out
, int ctb
, u32 len
)
1202 return write_header2( out
, ctb
, len
, 0 );
1207 write_sign_packet_header( IOBUF out
, int ctb
, u32 len
)
1209 /* work around a bug in the pgp read function for signature packets,
1210 * which are not correctly coded and silently assume at some
1211 * point 2 byte length headers.*/
1212 iobuf_put(out
, 0x89 );
1213 iobuf_put(out
, len
>> 8 );
1214 return iobuf_put(out
, len
) == -1 ? -1:0;
1218 * If HDRLEN is > 0, try to build a header of this length. We need
1219 * this so that we can hash packets without reading them again. If
1220 * len is 0, write a partial or indeterminate length header, unless
1221 * hdrlen is specified in which case write an actual zero length
1222 * (using the specified hdrlen).
1225 write_header2( IOBUF out
, int ctb
, u32 len
, int hdrlen
)
1228 return write_new_header( out
, ctb
, len
, hdrlen
);
1232 if( hdrlen
== 2 && len
< 256 )
1234 else if( hdrlen
== 3 && len
< 65536 )
1243 else if( len
< 256 )
1245 else if( len
< 65536 )
1251 if( iobuf_put(out
, ctb
) )
1258 if(iobuf_put(out
, len
>> 24 ))
1260 if(iobuf_put(out
, len
>> 16 ))
1265 if(iobuf_put(out
, len
>> 8 ))
1268 if( iobuf_put(out
, len
) )
1277 write_new_header( IOBUF out
, int ctb
, u32 len
, int hdrlen
)
1280 log_bug("can't cope with hdrlen yet\n");
1282 if( iobuf_put(out
, ctb
) )
1285 iobuf_set_partial_block_mode(out
, 512 );
1289 if( iobuf_put(out
, len
) )
1292 else if( len
< 8384 ) {
1294 if( iobuf_put( out
, (len
/ 256) + 192) )
1296 if( iobuf_put( out
, (len
% 256) ) )
1300 if( iobuf_put( out
, 0xff ) )
1302 if( iobuf_put( out
, (len
>> 24)&0xff ) )
1304 if( iobuf_put( out
, (len
>> 16)&0xff ) )
1306 if( iobuf_put( out
, (len
>> 8)&0xff ) )
1308 if( iobuf_put( out
, len
& 0xff ) )
1316 write_version( IOBUF out
, int ctb
)
1318 if( iobuf_put( out
, 3 ) )