1 /* $OpenBSD: ocsp_ext.c,v 1.9 2014/06/12 15:49:30 deraadt Exp $ */ 2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL 3 * project. */ 4 5 /* History: 6 This file was transfered to Richard Levitte from CertCo by Kathy 7 Weinhold in mid-spring 2000 to be included in OpenSSL or released 8 as a patch kit. */ 9 10 /* ==================================================================== 11 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. All advertising materials mentioning features or use of this 26 * software must display the following acknowledgment: 27 * "This product includes software developed by the OpenSSL Project 28 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 29 * 30 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 31 * endorse or promote products derived from this software without 32 * prior written permission. For written permission, please contact 33 * openssl-core@openssl.org. 34 * 35 * 5. Products derived from this software may not be called "OpenSSL" 36 * nor may "OpenSSL" appear in their names without prior written 37 * permission of the OpenSSL Project. 38 * 39 * 6. Redistributions of any form whatsoever must retain the following 40 * acknowledgment: 41 * "This product includes software developed by the OpenSSL Project 42 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 45 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 47 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 48 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 53 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 55 * OF THE POSSIBILITY OF SUCH DAMAGE. 56 * ==================================================================== 57 * 58 * This product includes cryptographic software written by Eric Young 59 * (eay@cryptsoft.com). This product includes software written by Tim 60 * Hudson (tjh@cryptsoft.com). 61 * 62 */ 63 64 #include <stdio.h> 65 #include <cryptlib.h> 66 #include <openssl/objects.h> 67 #include <openssl/x509.h> 68 #include <openssl/ocsp.h> 69 #include <openssl/rand.h> 70 #include <openssl/x509v3.h> 71 72 /* Standard wrapper functions for extensions */ 73 74 /* OCSP request extensions */ 75 76 int 77 OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x) 78 { 79 return X509v3_get_ext_count(x->tbsRequest->requestExtensions); 80 } 81 82 int 83 OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos) 84 { 85 return X509v3_get_ext_by_NID(x->tbsRequest->requestExtensions, nid, 86 lastpos); 87 } 88 89 int 90 OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, ASN1_OBJECT *obj, int lastpos) 91 { 92 return X509v3_get_ext_by_OBJ(x->tbsRequest->requestExtensions, obj, 93 lastpos); 94 } 95 96 int 97 OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos) 98 { 99 return X509v3_get_ext_by_critical(x->tbsRequest->requestExtensions, 100 crit, lastpos); 101 } 102 103 X509_EXTENSION * 104 OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc) 105 { 106 return X509v3_get_ext(x->tbsRequest->requestExtensions, loc); 107 } 108 109 X509_EXTENSION * 110 OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc) 111 { 112 return X509v3_delete_ext(x->tbsRequest->requestExtensions, loc); 113 } 114 115 void * 116 OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx) 117 { 118 return X509V3_get_d2i(x->tbsRequest->requestExtensions, nid, crit, idx); 119 } 120 121 int 122 OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit, 123 unsigned long flags) 124 { 125 return X509V3_add1_i2d(&x->tbsRequest->requestExtensions, nid, value, 126 crit, flags); 127 } 128 129 int 130 OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc) 131 { 132 return X509v3_add_ext(&(x->tbsRequest->requestExtensions), ex, 133 loc) != NULL; 134 } 135 136 /* Single extensions */ 137 138 int 139 OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x) 140 { 141 return X509v3_get_ext_count(x->singleRequestExtensions); 142 } 143 144 int 145 OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos) 146 { 147 return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos); 148 } 149 150 int 151 OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, ASN1_OBJECT *obj, int lastpos) 152 { 153 return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos); 154 } 155 156 int 157 OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos) 158 { 159 return X509v3_get_ext_by_critical(x->singleRequestExtensions, crit, 160 lastpos); 161 } 162 163 X509_EXTENSION * 164 OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc) 165 { 166 return X509v3_get_ext(x->singleRequestExtensions, loc); 167 } 168 169 X509_EXTENSION * 170 OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc) 171 { 172 return X509v3_delete_ext(x->singleRequestExtensions, loc); 173 } 174 175 void * 176 OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx) 177 { 178 return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx); 179 } 180 181 int 182 OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit, 183 unsigned long flags) 184 { 185 return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit, 186 flags); 187 } 188 189 int 190 OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc) 191 { 192 return X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL; 193 } 194 195 /* OCSP Basic response */ 196 197 int 198 OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x) 199 { 200 return X509v3_get_ext_count(x->tbsResponseData->responseExtensions); 201 } 202 203 int 204 OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos) 205 { 206 return X509v3_get_ext_by_NID(x->tbsResponseData->responseExtensions, 207 nid, lastpos); 208 } 209 210 int 211 OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, ASN1_OBJECT *obj, int lastpos) 212 { 213 return X509v3_get_ext_by_OBJ(x->tbsResponseData->responseExtensions, 214 obj, lastpos); 215 } 216 217 int 218 OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit, int lastpos) 219 { 220 return X509v3_get_ext_by_critical( 221 x->tbsResponseData->responseExtensions, crit, lastpos); 222 } 223 224 X509_EXTENSION * 225 OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc) 226 { 227 return X509v3_get_ext(x->tbsResponseData->responseExtensions, loc); 228 } 229 230 X509_EXTENSION * 231 OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc) 232 { 233 return X509v3_delete_ext(x->tbsResponseData->responseExtensions, loc); 234 } 235 236 void * 237 OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit, int *idx) 238 { 239 return X509V3_get_d2i(x->tbsResponseData->responseExtensions, nid, 240 crit, idx); 241 } 242 243 int 244 OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value, int crit, 245 unsigned long flags) 246 { 247 return X509V3_add1_i2d(&x->tbsResponseData->responseExtensions, nid, 248 value, crit, flags); 249 } 250 251 int 252 OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc) 253 { 254 return X509v3_add_ext(&(x->tbsResponseData->responseExtensions), ex, 255 loc) != NULL; 256 } 257 258 /* OCSP single response extensions */ 259 260 int 261 OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x) 262 { 263 return X509v3_get_ext_count(x->singleExtensions); 264 } 265 266 int 267 OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos) 268 { 269 return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos); 270 } 271 272 int 273 OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, ASN1_OBJECT *obj, 274 int lastpos) 275 { 276 return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos); 277 } 278 279 int 280 OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit, int lastpos) 281 { 282 return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos); 283 } 284 285 X509_EXTENSION * 286 OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc) 287 { 288 return X509v3_get_ext(x->singleExtensions, loc); 289 } 290 291 X509_EXTENSION * 292 OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc) 293 { 294 return X509v3_delete_ext(x->singleExtensions, loc); 295 } 296 297 void * 298 OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit, int *idx) 299 { 300 return X509V3_get_d2i(x->singleExtensions, nid, crit, idx); 301 } 302 303 int 304 OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value, int crit, 305 unsigned long flags) 306 { 307 return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags); 308 } 309 310 int 311 OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc) 312 { 313 return X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL; 314 } 315 316 /* also CRL Entry Extensions */ 317 #if 0 318 ASN1_STRING * 319 ASN1_STRING_encode(ASN1_STRING *s, i2d_of_void *i2d, void *data, 320 STACK_OF(ASN1_OBJECT) *sk) 321 { 322 int i; 323 unsigned char *p, *b = NULL; 324 325 if (data) { 326 if ((i = i2d(data, NULL)) <= 0) 327 goto err; 328 if (!(b = p = malloc((unsigned int)i))) 329 goto err; 330 if (i2d(data, &p) <= 0) 331 goto err; 332 } else if (sk) { 333 if ((i = i2d_ASN1_SET_OF_ASN1_OBJECT(sk, NULL, 334 (I2D_OF(ASN1_OBJECT))i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 335 IS_SEQUENCE)) <= 0) 336 goto err; 337 if (!(b = p = malloc((unsigned int)i))) 338 goto err; 339 if (i2d_ASN1_SET_OF_ASN1_OBJECT(sk, &p, 340 (I2D_OF(ASN1_OBJECT))i2d, V_ASN1_SEQUENCE, 341 V_ASN1_UNIVERSAL, IS_SEQUENCE) <= 0) 342 goto err; 343 } else { 344 OCSPerr(OCSP_F_ASN1_STRING_ENCODE, OCSP_R_BAD_DATA); 345 goto err; 346 } 347 if (!s && !(s = ASN1_STRING_new())) 348 goto err; 349 if (!(ASN1_STRING_set(s, b, i))) 350 goto err; 351 free(b); 352 return s; 353 354 err: 355 free(b); 356 return NULL; 357 } 358 #endif 359 360 /* Nonce handling functions */ 361 362 /* Add a nonce to an extension stack. A nonce can be specificed or if NULL 363 * a random nonce will be generated. 364 * Note: OpenSSL 0.9.7d and later create an OCTET STRING containing the 365 * nonce, previous versions used the raw nonce. 366 */ 367 368 static int 369 ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts, unsigned char *val, int len) 370 { 371 unsigned char *tmpval; 372 ASN1_OCTET_STRING os; 373 int ret = 0; 374 375 if (len <= 0) 376 len = OCSP_DEFAULT_NONCE_LENGTH; 377 /* Create the OCTET STRING manually by writing out the header and 378 * appending the content octets. This avoids an extra memory allocation 379 * operation in some cases. Applications should *NOT* do this because 380 * it relies on library internals. 381 */ 382 os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING); 383 os.data = malloc(os.length); 384 if (os.data == NULL) 385 goto err; 386 tmpval = os.data; 387 ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL); 388 if (val) 389 memcpy(tmpval, val, len); 390 else 391 RAND_pseudo_bytes(tmpval, len); 392 if (!X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce, &os, 0, 393 X509V3_ADD_REPLACE)) 394 goto err; 395 ret = 1; 396 397 err: 398 free(os.data); 399 return ret; 400 } 401 402 /* Add nonce to an OCSP request */ 403 int 404 OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len) 405 { 406 return ocsp_add1_nonce(&req->tbsRequest->requestExtensions, val, len); 407 } 408 409 /* Same as above but for a response */ 410 int 411 OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len) 412 { 413 return ocsp_add1_nonce(&resp->tbsResponseData->responseExtensions, val, 414 len); 415 } 416 417 /* Check nonce validity in a request and response. 418 * Return value reflects result: 419 * 1: nonces present and equal. 420 * 2: nonces both absent. 421 * 3: nonce present in response only. 422 * 0: nonces both present and not equal. 423 * -1: nonce in request only. 424 * 425 * For most responders clients can check return > 0. 426 * If responder doesn't handle nonces return != 0 may be 427 * necessary. return == 0 is always an error. 428 */ 429 int 430 OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs) 431 { 432 /* 433 * Since we are only interested in the presence or absence of 434 * the nonce and comparing its value there is no need to use 435 * the X509V3 routines: this way we can avoid them allocating an 436 * ASN1_OCTET_STRING structure for the value which would be 437 * freed immediately anyway. 438 */ 439 int req_idx, resp_idx; 440 X509_EXTENSION *req_ext, *resp_ext; 441 442 req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); 443 resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, 444 NID_id_pkix_OCSP_Nonce, -1); 445 /* Check both absent */ 446 if (req_idx < 0 && resp_idx < 0) 447 return 2; 448 /* Check in request only */ 449 if (req_idx >= 0 && resp_idx < 0) 450 return -1; 451 /* Check in response but not request */ 452 if (req_idx < 0 && resp_idx >= 0) 453 return 3; 454 /* Otherwise nonce in request and response so retrieve the extensions */ 455 req_ext = OCSP_REQUEST_get_ext(req, req_idx); 456 resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx); 457 if (ASN1_OCTET_STRING_cmp(req_ext->value, resp_ext->value)) 458 return 0; 459 return 1; 460 } 461 462 /* Copy the nonce value (if any) from an OCSP request to 463 * a response. 464 */ 465 int 466 OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req) 467 { 468 X509_EXTENSION *req_ext; 469 int req_idx; 470 471 /* Check for nonce in request */ 472 req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); 473 /* If no nonce that's OK */ 474 if (req_idx < 0) 475 return 2; 476 req_ext = OCSP_REQUEST_get_ext(req, req_idx); 477 return OCSP_BASICRESP_add_ext(resp, req_ext, -1); 478 } 479 480 X509_EXTENSION * 481 OCSP_crlID_new(char *url, long *n, char *tim) 482 { 483 X509_EXTENSION *x = NULL; 484 OCSP_CRLID *cid = NULL; 485 486 if (!(cid = OCSP_CRLID_new())) 487 goto err; 488 if (url) { 489 if (!(cid->crlUrl = ASN1_IA5STRING_new())) 490 goto err; 491 if (!(ASN1_STRING_set(cid->crlUrl, url, -1))) 492 goto err; 493 } 494 if (n) { 495 if (!(cid->crlNum = ASN1_INTEGER_new())) 496 goto err; 497 if (!(ASN1_INTEGER_set(cid->crlNum, *n))) 498 goto err; 499 } 500 if (tim) { 501 if (!(cid->crlTime = ASN1_GENERALIZEDTIME_new())) 502 goto err; 503 if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim))) 504 goto err; 505 } 506 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid); 507 508 err: 509 if (cid) 510 OCSP_CRLID_free(cid); 511 return x; 512 } 513 514 /* AcceptableResponses ::= SEQUENCE OF OBJECT IDENTIFIER */ 515 X509_EXTENSION * 516 OCSP_accept_responses_new(char **oids) 517 { 518 int nid; 519 STACK_OF(ASN1_OBJECT) *sk = NULL; 520 ASN1_OBJECT *o = NULL; 521 X509_EXTENSION *x = NULL; 522 523 if (!(sk = sk_ASN1_OBJECT_new_null())) 524 return NULL; 525 while (oids && *oids) { 526 if ((nid = OBJ_txt2nid(*oids)) != NID_undef && 527 (o = OBJ_nid2obj(nid))) 528 sk_ASN1_OBJECT_push(sk, o); 529 oids++; 530 } 531 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk); 532 sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free); 533 return x; 534 } 535 536 /* ArchiveCutoff ::= GeneralizedTime */ 537 X509_EXTENSION * 538 OCSP_archive_cutoff_new(char* tim) 539 { 540 X509_EXTENSION *x = NULL; 541 ASN1_GENERALIZEDTIME *gt = NULL; 542 543 if (!(gt = ASN1_GENERALIZEDTIME_new())) 544 return NULL; 545 if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim))) 546 goto err; 547 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt); 548 549 err: 550 if (gt) 551 ASN1_GENERALIZEDTIME_free(gt); 552 return x; 553 } 554 555 /* per ACCESS_DESCRIPTION parameter are oids, of which there are currently 556 * two--NID_ad_ocsp, NID_id_ad_caIssuers--and GeneralName value. This 557 * method forces NID_ad_ocsp and uniformResourceLocator [6] IA5String. 558 */ 559 X509_EXTENSION * 560 OCSP_url_svcloc_new(X509_NAME* issuer, char **urls) 561 { 562 X509_EXTENSION *x = NULL; 563 ASN1_IA5STRING *ia5 = NULL; 564 OCSP_SERVICELOC *sloc = NULL; 565 ACCESS_DESCRIPTION *ad = NULL; 566 567 if (!(sloc = OCSP_SERVICELOC_new())) 568 goto err; 569 if (!(sloc->issuer = X509_NAME_dup(issuer))) 570 goto err; 571 if (urls && *urls && 572 !(sloc->locator = sk_ACCESS_DESCRIPTION_new_null())) 573 goto err; 574 while (urls && *urls) { 575 if (!(ad = ACCESS_DESCRIPTION_new())) 576 goto err; 577 if (!(ad->method = OBJ_nid2obj(NID_ad_OCSP))) 578 goto err; 579 if (!(ad->location = GENERAL_NAME_new())) 580 goto err; 581 if (!(ia5 = ASN1_IA5STRING_new())) 582 goto err; 583 if (!ASN1_STRING_set((ASN1_STRING*)ia5, *urls, -1)) 584 goto err; 585 ad->location->type = GEN_URI; 586 ad->location->d.ia5 = ia5; 587 ia5 = NULL; 588 if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad)) 589 goto err; 590 ad = NULL; 591 urls++; 592 } 593 x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc); 594 595 err: 596 if (ia5) 597 ASN1_IA5STRING_free(ia5); 598 if (ad) 599 ACCESS_DESCRIPTION_free(ad); 600 if (sloc) 601 OCSP_SERVICELOC_free(sloc); 602 return x; 603 } 604