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