trading keys

Werner Koch wk at gnupg.org
Tue Jul 20 19:54:47 CEST 2004


On Tue, 20 Jul 2004 18:08:15 +0200, salvatore  said:

> Here is how i trade publics key... might be this wrong?!7

Yes.


>	 /* KEY_LENTGH is 1024 */
>         memset(my_pub,0,KEY_LENTGH);

Why using a macro at all if you state above that it is 1024 byte?  Are
you sure about the 1024 byte?  It is not the key length (we usually
count by bits) but the maximum length of the S-expression (in bytes).

        gcry_sexp_sprint(key.my_public_key, GCRYSEXP_FMT_CANON, my_pub, sizeof(my_pub));

No error checking done.

>         /* Send my public */
>         while( (write(sockt, (char *) my_pub, sizeof(my_pub)) ) < 0 )
>                 ;

This will leand to an endless loop as soon as there is a write error -
don't do this.  Casting to char* is unneeded:  ist is already a char*
and write takes a void* anyway.
        
        
>         if( (read(sockt, (char *) his_pub, KEY_LENTGH ) ) < 0 )
>                 return 0;

Same remarks as above.

Why do you use KEY_LENGH here but above sizeof?  Actuall

In general you should send the actual length of the key and not a
fixed buffer.  However your approach works as long as you don't
enlarge the key size.  The actual length is returned by
gcry_sexp_sprint; encode it in network byte order; ie.


  unsigned char length_buffer[4];
  size_t length;

  length = gcry_sexp_sprint (....);
  if (!length)
    die ();

  length_buffer[0] = length >> 24;
  length_buffer[1] = length >> 16;
  length_buffer[2] = length >> 8;
  length_buffer[3] = length;

  if (writen (sock, length_buffer, 4))
    die ();
  if (writen (sock, my_pub, sizeof my_pub))
    die ();
    

writen() is the usual write wrapper, making sure that all bytes are
written and protects against INTR.  See Steven's APUE for example
(This is a must read for everyone doing network stuff or serious Unix
hacking).

  


Salam-Shalom,

   Werner





More information about the Gcrypt-devel mailing list