2
0

ts_rsp_utils.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/ts.h>
  13. #include <openssl/pkcs7.h>
  14. #include "ts_local.h"
  15. DEFINE_STACK_OF(X509_EXTENSION)
  16. int TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
  17. {
  18. TS_STATUS_INFO *new_status_info;
  19. if (a->status_info == status_info)
  20. return 1;
  21. new_status_info = TS_STATUS_INFO_dup(status_info);
  22. if (new_status_info == NULL) {
  23. TSerr(TS_F_TS_RESP_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
  24. return 0;
  25. }
  26. TS_STATUS_INFO_free(a->status_info);
  27. a->status_info = new_status_info;
  28. return 1;
  29. }
  30. TS_STATUS_INFO *TS_RESP_get_status_info(TS_RESP *a)
  31. {
  32. return a->status_info;
  33. }
  34. /* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
  35. void TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
  36. {
  37. PKCS7_free(a->token);
  38. a->token = p7;
  39. TS_TST_INFO_free(a->tst_info);
  40. a->tst_info = tst_info;
  41. }
  42. PKCS7 *TS_RESP_get_token(TS_RESP *a)
  43. {
  44. return a->token;
  45. }
  46. TS_TST_INFO *TS_RESP_get_tst_info(TS_RESP *a)
  47. {
  48. return a->tst_info;
  49. }
  50. int TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
  51. {
  52. return ASN1_INTEGER_set(a->version, version);
  53. }
  54. long TS_TST_INFO_get_version(const TS_TST_INFO *a)
  55. {
  56. return ASN1_INTEGER_get(a->version);
  57. }
  58. int TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
  59. {
  60. ASN1_OBJECT *new_policy;
  61. if (a->policy_id == policy)
  62. return 1;
  63. new_policy = OBJ_dup(policy);
  64. if (new_policy == NULL) {
  65. TSerr(TS_F_TS_TST_INFO_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
  66. return 0;
  67. }
  68. ASN1_OBJECT_free(a->policy_id);
  69. a->policy_id = new_policy;
  70. return 1;
  71. }
  72. ASN1_OBJECT *TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
  73. {
  74. return a->policy_id;
  75. }
  76. int TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
  77. {
  78. TS_MSG_IMPRINT *new_msg_imprint;
  79. if (a->msg_imprint == msg_imprint)
  80. return 1;
  81. new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
  82. if (new_msg_imprint == NULL) {
  83. TSerr(TS_F_TS_TST_INFO_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
  84. return 0;
  85. }
  86. TS_MSG_IMPRINT_free(a->msg_imprint);
  87. a->msg_imprint = new_msg_imprint;
  88. return 1;
  89. }
  90. TS_MSG_IMPRINT *TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
  91. {
  92. return a->msg_imprint;
  93. }
  94. int TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
  95. {
  96. ASN1_INTEGER *new_serial;
  97. if (a->serial == serial)
  98. return 1;
  99. new_serial = ASN1_INTEGER_dup(serial);
  100. if (new_serial == NULL) {
  101. TSerr(TS_F_TS_TST_INFO_SET_SERIAL, ERR_R_MALLOC_FAILURE);
  102. return 0;
  103. }
  104. ASN1_INTEGER_free(a->serial);
  105. a->serial = new_serial;
  106. return 1;
  107. }
  108. const ASN1_INTEGER *TS_TST_INFO_get_serial(const TS_TST_INFO *a)
  109. {
  110. return a->serial;
  111. }
  112. int TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
  113. {
  114. ASN1_GENERALIZEDTIME *new_time;
  115. if (a->time == gtime)
  116. return 1;
  117. new_time = ASN1_STRING_dup(gtime);
  118. if (new_time == NULL) {
  119. TSerr(TS_F_TS_TST_INFO_SET_TIME, ERR_R_MALLOC_FAILURE);
  120. return 0;
  121. }
  122. ASN1_GENERALIZEDTIME_free(a->time);
  123. a->time = new_time;
  124. return 1;
  125. }
  126. const ASN1_GENERALIZEDTIME *TS_TST_INFO_get_time(const TS_TST_INFO *a)
  127. {
  128. return a->time;
  129. }
  130. int TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
  131. {
  132. TS_ACCURACY *new_accuracy;
  133. if (a->accuracy == accuracy)
  134. return 1;
  135. new_accuracy = TS_ACCURACY_dup(accuracy);
  136. if (new_accuracy == NULL) {
  137. TSerr(TS_F_TS_TST_INFO_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
  138. return 0;
  139. }
  140. TS_ACCURACY_free(a->accuracy);
  141. a->accuracy = new_accuracy;
  142. return 1;
  143. }
  144. TS_ACCURACY *TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
  145. {
  146. return a->accuracy;
  147. }
  148. int TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
  149. {
  150. ASN1_INTEGER *new_seconds;
  151. if (a->seconds == seconds)
  152. return 1;
  153. new_seconds = ASN1_INTEGER_dup(seconds);
  154. if (new_seconds == NULL) {
  155. TSerr(TS_F_TS_ACCURACY_SET_SECONDS, ERR_R_MALLOC_FAILURE);
  156. return 0;
  157. }
  158. ASN1_INTEGER_free(a->seconds);
  159. a->seconds = new_seconds;
  160. return 1;
  161. }
  162. const ASN1_INTEGER *TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
  163. {
  164. return a->seconds;
  165. }
  166. int TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
  167. {
  168. ASN1_INTEGER *new_millis = NULL;
  169. if (a->millis == millis)
  170. return 1;
  171. if (millis != NULL) {
  172. new_millis = ASN1_INTEGER_dup(millis);
  173. if (new_millis == NULL) {
  174. TSerr(TS_F_TS_ACCURACY_SET_MILLIS, ERR_R_MALLOC_FAILURE);
  175. return 0;
  176. }
  177. }
  178. ASN1_INTEGER_free(a->millis);
  179. a->millis = new_millis;
  180. return 1;
  181. }
  182. const ASN1_INTEGER *TS_ACCURACY_get_millis(const TS_ACCURACY *a)
  183. {
  184. return a->millis;
  185. }
  186. int TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
  187. {
  188. ASN1_INTEGER *new_micros = NULL;
  189. if (a->micros == micros)
  190. return 1;
  191. if (micros != NULL) {
  192. new_micros = ASN1_INTEGER_dup(micros);
  193. if (new_micros == NULL) {
  194. TSerr(TS_F_TS_ACCURACY_SET_MICROS, ERR_R_MALLOC_FAILURE);
  195. return 0;
  196. }
  197. }
  198. ASN1_INTEGER_free(a->micros);
  199. a->micros = new_micros;
  200. return 1;
  201. }
  202. const ASN1_INTEGER *TS_ACCURACY_get_micros(const TS_ACCURACY *a)
  203. {
  204. return a->micros;
  205. }
  206. int TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
  207. {
  208. a->ordering = ordering ? 0xFF : 0x00;
  209. return 1;
  210. }
  211. int TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
  212. {
  213. return a->ordering ? 1 : 0;
  214. }
  215. int TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
  216. {
  217. ASN1_INTEGER *new_nonce;
  218. if (a->nonce == nonce)
  219. return 1;
  220. new_nonce = ASN1_INTEGER_dup(nonce);
  221. if (new_nonce == NULL) {
  222. TSerr(TS_F_TS_TST_INFO_SET_NONCE, ERR_R_MALLOC_FAILURE);
  223. return 0;
  224. }
  225. ASN1_INTEGER_free(a->nonce);
  226. a->nonce = new_nonce;
  227. return 1;
  228. }
  229. const ASN1_INTEGER *TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
  230. {
  231. return a->nonce;
  232. }
  233. int TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
  234. {
  235. GENERAL_NAME *new_tsa;
  236. if (a->tsa == tsa)
  237. return 1;
  238. new_tsa = GENERAL_NAME_dup(tsa);
  239. if (new_tsa == NULL) {
  240. TSerr(TS_F_TS_TST_INFO_SET_TSA, ERR_R_MALLOC_FAILURE);
  241. return 0;
  242. }
  243. GENERAL_NAME_free(a->tsa);
  244. a->tsa = new_tsa;
  245. return 1;
  246. }
  247. GENERAL_NAME *TS_TST_INFO_get_tsa(TS_TST_INFO *a)
  248. {
  249. return a->tsa;
  250. }
  251. STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
  252. {
  253. return a->extensions;
  254. }
  255. void TS_TST_INFO_ext_free(TS_TST_INFO *a)
  256. {
  257. if (!a)
  258. return;
  259. sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
  260. a->extensions = NULL;
  261. }
  262. int TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
  263. {
  264. return X509v3_get_ext_count(a->extensions);
  265. }
  266. int TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
  267. {
  268. return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
  269. }
  270. int TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, int lastpos)
  271. {
  272. return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
  273. }
  274. int TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
  275. {
  276. return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
  277. }
  278. X509_EXTENSION *TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
  279. {
  280. return X509v3_get_ext(a->extensions, loc);
  281. }
  282. X509_EXTENSION *TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
  283. {
  284. return X509v3_delete_ext(a->extensions, loc);
  285. }
  286. int TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
  287. {
  288. return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
  289. }
  290. void *TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
  291. {
  292. return X509V3_get_d2i(a->extensions, nid, crit, idx);
  293. }
  294. int TS_STATUS_INFO_set_status(TS_STATUS_INFO *a, int i)
  295. {
  296. return ASN1_INTEGER_set(a->status, i);
  297. }
  298. const ASN1_INTEGER *TS_STATUS_INFO_get0_status(const TS_STATUS_INFO *a)
  299. {
  300. return a->status;
  301. }
  302. const STACK_OF(ASN1_UTF8STRING) *
  303. TS_STATUS_INFO_get0_text(const TS_STATUS_INFO *a)
  304. {
  305. return a->text;
  306. }
  307. const ASN1_BIT_STRING *TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO *a)
  308. {
  309. return a->failure_info;
  310. }