ts_asn1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* crypto/ts/ts_asn1.c */
  2. /* Written by Nils Larsch for the OpenSSL project 2004.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <openssl/ts.h>
  58. #include <openssl/err.h>
  59. #include <openssl/asn1t.h>
  60. ASN1_SEQUENCE(TS_MSG_IMPRINT) = {
  61. ASN1_SIMPLE(TS_MSG_IMPRINT, hash_algo, X509_ALGOR),
  62. ASN1_SIMPLE(TS_MSG_IMPRINT, hashed_msg, ASN1_OCTET_STRING)
  63. } ASN1_SEQUENCE_END(TS_MSG_IMPRINT)
  64. IMPLEMENT_ASN1_FUNCTIONS_const(TS_MSG_IMPRINT)
  65. IMPLEMENT_ASN1_DUP_FUNCTION(TS_MSG_IMPRINT)
  66. #ifndef OPENSSL_NO_BIO
  67. TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT **a)
  68. {
  69. return ASN1_d2i_bio_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new, d2i_TS_MSG_IMPRINT, bp, a);
  70. }
  71. int i2d_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT *a)
  72. {
  73. return ASN1_i2d_bio_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, bp, a);
  74. }
  75. #endif
  76. #ifndef OPENSSL_NO_FP_API
  77. TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT **a)
  78. {
  79. return ASN1_d2i_fp_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new, d2i_TS_MSG_IMPRINT, fp, a);
  80. }
  81. int i2d_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT *a)
  82. {
  83. return ASN1_i2d_fp_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, fp, a);
  84. }
  85. #endif
  86. ASN1_SEQUENCE(TS_REQ) = {
  87. ASN1_SIMPLE(TS_REQ, version, ASN1_INTEGER),
  88. ASN1_SIMPLE(TS_REQ, msg_imprint, TS_MSG_IMPRINT),
  89. ASN1_OPT(TS_REQ, policy_id, ASN1_OBJECT),
  90. ASN1_OPT(TS_REQ, nonce, ASN1_INTEGER),
  91. ASN1_OPT(TS_REQ, cert_req, ASN1_FBOOLEAN),
  92. ASN1_IMP_SEQUENCE_OF_OPT(TS_REQ, extensions, X509_EXTENSION, 0)
  93. } ASN1_SEQUENCE_END(TS_REQ)
  94. IMPLEMENT_ASN1_FUNCTIONS_const(TS_REQ)
  95. IMPLEMENT_ASN1_DUP_FUNCTION(TS_REQ)
  96. #ifndef OPENSSL_NO_BIO
  97. TS_REQ *d2i_TS_REQ_bio(BIO *bp, TS_REQ **a)
  98. {
  99. return ASN1_d2i_bio_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, bp, a);
  100. }
  101. int i2d_TS_REQ_bio(BIO *bp, TS_REQ *a)
  102. {
  103. return ASN1_i2d_bio_of_const(TS_REQ, i2d_TS_REQ, bp, a);
  104. }
  105. #endif
  106. #ifndef OPENSSL_NO_FP_API
  107. TS_REQ *d2i_TS_REQ_fp(FILE *fp, TS_REQ **a)
  108. {
  109. return ASN1_d2i_fp_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, fp, a);
  110. }
  111. int i2d_TS_REQ_fp(FILE *fp, TS_REQ *a)
  112. {
  113. return ASN1_i2d_fp_of_const(TS_REQ, i2d_TS_REQ, fp, a);
  114. }
  115. #endif
  116. ASN1_SEQUENCE(TS_ACCURACY) = {
  117. ASN1_OPT(TS_ACCURACY, seconds, ASN1_INTEGER),
  118. ASN1_IMP_OPT(TS_ACCURACY, millis, ASN1_INTEGER, 0),
  119. ASN1_IMP_OPT(TS_ACCURACY, micros, ASN1_INTEGER, 1)
  120. } ASN1_SEQUENCE_END(TS_ACCURACY)
  121. IMPLEMENT_ASN1_FUNCTIONS_const(TS_ACCURACY)
  122. IMPLEMENT_ASN1_DUP_FUNCTION(TS_ACCURACY)
  123. ASN1_SEQUENCE(TS_TST_INFO) = {
  124. ASN1_SIMPLE(TS_TST_INFO, version, ASN1_INTEGER),
  125. ASN1_SIMPLE(TS_TST_INFO, policy_id, ASN1_OBJECT),
  126. ASN1_SIMPLE(TS_TST_INFO, msg_imprint, TS_MSG_IMPRINT),
  127. ASN1_SIMPLE(TS_TST_INFO, serial, ASN1_INTEGER),
  128. ASN1_SIMPLE(TS_TST_INFO, time, ASN1_GENERALIZEDTIME),
  129. ASN1_OPT(TS_TST_INFO, accuracy, TS_ACCURACY),
  130. ASN1_OPT(TS_TST_INFO, ordering, ASN1_FBOOLEAN),
  131. ASN1_OPT(TS_TST_INFO, nonce, ASN1_INTEGER),
  132. ASN1_EXP_OPT(TS_TST_INFO, tsa, GENERAL_NAME, 0),
  133. ASN1_IMP_SEQUENCE_OF_OPT(TS_TST_INFO, extensions, X509_EXTENSION, 1)
  134. } ASN1_SEQUENCE_END(TS_TST_INFO)
  135. IMPLEMENT_ASN1_FUNCTIONS_const(TS_TST_INFO)
  136. IMPLEMENT_ASN1_DUP_FUNCTION(TS_TST_INFO)
  137. #ifndef OPENSSL_NO_BIO
  138. TS_TST_INFO *d2i_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO **a)
  139. {
  140. return ASN1_d2i_bio_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO, bp, a);
  141. }
  142. int i2d_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO *a)
  143. {
  144. return ASN1_i2d_bio_of_const(TS_TST_INFO, i2d_TS_TST_INFO, bp, a);
  145. }
  146. #endif
  147. #ifndef OPENSSL_NO_FP_API
  148. TS_TST_INFO *d2i_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO **a)
  149. {
  150. return ASN1_d2i_fp_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO, fp, a);
  151. }
  152. int i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a)
  153. {
  154. return ASN1_i2d_fp_of_const(TS_TST_INFO, i2d_TS_TST_INFO, fp, a);
  155. }
  156. #endif
  157. ASN1_SEQUENCE(TS_STATUS_INFO) = {
  158. ASN1_SIMPLE(TS_STATUS_INFO, status, ASN1_INTEGER),
  159. ASN1_SEQUENCE_OF_OPT(TS_STATUS_INFO, text, ASN1_UTF8STRING),
  160. ASN1_OPT(TS_STATUS_INFO, failure_info, ASN1_BIT_STRING)
  161. } ASN1_SEQUENCE_END(TS_STATUS_INFO)
  162. IMPLEMENT_ASN1_FUNCTIONS_const(TS_STATUS_INFO)
  163. IMPLEMENT_ASN1_DUP_FUNCTION(TS_STATUS_INFO)
  164. static int ts_resp_set_tst_info(TS_RESP *a)
  165. {
  166. long status;
  167. status = ASN1_INTEGER_get(a->status_info->status);
  168. if (a->token) {
  169. if (status != 0 && status != 1) {
  170. TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_PRESENT);
  171. return 0;
  172. }
  173. if (a->tst_info != NULL)
  174. TS_TST_INFO_free(a->tst_info);
  175. a->tst_info = PKCS7_to_TS_TST_INFO(a->token);
  176. if (!a->tst_info) {
  177. TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_PKCS7_TO_TS_TST_INFO_FAILED);
  178. return 0;
  179. }
  180. } else if (status == 0 || status == 1) {
  181. TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_NOT_PRESENT);
  182. return 0;
  183. }
  184. return 1;
  185. }
  186. static int ts_resp_cb(int op, ASN1_VALUE **pval, const ASN1_ITEM *it,
  187. void *exarg)
  188. {
  189. TS_RESP *ts_resp = (TS_RESP *)*pval;
  190. if (op == ASN1_OP_NEW_POST) {
  191. ts_resp->tst_info = NULL;
  192. } else if (op == ASN1_OP_FREE_POST) {
  193. if (ts_resp->tst_info != NULL)
  194. TS_TST_INFO_free(ts_resp->tst_info);
  195. } else if (op == ASN1_OP_D2I_POST) {
  196. if (ts_resp_set_tst_info(ts_resp) == 0)
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. ASN1_SEQUENCE_cb(TS_RESP, ts_resp_cb) = {
  202. ASN1_SIMPLE(TS_RESP, status_info, TS_STATUS_INFO),
  203. ASN1_OPT(TS_RESP, token, PKCS7),
  204. } ASN1_SEQUENCE_END_cb(TS_RESP, TS_RESP)
  205. IMPLEMENT_ASN1_FUNCTIONS_const(TS_RESP)
  206. IMPLEMENT_ASN1_DUP_FUNCTION(TS_RESP)
  207. #ifndef OPENSSL_NO_BIO
  208. TS_RESP *d2i_TS_RESP_bio(BIO *bp, TS_RESP **a)
  209. {
  210. return ASN1_d2i_bio_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, bp, a);
  211. }
  212. int i2d_TS_RESP_bio(BIO *bp, TS_RESP *a)
  213. {
  214. return ASN1_i2d_bio_of_const(TS_RESP, i2d_TS_RESP, bp, a);
  215. }
  216. #endif
  217. #ifndef OPENSSL_NO_FP_API
  218. TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a)
  219. {
  220. return ASN1_d2i_fp_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, fp, a);
  221. }
  222. int i2d_TS_RESP_fp(FILE *fp, TS_RESP *a)
  223. {
  224. return ASN1_i2d_fp_of_const(TS_RESP, i2d_TS_RESP, fp, a);
  225. }
  226. #endif
  227. ASN1_SEQUENCE(ESS_ISSUER_SERIAL) = {
  228. ASN1_SEQUENCE_OF(ESS_ISSUER_SERIAL, issuer, GENERAL_NAME),
  229. ASN1_SIMPLE(ESS_ISSUER_SERIAL, serial, ASN1_INTEGER)
  230. } ASN1_SEQUENCE_END(ESS_ISSUER_SERIAL)
  231. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_ISSUER_SERIAL)
  232. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_ISSUER_SERIAL)
  233. ASN1_SEQUENCE(ESS_CERT_ID) = {
  234. ASN1_SIMPLE(ESS_CERT_ID, hash, ASN1_OCTET_STRING),
  235. ASN1_OPT(ESS_CERT_ID, issuer_serial, ESS_ISSUER_SERIAL)
  236. } ASN1_SEQUENCE_END(ESS_CERT_ID)
  237. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_CERT_ID)
  238. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_CERT_ID)
  239. ASN1_SEQUENCE(ESS_SIGNING_CERT) = {
  240. ASN1_SEQUENCE_OF(ESS_SIGNING_CERT, cert_ids, ESS_CERT_ID),
  241. ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT, policy_info, POLICYINFO)
  242. } ASN1_SEQUENCE_END(ESS_SIGNING_CERT)
  243. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_SIGNING_CERT)
  244. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_SIGNING_CERT)
  245. /* Getting encapsulated TS_TST_INFO object from PKCS7. */
  246. TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token)
  247. {
  248. PKCS7_SIGNED *pkcs7_signed;
  249. PKCS7 *enveloped;
  250. ASN1_TYPE *tst_info_wrapper;
  251. ASN1_OCTET_STRING *tst_info_der;
  252. const unsigned char *p;
  253. if (!PKCS7_type_is_signed(token))
  254. {
  255. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
  256. return NULL;
  257. }
  258. /* Content must be present. */
  259. if (PKCS7_get_detached(token))
  260. {
  261. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_DETACHED_CONTENT);
  262. return NULL;
  263. }
  264. /* We have a signed data with content. */
  265. pkcs7_signed = token->d.sign;
  266. enveloped = pkcs7_signed->contents;
  267. if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo)
  268. {
  269. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
  270. return NULL;
  271. }
  272. /* We have a DER encoded TST_INFO as the signed data. */
  273. tst_info_wrapper = enveloped->d.other;
  274. if (tst_info_wrapper->type != V_ASN1_OCTET_STRING)
  275. {
  276. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_TYPE);
  277. return NULL;
  278. }
  279. /* We have the correct ASN1_OCTET_STRING type. */
  280. tst_info_der = tst_info_wrapper->value.octet_string;
  281. /* At last, decode the TST_INFO. */
  282. p = tst_info_der->data;
  283. return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
  284. }