summaryrefslogtreecommitdiff
path: root/doc/tex/ex-pgp-keyserver.tex
blob: 4d55e3669904e0f931aee4234d5f4055fd966390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
\begin {verbatim}

/* This file is actually an example of using the OpenCDK library
 * to retrieve an OpenPGP key from a key server.
 */

#include <gnutls/gnutls.h>
#include <gnutls/extra.h>
#include <opencdk.h>

/* A callback function that tries to connect
 * to a public keyserver to get the specified key.
 * The callback should be set as:
 *
 * gnutls_openpgp_set_recv_key_function( session, recv_openpgp_key);
 *
 * in the initialization of a gnutls session.
 */

static const char *hostname = "keyserver.somewhere.com";
static const short port = 11371;

int
recv_openpgp_key(const unsigned char *keyfpr, unsigned int
                 keyfpr_length, gnutls_datum * key)
{
   const unsigned char *keyid;
   int rc;
   CDK_KBNODE knode = NULL;

   /* The key fingerprint should be 20 bytes
    */
   if (keyfpr_length != 20)
      return -1;

   /* The key id is the last 4 bytes of the key fingerprint
    */
   keyid = keyfpr[16];

   if (key == NULL) {
      return -1;
   }

   rc = cdk_keyserver_recv_key( hostname, port, keyid, &knode );
   if( !rc ) {
       unsigned char *buf = cdk_calloc( 1, 20001 );
       size_t len = 20000;
       cdk_kbnode_write_to_mem( knode, buf, &len);
       datum_append( key, buf, len );
       cdk_free( buf );
   }
   cdk_kbnode_release( knode );
   return map_cdk_rc( rc );

}


\end{verbatim}