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