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"
10 #include "gnutls_openpgp.h"
12 #include <gnutls_str.h>
32 get_pktime (long timestamp
)
37 tb
= localtime (×tamp
);
38 sprintf (buf
, "%04d-%02d-%02d", tb
->tm_year
+ 1900, tb
->tm_mon
+ 1,
44 get_pubkey (gnutls_datum_t
* pk
, const gnutls_datum_t
* kr
, unsigned long kid
)
52 return gnutls_openpgp_get_key (pk
, kr
, KEY_ATTR_SHORT_KEYID
, buf
);
57 main (int argc
, char **argv
)
59 gnutls_certificate_credentials ctx
;
60 gnutls_datum_t dat
, xml
, pk
;
61 gnutls_openpgp_name uid
;
64 unsigned char fpr
[20], keyid
[8];
69 rc
= gnutls_certificate_allocate_credentials (&ctx
);
72 s
= "../doc/credentials/openpgp/cli_ring.gpg";
73 rc
= gnutls_certificate_set_openpgp_keyring_file (ctx
, s
);
76 s
= "../doc/credentials/openpgp/pub.asc";
77 t
= "../doc/credentials/openpgp/sec.asc";
78 rc
= gnutls_certificate_set_openpgp_key_file (ctx
, s
, t
);
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
);
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
);
100 for (i
= 0; i
< fprlen
/ 2; i
++)
101 printf ("%02X%02X ", fpr
[2 * i
], fpr
[2 * i
+ 1]);
105 rc
= gnutls_openpgp_extract_key_id (&dat
, keyid
);
107 for (i
= 0; i
< 8; i
++)
108 printf ("%02X", keyid
[i
]);
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");
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);
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]);
149 _gnutls_free_datum (&pk
);
152 rc
= gnutls_openpgp_key_to_xml (&dat
, &xml
, 1);
153 printf ("rc=%d\n", rc
);
155 xml
.data
[xml
.size
] = '\0';
156 printf ("%s\n", xml
.data
);
157 _gnutls_free_datum (&xml
);
160 _gnutls_free_datum (&dat
);
161 gnutls_certificate_free_credentials (ctx
);