ts_rsp_sign.c 25 KB

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