Skip to content

Commit 973ee67

Browse files
committed
Polishing up ocsp_nonblock_async.c example
1 parent 530b3da commit 973ee67

2 files changed

Lines changed: 134 additions & 7 deletions

File tree

ocsp/ocsp_nonblock/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,93 @@ OCSP Response: ret 471, nonblock count 409421
3232
Check OCSP for Google.com (ret 1)
3333
Ret = 1: success
3434
```
35+
36+
## OCSP non-blocking Async example
37+
38+
This uses your system certificate chain to demonstrate validating revocation status using an OCSP
39+
public server with wolfSSL's Asynchronous crypto.
40+
41+
The example uses youtube.com as the public server.
42+
43+
Example usage:
44+
45+
```sh
46+
$ ./configure --enable-ocsp --enable-asynccrypt --enable-sni --enable-alpn --enable-ocspstapling
47+
--enable-ocspstapling2 --enable-opensslextra --enable-curve25519 CFLAGS="-DWOLFSSL_NONBLOCK_OCSP"
48+
$ make
49+
$ sudo make install
50+
51+
% make
52+
gcc -o ocsp_nonblock_async ocsp_nonblock_async.c -Wall -I/usr/local/include -Os -L/usr/local/lib -lwolfssl
53+
54+
% ./ocsp_nonblock_async
55+
WolfSSL AsyncCrypt Enabled
56+
WolfSSL AsyncCrypt with Simulation Mode
57+
Connecting...
58+
wolfSSL_connect() returned -1 (error code -108)
59+
wolfSSL_connect() returned -1 (error code -108)
60+
wolfSSL_connect() returned -1 (error code -108)
61+
ocsp_cb(): http://ocsp.pki.goog/gsr1
62+
simulate 'want read'
63+
wolfSSL_connect() returned -1 (error code -408)
64+
ocsp_cb(): http://ocsp.pki.goog/gsr1
65+
Running command:
66+
curl -s --data-binary '@ocsp.req' -o 'ocsp.resp' -X POST -H 'Cache-Control: no-cache' -H 'Content-Type: application/ocsp-request' 'http://ocsp.pki.goog/gsr1'
67+
Reading OCSP response from file...
68+
Read 1447 bytes.
69+
*response is (nil)
70+
Allocating 1447 bytes...
71+
*response is now 0x55ef7fdcb4e0
72+
Copying bytes...
73+
Bytes copied.
74+
verify_cb()
75+
preverify_ok = 1
76+
wolfSSL_connect() returned -1 (error code -108)
77+
ocsp_cb(): http://ocsp.pki.goog/gtsr1
78+
simulate 'want read'
79+
wolfSSL_connect() returned -1 (error code -408)
80+
ocsp_cb(): http://ocsp.pki.goog/gtsr1
81+
Running command:
82+
curl -s --data-binary '@ocsp.req' -o 'ocsp.resp' -X POST -H 'Cache-Control: no-cache' -H 'Content-Type: application/ocsp-request' 'http://ocsp.pki.goog/gtsr1'
83+
Reading OCSP response from file...
84+
Read 724 bytes.
85+
*response is (nil)
86+
Allocating 724 bytes...
87+
*response is now 0x55ef7fdaf030
88+
Copying bytes...
89+
Bytes copied.
90+
verify_cb()
91+
preverify_ok = 1
92+
wolfSSL_connect() returned -1 (error code -108)
93+
ocsp_cb(): http://ocsp.pki.goog/gts1c3
94+
simulate 'want read'
95+
wolfSSL_connect() returned -1 (error code -408)
96+
ocsp_cb(): http://ocsp.pki.goog/gts1c3
97+
Running command:
98+
curl -s --data-binary '@ocsp.req' -o 'ocsp.resp' -X POST -H 'Cache-Control: no-cache' -H 'Content-Type: application/ocsp-request' 'http://ocsp.pki.goog/gts1c3'
99+
Reading OCSP response from file...
100+
Read 472 bytes.
101+
*response is (nil)
102+
Allocating 472 bytes...
103+
*response is now 0x55ef7fdac4b0
104+
Copying bytes...
105+
Bytes copied.
106+
verify_cb()
107+
preverify_ok = 1
108+
wolfSSL_connect() returned -1 (error code -108)
109+
CONNECTED
110+
Closing connection...
111+
wolfSSL_shutdown() failed with code 2 (error 0)
112+
CLOSED
113+
test_connect() failed
114+
CONNECT FAILED
115+
116+
DONE
117+
```
118+
119+
The example uses `/etc/ssl/certs/ca-certificates.crt` as the system certs file by default. If your
120+
system doesn't have this file, just run the executable with the path to your own cert file.
121+
122+
```
123+
./ocsp_nonblock_asynccrypt ../../mycerts/ca.crt
124+
```

ocsp/ocsp_nonblock/ocsp_nonblock_async.c

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
#define SERVER_PORT 443
3939
#define ALPN_PROTOS "http/1.1"
4040

41-
#define SYS_CERTS_FILE "/etc/ssl/certs/ca-certificates.crt"
41+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SNI) && defined(HAVE_ALPN) \
42+
&& defined(WOLFSSL_NONBLOCK_OCSP) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
43+
&& defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
44+
45+
static const char* sys_certs_file = "/etc/ssl/certs/ca-certificates.crt";
4246

4347
#if defined(WOLFSSL_ASYNC_CRYPT)
4448
static int wait_async(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
@@ -89,7 +93,7 @@ static int ocsp_cb(void* ctx, const char* url, int urlSz, unsigned char* request
8993
if (frq != NULL) {
9094
size_t nbytes = fwrite(request, 1, requestSz, frq);
9195
if (requestSz != nbytes) {
92-
printf("Failed to write all data. Wrote only %u bytes.\n", nbytes);
96+
printf("Failed to write all data. Wrote only %zu bytes.\n", nbytes);
9397
}
9498
fclose(frq);
9599
frq = NULL;
@@ -107,12 +111,12 @@ static int ocsp_cb(void* ctx, const char* url, int urlSz, unsigned char* request
107111
printf("Reading OCSP response from file...\n");
108112
char resp[4096];
109113
size_t nbytes = fread(resp, 1, sizeof(resp), frsp);
110-
printf("Read %u bytes.\n", nbytes);
114+
printf("Read %zu bytes.\n", nbytes);
111115
fclose(frsp);
112116
frsp = NULL;
113117

114118
printf("*response is %p\n", *response);
115-
printf("Allocating %u bytes...\n", nbytes);
119+
printf("Allocating %zu bytes...\n", nbytes);
116120
*response = malloc(nbytes);
117121
if (*response == NULL) {
118122
printf("malloc() failed\n");
@@ -307,9 +311,14 @@ int test_connect(WOLFSSL_CTX* ctx)
307311
exit:
308312
return result;
309313
}
314+
#endif
310315

311-
int main(void)
316+
int main(int argc, char** argv)
312317
{
318+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SNI) && defined(HAVE_ALPN) \
319+
&& defined(WOLFSSL_NONBLOCK_OCSP) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
320+
&& defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
321+
313322
int err;
314323
int result = 0;
315324
WC_RNG rng;
@@ -318,10 +327,29 @@ int main(void)
318327
WOLFSSL_METHOD *method = NULL;
319328
WOLFSSL_CTX *ctx = NULL;
320329

330+
/* Check presence of sys_certs_file */
331+
if (access (sys_certs_file, F_OK) == -1 && argc == 1) {
332+
fprintf(stderr, "Default system cert file /etc/ssl/certs/ca-certificates.crt doesn't exist."
333+
" Please provide cert file path as show below.\n");
334+
fprintf(stderr, "./ocsp_nonblock_asynccrypt ../../mycerts/ca.crt\n");
335+
return -1;
336+
}
337+
/* Handle user provided certs file */
338+
else if (argc == 2) {
339+
if (access (argv[1], F_OK) == -1) {
340+
fprintf(stderr, "Provided cert file %s doesn't exist."
341+
" Please provide a valid path.\n", argv[1]);
342+
return -1;
343+
}
344+
else {
345+
sys_certs_file = argv[1];
346+
}
347+
}
348+
321349
wolfSSL_Debugging_ON();
322350

323351
err = wolfSSL_Init();
324-
if (err != 0) {
352+
if (err != SSL_SUCCESS) {
325353
fprintf(stderr, "wolfSSL_Init() failed with code %d\n", err);
326354
return -1;
327355
}
@@ -398,7 +426,7 @@ int main(void)
398426
goto exit;
399427
}
400428

401-
err = wolfSSL_CTX_load_verify_locations(ctx, SYS_CERTS_FILE, NULL);
429+
err = wolfSSL_CTX_load_verify_locations(ctx, sys_certs_file, NULL);
402430
if (err != SSL_SUCCESS) {
403431
fprintf(stderr, "wolfSSL_CTX_load_verify_locations() returned %d\n", err);
404432
result = -1;
@@ -443,4 +471,13 @@ int main(void)
443471
wolfSSL_Cleanup();
444472

445473
return result;
474+
#else
475+
(void)argc;
476+
(void)argv;
477+
478+
printf("Please compile wolfSSL with ./configure --enable-asynccrypt --enable-sni"
479+
" --enable-alpn --enable-ocspstapling --enable-ocspstapling2 --enable-opensslextra"
480+
" --enable-curve25519 CFLAGS=-DWOLFSSL_NONBLOCK_OCSP")
481+
return -1;
482+
#endif
446483
}

0 commit comments

Comments
 (0)