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