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