1 /* $OpenBSD: ts_rsp_sign.c,v 1.35 2024/03/26 00:39:22 beck Exp $ */ 2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL 3 * project 2002. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <sys/time.h> 60 61 #include <string.h> 62 63 #include <openssl/err.h> 64 #include <openssl/objects.h> 65 #include <openssl/pkcs7.h> 66 #include <openssl/ts.h> 67 68 #include "evp_local.h" 69 #include "ts_local.h" 70 #include "x509_local.h" 71 72 /* Private function declarations. */ 73 74 static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *); 75 static int def_time_cb(struct TS_resp_ctx *, void *, time_t *sec, long *usec); 76 static int def_extension_cb(struct TS_resp_ctx *, X509_EXTENSION *, void *); 77 78 static void TS_RESP_CTX_init(TS_RESP_CTX *ctx); 79 static void TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx); 80 static int TS_RESP_check_request(TS_RESP_CTX *ctx); 81 static ASN1_OBJECT *TS_RESP_get_policy(TS_RESP_CTX *ctx); 82 static TS_TST_INFO *TS_RESP_create_tst_info(TS_RESP_CTX *ctx, 83 ASN1_OBJECT *policy); 84 static int TS_RESP_process_extensions(TS_RESP_CTX *ctx); 85 static int TS_RESP_sign(TS_RESP_CTX *ctx); 86 87 static ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert, 88 STACK_OF(X509) *certs); 89 static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed); 90 static int TS_TST_INFO_content_new(PKCS7 *p7); 91 static int ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc); 92 93 /* Default callbacks for response generation. */ 94 95 static ASN1_INTEGER * 96 def_serial_cb(struct TS_resp_ctx *ctx, void *data) 97 { 98 ASN1_INTEGER *serial; 99 100 if ((serial = ASN1_INTEGER_new()) == NULL) 101 goto err; 102 if (!ASN1_INTEGER_set(serial, 1)) 103 goto err; 104 105 return serial; 106 107 err: 108 ASN1_INTEGER_free(serial); 109 TSerror(ERR_R_MALLOC_FAILURE); 110 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 111 "Error during serial number generation."); 112 113 return NULL; 114 } 115 116 /* Use the gettimeofday function call. */ 117 static int 118 def_time_cb(struct TS_resp_ctx *ctx, void *data, time_t *sec, long *usec) 119 { 120 struct timeval tv; 121 122 if (gettimeofday(&tv, NULL) != 0) { 123 TSerror(TS_R_TIME_SYSCALL_ERROR); 124 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 125 "Time is not available."); 126 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE); 127 return 0; 128 } 129 /* Return time to caller. */ 130 *sec = tv.tv_sec; 131 *usec = tv.tv_usec; 132 133 return 1; 134 } 135 136 static int 137 def_extension_cb(struct TS_resp_ctx *ctx, X509_EXTENSION *ext, void *data) 138 { 139 /* No extensions are processed here. */ 140 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 141 "Unsupported extension."); 142 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_EXTENSION); 143 return 0; 144 } 145 146 void 147 TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data) 148 { 149 ctx->time_cb = cb; 150 ctx->time_cb_data = data; 151 } 152 LCRYPTO_ALIAS(TS_RESP_CTX_set_time_cb); 153 154 /* TS_RESP_CTX management functions. */ 155 156 TS_RESP_CTX * 157 TS_RESP_CTX_new(void) 158 { 159 TS_RESP_CTX *ctx; 160 161 if (!(ctx = calloc(1, sizeof(TS_RESP_CTX)))) { 162 TSerror(ERR_R_MALLOC_FAILURE); 163 return NULL; 164 } 165 166 /* Setting default callbacks. */ 167 ctx->serial_cb = def_serial_cb; 168 ctx->time_cb = def_time_cb; 169 ctx->extension_cb = def_extension_cb; 170 171 return ctx; 172 } 173 LCRYPTO_ALIAS(TS_RESP_CTX_new); 174 175 void 176 TS_RESP_CTX_free(TS_RESP_CTX *ctx) 177 { 178 if (!ctx) 179 return; 180 181 X509_free(ctx->signer_cert); 182 EVP_PKEY_free(ctx->signer_key); 183 sk_X509_pop_free(ctx->certs, X509_free); 184 sk_ASN1_OBJECT_pop_free(ctx->policies, ASN1_OBJECT_free); 185 ASN1_OBJECT_free(ctx->default_policy); 186 sk_EVP_MD_free(ctx->mds); /* No EVP_MD_free method exists. */ 187 ASN1_INTEGER_free(ctx->seconds); 188 ASN1_INTEGER_free(ctx->millis); 189 ASN1_INTEGER_free(ctx->micros); 190 free(ctx); 191 } 192 LCRYPTO_ALIAS(TS_RESP_CTX_free); 193 194 int 195 TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer) 196 { 197 if (X509_check_purpose(signer, X509_PURPOSE_TIMESTAMP_SIGN, 0) != 1) { 198 TSerror(TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE); 199 return 0; 200 } 201 X509_free(ctx->signer_cert); 202 ctx->signer_cert = signer; 203 CRYPTO_add(&ctx->signer_cert->references, +1, CRYPTO_LOCK_X509); 204 return 1; 205 } 206 LCRYPTO_ALIAS(TS_RESP_CTX_set_signer_cert); 207 208 int 209 TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key) 210 { 211 EVP_PKEY_free(ctx->signer_key); 212 ctx->signer_key = key; 213 CRYPTO_add(&ctx->signer_key->references, +1, CRYPTO_LOCK_EVP_PKEY); 214 215 return 1; 216 } 217 LCRYPTO_ALIAS(TS_RESP_CTX_set_signer_key); 218 219 int 220 TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *def_policy) 221 { 222 if (ctx->default_policy) 223 ASN1_OBJECT_free(ctx->default_policy); 224 if (!(ctx->default_policy = OBJ_dup(def_policy))) 225 goto err; 226 return 1; 227 228 err: 229 TSerror(ERR_R_MALLOC_FAILURE); 230 return 0; 231 } 232 LCRYPTO_ALIAS(TS_RESP_CTX_set_def_policy); 233 234 int 235 TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs) 236 { 237 int i; 238 239 if (ctx->certs) { 240 sk_X509_pop_free(ctx->certs, X509_free); 241 ctx->certs = NULL; 242 } 243 if (!certs) 244 return 1; 245 if (!(ctx->certs = sk_X509_dup(certs))) { 246 TSerror(ERR_R_MALLOC_FAILURE); 247 return 0; 248 } 249 for (i = 0; i < sk_X509_num(ctx->certs); ++i) { 250 X509 *cert = sk_X509_value(ctx->certs, i); 251 CRYPTO_add(&cert->references, +1, CRYPTO_LOCK_X509); 252 } 253 254 return 1; 255 } 256 LCRYPTO_ALIAS(TS_RESP_CTX_set_certs); 257 258 int 259 TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *policy) 260 { 261 ASN1_OBJECT *copy = NULL; 262 263 /* Create new policy stack if necessary. */ 264 if (!ctx->policies && !(ctx->policies = sk_ASN1_OBJECT_new_null())) 265 goto err; 266 if (!(copy = OBJ_dup(policy))) 267 goto err; 268 if (!sk_ASN1_OBJECT_push(ctx->policies, copy)) 269 goto err; 270 271 return 1; 272 273 err: 274 TSerror(ERR_R_MALLOC_FAILURE); 275 ASN1_OBJECT_free(copy); 276 return 0; 277 } 278 LCRYPTO_ALIAS(TS_RESP_CTX_add_policy); 279 280 int 281 TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md) 282 { 283 /* Create new md stack if necessary. */ 284 if (!ctx->mds && !(ctx->mds = sk_EVP_MD_new_null())) 285 goto err; 286 /* Add the shared md, no copy needed. */ 287 if (!sk_EVP_MD_push(ctx->mds, (EVP_MD *)md)) 288 goto err; 289 290 return 1; 291 292 err: 293 TSerror(ERR_R_MALLOC_FAILURE); 294 return 0; 295 } 296 LCRYPTO_ALIAS(TS_RESP_CTX_add_md); 297 298 #define TS_RESP_CTX_accuracy_free(ctx) \ 299 ASN1_INTEGER_free(ctx->seconds); \ 300 ctx->seconds = NULL; \ 301 ASN1_INTEGER_free(ctx->millis); \ 302 ctx->millis = NULL; \ 303 ASN1_INTEGER_free(ctx->micros); \ 304 ctx->micros = NULL; 305 306 int 307 TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx, int secs, int millis, int micros) 308 { 309 TS_RESP_CTX_accuracy_free(ctx); 310 if (secs && (!(ctx->seconds = ASN1_INTEGER_new()) || 311 !ASN1_INTEGER_set(ctx->seconds, secs))) 312 goto err; 313 if (millis && (!(ctx->millis = ASN1_INTEGER_new()) || 314 !ASN1_INTEGER_set(ctx->millis, millis))) 315 goto err; 316 if (micros && (!(ctx->micros = ASN1_INTEGER_new()) || 317 !ASN1_INTEGER_set(ctx->micros, micros))) 318 goto err; 319 320 return 1; 321 322 err: 323 TS_RESP_CTX_accuracy_free(ctx); 324 TSerror(ERR_R_MALLOC_FAILURE); 325 return 0; 326 } 327 LCRYPTO_ALIAS(TS_RESP_CTX_set_accuracy); 328 329 void 330 TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags) 331 { 332 ctx->flags |= flags; 333 } 334 LCRYPTO_ALIAS(TS_RESP_CTX_add_flags); 335 336 void 337 TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data) 338 { 339 ctx->serial_cb = cb; 340 ctx->serial_cb_data = data; 341 } 342 LCRYPTO_ALIAS(TS_RESP_CTX_set_serial_cb); 343 344 void 345 TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx, TS_extension_cb cb, void *data) 346 { 347 ctx->extension_cb = cb; 348 ctx->extension_cb_data = data; 349 } 350 LCRYPTO_ALIAS(TS_RESP_CTX_set_extension_cb); 351 352 int 353 TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx, int status, const char *text) 354 { 355 TS_STATUS_INFO *si = NULL; 356 ASN1_UTF8STRING *utf8_text = NULL; 357 int ret = 0; 358 359 if (!(si = TS_STATUS_INFO_new())) 360 goto err; 361 if (!ASN1_INTEGER_set(si->status, status)) 362 goto err; 363 if (text) { 364 if (!(utf8_text = ASN1_UTF8STRING_new()) || 365 !ASN1_STRING_set(utf8_text, text, strlen(text))) 366 goto err; 367 if (!si->text && !(si->text = sk_ASN1_UTF8STRING_new_null())) 368 goto err; 369 if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text)) 370 goto err; 371 utf8_text = NULL; /* Ownership is lost. */ 372 } 373 if (!TS_RESP_set_status_info(ctx->response, si)) 374 goto err; 375 ret = 1; 376 377 err: 378 if (!ret) 379 TSerror(ERR_R_MALLOC_FAILURE); 380 TS_STATUS_INFO_free(si); 381 ASN1_UTF8STRING_free(utf8_text); 382 return ret; 383 } 384 LCRYPTO_ALIAS(TS_RESP_CTX_set_status_info); 385 386 int 387 TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx, int status, const char *text) 388 { 389 int ret = 1; 390 TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response); 391 392 if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) { 393 /* Status has not been set, set it now. */ 394 ret = TS_RESP_CTX_set_status_info(ctx, status, text); 395 } 396 return ret; 397 } 398 LCRYPTO_ALIAS(TS_RESP_CTX_set_status_info_cond); 399 400 int 401 TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure) 402 { 403 TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response); 404 405 if (!si->failure_info && !(si->failure_info = ASN1_BIT_STRING_new())) 406 goto err; 407 if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1)) 408 goto err; 409 return 1; 410 411 err: 412 TSerror(ERR_R_MALLOC_FAILURE); 413 return 0; 414 } 415 LCRYPTO_ALIAS(TS_RESP_CTX_add_failure_info); 416 417 TS_REQ * 418 TS_RESP_CTX_get_request(TS_RESP_CTX *ctx) 419 { 420 return ctx->request; 421 } 422 LCRYPTO_ALIAS(TS_RESP_CTX_get_request); 423 424 TS_TST_INFO * 425 TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx) 426 { 427 return ctx->tst_info; 428 } 429 LCRYPTO_ALIAS(TS_RESP_CTX_get_tst_info); 430 431 int 432 TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx, unsigned precision) 433 { 434 if (precision > 0) 435 return 0; 436 ctx->clock_precision_digits = precision; 437 return 1; 438 } 439 LCRYPTO_ALIAS(TS_RESP_CTX_set_clock_precision_digits); 440 441 /* Main entry method of the response generation. */ 442 TS_RESP * 443 TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio) 444 { 445 ASN1_OBJECT *policy; 446 TS_RESP *response; 447 int result = 0; 448 449 TS_RESP_CTX_init(ctx); 450 451 /* Creating the response object. */ 452 if (!(ctx->response = TS_RESP_new())) { 453 TSerror(ERR_R_MALLOC_FAILURE); 454 goto end; 455 } 456 457 /* Parsing DER request. */ 458 if (!(ctx->request = d2i_TS_REQ_bio(req_bio, NULL))) { 459 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 460 "Bad request format or " 461 "system error."); 462 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT); 463 goto end; 464 } 465 466 /* Setting default status info. */ 467 if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL)) 468 goto end; 469 470 /* Checking the request format. */ 471 if (!TS_RESP_check_request(ctx)) 472 goto end; 473 474 /* Checking acceptable policies. */ 475 if (!(policy = TS_RESP_get_policy(ctx))) 476 goto end; 477 478 /* Creating the TS_TST_INFO object. */ 479 if (!(ctx->tst_info = TS_RESP_create_tst_info(ctx, policy))) 480 goto end; 481 482 /* Processing extensions. */ 483 if (!TS_RESP_process_extensions(ctx)) 484 goto end; 485 486 /* Generating the signature. */ 487 if (!TS_RESP_sign(ctx)) 488 goto end; 489 490 /* Everything was successful. */ 491 result = 1; 492 493 end: 494 if (!result) { 495 TSerror(TS_R_RESPONSE_SETUP_ERROR); 496 if (ctx->response != NULL) { 497 if (TS_RESP_CTX_set_status_info_cond(ctx, 498 TS_STATUS_REJECTION, "Error during response " 499 "generation.") == 0) { 500 TS_RESP_free(ctx->response); 501 ctx->response = NULL; 502 } 503 } 504 } 505 response = ctx->response; 506 ctx->response = NULL; /* Ownership will be returned to caller. */ 507 TS_RESP_CTX_cleanup(ctx); 508 return response; 509 } 510 LCRYPTO_ALIAS(TS_RESP_create_response); 511 512 /* Initializes the variable part of the context. */ 513 static void 514 TS_RESP_CTX_init(TS_RESP_CTX *ctx) 515 { 516 ctx->request = NULL; 517 ctx->response = NULL; 518 ctx->tst_info = NULL; 519 } 520 521 /* Cleans up the variable part of the context. */ 522 static void 523 TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx) 524 { 525 TS_REQ_free(ctx->request); 526 ctx->request = NULL; 527 TS_RESP_free(ctx->response); 528 ctx->response = NULL; 529 TS_TST_INFO_free(ctx->tst_info); 530 ctx->tst_info = NULL; 531 } 532 533 /* Checks the format and content of the request. */ 534 static int 535 TS_RESP_check_request(TS_RESP_CTX *ctx) 536 { 537 TS_REQ *request = ctx->request; 538 TS_MSG_IMPRINT *msg_imprint; 539 X509_ALGOR *md_alg; 540 int md_alg_id; 541 const ASN1_OCTET_STRING *digest; 542 EVP_MD *md = NULL; 543 int i; 544 545 /* Checking request version. */ 546 if (TS_REQ_get_version(request) != 1) { 547 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 548 "Bad request version."); 549 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST); 550 return 0; 551 } 552 553 /* Checking message digest algorithm. */ 554 msg_imprint = TS_REQ_get_msg_imprint(request); 555 md_alg = TS_MSG_IMPRINT_get_algo(msg_imprint); 556 md_alg_id = OBJ_obj2nid(md_alg->algorithm); 557 for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) { 558 EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i); 559 if (md_alg_id == EVP_MD_type(current_md)) 560 md = current_md; 561 } 562 if (!md) { 563 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 564 "Message digest algorithm is " 565 "not supported."); 566 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG); 567 return 0; 568 } 569 570 /* No message digest takes parameter. */ 571 if (md_alg->parameter && 572 ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) { 573 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 574 "Superfluous message digest " 575 "parameter."); 576 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG); 577 return 0; 578 } 579 /* Checking message digest size. */ 580 digest = TS_MSG_IMPRINT_get_msg(msg_imprint); 581 if (digest->length != EVP_MD_size(md)) { 582 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 583 "Bad message digest."); 584 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT); 585 return 0; 586 } 587 588 return 1; 589 } 590 591 /* Returns the TSA policy based on the requested and acceptable policies. */ 592 static ASN1_OBJECT * 593 TS_RESP_get_policy(TS_RESP_CTX *ctx) 594 { 595 ASN1_OBJECT *requested = TS_REQ_get_policy_id(ctx->request); 596 ASN1_OBJECT *policy = NULL; 597 int i; 598 599 if (ctx->default_policy == NULL) { 600 TSerror(TS_R_INVALID_NULL_POINTER); 601 return NULL; 602 } 603 /* Return the default policy if none is requested or the default is 604 requested. */ 605 if (!requested || !OBJ_cmp(requested, ctx->default_policy)) 606 policy = ctx->default_policy; 607 608 /* Check if the policy is acceptable. */ 609 for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) { 610 ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i); 611 if (!OBJ_cmp(requested, current)) 612 policy = current; 613 } 614 if (!policy) { 615 TSerror(TS_R_UNACCEPTABLE_POLICY); 616 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, 617 "Requested policy is not " 618 "supported."); 619 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY); 620 } 621 return policy; 622 } 623 624 /* Creates the TS_TST_INFO object based on the settings of the context. */ 625 static TS_TST_INFO * 626 TS_RESP_create_tst_info(TS_RESP_CTX *ctx, ASN1_OBJECT *policy) 627 { 628 int result = 0; 629 TS_TST_INFO *tst_info = NULL; 630 ASN1_INTEGER *serial = NULL; 631 ASN1_GENERALIZEDTIME *asn1_time = NULL; 632 time_t sec; 633 long usec; 634 TS_ACCURACY *accuracy = NULL; 635 const ASN1_INTEGER *nonce; 636 GENERAL_NAME *tsa_name = NULL; 637 638 if (!(tst_info = TS_TST_INFO_new())) 639 goto end; 640 if (!TS_TST_INFO_set_version(tst_info, 1)) 641 goto end; 642 if (!TS_TST_INFO_set_policy_id(tst_info, policy)) 643 goto end; 644 if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint)) 645 goto end; 646 if (!(serial = (*ctx->serial_cb)(ctx, ctx->serial_cb_data)) || 647 !TS_TST_INFO_set_serial(tst_info, serial)) 648 goto end; 649 if (!(*ctx->time_cb)(ctx, ctx->time_cb_data, &sec, &usec) || 650 ((asn1_time = ASN1_GENERALIZEDTIME_set(NULL, sec)) == NULL) || 651 !TS_TST_INFO_set_time(tst_info, asn1_time)) 652 goto end; 653 654 /* Setting accuracy if needed. */ 655 if ((ctx->seconds || ctx->millis || ctx->micros) && 656 !(accuracy = TS_ACCURACY_new())) 657 goto end; 658 659 if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds)) 660 goto end; 661 if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis)) 662 goto end; 663 if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros)) 664 goto end; 665 if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy)) 666 goto end; 667 668 /* Setting ordering. */ 669 if ((ctx->flags & TS_ORDERING) && 670 !TS_TST_INFO_set_ordering(tst_info, 1)) 671 goto end; 672 673 /* Setting nonce if needed. */ 674 if ((nonce = TS_REQ_get_nonce(ctx->request)) != NULL && 675 !TS_TST_INFO_set_nonce(tst_info, nonce)) 676 goto end; 677 678 /* Setting TSA name to subject of signer certificate. */ 679 if (ctx->flags & TS_TSA_NAME) { 680 if (!(tsa_name = GENERAL_NAME_new())) 681 goto end; 682 tsa_name->type = GEN_DIRNAME; 683 tsa_name->d.dirn = 684 X509_NAME_dup(X509_get_subject_name(ctx->signer_cert)); 685 if (!tsa_name->d.dirn) 686 goto end; 687 if (!TS_TST_INFO_set_tsa(tst_info, tsa_name)) 688 goto end; 689 } 690 691 result = 1; 692 693 end: 694 if (!result) { 695 TS_TST_INFO_free(tst_info); 696 tst_info = NULL; 697 TSerror(TS_R_TST_INFO_SETUP_ERROR); 698 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION, 699 "Error during TSTInfo " 700 "generation."); 701 } 702 GENERAL_NAME_free(tsa_name); 703 TS_ACCURACY_free(accuracy); 704 ASN1_GENERALIZEDTIME_free(asn1_time); 705 ASN1_INTEGER_free(serial); 706 707 return tst_info; 708 } 709 710 /* Processing the extensions of the request. */ 711 static int 712 TS_RESP_process_extensions(TS_RESP_CTX *ctx) 713 { 714 STACK_OF(X509_EXTENSION) *exts = TS_REQ_get_exts(ctx->request); 715 int i; 716 int ok = 1; 717 718 for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) { 719 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); 720 /* XXXXX The last argument was previously 721 (void *)ctx->extension_cb, but ISO C doesn't permit 722 converting a function pointer to void *. For lack of 723 better information, I'm placing a NULL there instead. 724 The callback can pick its own address out from the ctx 725 anyway... 726 */ 727 ok = (*ctx->extension_cb)(ctx, ext, NULL); 728 } 729 730 return ok; 731 } 732 733 /* Functions for signing the TS_TST_INFO structure of the context. */ 734 static int 735 TS_RESP_sign(TS_RESP_CTX *ctx) 736 { 737 int ret = 0; 738 PKCS7 *p7 = NULL; 739 PKCS7_SIGNER_INFO *si; 740 STACK_OF(X509) *certs; /* Certificates to include in sc. */ 741 ESS_SIGNING_CERT *sc = NULL; 742 ASN1_OBJECT *oid; 743 BIO *p7bio = NULL; 744 int i; 745 746 /* Check if signcert and pkey match. */ 747 if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) { 748 TSerror(TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); 749 goto err; 750 } 751 752 /* Create a new PKCS7 signed object. */ 753 if (!(p7 = PKCS7_new())) { 754 TSerror(ERR_R_MALLOC_FAILURE); 755 goto err; 756 } 757 if (!PKCS7_set_type(p7, NID_pkcs7_signed)) 758 goto err; 759 760 /* Force SignedData version to be 3 instead of the default 1. */ 761 if (!ASN1_INTEGER_set(p7->d.sign->version, 3)) 762 goto err; 763 764 /* Add signer certificate and optional certificate chain. */ 765 if (TS_REQ_get_cert_req(ctx->request)) { 766 PKCS7_add_certificate(p7, ctx->signer_cert); 767 if (ctx->certs) { 768 for (i = 0; i < sk_X509_num(ctx->certs); ++i) { 769 X509 *cert = sk_X509_value(ctx->certs, i); 770 PKCS7_add_certificate(p7, cert); 771 } 772 } 773 } 774 775 /* Add a new signer info. */ 776 if (!(si = PKCS7_add_signature(p7, ctx->signer_cert, 777 ctx->signer_key, EVP_sha1()))) { 778 TSerror(TS_R_PKCS7_ADD_SIGNATURE_ERROR); 779 goto err; 780 } 781 782 /* Add content type signed attribute to the signer info. */ 783 oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo); 784 if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, 785 V_ASN1_OBJECT, oid)) { 786 TSerror(TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR); 787 goto err; 788 } 789 790 /* Create the ESS SigningCertificate attribute which contains 791 the signer certificate id and optionally the certificate chain. */ 792 certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL; 793 if (!(sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs))) 794 goto err; 795 796 /* Add SigningCertificate signed attribute to the signer info. */ 797 if (!ESS_add_signing_cert(si, sc)) { 798 TSerror(TS_R_ESS_ADD_SIGNING_CERT_ERROR); 799 goto err; 800 } 801 802 /* Add a new empty NID_id_smime_ct_TSTInfo encapsulated content. */ 803 if (!TS_TST_INFO_content_new(p7)) 804 goto err; 805 806 /* Add the DER encoded tst_info to the PKCS7 structure. */ 807 if (!(p7bio = PKCS7_dataInit(p7, NULL))) { 808 TSerror(ERR_R_MALLOC_FAILURE); 809 goto err; 810 } 811 812 /* Convert tst_info to DER. */ 813 if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) { 814 TSerror(TS_R_TS_DATASIGN); 815 goto err; 816 } 817 818 /* Create the signature and add it to the signer info. */ 819 if (!PKCS7_dataFinal(p7, p7bio)) { 820 TSerror(TS_R_TS_DATASIGN); 821 goto err; 822 } 823 824 /* Set new PKCS7 and TST_INFO objects. */ 825 TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info); 826 p7 = NULL; /* Ownership is lost. */ 827 ctx->tst_info = NULL; /* Ownership is lost. */ 828 829 ret = 1; 830 831 err: 832 if (!ret) 833 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION, 834 "Error during signature " 835 "generation."); 836 BIO_free_all(p7bio); 837 ESS_SIGNING_CERT_free(sc); 838 PKCS7_free(p7); 839 return ret; 840 } 841 842 static ESS_SIGNING_CERT * 843 ESS_SIGNING_CERT_new_init(X509 *signcert, STACK_OF(X509) *certs) 844 { 845 ESS_CERT_ID *cid; 846 ESS_SIGNING_CERT *sc = NULL; 847 int i; 848 849 /* Creating the ESS_CERT_ID stack. */ 850 if (!(sc = ESS_SIGNING_CERT_new())) 851 goto err; 852 if (!sc->cert_ids && !(sc->cert_ids = sk_ESS_CERT_ID_new_null())) 853 goto err; 854 855 /* Adding the signing certificate id. */ 856 if (!(cid = ESS_CERT_ID_new_init(signcert, 0)) || 857 !sk_ESS_CERT_ID_push(sc->cert_ids, cid)) 858 goto err; 859 /* Adding the certificate chain ids. */ 860 for (i = 0; i < sk_X509_num(certs); ++i) { 861 X509 *cert = sk_X509_value(certs, i); 862 if (!(cid = ESS_CERT_ID_new_init(cert, 1)) || 863 !sk_ESS_CERT_ID_push(sc->cert_ids, cid)) 864 goto err; 865 } 866 867 return sc; 868 869 err: 870 ESS_SIGNING_CERT_free(sc); 871 TSerror(ERR_R_MALLOC_FAILURE); 872 return NULL; 873 } 874 875 static ESS_CERT_ID * 876 ESS_CERT_ID_new_init(X509 *cert, int issuer_needed) 877 { 878 ESS_CERT_ID *cid = NULL; 879 GENERAL_NAME *name = NULL; 880 unsigned char cert_hash[TS_HASH_LEN]; 881 882 /* Recompute SHA1 hash of certificate if necessary (side effect). */ 883 X509_check_purpose(cert, -1, 0); 884 885 if (!(cid = ESS_CERT_ID_new())) 886 goto err; 887 888 if (!X509_digest(cert, TS_HASH_EVP, cert_hash, NULL)) 889 goto err; 890 891 if (!ASN1_OCTET_STRING_set(cid->hash, cert_hash, sizeof(cert_hash))) 892 goto err; 893 894 /* Setting the issuer/serial if requested. */ 895 if (issuer_needed) { 896 /* Creating issuer/serial structure. */ 897 if (!cid->issuer_serial && 898 !(cid->issuer_serial = ESS_ISSUER_SERIAL_new())) 899 goto err; 900 /* Creating general name from the certificate issuer. */ 901 if (!(name = GENERAL_NAME_new())) 902 goto err; 903 name->type = GEN_DIRNAME; 904 if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL) 905 goto err; 906 if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name)) 907 goto err; 908 name = NULL; /* Ownership is lost. */ 909 /* Setting the serial number. */ 910 ASN1_INTEGER_free(cid->issuer_serial->serial); 911 if (!(cid->issuer_serial->serial = 912 ASN1_INTEGER_dup(X509_get_serialNumber(cert)))) 913 goto err; 914 } 915 916 return cid; 917 918 err: 919 GENERAL_NAME_free(name); 920 ESS_CERT_ID_free(cid); 921 TSerror(ERR_R_MALLOC_FAILURE); 922 return NULL; 923 } 924 925 static int 926 TS_TST_INFO_content_new(PKCS7 *p7) 927 { 928 PKCS7 *ret = NULL; 929 ASN1_OCTET_STRING *octet_string = NULL; 930 931 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */ 932 if (!(ret = PKCS7_new())) 933 goto err; 934 if (!(ret->d.other = ASN1_TYPE_new())) 935 goto err; 936 ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo); 937 if (!(octet_string = ASN1_OCTET_STRING_new())) 938 goto err; 939 ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string); 940 octet_string = NULL; 941 942 /* Add encapsulated content to signed PKCS7 structure. */ 943 if (!PKCS7_set_content(p7, ret)) 944 goto err; 945 946 return 1; 947 948 err: 949 ASN1_OCTET_STRING_free(octet_string); 950 PKCS7_free(ret); 951 return 0; 952 } 953 954 static int 955 ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc) 956 { 957 ASN1_STRING *seq = NULL; 958 unsigned char *p, *pp = NULL; 959 int len; 960 961 len = i2d_ESS_SIGNING_CERT(sc, NULL); 962 if (!(pp = malloc(len))) { 963 TSerror(ERR_R_MALLOC_FAILURE); 964 goto err; 965 } 966 p = pp; 967 i2d_ESS_SIGNING_CERT(sc, &p); 968 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) { 969 TSerror(ERR_R_MALLOC_FAILURE); 970 goto err; 971 } 972 free(pp); 973 pp = NULL; 974 return PKCS7_add_signed_attribute(si, 975 NID_id_smime_aa_signingCertificate, V_ASN1_SEQUENCE, seq); 976 977 err: 978 ASN1_STRING_free(seq); 979 free(pp); 980 981 return 0; 982 } 983