ts_rsp_sign.c 32 KB

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