Coverage Report

Created: 2025-04-22 06:18

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