guile: Fix `priorities' test to use `run-test'.
[gnutls.git] / doc / examples / ex-session-info.c
blobe5f0b545e7ca98c3c3747aaf8f8fae708d17fd74
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <gnutls/gnutls.h>
10 #include <gnutls/x509.h>
12 #include "examples.h"
14 /* This function will print some details of the
15 * given session.
17 int
18 print_info (gnutls_session_t session)
20 const char *tmp;
21 gnutls_credentials_type_t cred;
22 gnutls_kx_algorithm_t kx;
23 int dhe, ecdh;
25 dhe = ecdh = 0;
27 /* print the key exchange's algorithm name
29 kx = gnutls_kx_get (session);
30 tmp = gnutls_kx_get_name (kx);
31 printf ("- Key Exchange: %s\n", tmp);
33 /* Check the authentication type used and switch
34 * to the appropriate.
36 cred = gnutls_auth_get_type (session);
37 switch (cred)
39 case GNUTLS_CRD_IA:
40 printf ("- TLS/IA session\n");
41 break;
44 #ifdef ENABLE_SRP
45 case GNUTLS_CRD_SRP:
46 printf ("- SRP session with username %s\n",
47 gnutls_srp_server_get_username (session));
48 break;
49 #endif
51 case GNUTLS_CRD_PSK:
52 /* This returns NULL in server side.
54 if (gnutls_psk_client_get_hint (session) != NULL)
55 printf ("- PSK authentication. PSK hint '%s'\n",
56 gnutls_psk_client_get_hint (session));
57 /* This returns NULL in client side.
59 if (gnutls_psk_server_get_username (session) != NULL)
60 printf ("- PSK authentication. Connected as '%s'\n",
61 gnutls_psk_server_get_username (session));
63 if (kx == GNUTLS_KX_ECDHE_PSK)
64 ecdh = 1;
65 else if (kx == GNUTLS_KX_DHE_PSK)
66 dhe = 1;
67 break;
69 case GNUTLS_CRD_ANON: /* anonymous authentication */
71 printf ("- Anonymous authentication.\n");
72 if (kx == GNUTLS_KX_ANON_ECDH)
73 ecdh = 1;
74 else if (kx == GNUTLS_KX_ANON_DH)
75 dhe = 1;
76 break;
78 case GNUTLS_CRD_CERTIFICATE: /* certificate authentication */
80 /* Check if we have been using ephemeral Diffie-Hellman.
82 if (kx == GNUTLS_KX_DHE_RSA || kx == GNUTLS_KX_DHE_DSS)
83 dhe = 1;
84 else if (kx == GNUTLS_KX_ECDHE_RSA || kx == GNUTLS_KX_ECDHE_ECDSA)
85 ecdh = 1;
87 /* if the certificate list is available, then
88 * print some information about it.
90 print_x509_certificate_info (session);
92 } /* switch */
94 if (ecdh != 0)
95 printf ("- Ephemeral ECDH using curve %s\n",
96 gnutls_ecc_curve_get_name (gnutls_ecc_curve_get (session)));
97 else if (dhe != 0)
98 printf ("- Ephemeral DH using prime of %d bits\n",
99 gnutls_dh_get_prime_bits (session));
101 /* print the protocol's name (ie TLS 1.0)
103 tmp = gnutls_protocol_get_name (gnutls_protocol_get_version (session));
104 printf ("- Protocol: %s\n", tmp);
106 /* print the certificate type of the peer.
107 * ie X.509
109 tmp =
110 gnutls_certificate_type_get_name (gnutls_certificate_type_get (session));
112 printf ("- Certificate Type: %s\n", tmp);
114 /* print the compression algorithm (if any)
116 tmp = gnutls_compression_get_name (gnutls_compression_get (session));
117 printf ("- Compression: %s\n", tmp);
119 /* print the name of the cipher used.
120 * ie 3DES.
122 tmp = gnutls_cipher_get_name (gnutls_cipher_get (session));
123 printf ("- Cipher: %s\n", tmp);
125 /* Print the MAC algorithms name.
126 * ie SHA1
128 tmp = gnutls_mac_get_name (gnutls_mac_get (session));
129 printf ("- MAC: %s\n", tmp);
131 return 0;