2
0

ts_rsp_sign.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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, md_size;
  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. md_size = EVP_MD_get_size(md);
  414. if (md_size <= 0)
  415. return 0;
  416. if (md_alg->parameter && ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
  417. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  418. "Superfluous message digest "
  419. "parameter.");
  420. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
  421. return 0;
  422. }
  423. digest = msg_imprint->hashed_msg;
  424. if (digest->length != md_size) {
  425. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  426. "Bad message digest.");
  427. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
  428. return 0;
  429. }
  430. return 1;
  431. }
  432. /* Returns the TSA policy based on the requested and acceptable policies. */
  433. static ASN1_OBJECT *ts_RESP_get_policy(TS_RESP_CTX *ctx)
  434. {
  435. ASN1_OBJECT *requested = ctx->request->policy_id;
  436. ASN1_OBJECT *policy = NULL;
  437. int i;
  438. if (ctx->default_policy == NULL) {
  439. ERR_raise(ERR_LIB_TS, TS_R_INVALID_NULL_POINTER);
  440. return NULL;
  441. }
  442. if (!requested || !OBJ_cmp(requested, ctx->default_policy))
  443. policy = ctx->default_policy;
  444. /* Check if the policy is acceptable. */
  445. for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
  446. ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
  447. if (!OBJ_cmp(requested, current))
  448. policy = current;
  449. }
  450. if (policy == NULL) {
  451. ERR_raise(ERR_LIB_TS, TS_R_UNACCEPTABLE_POLICY);
  452. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  453. "Requested policy is not " "supported.");
  454. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
  455. }
  456. return policy;
  457. }
  458. /* Creates the TS_TST_INFO object based on the settings of the context. */
  459. static TS_TST_INFO *ts_RESP_create_tst_info(TS_RESP_CTX *ctx,
  460. ASN1_OBJECT *policy)
  461. {
  462. int result = 0;
  463. TS_TST_INFO *tst_info = NULL;
  464. ASN1_INTEGER *serial = NULL;
  465. ASN1_GENERALIZEDTIME *asn1_time = NULL;
  466. long sec, usec;
  467. TS_ACCURACY *accuracy = NULL;
  468. const ASN1_INTEGER *nonce;
  469. GENERAL_NAME *tsa_name = NULL;
  470. if ((tst_info = TS_TST_INFO_new()) == NULL)
  471. goto end;
  472. if (!TS_TST_INFO_set_version(tst_info, 1))
  473. goto end;
  474. if (!TS_TST_INFO_set_policy_id(tst_info, policy))
  475. goto end;
  476. if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
  477. goto end;
  478. if ((serial = ctx->serial_cb(ctx, ctx->serial_cb_data)) == NULL
  479. || !TS_TST_INFO_set_serial(tst_info, serial))
  480. goto end;
  481. if (!ctx->time_cb(ctx, ctx->time_cb_data, &sec, &usec)
  482. || (asn1_time =
  483. TS_RESP_set_genTime_with_precision(NULL, sec, usec,
  484. ctx->clock_precision_digits)) == NULL
  485. || !TS_TST_INFO_set_time(tst_info, asn1_time))
  486. goto end;
  487. if ((ctx->seconds || ctx->millis || ctx->micros)
  488. && (accuracy = TS_ACCURACY_new()) == NULL)
  489. goto end;
  490. if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
  491. goto end;
  492. if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
  493. goto end;
  494. if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
  495. goto end;
  496. if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
  497. goto end;
  498. if ((ctx->flags & TS_ORDERING)
  499. && !TS_TST_INFO_set_ordering(tst_info, 1))
  500. goto end;
  501. if ((nonce = ctx->request->nonce) != NULL
  502. && !TS_TST_INFO_set_nonce(tst_info, nonce))
  503. goto end;
  504. if (ctx->flags & TS_TSA_NAME) {
  505. if ((tsa_name = GENERAL_NAME_new()) == NULL)
  506. goto end;
  507. tsa_name->type = GEN_DIRNAME;
  508. tsa_name->d.dirn =
  509. X509_NAME_dup(X509_get_subject_name(ctx->signer_cert));
  510. if (!tsa_name->d.dirn)
  511. goto end;
  512. if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
  513. goto end;
  514. }
  515. result = 1;
  516. end:
  517. if (!result) {
  518. TS_TST_INFO_free(tst_info);
  519. tst_info = NULL;
  520. ERR_raise(ERR_LIB_TS, TS_R_TST_INFO_SETUP_ERROR);
  521. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  522. "Error during TSTInfo "
  523. "generation.");
  524. }
  525. GENERAL_NAME_free(tsa_name);
  526. TS_ACCURACY_free(accuracy);
  527. ASN1_GENERALIZEDTIME_free(asn1_time);
  528. ASN1_INTEGER_free(serial);
  529. return tst_info;
  530. }
  531. /* Processing the extensions of the request. */
  532. static int ts_RESP_process_extensions(TS_RESP_CTX *ctx)
  533. {
  534. STACK_OF(X509_EXTENSION) *exts = ctx->request->extensions;
  535. int i;
  536. int ok = 1;
  537. for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
  538. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  539. /*
  540. * The last argument was previously (void *)ctx->extension_cb,
  541. * but ISO C doesn't permit converting a function pointer to void *.
  542. * For lack of better information, I'm placing a NULL there instead.
  543. * The callback can pick its own address out from the ctx anyway...
  544. */
  545. ok = (*ctx->extension_cb) (ctx, ext, NULL);
  546. }
  547. return ok;
  548. }
  549. /* Functions for signing the TS_TST_INFO structure of the context. */
  550. static int ossl_ess_add1_signing_cert(PKCS7_SIGNER_INFO *si,
  551. const ESS_SIGNING_CERT *sc)
  552. {
  553. ASN1_STRING *seq = NULL;
  554. int len = i2d_ESS_SIGNING_CERT(sc, NULL);
  555. unsigned char *p, *pp = OPENSSL_malloc(len);
  556. if (pp == NULL)
  557. return 0;
  558. p = pp;
  559. i2d_ESS_SIGNING_CERT(sc, &p);
  560. if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
  561. ASN1_STRING_free(seq);
  562. OPENSSL_free(pp);
  563. return 0;
  564. }
  565. OPENSSL_free(pp);
  566. return PKCS7_add_signed_attribute(si, NID_id_smime_aa_signingCertificate,
  567. V_ASN1_SEQUENCE, seq);
  568. }
  569. static int ossl_ess_add1_signing_cert_v2(PKCS7_SIGNER_INFO *si,
  570. const ESS_SIGNING_CERT_V2 *sc)
  571. {
  572. ASN1_STRING *seq = NULL;
  573. int len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
  574. unsigned char *p, *pp = OPENSSL_malloc(len);
  575. if (pp == NULL)
  576. return 0;
  577. p = pp;
  578. i2d_ESS_SIGNING_CERT_V2(sc, &p);
  579. if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
  580. ASN1_STRING_free(seq);
  581. OPENSSL_free(pp);
  582. return 0;
  583. }
  584. OPENSSL_free(pp);
  585. return PKCS7_add_signed_attribute(si, NID_id_smime_aa_signingCertificateV2,
  586. V_ASN1_SEQUENCE, seq);
  587. }
  588. static int ts_RESP_sign(TS_RESP_CTX *ctx)
  589. {
  590. int ret = 0;
  591. PKCS7 *p7 = NULL;
  592. PKCS7_SIGNER_INFO *si;
  593. STACK_OF(X509) *certs; /* Certificates to include in sc. */
  594. ESS_SIGNING_CERT_V2 *sc2 = NULL;
  595. ESS_SIGNING_CERT *sc = NULL;
  596. ASN1_OBJECT *oid;
  597. BIO *p7bio = NULL;
  598. int i;
  599. EVP_MD *signer_md = NULL;
  600. if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
  601. ERR_raise(ERR_LIB_TS, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  602. goto err;
  603. }
  604. if ((p7 = PKCS7_new_ex(ctx->libctx, ctx->propq)) == NULL) {
  605. ERR_raise(ERR_LIB_TS, ERR_R_ASN1_LIB);
  606. goto err;
  607. }
  608. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  609. goto err;
  610. if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
  611. goto err;
  612. if (ctx->request->cert_req) {
  613. PKCS7_add_certificate(p7, ctx->signer_cert);
  614. if (ctx->certs) {
  615. for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
  616. X509 *cert = sk_X509_value(ctx->certs, i);
  617. PKCS7_add_certificate(p7, cert);
  618. }
  619. }
  620. }
  621. if (ctx->signer_md == NULL)
  622. signer_md = EVP_MD_fetch(ctx->libctx, "SHA256", ctx->propq);
  623. else if (EVP_MD_get0_provider(ctx->signer_md) == NULL)
  624. signer_md = EVP_MD_fetch(ctx->libctx, EVP_MD_get0_name(ctx->signer_md),
  625. ctx->propq);
  626. else
  627. signer_md = (EVP_MD *)ctx->signer_md;
  628. if ((si = PKCS7_add_signature(p7, ctx->signer_cert,
  629. ctx->signer_key, signer_md)) == NULL) {
  630. ERR_raise(ERR_LIB_TS, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
  631. goto err;
  632. }
  633. oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  634. if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  635. V_ASN1_OBJECT, oid)) {
  636. ERR_raise(ERR_LIB_TS, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
  637. goto err;
  638. }
  639. certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
  640. if (ctx->ess_cert_id_digest == NULL
  641. || EVP_MD_is_a(ctx->ess_cert_id_digest, SN_sha1)) {
  642. if ((sc = OSSL_ESS_signing_cert_new_init(ctx->signer_cert,
  643. certs, 0)) == NULL)
  644. goto err;
  645. if (!ossl_ess_add1_signing_cert(si, sc)) {
  646. ERR_raise(ERR_LIB_TS, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
  647. goto err;
  648. }
  649. } else {
  650. sc2 = OSSL_ESS_signing_cert_v2_new_init(ctx->ess_cert_id_digest,
  651. ctx->signer_cert, certs, 0);
  652. if (sc2 == NULL)
  653. goto err;
  654. if (!ossl_ess_add1_signing_cert_v2(si, sc2)) {
  655. ERR_raise(ERR_LIB_TS, TS_R_ESS_ADD_SIGNING_CERT_V2_ERROR);
  656. goto err;
  657. }
  658. }
  659. if (!ts_TST_INFO_content_new(p7))
  660. goto err;
  661. if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
  662. ERR_raise(ERR_LIB_TS, ERR_R_PKCS7_LIB);
  663. goto err;
  664. }
  665. if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
  666. ERR_raise(ERR_LIB_TS, TS_R_TS_DATASIGN);
  667. goto err;
  668. }
  669. if (!PKCS7_dataFinal(p7, p7bio)) {
  670. ERR_raise(ERR_LIB_TS, TS_R_TS_DATASIGN);
  671. goto err;
  672. }
  673. TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
  674. p7 = NULL; /* Ownership is lost. */
  675. ctx->tst_info = NULL; /* Ownership is lost. */
  676. ret = 1;
  677. err:
  678. if (signer_md != ctx->signer_md)
  679. EVP_MD_free(signer_md);
  680. if (!ret)
  681. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  682. "Error during signature "
  683. "generation.");
  684. BIO_free_all(p7bio);
  685. ESS_SIGNING_CERT_V2_free(sc2);
  686. ESS_SIGNING_CERT_free(sc);
  687. PKCS7_free(p7);
  688. return ret;
  689. }
  690. static int ts_TST_INFO_content_new(PKCS7 *p7)
  691. {
  692. PKCS7 *ret = NULL;
  693. ASN1_OCTET_STRING *octet_string = NULL;
  694. /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
  695. if ((ret = PKCS7_new()) == NULL)
  696. goto err;
  697. if ((ret->d.other = ASN1_TYPE_new()) == NULL)
  698. goto err;
  699. ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  700. if ((octet_string = ASN1_OCTET_STRING_new()) == NULL)
  701. goto err;
  702. ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
  703. octet_string = NULL;
  704. /* Add encapsulated content to signed PKCS7 structure. */
  705. if (!PKCS7_set_content(p7, ret))
  706. goto err;
  707. return 1;
  708. err:
  709. ASN1_OCTET_STRING_free(octet_string);
  710. PKCS7_free(ret);
  711. return 0;
  712. }
  713. static ASN1_GENERALIZEDTIME *TS_RESP_set_genTime_with_precision(
  714. ASN1_GENERALIZEDTIME *asn1_time, long sec, long usec,
  715. unsigned precision)
  716. {
  717. time_t time_sec = (time_t)sec;
  718. struct tm *tm = NULL, tm_result;
  719. char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
  720. char *p = genTime_str;
  721. char *p_end = genTime_str + sizeof(genTime_str);
  722. if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
  723. goto err;
  724. if ((tm = OPENSSL_gmtime(&time_sec, &tm_result)) == NULL)
  725. goto err;
  726. /*
  727. * Put "genTime_str" in GeneralizedTime format. We work around the
  728. * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
  729. * NOT include fractional seconds") and OpenSSL related functions to
  730. * meet the rfc3161 requirement: "GeneralizedTime syntax can include
  731. * fraction-of-second details".
  732. */
  733. p += BIO_snprintf(p, p_end - p,
  734. "%04d%02d%02d%02d%02d%02d",
  735. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  736. tm->tm_hour, tm->tm_min, tm->tm_sec);
  737. if (precision > 0) {
  738. BIO_snprintf(p, 2 + precision, ".%06ld", usec);
  739. p += strlen(p);
  740. /*
  741. * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
  742. * following restrictions for a DER-encoding, which OpenSSL
  743. * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
  744. * support: "The encoding MUST terminate with a "Z" (which means
  745. * "Zulu" time). The decimal point element, if present, MUST be the
  746. * point option ".". The fractional-seconds elements, if present,
  747. * MUST omit all trailing 0's; if the elements correspond to 0, they
  748. * MUST be wholly omitted, and the decimal point element also MUST be
  749. * omitted."
  750. */
  751. /*
  752. * Remove trailing zeros. The dot guarantees the exit condition of
  753. * this loop even if all the digits are zero.
  754. */
  755. while (*--p == '0')
  756. continue;
  757. if (*p != '.')
  758. ++p;
  759. }
  760. *p++ = 'Z';
  761. *p++ = '\0';
  762. if (asn1_time == NULL
  763. && (asn1_time = ASN1_GENERALIZEDTIME_new()) == NULL)
  764. goto err;
  765. if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
  766. ASN1_GENERALIZEDTIME_free(asn1_time);
  767. goto err;
  768. }
  769. return asn1_time;
  770. err:
  771. ERR_raise(ERR_LIB_TS, TS_R_COULD_NOT_SET_TIME);
  772. return NULL;
  773. }
  774. int TS_RESP_CTX_set_ess_cert_id_digest(TS_RESP_CTX *ctx, const EVP_MD *md)
  775. {
  776. ctx->ess_cert_id_digest = md;
  777. return 1;
  778. }