1/*
2 * Copyright 2006-2016 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
15ASN1_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
20IMPLEMENT_ASN1_FUNCTIONS(TS_MSG_IMPRINT)
21IMPLEMENT_ASN1_DUP_FUNCTION(TS_MSG_IMPRINT)
22TS_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
28int 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
33TS_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
39int 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
45ASN1_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
54IMPLEMENT_ASN1_FUNCTIONS(TS_REQ)
55IMPLEMENT_ASN1_DUP_FUNCTION(TS_REQ)
56TS_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
61int 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
66TS_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
71int 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
77ASN1_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
83IMPLEMENT_ASN1_FUNCTIONS(TS_ACCURACY)
84IMPLEMENT_ASN1_DUP_FUNCTION(TS_ACCURACY)
85
86ASN1_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
99IMPLEMENT_ASN1_FUNCTIONS(TS_TST_INFO)
100IMPLEMENT_ASN1_DUP_FUNCTION(TS_TST_INFO)
101TS_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
107int 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
112TS_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
118int 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
124ASN1_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
130IMPLEMENT_ASN1_FUNCTIONS(TS_STATUS_INFO)
131IMPLEMENT_ASN1_DUP_FUNCTION(TS_STATUS_INFO)
132
133static 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
159static 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
174ASN1_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
179IMPLEMENT_ASN1_FUNCTIONS(TS_RESP)
180
181IMPLEMENT_ASN1_DUP_FUNCTION(TS_RESP)
182
183TS_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
188int i2d_TS_RESP_bio(BIO *bp, const TS_RESP *a)
189{
190 return ASN1_i2d_bio_of(TS_RESP, i2d_TS_RESP, bp, a);
191}
192#ifndef OPENSSL_NO_STDIO
193TS_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
198int i2d_TS_RESP_fp(FILE *fp, const TS_RESP *a)
199{
200 return ASN1_i2d_fp_of(TS_RESP, i2d_TS_RESP, fp, a);
201}
202#endif
203
204/* Getting encapsulated TS_TST_INFO object from PKCS7. */
205TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token)
206{
207 PKCS7_SIGNED *pkcs7_signed;
208 PKCS7 *enveloped;
209 ASN1_TYPE *tst_info_wrapper;
210 ASN1_OCTET_STRING *tst_info_der;
211 const unsigned char *p;
212
213 if (!PKCS7_type_is_signed(token)) {
214 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
215 return NULL;
216 }
217 if (PKCS7_get_detached(token)) {
218 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_DETACHED_CONTENT);
219 return NULL;
220 }
221 pkcs7_signed = token->d.sign;
222 enveloped = pkcs7_signed->contents;
223 if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo) {
224 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
225 return NULL;
226 }
227 tst_info_wrapper = enveloped->d.other;
228 if (tst_info_wrapper->type != V_ASN1_OCTET_STRING) {
229 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_TYPE);
230 return NULL;
231 }
232 tst_info_der = tst_info_wrapper->value.octet_string;
233 p = tst_info_der->data;
234 return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
235}
236