1 /* $OpenBSD: ts_rsp_utils.c,v 1.7 2018/05/13 15:35:46 tb Exp $ */
2 /* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3  * project 2002.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/err.h>
62 #include <openssl/objects.h>
63 #include <openssl/pkcs7.h>
64 #include <openssl/ts.h>
65 
66 /* Function definitions. */
67 
68 int
69 TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
70 {
71 	TS_STATUS_INFO *new_status_info;
72 
73 	if (a->status_info == status_info)
74 		return 1;
75 	new_status_info = TS_STATUS_INFO_dup(status_info);
76 	if (new_status_info == NULL) {
77 		TSerror(ERR_R_MALLOC_FAILURE);
78 		return 0;
79 	}
80 	TS_STATUS_INFO_free(a->status_info);
81 	a->status_info = new_status_info;
82 
83 	return 1;
84 }
85 
86 TS_STATUS_INFO *
87 TS_RESP_get_status_info(TS_RESP *a)
88 {
89 	return a->status_info;
90 }
91 
92 /* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
93 void
94 TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
95 {
96 	/* Set new PKCS7 and TST_INFO objects. */
97 	PKCS7_free(a->token);
98 	a->token = p7;
99 	TS_TST_INFO_free(a->tst_info);
100 	a->tst_info = tst_info;
101 }
102 
103 PKCS7 *
104 TS_RESP_get_token(TS_RESP *a)
105 {
106 	return a->token;
107 }
108 
109 TS_TST_INFO *
110 TS_RESP_get_tst_info(TS_RESP *a)
111 {
112 	return a->tst_info;
113 }
114 
115 int
116 TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
117 {
118 	return ASN1_INTEGER_set(a->version, version);
119 }
120 
121 long
122 TS_TST_INFO_get_version(const TS_TST_INFO *a)
123 {
124 	return ASN1_INTEGER_get(a->version);
125 }
126 
127 int
128 TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
129 {
130 	ASN1_OBJECT *new_policy;
131 
132 	if (a->policy_id == policy)
133 		return 1;
134 	new_policy = OBJ_dup(policy);
135 	if (new_policy == NULL) {
136 		TSerror(ERR_R_MALLOC_FAILURE);
137 		return 0;
138 	}
139 	ASN1_OBJECT_free(a->policy_id);
140 	a->policy_id = new_policy;
141 	return 1;
142 }
143 
144 ASN1_OBJECT *
145 TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
146 {
147 	return a->policy_id;
148 }
149 
150 int
151 TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
152 {
153 	TS_MSG_IMPRINT *new_msg_imprint;
154 
155 	if (a->msg_imprint == msg_imprint)
156 		return 1;
157 	new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
158 	if (new_msg_imprint == NULL) {
159 		TSerror(ERR_R_MALLOC_FAILURE);
160 		return 0;
161 	}
162 	TS_MSG_IMPRINT_free(a->msg_imprint);
163 	a->msg_imprint = new_msg_imprint;
164 	return 1;
165 }
166 
167 TS_MSG_IMPRINT *
168 TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
169 {
170 	return a->msg_imprint;
171 }
172 
173 int
174 TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
175 {
176 	ASN1_INTEGER *new_serial;
177 
178 	if (a->serial == serial)
179 		return 1;
180 	new_serial = ASN1_INTEGER_dup(serial);
181 	if (new_serial == NULL) {
182 		TSerror(ERR_R_MALLOC_FAILURE);
183 		return 0;
184 	}
185 	ASN1_INTEGER_free(a->serial);
186 	a->serial = new_serial;
187 	return 1;
188 }
189 
190 const ASN1_INTEGER *
191 TS_TST_INFO_get_serial(const TS_TST_INFO *a)
192 {
193 	return a->serial;
194 }
195 
196 int
197 TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
198 {
199 	ASN1_GENERALIZEDTIME *new_time;
200 
201 	if (a->time == gtime)
202 		return 1;
203 	new_time = ASN1_STRING_dup(gtime);
204 	if (new_time == NULL) {
205 		TSerror(ERR_R_MALLOC_FAILURE);
206 		return 0;
207 	}
208 	ASN1_GENERALIZEDTIME_free(a->time);
209 	a->time = new_time;
210 	return 1;
211 }
212 
213 const ASN1_GENERALIZEDTIME *
214 TS_TST_INFO_get_time(const TS_TST_INFO *a)
215 {
216 	return a->time;
217 }
218 
219 int
220 TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
221 {
222 	TS_ACCURACY *new_accuracy;
223 
224 	if (a->accuracy == accuracy)
225 		return 1;
226 	new_accuracy = TS_ACCURACY_dup(accuracy);
227 	if (new_accuracy == NULL) {
228 		TSerror(ERR_R_MALLOC_FAILURE);
229 		return 0;
230 	}
231 	TS_ACCURACY_free(a->accuracy);
232 	a->accuracy = new_accuracy;
233 	return 1;
234 }
235 
236 TS_ACCURACY *
237 TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
238 {
239 	return a->accuracy;
240 }
241 
242 int
243 TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
244 {
245 	ASN1_INTEGER *new_seconds;
246 
247 	if (a->seconds == seconds)
248 		return 1;
249 	new_seconds = ASN1_INTEGER_dup(seconds);
250 	if (new_seconds == NULL) {
251 		TSerror(ERR_R_MALLOC_FAILURE);
252 		return 0;
253 	}
254 	ASN1_INTEGER_free(a->seconds);
255 	a->seconds = new_seconds;
256 	return 1;
257 }
258 
259 const ASN1_INTEGER *
260 TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
261 {
262 	return a->seconds;
263 }
264 
265 int
266 TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
267 {
268 	ASN1_INTEGER *new_millis = NULL;
269 
270 	if (a->millis == millis)
271 		return 1;
272 	if (millis != NULL) {
273 		new_millis = ASN1_INTEGER_dup(millis);
274 		if (new_millis == NULL) {
275 			TSerror(ERR_R_MALLOC_FAILURE);
276 			return 0;
277 		}
278 	}
279 	ASN1_INTEGER_free(a->millis);
280 	a->millis = new_millis;
281 	return 1;
282 }
283 
284 const ASN1_INTEGER *
285 TS_ACCURACY_get_millis(const TS_ACCURACY *a)
286 {
287 	return a->millis;
288 }
289 
290 int
291 TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
292 {
293 	ASN1_INTEGER *new_micros = NULL;
294 
295 	if (a->micros == micros)
296 		return 1;
297 	if (micros != NULL) {
298 		new_micros = ASN1_INTEGER_dup(micros);
299 		if (new_micros == NULL) {
300 			TSerror(ERR_R_MALLOC_FAILURE);
301 			return 0;
302 		}
303 	}
304 	ASN1_INTEGER_free(a->micros);
305 	a->micros = new_micros;
306 	return 1;
307 }
308 
309 const ASN1_INTEGER *
310 TS_ACCURACY_get_micros(const TS_ACCURACY *a)
311 {
312 	return a->micros;
313 }
314 
315 int
316 TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
317 {
318 	a->ordering = ordering ? 0xFF : 0x00;
319 	return 1;
320 }
321 
322 int
323 TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
324 {
325 	return a->ordering ? 1 : 0;
326 }
327 
328 int
329 TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
330 {
331 	ASN1_INTEGER *new_nonce;
332 
333 	if (a->nonce == nonce)
334 		return 1;
335 	new_nonce = ASN1_INTEGER_dup(nonce);
336 	if (new_nonce == NULL) {
337 		TSerror(ERR_R_MALLOC_FAILURE);
338 		return 0;
339 	}
340 	ASN1_INTEGER_free(a->nonce);
341 	a->nonce = new_nonce;
342 	return 1;
343 }
344 
345 const ASN1_INTEGER *
346 TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
347 {
348 	return a->nonce;
349 }
350 
351 int
352 TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
353 {
354 	GENERAL_NAME *new_tsa;
355 
356 	if (a->tsa == tsa)
357 		return 1;
358 	new_tsa = GENERAL_NAME_dup(tsa);
359 	if (new_tsa == NULL) {
360 		TSerror(ERR_R_MALLOC_FAILURE);
361 		return 0;
362 	}
363 	GENERAL_NAME_free(a->tsa);
364 	a->tsa = new_tsa;
365 	return 1;
366 }
367 
368 GENERAL_NAME *
369 TS_TST_INFO_get_tsa(TS_TST_INFO *a)
370 {
371 	return a->tsa;
372 }
373 
374 STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
375 {
376 	return a->extensions;
377 }
378 
379 void
380 TS_TST_INFO_ext_free(TS_TST_INFO *a)
381 {
382 	if (!a)
383 		return;
384 	sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
385 	a->extensions = NULL;
386 }
387 
388 int
389 TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
390 {
391 	return X509v3_get_ext_count(a->extensions);
392 }
393 
394 int
395 TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
396 {
397 	return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
398 }
399 
400 int
401 TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, int lastpos)
402 {
403 	return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
404 }
405 
406 int
407 TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
408 {
409 	return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
410 }
411 
412 X509_EXTENSION *
413 TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
414 {
415 	return X509v3_get_ext(a->extensions, loc);
416 }
417 
418 X509_EXTENSION *
419 TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
420 {
421 	return X509v3_delete_ext(a->extensions, loc);
422 }
423 
424 int
425 TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
426 {
427 	return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
428 }
429 
430 void *
431 TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
432 {
433 	return X509V3_get_d2i(a->extensions, nid, crit, idx);
434 }
435