2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
13 #include <openssl/objects.h>
14 #include <openssl/ts.h>
15 #include <openssl/pkcs7.h>
16 #include <openssl/crypto.h>
18 #include "crypto/ess.h"
20 DEFINE_STACK_OF(ASN1_UTF8STRING)
21 DEFINE_STACK_OF(ASN1_OBJECT)
22 DEFINE_STACK_OF_CONST(EVP_MD)
24 static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *);
25 static int def_time_cb(struct TS_resp_ctx *, void *, long *sec, long *usec);
26 static int def_extension_cb(struct TS_resp_ctx *, X509_EXTENSION *, void *);
28 static void ts_RESP_CTX_init(TS_RESP_CTX *ctx);
29 static void ts_RESP_CTX_cleanup(TS_RESP_CTX *ctx);
30 static int ts_RESP_check_request(TS_RESP_CTX *ctx);
31 static ASN1_OBJECT *ts_RESP_get_policy(TS_RESP_CTX *ctx);
32 static TS_TST_INFO *ts_RESP_create_tst_info(TS_RESP_CTX *ctx,
34 static int ts_RESP_process_extensions(TS_RESP_CTX *ctx);
35 static int ts_RESP_sign(TS_RESP_CTX *ctx);
37 static int ts_TST_INFO_content_new(PKCS7 *p7);
39 static ASN1_GENERALIZEDTIME
40 *TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *, long, long,
43 /* Default callback for response generation. */
44 static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *ctx, void *data)
46 ASN1_INTEGER *serial = ASN1_INTEGER_new();
50 if (!ASN1_INTEGER_set(serial, 1))
55 TSerr(TS_F_DEF_SERIAL_CB, ERR_R_MALLOC_FAILURE);
56 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
57 "Error during serial number generation.");
58 ASN1_INTEGER_free(serial);
62 #if defined(OPENSSL_SYS_UNIX)
64 static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
65 long *sec, long *usec)
68 if (gettimeofday(&tv, NULL) != 0) {
69 TSerr(TS_F_DEF_TIME_CB, TS_R_TIME_SYSCALL_ERROR);
70 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
71 "Time is not available.");
72 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
83 static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
84 long *sec, long *usec)
87 if (time(&t) == (time_t)-1) {
88 TSerr(TS_F_DEF_TIME_CB, TS_R_TIME_SYSCALL_ERROR);
89 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
90 "Time is not available.");
91 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
102 static int def_extension_cb(struct TS_resp_ctx *ctx, X509_EXTENSION *ext,
105 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
106 "Unsupported extension.");
107 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_EXTENSION);
111 /* TS_RESP_CTX management functions. */
113 TS_RESP_CTX *TS_RESP_CTX_new(void)
117 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
118 TSerr(TS_F_TS_RESP_CTX_NEW, ERR_R_MALLOC_FAILURE);
122 ctx->signer_md = EVP_sha256();
124 ctx->serial_cb = def_serial_cb;
125 ctx->time_cb = def_time_cb;
126 ctx->extension_cb = def_extension_cb;
131 void TS_RESP_CTX_free(TS_RESP_CTX *ctx)
136 X509_free(ctx->signer_cert);
137 EVP_PKEY_free(ctx->signer_key);
138 sk_X509_pop_free(ctx->certs, X509_free);
139 sk_ASN1_OBJECT_pop_free(ctx->policies, ASN1_OBJECT_free);
140 ASN1_OBJECT_free(ctx->default_policy);
141 sk_EVP_MD_free(ctx->mds); /* No EVP_MD_free method exists. */
142 ASN1_INTEGER_free(ctx->seconds);
143 ASN1_INTEGER_free(ctx->millis);
144 ASN1_INTEGER_free(ctx->micros);
148 int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer)
150 if (X509_check_purpose(signer, X509_PURPOSE_TIMESTAMP_SIGN, 0) != 1) {
151 TSerr(TS_F_TS_RESP_CTX_SET_SIGNER_CERT,
152 TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE);
155 X509_free(ctx->signer_cert);
156 ctx->signer_cert = signer;
157 X509_up_ref(ctx->signer_cert);
161 int TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key)
163 EVP_PKEY_free(ctx->signer_key);
164 ctx->signer_key = key;
165 EVP_PKEY_up_ref(ctx->signer_key);
170 int TS_RESP_CTX_set_signer_digest(TS_RESP_CTX *ctx, const EVP_MD *md)
176 int TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *def_policy)
178 ASN1_OBJECT_free(ctx->default_policy);
179 if ((ctx->default_policy = OBJ_dup(def_policy)) == NULL)
183 TSerr(TS_F_TS_RESP_CTX_SET_DEF_POLICY, ERR_R_MALLOC_FAILURE);
187 int TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs)
190 sk_X509_pop_free(ctx->certs, X509_free);
194 if ((ctx->certs = X509_chain_up_ref(certs)) == NULL) {
195 TSerr(TS_F_TS_RESP_CTX_SET_CERTS, ERR_R_MALLOC_FAILURE);
202 int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *policy)
204 ASN1_OBJECT *copy = NULL;
206 if (ctx->policies == NULL
207 && (ctx->policies = sk_ASN1_OBJECT_new_null()) == NULL)
209 if ((copy = OBJ_dup(policy)) == NULL)
211 if (!sk_ASN1_OBJECT_push(ctx->policies, copy))
216 TSerr(TS_F_TS_RESP_CTX_ADD_POLICY, ERR_R_MALLOC_FAILURE);
217 ASN1_OBJECT_free(copy);
221 int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md)
224 && (ctx->mds = sk_EVP_MD_new_null()) == NULL)
226 if (!sk_EVP_MD_push(ctx->mds, md))
231 TSerr(TS_F_TS_RESP_CTX_ADD_MD, ERR_R_MALLOC_FAILURE);
235 #define TS_RESP_CTX_accuracy_free(ctx) \
236 ASN1_INTEGER_free(ctx->seconds); \
237 ctx->seconds = NULL; \
238 ASN1_INTEGER_free(ctx->millis); \
239 ctx->millis = NULL; \
240 ASN1_INTEGER_free(ctx->micros); \
243 int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx,
244 int secs, int millis, int micros)
247 TS_RESP_CTX_accuracy_free(ctx);
249 && ((ctx->seconds = ASN1_INTEGER_new()) == NULL
250 || !ASN1_INTEGER_set(ctx->seconds, secs)))
253 && ((ctx->millis = ASN1_INTEGER_new()) == NULL
254 || !ASN1_INTEGER_set(ctx->millis, millis)))
257 && ((ctx->micros = ASN1_INTEGER_new()) == NULL
258 || !ASN1_INTEGER_set(ctx->micros, micros)))
263 TS_RESP_CTX_accuracy_free(ctx);
264 TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
268 void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags)
273 void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data)
276 ctx->serial_cb_data = data;
279 void TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data)
282 ctx->time_cb_data = data;
285 void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx,
286 TS_extension_cb cb, void *data)
288 ctx->extension_cb = cb;
289 ctx->extension_cb_data = data;
292 int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx,
293 int status, const char *text)
295 TS_STATUS_INFO *si = NULL;
296 ASN1_UTF8STRING *utf8_text = NULL;
299 if ((si = TS_STATUS_INFO_new()) == NULL)
301 if (!ASN1_INTEGER_set(si->status, status))
304 if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
305 || !ASN1_STRING_set(utf8_text, text, strlen(text)))
308 && (si->text = sk_ASN1_UTF8STRING_new_null()) == NULL)
310 if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text))
312 utf8_text = NULL; /* Ownership is lost. */
314 if (!TS_RESP_set_status_info(ctx->response, si))
319 TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
320 TS_STATUS_INFO_free(si);
321 ASN1_UTF8STRING_free(utf8_text);
325 int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx,
326 int status, const char *text)
329 TS_STATUS_INFO *si = ctx->response->status_info;
331 if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) {
332 ret = TS_RESP_CTX_set_status_info(ctx, status, text);
337 int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure)
339 TS_STATUS_INFO *si = ctx->response->status_info;
340 if (si->failure_info == NULL
341 && (si->failure_info = ASN1_BIT_STRING_new()) == NULL)
343 if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1))
347 TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO, ERR_R_MALLOC_FAILURE);
351 TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx)
356 TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx)
358 return ctx->tst_info;
361 int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx,
364 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
366 ctx->clock_precision_digits = precision;
370 /* Main entry method of the response generation. */
371 TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio)
377 ts_RESP_CTX_init(ctx);
379 if ((ctx->response = TS_RESP_new()) == NULL) {
380 TSerr(TS_F_TS_RESP_CREATE_RESPONSE, ERR_R_MALLOC_FAILURE);
383 if ((ctx->request = d2i_TS_REQ_bio(req_bio, NULL)) == NULL) {
384 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
385 "Bad request format or system error.");
386 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
389 if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL))
391 if (!ts_RESP_check_request(ctx))
393 if ((policy = ts_RESP_get_policy(ctx)) == NULL)
395 if ((ctx->tst_info = ts_RESP_create_tst_info(ctx, policy)) == NULL)
397 if (!ts_RESP_process_extensions(ctx))
399 if (!ts_RESP_sign(ctx))
405 TSerr(TS_F_TS_RESP_CREATE_RESPONSE, TS_R_RESPONSE_SETUP_ERROR);
406 if (ctx->response != NULL) {
407 if (TS_RESP_CTX_set_status_info_cond(ctx,
409 "Error during response "
410 "generation.") == 0) {
411 TS_RESP_free(ctx->response);
412 ctx->response = NULL;
416 response = ctx->response;
417 ctx->response = NULL; /* Ownership will be returned to caller. */
418 ts_RESP_CTX_cleanup(ctx);
422 /* Initializes the variable part of the context. */
423 static void ts_RESP_CTX_init(TS_RESP_CTX *ctx)
426 ctx->response = NULL;
427 ctx->tst_info = NULL;
430 /* Cleans up the variable part of the context. */
431 static void ts_RESP_CTX_cleanup(TS_RESP_CTX *ctx)
433 TS_REQ_free(ctx->request);
435 TS_RESP_free(ctx->response);
436 ctx->response = NULL;
437 TS_TST_INFO_free(ctx->tst_info);
438 ctx->tst_info = NULL;
441 /* Checks the format and content of the request. */
442 static int ts_RESP_check_request(TS_RESP_CTX *ctx)
444 TS_REQ *request = ctx->request;
445 TS_MSG_IMPRINT *msg_imprint;
448 const ASN1_OCTET_STRING *digest;
449 const EVP_MD *md = NULL;
452 if (TS_REQ_get_version(request) != 1) {
453 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
454 "Bad request version.");
455 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST);
459 msg_imprint = request->msg_imprint;
460 md_alg = msg_imprint->hash_algo;
461 md_alg_id = OBJ_obj2nid(md_alg->algorithm);
462 for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) {
463 const EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i);
464 if (md_alg_id == EVP_MD_type(current_md))
468 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
469 "Message digest algorithm is "
471 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
475 if (md_alg->parameter && ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
476 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
477 "Superfluous message digest "
479 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
482 digest = msg_imprint->hashed_msg;
483 if (digest->length != EVP_MD_size(md)) {
484 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
485 "Bad message digest.");
486 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
493 /* Returns the TSA policy based on the requested and acceptable policies. */
494 static ASN1_OBJECT *ts_RESP_get_policy(TS_RESP_CTX *ctx)
496 ASN1_OBJECT *requested = ctx->request->policy_id;
497 ASN1_OBJECT *policy = NULL;
500 if (ctx->default_policy == NULL) {
501 TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_INVALID_NULL_POINTER);
504 if (!requested || !OBJ_cmp(requested, ctx->default_policy))
505 policy = ctx->default_policy;
507 /* Check if the policy is acceptable. */
508 for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
509 ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
510 if (!OBJ_cmp(requested, current))
513 if (policy == NULL) {
514 TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_UNACCEPTABLE_POLICY);
515 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
516 "Requested policy is not " "supported.");
517 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
522 /* Creates the TS_TST_INFO object based on the settings of the context. */
523 static TS_TST_INFO *ts_RESP_create_tst_info(TS_RESP_CTX *ctx,
527 TS_TST_INFO *tst_info = NULL;
528 ASN1_INTEGER *serial = NULL;
529 ASN1_GENERALIZEDTIME *asn1_time = NULL;
531 TS_ACCURACY *accuracy = NULL;
532 const ASN1_INTEGER *nonce;
533 GENERAL_NAME *tsa_name = NULL;
535 if ((tst_info = TS_TST_INFO_new()) == NULL)
537 if (!TS_TST_INFO_set_version(tst_info, 1))
539 if (!TS_TST_INFO_set_policy_id(tst_info, policy))
541 if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
543 if ((serial = ctx->serial_cb(ctx, ctx->serial_cb_data)) == NULL
544 || !TS_TST_INFO_set_serial(tst_info, serial))
546 if (!ctx->time_cb(ctx, ctx->time_cb_data, &sec, &usec)
548 TS_RESP_set_genTime_with_precision(NULL, sec, usec,
549 ctx->clock_precision_digits)) == NULL
550 || !TS_TST_INFO_set_time(tst_info, asn1_time))
553 if ((ctx->seconds || ctx->millis || ctx->micros)
554 && (accuracy = TS_ACCURACY_new()) == NULL)
556 if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
558 if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
560 if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
562 if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
565 if ((ctx->flags & TS_ORDERING)
566 && !TS_TST_INFO_set_ordering(tst_info, 1))
569 if ((nonce = ctx->request->nonce) != NULL
570 && !TS_TST_INFO_set_nonce(tst_info, nonce))
573 if (ctx->flags & TS_TSA_NAME) {
574 if ((tsa_name = GENERAL_NAME_new()) == NULL)
576 tsa_name->type = GEN_DIRNAME;
578 X509_NAME_dup(X509_get_subject_name(ctx->signer_cert));
579 if (!tsa_name->d.dirn)
581 if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
588 TS_TST_INFO_free(tst_info);
590 TSerr(TS_F_TS_RESP_CREATE_TST_INFO, TS_R_TST_INFO_SETUP_ERROR);
591 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
592 "Error during TSTInfo "
595 GENERAL_NAME_free(tsa_name);
596 TS_ACCURACY_free(accuracy);
597 ASN1_GENERALIZEDTIME_free(asn1_time);
598 ASN1_INTEGER_free(serial);
603 /* Processing the extensions of the request. */
604 static int ts_RESP_process_extensions(TS_RESP_CTX *ctx)
606 STACK_OF(X509_EXTENSION) *exts = ctx->request->extensions;
610 for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
611 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
613 * The last argument was previously (void *)ctx->extension_cb,
614 * but ISO C doesn't permit converting a function pointer to void *.
615 * For lack of better information, I'm placing a NULL there instead.
616 * The callback can pick its own address out from the ctx anyway...
618 ok = (*ctx->extension_cb) (ctx, ext, NULL);
624 /* Functions for signing the TS_TST_INFO structure of the context. */
625 static int ts_RESP_sign(TS_RESP_CTX *ctx)
629 PKCS7_SIGNER_INFO *si;
630 STACK_OF(X509) *certs; /* Certificates to include in sc. */
631 ESS_SIGNING_CERT_V2 *sc2 = NULL;
632 ESS_SIGNING_CERT *sc = NULL;
637 if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
638 TSerr(TS_F_TS_RESP_SIGN, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
642 if ((p7 = PKCS7_new()) == NULL) {
643 TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
646 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
648 if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
651 if (ctx->request->cert_req) {
652 PKCS7_add_certificate(p7, ctx->signer_cert);
654 for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
655 X509 *cert = sk_X509_value(ctx->certs, i);
656 PKCS7_add_certificate(p7, cert);
661 if ((si = PKCS7_add_signature(p7, ctx->signer_cert,
662 ctx->signer_key, ctx->signer_md)) == NULL) {
663 TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
667 oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
668 if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
669 V_ASN1_OBJECT, oid)) {
670 TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
674 certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
675 if (ctx->ess_cert_id_digest == NULL
676 || ctx->ess_cert_id_digest == EVP_sha1()) {
677 if ((sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs, 0)) == NULL)
680 if (!ESS_SIGNING_CERT_add(si, sc)) {
681 TSerr(TS_F_TS_RESP_SIGN, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
685 sc2 = ESS_SIGNING_CERT_V2_new_init(ctx->ess_cert_id_digest,
686 ctx->signer_cert, certs, 0);
690 if (!ESS_SIGNING_CERT_V2_add(si, sc2)) {
691 TSerr(TS_F_TS_RESP_SIGN, TS_R_ESS_ADD_SIGNING_CERT_V2_ERROR);
696 if (!ts_TST_INFO_content_new(p7))
698 if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
699 TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
702 if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
703 TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
706 if (!PKCS7_dataFinal(p7, p7bio)) {
707 TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
710 TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
711 p7 = NULL; /* Ownership is lost. */
712 ctx->tst_info = NULL; /* Ownership is lost. */
717 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
718 "Error during signature "
721 ESS_SIGNING_CERT_V2_free(sc2);
722 ESS_SIGNING_CERT_free(sc);
727 static int ts_TST_INFO_content_new(PKCS7 *p7)
730 ASN1_OCTET_STRING *octet_string = NULL;
732 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
733 if ((ret = PKCS7_new()) == NULL)
735 if ((ret->d.other = ASN1_TYPE_new()) == NULL)
737 ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
738 if ((octet_string = ASN1_OCTET_STRING_new()) == NULL)
740 ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
743 /* Add encapsulated content to signed PKCS7 structure. */
744 if (!PKCS7_set_content(p7, ret))
749 ASN1_OCTET_STRING_free(octet_string);
754 static ASN1_GENERALIZEDTIME *TS_RESP_set_genTime_with_precision(
755 ASN1_GENERALIZEDTIME *asn1_time, long sec, long usec,
758 time_t time_sec = (time_t)sec;
759 struct tm *tm = NULL, tm_result;
760 char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
761 char *p = genTime_str;
762 char *p_end = genTime_str + sizeof(genTime_str);
764 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
767 if ((tm = OPENSSL_gmtime(&time_sec, &tm_result)) == NULL)
771 * Put "genTime_str" in GeneralizedTime format. We work around the
772 * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
773 * NOT include fractional seconds") and OpenSSL related functions to
774 * meet the rfc3161 requirement: "GeneralizedTime syntax can include
775 * fraction-of-second details".
777 p += BIO_snprintf(p, p_end - p,
778 "%04d%02d%02d%02d%02d%02d",
779 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
780 tm->tm_hour, tm->tm_min, tm->tm_sec);
782 BIO_snprintf(p, 2 + precision, ".%06ld", usec);
786 * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
787 * following restrictions for a DER-encoding, which OpenSSL
788 * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
789 * support: "The encoding MUST terminate with a "Z" (which means
790 * "Zulu" time). The decimal point element, if present, MUST be the
791 * point option ".". The fractional-seconds elements, if present,
792 * MUST omit all trailing 0's; if the elements correspond to 0, they
793 * MUST be wholly omitted, and the decimal point element also MUST be
797 * Remove trailing zeros. The dot guarantees the exit condition of
798 * this loop even if all the digits are zero.
808 if (asn1_time == NULL
809 && (asn1_time = ASN1_GENERALIZEDTIME_new()) == NULL)
811 if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
812 ASN1_GENERALIZEDTIME_free(asn1_time);
818 TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION, TS_R_COULD_NOT_SET_TIME);
822 int TS_RESP_CTX_set_ess_cert_id_digest(TS_RESP_CTX *ctx, const EVP_MD *md)
824 ctx->ess_cert_id_digest = md;