ts_rsp_sign.c 27 KB

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