@@ -16098,6 +16098,7 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1609816098 int ret = 0;
1609916099 byte status_type;
1610016100 word32 status_length;
16101+ int endCertificateOK = 0;
1610116102
1610216103 WOLFSSL_START(WC_FUNC_CERTIFICATE_STATUS_DO);
1610316104 WOLFSSL_ENTER("DoCertificateStatus");
@@ -16121,6 +16122,7 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1612116122 /* WOLFSSL_CSR_OCSP overlaps with WOLFSSL_CSR2_OCSP */
1612216123 case WOLFSSL_CSR2_OCSP:
1612316124 ret = ProcessCSR(ssl, input, inOutIdx, status_length);
16125+ endCertificateOK = (ret == 0);
1612416126 break;
1612516127
1612616128 #endif
@@ -16197,14 +16199,19 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1619716199
1619816200 while (ret == 0) {
1619916201 request = (OcspRequest*)TLSX_CSR2_GetRequest(
16200- ssl->extensions, status_type, idx++ );
16202+ ssl->extensions, status_type, idx);
1620116203
16202- if (request == NULL)
16204+ if (request == NULL) {
1620316205 ret = BAD_CERTIFICATE_STATUS_ERROR;
16204- else if (CompareOcspReqResp(request, response) == 0)
16205- break;
16206- else if (idx == 1) /* server cert must be OK */
16206+ }
16207+ else if (CompareOcspReqResp(request, response) != 0) {
1620716208 ret = BAD_CERTIFICATE_STATUS_ERROR;
16209+ }
16210+ else {
16211+ if (idx == 0) /* server cert must be OK */
16212+ endCertificateOK = 1;
16213+ break;
16214+ }
1620816215 }
1620916216
1621016217 /* only frees 'single' if single->isDynamic is set */
@@ -16213,6 +16220,7 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1621316220 *inOutIdx += status_length;
1621416221 list_length -= status_length;
1621516222 }
16223+ idx++;
1621616224 }
1621716225
1621816226 ssl->status_request_v2 = 0;
@@ -16232,6 +16240,9 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
1623216240 ret = BUFFER_ERROR;
1623316241 }
1623416242
16243+ /* end certificate MUST be present */
16244+ if (endCertificateOK == 0)
16245+ ret = BAD_CERTIFICATE_STATUS_ERROR;
1623516246#if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
1623616247 if (ret == 0) {
1623716248 if (TLSX_CSR2_MergePendingCA(ssl) < 0) {
@@ -16646,44 +16657,6 @@ static int SanityCheckMsgReceived(WOLFSSL* ssl, byte type)
1664616657 WOLFSSL_ERROR_VERBOSE(OUT_OF_ORDER_E);
1664716658 return OUT_OF_ORDER_E;
1664816659 }
16649- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
16650- defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
16651- if (ssl->msgsReceived.got_certificate_status == 0) {
16652- int csrRet = 0;
16653- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
16654- if (csrRet == 0 && ssl->status_request) {
16655- WOLFSSL_MSG("No CertificateStatus before ServerKeyExchange");
16656- csrRet = TLSX_CSR_ForceRequest(ssl);
16657- }
16658- #endif
16659- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
16660- if (csrRet == 0 && ssl->status_request_v2) {
16661- WOLFSSL_MSG("No CertificateStatus before ServerKeyExchange");
16662- csrRet = TLSX_CSR2_ForceRequest(ssl);
16663- }
16664- #endif
16665- if (csrRet != 0) {
16666- /* Error out if OCSP lookups are enabled and failed or if
16667- * the user requires stapling. */
16668- if (SSL_CM(ssl)->ocspEnabled || SSL_CM(ssl)->ocspMustStaple)
16669- return csrRet;
16670- }
16671- /* Check that a status request extension was seen as the
16672- * CertificateStatus wasn't when an OCSP staple is required.
16673- */
16674- if (
16675- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
16676- !ssl->status_request &&
16677- #endif
16678- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
16679- !ssl->status_request_v2 &&
16680- #endif
16681- SSL_CM(ssl)->ocspMustStaple) {
16682- WOLFSSL_ERROR_VERBOSE(OCSP_CERT_UNKNOWN);
16683- return OCSP_CERT_UNKNOWN;
16684- }
16685- }
16686- #endif
1668716660
1668816661 break;
1668916662#endif
@@ -16756,6 +16729,54 @@ static int SanityCheckMsgReceived(WOLFSSL* ssl, byte type)
1675616729 return OUT_OF_ORDER_E;
1675716730 }
1675816731 }
16732+ #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
16733+ defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
16734+ if (ssl->msgsReceived.got_certificate_status == 0) {
16735+ int csrRet = 0;
16736+ #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
16737+ if (csrRet == 0 && ssl->status_request) {
16738+ WOLFSSL_MSG("No CertificateStatus before ServerHelloDone");
16739+ csrRet = TLSX_CSR_ForceRequest(ssl);
16740+ }
16741+ #endif
16742+ #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
16743+ if (csrRet == 0 && ssl->status_request_v2) {
16744+ WOLFSSL_MSG("No CertificateStatus before ServerHelloDone");
16745+ csrRet = TLSX_CSR2_ForceRequest(ssl);
16746+ }
16747+ if (ssl->status_request_v2) {
16748+ if (csrRet == 0) {
16749+ if (TLSX_CSR2_MergePendingCA(ssl) < 0) {
16750+ WOLFSSL_MSG("Failed to merge pending CAs");
16751+ }
16752+ }
16753+ else {
16754+ TLSX_CSR2_ClearPendingCA(ssl);
16755+ }
16756+ }
16757+ #endif
16758+ if (csrRet != 0) {
16759+ /* Error out if OCSP lookups are enabled and failed or if
16760+ * the user requires stapling. */
16761+ if (SSL_CM(ssl)->ocspEnabled || SSL_CM(ssl)->ocspMustStaple)
16762+ return csrRet;
16763+ }
16764+ /* Check that a status request extension was seen as the
16765+ * CertificateStatus wasn't when an OCSP staple is required.
16766+ */
16767+ if (
16768+ #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
16769+ !ssl->status_request &&
16770+ #endif
16771+ #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
16772+ !ssl->status_request_v2 &&
16773+ #endif
16774+ SSL_CM(ssl)->ocspMustStaple) {
16775+ WOLFSSL_ERROR_VERBOSE(OCSP_CERT_UNKNOWN);
16776+ return OCSP_CERT_UNKNOWN;
16777+ }
16778+ }
16779+ #endif
1675916780 break;
1676016781#endif
1676116782
0 commit comments