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