1 /* crypto/ts/ts_asn1.c */
2 /* Written by Nils Larsch for the OpenSSL project 2004.
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
58 #include <openssl/ts.h>
59 #include <openssl/err.h>
60 #include <openssl/asn1t.h>
62 ASN1_SEQUENCE(TS_MSG_IMPRINT
) = {
63 ASN1_SIMPLE(TS_MSG_IMPRINT
, hash_algo
, X509_ALGOR
),
64 ASN1_SIMPLE(TS_MSG_IMPRINT
, hashed_msg
, ASN1_OCTET_STRING
)
65 } ASN1_SEQUENCE_END(TS_MSG_IMPRINT
)
67 IMPLEMENT_ASN1_FUNCTIONS_const(TS_MSG_IMPRINT
)
68 IMPLEMENT_ASN1_DUP_FUNCTION(TS_MSG_IMPRINT
)
69 #ifndef OPENSSL_NO_BIO
70 int i2d_TS_MSG_IMPRINT_bio(BIO
*bp
, TS_MSG_IMPRINT
*a
)
72 return ASN1_i2d_bio_of_const(TS_MSG_IMPRINT
, i2d_TS_MSG_IMPRINT
, bp
, a
);
75 #ifndef OPENSSL_NO_FP_API
76 TS_MSG_IMPRINT
*d2i_TS_MSG_IMPRINT_fp(FILE *fp
, TS_MSG_IMPRINT
**a
)
78 return ASN1_d2i_fp_of(TS_MSG_IMPRINT
, TS_MSG_IMPRINT_new
, d2i_TS_MSG_IMPRINT
, fp
, a
);
81 int i2d_TS_MSG_IMPRINT_fp(FILE *fp
, TS_MSG_IMPRINT
*a
)
83 return ASN1_i2d_fp_of_const(TS_MSG_IMPRINT
, i2d_TS_MSG_IMPRINT
, fp
, a
);
87 ASN1_SEQUENCE(TS_REQ
) = {
88 ASN1_SIMPLE(TS_REQ
, version
, ASN1_INTEGER
),
89 ASN1_SIMPLE(TS_REQ
, msg_imprint
, TS_MSG_IMPRINT
),
90 ASN1_OPT(TS_REQ
, policy_id
, ASN1_OBJECT
),
91 ASN1_OPT(TS_REQ
, nonce
, ASN1_INTEGER
),
92 ASN1_OPT(TS_REQ
, cert_req
, ASN1_BOOLEAN
),
93 ASN1_IMP_SEQUENCE_OF_OPT(TS_REQ
, extensions
, X509_EXTENSION
, 0)
94 } ASN1_SEQUENCE_END(TS_REQ
)
96 IMPLEMENT_ASN1_FUNCTIONS_const(TS_REQ
)
97 IMPLEMENT_ASN1_DUP_FUNCTION(TS_REQ
)
98 #ifndef OPENSSL_NO_BIO
99 TS_REQ
*d2i_TS_REQ_bio(BIO
*bp
, TS_REQ
**a
)
101 return ASN1_d2i_bio_of(TS_REQ
, TS_REQ_new
, d2i_TS_REQ
, bp
, a
);
104 int i2d_TS_REQ_bio(BIO
*bp
, TS_REQ
*a
)
106 return ASN1_i2d_bio_of_const(TS_REQ
, i2d_TS_REQ
, bp
, a
);
109 #ifndef OPENSSL_NO_FP_API
110 TS_REQ
*d2i_TS_REQ_fp(FILE *fp
, TS_REQ
**a
)
112 return ASN1_d2i_fp_of(TS_REQ
, TS_REQ_new
, d2i_TS_REQ
, fp
, a
);
115 int i2d_TS_REQ_fp(FILE *fp
, TS_REQ
*a
)
117 return ASN1_i2d_fp_of_const(TS_REQ
, i2d_TS_REQ
, fp
, a
);
121 ASN1_SEQUENCE(TS_ACCURACY
) = {
122 ASN1_OPT(TS_ACCURACY
, seconds
, ASN1_INTEGER
),
123 ASN1_IMP_OPT(TS_ACCURACY
, millis
, ASN1_INTEGER
, 0),
124 ASN1_IMP_OPT(TS_ACCURACY
, micros
, ASN1_INTEGER
, 1)
125 } ASN1_SEQUENCE_END(TS_ACCURACY
)
127 IMPLEMENT_ASN1_FUNCTIONS_const(TS_ACCURACY
)
128 IMPLEMENT_ASN1_DUP_FUNCTION(TS_ACCURACY
)
130 ASN1_SEQUENCE(TS_TST_INFO
) = {
131 ASN1_SIMPLE(TS_TST_INFO
, version
, ASN1_INTEGER
),
132 ASN1_SIMPLE(TS_TST_INFO
, policy_id
, ASN1_OBJECT
),
133 ASN1_SIMPLE(TS_TST_INFO
, msg_imprint
, TS_MSG_IMPRINT
),
134 ASN1_SIMPLE(TS_TST_INFO
, serial
, ASN1_INTEGER
),
135 ASN1_SIMPLE(TS_TST_INFO
, time
, ASN1_GENERALIZEDTIME
),
136 ASN1_OPT(TS_TST_INFO
, accuracy
, TS_ACCURACY
),
137 ASN1_OPT(TS_TST_INFO
, ordering
, ASN1_BOOLEAN
),
138 ASN1_OPT(TS_TST_INFO
, nonce
, ASN1_INTEGER
),
139 ASN1_EXP_OPT(TS_TST_INFO
, tsa
, GENERAL_NAME
, 0),
140 ASN1_IMP_SEQUENCE_OF_OPT(TS_TST_INFO
, extensions
, X509_EXTENSION
, 1)
141 } ASN1_SEQUENCE_END(TS_TST_INFO
)
143 IMPLEMENT_ASN1_FUNCTIONS_const(TS_TST_INFO
)
144 IMPLEMENT_ASN1_DUP_FUNCTION(TS_TST_INFO
)
145 #ifndef OPENSSL_NO_BIO
146 TS_TST_INFO
*d2i_TS_TST_INFO_bio(BIO
*bp
, TS_TST_INFO
**a
)
148 return ASN1_d2i_bio_of(TS_TST_INFO
, TS_TST_INFO_new
, d2i_TS_TST_INFO
, bp
, a
);
151 int i2d_TS_TST_INFO_bio(BIO
*bp
, TS_TST_INFO
*a
)
153 return ASN1_i2d_bio_of_const(TS_TST_INFO
, i2d_TS_TST_INFO
, bp
, a
);
156 #ifndef OPENSSL_NO_FP_API
157 TS_TST_INFO
*d2i_TS_TST_INFO_fp(FILE *fp
, TS_TST_INFO
**a
)
159 return ASN1_d2i_fp_of(TS_TST_INFO
, TS_TST_INFO_new
, d2i_TS_TST_INFO
, fp
, a
);
162 int i2d_TS_TST_INFO_fp(FILE *fp
, TS_TST_INFO
*a
)
164 return ASN1_i2d_fp_of_const(TS_TST_INFO
, i2d_TS_TST_INFO
, fp
, a
);
168 ASN1_SEQUENCE(TS_STATUS_INFO
) = {
169 ASN1_SIMPLE(TS_STATUS_INFO
, status
, ASN1_INTEGER
),
170 ASN1_SEQUENCE_OF_OPT(TS_STATUS_INFO
, text
, ASN1_UTF8STRING
),
171 ASN1_OPT(TS_STATUS_INFO
, failure_info
, ASN1_BIT_STRING
)
172 } ASN1_SEQUENCE_END(TS_STATUS_INFO
)
174 IMPLEMENT_ASN1_FUNCTIONS_const(TS_STATUS_INFO
)
175 IMPLEMENT_ASN1_DUP_FUNCTION(TS_STATUS_INFO
)
177 ASN1_SEQUENCE(TS_RESP
) = {
178 ASN1_SIMPLE(TS_RESP
, status_info
, TS_STATUS_INFO
),
179 ASN1_OPT(TS_RESP
, token
, PKCS7
),
180 } ASN1_SEQUENCE_END(TS_RESP
)
182 DECLARE_ASN1_ALLOC_FUNCTIONS_name(TS_RESP
, TS_RESP_int
)
183 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(TS_RESP
, TS_RESP
, TS_RESP_int
)
184 DECLARE_ASN1_ENCODE_FUNCTIONS_const(TS_RESP
, TS_RESP_int
)
185 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(TS_RESP
, TS_RESP
, TS_RESP_int
)
187 TS_RESP
*TS_RESP_new(void)
189 TS_RESP
*ret
= TS_RESP_int_new();
192 ret
->tst_info
= NULL
;
196 void TS_RESP_free(TS_RESP
*a
)
201 TS_TST_INFO_free(a
->tst_info
);
205 int i2d_TS_RESP(const TS_RESP
*a
, unsigned char **pp
)
207 return i2d_TS_RESP_int(a
, pp
);
210 TS_RESP
*d2i_TS_RESP(TS_RESP
**a
, const unsigned char **pp
, long len
)
215 ret
= d2i_TS_RESP_int(a
, pp
, len
);
217 TSerr(TS_F_D2I_TS_RESP
, TS_R_D2I_TS_RESP_INT_FAILED
);
220 status
= ASN1_INTEGER_get(ret
->status_info
->status
);
223 if (status
!= 0 && status
!= 1) {
224 TSerr(TS_F_D2I_TS_RESP
, TS_R_TOKEN_PRESENT
);
229 ret
->tst_info
= PKCS7_to_TS_TST_INFO(ret
->token
);
230 if (!ret
->tst_info
) {
231 TSerr(TS_F_D2I_TS_RESP
, TS_R_PKCS7_TO_TS_TST_INFO_FAILED
);
236 } else if (status
== 0 || status
== 1) {
237 TSerr(TS_F_D2I_TS_RESP
, TS_R_TOKEN_NOT_PRESENT
);
246 IMPLEMENT_ASN1_DUP_FUNCTION(TS_RESP
)
247 #ifndef OPENSSL_NO_BIO
248 TS_RESP
*d2i_TS_RESP_bio(BIO
*bp
, TS_RESP
**a
)
250 return ASN1_d2i_bio_of(TS_RESP
, TS_RESP_new
, d2i_TS_RESP
, bp
, a
);
253 int i2d_TS_RESP_bio(BIO
*bp
, TS_RESP
*a
)
255 return ASN1_i2d_bio_of_const(TS_RESP
, i2d_TS_RESP
, bp
, a
);
258 #ifndef OPENSSL_NO_FP_API
259 TS_RESP
*d2i_TS_RESP_fp(FILE *fp
, TS_RESP
**a
)
261 return ASN1_d2i_fp_of(TS_RESP
, TS_RESP_new
, d2i_TS_RESP
, fp
, a
);
264 int i2d_TS_RESP_fp(FILE *fp
, TS_RESP
*a
)
266 return ASN1_i2d_fp_of_const(TS_RESP
, i2d_TS_RESP
, fp
, a
);
270 ASN1_SEQUENCE(ESS_ISSUER_SERIAL
) = {
271 ASN1_SEQUENCE_OF(ESS_ISSUER_SERIAL
, issuer
, GENERAL_NAME
),
272 ASN1_SIMPLE(ESS_ISSUER_SERIAL
, serial
, ASN1_INTEGER
)
273 } ASN1_SEQUENCE_END(ESS_ISSUER_SERIAL
)
275 IMPLEMENT_ASN1_FUNCTIONS_const(ESS_ISSUER_SERIAL
)
276 IMPLEMENT_ASN1_DUP_FUNCTION(ESS_ISSUER_SERIAL
)
278 ASN1_SEQUENCE(ESS_CERT_ID
) = {
279 ASN1_SIMPLE(ESS_CERT_ID
, hash
, ASN1_OCTET_STRING
),
280 ASN1_OPT(ESS_CERT_ID
, issuer_serial
, ESS_ISSUER_SERIAL
)
281 } ASN1_SEQUENCE_END(ESS_CERT_ID
)
283 IMPLEMENT_ASN1_FUNCTIONS_const(ESS_CERT_ID
)
284 IMPLEMENT_ASN1_DUP_FUNCTION(ESS_CERT_ID
)
286 ASN1_SEQUENCE(ESS_SIGNING_CERT
) = {
287 ASN1_SEQUENCE_OF(ESS_SIGNING_CERT
, cert_ids
, ESS_CERT_ID
),
288 ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT
, policy_info
, POLICYINFO
)
289 } ASN1_SEQUENCE_END(ESS_SIGNING_CERT
)
291 IMPLEMENT_ASN1_FUNCTIONS_const(ESS_SIGNING_CERT
)
292 IMPLEMENT_ASN1_DUP_FUNCTION(ESS_SIGNING_CERT
)
294 /* Getting encapsulated TS_TST_INFO object from PKCS7. */
295 TS_TST_INFO
*PKCS7_to_TS_TST_INFO(PKCS7
*token
)
297 PKCS7_SIGNED
*pkcs7_signed
;
299 ASN1_TYPE
*tst_info_wrapper
;
300 ASN1_OCTET_STRING
*tst_info_der
;
301 const unsigned char *p
;
303 if (!PKCS7_type_is_signed(token
))
305 TSerr(TS_F_PKCS7_TO_TS_TST_INFO
, TS_R_BAD_PKCS7_TYPE
);
309 /* Content must be present. */
310 if (PKCS7_get_detached(token
))
312 TSerr(TS_F_PKCS7_TO_TS_TST_INFO
, TS_R_DETACHED_CONTENT
);
316 /* We have a signed data with content. */
317 pkcs7_signed
= token
->d
.sign
;
318 enveloped
= pkcs7_signed
->contents
;
319 if (OBJ_obj2nid(enveloped
->type
) != NID_id_smime_ct_TSTInfo
)
321 TSerr(TS_F_PKCS7_TO_TS_TST_INFO
, TS_R_BAD_PKCS7_TYPE
);
325 /* We have a DER encoded TST_INFO as the signed data. */
326 tst_info_wrapper
= enveloped
->d
.other
;
327 if (tst_info_wrapper
->type
!= V_ASN1_OCTET_STRING
)
329 TSerr(TS_F_PKCS7_TO_TS_TST_INFO
, TS_R_BAD_TYPE
);
333 /* We have the correct ASN1_OCTET_STRING type. */
334 tst_info_der
= tst_info_wrapper
->value
.octet_string
;
335 /* At last, decode the TST_INFO. */
336 p
= tst_info_der
->data
;
337 return d2i_TS_TST_INFO(NULL
, &p
, tst_info_der
->length
);