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