ts_rsp_sign.c 27 KB

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