Tizen 2.0 Release
[external/libgnutls26.git] / doc / examples / ex-session-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 /* This function will print some details of the
15  * given session.
16  */
17 int
18 print_info (gnutls_session_t session)
19 {
20   const char *tmp;
21   gnutls_credentials_type_t cred;
22   gnutls_kx_algorithm_t kx;
23
24   /* print the key exchange's algorithm name
25    */
26   kx = gnutls_kx_get (session);
27   tmp = gnutls_kx_get_name (kx);
28   printf ("- Key Exchange: %s\n", tmp);
29
30   /* Check the authentication type used and switch
31    * to the appropriate.
32    */
33   cred = gnutls_auth_get_type (session);
34   switch (cred)
35     {
36     case GNUTLS_CRD_IA:
37       printf ("- TLS/IA session\n");
38       break;
39
40
41 #ifdef ENABLE_SRP
42     case GNUTLS_CRD_SRP:
43       printf ("- SRP session with username %s\n",
44               gnutls_srp_server_get_username (session));
45       break;
46 #endif
47
48     case GNUTLS_CRD_PSK:
49       /* This returns NULL in server side.
50        */
51       if (gnutls_psk_client_get_hint (session) != NULL)
52         printf ("- PSK authentication. PSK hint '%s'\n",
53                 gnutls_psk_client_get_hint (session));
54       /* This returns NULL in client side.
55        */
56       if (gnutls_psk_server_get_username (session) != NULL)
57         printf ("- PSK authentication. Connected as '%s'\n",
58                 gnutls_psk_server_get_username (session));
59       break;
60
61     case GNUTLS_CRD_ANON:      /* anonymous authentication */
62
63       printf ("- Anonymous DH using prime of %d bits\n",
64               gnutls_dh_get_prime_bits (session));
65       break;
66
67     case GNUTLS_CRD_CERTIFICATE:       /* certificate authentication */
68
69       /* Check if we have been using ephemeral Diffie-Hellman.
70        */
71       if (kx == GNUTLS_KX_DHE_RSA || kx == GNUTLS_KX_DHE_DSS)
72         {
73           printf ("\n- Ephemeral DH using prime of %d bits\n",
74                   gnutls_dh_get_prime_bits (session));
75         }
76
77       /* if the certificate list is available, then
78        * print some information about it.
79        */
80       print_x509_certificate_info (session);
81
82     }                           /* switch */
83
84   /* print the protocol's name (ie TLS 1.0) 
85    */
86   tmp = gnutls_protocol_get_name (gnutls_protocol_get_version (session));
87   printf ("- Protocol: %s\n", tmp);
88
89   /* print the certificate type of the peer.
90    * ie X.509
91    */
92   tmp =
93     gnutls_certificate_type_get_name (gnutls_certificate_type_get (session));
94
95   printf ("- Certificate Type: %s\n", tmp);
96
97   /* print the compression algorithm (if any)
98    */
99   tmp = gnutls_compression_get_name (gnutls_compression_get (session));
100   printf ("- Compression: %s\n", tmp);
101
102   /* print the name of the cipher used.
103    * ie 3DES.
104    */
105   tmp = gnutls_cipher_get_name (gnutls_cipher_get (session));
106   printf ("- Cipher: %s\n", tmp);
107
108   /* Print the MAC algorithms name.
109    * ie SHA1
110    */
111   tmp = gnutls_mac_get_name (gnutls_mac_get (session));
112   printf ("- MAC: %s\n", tmp);
113
114   return 0;
115 }