Debian Codegraph @

src/canl_ocsp.c

raw
1 #include "canl_ocsp.h"
2 #include "canl_mech_ssl.h"
3 #include <openssl/ocsp.h>
4
5 #define USENONCE 0
6
7 static X509_STORE * canl_create_x509store(canl_x509store_t *store);
8
9 static OCSP_RESPONSE *send_request(OCSP_REQUEST *req, char *host, char *path,
10 int port, int ssl, int req_timeout);
11
12 static OCSP_RESPONSE *
13 query_responder(BIO *conn, char *path, OCSP_REQUEST *req, int req_timeout);
14
15 static char *get_ocsp_url_from_aia(X509 * cert, char** urls);
16
17 int ocsprequest_init(canl_ocsprequest_t **ocspreq)
18 {
19 if (!ocspreq)
20 return 1;
21 *ocspreq = calloc(1, sizeof(**ocspreq));
22 if (!(*ocspreq))
23 return 1;
24
25 return 0;
26 }
27
28 void ocsprequest_free(canl_ocsprequest_t *ocspreq)
29 {
30 if (ocspreq)
31 free(ocspreq);
32 }
33
34 int set_ocsp_url(canl_ocsprequest_t *ocspreq, char *url)
35 {
36 int len = 0;
37 if (!ocspreq)
38 return 1;
39
40 if (url) {
41 if (!ocspreq->url) {
42 free (ocspreq->url);
43 ocspreq->url = NULL;
44 }
45 len = strlen(url);
46 ocspreq->url = (char *) malloc((len +1) * sizeof (char));
47 if (!ocspreq->url)
48 return 1;
49 strncpy(ocspreq->url, url, len + 1);
50 }
51 return 0;
52 }
53
54 int set_ocsp_sign_cert(canl_ocsprequest_t *ocspreq, X509 *sign_cert)
55 {
56 if (!ocspreq)
57 return 1;
58 if (sign_cert) {
59 if (!ocspreq->sign_cert) {
60 X509_free (ocspreq->sign_cert);
61 ocspreq->sign_cert = NULL;
62 }
63 ocspreq->sign_cert = X509_dup(sign_cert);
64 if (!ocspreq->sign_cert)
65 return 1;
66 }
67 return 0;
68 }
69
70 int set_ocsp_sign_key(canl_ocsprequest_t *ocspreq, EVP_PKEY *sign_key)
71 {
72 if (!ocspreq)
73 return 1;
74 if (sign_key) {
75 if (!ocspreq->sign_key) {
76 EVP_PKEY_free (ocspreq->sign_key);
77 ocspreq->sign_key = NULL;
78 }
79 pkey_dup(&ocspreq->sign_key, sign_key);
80 if (!ocspreq->sign_key)
81 return 1;
82 }
83 return 0;
84 }
85
86 static X509_STORE *
87 canl_create_x509store(canl_x509store_t *c_store)
88 {
89 X509_STORE *store = NULL;
90 X509_LOOKUP *lookup = NULL;
91
92 if (!c_store)
93 return NULL;
94 if(!(store = X509_STORE_new()))
95 goto end;
96 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
97 if (lookup == NULL)
98 goto end;
99 if (c_store->ca_file) {
100 if(!X509_LOOKUP_load_file(lookup, c_store->ca_file, X509_FILETYPE_PEM)) {
101 goto end;
102 }
103 }
104 else X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
105
106 lookup=X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
107 if (lookup == NULL)
108 goto end;
109 if (c_store->ca_dir) {
110 if(!X509_LOOKUP_add_dir(lookup, c_store->ca_dir, X509_FILETYPE_PEM)) {
111 goto end;
112 }
113 }
114 else X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
115
116 ERR_clear_error();
117 return store;
118 end:
119 X509_STORE_free(store);
120 return NULL;
121 }
122
123 /* Extract an url of given ocsp responder out of the AIA extension.
124
125 Newer openssl libs support OPENSSL_STRING type and X509_get1_ocsp()
126 funcion which can easily do that. We may use it in future.
127 aia = X509_get1_ocsp(x);
128 return sk_OPENSSL_STRING_value(aia, 0);
129
130 Returns string of the form: URI:uri1 \0 URI:uri2 \0 ... URI:urin \0\0
131 (without spaces)
132 */
133 static char *get_ocsp_url_from_aia(X509 * cert, char** urls)
134 {
135 BIO* mem=NULL;
136 ACCESS_DESCRIPTION* ad=NULL;
137 STACK_OF(ACCESS_DESCRIPTION)* ads=NULL;
138 int adsnum;
139 int crit = 0;
140 int idx = 0;
141 int i;
142
143 if(!cert||!urls)
144 return NULL;
145
146 *urls=NULL;
147
148 mem=BIO_new(BIO_s_mem());
149 if(!mem)
150 goto cleanup;
151
152 ads=(STACK_OF(ACCESS_DESCRIPTION)*)X509_get_ext_d2i(cert,
153 NID_info_access, &crit, &idx);
154 if(!ads)
155 goto cleanup;
156 adsnum=sk_ACCESS_DESCRIPTION_num(ads);
157
158 for(i=0; i<adsnum; i++){
159 ad=sk_ACCESS_DESCRIPTION_value(ads, i);
160 if(!ad)
161 continue;
162 if(OBJ_obj2nid(ad->method) == NID_ad_OCSP){
163 if(GENERAL_NAME_print(mem, ad->location)<=0)
164 goto cleanup;
165
166 BIO_write(mem, "\0", 1);
167 }
168 }
169
170 BIO_write(mem, "\0\0", 2);
171 BIO_flush(mem);
172
173 BIO_get_mem_data(mem, urls);
174 BIO_set_close(mem, BIO_NOCLOSE);
175
176 cleanup:
177
178 if(ads)
179 sk_ACCESS_DESCRIPTION_free(ads);
180 if(mem)
181 BIO_free(mem);
182
183 return *urls;
184 }
185
186 /*TODO error codes in this function has to be passed to canl_ctx somehow*/
187 /*Timeout shoult be in data structure*/
188 int do_ocsp_verify (canl_ocsprequest_t *data)
189 {
190 OCSP_REQUEST *req = NULL;
191 OCSP_RESPONSE *resp = NULL;
192 OCSP_BASICRESP *basic = NULL;
193 X509_STORE *store = NULL;
194 int rc = 0, reason = 0, ssl = 0, status = 0;
195 char *host = NULL, *path = NULL, *port = NULL;
196 OCSP_CERTID *id = NULL;
197 char *chosenurl = NULL;
198 char *tr_chosenurl = NULL;
199 canl_ocspresult_t result = 0;
200 ASN1_GENERALIZEDTIME *producedAt, *thisUpdate, *nextUpdate;
201 int timeout = -1; // -1 means no timeout - use blocking I/O
202 unsigned long verify_flags = 0;
203
204 if (!data || !data->cert) { // TODO || !data->issuer ?
205 result = EINVAL; //TODO error code
206 return result;
207 }
208
209 /*get url from cert or use some implicit value*/
210 /*TODO duplicate the value*/
211 if (data->url)
212 chosenurl = strdup(data->url);
213 else
214 if (!get_ocsp_url_from_aia(data->cert, &chosenurl)) {
215 result = CANL_OCSPRESULT_ERROR_NOAIAOCSPURI;
216 goto end;
217 }
218 /* It is necessary to truncate chosenurl for URI:*/
219 tr_chosenurl = strstr(chosenurl, "URI:");
220 if (tr_chosenurl)
221 tr_chosenurl += 4;
222 else
223 tr_chosenurl = chosenurl;
224
225 /*get connection parameters out of the tr_chosenurl.
226 Determine whether to use encrypted (ssl) connection (based on the url
227 format). Url is http[s]://host where host consists of
228 DN [:port] and [path]*/
229 if (!OCSP_parse_url(tr_chosenurl, &host, &port, &path, &ssl)) {
230 result = CANL_OCSPRESULT_ERROR_BADOCSPADDRESS;
231 goto end;
232 }
233 /*Make new OCSP_REQUEST*/
234 if (!(req = OCSP_REQUEST_new())) {
235 result = CANL_OCSPRESULT_ERROR_OUTOFMEMORY;
236 goto end;
237 }
238
239 /*map a cert and its issuer to an ID*/
240 id = OCSP_cert_to_id(0, data->cert, data->issuer);
241
242 /* Add an id and nonce to the request*/
243 if (!id || !OCSP_request_add0_id(req, id))
244 goto end;
245 if (USENONCE)
246 OCSP_request_add1_nonce(req, 0, -1);
247
248 /* sign the request
249 Default hash algorithm is sha1(), might be changed.
250 Do not add additional certificates to request
251 Do not use flags (e.g. like -no_certs for command line ) now */
252 if (data->sign_cert && data->sign_key &&
253 !OCSP_request_sign(req, data->sign_cert, data->sign_key,
254 EVP_sha1(), 0, 0)) {
255 result = CANL_OCSPRESULT_ERROR_SIGNFAILURE;
256 goto end;
257 }
258
259
260 /* establish a connection to the OCSP responder */
261 if (!(resp = send_request(req, host, path, atoi(port), ssl, timeout))) {
262 result = CANL_OCSPRESULT_ERROR_CONNECTFAILURE;
263 goto end;
264 }
265
266 /* send the request and get a response */
267 if ((rc = OCSP_response_status(resp)) != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
268 switch (rc) {
269 case OCSP_RESPONSE_STATUS_MALFORMEDREQUEST:
270 result = CANL_OCSPRESULT_ERROR_MALFORMEDREQUEST; break;
271 case OCSP_RESPONSE_STATUS_INTERNALERROR:
272 result = CANL_OCSPRESULT_ERROR_INTERNALERROR; break;
273 case OCSP_RESPONSE_STATUS_TRYLATER:
274 result = CANL_OCSPRESULT_ERROR_TRYLATER; break;
275 case OCSP_RESPONSE_STATUS_SIGREQUIRED:
276 result = CANL_OCSPRESULT_ERROR_SIGREQUIRED; break;
277 case OCSP_RESPONSE_STATUS_UNAUTHORIZED:
278 result = CANL_OCSPRESULT_ERROR_UNAUTHORIZED; break;
279 }
280 goto end;
281 }
282
283 /* verify the response */
284 result = CANL_OCSPRESULT_ERROR_INVALIDRESPONSE;
285 if (!(basic = OCSP_response_get1_basic(resp)))
286 goto end;
287 if (USENONCE && OCSP_check_nonce(req, basic) <= 0)
288 goto end;
289 /* TODO is this compulsory? */
290 store = canl_create_x509store(&data->store);
291 if (!store)
292 goto end;
293
294 /* The last param. may be used when OCSP API is fully defined*/
295 rc = OCSP_basic_verify(basic, data->cert_chain, store, verify_flags);
296 if (rc < 0)
297 rc = OCSP_basic_verify(basic, NULL, store, 0);
298 if (rc <= 0) {
299 /*response verify failure*/
300 result = CANL_OCSPRESULT_ERROR_VERIFYRESPONSE;
301 goto end;
302 }
303
304 if (!OCSP_resp_find_status(basic, id, &status, &reason, &producedAt,
305 &thisUpdate, &nextUpdate)){
306 result = CANL_OCSPRESULT_ERROR_NOSTATUS;
307 goto end;
308 }
309 if (!OCSP_check_validity(thisUpdate, nextUpdate,
310 data->skew, data->maxage)) {
311 result = CANL_OCSPRESULT_ERROR_INVTIME;
312 goto end;
313 }
314
315 /* All done. Set the return code based on the status from the response. */
316 if (status == V_OCSP_CERTSTATUS_REVOKED) {
317 result = CANL_OCSPRESULT_CERTIFICATE_REVOKED;
318 /*TODO myproxy_log("OCSP status revoked!"); */
319 } else {
320 result = CANL_OCSPRESULT_CERTIFICATE_VALID;
321 /*TODO myproxy_log("OCSP status valid"); */
322 }
323 end:
324 if (host)
325 OPENSSL_free(host);
326 if (port)
327 OPENSSL_free(port);
328 if (path)
329 OPENSSL_free(path);
330 if (req)
331 OCSP_REQUEST_free(req);
332 if (resp)
333 OCSP_RESPONSE_free(resp);
334 if (basic)
335 OCSP_BASICRESP_free(basic);
336 if (chosenurl)
337 free(chosenurl);
338 if (store)
339 X509_STORE_free(store);
340
341 return result;
342 }
343
344 static OCSP_RESPONSE *
345 send_request(OCSP_REQUEST *req, char *host, char *path, int port, int ssl,
346 int req_timeout) {
347 BIO *conn;
348 SSL_CTX *ctx_in = NULL;
349 OCSP_RESPONSE *resp = NULL;
350
351 if (!(conn = BIO_new_connect(host)))
352 goto end;
353 BIO_set_conn_port(conn, &port);
354
355 if (ssl){
356 BIO *sbio;
357 ctx_in = SSL_CTX_new(TLS_client_method());
358 if (ctx_in == NULL) {
359 goto end;
360 }
361 //SSL_CTX_set_cert_store(ctx_in, store);
362 /*TODO verify using OCSP? Infinite loop
363 !!!!!!!!!!!!!!!!!!!!!!!ยง
364 SSL_CTX_set_mode(ctx_in, SSL_MODE_AUTO_RETRY); ? - return only after
365 the handshake and successful completion*/
366 SSL_CTX_set_verify(ctx_in, SSL_VERIFY_PEER, NULL);
367
368 sbio = BIO_new_ssl(ctx_in, 1);
369 conn = BIO_push(sbio, conn);
370 /*
371 BIO_get_ssl(conn, &ssl_ptr);
372
373 TODO figure out, how to check cert without canl_ctx
374
375 if (!check_hostname_cert(SSL_get_peer_certificate(ssl_ptr), host))
376 goto end;
377
378 TODO verify certs in OCSP at this place? openssl CL tool does not do
379 that.
380 if (SSL_get_verify_result(ssl_ptr) != X509_V_OK)
381 goto end;
382 */
383 }
384
385 resp = query_responder(conn, path, req, req_timeout);
386
387 end:
388 if (conn)
389 BIO_free_all(conn);
390 if (ctx_in)
391 SSL_CTX_free(ctx_in);
392 return resp;
393 }
394
395 #ifdef OPENSSL_SYSNAME_WIN32
396 #define openssl_fdset(a,b) FD_SET((unsigned int)a, b)
397 #else
398 #define openssl_fdset(a,b) FD_SET(a, b)
399 #endif
400
401 #if SSLEAY_VERSION_NUMBER >= 0x0090900fL
402 /*TODO the timeout variable should be modified if TO is reached.
403 Somehow retur error codes! */
404 static OCSP_RESPONSE *
405 query_responder(BIO *conn, char *path, OCSP_REQUEST *req, int req_timeout)
406 {
407 OCSP_RESPONSE *rsp = NULL;
408 int fd;
409 int rv;
410 OCSP_REQ_CTX *ctx = NULL;
411 fd_set confds;
412 struct timeval tv;
413
414 /*If timeout is set, the nonblocking I/O flag is set*/
415 if (req_timeout != -1)
416 BIO_set_nbio(conn, 1);
417
418 rv = BIO_do_connect(conn);
419 if ((rv <= 0) && ((req_timeout == -1) || !BIO_should_retry(conn)))
420 {
421 /*connect failed*/
422 return NULL;
423 }
424
425 if (BIO_get_fd(conn, &fd) <= 0)
426 {
427 /*Cannot get the socket*/
428 goto err;
429 }
430
431 if (req_timeout != -1 && rv <= 0)
432 {
433 /*try connecting untill timeout is reached*/
434 FD_ZERO(&confds);
435 FD_SET(fd, &confds);
436 tv.tv_usec = 0;
437 tv.tv_sec = req_timeout;
438 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
439 if (rv == 0)
440 {
441 /*Timeout reached*/
442 return NULL;
443 }
444 }
445
446
447 /*Prepare OCSP_REQ_CTX*/
448 ctx = OCSP_sendreq_new(conn, path, NULL, -1);
449 if (!ctx)
450 return NULL;
451 if (!OCSP_REQ_CTX_set1_req(ctx, req))
452 goto err;
453
454 /*send the OCSP request and wait for the response*/
455 for (;;)
456 {
457 rv = OCSP_sendreq_nbio(&rsp, ctx);
458 if (rv != -1)
459 break;
460
461 /* Blocking I/O flag set
462 TODO - might end in an infinite loop? - what about
463 SSL_MODE_AUTO_RETRY ?? */
464 if (req_timeout == -1)
465 continue;
466 FD_ZERO(&confds);
467 openssl_fdset(fd, &confds);
468 tv.tv_usec = 0;
469 tv.tv_sec = req_timeout;
470 if (BIO_should_read(conn))
471 rv = select(fd + 1, (void *)&confds, NULL, NULL, &tv);
472 else if (BIO_should_write(conn))
473 rv = select(fd + 1, NULL, (void *)&confds, NULL, &tv);
474 else
475 {
476 /* Unexpected retry condition */
477 goto err;
478 }
479 if (rv == 0)
480 {
481 /*Timeout on request */
482 break;
483 }
484 if (rv == -1)
485 {
486 /*Select error*/
487 break;
488 }
489
490 }
491 err:
492 if (ctx)
493 OCSP_REQ_CTX_free(ctx);
494 return rsp;
495 }
496 #endif
497
498 #if SSLEAY_VERSION_NUMBER < 0x0090900fL
499 /*TODO the timeout variable should be modified if TO is reached.
500 Somehow retur error codes! */
501 static OCSP_RESPONSE *
502 query_responder(BIO *conn, char *path, OCSP_REQUEST *req, int req_timeout)
503 {
504 OCSP_RESPONSE *rsp = NULL;
505
506 /*openssl does support non blocking BIO for OCSP_send_request*/
507
508 /*openssl does not support non blocking BIO for OCSP_send_request*/
509
510 if (BIO_do_connect(conn) <= 0)
511 {
512 /*Error connecting BIO*/
513 goto err;
514 }
515
516 rsp = OCSP_sendreq_bio(conn, path, req);
517 if (!rsp) {
518 /*no response from the server*/
519 goto err;
520 }
521
522 err:
523 return rsp;
524 }
525 #endif