Tizen 2.0 Release
[external/libgnutls26.git] / doc / examples / ex-x509-info.c
1 /* This example code is placed in the public domain. */
2
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <gnutls/gnutls.h>
10 #include <gnutls/x509.h>
11
12 #include "examples.h"
13
14 static const char *
15 bin2hex (const void *bin, size_t bin_size)
16 {
17   static char printable[110];
18   const unsigned char *_bin = bin;
19   char *print;
20   size_t i;
21
22   if (bin_size > 50)
23     bin_size = 50;
24
25   print = printable;
26   for (i = 0; i < bin_size; i++)
27     {
28       sprintf (print, "%.2x ", _bin[i]);
29       print += 2;
30     }
31
32   return printable;
33 }
34
35 /* This function will print information about this session's peer
36  * certificate.
37  */
38 void
39 print_x509_certificate_info (gnutls_session_t session)
40 {
41   char serial[40];
42   char dn[256];
43   size_t size;
44   unsigned int algo, bits;
45   time_t expiration_time, activation_time;
46   const gnutls_datum_t *cert_list;
47   unsigned int cert_list_size = 0;
48   gnutls_x509_crt_t cert;
49   gnutls_datum_t cinfo;
50
51   /* This function only works for X.509 certificates.
52    */
53   if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509)
54     return;
55
56   cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
57
58   printf ("Peer provided %d certificates.\n", cert_list_size);
59
60   if (cert_list_size > 0)
61     {
62       int ret;
63
64       /* we only print information about the first certificate.
65        */
66       gnutls_x509_crt_init (&cert);
67
68       gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER);
69
70       printf ("Certificate info:\n");
71
72       /* This is the preferred way of printing short information about
73          a certificate. */
74
75       ret = gnutls_x509_crt_print (cert, GNUTLS_CRT_PRINT_ONELINE, &cinfo);
76       if (ret == 0)
77         {
78           printf ("\t%s\n", cinfo.data);
79           gnutls_free (cinfo.data);
80         }
81
82       /* If you want to extract fields manually for some other reason,
83          below are popular example calls. */
84
85       expiration_time = gnutls_x509_crt_get_expiration_time (cert);
86       activation_time = gnutls_x509_crt_get_activation_time (cert);
87
88       printf ("\tCertificate is valid since: %s", ctime (&activation_time));
89       printf ("\tCertificate expires: %s", ctime (&expiration_time));
90
91       /* Print the serial number of the certificate.
92        */
93       size = sizeof (serial);
94       gnutls_x509_crt_get_serial (cert, serial, &size);
95
96       printf ("\tCertificate serial number: %s\n", bin2hex (serial, size));
97
98       /* Extract some of the public key algorithm's parameters
99        */
100       algo = gnutls_x509_crt_get_pk_algorithm (cert, &bits);
101
102       printf ("Certificate public key: %s",
103               gnutls_pk_algorithm_get_name (algo));
104
105       /* Print the version of the X.509
106        * certificate.
107        */
108       printf ("\tCertificate version: #%d\n",
109               gnutls_x509_crt_get_version (cert));
110
111       size = sizeof (dn);
112       gnutls_x509_crt_get_dn (cert, dn, &size);
113       printf ("\tDN: %s\n", dn);
114
115       size = sizeof (dn);
116       gnutls_x509_crt_get_issuer_dn (cert, dn, &size);
117       printf ("\tIssuer's DN: %s\n", dn);
118
119       gnutls_x509_crt_deinit (cert);
120
121     }
122 }