OpenSSL  1.0.1c
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
ts_asn1.c
Go to the documentation of this file.
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
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  * ([email protected]). This product includes software written by Tim
54  * Hudson ([email protected]).
55  *
56  */
57 
58 #include <openssl/ts.h>
59 #include <openssl/err.h>
60 #include <openssl/asn1t.h>
61 
66 
69 #ifndef OPENSSL_NO_BIO
71  {
73  }
74 
76 {
78 }
79 #endif
80 #ifndef OPENSSL_NO_FP_API
82  {
84  }
85 
87  {
89  }
90 #endif
91 
93  ASN1_SIMPLE(TS_REQ, version, ASN1_INTEGER),
94  ASN1_SIMPLE(TS_REQ, msg_imprint, TS_MSG_IMPRINT),
95  ASN1_OPT(TS_REQ, policy_id, ASN1_OBJECT),
96  ASN1_OPT(TS_REQ, nonce, ASN1_INTEGER),
97  ASN1_OPT(TS_REQ, cert_req, ASN1_FBOOLEAN),
100 
103 #ifndef OPENSSL_NO_BIO
105  {
106  return ASN1_d2i_bio_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, bp, a);
107  }
108 
109 int i2d_TS_REQ_bio(BIO *bp, TS_REQ *a)
110  {
111  return ASN1_i2d_bio_of_const(TS_REQ, i2d_TS_REQ, bp, a);
112  }
113 #endif
114 #ifndef OPENSSL_NO_FP_API
115 TS_REQ *d2i_TS_REQ_fp(FILE *fp, TS_REQ **a)
116  {
117  return ASN1_d2i_fp_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, fp, a);
118  }
119 
120 int i2d_TS_REQ_fp(FILE *fp, TS_REQ *a)
121  {
122  return ASN1_i2d_fp_of_const(TS_REQ, i2d_TS_REQ, fp, a);
123  }
124 #endif
125 
127  ASN1_OPT(TS_ACCURACY, seconds, ASN1_INTEGER),
131 
134 
136  ASN1_SIMPLE(TS_TST_INFO, version, ASN1_INTEGER),
137  ASN1_SIMPLE(TS_TST_INFO, policy_id, ASN1_OBJECT),
138  ASN1_SIMPLE(TS_TST_INFO, msg_imprint, TS_MSG_IMPRINT),
139  ASN1_SIMPLE(TS_TST_INFO, serial, ASN1_INTEGER),
140  ASN1_SIMPLE(TS_TST_INFO, time, ASN1_GENERALIZEDTIME),
141  ASN1_OPT(TS_TST_INFO, accuracy, TS_ACCURACY),
142  ASN1_OPT(TS_TST_INFO, ordering, ASN1_FBOOLEAN),
143  ASN1_OPT(TS_TST_INFO, nonce, ASN1_INTEGER),
144  ASN1_EXP_OPT(TS_TST_INFO, tsa, GENERAL_NAME, 0),
145  ASN1_IMP_SEQUENCE_OF_OPT(TS_TST_INFO, extensions, X509_EXTENSION, 1)
147 
150 #ifndef OPENSSL_NO_BIO
152  {
154  }
155 
157  {
159  }
160 #endif
161 #ifndef OPENSSL_NO_FP_API
163  {
165  }
166 
167 int i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a)
168  {
170  }
171 #endif
172 
176  ASN1_OPT(TS_STATUS_INFO, failure_info, ASN1_BIT_STRING)
178 
181 
182 static int ts_resp_set_tst_info(TS_RESP *a)
183 {
184  long status;
185 
186  status = ASN1_INTEGER_get(a->status_info->status);
187 
188  if (a->token) {
189  if (status != 0 && status != 1) {
191  return 0;
192  }
193  if (a->tst_info != NULL)
194  TS_TST_INFO_free(a->tst_info);
195  a->tst_info = PKCS7_to_TS_TST_INFO(a->token);
196  if (!a->tst_info) {
198  return 0;
199  }
200  } else if (status == 0 || status == 1) {
202  return 0;
203  }
204 
205  return 1;
206 }
207 
208 static int ts_resp_cb(int op, ASN1_VALUE **pval, const ASN1_ITEM *it,
209  void *exarg)
210 {
211  TS_RESP *ts_resp = (TS_RESP *)*pval;
212  if (op == ASN1_OP_NEW_POST) {
213  ts_resp->tst_info = NULL;
214  } else if (op == ASN1_OP_FREE_POST) {
215  if (ts_resp->tst_info != NULL)
216  TS_TST_INFO_free(ts_resp->tst_info);
217  } else if (op == ASN1_OP_D2I_POST) {
218  if (ts_resp_set_tst_info(ts_resp) == 0)
219  return 0;
220  }
221  return 1;
222 }
223 
224 ASN1_SEQUENCE_cb(TS_RESP, ts_resp_cb) = {
225  ASN1_SIMPLE(TS_RESP, status_info, TS_STATUS_INFO),
226  ASN1_OPT(TS_RESP, token, PKCS7),
228 
231 #ifndef OPENSSL_NO_BIO
233  {
235  }
236 
237 int i2d_TS_RESP_bio(BIO *bp, TS_RESP *a)
238  {
239  return ASN1_i2d_bio_of_const(TS_RESP, i2d_TS_RESP, bp, a);
240  }
241 #endif
242 #ifndef OPENSSL_NO_FP_API
243 TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a)
244  {
246  }
247 
248 int i2d_TS_RESP_fp(FILE *fp, TS_RESP *a)
249  {
250  return ASN1_i2d_fp_of_const(TS_RESP, i2d_TS_RESP, fp, a);
251  }
252 #endif
253 
258 
261 
263  ASN1_SIMPLE(ESS_CERT_ID, hash, ASN1_OCTET_STRING),
264  ASN1_OPT(ESS_CERT_ID, issuer_serial, ESS_ISSUER_SERIAL)
266 
269 
271  ASN1_SEQUENCE_OF(ESS_SIGNING_CERT, cert_ids, ESS_CERT_ID),
272  ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT, policy_info, POLICYINFO)
274 
277 
278 /* Getting encapsulated TS_TST_INFO object from PKCS7. */
280 {
281  PKCS7_SIGNED *pkcs7_signed;
282  PKCS7 *enveloped;
283  ASN1_TYPE *tst_info_wrapper;
284  ASN1_OCTET_STRING *tst_info_der;
285  const unsigned char *p;
286 
287  if (!PKCS7_type_is_signed(token))
288  {
290  return NULL;
291  }
292 
293  /* Content must be present. */
294  if (PKCS7_get_detached(token))
295  {
297  return NULL;
298  }
299 
300  /* We have a signed data with content. */
301  pkcs7_signed = token->d.sign;
302  enveloped = pkcs7_signed->contents;
303  if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo)
304  {
306  return NULL;
307  }
308 
309  /* We have a DER encoded TST_INFO as the signed data. */
310  tst_info_wrapper = enveloped->d.other;
311  if (tst_info_wrapper->type != V_ASN1_OCTET_STRING)
312  {
314  return NULL;
315  }
316 
317  /* We have the correct ASN1_OCTET_STRING type. */
318  tst_info_der = tst_info_wrapper->value.octet_string;
319  /* At last, decode the TST_INFO. */
320  p = tst_info_der->data;
321  return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
322 }