ts_rsp_sign.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /* crypto/ts/ts_resp_sign.c */
  2. /*
  3. * Written by Zoltan Glozik ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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. int i;
  212. if (ctx->certs) {
  213. sk_X509_pop_free(ctx->certs, X509_free);
  214. ctx->certs = NULL;
  215. }
  216. if (!certs)
  217. return 1;
  218. if (!(ctx->certs = sk_X509_dup(certs))) {
  219. TSerr(TS_F_TS_RESP_CTX_SET_CERTS, ERR_R_MALLOC_FAILURE);
  220. return 0;
  221. }
  222. for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
  223. X509 *cert = sk_X509_value(ctx->certs, i);
  224. CRYPTO_add(&cert->references, +1, CRYPTO_LOCK_X509);
  225. }
  226. return 1;
  227. }
  228. int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *policy)
  229. {
  230. ASN1_OBJECT *copy = NULL;
  231. /* Create new policy stack if necessary. */
  232. if (!ctx->policies && !(ctx->policies = sk_ASN1_OBJECT_new_null()))
  233. goto err;
  234. if (!(copy = OBJ_dup(policy)))
  235. goto err;
  236. if (!sk_ASN1_OBJECT_push(ctx->policies, copy))
  237. goto err;
  238. return 1;
  239. err:
  240. TSerr(TS_F_TS_RESP_CTX_ADD_POLICY, ERR_R_MALLOC_FAILURE);
  241. ASN1_OBJECT_free(copy);
  242. return 0;
  243. }
  244. int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md)
  245. {
  246. /* Create new md stack if necessary. */
  247. if (!ctx->mds && !(ctx->mds = sk_EVP_MD_new_null()))
  248. goto err;
  249. /* Add the shared md, no copy needed. */
  250. if (!sk_EVP_MD_push(ctx->mds, (EVP_MD *)md))
  251. goto err;
  252. return 1;
  253. err:
  254. TSerr(TS_F_TS_RESP_CTX_ADD_MD, ERR_R_MALLOC_FAILURE);
  255. return 0;
  256. }
  257. #define TS_RESP_CTX_accuracy_free(ctx) \
  258. ASN1_INTEGER_free(ctx->seconds); \
  259. ctx->seconds = NULL; \
  260. ASN1_INTEGER_free(ctx->millis); \
  261. ctx->millis = NULL; \
  262. ASN1_INTEGER_free(ctx->micros); \
  263. ctx->micros = NULL;
  264. int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx,
  265. int secs, int millis, int micros)
  266. {
  267. TS_RESP_CTX_accuracy_free(ctx);
  268. if (secs && (!(ctx->seconds = ASN1_INTEGER_new())
  269. || !ASN1_INTEGER_set(ctx->seconds, secs)))
  270. goto err;
  271. if (millis && (!(ctx->millis = ASN1_INTEGER_new())
  272. || !ASN1_INTEGER_set(ctx->millis, millis)))
  273. goto err;
  274. if (micros && (!(ctx->micros = ASN1_INTEGER_new())
  275. || !ASN1_INTEGER_set(ctx->micros, micros)))
  276. goto err;
  277. return 1;
  278. err:
  279. TS_RESP_CTX_accuracy_free(ctx);
  280. TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
  281. return 0;
  282. }
  283. void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags)
  284. {
  285. ctx->flags |= flags;
  286. }
  287. void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data)
  288. {
  289. ctx->serial_cb = cb;
  290. ctx->serial_cb_data = data;
  291. }
  292. void TS_RESP_CTX_set_time_cb(TS_RESP_CTX *ctx, TS_time_cb cb, void *data)
  293. {
  294. ctx->time_cb = cb;
  295. ctx->time_cb_data = data;
  296. }
  297. void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx,
  298. TS_extension_cb cb, void *data)
  299. {
  300. ctx->extension_cb = cb;
  301. ctx->extension_cb_data = data;
  302. }
  303. int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx,
  304. int status, const char *text)
  305. {
  306. TS_STATUS_INFO *si = NULL;
  307. ASN1_UTF8STRING *utf8_text = NULL;
  308. int ret = 0;
  309. if (!(si = TS_STATUS_INFO_new()))
  310. goto err;
  311. if (!ASN1_INTEGER_set(si->status, status))
  312. goto err;
  313. if (text) {
  314. if (!(utf8_text = ASN1_UTF8STRING_new())
  315. || !ASN1_STRING_set(utf8_text, text, strlen(text)))
  316. goto err;
  317. if (!si->text && !(si->text = sk_ASN1_UTF8STRING_new_null()))
  318. goto err;
  319. if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text))
  320. goto err;
  321. utf8_text = NULL; /* Ownership is lost. */
  322. }
  323. if (!TS_RESP_set_status_info(ctx->response, si))
  324. goto err;
  325. ret = 1;
  326. err:
  327. if (!ret)
  328. TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
  329. TS_STATUS_INFO_free(si);
  330. ASN1_UTF8STRING_free(utf8_text);
  331. return ret;
  332. }
  333. int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx,
  334. int status, const char *text)
  335. {
  336. int ret = 1;
  337. TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
  338. if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) {
  339. /* Status has not been set, set it now. */
  340. ret = TS_RESP_CTX_set_status_info(ctx, status, text);
  341. }
  342. return ret;
  343. }
  344. int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure)
  345. {
  346. TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
  347. if (!si->failure_info && !(si->failure_info = ASN1_BIT_STRING_new()))
  348. goto err;
  349. if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1))
  350. goto err;
  351. return 1;
  352. err:
  353. TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO, ERR_R_MALLOC_FAILURE);
  354. return 0;
  355. }
  356. TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx)
  357. {
  358. return ctx->request;
  359. }
  360. TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx)
  361. {
  362. return ctx->tst_info;
  363. }
  364. int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx,
  365. unsigned precision)
  366. {
  367. if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
  368. return 0;
  369. ctx->clock_precision_digits = precision;
  370. return 1;
  371. }
  372. /* Main entry method of the response generation. */
  373. TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio)
  374. {
  375. ASN1_OBJECT *policy;
  376. TS_RESP *response;
  377. int result = 0;
  378. TS_RESP_CTX_init(ctx);
  379. /* Creating the response object. */
  380. if (!(ctx->response = TS_RESP_new())) {
  381. TSerr(TS_F_TS_RESP_CREATE_RESPONSE, ERR_R_MALLOC_FAILURE);
  382. goto end;
  383. }
  384. /* Parsing DER request. */
  385. if (!(ctx->request = d2i_TS_REQ_bio(req_bio, NULL))) {
  386. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  387. "Bad request format or " "system error.");
  388. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
  389. goto end;
  390. }
  391. /* Setting default status info. */
  392. if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL))
  393. goto end;
  394. /* Checking the request format. */
  395. if (!TS_RESP_check_request(ctx))
  396. goto end;
  397. /* Checking acceptable policies. */
  398. if (!(policy = TS_RESP_get_policy(ctx)))
  399. goto end;
  400. /* Creating the TS_TST_INFO object. */
  401. if (!(ctx->tst_info = TS_RESP_create_tst_info(ctx, policy)))
  402. goto end;
  403. /* Processing extensions. */
  404. if (!TS_RESP_process_extensions(ctx))
  405. goto end;
  406. /* Generating the signature. */
  407. if (!TS_RESP_sign(ctx))
  408. goto end;
  409. /* Everything was successful. */
  410. result = 1;
  411. end:
  412. if (!result) {
  413. TSerr(TS_F_TS_RESP_CREATE_RESPONSE, TS_R_RESPONSE_SETUP_ERROR);
  414. if (ctx->response != NULL) {
  415. if (TS_RESP_CTX_set_status_info_cond(ctx,
  416. TS_STATUS_REJECTION,
  417. "Error during response "
  418. "generation.") == 0) {
  419. TS_RESP_free(ctx->response);
  420. ctx->response = NULL;
  421. }
  422. }
  423. }
  424. response = ctx->response;
  425. ctx->response = NULL; /* Ownership will be returned to caller. */
  426. TS_RESP_CTX_cleanup(ctx);
  427. return response;
  428. }
  429. /* Initializes the variable part of the context. */
  430. static void TS_RESP_CTX_init(TS_RESP_CTX *ctx)
  431. {
  432. ctx->request = NULL;
  433. ctx->response = NULL;
  434. ctx->tst_info = NULL;
  435. }
  436. /* Cleans up the variable part of the context. */
  437. static void TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx)
  438. {
  439. TS_REQ_free(ctx->request);
  440. ctx->request = NULL;
  441. TS_RESP_free(ctx->response);
  442. ctx->response = NULL;
  443. TS_TST_INFO_free(ctx->tst_info);
  444. ctx->tst_info = NULL;
  445. }
  446. /* Checks the format and content of the request. */
  447. static int TS_RESP_check_request(TS_RESP_CTX *ctx)
  448. {
  449. TS_REQ *request = ctx->request;
  450. TS_MSG_IMPRINT *msg_imprint;
  451. X509_ALGOR *md_alg;
  452. int md_alg_id;
  453. const ASN1_OCTET_STRING *digest;
  454. EVP_MD *md = NULL;
  455. int i;
  456. /* Checking request version. */
  457. if (TS_REQ_get_version(request) != 1) {
  458. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  459. "Bad request version.");
  460. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST);
  461. return 0;
  462. }
  463. /* Checking message digest algorithm. */
  464. msg_imprint = TS_REQ_get_msg_imprint(request);
  465. md_alg = TS_MSG_IMPRINT_get_algo(msg_imprint);
  466. md_alg_id = OBJ_obj2nid(md_alg->algorithm);
  467. for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) {
  468. EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i);
  469. if (md_alg_id == EVP_MD_type(current_md))
  470. md = current_md;
  471. }
  472. if (!md) {
  473. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  474. "Message digest algorithm is "
  475. "not supported.");
  476. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
  477. return 0;
  478. }
  479. /* No message digest takes parameter. */
  480. if (md_alg->parameter && ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
  481. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  482. "Superfluous message digest "
  483. "parameter.");
  484. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
  485. return 0;
  486. }
  487. /* Checking message digest size. */
  488. digest = TS_MSG_IMPRINT_get_msg(msg_imprint);
  489. if (digest->length != EVP_MD_size(md)) {
  490. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  491. "Bad message digest.");
  492. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
  493. return 0;
  494. }
  495. return 1;
  496. }
  497. /* Returns the TSA policy based on the requested and acceptable policies. */
  498. static ASN1_OBJECT *TS_RESP_get_policy(TS_RESP_CTX *ctx)
  499. {
  500. ASN1_OBJECT *requested = TS_REQ_get_policy_id(ctx->request);
  501. ASN1_OBJECT *policy = NULL;
  502. int i;
  503. if (ctx->default_policy == NULL) {
  504. TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_INVALID_NULL_POINTER);
  505. return NULL;
  506. }
  507. /*
  508. * Return the default policy if none is requested or the default is
  509. * requested.
  510. */
  511. if (!requested || !OBJ_cmp(requested, ctx->default_policy))
  512. policy = ctx->default_policy;
  513. /* Check if the policy is acceptable. */
  514. for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
  515. ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
  516. if (!OBJ_cmp(requested, current))
  517. policy = current;
  518. }
  519. if (!policy) {
  520. TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_UNACCEPTABLE_POLICY);
  521. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  522. "Requested policy is not " "supported.");
  523. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
  524. }
  525. return policy;
  526. }
  527. /* Creates the TS_TST_INFO object based on the settings of the context. */
  528. static TS_TST_INFO *TS_RESP_create_tst_info(TS_RESP_CTX *ctx,
  529. ASN1_OBJECT *policy)
  530. {
  531. int result = 0;
  532. TS_TST_INFO *tst_info = NULL;
  533. ASN1_INTEGER *serial = NULL;
  534. ASN1_GENERALIZEDTIME *asn1_time = NULL;
  535. long sec, usec;
  536. TS_ACCURACY *accuracy = NULL;
  537. const ASN1_INTEGER *nonce;
  538. GENERAL_NAME *tsa_name = NULL;
  539. if (!(tst_info = TS_TST_INFO_new()))
  540. goto end;
  541. if (!TS_TST_INFO_set_version(tst_info, 1))
  542. goto end;
  543. if (!TS_TST_INFO_set_policy_id(tst_info, policy))
  544. goto end;
  545. if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
  546. goto end;
  547. if (!(serial = (*ctx->serial_cb) (ctx, ctx->serial_cb_data))
  548. || !TS_TST_INFO_set_serial(tst_info, serial))
  549. goto end;
  550. if (!(*ctx->time_cb) (ctx, ctx->time_cb_data, &sec, &usec)
  551. || !(asn1_time = TS_RESP_set_genTime_with_precision(NULL,
  552. sec, usec,
  553. ctx->clock_precision_digits))
  554. || !TS_TST_INFO_set_time(tst_info, asn1_time))
  555. goto end;
  556. /* Setting accuracy if needed. */
  557. if ((ctx->seconds || ctx->millis || ctx->micros)
  558. && !(accuracy = TS_ACCURACY_new()))
  559. goto end;
  560. if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
  561. goto end;
  562. if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
  563. goto end;
  564. if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
  565. goto end;
  566. if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
  567. goto end;
  568. /* Setting ordering. */
  569. if ((ctx->flags & TS_ORDERING)
  570. && !TS_TST_INFO_set_ordering(tst_info, 1))
  571. goto end;
  572. /* Setting nonce if needed. */
  573. if ((nonce = TS_REQ_get_nonce(ctx->request)) != NULL
  574. && !TS_TST_INFO_set_nonce(tst_info, nonce))
  575. goto end;
  576. /* Setting TSA name to subject of signer certificate. */
  577. if (ctx->flags & TS_TSA_NAME) {
  578. if (!(tsa_name = GENERAL_NAME_new()))
  579. goto end;
  580. tsa_name->type = GEN_DIRNAME;
  581. tsa_name->d.dirn =
  582. X509_NAME_dup(ctx->signer_cert->cert_info->subject);
  583. if (!tsa_name->d.dirn)
  584. goto end;
  585. if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
  586. goto end;
  587. }
  588. result = 1;
  589. end:
  590. if (!result) {
  591. TS_TST_INFO_free(tst_info);
  592. tst_info = NULL;
  593. TSerr(TS_F_TS_RESP_CREATE_TST_INFO, TS_R_TST_INFO_SETUP_ERROR);
  594. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  595. "Error during TSTInfo "
  596. "generation.");
  597. }
  598. GENERAL_NAME_free(tsa_name);
  599. TS_ACCURACY_free(accuracy);
  600. ASN1_GENERALIZEDTIME_free(asn1_time);
  601. ASN1_INTEGER_free(serial);
  602. return tst_info;
  603. }
  604. /* Processing the extensions of the request. */
  605. static int TS_RESP_process_extensions(TS_RESP_CTX *ctx)
  606. {
  607. STACK_OF(X509_EXTENSION) *exts = TS_REQ_get_exts(ctx->request);
  608. int i;
  609. int ok = 1;
  610. for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
  611. X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
  612. /*
  613. * XXXXX The last argument was previously (void *)ctx->extension_cb,
  614. * but ISO C doesn't permit converting a function pointer to void *.
  615. * For lack of better information, I'm placing a NULL there instead.
  616. * The callback can pick its own address out from the ctx anyway...
  617. */
  618. ok = (*ctx->extension_cb) (ctx, ext, NULL);
  619. }
  620. return ok;
  621. }
  622. /* Functions for signing the TS_TST_INFO structure of the context. */
  623. static int TS_RESP_sign(TS_RESP_CTX *ctx)
  624. {
  625. int ret = 0;
  626. PKCS7 *p7 = NULL;
  627. PKCS7_SIGNER_INFO *si;
  628. STACK_OF(X509) *certs; /* Certificates to include in sc. */
  629. ESS_SIGNING_CERT *sc = NULL;
  630. ASN1_OBJECT *oid;
  631. BIO *p7bio = NULL;
  632. int i;
  633. /* Check if signcert and pkey match. */
  634. if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
  635. TSerr(TS_F_TS_RESP_SIGN, TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  636. goto err;
  637. }
  638. /* Create a new PKCS7 signed object. */
  639. if (!(p7 = PKCS7_new())) {
  640. TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
  641. goto err;
  642. }
  643. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  644. goto err;
  645. /* Force SignedData version to be 3 instead of the default 1. */
  646. if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
  647. goto err;
  648. /* Add signer certificate and optional certificate chain. */
  649. if (TS_REQ_get_cert_req(ctx->request)) {
  650. PKCS7_add_certificate(p7, ctx->signer_cert);
  651. if (ctx->certs) {
  652. for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
  653. X509 *cert = sk_X509_value(ctx->certs, i);
  654. PKCS7_add_certificate(p7, cert);
  655. }
  656. }
  657. }
  658. /* Add a new signer info. */
  659. if (!(si = PKCS7_add_signature(p7, ctx->signer_cert,
  660. ctx->signer_key, EVP_sha1()))) {
  661. TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
  662. goto err;
  663. }
  664. /* Add content type signed attribute to the signer info. */
  665. oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  666. if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  667. V_ASN1_OBJECT, oid)) {
  668. TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
  669. goto err;
  670. }
  671. /*
  672. * Create the ESS SigningCertificate attribute which contains the signer
  673. * certificate id and optionally the certificate chain.
  674. */
  675. certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
  676. if (!(sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs)))
  677. goto err;
  678. /* Add SigningCertificate signed attribute to the signer info. */
  679. if (!ESS_add_signing_cert(si, sc)) {
  680. TSerr(TS_F_TS_RESP_SIGN, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
  681. goto err;
  682. }
  683. /* Add a new empty NID_id_smime_ct_TSTInfo encapsulated content. */
  684. if (!TS_TST_INFO_content_new(p7))
  685. goto err;
  686. /* Add the DER encoded tst_info to the PKCS7 structure. */
  687. if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
  688. TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
  689. goto err;
  690. }
  691. /* Convert tst_info to DER. */
  692. if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
  693. TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
  694. goto err;
  695. }
  696. /* Create the signature and add it to the signer info. */
  697. if (!PKCS7_dataFinal(p7, p7bio)) {
  698. TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
  699. goto err;
  700. }
  701. /* Set new PKCS7 and TST_INFO objects. */
  702. TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
  703. p7 = NULL; /* Ownership is lost. */
  704. ctx->tst_info = NULL; /* Ownership is lost. */
  705. ret = 1;
  706. err:
  707. if (!ret)
  708. TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
  709. "Error during signature "
  710. "generation.");
  711. BIO_free_all(p7bio);
  712. ESS_SIGNING_CERT_free(sc);
  713. PKCS7_free(p7);
  714. return ret;
  715. }
  716. static ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert,
  717. STACK_OF(X509) *certs)
  718. {
  719. ESS_CERT_ID *cid;
  720. ESS_SIGNING_CERT *sc = NULL;
  721. int i;
  722. /* Creating the ESS_CERT_ID stack. */
  723. if (!(sc = ESS_SIGNING_CERT_new()))
  724. goto err;
  725. if (!sc->cert_ids && !(sc->cert_ids = sk_ESS_CERT_ID_new_null()))
  726. goto err;
  727. /* Adding the signing certificate id. */
  728. if (!(cid = ESS_CERT_ID_new_init(signcert, 0))
  729. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  730. goto err;
  731. /* Adding the certificate chain ids. */
  732. for (i = 0; i < sk_X509_num(certs); ++i) {
  733. X509 *cert = sk_X509_value(certs, i);
  734. if (!(cid = ESS_CERT_ID_new_init(cert, 1))
  735. || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
  736. goto err;
  737. }
  738. return sc;
  739. err:
  740. ESS_SIGNING_CERT_free(sc);
  741. TSerr(TS_F_ESS_SIGNING_CERT_NEW_INIT, ERR_R_MALLOC_FAILURE);
  742. return NULL;
  743. }
  744. static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed)
  745. {
  746. ESS_CERT_ID *cid = NULL;
  747. GENERAL_NAME *name = NULL;
  748. /* Recompute SHA1 hash of certificate if necessary (side effect). */
  749. X509_check_purpose(cert, -1, 0);
  750. if (!(cid = ESS_CERT_ID_new()))
  751. goto err;
  752. if (!ASN1_OCTET_STRING_set(cid->hash, cert->sha1_hash,
  753. sizeof(cert->sha1_hash)))
  754. goto err;
  755. /* Setting the issuer/serial if requested. */
  756. if (issuer_needed) {
  757. /* Creating issuer/serial structure. */
  758. if (!cid->issuer_serial
  759. && !(cid->issuer_serial = ESS_ISSUER_SERIAL_new()))
  760. goto err;
  761. /* Creating general name from the certificate issuer. */
  762. if (!(name = GENERAL_NAME_new()))
  763. goto err;
  764. name->type = GEN_DIRNAME;
  765. if (!(name->d.dirn = X509_NAME_dup(cert->cert_info->issuer)))
  766. goto err;
  767. if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
  768. goto err;
  769. name = NULL; /* Ownership is lost. */
  770. /* Setting the serial number. */
  771. ASN1_INTEGER_free(cid->issuer_serial->serial);
  772. if (!(cid->issuer_serial->serial =
  773. ASN1_INTEGER_dup(cert->cert_info->serialNumber)))
  774. goto err;
  775. }
  776. return cid;
  777. err:
  778. GENERAL_NAME_free(name);
  779. ESS_CERT_ID_free(cid);
  780. TSerr(TS_F_ESS_CERT_ID_NEW_INIT, ERR_R_MALLOC_FAILURE);
  781. return NULL;
  782. }
  783. static int TS_TST_INFO_content_new(PKCS7 *p7)
  784. {
  785. PKCS7 *ret = NULL;
  786. ASN1_OCTET_STRING *octet_string = NULL;
  787. /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
  788. if (!(ret = PKCS7_new()))
  789. goto err;
  790. if (!(ret->d.other = ASN1_TYPE_new()))
  791. goto err;
  792. ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
  793. if (!(octet_string = ASN1_OCTET_STRING_new()))
  794. goto err;
  795. ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
  796. octet_string = NULL;
  797. /* Add encapsulated content to signed PKCS7 structure. */
  798. if (!PKCS7_set_content(p7, ret))
  799. goto err;
  800. return 1;
  801. err:
  802. ASN1_OCTET_STRING_free(octet_string);
  803. PKCS7_free(ret);
  804. return 0;
  805. }
  806. static int ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc)
  807. {
  808. ASN1_STRING *seq = NULL;
  809. unsigned char *p, *pp = NULL;
  810. int len;
  811. len = i2d_ESS_SIGNING_CERT(sc, NULL);
  812. if (!(pp = (unsigned char *)OPENSSL_malloc(len))) {
  813. TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
  814. goto err;
  815. }
  816. p = pp;
  817. i2d_ESS_SIGNING_CERT(sc, &p);
  818. if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
  819. TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
  820. goto err;
  821. }
  822. OPENSSL_free(pp);
  823. pp = NULL;
  824. return PKCS7_add_signed_attribute(si,
  825. NID_id_smime_aa_signingCertificate,
  826. V_ASN1_SEQUENCE, seq);
  827. err:
  828. ASN1_STRING_free(seq);
  829. OPENSSL_free(pp);
  830. return 0;
  831. }
  832. static ASN1_GENERALIZEDTIME
  833. *TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *asn1_time,
  834. long sec, long usec, unsigned precision)
  835. {
  836. time_t time_sec = (time_t)sec;
  837. struct tm *tm = NULL;
  838. char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
  839. char *p = genTime_str;
  840. char *p_end = genTime_str + sizeof(genTime_str);
  841. if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
  842. goto err;
  843. if (!(tm = gmtime(&time_sec)))
  844. goto err;
  845. /*
  846. * Put "genTime_str" in GeneralizedTime format. We work around the
  847. * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
  848. * NOT include fractional seconds") and OpenSSL related functions to
  849. * meet the rfc3161 requirement: "GeneralizedTime syntax can include
  850. * fraction-of-second details".
  851. */
  852. p += BIO_snprintf(p, p_end - p,
  853. "%04d%02d%02d%02d%02d%02d",
  854. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  855. tm->tm_hour, tm->tm_min, tm->tm_sec);
  856. if (precision > 0) {
  857. /* Add fraction of seconds (leave space for dot and null). */
  858. BIO_snprintf(p, 2 + precision, ".%06ld", usec);
  859. /*
  860. * We cannot use the snprintf return value, because it might have
  861. * been truncated.
  862. */
  863. p += strlen(p);
  864. /*
  865. * To make things a bit harder, X.690 | ISO/IEC 8825-1 provides the
  866. * following restrictions for a DER-encoding, which OpenSSL
  867. * (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
  868. * support: "The encoding MUST terminate with a "Z" (which means
  869. * "Zulu" time). The decimal point element, if present, MUST be the
  870. * point option ".". The fractional-seconds elements, if present,
  871. * MUST omit all trailing 0's; if the elements correspond to 0, they
  872. * MUST be wholly omitted, and the decimal point element also MUST be
  873. * omitted."
  874. */
  875. /*
  876. * Remove trailing zeros. The dot guarantees the exit condition of
  877. * this loop even if all the digits are zero.
  878. */
  879. while (*--p == '0')
  880. /*
  881. * empty
  882. */ ;
  883. /* p points to either the dot or the last non-zero digit. */
  884. if (*p != '.')
  885. ++p;
  886. }
  887. /* Add the trailing Z and the terminating null. */
  888. *p++ = 'Z';
  889. *p++ = '\0';
  890. /* Now call OpenSSL to check and set our genTime value */
  891. if (!asn1_time && !(asn1_time = M_ASN1_GENERALIZEDTIME_new()))
  892. goto err;
  893. if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
  894. ASN1_GENERALIZEDTIME_free(asn1_time);
  895. goto err;
  896. }
  897. return asn1_time;
  898. err:
  899. TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION, TS_R_COULD_NOT_SET_TIME);
  900. return NULL;
  901. }