2
0

ts_rsp_utils.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* crypto/ts/ts_resp_utils.c */
  2. /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
  3. * project 2002.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/objects.h>
  61. #include <openssl/ts.h>
  62. #include <openssl/pkcs7.h>
  63. /* Function definitions. */
  64. int TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
  65. {
  66. TS_STATUS_INFO *new_status_info;
  67. if (a->status_info == status_info)
  68. return 1;
  69. new_status_info = TS_STATUS_INFO_dup(status_info);
  70. if (new_status_info == NULL)
  71. {
  72. TSerr(TS_F_TS_RESP_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
  73. return 0;
  74. }
  75. TS_STATUS_INFO_free(a->status_info);
  76. a->status_info = new_status_info;
  77. return 1;
  78. }
  79. TS_STATUS_INFO *TS_RESP_get_status_info(TS_RESP *a)
  80. {
  81. return a->status_info;
  82. }
  83. /* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
  84. void TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
  85. {
  86. /* Set new PKCS7 and TST_INFO objects. */
  87. PKCS7_free(a->token);
  88. a->token = p7;
  89. TS_TST_INFO_free(a->tst_info);
  90. a->tst_info = tst_info;
  91. }
  92. PKCS7 *TS_RESP_get_token(TS_RESP *a)
  93. {
  94. return a->token;
  95. }
  96. TS_TST_INFO *TS_RESP_get_tst_info(TS_RESP *a)
  97. {
  98. return a->tst_info;
  99. }
  100. int TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
  101. {
  102. return ASN1_INTEGER_set(a->version, version);
  103. }
  104. long TS_TST_INFO_get_version(const TS_TST_INFO *a)
  105. {
  106. return ASN1_INTEGER_get(a->version);
  107. }
  108. int TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
  109. {
  110. ASN1_OBJECT *new_policy;
  111. if (a->policy_id == policy)
  112. return 1;
  113. new_policy = OBJ_dup(policy);
  114. if (new_policy == NULL)
  115. {
  116. TSerr(TS_F_TS_TST_INFO_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
  117. return 0;
  118. }
  119. ASN1_OBJECT_free(a->policy_id);
  120. a->policy_id = new_policy;
  121. return 1;
  122. }
  123. ASN1_OBJECT *TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
  124. {
  125. return a->policy_id;
  126. }
  127. int TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
  128. {
  129. TS_MSG_IMPRINT *new_msg_imprint;
  130. if (a->msg_imprint == msg_imprint)
  131. return 1;
  132. new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
  133. if (new_msg_imprint == NULL)
  134. {
  135. TSerr(TS_F_TS_TST_INFO_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
  136. return 0;
  137. }
  138. TS_MSG_IMPRINT_free(a->msg_imprint);
  139. a->msg_imprint = new_msg_imprint;
  140. return 1;
  141. }
  142. TS_MSG_IMPRINT *TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
  143. {
  144. return a->msg_imprint;
  145. }
  146. int TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
  147. {
  148. ASN1_INTEGER *new_serial;
  149. if (a->serial == serial)
  150. return 1;
  151. new_serial = ASN1_INTEGER_dup(serial);
  152. if (new_serial == NULL)
  153. {
  154. TSerr(TS_F_TS_TST_INFO_SET_SERIAL, ERR_R_MALLOC_FAILURE);
  155. return 0;
  156. }
  157. ASN1_INTEGER_free(a->serial);
  158. a->serial = new_serial;
  159. return 1;
  160. }
  161. const ASN1_INTEGER *TS_TST_INFO_get_serial(const TS_TST_INFO *a)
  162. {
  163. return a->serial;
  164. }
  165. int TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
  166. {
  167. ASN1_GENERALIZEDTIME *new_time;
  168. if (a->time == gtime)
  169. return 1;
  170. new_time = M_ASN1_GENERALIZEDTIME_dup(gtime);
  171. if (new_time == NULL)
  172. {
  173. TSerr(TS_F_TS_TST_INFO_SET_TIME, ERR_R_MALLOC_FAILURE);
  174. return 0;
  175. }
  176. ASN1_GENERALIZEDTIME_free(a->time);
  177. a->time = new_time;
  178. return 1;
  179. }
  180. const ASN1_GENERALIZEDTIME *TS_TST_INFO_get_time(const TS_TST_INFO *a)
  181. {
  182. return a->time;
  183. }
  184. int TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
  185. {
  186. TS_ACCURACY *new_accuracy;
  187. if (a->accuracy == accuracy)
  188. return 1;
  189. new_accuracy = TS_ACCURACY_dup(accuracy);
  190. if (new_accuracy == NULL)
  191. {
  192. TSerr(TS_F_TS_TST_INFO_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
  193. return 0;
  194. }
  195. TS_ACCURACY_free(a->accuracy);
  196. a->accuracy = new_accuracy;
  197. return 1;
  198. }
  199. TS_ACCURACY *TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
  200. {
  201. return a->accuracy;
  202. }
  203. int TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
  204. {
  205. ASN1_INTEGER *new_seconds;
  206. if (a->seconds == seconds)
  207. return 1;
  208. new_seconds = ASN1_INTEGER_dup(seconds);
  209. if (new_seconds == NULL)
  210. {
  211. TSerr(TS_F_TS_ACCURACY_SET_SECONDS, ERR_R_MALLOC_FAILURE);
  212. return 0;
  213. }
  214. ASN1_INTEGER_free(a->seconds);
  215. a->seconds = new_seconds;
  216. return 1;
  217. }
  218. const ASN1_INTEGER *TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
  219. {
  220. return a->seconds;
  221. }
  222. int TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
  223. {
  224. ASN1_INTEGER *new_millis = NULL;
  225. if (a->millis == millis)
  226. return 1;
  227. if (millis != NULL)
  228. {
  229. new_millis = ASN1_INTEGER_dup(millis);
  230. if (new_millis == NULL)
  231. {
  232. TSerr(TS_F_TS_ACCURACY_SET_MILLIS,
  233. ERR_R_MALLOC_FAILURE);
  234. return 0;
  235. }
  236. }
  237. ASN1_INTEGER_free(a->millis);
  238. a->millis = new_millis;
  239. return 1;
  240. }
  241. const ASN1_INTEGER *TS_ACCURACY_get_millis(const TS_ACCURACY *a)
  242. {
  243. return a->millis;
  244. }
  245. int TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
  246. {
  247. ASN1_INTEGER *new_micros = NULL;
  248. if (a->micros == micros)
  249. return 1;
  250. if (micros != NULL)
  251. {
  252. new_micros = ASN1_INTEGER_dup(micros);
  253. if (new_micros == NULL)
  254. {
  255. TSerr(TS_F_TS_ACCURACY_SET_MICROS,
  256. ERR_R_MALLOC_FAILURE);
  257. return 0;
  258. }
  259. }
  260. ASN1_INTEGER_free(a->micros);
  261. a->micros = new_micros;
  262. return 1;
  263. }
  264. const ASN1_INTEGER *TS_ACCURACY_get_micros(const TS_ACCURACY *a)
  265. {
  266. return a->micros;
  267. }
  268. int TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
  269. {
  270. a->ordering = ordering ? 0xFF : 0x00;
  271. return 1;
  272. }
  273. int TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
  274. {
  275. return a->ordering ? 1 : 0;
  276. }
  277. int TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
  278. {
  279. ASN1_INTEGER *new_nonce;
  280. if (a->nonce == nonce)
  281. return 1;
  282. new_nonce = ASN1_INTEGER_dup(nonce);
  283. if (new_nonce == NULL)
  284. {
  285. TSerr(TS_F_TS_TST_INFO_SET_NONCE, ERR_R_MALLOC_FAILURE);
  286. return 0;
  287. }
  288. ASN1_INTEGER_free(a->nonce);
  289. a->nonce = new_nonce;
  290. return 1;
  291. }
  292. const ASN1_INTEGER *TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
  293. {
  294. return a->nonce;
  295. }
  296. int TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
  297. {
  298. GENERAL_NAME *new_tsa;
  299. if (a->tsa == tsa)
  300. return 1;
  301. new_tsa = GENERAL_NAME_dup(tsa);
  302. if (new_tsa == NULL)
  303. {
  304. TSerr(TS_F_TS_TST_INFO_SET_TSA, ERR_R_MALLOC_FAILURE);
  305. return 0;
  306. }
  307. GENERAL_NAME_free(a->tsa);
  308. a->tsa = new_tsa;
  309. return 1;
  310. }
  311. GENERAL_NAME *TS_TST_INFO_get_tsa(TS_TST_INFO *a)
  312. {
  313. return a->tsa;
  314. }
  315. STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
  316. {
  317. return a->extensions;
  318. }
  319. void TS_TST_INFO_ext_free(TS_TST_INFO *a)
  320. {
  321. if (!a) return;
  322. sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
  323. a->extensions = NULL;
  324. }
  325. int TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
  326. {
  327. return X509v3_get_ext_count(a->extensions);
  328. }
  329. int TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
  330. {
  331. return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
  332. }
  333. int TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, ASN1_OBJECT *obj, int lastpos)
  334. {
  335. return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
  336. }
  337. int TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
  338. {
  339. return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
  340. }
  341. X509_EXTENSION *TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
  342. {
  343. return X509v3_get_ext(a->extensions,loc);
  344. }
  345. X509_EXTENSION *TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
  346. {
  347. return X509v3_delete_ext(a->extensions,loc);
  348. }
  349. int TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
  350. {
  351. return X509v3_add_ext(&a->extensions,ex,loc) != NULL;
  352. }
  353. void *TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
  354. {
  355. return X509V3_get_d2i(a->extensions, nid, crit, idx);
  356. }