]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ts/ts_rsp_sign.c
-Wunused-function cleanup
[thirdparty/openssl.git] / crypto / ts / ts_rsp_sign.c
CommitLineData
0f113f3e 1/*
a28d06f3 2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
c7235be6 3 *
a1b4409d 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
c7235be6
UM
8 */
9
4cff10dc 10#include "e_os.h"
b39fc560 11#include "internal/cryptlib.h"
c7235be6 12
c7235be6
UM
13#include <openssl/objects.h>
14#include <openssl/ts.h>
15#include <openssl/pkcs7.h>
98c03302 16#include <openssl/crypto.h>
706457b7 17#include "ts_local.h"
25f2138b 18#include "crypto/ess.h"
c7235be6 19
852c2ed2
RS
20DEFINE_STACK_OF_CONST(EVP_MD)
21
c7235be6
UM
22static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *);
23static int def_time_cb(struct TS_resp_ctx *, void *, long *sec, long *usec);
24static int def_extension_cb(struct TS_resp_ctx *, X509_EXTENSION *, void *);
25
9c422b5b
RS
26static void ts_RESP_CTX_init(TS_RESP_CTX *ctx);
27static void ts_RESP_CTX_cleanup(TS_RESP_CTX *ctx);
28static int ts_RESP_check_request(TS_RESP_CTX *ctx);
29static ASN1_OBJECT *ts_RESP_get_policy(TS_RESP_CTX *ctx);
30static TS_TST_INFO *ts_RESP_create_tst_info(TS_RESP_CTX *ctx,
0f113f3e 31 ASN1_OBJECT *policy);
9c422b5b
RS
32static int ts_RESP_process_extensions(TS_RESP_CTX *ctx);
33static int ts_RESP_sign(TS_RESP_CTX *ctx);
c7235be6 34
9c422b5b 35static int ts_TST_INFO_content_new(PKCS7 *p7);
c7235be6 36
0f113f3e
MC
37static ASN1_GENERALIZEDTIME
38*TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *, long, long,
39 unsigned);
c7235be6 40
18cd23df 41/* Default callback for response generation. */
c7235be6 42static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *ctx, void *data)
0f113f3e
MC
43{
44 ASN1_INTEGER *serial = ASN1_INTEGER_new();
18cd23df 45
90945fa3 46 if (serial == NULL)
0f113f3e
MC
47 goto err;
48 if (!ASN1_INTEGER_set(serial, 1))
49 goto err;
50 return serial;
18cd23df 51
c7235be6 52 err:
9311d0c4 53 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
54 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
55 "Error during serial number generation.");
d4d67baf 56 ASN1_INTEGER_free(serial);
0f113f3e
MC
57 return NULL;
58}
c7235be6
UM
59
60#if defined(OPENSSL_SYS_UNIX)
61
0f113f3e
MC
62static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
63 long *sec, long *usec)
64{
65 struct timeval tv;
66 if (gettimeofday(&tv, NULL) != 0) {
9311d0c4 67 ERR_raise(ERR_LIB_TS, TS_R_TIME_SYSCALL_ERROR);
0f113f3e
MC
68 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
69 "Time is not available.");
70 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
71 return 0;
72 }
0f113f3e
MC
73 *sec = tv.tv_sec;
74 *usec = tv.tv_usec;
75
76 return 1;
77}
c7235be6
UM
78
79#else
80
0f113f3e
MC
81static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
82 long *sec, long *usec)
83{
84 time_t t;
85 if (time(&t) == (time_t)-1) {
9311d0c4 86 ERR_raise(ERR_LIB_TS, TS_R_TIME_SYSCALL_ERROR);
0f113f3e
MC
87 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
88 "Time is not available.");
89 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
90 return 0;
91 }
0f113f3e
MC
92 *sec = (long)t;
93 *usec = 0;
94
95 return 1;
96}
c7235be6
UM
97
98#endif
99
100static int def_extension_cb(struct TS_resp_ctx *ctx, X509_EXTENSION *ext,
0f113f3e
MC
101 void *data)
102{
0f113f3e
MC
103 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
104 "Unsupported extension.");
105 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_EXTENSION);
106 return 0;
107}
c7235be6
UM
108
109/* TS_RESP_CTX management functions. */
110
3cb7c5cf 111TS_RESP_CTX *TS_RESP_CTX_new(void)
0f113f3e
MC
112{
113 TS_RESP_CTX *ctx;
c7235be6 114
b51bce94 115 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
9311d0c4 116 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
117 return NULL;
118 }
c7235be6 119
e20b4727
DSH
120 ctx->signer_md = EVP_sha256();
121
0f113f3e
MC
122 ctx->serial_cb = def_serial_cb;
123 ctx->time_cb = def_time_cb;
124 ctx->extension_cb = def_extension_cb;
c7235be6 125
0f113f3e
MC
126 return ctx;
127}
c7235be6
UM
128
129void TS_RESP_CTX_free(TS_RESP_CTX *ctx)
0f113f3e
MC
130{
131 if (!ctx)
132 return;
133
134 X509_free(ctx->signer_cert);
135 EVP_PKEY_free(ctx->signer_key);
136 sk_X509_pop_free(ctx->certs, X509_free);
137 sk_ASN1_OBJECT_pop_free(ctx->policies, ASN1_OBJECT_free);
138 ASN1_OBJECT_free(ctx->default_policy);
139 sk_EVP_MD_free(ctx->mds); /* No EVP_MD_free method exists. */
140 ASN1_INTEGER_free(ctx->seconds);
141 ASN1_INTEGER_free(ctx->millis);
142 ASN1_INTEGER_free(ctx->micros);
143 OPENSSL_free(ctx);
144}
c7235be6
UM
145
146int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer)
0f113f3e
MC
147{
148 if (X509_check_purpose(signer, X509_PURPOSE_TIMESTAMP_SIGN, 0) != 1) {
9311d0c4 149 ERR_raise(ERR_LIB_TS, TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE);
0f113f3e
MC
150 return 0;
151 }
222561fe 152 X509_free(ctx->signer_cert);
0f113f3e 153 ctx->signer_cert = signer;
05f0fb9f 154 X509_up_ref(ctx->signer_cert);
0f113f3e
MC
155 return 1;
156}
c7235be6
UM
157
158int TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key)
0f113f3e 159{
c5ba2d99 160 EVP_PKEY_free(ctx->signer_key);
0f113f3e 161 ctx->signer_key = key;
3aeb9348 162 EVP_PKEY_up_ref(ctx->signer_key);
c7235be6 163
0f113f3e
MC
164 return 1;
165}
c7235be6 166
e20b4727
DSH
167int TS_RESP_CTX_set_signer_digest(TS_RESP_CTX *ctx, const EVP_MD *md)
168{
169 ctx->signer_md = md;
170 return 1;
171}
172
c47ba4e9 173int TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *def_policy)
0f113f3e 174{
2ace7450 175 ASN1_OBJECT_free(ctx->default_policy);
75ebbd9a 176 if ((ctx->default_policy = OBJ_dup(def_policy)) == NULL)
0f113f3e
MC
177 goto err;
178 return 1;
c7235be6 179 err:
9311d0c4 180 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
181 return 0;
182}
c7235be6
UM
183
184int TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs)
0f113f3e 185{
222561fe
RS
186 sk_X509_pop_free(ctx->certs, X509_free);
187 ctx->certs = NULL;
0f113f3e 188
d53b437f 189 return certs == NULL || (ctx->certs = X509_chain_up_ref(certs)) != NULL;
0f113f3e 190}
c7235be6 191
c47ba4e9 192int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, const ASN1_OBJECT *policy)
0f113f3e
MC
193{
194 ASN1_OBJECT *copy = NULL;
195
75ebbd9a
RS
196 if (ctx->policies == NULL
197 && (ctx->policies = sk_ASN1_OBJECT_new_null()) == NULL)
0f113f3e 198 goto err;
75ebbd9a 199 if ((copy = OBJ_dup(policy)) == NULL)
0f113f3e
MC
200 goto err;
201 if (!sk_ASN1_OBJECT_push(ctx->policies, copy))
202 goto err;
203
204 return 1;
c7235be6 205 err:
9311d0c4 206 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
207 ASN1_OBJECT_free(copy);
208 return 0;
209}
c7235be6
UM
210
211int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md)
0f113f3e 212{
75ebbd9a
RS
213 if (ctx->mds == NULL
214 && (ctx->mds = sk_EVP_MD_new_null()) == NULL)
0f113f3e 215 goto err;
ac94c8fd 216 if (!sk_EVP_MD_push(ctx->mds, md))
0f113f3e
MC
217 goto err;
218
219 return 1;
c7235be6 220 err:
9311d0c4 221 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
222 return 0;
223}
224
225#define TS_RESP_CTX_accuracy_free(ctx) \
226 ASN1_INTEGER_free(ctx->seconds); \
227 ctx->seconds = NULL; \
228 ASN1_INTEGER_free(ctx->millis); \
229 ctx->millis = NULL; \
230 ASN1_INTEGER_free(ctx->micros); \
231 ctx->micros = NULL;
232
233int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx,
234 int secs, int millis, int micros)
235{
236
237 TS_RESP_CTX_accuracy_free(ctx);
75ebbd9a
RS
238 if (secs
239 && ((ctx->seconds = ASN1_INTEGER_new()) == NULL
240 || !ASN1_INTEGER_set(ctx->seconds, secs)))
0f113f3e 241 goto err;
75ebbd9a
RS
242 if (millis
243 && ((ctx->millis = ASN1_INTEGER_new()) == NULL
244 || !ASN1_INTEGER_set(ctx->millis, millis)))
0f113f3e 245 goto err;
75ebbd9a
RS
246 if (micros
247 && ((ctx->micros = ASN1_INTEGER_new()) == NULL
248 || !ASN1_INTEGER_set(ctx->micros, micros)))
0f113f3e
MC
249 goto err;
250
251 return 1;
c7235be6 252 err:
0f113f3e 253 TS_RESP_CTX_accuracy_free(ctx);
9311d0c4 254 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
255 return 0;
256}
c7235be6
UM
257
258void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags)
0f113f3e
MC
259{
260 ctx->flags |= flags;
261}
c7235be6
UM
262
263void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data)
0f113f3e
MC
264{
265 ctx->serial_cb = cb;
266 ctx->serial_cb_data = data;
267}
c7235be6
UM
268
269void TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data)
0f113f3e
MC
270{
271 ctx->time_cb = cb;
272 ctx->time_cb_data = data;
273}
274
275void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx,
276 TS_extension_cb cb, void *data)
277{
278 ctx->extension_cb = cb;
279 ctx->extension_cb_data = data;
280}
281
282int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx,
283 int status, const char *text)
284{
285 TS_STATUS_INFO *si = NULL;
286 ASN1_UTF8STRING *utf8_text = NULL;
287 int ret = 0;
288
75ebbd9a 289 if ((si = TS_STATUS_INFO_new()) == NULL)
0f113f3e
MC
290 goto err;
291 if (!ASN1_INTEGER_set(si->status, status))
292 goto err;
293 if (text) {
75ebbd9a 294 if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
0f113f3e
MC
295 || !ASN1_STRING_set(utf8_text, text, strlen(text)))
296 goto err;
75ebbd9a
RS
297 if (si->text == NULL
298 && (si->text = sk_ASN1_UTF8STRING_new_null()) == NULL)
0f113f3e
MC
299 goto err;
300 if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text))
301 goto err;
302 utf8_text = NULL; /* Ownership is lost. */
303 }
304 if (!TS_RESP_set_status_info(ctx->response, si))
305 goto err;
306 ret = 1;
c7235be6 307 err:
0f113f3e 308 if (!ret)
9311d0c4 309 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
310 TS_STATUS_INFO_free(si);
311 ASN1_UTF8STRING_free(utf8_text);
312 return ret;
313}
314
315int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx,
316 int status, const char *text)
317{
318 int ret = 1;
ca4a494c 319 TS_STATUS_INFO *si = ctx->response->status_info;
0f113f3e
MC
320
321 if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) {
0f113f3e
MC
322 ret = TS_RESP_CTX_set_status_info(ctx, status, text);
323 }
324 return ret;
325}
c7235be6
UM
326
327int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure)
0f113f3e 328{
ca4a494c 329 TS_STATUS_INFO *si = ctx->response->status_info;
75ebbd9a
RS
330 if (si->failure_info == NULL
331 && (si->failure_info = ASN1_BIT_STRING_new()) == NULL)
0f113f3e
MC
332 goto err;
333 if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1))
334 goto err;
335 return 1;
c7235be6 336 err:
9311d0c4 337 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
338 return 0;
339}
c7235be6
UM
340
341TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx)
0f113f3e
MC
342{
343 return ctx->request;
344}
c7235be6
UM
345
346TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx)
0f113f3e
MC
347{
348 return ctx->tst_info;
349}
350
351int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx,
352 unsigned precision)
353{
354 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
355 return 0;
356 ctx->clock_precision_digits = precision;
357 return 1;
358}
c7235be6
UM
359
360/* Main entry method of the response generation. */
361TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio)
0f113f3e
MC
362{
363 ASN1_OBJECT *policy;
364 TS_RESP *response;
365 int result = 0;
366
9c422b5b 367 ts_RESP_CTX_init(ctx);
0f113f3e 368
75ebbd9a 369 if ((ctx->response = TS_RESP_new()) == NULL) {
9311d0c4 370 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
371 goto end;
372 }
75ebbd9a 373 if ((ctx->request = d2i_TS_REQ_bio(req_bio, NULL)) == NULL) {
0f113f3e 374 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
18cd23df 375 "Bad request format or system error.");
0f113f3e
MC
376 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
377 goto end;
378 }
0f113f3e
MC
379 if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL))
380 goto end;
9c422b5b 381 if (!ts_RESP_check_request(ctx))
0f113f3e 382 goto end;
9c422b5b 383 if ((policy = ts_RESP_get_policy(ctx)) == NULL)
0f113f3e 384 goto end;
9c422b5b 385 if ((ctx->tst_info = ts_RESP_create_tst_info(ctx, policy)) == NULL)
0f113f3e 386 goto end;
9c422b5b 387 if (!ts_RESP_process_extensions(ctx))
0f113f3e 388 goto end;
9c422b5b 389 if (!ts_RESP_sign(ctx))
0f113f3e 390 goto end;
0f113f3e 391 result = 1;
18cd23df 392
c7235be6 393 end:
0f113f3e 394 if (!result) {
9311d0c4 395 ERR_raise(ERR_LIB_TS, TS_R_RESPONSE_SETUP_ERROR);
0f113f3e
MC
396 if (ctx->response != NULL) {
397 if (TS_RESP_CTX_set_status_info_cond(ctx,
398 TS_STATUS_REJECTION,
399 "Error during response "
400 "generation.") == 0) {
401 TS_RESP_free(ctx->response);
402 ctx->response = NULL;
403 }
404 }
405 }
406 response = ctx->response;
407 ctx->response = NULL; /* Ownership will be returned to caller. */
9c422b5b 408 ts_RESP_CTX_cleanup(ctx);
0f113f3e
MC
409 return response;
410}
c7235be6
UM
411
412/* Initializes the variable part of the context. */
9c422b5b 413static void ts_RESP_CTX_init(TS_RESP_CTX *ctx)
0f113f3e
MC
414{
415 ctx->request = NULL;
416 ctx->response = NULL;
417 ctx->tst_info = NULL;
418}
c7235be6
UM
419
420/* Cleans up the variable part of the context. */
9c422b5b 421static void ts_RESP_CTX_cleanup(TS_RESP_CTX *ctx)
0f113f3e
MC
422{
423 TS_REQ_free(ctx->request);
424 ctx->request = NULL;
425 TS_RESP_free(ctx->response);
426 ctx->response = NULL;
427 TS_TST_INFO_free(ctx->tst_info);
428 ctx->tst_info = NULL;
429}
c7235be6
UM
430
431/* Checks the format and content of the request. */
9c422b5b 432static int ts_RESP_check_request(TS_RESP_CTX *ctx)
0f113f3e
MC
433{
434 TS_REQ *request = ctx->request;
435 TS_MSG_IMPRINT *msg_imprint;
436 X509_ALGOR *md_alg;
437 int md_alg_id;
438 const ASN1_OCTET_STRING *digest;
ac94c8fd 439 const EVP_MD *md = NULL;
0f113f3e
MC
440 int i;
441
0f113f3e
MC
442 if (TS_REQ_get_version(request) != 1) {
443 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
444 "Bad request version.");
445 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST);
446 return 0;
447 }
448
ca4a494c
RS
449 msg_imprint = request->msg_imprint;
450 md_alg = msg_imprint->hash_algo;
0f113f3e
MC
451 md_alg_id = OBJ_obj2nid(md_alg->algorithm);
452 for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) {
ac94c8fd 453 const EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i);
0f113f3e
MC
454 if (md_alg_id == EVP_MD_type(current_md))
455 md = current_md;
456 }
457 if (!md) {
458 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
459 "Message digest algorithm is "
460 "not supported.");
461 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
462 return 0;
463 }
464
0f113f3e
MC
465 if (md_alg->parameter && ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
466 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
467 "Superfluous message digest "
468 "parameter.");
469 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
470 return 0;
471 }
ca4a494c 472 digest = msg_imprint->hashed_msg;
0f113f3e
MC
473 if (digest->length != EVP_MD_size(md)) {
474 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
475 "Bad message digest.");
476 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
477 return 0;
478 }
479
480 return 1;
481}
c7235be6 482
b4e88ccb 483/* Returns the TSA policy based on the requested and acceptable policies. */
9c422b5b 484static ASN1_OBJECT *ts_RESP_get_policy(TS_RESP_CTX *ctx)
0f113f3e 485{
ca4a494c 486 ASN1_OBJECT *requested = ctx->request->policy_id;
0f113f3e
MC
487 ASN1_OBJECT *policy = NULL;
488 int i;
489
490 if (ctx->default_policy == NULL) {
9311d0c4 491 ERR_raise(ERR_LIB_TS, TS_R_INVALID_NULL_POINTER);
0f113f3e
MC
492 return NULL;
493 }
0f113f3e
MC
494 if (!requested || !OBJ_cmp(requested, ctx->default_policy))
495 policy = ctx->default_policy;
496
497 /* Check if the policy is acceptable. */
498 for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
499 ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
500 if (!OBJ_cmp(requested, current))
501 policy = current;
502 }
12a765a5 503 if (policy == NULL) {
9311d0c4 504 ERR_raise(ERR_LIB_TS, TS_R_UNACCEPTABLE_POLICY);
0f113f3e
MC
505 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
506 "Requested policy is not " "supported.");
507 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
508 }
509 return policy;
510}
c7235be6
UM
511
512/* Creates the TS_TST_INFO object based on the settings of the context. */
9c422b5b 513static TS_TST_INFO *ts_RESP_create_tst_info(TS_RESP_CTX *ctx,
0f113f3e
MC
514 ASN1_OBJECT *policy)
515{
516 int result = 0;
517 TS_TST_INFO *tst_info = NULL;
518 ASN1_INTEGER *serial = NULL;
519 ASN1_GENERALIZEDTIME *asn1_time = NULL;
520 long sec, usec;
521 TS_ACCURACY *accuracy = NULL;
522 const ASN1_INTEGER *nonce;
523 GENERAL_NAME *tsa_name = NULL;
524
75ebbd9a 525 if ((tst_info = TS_TST_INFO_new()) == NULL)
0f113f3e
MC
526 goto end;
527 if (!TS_TST_INFO_set_version(tst_info, 1))
528 goto end;
529 if (!TS_TST_INFO_set_policy_id(tst_info, policy))
530 goto end;
531 if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
532 goto end;
75ebbd9a 533 if ((serial = ctx->serial_cb(ctx, ctx->serial_cb_data)) == NULL
0f113f3e
MC
534 || !TS_TST_INFO_set_serial(tst_info, serial))
535 goto end;
75ebbd9a
RS
536 if (!ctx->time_cb(ctx, ctx->time_cb_data, &sec, &usec)
537 || (asn1_time =
538 TS_RESP_set_genTime_with_precision(NULL, sec, usec,
539 ctx->clock_precision_digits)) == NULL
0f113f3e
MC
540 || !TS_TST_INFO_set_time(tst_info, asn1_time))
541 goto end;
542
0f113f3e 543 if ((ctx->seconds || ctx->millis || ctx->micros)
75ebbd9a 544 && (accuracy = TS_ACCURACY_new()) == NULL)
0f113f3e 545 goto end;
0f113f3e
MC
546 if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
547 goto end;
548 if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
549 goto end;
550 if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
551 goto end;
552 if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
553 goto end;
554
0f113f3e
MC
555 if ((ctx->flags & TS_ORDERING)
556 && !TS_TST_INFO_set_ordering(tst_info, 1))
557 goto end;
558
ca4a494c 559 if ((nonce = ctx->request->nonce) != NULL
0f113f3e
MC
560 && !TS_TST_INFO_set_nonce(tst_info, nonce))
561 goto end;
562
0f113f3e 563 if (ctx->flags & TS_TSA_NAME) {
75ebbd9a 564 if ((tsa_name = GENERAL_NAME_new()) == NULL)
0f113f3e
MC
565 goto end;
566 tsa_name->type = GEN_DIRNAME;
567 tsa_name->d.dirn =
a8d8e06b 568 X509_NAME_dup(X509_get_subject_name(ctx->signer_cert));
0f113f3e
MC
569 if (!tsa_name->d.dirn)
570 goto end;
571 if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
572 goto end;
573 }
574
575 result = 1;
c7235be6 576 end:
0f113f3e
MC
577 if (!result) {
578 TS_TST_INFO_free(tst_info);
579 tst_info = NULL;
9311d0c4 580 ERR_raise(ERR_LIB_TS, TS_R_TST_INFO_SETUP_ERROR);
0f113f3e
MC
581 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
582 "Error during TSTInfo "
583 "generation.");
584 }
585 GENERAL_NAME_free(tsa_name);
586 TS_ACCURACY_free(accuracy);
587 ASN1_GENERALIZEDTIME_free(asn1_time);
588 ASN1_INTEGER_free(serial);
589
590 return tst_info;
591}
c7235be6
UM
592
593/* Processing the extensions of the request. */
9c422b5b 594static int ts_RESP_process_extensions(TS_RESP_CTX *ctx)
0f113f3e 595{
ca4a494c 596 STACK_OF(X509_EXTENSION) *exts = ctx->request->extensions;
0f113f3e
MC
597 int i;
598 int ok = 1;
599
600 for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
601 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
602 /*
18cd23df 603 * The last argument was previously (void *)ctx->extension_cb,
0f113f3e
MC
604 * but ISO C doesn't permit converting a function pointer to void *.
605 * For lack of better information, I'm placing a NULL there instead.
606 * The callback can pick its own address out from the ctx anyway...
607 */
608 ok = (*ctx->extension_cb) (ctx, ext, NULL);
609 }
610
611 return ok;
612}
c7235be6
UM
613
614/* Functions for signing the TS_TST_INFO structure of the context. */
9c422b5b 615static int ts_RESP_sign(TS_RESP_CTX *ctx)
0f113f3e
MC
616{
617 int ret = 0;
618 PKCS7 *p7 = NULL;
619 PKCS7_SIGNER_INFO *si;
620 STACK_OF(X509) *certs; /* Certificates to include in sc. */
f0ef20bf 621 ESS_SIGNING_CERT_V2 *sc2 = NULL;
0f113f3e
MC
622 ESS_SIGNING_CERT *sc = NULL;
623 ASN1_OBJECT *oid;
624 BIO *p7bio = NULL;
625 int i;
626
0f113f3e 627 if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
9311d0c4 628 ERR_raise(ERR_LIB_TS, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
0f113f3e
MC
629 goto err;
630 }
631
75ebbd9a 632 if ((p7 = PKCS7_new()) == NULL) {
9311d0c4 633 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
634 goto err;
635 }
636 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
637 goto err;
0f113f3e
MC
638 if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
639 goto err;
640
ca4a494c 641 if (ctx->request->cert_req) {
0f113f3e
MC
642 PKCS7_add_certificate(p7, ctx->signer_cert);
643 if (ctx->certs) {
644 for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
645 X509 *cert = sk_X509_value(ctx->certs, i);
646 PKCS7_add_certificate(p7, cert);
647 }
648 }
649 }
650
75ebbd9a 651 if ((si = PKCS7_add_signature(p7, ctx->signer_cert,
e20b4727 652 ctx->signer_key, ctx->signer_md)) == NULL) {
9311d0c4 653 ERR_raise(ERR_LIB_TS, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
0f113f3e
MC
654 goto err;
655 }
656
0f113f3e
MC
657 oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
658 if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
659 V_ASN1_OBJECT, oid)) {
9311d0c4 660 ERR_raise(ERR_LIB_TS, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
0f113f3e
MC
661 goto err;
662 }
663
0f113f3e 664 certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
d597208c
RL
665 if (ctx->ess_cert_id_digest == NULL
666 || ctx->ess_cert_id_digest == EVP_sha1()) {
e85d19c6 667 if ((sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs, 0)) == NULL)
f0ef20bf
MK
668 goto err;
669
e85d19c6 670 if (!ESS_SIGNING_CERT_add(si, sc)) {
9311d0c4 671 ERR_raise(ERR_LIB_TS, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
f0ef20bf
MK
672 goto err;
673 }
674 } else {
e85d19c6
AI
675 sc2 = ESS_SIGNING_CERT_V2_new_init(ctx->ess_cert_id_digest,
676 ctx->signer_cert, certs, 0);
f0ef20bf
MK
677 if (sc2 == NULL)
678 goto err;
679
e85d19c6 680 if (!ESS_SIGNING_CERT_V2_add(si, sc2)) {
9311d0c4 681 ERR_raise(ERR_LIB_TS, TS_R_ESS_ADD_SIGNING_CERT_V2_ERROR);
f0ef20bf
MK
682 goto err;
683 }
0f113f3e
MC
684 }
685
9c422b5b 686 if (!ts_TST_INFO_content_new(p7))
0f113f3e 687 goto err;
75ebbd9a 688 if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
9311d0c4 689 ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
690 goto err;
691 }
0f113f3e 692 if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
9311d0c4 693 ERR_raise(ERR_LIB_TS, TS_R_TS_DATASIGN);
0f113f3e
MC
694 goto err;
695 }
0f113f3e 696 if (!PKCS7_dataFinal(p7, p7bio)) {
9311d0c4 697 ERR_raise(ERR_LIB_TS, TS_R_TS_DATASIGN);
0f113f3e
MC
698 goto err;
699 }
0f113f3e
MC
700 TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
701 p7 = NULL; /* Ownership is lost. */
702 ctx->tst_info = NULL; /* Ownership is lost. */
703
704 ret = 1;
c7235be6 705 err:
0f113f3e
MC
706 if (!ret)
707 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
708 "Error during signature "
709 "generation.");
710 BIO_free_all(p7bio);
f0ef20bf 711 ESS_SIGNING_CERT_V2_free(sc2);
0f113f3e
MC
712 ESS_SIGNING_CERT_free(sc);
713 PKCS7_free(p7);
714 return ret;
715}
716
9c422b5b 717static int ts_TST_INFO_content_new(PKCS7 *p7)
0f113f3e
MC
718{
719 PKCS7 *ret = NULL;
720 ASN1_OCTET_STRING *octet_string = NULL;
721
722 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
75ebbd9a 723 if ((ret = PKCS7_new()) == NULL)
0f113f3e 724 goto err;
75ebbd9a 725 if ((ret->d.other = ASN1_TYPE_new()) == NULL)
0f113f3e
MC
726 goto err;
727 ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
75ebbd9a 728 if ((octet_string = ASN1_OCTET_STRING_new()) == NULL)
0f113f3e
MC
729 goto err;
730 ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
731 octet_string = NULL;
732
733 /* Add encapsulated content to signed PKCS7 structure. */
734 if (!PKCS7_set_content(p7, ret))
735 goto err;
736
737 return 1;
c7235be6 738 err:
0f113f3e
MC
739 ASN1_OCTET_STRING_free(octet_string);
740 PKCS7_free(ret);
741 return 0;
742}
c7235be6 743
f0ef20bf
MK
744static ASN1_GENERALIZEDTIME *TS_RESP_set_genTime_with_precision(
745 ASN1_GENERALIZEDTIME *asn1_time, long sec, long usec,
746 unsigned precision)
0f113f3e
MC
747{
748 time_t time_sec = (time_t)sec;
98c03302 749 struct tm *tm = NULL, tm_result;
0f113f3e
MC
750 char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
751 char *p = genTime_str;
752 char *p_end = genTime_str + sizeof(genTime_str);
753
754 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
755 goto err;
756
98c03302 757 if ((tm = OPENSSL_gmtime(&time_sec, &tm_result)) == NULL)
0f113f3e
MC
758 goto err;
759
760 /*
761 * Put "genTime_str" in GeneralizedTime format. We work around the
762 * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
763 * NOT include fractional seconds") and OpenSSL related functions to
764 * meet the rfc3161 requirement: "GeneralizedTime syntax can include
765 * fraction-of-second details".
766 */
767 p += BIO_snprintf(p, p_end - p,
768 "%04d%02d%02d%02d%02d%02d",
769 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
770 tm->tm_hour, tm->tm_min, tm->tm_sec);
771 if (precision > 0) {
0f113f3e 772 BIO_snprintf(p, 2 + precision, ".%06ld", usec);
0f113f3e
MC
773 p += strlen(p);
774
775 /*
776 * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
777 * following restrictions for a DER-encoding, which OpenSSL
778 * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
779 * support: "The encoding MUST terminate with a "Z" (which means
780 * "Zulu" time). The decimal point element, if present, MUST be the
781 * point option ".". The fractional-seconds elements, if present,
782 * MUST omit all trailing 0's; if the elements correspond to 0, they
783 * MUST be wholly omitted, and the decimal point element also MUST be
784 * omitted."
785 */
786 /*
787 * Remove trailing zeros. The dot guarantees the exit condition of
788 * this loop even if all the digits are zero.
789 */
790 while (*--p == '0')
18cd23df 791 continue;
0f113f3e
MC
792 if (*p != '.')
793 ++p;
794 }
0f113f3e
MC
795 *p++ = 'Z';
796 *p++ = '\0';
797
75ebbd9a
RS
798 if (asn1_time == NULL
799 && (asn1_time = ASN1_GENERALIZEDTIME_new()) == NULL)
0f113f3e
MC
800 goto err;
801 if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
802 ASN1_GENERALIZEDTIME_free(asn1_time);
803 goto err;
804 }
0f113f3e 805 return asn1_time;
18cd23df 806
c7235be6 807 err:
9311d0c4 808 ERR_raise(ERR_LIB_TS, TS_R_COULD_NOT_SET_TIME);
0f113f3e
MC
809 return NULL;
810}
f0ef20bf
MK
811
812int TS_RESP_CTX_set_ess_cert_id_digest(TS_RESP_CTX *ctx, const EVP_MD *md)
813{
814 ctx->ess_cert_id_digest = md;
815 return 1;
816}