Skip to content

Commit 653a2a9

Browse files
committed
Add in client examples that use PKCS11.
1 parent d56158e commit 653a2a9

2 files changed

Lines changed: 490 additions & 0 deletions

File tree

pkcs11/client-tls-pkcs11-ecc.c

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/* client-tls-pkcs11.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
/* the usual suspects */
23+
#include <stdlib.h>
24+
#include <stdio.h>
25+
#include <string.h>
26+
27+
/* socket includes */
28+
#include <sys/socket.h>
29+
#include <arpa/inet.h>
30+
#include <netinet/in.h>
31+
#include <unistd.h>
32+
33+
/* wolfSSL */
34+
#include <wolfssl/options.h>
35+
#include <wolfssl/ssl.h>
36+
#include <wolfssl/wolfcrypt/wc_pkcs11.h>
37+
38+
#define DEFAULT_PORT 11111
39+
40+
#define CA_FILE "../certs/ca-ecc-cert.pem"
41+
42+
int client_tls(int devId, Pkcs11Token* token)
43+
{
44+
int sockfd;
45+
struct sockaddr_in servAddr;
46+
socklen_t size = sizeof(servAddr);
47+
char buff[256];
48+
size_t len;
49+
int shutdown = 0;
50+
int ret;
51+
52+
/* declare wolfSSL objects */
53+
WOLFSSL_CTX* ctx = NULL;
54+
WOLFSSL* ssl = NULL;
55+
WOLFSSL_CIPHER* cipher;
56+
57+
/* Initialize wolfSSL */
58+
wolfSSL_Init();
59+
60+
/* Create a socket that uses an internet IPv4 address,
61+
* Sets the socket to be stream based (TCP),
62+
* 0 means choose the default protocol. */
63+
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
64+
fprintf(stderr, "ERROR: failed to create the socket\n");
65+
return -1;
66+
}
67+
68+
/* Initialize the server address struct with zeros */
69+
memset(&servAddr, 0, sizeof(servAddr));
70+
71+
/* Fill in the server address */
72+
servAddr.sin_family = AF_INET; /* using IPv4 */
73+
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
74+
75+
/* IPv4 127.0.0.1 */
76+
if (inet_pton(AF_INET, "127.0.0.1", &servAddr.sin_addr) != 1) {
77+
fprintf(stderr, "ERROR: invalid address\n");
78+
ret = -1;
79+
goto exit;
80+
}
81+
82+
/* Connect to the server */
83+
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
84+
== -1) {
85+
fprintf(stderr, "ERROR: failed to connect\n");
86+
goto exit;
87+
}
88+
89+
#if 0
90+
wolfSSL_Debugging_ON();
91+
#endif
92+
93+
/* Initialize wolfSSL */
94+
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
95+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
96+
return -1;
97+
}
98+
99+
/* Set devId associated with the PKCS11 device. */
100+
if (wolfSSL_CTX_SetDevId(ctx, devId) != WOLFSSL_SUCCESS) {
101+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
102+
return -1;
103+
}
104+
105+
/* Load CA certificate into WOLFSSL_CTX for validating peer */
106+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
107+
!= WOLFSSL_SUCCESS) {
108+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
109+
CA_FILE);
110+
goto exit;
111+
}
112+
113+
/* validate peer certificate */
114+
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
115+
116+
/* Open the PKCS11 token. */
117+
if ((ret = wc_Pkcs11Token_Open(token, 1)) != 0) {
118+
fprintf(stderr, "ERROR: failed to open session on token (%d)\n", ret);
119+
return -1;
120+
}
121+
122+
/* Create a WOLFSSL object */
123+
if ((ssl = wolfSSL_new(ctx)) == NULL) {
124+
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
125+
return -1;
126+
}
127+
128+
/* Attach wolfSSL to the socket */
129+
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
130+
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
131+
goto exit;
132+
}
133+
134+
/* Connect to wolfSSL on the server side */
135+
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
136+
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
137+
goto exit;
138+
}
139+
140+
cipher = wolfSSL_get_current_cipher(ssl);
141+
printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
142+
143+
/* Get a message for the server from stdin */
144+
printf("Message for server: ");
145+
memset(buff, 0, sizeof(buff));
146+
if (fgets(buff, sizeof(buff), stdin) == NULL) {
147+
fprintf(stderr, "ERROR: failed to get message for server\n");
148+
ret = -1;
149+
goto exit;
150+
}
151+
len = strnlen(buff, sizeof(buff));
152+
153+
/* Send the message to the server */
154+
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
155+
fprintf(stderr, "ERROR: failed to write entire message\n");
156+
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
157+
goto exit;
158+
}
159+
160+
/* Read the server data into our buff array */
161+
memset(buff, 0, sizeof(buff));
162+
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
163+
fprintf(stderr, "ERROR: failed to read\n");
164+
goto exit;
165+
}
166+
167+
/* Print to stdout any data the server sends */
168+
printf("Server: %s\n", buff);
169+
170+
ret = 0; /* return success */
171+
172+
exit:
173+
/* Cleanup after this connection */
174+
wolfSSL_free(ssl);
175+
wc_Pkcs11Token_Close(token);
176+
177+
wolfSSL_CTX_free(ctx);
178+
if (sockfd != SOCKET_INVALID)
179+
close(sockfd);
180+
return ret;
181+
}
182+
183+
int main(int argc, char* argv[])
184+
{
185+
int ret;
186+
const char* library;
187+
const char* slot;
188+
const char* tokenName;
189+
const char* userPin;
190+
Pkcs11Dev dev;
191+
Pkcs11Token token;
192+
int slotId;
193+
int devId = 1;
194+
195+
if (argc != 4 && argc != 5) {
196+
fprintf(stderr,
197+
"Usage: server_tls_pkcs11 <libname> <slot> <tokenname> [userpin]\n");
198+
return 1;
199+
}
200+
201+
library = argv[1];
202+
slot = argv[2];
203+
tokenName = argv[3];
204+
userPin = (argc == 4) ? NULL : argv[4];
205+
slotId = atoi(slot);
206+
207+
#if defined(DEBUG_WOLFSSL)
208+
wolfSSL_Debugging_ON();
209+
#endif
210+
wolfCrypt_Init();
211+
212+
ret = wc_Pkcs11_Initialize(&dev, library, NULL);
213+
if (ret != 0) {
214+
fprintf(stderr, "Failed to initialize PKCS#11 library\n");
215+
ret = 2;
216+
}
217+
if (ret == 0) {
218+
ret = wc_Pkcs11Token_Init(&token, &dev, slotId, tokenName,
219+
(byte*)userPin, userPin == NULL ? 0 : strlen(userPin));
220+
if (ret != 0) {
221+
fprintf(stderr, "Failed to initialize PKCS#11 token\n");
222+
ret = 2;
223+
}
224+
if (ret == 0) {
225+
ret = wc_CryptoDev_RegisterDevice(devId, wc_Pkcs11_CryptoDevCb,
226+
&token);
227+
if (ret != 0) {
228+
fprintf(stderr, "Failed to register PKCS#11 token\n");
229+
ret = 2;
230+
}
231+
if (ret == 0) {
232+
ret = client_tls(devId, &token);
233+
if (ret != 0)
234+
ret = 1;
235+
}
236+
wc_Pkcs11Token_Final(&token);
237+
}
238+
wc_Pkcs11_Finalize(&dev);
239+
}
240+
241+
wolfCrypt_Cleanup();
242+
243+
return ret;
244+
}
245+

0 commit comments

Comments
 (0)