1 /* This example code is placed in the public domain. */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <gnutls/gnutls.h>
15 #include <gnutls/x509.h>
16 #include <gnutls/pkcs11.h>
17 #include <sys/types.h>
20 #include <getpass.h> /* for getpass() */
22 /* A TLS client that loads the certificate and key.
26 #define MSG "GET / HTTP/1.0\r\n\r\n"
27 #define MIN(x,y) (((x)<(y))?(x):(y))
29 #define CAFILE "/etc/ssl/certs/ca-certificates.crt"
31 /* The URLs of the objects can be obtained
32 * using p11tool --list-all --login
34 #define KEY_URL "pkcs11:manufacturer=SomeManufacturer;object=Private%20Key" \
35 ";objecttype=private;id=%db%5b%3e%b5%72%33"
36 #define CERT_URL "pkcs11:manufacturer=SomeManufacturer;object=Certificate;" \
37 "objecttype=cert;id=db%5b%3e%b5%72%33"
39 extern int tcp_connect (void);
40 extern void tcp_close (int sd
);
43 pin_callback (void *user
, int attempt
, const char *token_url
,
44 const char *token_label
, unsigned int flags
, char *pin
,
50 printf ("PIN required for token '%s' with URL '%s'\n", token_label
,
52 if (flags
& GNUTLS_PIN_FINAL_TRY
)
53 printf ("*** This is the final try before locking!\n");
54 if (flags
& GNUTLS_PIN_COUNT_LOW
)
55 printf ("*** Only few tries left before locking!\n");
56 if (flags
& GNUTLS_PIN_WRONG
)
57 printf ("*** Wrong PIN\n");
59 password
= getpass ("Enter pin: ");
60 if (password
== NULL
|| password
[0] == 0)
62 fprintf (stderr
, "No password given\n");
66 len
= MIN (pin_max
, strlen (password
));
67 memcpy (pin
, password
, len
);
77 gnutls_session_t session
;
78 gnutls_priority_t priorities_cache
;
79 char buffer
[MAX_BUF
+ 1];
80 gnutls_certificate_credentials_t xcred
;
81 /* Allow connections to servers that have OpenPGP keys as well.
84 gnutls_global_init ();
85 /* PKCS11 private key operations might require PIN.
86 * Register a callback.
88 gnutls_pkcs11_set_pin_function (pin_callback
, NULL
);
91 gnutls_certificate_allocate_credentials (&xcred
);
94 gnutls_priority_init (&priorities_cache
, "NORMAL", NULL
);
96 /* sets the trusted cas file
98 gnutls_certificate_set_x509_trust_file (xcred
, CAFILE
, GNUTLS_X509_FMT_PEM
);
100 gnutls_certificate_set_x509_key_file (xcred
, CERT_URL
, KEY_URL
, GNUTLS_X509_FMT_DER
);
102 /* Initialize TLS session
104 gnutls_init (&session
, GNUTLS_CLIENT
);
106 /* Use default priorities */
107 gnutls_priority_set (session
, priorities_cache
);
109 /* put the x509 credentials to the current session
111 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, xcred
);
113 /* connect to the peer
117 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
119 /* Perform the TLS handshake
121 ret
= gnutls_handshake (session
);
125 fprintf (stderr
, "*** Handshake failed\n");
131 printf ("- Handshake was completed\n");
134 gnutls_record_send (session
, MSG
, strlen (MSG
));
136 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
139 printf ("- Peer has closed the TLS connection\n");
144 fprintf (stderr
, "*** Error: %s\n", gnutls_strerror (ret
));
148 printf ("- Received %d bytes: ", ret
);
149 for (ii
= 0; ii
< ret
; ii
++)
151 fputc (buffer
[ii
], stdout
);
153 fputs ("\n", stdout
);
155 gnutls_bye (session
, GNUTLS_SHUT_RDWR
);
161 gnutls_deinit (session
);
163 gnutls_certificate_free_credentials (xcred
);
164 gnutls_priority_deinit (priorities_cache
);
166 gnutls_global_deinit ();