summaryrefslogtreecommitdiff
path: root/tests/openpgp_test.c
blob: fa87b232705bee0351d163c2e3c8893450a99b2f (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/** t-openpgp.c -- OpenPGP regression test **/

#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "gnutls_mpi.h"
#include "gnutls_cert.h"
#include "gnutls_datum.h"
#include "gnutls_global.h"
#include "auth_cert.h"
#include "gnutls_openpgp.h"

#include <gnutls_str.h>
#include <stdio.h>
#include <gcrypt.h>
#include <time.h>
#include <assert.h>

static const char *
get_pkalgo (int algo)
{
  switch (algo)
    {
    case GNUTLS_PK_DSA:
      return "DSA";
    case GNUTLS_PK_RSA:
      return "RSA";
    }
  return NULL;
}

static const char *
get_pktime (long timestamp)
{
  static char buf[128];
  struct tm *tb;

  tb = localtime (&timestamp);
  sprintf (buf, "%04d-%02d-%02d", tb->tm_year + 1900, tb->tm_mon + 1,
	   tb->tm_mday);
  return buf;
}

int
get_pubkey (gnutls_datum * pk, const gnutls_datum * kr, unsigned long kid)
{
  unsigned char buf[4];

  buf[0] = kid >> 24;
  buf[1] = kid >> 16;
  buf[2] = kid >> 8;
  buf[3] = kid;
  return gnutls_openpgp_get_key (pk, kr, KEY_ATTR_SHORT_KEYID, buf);
}


int
main (int argc, char **argv)
{
  gnutls_certificate_credentials ctx;
  gnutls_datum dat, xml, pk;
  gnutls_openpgp_name uid;
  gnutls_privkey *pkey;
  gnutls_cert *cert;
  unsigned char fpr[20], keyid[8];
  char *s, *t;
  size_t fprlen = 0;
  int rc, nbits = 0, i;

  rc = gnutls_certificate_allocate_credentials (&ctx);
  assert (rc == 0);

  s = "../src/openpgp/cli_ring.gpg";
  rc = gnutls_certificate_set_openpgp_keyring_file (ctx, s);
  assert (rc == 0);

  s = "../src/openpgp/pub.asc";
  t = "../src/openpgp/sec.asc";
  rc = gnutls_certificate_set_openpgp_key_file (ctx, s, t);
  assert (rc == 0);

  dat = ctx->cert_list[0]->raw;
  assert (ctx->cert_list[0]);
  printf ("Key v%d\n", gnutls_openpgp_extract_key_version (&dat));
  rc = gnutls_openpgp_extract_key_name (&dat, 1, &uid);
  assert (rc == 0);
  printf ("userID    %s\n", uid.name);

  rc = gnutls_openpgp_extract_key_pk_algorithm (&dat, &nbits);
  printf ("pk-algorithm %s %d bits\n", get_pkalgo (rc), nbits);

  rc = gnutls_openpgp_extract_key_creation_time (&dat);
  printf ("creation time %s\n", get_pktime (rc));

  rc = gnutls_openpgp_extract_key_expiration_time (&dat);
  printf ("expiration time %lu\n", rc);

  printf ("key fingerprint: ");
  rc = gnutls_openpgp_fingerprint (&dat, fpr, &fprlen);
  assert (rc == 0);
  for (i = 0; i < fprlen / 2; i++)
    printf ("%02X%02X ", fpr[2 * i], fpr[2 * i + 1]);
  printf ("\n");

  printf ("key id: ");
  rc = gnutls_openpgp_extract_key_id (&dat, keyid);
  assert (rc == 0);
  for (i = 0; i < 8; i++)
    printf ("%02X", keyid[i]);
  printf ("\n\n");

  printf ("Check MPIs\n");
  cert = ctx->cert_list[0];
  printf ("number of certs %d\n", *ctx->cert_list_length);
  assert (*ctx->cert_list_length == 1);
  printf ("number of items %d\n", cert->params_size);
  for (i = 0; i < cert->params_size; i++)
    {
      nbits = gcry_mpi_get_nbits (cert->params[i]);
      printf ("mpi %d %d bits\n", i, nbits);
    }

  printf ("\nCheck key\n");
  rc = gnutls_openpgp_verify_key (NULL, &ctx->keyring, &dat, 1);
  printf ("certifiacte status...%d\n", rc);

  printf ("\nSeckey\n");
  pkey = ctx->pkey;
  assert (pkey);
  assert (pkey->params_size);
  nbits = gcry_mpi_get_nbits (pkey->params[0]);
  rc = pkey->pk_algorithm;
  printf ("pk-algorithm %s %d bits\n", get_pkalgo (rc), nbits);
  printf ("number of items %d\n", pkey->params_size);
  for (i = 0; i < pkey->params_size; i++)
    {
      nbits = gcry_mpi_get_nbits (pkey->params[i]);
      printf ("mpi %d %d bits\n", i, nbits);
    }

  printf ("\nGet public key\n");
  rc = get_pubkey (&pk, &ctx->keyring, 0xA7D93C3F);
  assert (rc == 0);

  printf ("key fingerprint: ");
  gnutls_openpgp_fingerprint (&pk, fpr, &fprlen);
  for (i = 0; i < fprlen / 2; i++)
    printf ("%02X%02X ", fpr[2 * i], fpr[2 * i + 1]);
  printf ("\n");
  _gnutls_free_datum (&pk);

#if 0
  rc = gnutls_openpgp_key_to_xml (&dat, &xml, 1);
  printf ("rc=%d\n", rc);
  assert (rc == 0);
  xml.data[xml.size] = '\0';
  printf ("%s\n", xml.data);
  _gnutls_free_datum (&xml);
#endif

  _gnutls_free_datum (&dat);
  gnutls_certificate_free_credentials (ctx);

  return 0;
}