xref: /openssl/crypto/ts/ts_asn1.c (revision 605856d7)
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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(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,const TS_MSG_IMPRINT * a)28 int i2d_TS_MSG_IMPRINT_bio(BIO *bp, const TS_MSG_IMPRINT *a)
29 {
30     return ASN1_i2d_bio_of(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,const TS_MSG_IMPRINT * a)39 int i2d_TS_MSG_IMPRINT_fp(FILE *fp, const TS_MSG_IMPRINT *a)
40 {
41     return ASN1_i2d_fp_of(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(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,const TS_REQ * a)61 int i2d_TS_REQ_bio(BIO *bp, const TS_REQ *a)
62 {
63     return ASN1_i2d_bio_of(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,const TS_REQ * a)71 int i2d_TS_REQ_fp(FILE *fp, const TS_REQ *a)
72 {
73     return ASN1_i2d_fp_of(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(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(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,const TS_TST_INFO * a)107 int i2d_TS_TST_INFO_bio(BIO *bp, const TS_TST_INFO *a)
108 {
109     return ASN1_i2d_bio_of(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,const TS_TST_INFO * a)118 int i2d_TS_TST_INFO_fp(FILE *fp, const TS_TST_INFO *a)
119 {
120     return ASN1_i2d_fp_of(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(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             ERR_raise(ERR_LIB_TS, 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             ERR_raise(ERR_LIB_TS, TS_R_PKCS7_TO_TS_TST_INFO_FAILED);
148             return 0;
149         }
150     } else if (status == 0 || status == 1) {
151         ERR_raise(ERR_LIB_TS, TS_R_TOKEN_NOT_PRESENT);
152         return 0;
153     }
154 
155     return 1;
156 }
157 
ts_resp_cb(int op,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)158 static int ts_resp_cb(int op, ASN1_VALUE **pval, const ASN1_ITEM *it,
159                       void *exarg)
160 {
161     TS_RESP *ts_resp = (TS_RESP *)*pval;
162     if (op == ASN1_OP_NEW_POST) {
163         ts_resp->tst_info = NULL;
164     } else if (op == ASN1_OP_FREE_POST) {
165         TS_TST_INFO_free(ts_resp->tst_info);
166     } else if (op == ASN1_OP_D2I_POST) {
167         if (ts_resp_set_tst_info(ts_resp) == 0)
168             return 0;
169     }
170     return 1;
171 }
172 
173 ASN1_SEQUENCE_cb(TS_RESP, ts_resp_cb) = {
174         ASN1_SIMPLE(TS_RESP, status_info, TS_STATUS_INFO),
175         ASN1_OPT(TS_RESP, token, PKCS7),
176 } static_ASN1_SEQUENCE_END_cb(TS_RESP, TS_RESP)
177 
178 IMPLEMENT_ASN1_FUNCTIONS(TS_RESP)
179 
180 IMPLEMENT_ASN1_DUP_FUNCTION(TS_RESP)
181 
182 TS_RESP *d2i_TS_RESP_bio(BIO *bp, TS_RESP **a)
183 {
184     return ASN1_d2i_bio_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, bp, a);
185 }
186 
i2d_TS_RESP_bio(BIO * bp,const TS_RESP * a)187 int i2d_TS_RESP_bio(BIO *bp, const TS_RESP *a)
188 {
189     return ASN1_i2d_bio_of(TS_RESP, i2d_TS_RESP, bp, a);
190 }
191 #ifndef OPENSSL_NO_STDIO
d2i_TS_RESP_fp(FILE * fp,TS_RESP ** a)192 TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a)
193 {
194     return ASN1_d2i_fp_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, fp, a);
195 }
196 
i2d_TS_RESP_fp(FILE * fp,const TS_RESP * a)197 int i2d_TS_RESP_fp(FILE *fp, const TS_RESP *a)
198 {
199     return ASN1_i2d_fp_of(TS_RESP, i2d_TS_RESP, fp, a);
200 }
201 #endif
202 
203 /* Getting encapsulated TS_TST_INFO object from PKCS7. */
PKCS7_to_TS_TST_INFO(PKCS7 * token)204 TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token)
205 {
206     PKCS7_SIGNED *pkcs7_signed;
207     PKCS7 *enveloped;
208     ASN1_TYPE *tst_info_wrapper;
209     ASN1_OCTET_STRING *tst_info_der;
210     const unsigned char *p;
211 
212     if (!PKCS7_type_is_signed(token)) {
213         ERR_raise(ERR_LIB_TS, TS_R_BAD_PKCS7_TYPE);
214         return NULL;
215     }
216     if (PKCS7_get_detached(token)) {
217         ERR_raise(ERR_LIB_TS, TS_R_DETACHED_CONTENT);
218         return NULL;
219     }
220     pkcs7_signed = token->d.sign;
221     enveloped = pkcs7_signed->contents;
222     if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo) {
223         ERR_raise(ERR_LIB_TS, TS_R_BAD_PKCS7_TYPE);
224         return NULL;
225     }
226     tst_info_wrapper = enveloped->d.other;
227     if (tst_info_wrapper->type != V_ASN1_OCTET_STRING) {
228         ERR_raise(ERR_LIB_TS, TS_R_BAD_TYPE);
229         return NULL;
230     }
231     tst_info_der = tst_info_wrapper->value.octet_string;
232     p = tst_info_der->data;
233     return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
234 }
235