ts_asn1.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/ts.h>
  10. #include <openssl/err.h>
  11. #include <openssl/asn1t.h>
  12. #include "ts_lcl.h"
  13. ASN1_SEQUENCE(TS_MSG_IMPRINT) = {
  14. ASN1_SIMPLE(TS_MSG_IMPRINT, hash_algo, X509_ALGOR),
  15. ASN1_SIMPLE(TS_MSG_IMPRINT, hashed_msg, ASN1_OCTET_STRING)
  16. } static_ASN1_SEQUENCE_END(TS_MSG_IMPRINT)
  17. IMPLEMENT_ASN1_FUNCTIONS_const(TS_MSG_IMPRINT)
  18. IMPLEMENT_ASN1_DUP_FUNCTION(TS_MSG_IMPRINT)
  19. TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT **a)
  20. {
  21. return ASN1_d2i_bio_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new,
  22. d2i_TS_MSG_IMPRINT, bp, a);
  23. }
  24. int i2d_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT *a)
  25. {
  26. return ASN1_i2d_bio_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, bp, a);
  27. }
  28. #ifndef OPENSSL_NO_STDIO
  29. TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT **a)
  30. {
  31. return ASN1_d2i_fp_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new,
  32. d2i_TS_MSG_IMPRINT, fp, a);
  33. }
  34. int i2d_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT *a)
  35. {
  36. return ASN1_i2d_fp_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, fp, a);
  37. }
  38. #endif
  39. ASN1_SEQUENCE(TS_REQ) = {
  40. ASN1_SIMPLE(TS_REQ, version, ASN1_INTEGER),
  41. ASN1_SIMPLE(TS_REQ, msg_imprint, TS_MSG_IMPRINT),
  42. ASN1_OPT(TS_REQ, policy_id, ASN1_OBJECT),
  43. ASN1_OPT(TS_REQ, nonce, ASN1_INTEGER),
  44. ASN1_OPT(TS_REQ, cert_req, ASN1_FBOOLEAN),
  45. ASN1_IMP_SEQUENCE_OF_OPT(TS_REQ, extensions, X509_EXTENSION, 0)
  46. } static_ASN1_SEQUENCE_END(TS_REQ)
  47. IMPLEMENT_ASN1_FUNCTIONS_const(TS_REQ)
  48. IMPLEMENT_ASN1_DUP_FUNCTION(TS_REQ)
  49. TS_REQ *d2i_TS_REQ_bio(BIO *bp, TS_REQ **a)
  50. {
  51. return ASN1_d2i_bio_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, bp, a);
  52. }
  53. int i2d_TS_REQ_bio(BIO *bp, TS_REQ *a)
  54. {
  55. return ASN1_i2d_bio_of_const(TS_REQ, i2d_TS_REQ, bp, a);
  56. }
  57. #ifndef OPENSSL_NO_STDIO
  58. TS_REQ *d2i_TS_REQ_fp(FILE *fp, TS_REQ **a)
  59. {
  60. return ASN1_d2i_fp_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, fp, a);
  61. }
  62. int i2d_TS_REQ_fp(FILE *fp, TS_REQ *a)
  63. {
  64. return ASN1_i2d_fp_of_const(TS_REQ, i2d_TS_REQ, fp, a);
  65. }
  66. #endif
  67. ASN1_SEQUENCE(TS_ACCURACY) = {
  68. ASN1_OPT(TS_ACCURACY, seconds, ASN1_INTEGER),
  69. ASN1_IMP_OPT(TS_ACCURACY, millis, ASN1_INTEGER, 0),
  70. ASN1_IMP_OPT(TS_ACCURACY, micros, ASN1_INTEGER, 1)
  71. } static_ASN1_SEQUENCE_END(TS_ACCURACY)
  72. IMPLEMENT_ASN1_FUNCTIONS_const(TS_ACCURACY)
  73. IMPLEMENT_ASN1_DUP_FUNCTION(TS_ACCURACY)
  74. ASN1_SEQUENCE(TS_TST_INFO) = {
  75. ASN1_SIMPLE(TS_TST_INFO, version, ASN1_INTEGER),
  76. ASN1_SIMPLE(TS_TST_INFO, policy_id, ASN1_OBJECT),
  77. ASN1_SIMPLE(TS_TST_INFO, msg_imprint, TS_MSG_IMPRINT),
  78. ASN1_SIMPLE(TS_TST_INFO, serial, ASN1_INTEGER),
  79. ASN1_SIMPLE(TS_TST_INFO, time, ASN1_GENERALIZEDTIME),
  80. ASN1_OPT(TS_TST_INFO, accuracy, TS_ACCURACY),
  81. ASN1_OPT(TS_TST_INFO, ordering, ASN1_FBOOLEAN),
  82. ASN1_OPT(TS_TST_INFO, nonce, ASN1_INTEGER),
  83. ASN1_EXP_OPT(TS_TST_INFO, tsa, GENERAL_NAME, 0),
  84. ASN1_IMP_SEQUENCE_OF_OPT(TS_TST_INFO, extensions, X509_EXTENSION, 1)
  85. } static_ASN1_SEQUENCE_END(TS_TST_INFO)
  86. IMPLEMENT_ASN1_FUNCTIONS_const(TS_TST_INFO)
  87. IMPLEMENT_ASN1_DUP_FUNCTION(TS_TST_INFO)
  88. TS_TST_INFO *d2i_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO **a)
  89. {
  90. return ASN1_d2i_bio_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO, bp,
  91. a);
  92. }
  93. int i2d_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO *a)
  94. {
  95. return ASN1_i2d_bio_of_const(TS_TST_INFO, i2d_TS_TST_INFO, bp, a);
  96. }
  97. #ifndef OPENSSL_NO_STDIO
  98. TS_TST_INFO *d2i_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO **a)
  99. {
  100. return ASN1_d2i_fp_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO, fp,
  101. a);
  102. }
  103. int i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a)
  104. {
  105. return ASN1_i2d_fp_of_const(TS_TST_INFO, i2d_TS_TST_INFO, fp, a);
  106. }
  107. #endif
  108. ASN1_SEQUENCE(TS_STATUS_INFO) = {
  109. ASN1_SIMPLE(TS_STATUS_INFO, status, ASN1_INTEGER),
  110. ASN1_SEQUENCE_OF_OPT(TS_STATUS_INFO, text, ASN1_UTF8STRING),
  111. ASN1_OPT(TS_STATUS_INFO, failure_info, ASN1_BIT_STRING)
  112. } static_ASN1_SEQUENCE_END(TS_STATUS_INFO)
  113. IMPLEMENT_ASN1_FUNCTIONS_const(TS_STATUS_INFO)
  114. IMPLEMENT_ASN1_DUP_FUNCTION(TS_STATUS_INFO)
  115. static int ts_resp_set_tst_info(TS_RESP *a)
  116. {
  117. long status;
  118. status = ASN1_INTEGER_get(a->status_info->status);
  119. if (a->token) {
  120. if (status != 0 && status != 1) {
  121. TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_PRESENT);
  122. return 0;
  123. }
  124. TS_TST_INFO_free(a->tst_info);
  125. a->tst_info = PKCS7_to_TS_TST_INFO(a->token);
  126. if (!a->tst_info) {
  127. TSerr(TS_F_TS_RESP_SET_TST_INFO,
  128. TS_R_PKCS7_TO_TS_TST_INFO_FAILED);
  129. return 0;
  130. }
  131. } else if (status == 0 || status == 1) {
  132. TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_NOT_PRESENT);
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. static int ts_resp_cb(int op, ASN1_VALUE **pval, const ASN1_ITEM *it,
  138. void *exarg)
  139. {
  140. TS_RESP *ts_resp = (TS_RESP *)*pval;
  141. if (op == ASN1_OP_NEW_POST) {
  142. ts_resp->tst_info = NULL;
  143. } else if (op == ASN1_OP_FREE_POST) {
  144. TS_TST_INFO_free(ts_resp->tst_info);
  145. } else if (op == ASN1_OP_D2I_POST) {
  146. if (ts_resp_set_tst_info(ts_resp) == 0)
  147. return 0;
  148. }
  149. return 1;
  150. }
  151. ASN1_SEQUENCE_cb(TS_RESP, ts_resp_cb) = {
  152. ASN1_SIMPLE(TS_RESP, status_info, TS_STATUS_INFO),
  153. ASN1_OPT(TS_RESP, token, PKCS7),
  154. } static_ASN1_SEQUENCE_END_cb(TS_RESP, TS_RESP)
  155. IMPLEMENT_ASN1_FUNCTIONS_const(TS_RESP)
  156. IMPLEMENT_ASN1_DUP_FUNCTION(TS_RESP)
  157. TS_RESP *d2i_TS_RESP_bio(BIO *bp, TS_RESP **a)
  158. {
  159. return ASN1_d2i_bio_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, bp, a);
  160. }
  161. int i2d_TS_RESP_bio(BIO *bp, TS_RESP *a)
  162. {
  163. return ASN1_i2d_bio_of_const(TS_RESP, i2d_TS_RESP, bp, a);
  164. }
  165. #ifndef OPENSSL_NO_STDIO
  166. TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a)
  167. {
  168. return ASN1_d2i_fp_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, fp, a);
  169. }
  170. int i2d_TS_RESP_fp(FILE *fp, TS_RESP *a)
  171. {
  172. return ASN1_i2d_fp_of_const(TS_RESP, i2d_TS_RESP, fp, a);
  173. }
  174. #endif
  175. ASN1_SEQUENCE(ESS_ISSUER_SERIAL) = {
  176. ASN1_SEQUENCE_OF(ESS_ISSUER_SERIAL, issuer, GENERAL_NAME),
  177. ASN1_SIMPLE(ESS_ISSUER_SERIAL, serial, ASN1_INTEGER)
  178. } static_ASN1_SEQUENCE_END(ESS_ISSUER_SERIAL)
  179. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_ISSUER_SERIAL)
  180. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_ISSUER_SERIAL)
  181. ASN1_SEQUENCE(ESS_CERT_ID) = {
  182. ASN1_SIMPLE(ESS_CERT_ID, hash, ASN1_OCTET_STRING),
  183. ASN1_OPT(ESS_CERT_ID, issuer_serial, ESS_ISSUER_SERIAL)
  184. } static_ASN1_SEQUENCE_END(ESS_CERT_ID)
  185. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_CERT_ID)
  186. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_CERT_ID)
  187. ASN1_SEQUENCE(ESS_SIGNING_CERT) = {
  188. ASN1_SEQUENCE_OF(ESS_SIGNING_CERT, cert_ids, ESS_CERT_ID),
  189. ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT, policy_info, POLICYINFO)
  190. } static_ASN1_SEQUENCE_END(ESS_SIGNING_CERT)
  191. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_SIGNING_CERT)
  192. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_SIGNING_CERT)
  193. ASN1_SEQUENCE(ESS_CERT_ID_V2) = {
  194. ASN1_OPT(ESS_CERT_ID_V2, hash_alg, X509_ALGOR),
  195. ASN1_SIMPLE(ESS_CERT_ID_V2, hash, ASN1_OCTET_STRING),
  196. ASN1_OPT(ESS_CERT_ID_V2, issuer_serial, ESS_ISSUER_SERIAL)
  197. } static_ASN1_SEQUENCE_END(ESS_CERT_ID_V2)
  198. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_CERT_ID_V2)
  199. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_CERT_ID_V2)
  200. ASN1_SEQUENCE(ESS_SIGNING_CERT_V2) = {
  201. ASN1_SEQUENCE_OF(ESS_SIGNING_CERT_V2, cert_ids, ESS_CERT_ID_V2),
  202. ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT_V2, policy_info, POLICYINFO)
  203. } static_ASN1_SEQUENCE_END(ESS_SIGNING_CERT_V2)
  204. IMPLEMENT_ASN1_FUNCTIONS_const(ESS_SIGNING_CERT_V2)
  205. IMPLEMENT_ASN1_DUP_FUNCTION(ESS_SIGNING_CERT_V2)
  206. /* Getting encapsulated TS_TST_INFO object from PKCS7. */
  207. TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token)
  208. {
  209. PKCS7_SIGNED *pkcs7_signed;
  210. PKCS7 *enveloped;
  211. ASN1_TYPE *tst_info_wrapper;
  212. ASN1_OCTET_STRING *tst_info_der;
  213. const unsigned char *p;
  214. if (!PKCS7_type_is_signed(token)) {
  215. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
  216. return NULL;
  217. }
  218. if (PKCS7_get_detached(token)) {
  219. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_DETACHED_CONTENT);
  220. return NULL;
  221. }
  222. pkcs7_signed = token->d.sign;
  223. enveloped = pkcs7_signed->contents;
  224. if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo) {
  225. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
  226. return NULL;
  227. }
  228. tst_info_wrapper = enveloped->d.other;
  229. if (tst_info_wrapper->type != V_ASN1_OCTET_STRING) {
  230. TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_TYPE);
  231. return NULL;
  232. }
  233. tst_info_der = tst_info_wrapper->value.octet_string;
  234. p = tst_info_der->data;
  235. return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
  236. }