2 * Copyright (C) 2007, 2008 Free Software Foundation
4 * Author: Simon Josefsson, Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 /* Functions for printing X.509 Certificate structures
28 #include <gnutls_int.h>
29 #include <gnutls/openpgp.h>
30 #include <gnutls_errors.h>
32 /* I18n of error codes. */
34 #define _(String) dgettext (PACKAGE, String)
35 #define N_(String) gettext_noop (String)
37 #define addf _gnutls_string_append_printf
38 #define adds _gnutls_string_append_str
41 hexdump (gnutls_string
* str
, const char *data
, size_t len
, const char *spc
)
47 for (j
= 0; j
< len
; j
++)
49 if (((j
+ 1) % 16) == 0)
51 addf (str
, "%.2x\n", (unsigned char) data
[j
]);
52 if (spc
&& j
!= (len
- 1))
55 else if (j
== (len
- 1))
56 addf (str
, "%.2x", (unsigned char) data
[j
]);
58 addf (str
, "%.2x:", (unsigned char) data
[j
]);
65 hexprint (gnutls_string
* str
, const char *data
, size_t len
)
73 for (j
= 0; j
< len
; j
++)
74 addf (str
, "%.2x", (unsigned char) data
[j
]);
79 print_key_usage (gnutls_string
* str
, gnutls_openpgp_crt_t cert
, unsigned int idx
)
81 unsigned int key_usage
;
84 addf (str
, _("\t\tKey Usage:\n"));
88 err
= gnutls_openpgp_crt_get_key_usage (cert
, &key_usage
);
90 err
= gnutls_openpgp_crt_get_subkey_usage (cert
, idx
, &key_usage
);
93 addf (str
, _("error: get_key_usage: %s\n"), gnutls_strerror (err
));
97 if (key_usage
& GNUTLS_KEY_DIGITAL_SIGNATURE
)
98 addf (str
, _("\t\t\tDigital signatures.\n"));
99 if (key_usage
& GNUTLS_KEY_KEY_ENCIPHERMENT
)
100 addf (str
, _("\t\t\tCommunications encipherment.\n"));
101 if (key_usage
& GNUTLS_KEY_DATA_ENCIPHERMENT
)
102 addf (str
, _("\t\t\tStorage data encipherment.\n"));
103 if (key_usage
& GNUTLS_KEY_KEY_AGREEMENT
)
104 addf (str
, _("\t\t\tAuthentication.\n"));
105 if (key_usage
& GNUTLS_KEY_KEY_CERT_SIGN
)
106 addf (str
, _("\t\t\tCertificate signing.\n"));
109 /* idx == -1 indicates main key
110 * otherwise the subkey.
113 print_key_id (gnutls_string
* str
, gnutls_openpgp_crt_t cert
, int idx
)
115 gnutls_openpgp_keyid_t id
;
119 err
= gnutls_openpgp_crt_get_key_id (cert
, id
);
121 err
= gnutls_openpgp_crt_get_subkey_id( cert
, idx
, id
);
124 addf (str
, "error: get_key_id: %s\n", gnutls_strerror (err
));
127 addf (str
, _("\tID (hex): "));
128 hexprint (str
, id
, sizeof(id
));
133 /* idx == -1 indicates main key
134 * otherwise the subkey.
137 print_key_fingerprint (gnutls_string
* str
, gnutls_openpgp_crt_t cert
)
140 size_t fpr_size
= sizeof (fpr
);
143 err
= gnutls_openpgp_crt_get_fingerprint (cert
, fpr
, &fpr_size
);
145 addf (str
, "error: get_fingerprint: %s\n", gnutls_strerror (err
));
148 addf (str
, _("\tFingerprint (hex): "));
149 hexprint (str
, fpr
, fpr_size
);
155 print_key_revoked (gnutls_string
* str
, gnutls_openpgp_crt_t cert
, int idx
)
160 err
= gnutls_openpgp_crt_get_revoked_status (cert
);
162 err
= gnutls_openpgp_crt_get_subkey_revoked_status( cert
, idx
);
165 addf (str
, _("\tRevoked: True\n"));
167 addf (str
, _("\tRevoked: False\n"));
171 print_key_times(gnutls_string
* str
, gnutls_openpgp_crt_t cert
, int idx
)
175 addf (str
, _("\tTime stamps:\n"));
178 tim
= gnutls_openpgp_crt_get_creation_time (cert
);
180 tim
= gnutls_openpgp_crt_get_subkey_creation_time (cert
, idx
);
184 size_t max
= sizeof (s
);
187 if (gmtime_r (&tim
, &t
) == NULL
)
188 addf (str
, "error: gmtime_r (%d)\n", t
);
189 else if (strftime (s
, max
, "%a %b %e %H:%M:%S UTC %Y", &t
) == 0)
190 addf (str
, "error: strftime (%d)\n", t
);
192 addf (str
, _("\t\tCreation: %s\n"), s
);
196 tim
= gnutls_openpgp_crt_get_expiration_time (cert
);
198 tim
= gnutls_openpgp_crt_get_subkey_expiration_time (cert
, idx
);
201 size_t max
= sizeof (s
);
206 addf (str
, _("\t\tExpiration: Never\n"), s
);
210 if (gmtime_r (&tim
, &t
) == NULL
)
211 addf (str
, "error: gmtime_r (%d)\n", t
);
212 else if (strftime (s
, max
, "%a %b %e %H:%M:%S UTC %Y", &t
) == 0)
213 addf (str
, "error: strftime (%d)\n", t
);
215 addf (str
, _("\t\tExpiration: %s\n"), s
);
221 print_key_info(gnutls_string
* str
, gnutls_openpgp_crt_t cert
, int idx
)
227 err
= gnutls_openpgp_crt_get_pk_algorithm (cert
, &bits
);
229 err
= gnutls_openpgp_crt_get_subkey_pk_algorithm (cert
, idx
, &bits
);
232 addf (str
, "error: get_pk_algorithm: %s\n", gnutls_strerror (err
));
235 const char *name
= gnutls_pk_algorithm_get_name (err
);
239 addf (str
, _("\tPublic Key Algorithm: %s\n"), name
);
247 err
= gnutls_openpgp_crt_get_pk_rsa_raw (cert
, &m
, &e
);
249 err
= gnutls_openpgp_crt_get_subkey_pk_rsa_raw (cert
, idx
, &m
, &e
);
252 addf (str
, "error: get_pk_rsa_raw: %s\n",
253 gnutls_strerror (err
));
256 addf (str
, _("\t\tModulus (bits %d):\n"), bits
);
257 hexdump (str
, m
.data
, m
.size
, "\t\t\t");
258 addf (str
, _("\t\tExponent:\n"));
259 hexdump (str
, e
.data
, e
.size
, "\t\t\t");
261 gnutls_free (m
.data
);
262 gnutls_free (e
.data
);
270 gnutls_datum_t p
, q
, g
, y
;
273 err
= gnutls_openpgp_crt_get_pk_dsa_raw (cert
, &p
, &q
, &g
, &y
);
275 err
= gnutls_openpgp_crt_get_subkey_pk_dsa_raw (cert
, idx
, &p
, &q
, &g
, &y
);
277 addf (str
, "error: get_pk_dsa_raw: %s\n",
278 gnutls_strerror (err
));
281 addf (str
, _("\t\tPublic key (bits %d):\n"), bits
);
282 hexdump (str
, y
.data
, y
.size
, "\t\t\t");
283 addf (str
, _("\t\tP:\n"));
284 hexdump (str
, p
.data
, p
.size
, "\t\t\t");
285 addf (str
, _("\t\tQ:\n"));
286 hexdump (str
, q
.data
, q
.size
, "\t\t\t");
287 addf (str
, _("\t\tG:\n"));
288 hexdump (str
, g
.data
, g
.size
, "\t\t\t");
290 gnutls_free (p
.data
);
291 gnutls_free (q
.data
);
292 gnutls_free (g
.data
);
293 gnutls_free (y
.data
);
306 print_cert (gnutls_string
* str
, gnutls_openpgp_crt_t cert
, unsigned int format
)
313 print_key_revoked( str
, cert
, -1);
317 int version
= gnutls_openpgp_crt_get_version (cert
);
319 addf (str
, "error: get_version: %s\n", gnutls_strerror (version
));
321 addf (str
, _("\tVersion: %d\n"), version
);
325 print_key_id( str
, cert
, -1);
327 print_key_fingerprint( str
, cert
);
332 dn_size
= sizeof(dn
);
333 err
= gnutls_openpgp_crt_get_name (cert
, i
++, dn
, &dn_size
);
335 if (err
< 0 && err
!= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
&&
336 err
!= GNUTLS_E_OPENPGP_UID_REVOKED
)
338 addf (str
, "error: get_name: %s %d\n", gnutls_strerror (err
), err
);
343 addf (str
, _("\tName[%d]: %s\n"), i
-1, dn
);
344 else if (err
== GNUTLS_E_OPENPGP_UID_REVOKED
) {
345 addf (str
, _("\tRevoked Name[%d]: %s\n"), i
-1, dn
);
350 print_key_times( str
, cert
, -1);
352 print_key_info( str
, cert
, -1);
353 print_key_usage( str
, cert
, -1);
355 subkeys
= gnutls_openpgp_crt_get_subkey_count( cert
);
359 for (i
=0;i
<subkeys
;i
++) {
360 addf( str
, _("\n\tSubkey[%d]:\n"), i
);
362 print_key_revoked( str
, cert
, i
);
363 print_key_id( str
, cert
, i
);
364 print_key_times( str
, cert
, i
);
365 print_key_info( str
, cert
, i
);
366 print_key_usage( str
, cert
, i
);
372 * gnutls_openpgp_crt_print - Pretty print OpenPGP certificates
373 * @cert: The structure to be printed
374 * @format: Indicate the format to use
375 * @out: Newly allocated datum with zero terminated string.
377 * This function will pretty print an OpenPGP certificate, suitable
378 * for display to a human.
380 * The format should be zero for future compatibility.
382 * The output @out needs to be deallocate using gnutls_free().
384 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
387 gnutls_openpgp_crt_print (gnutls_openpgp_crt_t cert
,
388 gnutls_certificate_print_formats_t format
,
393 _gnutls_string_init (&str
, gnutls_malloc
, gnutls_realloc
, gnutls_free
);
395 _gnutls_string_append_str (&str
, _("OpenPGP Certificate Information:\n"));
397 print_cert (&str
, cert
, format
);
399 _gnutls_string_append_data (&str
, "\0", 1);
400 out
->data
= str
.data
;
401 out
->size
= strlen (str
.data
);