ts_rsp_sign.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* crypto/ts/ts_resp_sign.c */
  2. /*
  3. * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
  4. * 2002.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include "cryptlib.h"
  60. #if defined(OPENSSL_SYS_UNIX)
  61. # include <sys/time.h>
  62. #endif
  63. #include <openssl/objects.h>
  64. #include <openssl/ts.h>
  65. #include <openssl/pkcs7.h>
  66. /* Private function declarations. */
  67. static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *);
  68. static int def_time_cb(struct TS_resp_ctx *, void *, long *sec, long *usec);
  69. static int def_extension_cb(struct TS_resp_ctx *, X509_EXTENSION *, void *);
  70. static void TS_RESP_CTX_init(TS_RESP_CTX *ctx);
  71. static void TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx);
  72. static int TS_RESP_check_request(TS_RESP_CTX *ctx);
  73. static ASN1_OBJECT *TS_RESP_get_policy(TS_RESP_CTX *ctx);
  74. static TS_TST_INFO *TS_RESP_create_tst_info(TS_RESP_CTX *ctx,
  75. ASN1_OBJECT *policy);
  76. static int TS_RESP_process_extensions(TS_RESP_CTX *ctx);
  77. static int TS_RESP_sign(TS_RESP_CTX *ctx);
  78. static ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert,
  79. STACK_OF(X509) *certs);
  80. static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed);
  81. static int TS_TST_INFO_content_new(PKCS7 *p7);
  82. static int ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc);
  83. static ASN1_GENERALIZEDTIME
  84. *TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *, long, long,
  85. unsigned);
  86. /* Default callbacks for response generation. */
  87. static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *ctx, void *data)
  88. {
  89. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  90. if (!serial)
  91. goto err;
  92. if (!ASN1_INTEGER_set(serial, 1))
  93. goto err;
  94. return serial;
  95. err:
  96. TSerr(TS_F_DEF_SERIAL_CB, ERR_R_MALLOC_FAILURE);
  97. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  98. "Error during serial number generation.");
  99. return NULL;
  100. }
  101. #if defined(OPENSSL_SYS_UNIX)
  102. /* Use the gettimeofday function call. */
  103. static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
  104. long *sec, long *usec)
  105. {
  106. struct timeval tv;
  107. if (gettimeofday(&tv, NULL) != 0) {
  108. TSerr(TS_F_DEF_TIME_CB, TS_R_TIME_SYSCALL_ERROR);
  109. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  110. "Time is not available.");
  111. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
  112. return 0;
  113. }
  114. /* Return time to caller. */
  115. *sec = tv.tv_sec;
  116. *usec = tv.tv_usec;
  117. return 1;
  118. }
  119. #else
  120. /* Use the time function call that provides only seconds precision. */
  121. static int def_time_cb(struct TS_resp_ctx *ctx, void *data,
  122. long *sec, long *usec)
  123. {
  124. time_t t;
  125. if (time(&t) == (time_t)-1) {
  126. TSerr(TS_F_DEF_TIME_CB, TS_R_TIME_SYSCALL_ERROR);
  127. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  128. "Time is not available.");
  129. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
  130. return 0;
  131. }
  132. /* Return time to caller, only second precision. */
  133. *sec = (long)t;
  134. *usec = 0;
  135. return 1;
  136. }
  137. #endif
  138. static int def_extension_cb(struct TS_resp_ctx *ctx, X509_EXTENSION *ext,
  139. void *data)
  140. {
  141. /* No extensions are processed here. */
  142. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  143. "Unsupported extension.");
  144. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_EXTENSION);
  145. return 0;
  146. }
  147. /* TS_RESP_CTX management functions. */
  148. TS_RESP_CTX *TS_RESP_CTX_new()
  149. {
  150. TS_RESP_CTX *ctx;
  151. if (!(ctx = (TS_RESP_CTX *)OPENSSL_malloc(sizeof(TS_RESP_CTX)))) {
  152. TSerr(TS_F_TS_RESP_CTX_NEW, ERR_R_MALLOC_FAILURE);
  153. return NULL;
  154. }
  155. memset(ctx, 0, sizeof(TS_RESP_CTX));
  156. /* Setting default callbacks. */
  157. ctx->serial_cb = def_serial_cb;
  158. ctx->time_cb = def_time_cb;
  159. ctx->extension_cb = def_extension_cb;
  160. return ctx;
  161. }
  162. void TS_RESP_CTX_free(TS_RESP_CTX *ctx)
  163. {
  164. if (!ctx)
  165. return;
  166. X509_free(ctx->signer_cert);
  167. EVP_PKEY_free(ctx->signer_key);
  168. sk_X509_pop_free(ctx->certs, X509_free);
  169. sk_ASN1_OBJECT_pop_free(ctx->policies, ASN1_OBJECT_free);
  170. ASN1_OBJECT_free(ctx->default_policy);
  171. sk_EVP_MD_free(ctx->mds); /* No EVP_MD_free method exists. */
  172. ASN1_INTEGER_free(ctx->seconds);
  173. ASN1_INTEGER_free(ctx->millis);
  174. ASN1_INTEGER_free(ctx->micros);
  175. OPENSSL_free(ctx);
  176. }
  177. int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer)
  178. {
  179. if (X509_check_purpose(signer, X509_PURPOSE_TIMESTAMP_SIGN, 0) != 1) {
  180. TSerr(TS_F_TS_RESP_CTX_SET_SIGNER_CERT,
  181. TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE);
  182. return 0;
  183. }
  184. if (ctx->signer_cert)
  185. X509_free(ctx->signer_cert);
  186. ctx->signer_cert = signer;
  187. CRYPTO_add(&ctx->signer_cert->references, +1, CRYPTO_LOCK_X509);
  188. return 1;
  189. }
  190. int TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key)
  191. {
  192. if (ctx->signer_key)
  193. EVP_PKEY_free(ctx->signer_key);
  194. ctx->signer_key = key;
  195. CRYPTO_add(&ctx->signer_key->references, +1, CRYPTO_LOCK_EVP_PKEY);
  196. return 1;
  197. }
  198. int TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *def_policy)
  199. {
  200. if (ctx->default_policy)
  201. ASN1_OBJECT_free(ctx->default_policy);
  202. if (!(ctx->default_policy = OBJ_dup(def_policy)))
  203. goto err;
  204. return 1;
  205. err:
  206. TSerr(TS_F_TS_RESP_CTX_SET_DEF_POLICY, ERR_R_MALLOC_FAILURE);
  207. return 0;
  208. }
  209. int TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs)
  210. {
  211. if (ctx->certs) {
  212. sk_X509_pop_free(ctx->certs, X509_free);
  213. ctx->certs = NULL;
  214. }
  215. if (!certs)
  216. return 1;
  217. if (!(ctx->certs = X509_chain_up_ref(certs))) {
  218. TSerr(TS_F_TS_RESP_CTX_SET_CERTS, ERR_R_MALLOC_FAILURE);
  219. return 0;
  220. }
  221. return 1;
  222. }
  223. int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *policy)
  224. {
  225. ASN1_OBJECT *copy = NULL;
  226. /* Create new policy stack if necessary. */
  227. if (!ctx->policies && !(ctx->policies = sk_ASN1_OBJECT_new_null()))
  228. goto err;
  229. if (!(copy = OBJ_dup(policy)))
  230. goto err;
  231. if (!sk_ASN1_OBJECT_push(ctx->policies, copy))
  232. goto err;
  233. return 1;
  234. err:
  235. TSerr(TS_F_TS_RESP_CTX_ADD_POLICY, ERR_R_MALLOC_FAILURE);
  236. ASN1_OBJECT_free(copy);
  237. return 0;
  238. }
  239. int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md)
  240. {
  241. /* Create new md stack if necessary. */
  242. if (!ctx->mds && !(ctx->mds = sk_EVP_MD_new_null()))
  243. goto err;
  244. /* Add the shared md, no copy needed. */
  245. if (!sk_EVP_MD_push(ctx->mds, (EVP_MD *)md))
  246. goto err;
  247. return 1;
  248. err:
  249. TSerr(TS_F_TS_RESP_CTX_ADD_MD, ERR_R_MALLOC_FAILURE);
  250. return 0;
  251. }
  252. #define TS_RESP_CTX_accuracy_free(ctx) \
  253. ASN1_INTEGER_free(ctx->seconds); \
  254. ctx->seconds = NULL; \
  255. ASN1_INTEGER_free(ctx->millis); \
  256. ctx->millis = NULL; \
  257. ASN1_INTEGER_free(ctx->micros); \
  258. ctx->micros = NULL;
  259. int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx,
  260. int secs, int millis, int micros)
  261. {
  262. TS_RESP_CTX_accuracy_free(ctx);
  263. if (secs && (!(ctx->seconds = ASN1_INTEGER_new())
  264. || !ASN1_INTEGER_set(ctx->seconds, secs)))
  265. goto err;
  266. if (millis && (!(ctx->millis = ASN1_INTEGER_new())
  267. || !ASN1_INTEGER_set(ctx->millis, millis)))
  268. goto err;
  269. if (micros && (!(ctx->micros = ASN1_INTEGER_new())
  270. || !ASN1_INTEGER_set(ctx->micros, micros)))
  271. goto err;
  272. return 1;
  273. err:
  274. TS_RESP_CTX_accuracy_free(ctx);
  275. TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
  276. return 0;
  277. }
  278. void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags)
  279. {
  280. ctx->flags |= flags;
  281. }
  282. void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data)
  283. {
  284. ctx->serial_cb = cb;
  285. ctx->serial_cb_data = data;
  286. }
  287. void TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data)
  288. {
  289. ctx->time_cb = cb;
  290. ctx->time_cb_data = data;
  291. }
  292. void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx,
  293. TS_extension_cb cb, void *data)
  294. {
  295. ctx->extension_cb = cb;
  296. ctx->extension_cb_data = data;
  297. }
  298. int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx,
  299. int status, const char *text)
  300. {
  301. TS_STATUS_INFO *si = NULL;
  302. ASN1_UTF8STRING *utf8_text = NULL;
  303. int ret = 0;
  304. if (!(si = TS_STATUS_INFO_new()))
  305. goto err;
  306. if (!ASN1_INTEGER_set(si->status, status))
  307. goto err;
  308. if (text) {
  309. if (!(utf8_text = ASN1_UTF8STRING_new())
  310. || !ASN1_STRING_set(utf8_text, text, strlen(text)))
  311. goto err;
  312. if (!si->text && !(si->text = sk_ASN1_UTF8STRING_new_null()))
  313. goto err;
  314. if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text))
  315. goto err;
  316. utf8_text = NULL; /* Ownership is lost. */
  317. }
  318. if (!TS_RESP_set_status_info(ctx->response, si))
  319. goto err;
  320. ret = 1;
  321. err:
  322. if (!ret)
  323. TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
  324. TS_STATUS_INFO_free(si);
  325. ASN1_UTF8STRING_free(utf8_text);
  326. return ret;
  327. }
  328. int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx,
  329. int status, const char *text)
  330. {
  331. int ret = 1;
  332. TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
  333. if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) {
  334. /* Status has not been set, set it now. */
  335. ret = TS_RESP_CTX_set_status_info(ctx, status, text);
  336. }
  337. return ret;
  338. }
  339. int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure)
  340. {
  341. TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
  342. if (!si->failure_info && !(si->failure_info = ASN1_BIT_STRING_new()))
  343. goto err;
  344. if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1))
  345. goto err;
  346. return 1;
  347. err:
  348. TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO, ERR_R_MALLOC_FAILURE);
  349. return 0;
  350. }
  351. TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx)
  352. {
  353. return ctx->request;
  354. }
  355. TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx)
  356. {
  357. return ctx->tst_info;
  358. }
  359. int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx,
  360. unsigned precision)
  361. {
  362. if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
  363. return 0;
  364. ctx->clock_precision_digits = precision;
  365. return 1;
  366. }
  367. /* Main entry method of the response generation. */
  368. TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio)
  369. {
  370. ASN1_OBJECT *policy;
  371. TS_RESP *response;
  372. int result = 0;
  373. TS_RESP_CTX_init(ctx);
  374. /* Creating the response object. */
  375. if (!(ctx->response = TS_RESP_new())) {
  376. TSerr(TS_F_TS_RESP_CREATE_RESPONSE, ERR_R_MALLOC_FAILURE);
  377. goto end;
  378. }
  379. /* Parsing DER request. */
  380. if (!(ctx->request = d2i_TS_REQ_bio(req_bio, NULL))) {
  381. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  382. "Bad request format or " "system error.");
  383. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
  384. goto end;
  385. }
  386. /* Setting default status info. */
  387. if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL))
  388. goto end;
  389. /* Checking the request format. */
  390. if (!TS_RESP_check_request(ctx))
  391. goto end;
  392. /* Checking acceptable policies. */
  393. if (!(policy = TS_RESP_get_policy(ctx)))
  394. goto end;
  395. /* Creating the TS_TST_INFO object. */
  396. if (!(ctx->tst_info = TS_RESP_create_tst_info(ctx, policy)))
  397. goto end;
  398. /* Processing extensions. */
  399. if (!TS_RESP_process_extensions(ctx))
  400. goto end;
  401. /* Generating the signature. */
  402. if (!TS_RESP_sign(ctx))
  403. goto end;
  404. /* Everything was successful. */
  405. result = 1;
  406. end:
  407. if (!result) {
  408. TSerr(TS_F_TS_RESP_CREATE_RESPONSE, TS_R_RESPONSE_SETUP_ERROR);
  409. if (ctx->response != NULL) {
  410. if (TS_RESP_CTX_set_status_info_cond(ctx,
  411. TS_STATUS_REJECTION,
  412. "Error during response "
  413. "generation.") == 0) {
  414. TS_RESP_free(ctx->response);
  415. ctx->response = NULL;
  416. }
  417. }
  418. }
  419. response = ctx->response;
  420. ctx->response = NULL; /* Ownership will be returned to caller. */
  421. TS_RESP_CTX_cleanup(ctx);
  422. return response;
  423. }
  424. /* Initializes the variable part of the context. */
  425. static void TS_RESP_CTX_init(TS_RESP_CTX *ctx)
  426. {
  427. ctx->request = NULL;
  428. ctx->response = NULL;
  429. ctx->tst_info = NULL;
  430. }
  431. /* Cleans up the variable part of the context. */
  432. static void TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx)
  433. {
  434. TS_REQ_free(ctx->request);
  435. ctx->request = NULL;
  436. TS_RESP_free(ctx->response);
  437. ctx->response = NULL;
  438. TS_TST_INFO_free(ctx->tst_info);
  439. ctx->tst_info = NULL;
  440. }
  441. /* Checks the format and content of the request. */
  442. static int TS_RESP_check_request(TS_RESP_CTX *ctx)
  443. {
  444. TS_REQ *request = ctx->request;
  445. TS_MSG_IMPRINT *msg_imprint;
  446. X509_ALGOR *md_alg;
  447. int md_alg_id;
  448. const ASN1_OCTET_STRING *digest;
  449. EVP_MD *md = NULL;
  450. int i;
  451. /* Checking request version. */
  452. if (TS_REQ_get_version(request) != 1) {
  453. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  454. "Bad request version.");
  455. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST);
  456. return 0;
  457. }
  458. /* Checking message digest algorithm. */
  459. msg_imprint = TS_REQ_get_msg_imprint(request);
  460. md_alg = TS_MSG_IMPRINT_get_algo(msg_imprint);
  461. md_alg_id = OBJ_obj2nid(md_alg->algorithm);
  462. for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) {
  463. EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i);
  464. if (md_alg_id == EVP_MD_type(current_md))
  465. md = current_md;
  466. }
  467. if (!md) {
  468. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  469. "Message digest algorithm is "
  470. "not supported.");
  471. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
  472. return 0;
  473. }
  474. /* No message digest takes parameter. */
  475. if (md_alg->parameter && ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
  476. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  477. "Superfluous message digest "
  478. "parameter.");
  479. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
  480. return 0;
  481. }
  482. /* Checking message digest size. */
  483. digest = TS_MSG_IMPRINT_get_msg(msg_imprint);
  484. if (digest->length != EVP_MD_size(md)) {
  485. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  486. "Bad message digest.");
  487. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
  488. return 0;
  489. }
  490. return 1;
  491. }
  492. /* Returns the TSA policy based on the requested and acceptable policies. */
  493. static ASN1_OBJECT *TS_RESP_get_policy(TS_RESP_CTX *ctx)
  494. {
  495. ASN1_OBJECT *requested = TS_REQ_get_policy_id(ctx->request);
  496. ASN1_OBJECT *policy = NULL;
  497. int i;
  498. if (ctx->default_policy == NULL) {
  499. TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_INVALID_NULL_POINTER);
  500. return NULL;
  501. }
  502. /*
  503. * Return the default policy if none is requested or the default is
  504. * requested.
  505. */
  506. if (!requested || !OBJ_cmp(requested, ctx->default_policy))
  507. policy = ctx->default_policy;
  508. /* Check if the policy is acceptable. */
  509. for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
  510. ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
  511. if (!OBJ_cmp(requested, current))
  512. policy = current;
  513. }
  514. if (!policy) {
  515. TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_UNACCEPTABLE_POLICY);
  516. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  517. "Requested policy is not " "supported.");
  518. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
  519. }
  520. return policy;
  521. }
  522. /* Creates the TS_TST_INFO object based on the settings of the context. */
  523. static TS_TST_INFO *TS_RESP_create_tst_info(TS_RESP_CTX *ctx,
  524. ASN1_OBJECT *policy)
  525. {
  526. int result = 0;
  527. TS_TST_INFO *tst_info = NULL;
  528. ASN1_INTEGER *serial = NULL;
  529. ASN1_GENERALIZEDTIME *asn1_time = NULL;
  530. long sec, usec;
  531. TS_ACCURACY *accuracy = NULL;
  532. const ASN1_INTEGER *nonce;
  533. GENERAL_NAME *tsa_name = NULL;
  534. if (!(tst_info = TS_TST_INFO_new()))
  535. goto end;
  536. if (!TS_TST_INFO_set_version(tst_info, 1))
  537. goto end;
  538. if (!TS_TST_INFO_set_policy_id(tst_info, policy))
  539. goto end;
  540. if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
  541. goto end;
  542. if (!(serial = (*ctx->serial_cb) (ctx, ctx->serial_cb_data))
  543. || !TS_TST_INFO_set_serial(tst_info, serial))
  544. goto end;
  545. if (!(*ctx->time_cb) (ctx, ctx->time_cb_data, &sec, &usec)
  546. || !(asn1_time = TS_RESP_set_genTime_with_precision(NULL,
  547. sec, usec,
  548. ctx->clock_precision_digits))
  549. || !TS_TST_INFO_set_time(tst_info, asn1_time))
  550. goto end;
  551. /* Setting accuracy if needed. */
  552. if ((ctx->seconds || ctx->millis || ctx->micros)
  553. && !(accuracy = TS_ACCURACY_new()))
  554. goto end;
  555. if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
  556. goto end;
  557. if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
  558. goto end;
  559. if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
  560. goto end;
  561. if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
  562. goto end;
  563. /* Setting ordering. */
  564. if ((ctx->flags & TS_ORDERING)
  565. && !TS_TST_INFO_set_ordering(tst_info, 1))
  566. goto end;
  567. /* Setting nonce if needed. */
  568. if ((nonce = TS_REQ_get_nonce(ctx->request)) != NULL
  569. && !TS_TST_INFO_set_nonce(tst_info, nonce))
  570. goto end;
  571. /* Setting TSA name to subject of signer certificate. */
  572. if (ctx->flags & TS_TSA_NAME) {
  573. if (!(tsa_name = GENERAL_NAME_new()))
  574. goto end;
  575. tsa_name->type = GEN_DIRNAME;
  576. tsa_name->d.dirn =
  577. X509_NAME_dup(ctx->signer_cert->cert_info->subject);
  578. if (!tsa_name->d.dirn)
  579. goto end;
  580. if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
  581. goto end;
  582. }
  583. result = 1;
  584. end:
  585. if (!result) {
  586. TS_TST_INFO_free(tst_info);
  587. tst_info = NULL;
  588. TSerr(TS_F_TS_RESP_CREATE_TST_INFO, TS_R_TST_INFO_SETUP_ERROR);
  589. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  590. "Error during TSTInfo "
  591. "generation.");
  592. }
  593. GENERAL_NAME_free(tsa_name);
  594. TS_ACCURACY_free(accuracy);
  595. ASN1_GENERALIZEDTIME_free(asn1_time);
  596. ASN1_INTEGER_free(serial);
  597. return tst_info;
  598. }
  599. /* Processing the extensions of the request. */
  600. static int TS_RESP_process_extensions(TS_RESP_CTX *ctx)
  601. {
  602. STACK_OF(X509_EXTENSION) *exts = TS_REQ_get_exts(ctx->request);
  603. int i;
  604. int ok = 1;
  605. for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
  606. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  607. /*
  608. * XXXXX The last argument was previously (void *)ctx->extension_cb,
  609. * but ISO C doesn't permit converting a function pointer to void *.
  610. * For lack of better information, I'm placing a NULL there instead.
  611. * The callback can pick its own address out from the ctx anyway...
  612. */
  613. ok = (*ctx->extension_cb) (ctx, ext, NULL);
  614. }
  615. return ok;
  616. }
  617. /* Functions for signing the TS_TST_INFO structure of the context. */
  618. static int TS_RESP_sign(TS_RESP_CTX *ctx)
  619. {
  620. int ret = 0;
  621. PKCS7 *p7 = NULL;
  622. PKCS7_SIGNER_INFO *si;
  623. STACK_OF(X509) *certs; /* Certificates to include in sc. */
  624. ESS_SIGNING_CERT *sc = NULL;
  625. ASN1_OBJECT *oid;
  626. BIO *p7bio = NULL;
  627. int i;
  628. /* Check if signcert and pkey match. */
  629. if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
  630. TSerr(TS_F_TS_RESP_SIGN, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  631. goto err;
  632. }
  633. /* Create a new PKCS7 signed object. */
  634. if (!(p7 = PKCS7_new())) {
  635. TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
  636. goto err;
  637. }
  638. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  639. goto err;
  640. /* Force SignedData version to be 3 instead of the default 1. */
  641. if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
  642. goto err;
  643. /* Add signer certificate and optional certificate chain. */
  644. if (TS_REQ_get_cert_req(ctx->request)) {
  645. PKCS7_add_certificate(p7, ctx->signer_cert);
  646. if (ctx->certs) {
  647. for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
  648. X509 *cert = sk_X509_value(ctx->certs, i);
  649. PKCS7_add_certificate(p7, cert);
  650. }
  651. }
  652. }
  653. /* Add a new signer info. */
  654. if (!(si = PKCS7_add_signature(p7, ctx->signer_cert,
  655. ctx->signer_key, EVP_sha1()))) {
  656. TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
  657. goto err;
  658. }
  659. /* Add content type signed attribute to the signer info. */
  660. oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  661. if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  662. V_ASN1_OBJECT, oid)) {
  663. TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
  664. goto err;
  665. }
  666. /*
  667. * Create the ESS SigningCertificate attribute which contains the signer
  668. * certificate id and optionally the certificate chain.
  669. */
  670. certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
  671. if (!(sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs)))
  672. goto err;
  673. /* Add SigningCertificate signed attribute to the signer info. */
  674. if (!ESS_add_signing_cert(si, sc)) {
  675. TSerr(TS_F_TS_RESP_SIGN, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
  676. goto err;
  677. }
  678. /* Add a new empty NID_id_smime_ct_TSTInfo encapsulated content. */
  679. if (!TS_TST_INFO_content_new(p7))
  680. goto err;
  681. /* Add the DER encoded tst_info to the PKCS7 structure. */
  682. if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
  683. TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
  684. goto err;
  685. }
  686. /* Convert tst_info to DER. */
  687. if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
  688. TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
  689. goto err;
  690. }
  691. /* Create the signature and add it to the signer info. */
  692. if (!PKCS7_dataFinal(p7, p7bio)) {
  693. TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
  694. goto err;
  695. }
  696. /* Set new PKCS7 and TST_INFO objects. */
  697. TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
  698. p7 = NULL; /* Ownership is lost. */
  699. ctx->tst_info = NULL; /* Ownership is lost. */
  700. ret = 1;
  701. err:
  702. if (!ret)
  703. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  704. "Error during signature "
  705. "generation.");
  706. BIO_free_all(p7bio);
  707. ESS_SIGNING_CERT_free(sc);
  708. PKCS7_free(p7);
  709. return ret;
  710. }
  711. static ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert,
  712. STACK_OF(X509) *certs)
  713. {
  714. ESS_CERT_ID *cid;
  715. ESS_SIGNING_CERT *sc = NULL;
  716. int i;
  717. /* Creating the ESS_CERT_ID stack. */
  718. if (!(sc = ESS_SIGNING_CERT_new()))
  719. goto err;
  720. if (!sc->cert_ids && !(sc->cert_ids = sk_ESS_CERT_ID_new_null()))
  721. goto err;
  722. /* Adding the signing certificate id. */
  723. if (!(cid = ESS_CERT_ID_new_init(signcert, 0))
  724. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  725. goto err;
  726. /* Adding the certificate chain ids. */
  727. for (i = 0; i < sk_X509_num(certs); ++i) {
  728. X509 *cert = sk_X509_value(certs, i);
  729. if (!(cid = ESS_CERT_ID_new_init(cert, 1))
  730. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  731. goto err;
  732. }
  733. return sc;
  734. err:
  735. ESS_SIGNING_CERT_free(sc);
  736. TSerr(TS_F_ESS_SIGNING_CERT_NEW_INIT, ERR_R_MALLOC_FAILURE);
  737. return NULL;
  738. }
  739. static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed)
  740. {
  741. ESS_CERT_ID *cid = NULL;
  742. GENERAL_NAME *name = NULL;
  743. /* Recompute SHA1 hash of certificate if necessary (side effect). */
  744. X509_check_purpose(cert, -1, 0);
  745. if (!(cid = ESS_CERT_ID_new()))
  746. goto err;
  747. if (!ASN1_OCTET_STRING_set(cid->hash, cert->sha1_hash,
  748. sizeof(cert->sha1_hash)))
  749. goto err;
  750. /* Setting the issuer/serial if requested. */
  751. if (issuer_needed) {
  752. /* Creating issuer/serial structure. */
  753. if (!cid->issuer_serial
  754. && !(cid->issuer_serial = ESS_ISSUER_SERIAL_new()))
  755. goto err;
  756. /* Creating general name from the certificate issuer. */
  757. if (!(name = GENERAL_NAME_new()))
  758. goto err;
  759. name->type = GEN_DIRNAME;
  760. if (!(name->d.dirn = X509_NAME_dup(cert->cert_info->issuer)))
  761. goto err;
  762. if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
  763. goto err;
  764. name = NULL; /* Ownership is lost. */
  765. /* Setting the serial number. */
  766. ASN1_INTEGER_free(cid->issuer_serial->serial);
  767. if (!(cid->issuer_serial->serial =
  768. ASN1_INTEGER_dup(cert->cert_info->serialNumber)))
  769. goto err;
  770. }
  771. return cid;
  772. err:
  773. GENERAL_NAME_free(name);
  774. ESS_CERT_ID_free(cid);
  775. TSerr(TS_F_ESS_CERT_ID_NEW_INIT, ERR_R_MALLOC_FAILURE);
  776. return NULL;
  777. }
  778. static int TS_TST_INFO_content_new(PKCS7 *p7)
  779. {
  780. PKCS7 *ret = NULL;
  781. ASN1_OCTET_STRING *octet_string = NULL;
  782. /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
  783. if (!(ret = PKCS7_new()))
  784. goto err;
  785. if (!(ret->d.other = ASN1_TYPE_new()))
  786. goto err;
  787. ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  788. if (!(octet_string = ASN1_OCTET_STRING_new()))
  789. goto err;
  790. ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
  791. octet_string = NULL;
  792. /* Add encapsulated content to signed PKCS7 structure. */
  793. if (!PKCS7_set_content(p7, ret))
  794. goto err;
  795. return 1;
  796. err:
  797. ASN1_OCTET_STRING_free(octet_string);
  798. PKCS7_free(ret);
  799. return 0;
  800. }
  801. static int ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc)
  802. {
  803. ASN1_STRING *seq = NULL;
  804. unsigned char *p, *pp = NULL;
  805. int len;
  806. len = i2d_ESS_SIGNING_CERT(sc, NULL);
  807. if (!(pp = (unsigned char *)OPENSSL_malloc(len))) {
  808. TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
  809. goto err;
  810. }
  811. p = pp;
  812. i2d_ESS_SIGNING_CERT(sc, &p);
  813. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
  814. TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
  815. goto err;
  816. }
  817. OPENSSL_free(pp);
  818. pp = NULL;
  819. return PKCS7_add_signed_attribute(si,
  820. NID_id_smime_aa_signingCertificate,
  821. V_ASN1_SEQUENCE, seq);
  822. err:
  823. ASN1_STRING_free(seq);
  824. OPENSSL_free(pp);
  825. return 0;
  826. }
  827. static ASN1_GENERALIZEDTIME
  828. *TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *asn1_time,
  829. long sec, long usec, unsigned precision)
  830. {
  831. time_t time_sec = (time_t)sec;
  832. struct tm *tm = NULL;
  833. char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
  834. char *p = genTime_str;
  835. char *p_end = genTime_str + sizeof(genTime_str);
  836. if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
  837. goto err;
  838. if (!(tm = gmtime(&time_sec)))
  839. goto err;
  840. /*
  841. * Put "genTime_str" in GeneralizedTime format. We work around the
  842. * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
  843. * NOT include fractional seconds") and OpenSSL related functions to
  844. * meet the rfc3161 requirement: "GeneralizedTime syntax can include
  845. * fraction-of-second details".
  846. */
  847. p += BIO_snprintf(p, p_end - p,
  848. "%04d%02d%02d%02d%02d%02d",
  849. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  850. tm->tm_hour, tm->tm_min, tm->tm_sec);
  851. if (precision > 0) {
  852. /* Add fraction of seconds (leave space for dot and null). */
  853. BIO_snprintf(p, 2 + precision, ".%06ld", usec);
  854. /*
  855. * We cannot use the snprintf return value, because it might have
  856. * been truncated.
  857. */
  858. p += strlen(p);
  859. /*
  860. * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
  861. * following restrictions for a DER-encoding, which OpenSSL
  862. * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
  863. * support: "The encoding MUST terminate with a "Z" (which means
  864. * "Zulu" time). The decimal point element, if present, MUST be the
  865. * point option ".". The fractional-seconds elements, if present,
  866. * MUST omit all trailing 0's; if the elements correspond to 0, they
  867. * MUST be wholly omitted, and the decimal point element also MUST be
  868. * omitted."
  869. */
  870. /*
  871. * Remove trailing zeros. The dot guarantees the exit condition of
  872. * this loop even if all the digits are zero.
  873. */
  874. while (*--p == '0')
  875. /*
  876. * empty
  877. */ ;
  878. /* p points to either the dot or the last non-zero digit. */
  879. if (*p != '.')
  880. ++p;
  881. }
  882. /* Add the trailing Z and the terminating null. */
  883. *p++ = 'Z';
  884. *p++ = '\0';
  885. /* Now call OpenSSL to check and set our genTime value */
  886. if (!asn1_time && !(asn1_time = M_ASN1_GENERALIZEDTIME_new()))
  887. goto err;
  888. if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
  889. ASN1_GENERALIZEDTIME_free(asn1_time);
  890. goto err;
  891. }
  892. return asn1_time;
  893. err:
  894. TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION, TS_R_COULD_NOT_SET_TIME);
  895. return NULL;
  896. }