Skip to content

Commit 3c71b1e

Browse files
committed
Sync with latest wolfssl commit
- All OCSP verification is done in OCSP callback - Exit with error code on failure - Send chain (still only stapling leaf)
1 parent 90cf8cb commit 3c71b1e

3 files changed

Lines changed: 31 additions & 36 deletions

File tree

ocsp/stapling/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ debug: all
3535
responder:
3636
openssl ocsp -index responder-certs/index.txt -port 22221 \
3737
-rsigner responder-certs/ocsp-responder-cert.pem \
38-
-rkey \responder-certs/ocsp-responder-key.pem \
38+
-rkey responder-certs/ocsp-responder-key.pem \
3939
-CA client-certs/intermediate1-ca-cert.pem
4040

4141
clean:

ocsp/stapling/ocsp-client.c

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
268259
cleanup:
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
}

ocsp/stapling/ocsp-server.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int ocsp_resp_sz = 0;
4848
static int cert_cb(WOLFSSL* ssl, void* arg)
4949
{
5050
(void)arg;
51-
if (wolfSSL_use_certificate_file(ssl, SERVER_CERT, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
51+
if (wolfSSL_use_certificate_chain_file(ssl, SERVER_CERT) != WOLFSSL_SUCCESS) {
5252
fprintf(stderr, "Error loading server certificate: %s\n", wolfSSL_ERR_reason_error_string(wolfSSL_get_error(ssl, 0)));
5353
return 0;
5454
}
@@ -196,7 +196,7 @@ static int status_cb(WOLFSSL* ssl, void* ctx)
196196
return WOLFSSL_OCSP_STATUS_CB_ALERT_FATAL;
197197
}
198198

199-
// wolfSSL takes ownership of resp_buf, so do not free it here
199+
/* wolfSSL takes ownership of resp_buf, so do not free it here */
200200
return WOLFSSL_OCSP_STATUS_CB_OK;
201201
}
202202

@@ -206,6 +206,7 @@ int main()
206206
struct sockaddr_in serv_addr;
207207
WOLFSSL_CTX* ctx = NULL;
208208
WOLFSSL* ssl = NULL;
209+
int ret = 1;
209210

210211
if (fetch_ocsp_response(&ocsp_resp, &ocsp_resp_sz) != 0) {
211212
fprintf(stderr, "Failed to fetch OCSP response at startup\n");
@@ -275,16 +276,19 @@ int main()
275276
printf("Negotiated TLS version: %s\n", wolfSSL_get_version(ssl));
276277
if (wolfSSL_write(ssl, "hello", 5) != 5) {
277278
fprintf(stderr, "Server: wolfSSL_write failed\n");
279+
goto cleanup;
278280
}
279281
} else {
280282
fprintf(stderr, "Server: TLS handshake failed: %s\n", wolfSSL_ERR_reason_error_string(wolfSSL_get_error(ssl, 0)));
283+
goto cleanup;
281284
}
282285

286+
ret = 0;
283287
cleanup:
284288
if (ssl) wolfSSL_free(ssl);
285289
if (connfd >= 0) close(connfd);
286290
if (listenfd >= 0) close(listenfd);
287291
if (ctx) wolfSSL_CTX_free(ctx);
288292
wolfSSL_Cleanup();
289-
return 0;
293+
return ret;
290294
}

0 commit comments

Comments
 (0)