Skip to content

Commit 2f67b03

Browse files
Merge pull request wolfSSL#460 from anhu/client-tls-pkcs11
Add in client examples that use PKCS11.
2 parents f935fd0 + 0f7a32e commit 2f67b03

File tree

3 files changed

+261
-16
lines changed

3 files changed

+261
-16
lines changed

pkcs11/client-tls-pkcs11.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+
#ifndef WOLFSSL_USER_SETTINGS
35+
#include <wolfssl/options.h>
36+
#endif
37+
#include <wolfssl/ssl.h>
38+
#include <wolfssl/wolfcrypt/wc_pkcs11.h>
39+
40+
#define DEFAULT_PORT 11111
41+
42+
int client_tls(const char *cacert, int devId, Pkcs11Token* token)
43+
{
44+
int sockfd;
45+
struct sockaddr_in servAddr;
46+
char buff[256];
47+
size_t len;
48+
int ret;
49+
50+
/* declare wolfSSL objects */
51+
WOLFSSL_CTX* ctx = NULL;
52+
WOLFSSL* ssl = NULL;
53+
WOLFSSL_CIPHER* cipher;
54+
55+
/* Initialize wolfSSL */
56+
wolfSSL_Init();
57+
58+
/* Create a socket that uses an internet IPv4 address,
59+
* Sets the socket to be stream based (TCP),
60+
* 0 means choose the default protocol. */
61+
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
62+
fprintf(stderr, "ERROR: failed to create the socket\n");
63+
return -1;
64+
}
65+
66+
/* Initialize the server address struct with zeros */
67+
memset(&servAddr, 0, sizeof(servAddr));
68+
69+
/* Fill in the server address */
70+
servAddr.sin_family = AF_INET; /* using IPv4 */
71+
servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */
72+
73+
/* IPv4 127.0.0.1 */
74+
if (inet_pton(AF_INET, "127.0.0.1", &servAddr.sin_addr) != 1) {
75+
fprintf(stderr, "ERROR: invalid address\n");
76+
ret = -1;
77+
goto exit;
78+
}
79+
80+
/* Connect to the server */
81+
if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr)))
82+
== -1) {
83+
fprintf(stderr, "ERROR: failed to connect\n");
84+
goto exit;
85+
}
86+
87+
#if 0
88+
wolfSSL_Debugging_ON();
89+
#endif
90+
91+
/* Initialize wolfSSL */
92+
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
93+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
94+
return -1;
95+
}
96+
97+
/* Set devId associated with the PKCS11 device. */
98+
if (wolfSSL_CTX_SetDevId(ctx, devId) != WOLFSSL_SUCCESS) {
99+
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
100+
return -1;
101+
}
102+
103+
/* Load CA certificate into WOLFSSL_CTX for validating peer */
104+
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, cacert, NULL))
105+
!= WOLFSSL_SUCCESS) {
106+
fprintf(stderr, "ERROR: failed to load %s, please check the file.\n",
107+
cacert);
108+
goto exit;
109+
}
110+
111+
/* validate peer certificate */
112+
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
113+
114+
/* Open the PKCS11 token. */
115+
if ((ret = wc_Pkcs11Token_Open(token, 1)) != 0) {
116+
fprintf(stderr, "ERROR: failed to open session on token (%d)\n", ret);
117+
return -1;
118+
}
119+
120+
/* Create a WOLFSSL object */
121+
if ((ssl = wolfSSL_new(ctx)) == NULL) {
122+
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
123+
return -1;
124+
}
125+
126+
/* Attach wolfSSL to the socket */
127+
if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) {
128+
fprintf(stderr, "ERROR: Failed to set the file descriptor\n");
129+
goto exit;
130+
}
131+
132+
/* Connect to wolfSSL on the server side */
133+
if ((ret = wolfSSL_connect(ssl)) != WOLFSSL_SUCCESS) {
134+
fprintf(stderr, "ERROR: failed to connect to wolfSSL\n");
135+
goto exit;
136+
}
137+
138+
cipher = wolfSSL_get_current_cipher(ssl);
139+
printf("SSL cipher suite is %s\n", wolfSSL_CIPHER_get_name(cipher));
140+
141+
/* Get a message for the server from stdin */
142+
printf("Message for server: ");
143+
memset(buff, 0, sizeof(buff));
144+
if (fgets(buff, sizeof(buff), stdin) == NULL) {
145+
fprintf(stderr, "ERROR: failed to get message for server\n");
146+
ret = -1;
147+
goto exit;
148+
}
149+
len = strnlen(buff, sizeof(buff));
150+
151+
/* Send the message to the server */
152+
if ((ret = wolfSSL_write(ssl, buff, len)) != len) {
153+
fprintf(stderr, "ERROR: failed to write entire message\n");
154+
fprintf(stderr, "%d bytes of %d bytes were sent", ret, (int) len);
155+
goto exit;
156+
}
157+
158+
/* Read the server data into our buff array */
159+
memset(buff, 0, sizeof(buff));
160+
if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) {
161+
fprintf(stderr, "ERROR: failed to read\n");
162+
goto exit;
163+
}
164+
165+
/* Print to stdout any data the server sends */
166+
printf("Server: %s\n", buff);
167+
168+
ret = 0; /* return success */
169+
170+
exit:
171+
/* Cleanup after this connection */
172+
wolfSSL_free(ssl);
173+
wc_Pkcs11Token_Close(token);
174+
175+
wolfSSL_CTX_free(ctx);
176+
if (sockfd != SOCKET_INVALID)
177+
close(sockfd);
178+
return ret;
179+
}
180+
181+
int main(int argc, char* argv[])
182+
{
183+
int ret;
184+
const char* library;
185+
const char* slot;
186+
const char* tokenName;
187+
const char* userPin;
188+
const char* cacert;
189+
Pkcs11Dev dev;
190+
Pkcs11Token token;
191+
int slotId;
192+
int devId = 1;
193+
194+
if (argc != 5 && argc != 6) {
195+
fprintf(stderr,
196+
"Usage: client_tls_pkcs11 <cacert> <libname> <slot> <tokenname> [userpin]\n");
197+
return 1;
198+
}
199+
200+
cacert = argv[1];
201+
library = argv[2];
202+
slot = argv[3];
203+
tokenName = argv[4];
204+
userPin = (argc == 5) ? NULL : argv[5];
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(cacert, 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+

pkcs11/pkcs11_hmac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
int hmac(int devId, Pkcs11Token* token)
3232
{
3333
Hmac hmac;
34-
unsigned char key[SHA256_DIGEST_SIZE];
34+
unsigned char key[WC_SHA256_DIGEST_SIZE];
3535
int ret = 0;
3636
unsigned char data[57];
37-
unsigned char mac[SHA256_DIGEST_SIZE];
37+
unsigned char mac[WC_SHA256_DIGEST_SIZE];
3838

3939
memset(key, 9, sizeof(key));
4040
memset(data, 9, sizeof(data));

pkcs11/pkcs11_test.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,36 +1083,36 @@ int hmac_test(int devId, Pkcs11Token* token)
10831083
unsigned char key[WC_MAX_DIGEST_SIZE];
10841084
unsigned char data[57];
10851085
#if !defined(NO_MD5)
1086-
unsigned char exp_md5[MD5_DIGEST_SIZE] = {
1086+
unsigned char exp_md5[WC_MD5_DIGEST_SIZE] = {
10871087
0x58, 0x8e, 0xd2, 0x4e, 0x04, 0x1f, 0xf4, 0xc6,
10881088
0x98, 0x7c, 0x8e, 0xdc, 0xe5, 0xb1, 0xbc, 0x4b
10891089
};
10901090
#endif
10911091
#if !defined(NO_SHA)
1092-
unsigned char exp_sha[SHA_DIGEST_SIZE] = {
1092+
unsigned char exp_sha[WC_SHA_DIGEST_SIZE] = {
10931093
0x2f, 0x69, 0xc1, 0xf9, 0xe1, 0x97, 0x04, 0xe4,
10941094
0x75, 0x9f, 0x1c, 0x2a, 0x85, 0x87, 0x7e, 0x6b,
10951095
0xa7, 0x9f, 0xe1, 0x13
10961096
};
10971097
#endif
10981098
#if defined(WOLFSSL_SHA224)
1099-
unsigned char exp_sha224[SHA224_DIGEST_SIZE] = {
1099+
unsigned char exp_sha224[WC_SHA224_DIGEST_SIZE] = {
11001100
0x86, 0xa8, 0xfc, 0xfd, 0xd5, 0x95, 0xf2, 0xa6,
11011101
0x45, 0x89, 0x3b, 0x8b, 0x4c, 0x0d, 0xd1, 0x81,
11021102
0x20, 0x6b, 0x71, 0x2d, 0x7c, 0x88, 0x31, 0xa8,
11031103
0x17, 0x9f, 0xc7, 0x66
11041104
};
11051105
#endif
11061106
#if !defined(NO_SHA256)
1107-
unsigned char exp_sha256[SHA256_DIGEST_SIZE] = {
1107+
unsigned char exp_sha256[WC_SHA256_DIGEST_SIZE] = {
11081108
0x04, 0x9e, 0x43, 0x3c, 0x48, 0x7c, 0x31, 0x11,
11091109
0x54, 0x90, 0x43, 0xf6, 0x2f, 0x97, 0x42, 0x80,
11101110
0x3d, 0x22, 0x47, 0x1d, 0x4f, 0xc9, 0xb8, 0xa0,
11111111
0x02, 0x13, 0x2f, 0x8a, 0x31, 0xc2, 0x6e, 0xbe
11121112
};
11131113
#endif
11141114
#if defined(WOLFSSL_SHA384)
1115-
unsigned char exp_sha384[SHA384_DIGEST_SIZE] = {
1115+
unsigned char exp_sha384[WC_SHA384_DIGEST_SIZE] = {
11161116
0x0b, 0x4c, 0x32, 0x58, 0x7b, 0x00, 0xb7, 0xfa,
11171117
0x82, 0x9f, 0xf0, 0x1d, 0x10, 0x85, 0xbc, 0x2e,
11181118
0xe0, 0x4a, 0x71, 0x91, 0xd6, 0x9a, 0x93, 0xc2,
@@ -1122,7 +1122,7 @@ int hmac_test(int devId, Pkcs11Token* token)
11221122
};
11231123
#endif
11241124
#if defined(WOLFSSL_SHA512)
1125-
unsigned char exp_sha512[SHA512_DIGEST_SIZE] = {
1125+
unsigned char exp_sha512[WC_SHA512_DIGEST_SIZE] = {
11261126
0x94, 0x7b, 0x97, 0x0f, 0x48, 0x68, 0xd1, 0x88,
11271127
0x08, 0x09, 0xcf, 0xea, 0xae, 0x3e, 0xba, 0xa2,
11281128
0x3f, 0xf4, 0x9d, 0x73, 0x78, 0x15, 0x34, 0x44,
@@ -1140,42 +1140,42 @@ int hmac_test(int devId, Pkcs11Token* token)
11401140
#ifndef NO_MD5
11411141
if (ret == 0) {
11421142
fprintf(stderr, "HMAC-MD5\n");
1143-
ret = hmac_op(key, MD5_DIGEST_SIZE, WC_MD5, data, sizeof(data), exp_md5,
1144-
sizeof(exp_md5), devId, token);
1143+
ret = hmac_op(key, WC_MD5_DIGEST_SIZE, WC_MD5, data, sizeof(data),
1144+
exp_md5, sizeof(exp_md5), devId, token);
11451145
}
11461146
#endif
11471147
#ifndef NO_SHA
11481148
if (ret == 0) {
11491149
fprintf(stderr, "HMAC-SHA\n");
1150-
ret = hmac_op(key, SHA_DIGEST_SIZE, WC_SHA, data, sizeof(data), exp_sha,
1151-
sizeof(exp_sha), devId, token);
1150+
ret = hmac_op(key, WC_SHA_DIGEST_SIZE, WC_SHA, data, sizeof(data),
1151+
exp_sha, sizeof(exp_sha), devId, token);
11521152
}
11531153
#endif
11541154
#ifdef WOLFSSL_SHA224
11551155
if (ret == 0) {
11561156
fprintf(stderr, "HMAC-SHA224\n");
1157-
ret = hmac_op(key, SHA224_DIGEST_SIZE, WC_SHA224, data, sizeof(data),
1157+
ret = hmac_op(key, WC_SHA224_DIGEST_SIZE, WC_SHA224, data, sizeof(data),
11581158
exp_sha224, sizeof(exp_sha224), devId, token);
11591159
}
11601160
#endif
11611161
#ifndef NO_SHA256
11621162
if (ret == 0) {
11631163
fprintf(stderr, "HMAC-SHA256\n");
1164-
ret = hmac_op(key, SHA256_DIGEST_SIZE, WC_SHA256, data, sizeof(data),
1164+
ret = hmac_op(key, WC_SHA256_DIGEST_SIZE, WC_SHA256, data, sizeof(data),
11651165
exp_sha256, sizeof(exp_sha256), devId, token);
11661166
}
11671167
#endif
11681168
#ifdef WOLFSSL_SHA384
11691169
if (ret == 0) {
11701170
fprintf(stderr, "HMAC-SHA384\n");
1171-
ret = hmac_op(key, SHA384_DIGEST_SIZE, WC_SHA384, data, sizeof(data),
1171+
ret = hmac_op(key, WC_SHA384_DIGEST_SIZE, WC_SHA384, data, sizeof(data),
11721172
exp_sha384, sizeof(exp_sha384), devId, token);
11731173
}
11741174
#endif
11751175
#ifdef WOLFSSL_SHA512
11761176
if (ret == 0) {
11771177
fprintf(stderr, "HMAC-SHA512\n");
1178-
ret = hmac_op(key, SHA512_DIGEST_SIZE, WC_SHA512, data, sizeof(data),
1178+
ret = hmac_op(key, WC_SHA512_DIGEST_SIZE, WC_SHA512, data, sizeof(data),
11791179
exp_sha512, sizeof(exp_sha512), devId, token);
11801180
}
11811181
#endif

0 commit comments

Comments
 (0)