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