1 /* crypto/ts/ts_resp_sign.c */
2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #if defined(OPENSSL_SYS_UNIX)
65 #include <openssl/objects.h>
66 #include <openssl/ts.h>
67 #include <openssl/pkcs7.h>
69 /* Private function declarations. */
71 static ASN1_INTEGER
*def_serial_cb(struct TS_resp_ctx
*, void *);
72 static int def_time_cb(struct TS_resp_ctx
*, void *, long *sec
, long *usec
);
73 static int def_extension_cb(struct TS_resp_ctx
*, X509_EXTENSION
*, void *);
75 static void TS_RESP_CTX_init(TS_RESP_CTX
*ctx
);
76 static void TS_RESP_CTX_cleanup(TS_RESP_CTX
*ctx
);
77 static int TS_RESP_check_request(TS_RESP_CTX
*ctx
);
78 static ASN1_OBJECT
*TS_RESP_get_policy(TS_RESP_CTX
*ctx
);
79 static TS_TST_INFO
*TS_RESP_create_tst_info(TS_RESP_CTX
*ctx
,
81 static int TS_RESP_process_extensions(TS_RESP_CTX
*ctx
);
82 static int TS_RESP_sign(TS_RESP_CTX
*ctx
);
84 static ESS_SIGNING_CERT
*ESS_SIGNING_CERT_new_init(X509
*signcert
,
85 STACK_OF(X509
) *certs
);
86 static ESS_CERT_ID
*ESS_CERT_ID_new_init(X509
*cert
, int issuer_needed
);
87 static int TS_TST_INFO_content_new(PKCS7
*p7
);
88 static int ESS_add_signing_cert(PKCS7_SIGNER_INFO
*si
, ESS_SIGNING_CERT
*sc
);
90 static ASN1_GENERALIZEDTIME
*TS_RESP_set_genTime_with_precision(
91 ASN1_GENERALIZEDTIME
*, long, long, unsigned);
93 /* Default callbacks for response generation. */
95 static ASN1_INTEGER
*def_serial_cb(struct TS_resp_ctx
*ctx
, void *data
)
97 ASN1_INTEGER
*serial
= ASN1_INTEGER_new();
98 if (!serial
) goto err
;
99 if (!ASN1_INTEGER_set(serial
, 1)) goto err
;
102 TSerr(TS_F_DEF_SERIAL_CB
, ERR_R_MALLOC_FAILURE
);
103 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
104 "Error during serial number generation.");
108 #if defined(OPENSSL_SYS_UNIX)
110 /* Use the gettimeofday function call. */
111 static int def_time_cb(struct TS_resp_ctx
*ctx
, void *data
,
112 long *sec
, long *usec
)
115 if (gettimeofday(&tv
, NULL
) != 0)
117 TSerr(TS_F_DEF_TIME_CB
, TS_R_TIME_SYSCALL_ERROR
);
118 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
119 "Time is not available.");
120 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_TIME_NOT_AVAILABLE
);
123 /* Return time to caller. */
132 /* Use the time function call that provides only seconds precision. */
133 static int def_time_cb(struct TS_resp_ctx
*ctx
, void *data
,
134 long *sec
, long *usec
)
137 if (time(&t
) == (time_t) -1)
139 TSerr(TS_F_DEF_TIME_CB
, TS_R_TIME_SYSCALL_ERROR
);
140 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
141 "Time is not available.");
142 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_TIME_NOT_AVAILABLE
);
145 /* Return time to caller, only second precision. */
154 static int def_extension_cb(struct TS_resp_ctx
*ctx
, X509_EXTENSION
*ext
,
157 /* No extensions are processed here. */
158 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
159 "Unsupported extension.");
160 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_UNACCEPTED_EXTENSION
);
164 /* TS_RESP_CTX management functions. */
166 TS_RESP_CTX
*TS_RESP_CTX_new()
170 if (!(ctx
= (TS_RESP_CTX
*) OPENSSL_malloc(sizeof(TS_RESP_CTX
))))
172 TSerr(TS_F_TS_RESP_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
175 memset(ctx
, 0, sizeof(TS_RESP_CTX
));
177 /* Setting default callbacks. */
178 ctx
->serial_cb
= def_serial_cb
;
179 ctx
->time_cb
= def_time_cb
;
180 ctx
->extension_cb
= def_extension_cb
;
185 void TS_RESP_CTX_free(TS_RESP_CTX
*ctx
)
189 X509_free(ctx
->signer_cert
);
190 EVP_PKEY_free(ctx
->signer_key
);
191 sk_X509_pop_free(ctx
->certs
, X509_free
);
192 sk_ASN1_OBJECT_pop_free(ctx
->policies
, ASN1_OBJECT_free
);
193 ASN1_OBJECT_free(ctx
->default_policy
);
194 sk_EVP_MD_free(ctx
->mds
); /* No EVP_MD_free method exists. */
195 ASN1_INTEGER_free(ctx
->seconds
);
196 ASN1_INTEGER_free(ctx
->millis
);
197 ASN1_INTEGER_free(ctx
->micros
);
201 int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX
*ctx
, X509
*signer
)
203 if (X509_check_purpose(signer
, X509_PURPOSE_TIMESTAMP_SIGN
, 0) != 1)
205 TSerr(TS_F_TS_RESP_CTX_SET_SIGNER_CERT
,
206 TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE
);
209 if (ctx
->signer_cert
) X509_free(ctx
->signer_cert
);
210 ctx
->signer_cert
= signer
;
211 CRYPTO_add(&ctx
->signer_cert
->references
, +1, CRYPTO_LOCK_X509
);
215 int TS_RESP_CTX_set_signer_key(TS_RESP_CTX
*ctx
, EVP_PKEY
*key
)
217 if (ctx
->signer_key
) EVP_PKEY_free(ctx
->signer_key
);
218 ctx
->signer_key
= key
;
219 CRYPTO_add(&ctx
->signer_key
->references
, +1, CRYPTO_LOCK_EVP_PKEY
);
224 int TS_RESP_CTX_set_def_policy(TS_RESP_CTX
*ctx
, ASN1_OBJECT
*def_policy
)
226 if (ctx
->default_policy
) ASN1_OBJECT_free(ctx
->default_policy
);
227 if (!(ctx
->default_policy
= OBJ_dup(def_policy
))) goto err
;
230 TSerr(TS_F_TS_RESP_CTX_SET_DEF_POLICY
, ERR_R_MALLOC_FAILURE
);
234 int TS_RESP_CTX_set_certs(TS_RESP_CTX
*ctx
, STACK_OF(X509
) *certs
)
240 sk_X509_pop_free(ctx
->certs
, X509_free
);
243 if (!certs
) return 1;
244 if (!(ctx
->certs
= sk_X509_dup(certs
)))
246 TSerr(TS_F_TS_RESP_CTX_SET_CERTS
, ERR_R_MALLOC_FAILURE
);
249 for (i
= 0; i
< sk_X509_num(ctx
->certs
); ++i
)
251 X509
*cert
= sk_X509_value(ctx
->certs
, i
);
252 CRYPTO_add(&cert
->references
, +1, CRYPTO_LOCK_X509
);
258 int TS_RESP_CTX_add_policy(TS_RESP_CTX
*ctx
, ASN1_OBJECT
*policy
)
260 ASN1_OBJECT
*copy
= NULL
;
262 /* Create new policy stack if necessary. */
263 if (!ctx
->policies
&& !(ctx
->policies
= sk_ASN1_OBJECT_new_null()))
265 if (!(copy
= OBJ_dup(policy
))) goto err
;
266 if (!sk_ASN1_OBJECT_push(ctx
->policies
, copy
)) goto err
;
270 TSerr(TS_F_TS_RESP_CTX_ADD_POLICY
, ERR_R_MALLOC_FAILURE
);
271 ASN1_OBJECT_free(copy
);
275 int TS_RESP_CTX_add_md(TS_RESP_CTX
*ctx
, const EVP_MD
*md
)
277 /* Create new md stack if necessary. */
278 if (!ctx
->mds
&& !(ctx
->mds
= sk_EVP_MD_new_null()))
280 /* Add the shared md, no copy needed. */
281 if (!sk_EVP_MD_push(ctx
->mds
, (EVP_MD
*)md
)) goto err
;
285 TSerr(TS_F_TS_RESP_CTX_ADD_MD
, ERR_R_MALLOC_FAILURE
);
289 #define TS_RESP_CTX_accuracy_free(ctx) \
290 ASN1_INTEGER_free(ctx->seconds); \
291 ctx->seconds = NULL; \
292 ASN1_INTEGER_free(ctx->millis); \
293 ctx->millis = NULL; \
294 ASN1_INTEGER_free(ctx->micros); \
297 int TS_RESP_CTX_set_accuracy(TS_RESP_CTX
*ctx
,
298 int secs
, int millis
, int micros
)
301 TS_RESP_CTX_accuracy_free(ctx
);
302 if (secs
&& (!(ctx
->seconds
= ASN1_INTEGER_new())
303 || !ASN1_INTEGER_set(ctx
->seconds
, secs
)))
305 if (millis
&& (!(ctx
->millis
= ASN1_INTEGER_new())
306 || !ASN1_INTEGER_set(ctx
->millis
, millis
)))
308 if (micros
&& (!(ctx
->micros
= ASN1_INTEGER_new())
309 || !ASN1_INTEGER_set(ctx
->micros
, micros
)))
314 TS_RESP_CTX_accuracy_free(ctx
);
315 TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY
, ERR_R_MALLOC_FAILURE
);
319 void TS_RESP_CTX_add_flags(TS_RESP_CTX
*ctx
, int flags
)
324 void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX
*ctx
, TS_serial_cb cb
, void *data
)
327 ctx
->serial_cb_data
= data
;
330 void TS_RESP_CTX_set_time_cb(TS_RESP_CTX
*ctx
, TS_time_cb cb
, void *data
)
333 ctx
->time_cb_data
= data
;
336 void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX
*ctx
,
337 TS_extension_cb cb
, void *data
)
339 ctx
->extension_cb
= cb
;
340 ctx
->extension_cb_data
= data
;
343 int TS_RESP_CTX_set_status_info(TS_RESP_CTX
*ctx
,
344 int status
, const char *text
)
346 TS_STATUS_INFO
*si
= NULL
;
347 ASN1_UTF8STRING
*utf8_text
= NULL
;
350 if (!(si
= TS_STATUS_INFO_new())) goto err
;
351 if (!ASN1_INTEGER_set(si
->status
, status
)) goto err
;
354 if (!(utf8_text
= ASN1_UTF8STRING_new())
355 || !ASN1_STRING_set(utf8_text
, text
, strlen(text
)))
357 if (!si
->text
&& !(si
->text
= sk_ASN1_UTF8STRING_new_null()))
359 if (!sk_ASN1_UTF8STRING_push(si
->text
, utf8_text
)) goto err
;
360 utf8_text
= NULL
; /* Ownership is lost. */
362 if (!TS_RESP_set_status_info(ctx
->response
, si
)) goto err
;
366 TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO
, ERR_R_MALLOC_FAILURE
);
367 TS_STATUS_INFO_free(si
);
368 ASN1_UTF8STRING_free(utf8_text
);
372 int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX
*ctx
,
373 int status
, const char *text
)
376 TS_STATUS_INFO
*si
= TS_RESP_get_status_info(ctx
->response
);
378 if (ASN1_INTEGER_get(si
->status
) == TS_STATUS_GRANTED
)
380 /* Status has not been set, set it now. */
381 ret
= TS_RESP_CTX_set_status_info(ctx
, status
, text
);
386 int TS_RESP_CTX_add_failure_info(TS_RESP_CTX
*ctx
, int failure
)
388 TS_STATUS_INFO
*si
= TS_RESP_get_status_info(ctx
->response
);
389 if (!si
->failure_info
&& !(si
->failure_info
= ASN1_BIT_STRING_new()))
391 if (!ASN1_BIT_STRING_set_bit(si
->failure_info
, failure
, 1))
395 TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO
, ERR_R_MALLOC_FAILURE
);
399 TS_REQ
*TS_RESP_CTX_get_request(TS_RESP_CTX
*ctx
)
404 TS_TST_INFO
*TS_RESP_CTX_get_tst_info(TS_RESP_CTX
*ctx
)
406 return ctx
->tst_info
;
409 int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX
*ctx
, unsigned precision
)
411 if (precision
> TS_MAX_CLOCK_PRECISION_DIGITS
)
413 ctx
->clock_precision_digits
= precision
;
417 /* Main entry method of the response generation. */
418 TS_RESP
*TS_RESP_create_response(TS_RESP_CTX
*ctx
, BIO
*req_bio
)
424 TS_RESP_CTX_init(ctx
);
426 /* Creating the response object. */
427 if (!(ctx
->response
= TS_RESP_new()))
429 TSerr(TS_F_TS_RESP_CREATE_RESPONSE
, ERR_R_MALLOC_FAILURE
);
433 /* Parsing DER request. */
434 if (!(ctx
->request
= d2i_TS_REQ_bio(req_bio
, NULL
)))
436 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
437 "Bad request format or "
439 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_DATA_FORMAT
);
443 /* Setting default status info. */
444 if (!TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_GRANTED
, NULL
))
447 /* Checking the request format. */
448 if (!TS_RESP_check_request(ctx
)) goto end
;
450 /* Checking acceptable policies. */
451 if (!(policy
= TS_RESP_get_policy(ctx
))) goto end
;
453 /* Creating the TS_TST_INFO object. */
454 if (!(ctx
->tst_info
= TS_RESP_create_tst_info(ctx
, policy
)))
457 /* Processing extensions. */
458 if (!TS_RESP_process_extensions(ctx
)) goto end
;
460 /* Generating the signature. */
461 if (!TS_RESP_sign(ctx
)) goto end
;
463 /* Everything was successful. */
468 TSerr(TS_F_TS_RESP_CREATE_RESPONSE
, TS_R_RESPONSE_SETUP_ERROR
);
469 if (ctx
->response
!= NULL
)
471 if (TS_RESP_CTX_set_status_info_cond(ctx
,
472 TS_STATUS_REJECTION
, "Error during response "
475 TS_RESP_free(ctx
->response
);
476 ctx
->response
= NULL
;
480 response
= ctx
->response
;
481 ctx
->response
= NULL
; /* Ownership will be returned to caller. */
482 TS_RESP_CTX_cleanup(ctx
);
486 /* Initializes the variable part of the context. */
487 static void TS_RESP_CTX_init(TS_RESP_CTX
*ctx
)
490 ctx
->response
= NULL
;
491 ctx
->tst_info
= NULL
;
494 /* Cleans up the variable part of the context. */
495 static void TS_RESP_CTX_cleanup(TS_RESP_CTX
*ctx
)
497 TS_REQ_free(ctx
->request
);
499 TS_RESP_free(ctx
->response
);
500 ctx
->response
= NULL
;
501 TS_TST_INFO_free(ctx
->tst_info
);
502 ctx
->tst_info
= NULL
;
505 /* Checks the format and content of the request. */
506 static int TS_RESP_check_request(TS_RESP_CTX
*ctx
)
508 TS_REQ
*request
= ctx
->request
;
509 TS_MSG_IMPRINT
*msg_imprint
;
512 const ASN1_OCTET_STRING
*digest
;
516 /* Checking request version. */
517 if (TS_REQ_get_version(request
) != 1)
519 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
520 "Bad request version.");
521 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_REQUEST
);
525 /* Checking message digest algorithm. */
526 msg_imprint
= TS_REQ_get_msg_imprint(request
);
527 md_alg
= TS_MSG_IMPRINT_get_algo(msg_imprint
);
528 md_alg_id
= OBJ_obj2nid(md_alg
->algorithm
);
529 for (i
= 0; !md
&& i
< sk_EVP_MD_num(ctx
->mds
); ++i
)
531 EVP_MD
*current_md
= sk_EVP_MD_value(ctx
->mds
, i
);
532 if (md_alg_id
== EVP_MD_type(current_md
))
537 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
538 "Message digest algorithm is "
540 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_ALG
);
544 /* No message digest takes parameter. */
545 if (md_alg
->parameter
546 && ASN1_TYPE_get(md_alg
->parameter
) != V_ASN1_NULL
)
548 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
549 "Superfluous message digest "
551 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_ALG
);
554 /* Checking message digest size. */
555 digest
= TS_MSG_IMPRINT_get_msg(msg_imprint
);
556 if (digest
->length
!= EVP_MD_size(md
))
558 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
559 "Bad message digest.");
560 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_BAD_DATA_FORMAT
);
567 /* Returns the TSA policy based on the requested and acceptable policies. */
568 static ASN1_OBJECT
*TS_RESP_get_policy(TS_RESP_CTX
*ctx
)
570 ASN1_OBJECT
*requested
= TS_REQ_get_policy_id(ctx
->request
);
571 ASN1_OBJECT
*policy
= NULL
;
574 if (ctx
->default_policy
== NULL
)
576 TSerr(TS_F_TS_RESP_GET_POLICY
, TS_R_INVALID_NULL_POINTER
);
579 /* Return the default policy if none is requested or the default is
581 if (!requested
|| !OBJ_cmp(requested
, ctx
->default_policy
))
582 policy
= ctx
->default_policy
;
584 /* Check if the policy is acceptable. */
585 for (i
= 0; !policy
&& i
< sk_ASN1_OBJECT_num(ctx
->policies
); ++i
)
587 ASN1_OBJECT
*current
= sk_ASN1_OBJECT_value(ctx
->policies
, i
);
588 if (!OBJ_cmp(requested
, current
))
593 TSerr(TS_F_TS_RESP_GET_POLICY
, TS_R_UNACCEPTABLE_POLICY
);
594 TS_RESP_CTX_set_status_info(ctx
, TS_STATUS_REJECTION
,
595 "Requested policy is not "
597 TS_RESP_CTX_add_failure_info(ctx
, TS_INFO_UNACCEPTED_POLICY
);
602 /* Creates the TS_TST_INFO object based on the settings of the context. */
603 static TS_TST_INFO
*TS_RESP_create_tst_info(TS_RESP_CTX
*ctx
,
607 TS_TST_INFO
*tst_info
= NULL
;
608 ASN1_INTEGER
*serial
= NULL
;
609 ASN1_GENERALIZEDTIME
*asn1_time
= NULL
;
611 TS_ACCURACY
*accuracy
= NULL
;
612 const ASN1_INTEGER
*nonce
;
613 GENERAL_NAME
*tsa_name
= NULL
;
615 if (!(tst_info
= TS_TST_INFO_new())) goto end
;
616 if (!TS_TST_INFO_set_version(tst_info
, 1)) goto end
;
617 if (!TS_TST_INFO_set_policy_id(tst_info
, policy
)) goto end
;
618 if (!TS_TST_INFO_set_msg_imprint(tst_info
, ctx
->request
->msg_imprint
))
620 if (!(serial
= (*ctx
->serial_cb
)(ctx
, ctx
->serial_cb_data
))
621 || !TS_TST_INFO_set_serial(tst_info
, serial
))
623 if (!(*ctx
->time_cb
)(ctx
, ctx
->time_cb_data
, &sec
, &usec
)
624 || !(asn1_time
= TS_RESP_set_genTime_with_precision(NULL
,
626 ctx
->clock_precision_digits
))
627 || !TS_TST_INFO_set_time(tst_info
, asn1_time
))
630 /* Setting accuracy if needed. */
631 if ((ctx
->seconds
|| ctx
->millis
|| ctx
->micros
)
632 && !(accuracy
= TS_ACCURACY_new()))
635 if (ctx
->seconds
&& !TS_ACCURACY_set_seconds(accuracy
, ctx
->seconds
))
637 if (ctx
->millis
&& !TS_ACCURACY_set_millis(accuracy
, ctx
->millis
))
639 if (ctx
->micros
&& !TS_ACCURACY_set_micros(accuracy
, ctx
->micros
))
641 if (accuracy
&& !TS_TST_INFO_set_accuracy(tst_info
, accuracy
))
644 /* Setting ordering. */
645 if ((ctx
->flags
& TS_ORDERING
)
646 && !TS_TST_INFO_set_ordering(tst_info
, 1))
649 /* Setting nonce if needed. */
650 if ((nonce
= TS_REQ_get_nonce(ctx
->request
)) != NULL
651 && !TS_TST_INFO_set_nonce(tst_info
, nonce
))
654 /* Setting TSA name to subject of signer certificate. */
655 if (ctx
->flags
& TS_TSA_NAME
)
657 if (!(tsa_name
= GENERAL_NAME_new())) goto end
;
658 tsa_name
->type
= GEN_DIRNAME
;
660 X509_NAME_dup(ctx
->signer_cert
->cert_info
->subject
);
661 if (!tsa_name
->d
.dirn
) goto end
;
662 if (!TS_TST_INFO_set_tsa(tst_info
, tsa_name
)) goto end
;
669 TS_TST_INFO_free(tst_info
);
671 TSerr(TS_F_TS_RESP_CREATE_TST_INFO
, TS_R_TST_INFO_SETUP_ERROR
);
672 TS_RESP_CTX_set_status_info_cond(ctx
, TS_STATUS_REJECTION
,
673 "Error during TSTInfo "
676 GENERAL_NAME_free(tsa_name
);
677 TS_ACCURACY_free(accuracy
);
678 ASN1_GENERALIZEDTIME_free(asn1_time
);
679 ASN1_INTEGER_free(serial
);
684 /* Processing the extensions of the request. */
685 static int TS_RESP_process_extensions(TS_RESP_CTX
*ctx
)
687 STACK_OF(X509_EXTENSION
) *exts
= TS_REQ_get_exts(ctx
->request
);
691 for (i
= 0; ok
&& i
< sk_X509_EXTENSION_num(exts
); ++i
)
693 X509_EXTENSION
*ext
= sk_X509_EXTENSION_value(exts
, i
);
694 /* XXXXX The last argument was previously
695 (void *)ctx->extension_cb, but ISO C doesn't permit
696 converting a function pointer to void *. For lack of
697 better information, I'm placing a NULL there instead.
698 The callback can pick its own address out from the ctx
701 ok
= (*ctx
->extension_cb
)(ctx
, ext
, NULL
);
707 /* Functions for signing the TS_TST_INFO structure of the context. */
708 static int TS_RESP_sign(TS_RESP_CTX
*ctx
)
712 PKCS7_SIGNER_INFO
*si
;
713 STACK_OF(X509
) *certs
; /* Certificates to include in sc. */
714 ESS_SIGNING_CERT
*sc
= NULL
;
719 /* Check if signcert and pkey match. */
720 if (!X509_check_private_key(ctx
->signer_cert
, ctx
->signer_key
)) {
721 TSerr(TS_F_TS_RESP_SIGN
,
722 TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
726 /* Create a new PKCS7 signed object. */
727 if (!(p7
= PKCS7_new())) {
728 TSerr(TS_F_TS_RESP_SIGN
, ERR_R_MALLOC_FAILURE
);
731 if (!PKCS7_set_type(p7
, NID_pkcs7_signed
)) goto err
;
733 /* Force SignedData version to be 3 instead of the default 1. */
734 if (!ASN1_INTEGER_set(p7
->d
.sign
->version
, 3)) goto err
;
736 /* Add signer certificate and optional certificate chain. */
737 if (TS_REQ_get_cert_req(ctx
->request
))
739 PKCS7_add_certificate(p7
, ctx
->signer_cert
);
742 for(i
= 0; i
< sk_X509_num(ctx
->certs
); ++i
)
744 X509
*cert
= sk_X509_value(ctx
->certs
, i
);
745 PKCS7_add_certificate(p7
, cert
);
750 /* Add a new signer info. */
751 if (!(si
= PKCS7_add_signature(p7
, ctx
->signer_cert
,
752 ctx
->signer_key
, EVP_sha1())))
754 TSerr(TS_F_TS_RESP_SIGN
, TS_R_PKCS7_ADD_SIGNATURE_ERROR
);
758 /* Add content type signed attribute to the signer info. */
759 oid
= OBJ_nid2obj(NID_id_smime_ct_TSTInfo
);
760 if (!PKCS7_add_signed_attribute(si
, NID_pkcs9_contentType
,
763 TSerr(TS_F_TS_RESP_SIGN
, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR
);
767 /* Create the ESS SigningCertificate attribute which contains
768 the signer certificate id and optionally the certificate chain. */
769 certs
= ctx
->flags
& TS_ESS_CERT_ID_CHAIN
? ctx
->certs
: NULL
;
770 if (!(sc
= ESS_SIGNING_CERT_new_init(ctx
->signer_cert
, certs
)))
773 /* Add SigningCertificate signed attribute to the signer info. */
774 if (!ESS_add_signing_cert(si
, sc
))
776 TSerr(TS_F_TS_RESP_SIGN
, TS_R_ESS_ADD_SIGNING_CERT_ERROR
);
780 /* Add a new empty NID_id_smime_ct_TSTInfo encapsulated content. */
781 if (!TS_TST_INFO_content_new(p7
)) goto err
;
783 /* Add the DER encoded tst_info to the PKCS7 structure. */
784 if (!(p7bio
= PKCS7_dataInit(p7
, NULL
))) {
785 TSerr(TS_F_TS_RESP_SIGN
, ERR_R_MALLOC_FAILURE
);
789 /* Convert tst_info to DER. */
790 if (!i2d_TS_TST_INFO_bio(p7bio
, ctx
->tst_info
))
792 TSerr(TS_F_TS_RESP_SIGN
, TS_R_TS_DATASIGN
);
796 /* Create the signature and add it to the signer info. */
797 if (!PKCS7_dataFinal(p7
, p7bio
))
799 TSerr(TS_F_TS_RESP_SIGN
, TS_R_TS_DATASIGN
);
803 /* Set new PKCS7 and TST_INFO objects. */
804 TS_RESP_set_tst_info(ctx
->response
, p7
, ctx
->tst_info
);
805 p7
= NULL
; /* Ownership is lost. */
806 ctx
->tst_info
= NULL
; /* Ownership is lost. */
811 TS_RESP_CTX_set_status_info_cond(ctx
, TS_STATUS_REJECTION
,
812 "Error during signature "
815 ESS_SIGNING_CERT_free(sc
);
820 static ESS_SIGNING_CERT
*ESS_SIGNING_CERT_new_init(X509
*signcert
,
821 STACK_OF(X509
) *certs
)
824 ESS_SIGNING_CERT
*sc
= NULL
;
827 /* Creating the ESS_CERT_ID stack. */
828 if (!(sc
= ESS_SIGNING_CERT_new())) goto err
;
829 if (!sc
->cert_ids
&& !(sc
->cert_ids
= sk_ESS_CERT_ID_new_null()))
832 /* Adding the signing certificate id. */
833 if (!(cid
= ESS_CERT_ID_new_init(signcert
, 0))
834 || !sk_ESS_CERT_ID_push(sc
->cert_ids
, cid
))
836 /* Adding the certificate chain ids. */
837 for (i
= 0; i
< sk_X509_num(certs
); ++i
)
839 X509
*cert
= sk_X509_value(certs
, i
);
840 if (!(cid
= ESS_CERT_ID_new_init(cert
, 1))
841 || !sk_ESS_CERT_ID_push(sc
->cert_ids
, cid
))
847 ESS_SIGNING_CERT_free(sc
);
848 TSerr(TS_F_ESS_SIGNING_CERT_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
852 static ESS_CERT_ID
*ESS_CERT_ID_new_init(X509
*cert
, int issuer_needed
)
854 ESS_CERT_ID
*cid
= NULL
;
855 GENERAL_NAME
*name
= NULL
;
857 /* Recompute SHA1 hash of certificate if necessary (side effect). */
858 X509_check_purpose(cert
, -1, 0);
860 if (!(cid
= ESS_CERT_ID_new())) goto err
;
861 if (!ASN1_OCTET_STRING_set(cid
->hash
, cert
->sha1_hash
,
862 sizeof(cert
->sha1_hash
)))
865 /* Setting the issuer/serial if requested. */
868 /* Creating issuer/serial structure. */
869 if (!cid
->issuer_serial
870 && !(cid
->issuer_serial
= ESS_ISSUER_SERIAL_new()))
872 /* Creating general name from the certificate issuer. */
873 if (!(name
= GENERAL_NAME_new())) goto err
;
874 name
->type
= GEN_DIRNAME
;
875 if (!(name
->d
.dirn
= X509_NAME_dup(cert
->cert_info
->issuer
)))
877 if (!sk_GENERAL_NAME_push(cid
->issuer_serial
->issuer
, name
))
879 name
= NULL
; /* Ownership is lost. */
880 /* Setting the serial number. */
881 ASN1_INTEGER_free(cid
->issuer_serial
->serial
);
882 if (!(cid
->issuer_serial
->serial
=
883 ASN1_INTEGER_dup(cert
->cert_info
->serialNumber
)))
889 GENERAL_NAME_free(name
);
890 ESS_CERT_ID_free(cid
);
891 TSerr(TS_F_ESS_CERT_ID_NEW_INIT
, ERR_R_MALLOC_FAILURE
);
895 static int TS_TST_INFO_content_new(PKCS7
*p7
)
898 ASN1_OCTET_STRING
*octet_string
= NULL
;
900 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
901 if (!(ret
= PKCS7_new())) goto err
;
902 if (!(ret
->d
.other
= ASN1_TYPE_new())) goto err
;
903 ret
->type
= OBJ_nid2obj(NID_id_smime_ct_TSTInfo
);
904 if (!(octet_string
= ASN1_OCTET_STRING_new())) goto err
;
905 ASN1_TYPE_set(ret
->d
.other
, V_ASN1_OCTET_STRING
, octet_string
);
908 /* Add encapsulated content to signed PKCS7 structure. */
909 if (!PKCS7_set_content(p7
, ret
)) goto err
;
913 ASN1_OCTET_STRING_free(octet_string
);
918 static int ESS_add_signing_cert(PKCS7_SIGNER_INFO
*si
, ESS_SIGNING_CERT
*sc
)
920 ASN1_STRING
*seq
= NULL
;
921 unsigned char *p
, *pp
= NULL
;
924 len
= i2d_ESS_SIGNING_CERT(sc
, NULL
);
925 if (!(pp
= (unsigned char *) OPENSSL_malloc(len
)))
927 TSerr(TS_F_ESS_ADD_SIGNING_CERT
, ERR_R_MALLOC_FAILURE
);
931 i2d_ESS_SIGNING_CERT(sc
, &p
);
932 if (!(seq
= ASN1_STRING_new()) || !ASN1_STRING_set(seq
, pp
, len
))
934 TSerr(TS_F_ESS_ADD_SIGNING_CERT
, ERR_R_MALLOC_FAILURE
);
937 OPENSSL_free(pp
); pp
= NULL
;
938 return PKCS7_add_signed_attribute(si
,
939 NID_id_smime_aa_signingCertificate
,
940 V_ASN1_SEQUENCE
, seq
);
942 ASN1_STRING_free(seq
);
949 static ASN1_GENERALIZEDTIME
*
950 TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME
*asn1_time
,
951 long sec
, long usec
, unsigned precision
)
953 time_t time_sec
= (time_t) sec
;
954 struct tm
*tm
= NULL
;
955 char genTime_str
[17 + TS_MAX_CLOCK_PRECISION_DIGITS
];
956 char *p
= genTime_str
;
957 char *p_end
= genTime_str
+ sizeof(genTime_str
);
959 if (precision
> TS_MAX_CLOCK_PRECISION_DIGITS
)
963 if (!(tm
= gmtime(&time_sec
)))
967 * Put "genTime_str" in GeneralizedTime format. We work around the
968 * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
969 * NOT include fractional seconds") and OpenSSL related functions to
970 * meet the rfc3161 requirement: "GeneralizedTime syntax can include
971 * fraction-of-second details".
973 p
+= BIO_snprintf(p
, p_end
- p
,
974 "%04d%02d%02d%02d%02d%02d",
975 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
976 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
979 /* Add fraction of seconds (leave space for dot and null). */
980 BIO_snprintf(p
, 2 + precision
, ".%ld", usec
);
981 /* We cannot use the snprintf return value,
982 because it might have been truncated. */
985 /* To make things a bit harder, X.690 | ISO/IEC 8825-1 provides
986 the following restrictions for a DER-encoding, which OpenSSL
987 (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
989 "The encoding MUST terminate with a "Z" (which means "Zulu"
990 time). The decimal point element, if present, MUST be the
991 point option ".". The fractional-seconds elements,
992 if present, MUST omit all trailing 0's;
993 if the elements correspond to 0, they MUST be wholly
994 omitted, and the decimal point element also MUST be
996 /* Remove trailing zeros. The dot guarantees the exit
997 condition of this loop even if all the digits are zero. */
1000 /* p points to either the dot or the last non-zero digit. */
1003 /* Add the trailing Z and the terminating null. */
1007 /* Now call OpenSSL to check and set our genTime value */
1008 if (!asn1_time
&& !(asn1_time
= M_ASN1_GENERALIZEDTIME_new()))
1010 if (!ASN1_GENERALIZEDTIME_set_string(asn1_time
, genTime_str
))
1012 ASN1_GENERALIZEDTIME_free(asn1_time
);
1018 TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION
, TS_R_COULD_NOT_SET_TIME
);