@@ -57,23 +57,25 @@ static int cert_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store)
5757 || err == WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
5858#endif
5959 ) {
60-
60+ int i ;
6161 WOLFSSL_BUFFER_INFO * bInfo = & store -> certs [depth ];
6262 WOLFSSL_CERT_MANAGER * cm = NULL ;
6363 DecodedCert cert ;
6464 byte certInit = 0 ;
65- WOLFSSL * ssl = (WOLFSSL * )store -> userCtx ;
6665
6766 cm = wolfSSL_CertManagerNew ();
6867 if (cm == NULL )
6968 ret = 0 ;
7069 if (ret == 1 &&
7170 wolfSSL_CertManagerLoadCA (cm , CA_CERT , NULL ) != WOLFSSL_SUCCESS )
7271 ret = 0 ;
73- /* If verifying leaf cert then we need to load the intermediate CA */
74- if (ret == 1 && depth == 0 &&
75- wolfSSL_CertManagerLoadCA (cm , INTERMEDIATE_CA_CERT , NULL ) != WOLFSSL_SUCCESS )
76- ret = 0 ;
72+
73+ /* Certs are verified top down so we can load the remainder of the chain */
74+ for (i = depth + 1 ; i < store -> totalCerts && ret == 1 ; i ++ ) {
75+ if (wolfSSL_CertManagerLoadCABuffer (cm , store -> certs [i ].buffer , store -> certs [i ].length ,
76+ WOLFSSL_FILETYPE_ASN1 ) != WOLFSSL_SUCCESS )
77+ ret = 0 ;
78+ }
7779
7880 /* Verify cert with CA */
7981 if (ret == 1 ) {
@@ -83,27 +85,6 @@ static int cert_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store)
8385 if (ret == 1 && wc_ParseCert (& cert , CERT_TYPE , VERIFY , cm ) != 0 )
8486 ret = 0 ;
8587
86- if (ret == 1 && (wolfSSL_version (ssl ) == TLS1_3_VERSION ||
87- wolfSSL_version (ssl ) == DTLS1_3_VERSION )) {
88- WOLFSSL_BUFFER_INFO * ocspStaple = wolfSSL_GetTls13OcspStatusResp (ssl , (word32 )depth );
89- WOLFSSL_OCSP * ocsp = NULL ;
90-
91- if (ocspStaple == NULL || ocspStaple -> buffer == NULL || ocspStaple -> length == 0 )
92- ret = 0 ;
93- if (ret == 1 && (ocsp = wc_NewOCSP (cm )) == NULL )
94- ret = 0 ;
95- if (ret == 1 &&
96- wc_CheckCertOcspResponse (ocsp , & cert , ocspStaple -> buffer ,
97- ocspStaple -> length , NULL ) != 0 )
98- ret = 0 ;
99- wc_FreeOCSP (ocsp );
100-
101- if (ret == 1 )
102- printf ("Client: Manual OCSP staple verification succeeded at depth %d\n" , depth );
103- else
104- printf ("Client: Manual OCSP staple verification failed at depth %d\n" , depth );
105- }
106-
10788 if (certInit )
10889 wc_FreeDecodedCert (& cert );
10990 wolfSSL_CertManagerFree (cm );
@@ -135,21 +116,26 @@ static int ocsp_verify_cb(WOLFSSL* ssl, int err, unsigned char* staple, unsigned
135116 WOLFSSL_CERT_MANAGER * cm = NULL ;
136117 DecodedCert cert ;
137118 byte certInit = 0 ;
138- WOLFSSL_OCSP * ocsp ;
119+ WOLFSSL_OCSP * ocsp = NULL ;
139120 WOLFSSL_X509_CHAIN * peerCerts ;
121+ int i ;
140122
141123 cm = wolfSSL_CertManagerNew ();
142124 if (cm == NULL )
143125 goto cleanup ;
144126 if (wolfSSL_CertManagerLoadCA (cm , CA_CERT , NULL ) != WOLFSSL_SUCCESS )
145127 goto cleanup ;
146- if (wolfSSL_CertManagerLoadCA (cm , INTERMEDIATE_CA_CERT , NULL ) != WOLFSSL_SUCCESS )
147- goto cleanup ;
148128
149129 peerCerts = wolfSSL_get_peer_chain (ssl );
150130 if (peerCerts == NULL || wolfSSL_get_chain_count (peerCerts ) <= (int )idx )
151131 goto cleanup ;
152132
133+ for (i = idx + 1 ; i < wolfSSL_get_chain_count (peerCerts ); i ++ ) {
134+ if (wolfSSL_CertManagerLoadCABuffer (cm , wolfSSL_get_chain_cert (peerCerts , i ),
135+ wolfSSL_get_chain_length (peerCerts , i ), WOLFSSL_FILETYPE_ASN1 ) != WOLFSSL_SUCCESS )
136+ goto cleanup ;
137+ }
138+
153139 wc_InitDecodedCert (& cert , wolfSSL_get_chain_cert (peerCerts , idx ), wolfSSL_get_chain_length (peerCerts , idx ), NULL );
154140 certInit = 1 ;
155141 if (wc_ParseCert (& cert , CERT_TYPE , VERIFY , cm ) != 0 )
@@ -182,6 +168,7 @@ int main(int argc, char** argv)
182168 WOLFSSL * ssl = NULL ;
183169 char buf [32 ];
184170 int use_tls13 = 0 ;
171+ int ret = 1 ;
185172
186173 if (argc != 2 ) {
187174 printf ("Usage: %s [--tls12|--tls13]\n" , argv [0 ]);
@@ -223,7 +210,7 @@ int main(int argc, char** argv)
223210 goto cleanup ;
224211 }
225212 }
226- wolfSSL_CTX_set_tls12_ocsp_status_verify_cb (ctx , ocsp_verify_cb , NULL );
213+ wolfSSL_CTX_set_ocsp_status_verify_cb (ctx , ocsp_verify_cb , NULL );
227214
228215 sockfd = socket (AF_INET , SOCK_STREAM , 0 );
229216 if (sockfd < 0 ) {
@@ -258,17 +245,21 @@ int main(int argc, char** argv)
258245 if (n > 0 ) {
259246 buf [n ] = 0 ;
260247 printf ("Client: received: %s\n" , buf );
248+ ret = 0 ;
261249 } else if (n < 0 ) {
262250 fprintf (stderr , "Client: wolfSSL_read failed: %s\n" , wolfSSL_ERR_reason_error_string (wolfSSL_get_error (ssl , 0 )));
251+ goto cleanup ;
263252 }
264253 } else {
265254 fprintf (stderr , "Client: TLS handshake failed: %s\n" , wolfSSL_ERR_reason_error_string (wolfSSL_get_error (ssl , 0 )));
255+ goto cleanup ;
266256 }
267257
258+ ret = 0 ;
268259cleanup :
269260 if (ssl ) wolfSSL_free (ssl );
270261 if (sockfd >= 0 ) close (sockfd );
271262 if (ctx ) wolfSSL_CTX_free (ctx );
272263 wolfSSL_Cleanup ();
273- return 0 ;
264+ return ret ;
274265}
0 commit comments