Merge branch 'master' of git://repo.or.cz/gnutls
[gnutls.git] / tests / openpgp_test.c
blobfa87b232705bee0351d163c2e3c8893450a99b2f
1 /** t-openpgp.c -- OpenPGP regression test **/
3 #include "gnutls_int.h"
4 #include "gnutls_errors.h"
5 #include "gnutls_mpi.h"
6 #include "gnutls_cert.h"
7 #include "gnutls_datum.h"
8 #include "gnutls_global.h"
9 #include "auth_cert.h"
10 #include "gnutls_openpgp.h"
12 #include <gnutls_str.h>
13 #include <stdio.h>
14 #include <gcrypt.h>
15 #include <time.h>
16 #include <assert.h>
18 static const char *
19 get_pkalgo (int algo)
21 switch (algo)
23 case GNUTLS_PK_DSA:
24 return "DSA";
25 case GNUTLS_PK_RSA:
26 return "RSA";
28 return NULL;
31 static const char *
32 get_pktime (long timestamp)
34 static char buf[128];
35 struct tm *tb;
37 tb = localtime (&timestamp);
38 sprintf (buf, "%04d-%02d-%02d", tb->tm_year + 1900, tb->tm_mon + 1,
39 tb->tm_mday);
40 return buf;
43 int
44 get_pubkey (gnutls_datum * pk, const gnutls_datum * kr, unsigned long kid)
46 unsigned char buf[4];
48 buf[0] = kid >> 24;
49 buf[1] = kid >> 16;
50 buf[2] = kid >> 8;
51 buf[3] = kid;
52 return gnutls_openpgp_get_key (pk, kr, KEY_ATTR_SHORT_KEYID, buf);
56 int
57 main (int argc, char **argv)
59 gnutls_certificate_credentials ctx;
60 gnutls_datum dat, xml, pk;
61 gnutls_openpgp_name uid;
62 gnutls_privkey *pkey;
63 gnutls_cert *cert;
64 unsigned char fpr[20], keyid[8];
65 char *s, *t;
66 size_t fprlen = 0;
67 int rc, nbits = 0, i;
69 rc = gnutls_certificate_allocate_credentials (&ctx);
70 assert (rc == 0);
72 s = "../src/openpgp/cli_ring.gpg";
73 rc = gnutls_certificate_set_openpgp_keyring_file (ctx, s);
74 assert (rc == 0);
76 s = "../src/openpgp/pub.asc";
77 t = "../src/openpgp/sec.asc";
78 rc = gnutls_certificate_set_openpgp_key_file (ctx, s, t);
79 assert (rc == 0);
81 dat = ctx->cert_list[0]->raw;
82 assert (ctx->cert_list[0]);
83 printf ("Key v%d\n", gnutls_openpgp_extract_key_version (&dat));
84 rc = gnutls_openpgp_extract_key_name (&dat, 1, &uid);
85 assert (rc == 0);
86 printf ("userID %s\n", uid.name);
88 rc = gnutls_openpgp_extract_key_pk_algorithm (&dat, &nbits);
89 printf ("pk-algorithm %s %d bits\n", get_pkalgo (rc), nbits);
91 rc = gnutls_openpgp_extract_key_creation_time (&dat);
92 printf ("creation time %s\n", get_pktime (rc));
94 rc = gnutls_openpgp_extract_key_expiration_time (&dat);
95 printf ("expiration time %lu\n", rc);
97 printf ("key fingerprint: ");
98 rc = gnutls_openpgp_fingerprint (&dat, fpr, &fprlen);
99 assert (rc == 0);
100 for (i = 0; i < fprlen / 2; i++)
101 printf ("%02X%02X ", fpr[2 * i], fpr[2 * i + 1]);
102 printf ("\n");
104 printf ("key id: ");
105 rc = gnutls_openpgp_extract_key_id (&dat, keyid);
106 assert (rc == 0);
107 for (i = 0; i < 8; i++)
108 printf ("%02X", keyid[i]);
109 printf ("\n\n");
111 printf ("Check MPIs\n");
112 cert = ctx->cert_list[0];
113 printf ("number of certs %d\n", *ctx->cert_list_length);
114 assert (*ctx->cert_list_length == 1);
115 printf ("number of items %d\n", cert->params_size);
116 for (i = 0; i < cert->params_size; i++)
118 nbits = gcry_mpi_get_nbits (cert->params[i]);
119 printf ("mpi %d %d bits\n", i, nbits);
122 printf ("\nCheck key\n");
123 rc = gnutls_openpgp_verify_key (NULL, &ctx->keyring, &dat, 1);
124 printf ("certifiacte status...%d\n", rc);
126 printf ("\nSeckey\n");
127 pkey = ctx->pkey;
128 assert (pkey);
129 assert (pkey->params_size);
130 nbits = gcry_mpi_get_nbits (pkey->params[0]);
131 rc = pkey->pk_algorithm;
132 printf ("pk-algorithm %s %d bits\n", get_pkalgo (rc), nbits);
133 printf ("number of items %d\n", pkey->params_size);
134 for (i = 0; i < pkey->params_size; i++)
136 nbits = gcry_mpi_get_nbits (pkey->params[i]);
137 printf ("mpi %d %d bits\n", i, nbits);
140 printf ("\nGet public key\n");
141 rc = get_pubkey (&pk, &ctx->keyring, 0xA7D93C3F);
142 assert (rc == 0);
144 printf ("key fingerprint: ");
145 gnutls_openpgp_fingerprint (&pk, fpr, &fprlen);
146 for (i = 0; i < fprlen / 2; i++)
147 printf ("%02X%02X ", fpr[2 * i], fpr[2 * i + 1]);
148 printf ("\n");
149 _gnutls_free_datum (&pk);
151 #if 0
152 rc = gnutls_openpgp_key_to_xml (&dat, &xml, 1);
153 printf ("rc=%d\n", rc);
154 assert (rc == 0);
155 xml.data[xml.size] = '\0';
156 printf ("%s\n", xml.data);
157 _gnutls_free_datum (&xml);
158 #endif
160 _gnutls_free_datum (&dat);
161 gnutls_certificate_free_credentials (ctx);
163 return 0;