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