ts_rsp_sign.c 32 KB

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