guile: Fix `priorities' test to use `run-test'.
[gnutls.git] / doc / examples / ex-cert-select-pkcs11.c
blob492cd5a8bffde0e10fa486de7d5c141a2c135526
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <getpass.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <unistd.h>
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
18 #include <gnutls/pkcs11.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
23 /* A TLS client that loads the certificate and key.
26 #define MAX_BUF 1024
27 #define MSG "GET / HTTP/1.0\r\n\r\n"
28 #define MIN(x,y) (((x)<(y))?(x):(y))
30 #define CAFILE "ca.pem"
32 /* The URLs of the objects can be obtained
33 * using p11tool --list-all --login
35 #define KEY_URL "pkcs11:manufacturer=SomeManufacturer;object=Private%20Key" \
36 ";objecttype=private;id=%db%5b%3e%b5%72%33"
37 #define CERT_URL "pkcs11:manufacturer=SomeManufacturer;object=Certificate;" \
38 "objecttype=cert;id=db%5b%3e%b5%72%33"
40 extern int tcp_connect (void);
41 extern void tcp_close (int sd);
43 static int
44 pin_callback (void *user, int attempt, const char *token_url,
45 const char *token_label, unsigned int flags, char *pin,
46 size_t pin_max)
48 const char *password;
49 int len;
51 printf ("PIN required for token '%s' with URL '%s'\n", token_label,
52 token_url);
53 if (flags & GNUTLS_PKCS11_PIN_FINAL_TRY)
54 printf ("*** This is the final try before locking!\n");
55 if (flags & GNUTLS_PKCS11_PIN_COUNT_LOW)
56 printf ("*** Only few tries left before locking!\n");
57 if (flags & GNUTLS_PKCS11_PIN_WRONG)
58 printf ("*** Wrong PIN\n");
60 password = getpass ("Enter pin: ");
61 if (password == NULL || password[0] == 0)
63 fprintf (stderr, "No password given\n");
64 exit (1);
67 len = MIN (pin_max, strlen (password));
68 memcpy (pin, password, len);
69 pin[len] = 0;
71 return 0;
74 int
75 main (void)
77 int ret, sd, ii;
78 gnutls_session_t session;
79 gnutls_priority_t priorities_cache;
80 char buffer[MAX_BUF + 1];
81 gnutls_certificate_credentials_t xcred;
82 /* Allow connections to servers that have OpenPGP keys as well.
85 gnutls_global_init ();
86 /* PKCS11 private key operations might require PIN.
87 * Register a callback.
89 gnutls_pkcs11_set_pin_function (pin_callback, NULL);
91 /* X509 stuff */
92 gnutls_certificate_allocate_credentials (&xcred);
94 /* priorities */
95 gnutls_priority_init (&priorities_cache, "NORMAL", NULL);
97 /* sets the trusted cas file
99 gnutls_certificate_set_x509_trust_file (xcred, CAFILE, GNUTLS_X509_FMT_PEM);
101 gnutls_certificate_set_x509_key_file (xcred, CERT_URL, KEY_URL, GNUTLS_X509_FMT_DER);
103 /* Initialize TLS session
105 gnutls_init (&session, GNUTLS_CLIENT);
107 /* Use default priorities */
108 gnutls_priority_set (session, priorities_cache);
110 /* put the x509 credentials to the current session
112 gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
114 /* connect to the peer
116 sd = tcp_connect ();
118 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
120 /* Perform the TLS handshake
122 ret = gnutls_handshake (session);
124 if (ret < 0)
126 fprintf (stderr, "*** Handshake failed\n");
127 gnutls_perror (ret);
128 goto end;
130 else
132 printf ("- Handshake was completed\n");
135 gnutls_record_send (session, MSG, strlen (MSG));
137 ret = gnutls_record_recv (session, buffer, MAX_BUF);
138 if (ret == 0)
140 printf ("- Peer has closed the TLS connection\n");
141 goto end;
143 else if (ret < 0)
145 fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
146 goto end;
149 printf ("- Received %d bytes: ", ret);
150 for (ii = 0; ii < ret; ii++)
152 fputc (buffer[ii], stdout);
154 fputs ("\n", stdout);
156 gnutls_bye (session, GNUTLS_SHUT_RDWR);
158 end:
160 tcp_close (sd);
162 gnutls_deinit (session);
164 gnutls_certificate_free_credentials (xcred);
165 gnutls_priority_deinit (priorities_cache);
167 gnutls_global_deinit ();
169 return 0;