Skip to content

Commit c8c1baa

Browse files
author
SirTeggun
authored
Enforce safe OCSP request creation with proper error handling
- Move OCSP request generation from verify_ocsp_status into a separate static helper function create_request to improve code modularity. - Respect SSLOCSPUseRequestNonce configuration directive: the nonce is now added only if ocsp_use_request_nonce is not FALSE. - Add missing NULL check for OCSP_REQUEST_new() with appropriate error logging (APLOGNO 01920). - Ensure verify_ocsp_status appears only once (remove accidental duplication). - No functional change when nonce is enabled; configuration remains honored.
1 parent d53de3e commit c8c1baa

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

modules/ssl/ssl_engine_ocsp.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,30 @@ static OCSP_REQUEST *create_request(X509_STORE_CTX *ctx, X509 *cert,
117117
return NULL;
118118
}
119119

120-
static OCSP_REQUEST *create_request(X509_STORE_CTX *ctx, X509 *cert,
120+
static OCSP_REQUEST *create_request(X509_STORE_CTX *ctx, X509 *cert,
121121
OCSP_CERTID **certid,
122122
server_rec *s, apr_pool_t *p,
123123
SSLSrvConfigRec *sc)
124124
{
125125
OCSP_REQUEST *req = OCSP_REQUEST_new();
126+
if (!req) {
127+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01920)
128+
"failed to create OCSP request");
129+
return NULL;
130+
}
126131

127132
*certid = OCSP_cert_to_id(NULL, cert, X509_STORE_CTX_get0_current_issuer(ctx));
128133
if (!*certid || !OCSP_request_add0_id(req, *certid)) {
129134
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01921)
130135
"could not retrieve certificate id");
131136
ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, s);
137+
OCSP_REQUEST_free(req);
132138
return NULL;
133139
}
134140

135-
OCSP_request_add1_nonce(req, 0, -1);
141+
if (sc->server->ocsp_use_request_nonce != FALSE) {
142+
OCSP_request_add1_nonce(req, 0, -1);
143+
}
136144

137145
return req;
138146
}
@@ -151,8 +159,8 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
151159
ruri = determine_responder_uri(sc, cert, c, pool);
152160
if (!ruri) {
153161
if (sc->server->ocsp_mask & SSL_OCSPCHECK_NO_OCSP_FOR_CERT_OK) {
154-
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, c,
155-
"Skipping OCSP check for certificate cos no OCSP URL"
162+
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, c,
163+
"Skipping OCSP check for certificate because no OCSP URL"
156164
" found and no_ocsp_for_cert_ok is set");
157165
return V_OCSP_CERTSTATUS_GOOD;
158166
} else {
@@ -162,7 +170,7 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
162170

163171
request = create_request(ctx, cert, &certID, s, pool, sc);
164172
if (request) {
165-
apr_interval_time_t to = sc->server->ocsp_responder_timeout == UNSET ?
173+
apr_interval_time_t to = sc->server->ocsp_responder_timeout == UNSET ?
166174
apr_time_from_sec(DEFAULT_OCSP_TIMEOUT) :
167175
sc->server->ocsp_responder_timeout;
168176
response = modssl_dispatch_ocsp_request(ruri, to, request, c, pool);
@@ -174,7 +182,6 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
174182

175183
if (rc == V_OCSP_CERTSTATUS_GOOD) {
176184
int r = OCSP_response_status(response);
177-
178185
if (r != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
179186
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01922)
180187
"OCSP response not successful: %d", r);
@@ -193,18 +200,19 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
193200
}
194201

195202
if (rc == V_OCSP_CERTSTATUS_GOOD &&
196-
OCSP_check_nonce(request, basicResponse) != 1) {
203+
OCSP_check_nonce(request, basicResponse) != 1) {
197204
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01924)
198-
"Bad OCSP responder answer (bad nonce)");
205+
"Bad OCSP responder answer (bad nonce)");
199206
rc = V_OCSP_CERTSTATUS_UNKNOWN;
200207
}
201208

202209
if (rc == V_OCSP_CERTSTATUS_GOOD) {
203210
if (sc->server->ocsp_noverify != TRUE) {
204-
if (OCSP_basic_verify(basicResponse, sc->server->ocsp_certs, X509_STORE_CTX_get0_store(ctx),
211+
if (OCSP_basic_verify(basicResponse, sc->server->ocsp_certs,
212+
X509_STORE_CTX_get0_store(ctx),
205213
sc->server->ocsp_verify_flags) != 1) {
206214
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01925)
207-
"failed to verify the OCSP response");
215+
"failed to verify the OCSP response");
208216
ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, s);
209217
rc = V_OCSP_CERTSTATUS_UNKNOWN;
210218
}
@@ -228,7 +236,7 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
228236
}
229237

230238
if (rc != V_OCSP_CERTSTATUS_UNKNOWN) {
231-
long resptime_skew = sc->server->ocsp_resptime_skew == UNSET ?
239+
long resptime_skew = sc->server->ocsp_resptime_skew == UNSET ?
232240
DEFAULT_OCSP_MAX_SKEW : sc->server->ocsp_resptime_skew;
233241
int vrc = OCSP_check_validity(thisup, nextup, resptime_skew,
234242
sc->server->ocsp_resp_maxage);
@@ -260,7 +268,6 @@ static int verify_ocsp_status(X509 *cert, X509_STORE_CTX *ctx, conn_rec *c,
260268

261269
return rc;
262270
}
263-
264271
}
265272

266273
return req;

0 commit comments

Comments
 (0)