Skip to content

Commit 17fd5df

Browse files
committed
Refactor: OCSP object allocation
Refactors OCSP object allocation and freeing: - Introduces `wc_NewOCSP` to allocate and initialize `WOLFSSL_OCSP` objects with error handling. - Introduces `wc_FreeOCSP` to free `WOLFSSL_OCSP` objects. - Simplifies OCSP usage in tests by using the new allocation/freeing functions. - Remove `wc_InitOCSP` since `WOLFSSL_OCSP` is an opaque struct
1 parent 550e5a2 commit 17fd5df

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

src/ocsp.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,26 @@
4444
#include <wolfcrypt/src/misc.c>
4545
#endif
4646

47-
48-
int wc_InitOCSP(WOLFSSL_OCSP* ocsp, WOLFSSL_CERT_MANAGER* cm)
47+
/* Allocates and initializes a WOLFSSL_OCSP object. Returns pointer on success, NULL on failure. */
48+
WOLFSSL_OCSP* wc_NewOCSP(WOLFSSL_CERT_MANAGER* cm)
4949
{
50-
return InitOCSP(ocsp, cm);
50+
WOLFSSL_OCSP* ocsp = NULL;
51+
ocsp = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP), cm ? cm->heap : NULL, DYNAMIC_TYPE_OCSP);
52+
if (ocsp == NULL)
53+
return NULL;
54+
if (InitOCSP(ocsp, cm) != 0) {
55+
XFREE(ocsp, cm ? cm->heap : NULL, DYNAMIC_TYPE_OCSP);
56+
return NULL;
57+
}
58+
return ocsp;
5159
}
5260

61+
/* Frees a WOLFSSL_OCSP object allocated by wc_NewOCSP. */
5362
void wc_FreeOCSP(WOLFSSL_OCSP* ocsp)
5463
{
55-
FreeOCSP(ocsp, 0);
64+
if (ocsp) {
65+
FreeOCSP(ocsp, 1);
66+
}
5667
}
5768

5869
int wc_CheckCertOcspResponse(WOLFSSL_OCSP *ocsp, DecodedCert *cert,

tests/api/test_ocsp.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,7 @@ static int test_ocsp_tls_cert_cb_verify_cb(int preverify,
765765
WOLFSSL_CERT_MANAGER* cm = NULL;
766766
DecodedCert cert;
767767
byte certInit = 0;
768-
WOLFSSL_OCSP ocsp;
769-
byte ocspInit = 0;
768+
WOLFSSL_OCSP* ocsp = NULL;
770769

771770
ret = 1;
772771
cm = wolfSSL_CertManagerNew();
@@ -798,19 +797,16 @@ static int test_ocsp_tls_cert_cb_verify_cb(int preverify,
798797
if (ret == 1 && (ocspStaple == NULL || ocspStaple->buffer == NULL ||
799798
ocspStaple->length == 0))
800799
ret = 0;
801-
if (ret == 1 && wc_InitOCSP(&ocsp, cm) != 0)
800+
if (ret == 1 && (ocsp = wc_NewOCSP(cm)) == NULL)
802801
ret = 0;
803-
if (ret == 1)
804-
ocspInit = 1;
805802
if (ret == 1 &&
806-
wc_CheckCertOcspResponse(&ocsp, &cert, ocspStaple->buffer,
807-
ocspStaple->length, NULL) != 0)
803+
wc_CheckCertOcspResponse(ocsp, &cert, ocspStaple->buffer,
804+
ocspStaple->length, NULL) != 0)
808805
ret = 0;
809806
}
810807
#endif
811808

812-
if (ocspInit)
813-
wc_FreeOCSP(&ocsp);
809+
wc_FreeOCSP(ocsp);
814810
if (certInit)
815811
wc_FreeDecodedCert(&cert);
816812
wolfSSL_CertManagerFree(cm);
@@ -829,8 +825,7 @@ static int test_ocsp_tls_cert_cb_ocsp_verify_cb(WOLFSSL* ssl, int err,
829825
WOLFSSL_CERT_MANAGER* cm = NULL;
830826
DecodedCert cert;
831827
byte certInit = 0;
832-
WOLFSSL_OCSP ocsp;
833-
byte ocspInit = 0;
828+
WOLFSSL_OCSP* ocsp = NULL;
834829
WOLFSSL_X509_CHAIN* peerCerts;
835830

836831
cm = wolfSSL_CertManagerNew();
@@ -855,16 +850,14 @@ static int test_ocsp_tls_cert_cb_ocsp_verify_cb(WOLFSSL* ssl, int err,
855850
certInit = 1;
856851
if (wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm) != 0)
857852
goto cleanup;
858-
if (wc_InitOCSP(&ocsp, cm) != 0)
853+
if ((ocsp = wc_NewOCSP(cm)) == NULL)
859854
goto cleanup;
860-
ocspInit = 1;
861-
if (wc_CheckCertOcspResponse(&ocsp, &cert, staple, stapleSz, NULL) != 0)
855+
if (wc_CheckCertOcspResponse(ocsp, &cert, staple, stapleSz, NULL) != 0)
862856
goto cleanup;
863857

864858
err = 0;
865859
cleanup:
866-
if (ocspInit)
867-
wc_FreeOCSP(&ocsp);
860+
wc_FreeOCSP(ocsp);
868861
if (certInit)
869862
wc_FreeDecodedCert(&cert);
870863
wolfSSL_CertManagerFree(cm);

wolfssl/ocsp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ WOLFSSL_LOCAL int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int resp
7777
WOLFSSL_LOCAL int CheckOcspResponder(OcspResponse *bs, DecodedCert *cert,
7878
void* vp);
7979

80-
WOLFSSL_API int wc_InitOCSP(WOLFSSL_OCSP* ocsp, WOLFSSL_CERT_MANAGER* cm);
80+
/* Allocates and initializes a WOLFSSL_OCSP object */
81+
WOLFSSL_API WOLFSSL_OCSP* wc_NewOCSP(WOLFSSL_CERT_MANAGER* cm);
82+
/* Frees a WOLFSSL_OCSP object allocated by wc_NewOCSP */
8183
WOLFSSL_API void wc_FreeOCSP(WOLFSSL_OCSP* ocsp);
8284
WOLFSSL_API int wc_CheckCertOcspResponse(WOLFSSL_OCSP *ocsp, DecodedCert *cert,
8385
byte *response, int responseSz, void* heap);

0 commit comments

Comments
 (0)