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