Skip to content

Commit 345b90e

Browse files
committed
Patches to make callback non-blocking (from Juliusz).
1 parent b6e89d6 commit 345b90e

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

ocsp/ocsp_nonblock/ocsp_nonblock.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,31 @@ static const char* kGoogleCom = "google.pem"; /* www.google.com */
4242

4343
static int io_timeout_sec = DEFAULT_TIMEOUT_SEC;
4444

45+
46+
static SOCKET_T sfd = SOCKET_INVALID;
47+
static word16 port;
48+
static int ret = -1;
49+
static char path[MAX_URL_ITEM_SIZE];
50+
static char domainName[MAX_URL_ITEM_SIZE];
51+
static int nonBlockCnt = 0;
52+
static byte* httpBuf;
53+
4554
/* Return size of the OCSP response or negative for error */
4655
static int OcspLookupNonBlockCb(void* ctx, const char* url, int urlSz,
4756
byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf)
4857
{
49-
SOCKET_T sfd = SOCKET_INVALID;
50-
word16 port;
51-
int ret = -1;
52-
char path[MAX_URL_ITEM_SIZE];
53-
char domainName[MAX_URL_ITEM_SIZE];
54-
int nonBlockCnt = 0;
58+
if (sfd != SOCKET_INVALID) {
59+
ret = wolfIO_HttpProcessResponseOcsp(sfd, ocspRespBuf,
60+
httpBuf, HTTP_SCRATCH_BUFFER_SIZE, ctx);
61+
nonBlockCnt++;
62+
if (ret == OCSP_WANT_READ)
63+
return WOLFSSL_CBIO_ERR_WANT_READ;
64+
printf("OCSP Response: ret %d, nonblock count %d\n",
65+
ret, nonBlockCnt);
66+
XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP);
67+
httpBuf = NULL;
68+
return ret;
69+
}
5570

5671
if (ocspReqBuf == NULL || ocspReqSz == 0) {
5772
printf("OCSP request is required for lookup\n");
@@ -65,7 +80,7 @@ static int OcspLookupNonBlockCb(void* ctx, const char* url, int urlSz,
6580
else {
6681
/* Note: This is free'd in OcspRespFreeCb callback */
6782
int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE;
68-
byte* httpBuf = (byte*)XMALLOC(httpBufSz, NULL, DYNAMIC_TYPE_OCSP);
83+
httpBuf = (byte*)XMALLOC(httpBufSz, NULL, DYNAMIC_TYPE_OCSP);
6984

7085
printf("OCSP Lookup:\n");
7186
printf("\tURL: %s\n", url);
@@ -99,6 +114,8 @@ static int OcspLookupNonBlockCb(void* ctx, const char* url, int urlSz,
99114
ret = wolfIO_HttpProcessResponseOcsp(sfd, ocspRespBuf,
100115
httpBuf, HTTP_SCRATCH_BUFFER_SIZE, NULL);
101116
nonBlockCnt++;
117+
if (ret == OCSP_WANT_READ)
118+
return WOLFSSL_CBIO_ERR_WANT_READ;
102119
} while (ret == OCSP_WANT_READ);
103120
printf("OCSP Response: ret %d, nonblock count %d\n",
104121
ret, nonBlockCnt);
@@ -110,15 +127,18 @@ static int OcspLookupNonBlockCb(void* ctx, const char* url, int urlSz,
110127
if (sfd != SOCKET_INVALID)
111128
CloseSocket(sfd);
112129
XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP);
130+
httpBuf = NULL;
113131
}
114132
}
133+
printf("Resp ret: %d\n", ret);
115134
return ret;
116135
}
117136

118137
static void OcspRespFreeCb(void* ctx, byte *resp)
119138
{
120139
if (resp)
121140
XFREE(resp, NULL, DYNAMIC_TYPE_OCSP);
141+
httpBuf = NULL;
122142

123143
(void)ctx;
124144
}
@@ -130,7 +150,7 @@ int main(int argc, char** argv)
130150
char pem[2048];
131151
int pemSz = 0;
132152
byte der[2000];
133-
int derSz;
153+
int derSz = 0;
134154
FILE* file;
135155
const char* certFile = kGoogleCom;
136156

@@ -191,8 +211,10 @@ int main(int argc, char** argv)
191211
#ifdef HAVE_OCSP
192212
if (ret == WOLFSSL_SUCCESS) {
193213
/* Check OCSP for certificate */
194-
ret = wolfSSL_CertManagerCheckOCSP(pCm,
195-
der, derSz);
214+
do {
215+
ret = wolfSSL_CertManagerCheckOCSP(pCm,
216+
der, derSz);
217+
} while (ret == OCSP_WANT_READ);
196218
printf("Check OCSP for Google.com (ret %d)\n", ret);
197219
}
198220
#endif

0 commit comments

Comments
 (0)