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 <stdio.h>
11#include "internal/cryptlib.h"
12#include <openssl/objects.h>
13#include <openssl/ts.h>
14#include <openssl/pkcs7.h>
15#include "ts_local.h"
16
17int TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
18{
19    TS_STATUS_INFO *new_status_info;
20
21    if (a->status_info == status_info)
22        return 1;
23    new_status_info = TS_STATUS_INFO_dup(status_info);
24    if (new_status_info == NULL) {
25        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
26        return 0;
27    }
28    TS_STATUS_INFO_free(a->status_info);
29    a->status_info = new_status_info;
30
31    return 1;
32}
33
34TS_STATUS_INFO *TS_RESP_get_status_info(TS_RESP *a)
35{
36    return a->status_info;
37}
38
39/* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
40void TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
41{
42    PKCS7_free(a->token);
43    a->token = p7;
44    TS_TST_INFO_free(a->tst_info);
45    a->tst_info = tst_info;
46}
47
48PKCS7 *TS_RESP_get_token(TS_RESP *a)
49{
50    return a->token;
51}
52
53TS_TST_INFO *TS_RESP_get_tst_info(TS_RESP *a)
54{
55    return a->tst_info;
56}
57
58int TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
59{
60    return ASN1_INTEGER_set(a->version, version);
61}
62
63long TS_TST_INFO_get_version(const TS_TST_INFO *a)
64{
65    return ASN1_INTEGER_get(a->version);
66}
67
68int TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
69{
70    ASN1_OBJECT *new_policy;
71
72    if (a->policy_id == policy)
73        return 1;
74    new_policy = OBJ_dup(policy);
75    if (new_policy == NULL) {
76        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
77        return 0;
78    }
79    ASN1_OBJECT_free(a->policy_id);
80    a->policy_id = new_policy;
81    return 1;
82}
83
84ASN1_OBJECT *TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
85{
86    return a->policy_id;
87}
88
89int TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
90{
91    TS_MSG_IMPRINT *new_msg_imprint;
92
93    if (a->msg_imprint == msg_imprint)
94        return 1;
95    new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
96    if (new_msg_imprint == NULL) {
97        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
98        return 0;
99    }
100    TS_MSG_IMPRINT_free(a->msg_imprint);
101    a->msg_imprint = new_msg_imprint;
102    return 1;
103}
104
105TS_MSG_IMPRINT *TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
106{
107    return a->msg_imprint;
108}
109
110int TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
111{
112    ASN1_INTEGER *new_serial;
113
114    if (a->serial == serial)
115        return 1;
116    new_serial = ASN1_INTEGER_dup(serial);
117    if (new_serial == NULL) {
118        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
119        return 0;
120    }
121    ASN1_INTEGER_free(a->serial);
122    a->serial = new_serial;
123    return 1;
124}
125
126const ASN1_INTEGER *TS_TST_INFO_get_serial(const TS_TST_INFO *a)
127{
128    return a->serial;
129}
130
131int TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
132{
133    ASN1_GENERALIZEDTIME *new_time;
134
135    if (a->time == gtime)
136        return 1;
137    new_time = ASN1_STRING_dup(gtime);
138    if (new_time == NULL) {
139        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
140        return 0;
141    }
142    ASN1_GENERALIZEDTIME_free(a->time);
143    a->time = new_time;
144    return 1;
145}
146
147const ASN1_GENERALIZEDTIME *TS_TST_INFO_get_time(const TS_TST_INFO *a)
148{
149    return a->time;
150}
151
152int TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
153{
154    TS_ACCURACY *new_accuracy;
155
156    if (a->accuracy == accuracy)
157        return 1;
158    new_accuracy = TS_ACCURACY_dup(accuracy);
159    if (new_accuracy == NULL) {
160        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
161        return 0;
162    }
163    TS_ACCURACY_free(a->accuracy);
164    a->accuracy = new_accuracy;
165    return 1;
166}
167
168TS_ACCURACY *TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
169{
170    return a->accuracy;
171}
172
173int TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
174{
175    ASN1_INTEGER *new_seconds;
176
177    if (a->seconds == seconds)
178        return 1;
179    new_seconds = ASN1_INTEGER_dup(seconds);
180    if (new_seconds == NULL) {
181        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
182        return 0;
183    }
184    ASN1_INTEGER_free(a->seconds);
185    a->seconds = new_seconds;
186    return 1;
187}
188
189const ASN1_INTEGER *TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
190{
191    return a->seconds;
192}
193
194int TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
195{
196    ASN1_INTEGER *new_millis = NULL;
197
198    if (a->millis == millis)
199        return 1;
200    if (millis != NULL) {
201        new_millis = ASN1_INTEGER_dup(millis);
202        if (new_millis == NULL) {
203            ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
204            return 0;
205        }
206    }
207    ASN1_INTEGER_free(a->millis);
208    a->millis = new_millis;
209    return 1;
210}
211
212const ASN1_INTEGER *TS_ACCURACY_get_millis(const TS_ACCURACY *a)
213{
214    return a->millis;
215}
216
217int TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
218{
219    ASN1_INTEGER *new_micros = NULL;
220
221    if (a->micros == micros)
222        return 1;
223    if (micros != NULL) {
224        new_micros = ASN1_INTEGER_dup(micros);
225        if (new_micros == NULL) {
226            ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
227            return 0;
228        }
229    }
230    ASN1_INTEGER_free(a->micros);
231    a->micros = new_micros;
232    return 1;
233}
234
235const ASN1_INTEGER *TS_ACCURACY_get_micros(const TS_ACCURACY *a)
236{
237    return a->micros;
238}
239
240int TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
241{
242    a->ordering = ordering ? 0xFF : 0x00;
243    return 1;
244}
245
246int TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
247{
248    return a->ordering ? 1 : 0;
249}
250
251int TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
252{
253    ASN1_INTEGER *new_nonce;
254
255    if (a->nonce == nonce)
256        return 1;
257    new_nonce = ASN1_INTEGER_dup(nonce);
258    if (new_nonce == NULL) {
259        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
260        return 0;
261    }
262    ASN1_INTEGER_free(a->nonce);
263    a->nonce = new_nonce;
264    return 1;
265}
266
267const ASN1_INTEGER *TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
268{
269    return a->nonce;
270}
271
272int TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
273{
274    GENERAL_NAME *new_tsa;
275
276    if (a->tsa == tsa)
277        return 1;
278    new_tsa = GENERAL_NAME_dup(tsa);
279    if (new_tsa == NULL) {
280        ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE);
281        return 0;
282    }
283    GENERAL_NAME_free(a->tsa);
284    a->tsa = new_tsa;
285    return 1;
286}
287
288GENERAL_NAME *TS_TST_INFO_get_tsa(TS_TST_INFO *a)
289{
290    return a->tsa;
291}
292
293STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
294{
295    return a->extensions;
296}
297
298void TS_TST_INFO_ext_free(TS_TST_INFO *a)
299{
300    if (!a)
301        return;
302    sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
303    a->extensions = NULL;
304}
305
306int TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
307{
308    return X509v3_get_ext_count(a->extensions);
309}
310
311int TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
312{
313    return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
314}
315
316int TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, int lastpos)
317{
318    return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
319}
320
321int TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
322{
323    return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
324}
325
326X509_EXTENSION *TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
327{
328    return X509v3_get_ext(a->extensions, loc);
329}
330
331X509_EXTENSION *TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
332{
333    return X509v3_delete_ext(a->extensions, loc);
334}
335
336int TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
337{
338    return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
339}
340
341void *TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
342{
343    return X509V3_get_d2i(a->extensions, nid, crit, idx);
344}
345
346int TS_STATUS_INFO_set_status(TS_STATUS_INFO *a, int i)
347{
348    return ASN1_INTEGER_set(a->status, i);
349}
350
351const ASN1_INTEGER *TS_STATUS_INFO_get0_status(const TS_STATUS_INFO *a)
352{
353    return a->status;
354}
355
356const STACK_OF(ASN1_UTF8STRING) *
357TS_STATUS_INFO_get0_text(const TS_STATUS_INFO *a)
358{
359    return a->text;
360}
361
362const ASN1_BIT_STRING *TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO *a)
363{
364    return a->failure_info;
365}
366