2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
19 static ASN1_INTEGER
*def_serial_cb(struct TS_resp_ctx
*, void *);
20 static int def_time_cb(struct TS_resp_ctx
*, void *, long *sec
, long *usec
);
21 static int def_extension_cb(struct TS_resp_ctx
*, X509_EXTENSION
*, void *);
23 static void ts_RESP_CTX_init(TS_RESP_CTX
*ctx
);
24 static void ts_RESP_CTX_cleanup(TS_RESP_CTX
*ctx
);
25 static int ts_RESP_check_request(TS_RESP_CTX
*ctx
);
26 static ASN1_OBJECT
*ts_RESP_get_policy(TS_RESP_CTX
*ctx
);
27 static TS_TST_INFO
*ts_RESP_create_tst_info(TS_RESP_CTX
*ctx
,
29 static int ts_RESP_process_extensions(TS_RESP_CTX
*ctx
);
30 static int ts_RESP_sign(TS_RESP_CTX
*ctx
);
32 static ESS_SIGNING_CERT
*ess_SIGNING_CERT_new_init(X509
*signcert
,
33 STACK_OF(X509
) *certs
);
34 static ESS_CERT_ID
*ess_CERT_ID_new_init(X509
*cert
, int issuer_needed
);
35 static int ts_TST_INFO_content_new(PKCS7
*p7
);
36 static int ess_add_signing_cert(PKCS7_SIGNER_INFO
*si
, ESS_SIGNING_CERT
*sc
);
38 static ESS_SIGNING_CERT_V2
*ess_signing_cert_v2_new_init(const EVP_MD
*hash_alg
,
42 static ESS_CERT_ID_V2
*ess_cert_id_v2_new_init(const EVP_MD
*hash_alg
,
43 X509
*cert
, int issuer_needed
);
44 static int ess_add_signing_cert_v2(PKCS7_SIGNER_INFO
*si
,
45 ESS_SIGNING_CERT_V2
*sc
);
47 static ASN1_GENERALIZEDTIME
48 *TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME
*, long, long,
51 /* Default callback for response generation. */
52 static ASN1_INTEGER
*def_serial_cb(struct TS_resp_ctx
*ctx
, void *data
)
54 ASN1_INTEGER
*serial
= ASN1_INTEGER_new();
58 if (!ASN1_INTEGER_set(serial
, 1))
63 TSerr(TS_F_DEF_SERIAL_CB
, ERR_R_MALLOC_FAILURE
);
64 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
65 "Error during serial number generation.");
69 #if defined(OPENSSL_SYS_UNIX)
71 static int def_time_cb(struct TS_resp_ctx
*ctx
, void *data
,
72 long *sec
, long *usec
)
75 if (gettimeofday(&tv
, NULL
) != 0) {
76 TSerr(TS_F_DEF_TIME_CB
, TS_R_TIME_SYSCALL_ERROR
);
77 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
78 "Time is not available.");
79 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_TIME_NOT_AVAILABLE
);
90 static int def_time_cb(struct TS_resp_ctx
*ctx
, void *data
,
91 long *sec
, long *usec
)
94 if (time(&t
) == (time_t)-1) {
95 TSerr(TS_F_DEF_TIME_CB
, TS_R_TIME_SYSCALL_ERROR
);
96 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
97 "Time is not available.");
98 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_TIME_NOT_AVAILABLE
);
109 static int def_extension_cb(struct TS_resp_ctx
*ctx
, X509_EXTENSION
*ext
,
112 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
113 "Unsupported extension.");
114 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_UNACCEPTED_EXTENSION
);
118 /* TS_RESP_CTX management functions. */
120 TS_RESP_CTX
*TS_RESP_CTX_new(void)
124 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
125 TSerr(TS_F_TS_RESP_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
129 ctx
->signer_md
= EVP_sha256();
131 ctx
->serial_cb
= def_serial_cb
;
132 ctx
->time_cb
= def_time_cb
;
133 ctx
->extension_cb
= def_extension_cb
;
138 void TS_RESP_CTX_free(TS_RESP_CTX
*ctx
)
143 X509_free(ctx
->signer_cert
);
144 EVP_PKEY_free(ctx
->signer_key
);
145 sk_X509_pop_free(ctx
->certs
, X509_free
);
146 sk_ASN1_OBJECT_pop_free(ctx
->policies
, ASN1_OBJECT_free
);
147 ASN1_OBJECT_free(ctx
->default_policy
);
148 sk_EVP_MD_free(ctx
->mds
); /* No EVP_MD_free method exists. */
149 ASN1_INTEGER_free(ctx
->seconds
);
150 ASN1_INTEGER_free(ctx
->millis
);
151 ASN1_INTEGER_free(ctx
->micros
);
155 int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX
*ctx
, X509
*signer
)
157 if (X509_check_purpose(signer
, X509_PURPOSE_TIMESTAMP_SIGN
, 0) != 1) {
158 TSerr(TS_F_TS_RESP_CTX_SET_SIGNER_CERT
,
159 TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE
);
162 X509_free(ctx
->signer_cert
);
163 ctx
->signer_cert
= signer
;
164 X509_up_ref(ctx
->signer_cert
);
168 int TS_RESP_CTX_set_signer_key(TS_RESP_CTX
*ctx
, EVP_PKEY
*key
)
170 EVP_PKEY_free(ctx
->signer_key
);
171 ctx
->signer_key
= key
;
172 EVP_PKEY_up_ref(ctx
->signer_key
);
177 int TS_RESP_CTX_set_signer_digest(TS_RESP_CTX
*ctx
, const EVP_MD
*md
)
183 int TS_RESP_CTX_set_def_policy(TS_RESP_CTX
*ctx
, const ASN1_OBJECT
*def_policy
)
185 ASN1_OBJECT_free(ctx
->default_policy
);
186 if ((ctx
->default_policy
= OBJ_dup(def_policy
)) == NULL
)
190 TSerr(TS_F_TS_RESP_CTX_SET_DEF_POLICY
, ERR_R_MALLOC_FAILURE
);
194 int TS_RESP_CTX_set_certs(TS_RESP_CTX
*ctx
, STACK_OF(X509
) *certs
)
197 sk_X509_pop_free(ctx
->certs
, X509_free
);
201 if ((ctx
->certs
= X509_chain_up_ref(certs
)) == NULL
) {
202 TSerr(TS_F_TS_RESP_CTX_SET_CERTS
, ERR_R_MALLOC_FAILURE
);
209 int TS_RESP_CTX_add_policy(TS_RESP_CTX
*ctx
, const ASN1_OBJECT
*policy
)
211 ASN1_OBJECT
*copy
= NULL
;
213 if (ctx
->policies
== NULL
214 && (ctx
->policies
= sk_ASN1_OBJECT_new_null()) == NULL
)
216 if ((copy
= OBJ_dup(policy
)) == NULL
)
218 if (!sk_ASN1_OBJECT_push(ctx
->policies
, copy
))
223 TSerr(TS_F_TS_RESP_CTX_ADD_POLICY
, ERR_R_MALLOC_FAILURE
);
224 ASN1_OBJECT_free(copy
);
228 int TS_RESP_CTX_add_md(TS_RESP_CTX
*ctx
, const EVP_MD
*md
)
231 && (ctx
->mds
= sk_EVP_MD_new_null()) == NULL
)
233 if (!sk_EVP_MD_push(ctx
->mds
, md
))
238 TSerr(TS_F_TS_RESP_CTX_ADD_MD
, ERR_R_MALLOC_FAILURE
);
242 #define TS_RESP_CTX_accuracy_free(ctx) \
243 ASN1_INTEGER_free(ctx->seconds); \
244 ctx->seconds = NULL; \
245 ASN1_INTEGER_free(ctx->millis); \
246 ctx->millis = NULL; \
247 ASN1_INTEGER_free(ctx->micros); \
250 int TS_RESP_CTX_set_accuracy(TS_RESP_CTX
*ctx
,
251 int secs
, int millis
, int micros
)
254 TS_RESP_CTX_accuracy_free(ctx
);
256 && ((ctx
->seconds
= ASN1_INTEGER_new()) == NULL
257 || !ASN1_INTEGER_set(ctx
->seconds
, secs
)))
260 && ((ctx
->millis
= ASN1_INTEGER_new()) == NULL
261 || !ASN1_INTEGER_set(ctx
->millis
, millis
)))
264 && ((ctx
->micros
= ASN1_INTEGER_new()) == NULL
265 || !ASN1_INTEGER_set(ctx
->micros
, micros
)))
270 TS_RESP_CTX_accuracy_free(ctx
);
271 TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY
, ERR_R_MALLOC_FAILURE
);
275 void TS_RESP_CTX_add_flags(TS_RESP_CTX
*ctx
, int flags
)
280 void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX
*ctx
, TS_serial_cb cb
, void *data
)
283 ctx
->serial_cb_data
= data
;
286 void TS_RESP_CTX_set_time_cb(TS_RESP_CTX
*ctx
, TS_time_cb cb
, void *data
)
289 ctx
->time_cb_data
= data
;
292 void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX
*ctx
,
293 TS_extension_cb cb
, void *data
)
295 ctx
->extension_cb
= cb
;
296 ctx
->extension_cb_data
= data
;
299 int TS_RESP_CTX_set_status_info(TS_RESP_CTX
*ctx
,
300 int status
, const char *text
)
302 TS_STATUS_INFO
*si
= NULL
;
303 ASN1_UTF8STRING
*utf8_text
= NULL
;
306 if ((si
= TS_STATUS_INFO_new()) == NULL
)
308 if (!ASN1_INTEGER_set(si
->status
, status
))
311 if ((utf8_text
= ASN1_UTF8STRING_new()) == NULL
312 || !ASN1_STRING_set(utf8_text
, text
, strlen(text
)))
315 && (si
->text
= sk_ASN1_UTF8STRING_new_null()) == NULL
)
317 if (!sk_ASN1_UTF8STRING_push(si
->text
, utf8_text
))
319 utf8_text
= NULL
; /* Ownership is lost. */
321 if (!TS_RESP_set_status_info(ctx
->response
, si
))
326 TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO
, ERR_R_MALLOC_FAILURE
);
327 TS_STATUS_INFO_free(si
);
328 ASN1_UTF8STRING_free(utf8_text
);
332 int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX
*ctx
,
333 int status
, const char *text
)
336 TS_STATUS_INFO
*si
= ctx
->response
->status_info
;
338 if (ASN1_INTEGER_get(si
->status
) == TS_STATUS_GRANTED
) {
339 ret
= TS_RESP_CTX_set_status_info(ctx
, status
, text
);
344 int TS_RESP_CTX_add_failure_info(TS_RESP_CTX
*ctx
, int failure
)
346 TS_STATUS_INFO
*si
= ctx
->response
->status_info
;
347 if (si
->failure_info
== NULL
348 && (si
->failure_info
= ASN1_BIT_STRING_new()) == NULL
)
350 if (!ASN1_BIT_STRING_set_bit(si
->failure_info
, failure
, 1))
354 TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO
, ERR_R_MALLOC_FAILURE
);
358 TS_REQ
*TS_RESP_CTX_get_request(TS_RESP_CTX
*ctx
)
363 TS_TST_INFO
*TS_RESP_CTX_get_tst_info(TS_RESP_CTX
*ctx
)
365 return ctx
->tst_info
;
368 int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX
*ctx
,
371 if (precision
> TS_MAX_CLOCK_PRECISION_DIGITS
)
373 ctx
->clock_precision_digits
= precision
;
377 /* Main entry method of the response generation. */
378 TS_RESP
*TS_RESP_create_response(TS_RESP_CTX
*ctx
, BIO
*req_bio
)
384 ts_RESP_CTX_init(ctx
);
386 if ((ctx
->response
= TS_RESP_new()) == NULL
) {
387 TSerr(TS_F_TS_RESP_CREATE_RESPONSE
, ERR_R_MALLOC_FAILURE
);
390 if ((ctx
->request
= d2i_TS_REQ_bio(req_bio
, NULL
)) == NULL
) {
391 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
392 "Bad request format or system error.");
393 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_DATA_FORMAT
);
396 if (!TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_GRANTED
, NULL
))
398 if (!ts_RESP_check_request(ctx
))
400 if ((policy
= ts_RESP_get_policy(ctx
)) == NULL
)
402 if ((ctx
->tst_info
= ts_RESP_create_tst_info(ctx
, policy
)) == NULL
)
404 if (!ts_RESP_process_extensions(ctx
))
406 if (!ts_RESP_sign(ctx
))
412 TSerr(TS_F_TS_RESP_CREATE_RESPONSE
, TS_R_RESPONSE_SETUP_ERROR
);
413 if (ctx
->response
!= NULL
) {
414 if (TS_RESP_CTX_set_status_info_cond(ctx
,
416 "Error during response "
417 "generation.") == 0) {
418 TS_RESP_free(ctx
->response
);
419 ctx
->response
= NULL
;
423 response
= ctx
->response
;
424 ctx
->response
= NULL
; /* Ownership will be returned to caller. */
425 ts_RESP_CTX_cleanup(ctx
);
429 /* Initializes the variable part of the context. */
430 static void ts_RESP_CTX_init(TS_RESP_CTX
*ctx
)
433 ctx
->response
= NULL
;
434 ctx
->tst_info
= NULL
;
437 /* Cleans up the variable part of the context. */
438 static void ts_RESP_CTX_cleanup(TS_RESP_CTX
*ctx
)
440 TS_REQ_free(ctx
->request
);
442 TS_RESP_free(ctx
->response
);
443 ctx
->response
= NULL
;
444 TS_TST_INFO_free(ctx
->tst_info
);
445 ctx
->tst_info
= NULL
;
448 /* Checks the format and content of the request. */
449 static int ts_RESP_check_request(TS_RESP_CTX
*ctx
)
451 TS_REQ
*request
= ctx
->request
;
452 TS_MSG_IMPRINT
*msg_imprint
;
455 const ASN1_OCTET_STRING
*digest
;
456 const EVP_MD
*md
= NULL
;
459 if (TS_REQ_get_version(request
) != 1) {
460 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
461 "Bad request version.");
462 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_REQUEST
);
466 msg_imprint
= request
->msg_imprint
;
467 md_alg
= msg_imprint
->hash_algo
;
468 md_alg_id
= OBJ_obj2nid(md_alg
->algorithm
);
469 for (i
= 0; !md
&& i
< sk_EVP_MD_num(ctx
->mds
); ++i
) {
470 const EVP_MD
*current_md
= sk_EVP_MD_value(ctx
->mds
, i
);
471 if (md_alg_id
== EVP_MD_type(current_md
))
475 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
476 "Message digest algorithm is "
478 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_ALG
);
482 if (md_alg
->parameter
&& ASN1_TYPE_get(md_alg
->parameter
) != V_ASN1_NULL
) {
483 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
484 "Superfluous message digest "
486 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_ALG
);
489 digest
= msg_imprint
->hashed_msg
;
490 if (digest
->length
!= EVP_MD_size(md
)) {
491 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
492 "Bad message digest.");
493 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_DATA_FORMAT
);
500 /* Returns the TSA policy based on the requested and acceptable policies. */
501 static ASN1_OBJECT
*ts_RESP_get_policy(TS_RESP_CTX
*ctx
)
503 ASN1_OBJECT
*requested
= ctx
->request
->policy_id
;
504 ASN1_OBJECT
*policy
= NULL
;
507 if (ctx
->default_policy
== NULL
) {
508 TSerr(TS_F_TS_RESP_GET_POLICY
, TS_R_INVALID_NULL_POINTER
);
511 if (!requested
|| !OBJ_cmp(requested
, ctx
->default_policy
))
512 policy
= ctx
->default_policy
;
514 /* Check if the policy is acceptable. */
515 for (i
= 0; !policy
&& i
< sk_ASN1_OBJECT_num(ctx
->policies
); ++i
) {
516 ASN1_OBJECT
*current
= sk_ASN1_OBJECT_value(ctx
->policies
, i
);
517 if (!OBJ_cmp(requested
, current
))
521 TSerr(TS_F_TS_RESP_GET_POLICY
, TS_R_UNACCEPTABLE_POLICY
);
522 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
523 "Requested policy is not " "supported.");
524 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_UNACCEPTED_POLICY
);
529 /* Creates the TS_TST_INFO object based on the settings of the context. */
530 static TS_TST_INFO
*ts_RESP_create_tst_info(TS_RESP_CTX
*ctx
,
534 TS_TST_INFO
*tst_info
= NULL
;
535 ASN1_INTEGER
*serial
= NULL
;
536 ASN1_GENERALIZEDTIME
*asn1_time
= NULL
;
538 TS_ACCURACY
*accuracy
= NULL
;
539 const ASN1_INTEGER
*nonce
;
540 GENERAL_NAME
*tsa_name
= NULL
;
542 if ((tst_info
= TS_TST_INFO_new()) == NULL
)
544 if (!TS_TST_INFO_set_version(tst_info
, 1))
546 if (!TS_TST_INFO_set_policy_id(tst_info
, policy
))
548 if (!TS_TST_INFO_set_msg_imprint(tst_info
, ctx
->request
->msg_imprint
))
550 if ((serial
= ctx
->serial_cb(ctx
, ctx
->serial_cb_data
)) == NULL
551 || !TS_TST_INFO_set_serial(tst_info
, serial
))
553 if (!ctx
->time_cb(ctx
, ctx
->time_cb_data
, &sec
, &usec
)
555 TS_RESP_set_genTime_with_precision(NULL
, sec
, usec
,
556 ctx
->clock_precision_digits
)) == NULL
557 || !TS_TST_INFO_set_time(tst_info
, asn1_time
))
560 if ((ctx
->seconds
|| ctx
->millis
|| ctx
->micros
)
561 && (accuracy
= TS_ACCURACY_new()) == NULL
)
563 if (ctx
->seconds
&& !TS_ACCURACY_set_seconds(accuracy
, ctx
->seconds
))
565 if (ctx
->millis
&& !TS_ACCURACY_set_millis(accuracy
, ctx
->millis
))
567 if (ctx
->micros
&& !TS_ACCURACY_set_micros(accuracy
, ctx
->micros
))
569 if (accuracy
&& !TS_TST_INFO_set_accuracy(tst_info
, accuracy
))
572 if ((ctx
->flags
& TS_ORDERING
)
573 && !TS_TST_INFO_set_ordering(tst_info
, 1))
576 if ((nonce
= ctx
->request
->nonce
) != NULL
577 && !TS_TST_INFO_set_nonce(tst_info
, nonce
))
580 if (ctx
->flags
& TS_TSA_NAME
) {
581 if ((tsa_name
= GENERAL_NAME_new()) == NULL
)
583 tsa_name
->type
= GEN_DIRNAME
;
585 X509_NAME_dup(X509_get_subject_name(ctx
->signer_cert
));
586 if (!tsa_name
->d
.dirn
)
588 if (!TS_TST_INFO_set_tsa(tst_info
, tsa_name
))
595 TS_TST_INFO_free(tst_info
);
597 TSerr(TS_F_TS_RESP_CREATE_TST_INFO
, TS_R_TST_INFO_SETUP_ERROR
);
598 TS_RESP_CTX_set_status_info_cond(ctx
, TS_STATUS_REJECTION
,
599 "Error during TSTInfo "
602 GENERAL_NAME_free(tsa_name
);
603 TS_ACCURACY_free(accuracy
);
604 ASN1_GENERALIZEDTIME_free(asn1_time
);
605 ASN1_INTEGER_free(serial
);
610 /* Processing the extensions of the request. */
611 static int ts_RESP_process_extensions(TS_RESP_CTX
*ctx
)
613 STACK_OF(X509_EXTENSION
) *exts
= ctx
->request
->extensions
;
617 for (i
= 0; ok
&& i
< sk_X509_EXTENSION_num(exts
); ++i
) {
618 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
620 * The last argument was previously (void *)ctx->extension_cb,
621 * but ISO C doesn't permit converting a function pointer to void *.
622 * For lack of better information, I'm placing a NULL there instead.
623 * The callback can pick its own address out from the ctx anyway...
625 ok
= (*ctx
->extension_cb
) (ctx
, ext
, NULL
);
631 /* Functions for signing the TS_TST_INFO structure of the context. */
632 static int ts_RESP_sign(TS_RESP_CTX
*ctx
)
636 PKCS7_SIGNER_INFO
*si
;
637 STACK_OF(X509
) *certs
; /* Certificates to include in sc. */
638 ESS_SIGNING_CERT_V2
*sc2
= NULL
;
639 ESS_SIGNING_CERT
*sc
= NULL
;
644 if (!X509_check_private_key(ctx
->signer_cert
, ctx
->signer_key
)) {
645 TSerr(TS_F_TS_RESP_SIGN
, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
649 if ((p7
= PKCS7_new()) == NULL
) {
650 TSerr(TS_F_TS_RESP_SIGN
, ERR_R_MALLOC_FAILURE
);
653 if (!PKCS7_set_type(p7
, NID_pkcs7_signed
))
655 if (!ASN1_INTEGER_set(p7
->d
.sign
->version
, 3))
658 if (ctx
->request
->cert_req
) {
659 PKCS7_add_certificate(p7
, ctx
->signer_cert
);
661 for (i
= 0; i
< sk_X509_num(ctx
->certs
); ++i
) {
662 X509
*cert
= sk_X509_value(ctx
->certs
, i
);
663 PKCS7_add_certificate(p7
, cert
);
668 if ((si
= PKCS7_add_signature(p7
, ctx
->signer_cert
,
669 ctx
->signer_key
, ctx
->signer_md
)) == NULL
) {
670 TSerr(TS_F_TS_RESP_SIGN
, TS_R_PKCS7_ADD_SIGNATURE_ERROR
);
674 oid
= OBJ_nid2obj(NID_id_smime_ct_TSTInfo
);
675 if (!PKCS7_add_signed_attribute(si
, NID_pkcs9_contentType
,
676 V_ASN1_OBJECT
, oid
)) {
677 TSerr(TS_F_TS_RESP_SIGN
, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR
);
681 certs
= ctx
->flags
& TS_ESS_CERT_ID_CHAIN
? ctx
->certs
: NULL
;
682 if (ctx
->ess_cert_id_digest
== NULL
683 || ctx
->ess_cert_id_digest
== EVP_sha1()) {
684 if ((sc
= ess_SIGNING_CERT_new_init(ctx
->signer_cert
, certs
)) == NULL
)
687 if (!ess_add_signing_cert(si
, sc
)) {
688 TSerr(TS_F_TS_RESP_SIGN
, TS_R_ESS_ADD_SIGNING_CERT_ERROR
);
692 sc2
= ess_signing_cert_v2_new_init(ctx
->ess_cert_id_digest
,
693 ctx
->signer_cert
, certs
);
697 if (!ess_add_signing_cert_v2(si
, sc2
)) {
698 TSerr(TS_F_TS_RESP_SIGN
, TS_R_ESS_ADD_SIGNING_CERT_V2_ERROR
);
703 if (!ts_TST_INFO_content_new(p7
))
705 if ((p7bio
= PKCS7_dataInit(p7
, NULL
)) == NULL
) {
706 TSerr(TS_F_TS_RESP_SIGN
, ERR_R_MALLOC_FAILURE
);
709 if (!i2d_TS_TST_INFO_bio(p7bio
, ctx
->tst_info
)) {
710 TSerr(TS_F_TS_RESP_SIGN
, TS_R_TS_DATASIGN
);
713 if (!PKCS7_dataFinal(p7
, p7bio
)) {
714 TSerr(TS_F_TS_RESP_SIGN
, TS_R_TS_DATASIGN
);
717 TS_RESP_set_tst_info(ctx
->response
, p7
, ctx
->tst_info
);
718 p7
= NULL
; /* Ownership is lost. */
719 ctx
->tst_info
= NULL
; /* Ownership is lost. */
724 TS_RESP_CTX_set_status_info_cond(ctx
, TS_STATUS_REJECTION
,
725 "Error during signature "
728 ESS_SIGNING_CERT_V2_free(sc2
);
729 ESS_SIGNING_CERT_free(sc
);
734 static ESS_SIGNING_CERT
*ess_SIGNING_CERT_new_init(X509
*signcert
,
735 STACK_OF(X509
) *certs
)
738 ESS_SIGNING_CERT
*sc
= NULL
;
741 if ((sc
= ESS_SIGNING_CERT_new()) == NULL
)
743 if (sc
->cert_ids
== NULL
744 && (sc
->cert_ids
= sk_ESS_CERT_ID_new_null()) == NULL
)
747 if ((cid
= ess_CERT_ID_new_init(signcert
, 0)) == NULL
748 || !sk_ESS_CERT_ID_push(sc
->cert_ids
, cid
))
750 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
751 X509
*cert
= sk_X509_value(certs
, i
);
752 if ((cid
= ess_CERT_ID_new_init(cert
, 1)) == NULL
753 || !sk_ESS_CERT_ID_push(sc
->cert_ids
, cid
))
759 ESS_SIGNING_CERT_free(sc
);
760 TSerr(TS_F_ESS_SIGNING_CERT_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
764 static ESS_CERT_ID
*ess_CERT_ID_new_init(X509
*cert
, int issuer_needed
)
766 ESS_CERT_ID
*cid
= NULL
;
767 GENERAL_NAME
*name
= NULL
;
768 unsigned char cert_sha1
[SHA_DIGEST_LENGTH
];
770 /* Call for side-effect of computing hash and caching extensions */
771 X509_check_purpose(cert
, -1, 0);
772 if ((cid
= ESS_CERT_ID_new()) == NULL
)
774 X509_digest(cert
, EVP_sha1(), cert_sha1
, NULL
);
775 if (!ASN1_OCTET_STRING_set(cid
->hash
, cert_sha1
, SHA_DIGEST_LENGTH
))
778 /* Setting the issuer/serial if requested. */
780 if (cid
->issuer_serial
== NULL
781 && (cid
->issuer_serial
= ESS_ISSUER_SERIAL_new()) == NULL
)
783 if ((name
= GENERAL_NAME_new()) == NULL
)
785 name
->type
= GEN_DIRNAME
;
786 if ((name
->d
.dirn
= X509_NAME_dup(X509_get_issuer_name(cert
))) == NULL
)
788 if (!sk_GENERAL_NAME_push(cid
->issuer_serial
->issuer
, name
))
790 name
= NULL
; /* Ownership is lost. */
791 ASN1_INTEGER_free(cid
->issuer_serial
->serial
);
792 if (!(cid
->issuer_serial
->serial
=
793 ASN1_INTEGER_dup(X509_get_serialNumber(cert
))))
799 GENERAL_NAME_free(name
);
800 ESS_CERT_ID_free(cid
);
801 TSerr(TS_F_ESS_CERT_ID_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
805 static int ts_TST_INFO_content_new(PKCS7
*p7
)
808 ASN1_OCTET_STRING
*octet_string
= NULL
;
810 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
811 if ((ret
= PKCS7_new()) == NULL
)
813 if ((ret
->d
.other
= ASN1_TYPE_new()) == NULL
)
815 ret
->type
= OBJ_nid2obj(NID_id_smime_ct_TSTInfo
);
816 if ((octet_string
= ASN1_OCTET_STRING_new()) == NULL
)
818 ASN1_TYPE_set(ret
->d
.other
, V_ASN1_OCTET_STRING
, octet_string
);
821 /* Add encapsulated content to signed PKCS7 structure. */
822 if (!PKCS7_set_content(p7
, ret
))
827 ASN1_OCTET_STRING_free(octet_string
);
832 static int ess_add_signing_cert(PKCS7_SIGNER_INFO
*si
, ESS_SIGNING_CERT
*sc
)
834 ASN1_STRING
*seq
= NULL
;
835 unsigned char *p
, *pp
= NULL
;
838 len
= i2d_ESS_SIGNING_CERT(sc
, NULL
);
839 if ((pp
= OPENSSL_malloc(len
)) == NULL
) {
840 TSerr(TS_F_ESS_ADD_SIGNING_CERT
, ERR_R_MALLOC_FAILURE
);
844 i2d_ESS_SIGNING_CERT(sc
, &p
);
845 if ((seq
= ASN1_STRING_new()) == NULL
|| !ASN1_STRING_set(seq
, pp
, len
)) {
846 TSerr(TS_F_ESS_ADD_SIGNING_CERT
, ERR_R_MALLOC_FAILURE
);
851 return PKCS7_add_signed_attribute(si
,
852 NID_id_smime_aa_signingCertificate
,
853 V_ASN1_SEQUENCE
, seq
);
855 ASN1_STRING_free(seq
);
861 static ESS_SIGNING_CERT_V2
*ess_signing_cert_v2_new_init(const EVP_MD
*hash_alg
,
863 STACK_OF(X509
) *certs
)
865 ESS_CERT_ID_V2
*cid
= NULL
;
866 ESS_SIGNING_CERT_V2
*sc
= NULL
;
869 if ((sc
= ESS_SIGNING_CERT_V2_new()) == NULL
)
871 if ((cid
= ess_cert_id_v2_new_init(hash_alg
, signcert
, 0)) == NULL
)
873 if (!sk_ESS_CERT_ID_V2_push(sc
->cert_ids
, cid
))
877 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
878 X509
*cert
= sk_X509_value(certs
, i
);
880 if ((cid
= ess_cert_id_v2_new_init(hash_alg
, cert
, 1)) == NULL
)
882 if (!sk_ESS_CERT_ID_V2_push(sc
->cert_ids
, cid
))
889 ESS_SIGNING_CERT_V2_free(sc
);
890 ESS_CERT_ID_V2_free(cid
);
891 TSerr(TS_F_ESS_SIGNING_CERT_V2_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
895 static ESS_CERT_ID_V2
*ess_cert_id_v2_new_init(const EVP_MD
*hash_alg
,
896 X509
*cert
, int issuer_needed
)
898 ESS_CERT_ID_V2
*cid
= NULL
;
899 GENERAL_NAME
*name
= NULL
;
900 unsigned char hash
[EVP_MAX_MD_SIZE
];
901 unsigned int hash_len
= sizeof(hash
);
902 X509_ALGOR
*alg
= NULL
;
904 memset(hash
, 0, sizeof(hash
));
906 if ((cid
= ESS_CERT_ID_V2_new()) == NULL
)
909 if (hash_alg
!= EVP_sha256()) {
910 alg
= X509_ALGOR_new();
913 X509_ALGOR_set_md(alg
, hash_alg
);
914 if (alg
->algorithm
== NULL
)
919 cid
->hash_alg
= NULL
;
922 if (!X509_digest(cert
, hash_alg
, hash
, &hash_len
))
925 if (!ASN1_OCTET_STRING_set(cid
->hash
, hash
, hash_len
))
929 if ((cid
->issuer_serial
= ESS_ISSUER_SERIAL_new()) == NULL
)
931 if ((name
= GENERAL_NAME_new()) == NULL
)
933 name
->type
= GEN_DIRNAME
;
934 if ((name
->d
.dirn
= X509_NAME_dup(X509_get_issuer_name(cert
))) == NULL
)
936 if (!sk_GENERAL_NAME_push(cid
->issuer_serial
->issuer
, name
))
938 name
= NULL
; /* Ownership is lost. */
939 ASN1_INTEGER_free(cid
->issuer_serial
->serial
);
940 cid
->issuer_serial
->serial
=
941 ASN1_INTEGER_dup(X509_get_serialNumber(cert
));
942 if (cid
->issuer_serial
->serial
== NULL
)
948 X509_ALGOR_free(alg
);
949 GENERAL_NAME_free(name
);
950 ESS_CERT_ID_V2_free(cid
);
951 TSerr(TS_F_ESS_CERT_ID_V2_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
955 static int ess_add_signing_cert_v2(PKCS7_SIGNER_INFO
*si
,
956 ESS_SIGNING_CERT_V2
*sc
)
958 ASN1_STRING
*seq
= NULL
;
959 unsigned char *p
, *pp
= NULL
;
960 int len
= i2d_ESS_SIGNING_CERT_V2(sc
, NULL
);
962 if ((pp
= OPENSSL_malloc(len
)) == NULL
) {
963 TSerr(TS_F_ESS_ADD_SIGNING_CERT_V2
, ERR_R_MALLOC_FAILURE
);
968 i2d_ESS_SIGNING_CERT_V2(sc
, &p
);
969 if ((seq
= ASN1_STRING_new()) == NULL
|| !ASN1_STRING_set(seq
, pp
, len
)) {
970 TSerr(TS_F_ESS_ADD_SIGNING_CERT_V2
, ERR_R_MALLOC_FAILURE
);
976 return PKCS7_add_signed_attribute(si
,
977 NID_id_smime_aa_signingCertificateV2
,
978 V_ASN1_SEQUENCE
, seq
);
980 ASN1_STRING_free(seq
);
985 static ASN1_GENERALIZEDTIME
*TS_RESP_set_genTime_with_precision(
986 ASN1_GENERALIZEDTIME
*asn1_time
, long sec
, long usec
,
989 time_t time_sec
= (time_t)sec
;
990 struct tm
*tm
= NULL
, tm_result
;
991 char genTime_str
[17 + TS_MAX_CLOCK_PRECISION_DIGITS
];
992 char *p
= genTime_str
;
993 char *p_end
= genTime_str
+ sizeof(genTime_str
);
995 if (precision
> TS_MAX_CLOCK_PRECISION_DIGITS
)
998 if ((tm
= OPENSSL_gmtime(&time_sec
, &tm_result
)) == NULL
)
1002 * Put "genTime_str" in GeneralizedTime format. We work around the
1003 * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
1004 * NOT include fractional seconds") and OpenSSL related functions to
1005 * meet the rfc3161 requirement: "GeneralizedTime syntax can include
1006 * fraction-of-second details".
1008 p
+= BIO_snprintf(p
, p_end
- p
,
1009 "%04d%02d%02d%02d%02d%02d",
1010 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
1011 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1012 if (precision
> 0) {
1013 BIO_snprintf(p
, 2 + precision
, ".%06ld", usec
);
1017 * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
1018 * following restrictions for a DER-encoding, which OpenSSL
1019 * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
1020 * support: "The encoding MUST terminate with a "Z" (which means
1021 * "Zulu" time). The decimal point element, if present, MUST be the
1022 * point option ".". The fractional-seconds elements, if present,
1023 * MUST omit all trailing 0's; if the elements correspond to 0, they
1024 * MUST be wholly omitted, and the decimal point element also MUST be
1028 * Remove trailing zeros. The dot guarantees the exit condition of
1029 * this loop even if all the digits are zero.
1039 if (asn1_time
== NULL
1040 && (asn1_time
= ASN1_GENERALIZEDTIME_new()) == NULL
)
1042 if (!ASN1_GENERALIZEDTIME_set_string(asn1_time
, genTime_str
)) {
1043 ASN1_GENERALIZEDTIME_free(asn1_time
);
1049 TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION
, TS_R_COULD_NOT_SET_TIME
);
1053 int TS_RESP_CTX_set_ess_cert_id_digest(TS_RESP_CTX
*ctx
, const EVP_MD
*md
)
1055 ctx
->ess_cert_id_digest
= md
;