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