1 /* This example code is placed in the public domain. */
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
18 #include <gnutls/pkcs11.h>
19 #include <sys/types.h>
23 /* A TLS client that loads the certificate and key.
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
);
44 pin_callback (void *user
, int attempt
, const char *token_url
,
45 const char *token_label
, unsigned int flags
, char *pin
,
51 printf ("PIN required for token '%s' with URL '%s'\n", token_label
,
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");
67 len
= MIN (pin_max
, strlen (password
));
68 memcpy (pin
, password
, len
);
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
);
92 gnutls_certificate_allocate_credentials (&xcred
);
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
118 gnutls_transport_set_ptr (session
, (gnutls_transport_ptr_t
) sd
);
120 /* Perform the TLS handshake
122 ret
= gnutls_handshake (session
);
126 fprintf (stderr
, "*** Handshake failed\n");
132 printf ("- Handshake was completed\n");
135 gnutls_record_send (session
, MSG
, strlen (MSG
));
137 ret
= gnutls_record_recv (session
, buffer
, MAX_BUF
);
140 printf ("- Peer has closed the TLS connection\n");
145 fprintf (stderr
, "*** Error: %s\n", gnutls_strerror (ret
));
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
);
162 gnutls_deinit (session
);
164 gnutls_certificate_free_credentials (xcred
);
165 gnutls_priority_deinit (priorities_cache
);
167 gnutls_global_deinit ();