Skip to content

Commit 235f9e1

Browse files
committed
Implement copilot suggestions
1 parent 5864031 commit 235f9e1

3 files changed

Lines changed: 64 additions & 16 deletions

File tree

examples/ocsp_responder/ocsp_responder.c

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22-
/* This is a test program and should not be used as an example. */
22+
/* Example OCSP responder used for interoperability and stapling testing.
23+
* This code is for demonstration/testing only and is not hardened for
24+
* secure or production use. Do not use this as a reference implementation
25+
* for deploying an OCSP responder in production.
26+
*/
2327

2428
#ifdef HAVE_CONFIG_H
2529
#include <config.h>
@@ -515,15 +519,43 @@ static int ParseHttpRequest(const byte* httpReq, int httpReqSz,
515519
}
516520
if (contentLen) {
517521
*bodySz = atoi(contentLen + 15);
522+
/* Reject obviously invalid or unreasonably large Content-Length */
523+
if (*bodySz < 0 || *bodySz > MAX_REQUEST_SIZE) {
524+
LOG_ERROR("Invalid or too large Content-Length: %d\n", *bodySz);
525+
*body = NULL;
526+
*bodySz = 0;
527+
return -1;
528+
}
518529
}
519530

520531
/* Find body (after \r\n\r\n) */
521532
*body = (const byte*)XSTRSTR((char*)httpReq, "\r\n\r\n");
522533
if (*body) {
534+
int offset;
535+
523536
*body += 4;
537+
offset = (int)(*body - httpReq);
538+
539+
/* Validate that the body pointer is within the received buffer */
540+
if (offset < 0 || offset > httpReqSz) {
541+
LOG_ERROR("Invalid HTTP body offset\n");
542+
*body = NULL;
543+
*bodySz = 0;
544+
return -1;
545+
}
546+
524547
/* Use Content-Length if available, otherwise use remaining data */
525548
if (*bodySz == 0) {
526-
*bodySz = httpReqSz - (int)(*body - httpReq);
549+
*bodySz = httpReqSz - offset;
550+
}
551+
552+
/* Ensure that the claimed body length fits in the received data */
553+
if (offset + *bodySz > httpReqSz) {
554+
LOG_ERROR("Incomplete HTTP body: expected %d bytes, have %d\n",
555+
*bodySz, httpReqSz - offset);
556+
*body = NULL;
557+
*bodySz = 0;
558+
return -1;
527559
}
528560
return 0;
529561
}
@@ -554,17 +586,31 @@ static int SendHttpResponse(SOCKET_T clientfd, const byte* ocspResp, int ocspRes
554586
"\r\n", ocspRespSz);
555587

556588
/* Send header */
557-
sent = (int)send(clientfd, header, (size_t)headerLen, 0);
558-
if (sent != headerLen) {
559-
LOG_ERROR("Failed to send HTTP header\n");
560-
return -1;
589+
{
590+
int totalSent = 0;
591+
while (totalSent < headerLen) {
592+
sent = (int)send(clientfd, header + totalSent,
593+
(size_t)(headerLen - totalSent), 0);
594+
if (sent <= 0) {
595+
LOG_ERROR("Failed to send HTTP header\n");
596+
return -1;
597+
}
598+
totalSent += sent;
599+
}
561600
}
562601

563602
/* Send body */
564-
sent = (int)send(clientfd, (const char*)ocspResp, (size_t)ocspRespSz, 0);
565-
if (sent != ocspRespSz) {
566-
LOG_ERROR("Failed to send OCSP response\n");
567-
return -1;
603+
{
604+
int totalSent = 0;
605+
while (totalSent < ocspRespSz) {
606+
sent = (int)send(clientfd, (const char*)ocspResp + totalSent,
607+
(size_t)(ocspRespSz - totalSent), 0);
608+
if (sent <= 0) {
609+
LOG_ERROR("Failed to send OCSP response\n");
610+
return -1;
611+
}
612+
totalSent += sent;
613+
}
568614
}
569615

570616
return 0;
@@ -815,9 +861,9 @@ THREAD_RETURN WOLFSSL_THREAD ocsp_responder_test(void* args)
815861
/* Write ready file if requested */
816862
if (opts.readyFile != NULL) {
817863
XFILE rf = XFOPEN(opts.readyFile, "w");
818-
if (rf != NULL) {
864+
if (rf != XBADFILE) {
819865
fprintf(rf, "%d\n", (int)opts.port);
820-
fclose(rf);
866+
XFCLOSE(rf);
821867
if (opts.verbose) {
822868
LOG_MSG("Ready file created: %s\n", opts.readyFile);
823869
}

src/ocsp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ int wc_OcspResponder_AddSigner(OcspResponder* responder,
22602260
DecodedCert* decoded = NULL;
22612261
word32 keyOID = 0;
22622262

2263-
WOLFSSL_ENTER("wc_OcspResponder_AddResponder");
2263+
WOLFSSL_ENTER("wc_OcspResponder_AddSigner");
22642264

22652265
if (responder == NULL || signerDer == NULL || signerDerSz == 0 ||
22662266
keyDer == NULL || keyDerSz == 0)
@@ -2326,7 +2326,7 @@ int wc_OcspResponder_AddSigner(OcspResponder* responder,
23262326
if (ret != 0)
23272327
goto out;
23282328

2329-
if (XMEMCMP(issuer, decoded->subject, WC_ASN_NAME_MAX) != 0) {
2329+
if (XSTRNCMP(issuer, decoded->subject, WC_ASN_NAME_MAX) != 0) {
23302330
/* Issuer name in responder cert does not match subject of issuer cert */
23312331
ret = BAD_FUNC_ARG;
23322332
goto out;
@@ -2827,6 +2827,8 @@ int wc_OcspResponder_WriteErrorResponse(enum Ocsp_Response_Status status,
28272827
return ret;
28282828
}
28292829

2830+
#endif /* HAVE_OCSP_RESPONDER */
2831+
28302832
/* Helper functions for testing */
28312833
int wc_InitOcspRequest(OcspRequest* req, DecodedCert* cert,
28322834
byte useNonce, void* heap)
@@ -2840,8 +2842,6 @@ int wc_EncodeOcspRequest(OcspRequest* req, byte* output,
28402842
return EncodeOcspRequest(req, output, size);
28412843
}
28422844

2843-
#endif /* HAVE_OCSP_RESPONDER */
2844-
28452845
#else /* HAVE_OCSP */
28462846

28472847

wolfcrypt/src/asn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40939,6 +40939,8 @@ int OcspResponseEncode(OcspResponse* resp, byte* out, word32* outSz,
4093940939
ret = SizeASN_Items(ocspResponseASN, dataASN,
4094040940
ocspResponseASN_Length, &sz);
4094140941
}
40942+
if (ret == 0 && sz > (int)*outSz)
40943+
ret = BUFFER_E;
4094240944
if (ret == 0) {
4094340945
if (SetASN_Items(ocspResponseASN, dataASN,
4094440946
ocspResponseASN_Length, out) != sz)

0 commit comments

Comments
 (0)