1 /* $OpenBSD: ts_rsp_utils.c,v 1.4 2014/07/11 08:44:49 jsing 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 		TSerr(TS_F_TS_RESP_SET_STATUS_INFO, 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 		TSerr(TS_F_TS_TST_INFO_SET_POLICY_ID, 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 		TSerr(TS_F_TS_TST_INFO_SET_MSG_IMPRINT, 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 		TSerr(TS_F_TS_TST_INFO_SET_SERIAL, 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 		TSerr(TS_F_TS_TST_INFO_SET_TIME, 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 		TSerr(TS_F_TS_TST_INFO_SET_ACCURACY, 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 		TSerr(TS_F_TS_ACCURACY_SET_SECONDS, 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 			TSerr(TS_F_TS_ACCURACY_SET_MILLIS,
276 			    ERR_R_MALLOC_FAILURE);
277 			return 0;
278 		}
279 	}
280 	ASN1_INTEGER_free(a->millis);
281 	a->millis = new_millis;
282 	return 1;
283 }
284 
285 const ASN1_INTEGER *
286 TS_ACCURACY_get_millis(const TS_ACCURACY *a)
287 {
288 	return a->millis;
289 }
290 
291 int
292 TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
293 {
294 	ASN1_INTEGER *new_micros = NULL;
295 
296 	if (a->micros == micros)
297 		return 1;
298 	if (micros != NULL) {
299 		new_micros = ASN1_INTEGER_dup(micros);
300 		if (new_micros == NULL) {
301 			TSerr(TS_F_TS_ACCURACY_SET_MICROS,
302 			    ERR_R_MALLOC_FAILURE);
303 			return 0;
304 		}
305 	}
306 	ASN1_INTEGER_free(a->micros);
307 	a->micros = new_micros;
308 	return 1;
309 }
310 
311 const ASN1_INTEGER *
312 TS_ACCURACY_get_micros(const TS_ACCURACY *a)
313 {
314 	return a->micros;
315 }
316 
317 int
318 TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
319 {
320 	a->ordering = ordering ? 0xFF : 0x00;
321 	return 1;
322 }
323 
324 int
325 TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
326 {
327 	return a->ordering ? 1 : 0;
328 }
329 
330 int
331 TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
332 {
333 	ASN1_INTEGER *new_nonce;
334 
335 	if (a->nonce == nonce)
336 		return 1;
337 	new_nonce = ASN1_INTEGER_dup(nonce);
338 	if (new_nonce == NULL) {
339 		TSerr(TS_F_TS_TST_INFO_SET_NONCE, ERR_R_MALLOC_FAILURE);
340 		return 0;
341 	}
342 	ASN1_INTEGER_free(a->nonce);
343 	a->nonce = new_nonce;
344 	return 1;
345 }
346 
347 const ASN1_INTEGER *
348 TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
349 {
350 	return a->nonce;
351 }
352 
353 int
354 TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
355 {
356 	GENERAL_NAME *new_tsa;
357 
358 	if (a->tsa == tsa)
359 		return 1;
360 	new_tsa = GENERAL_NAME_dup(tsa);
361 	if (new_tsa == NULL) {
362 		TSerr(TS_F_TS_TST_INFO_SET_TSA, ERR_R_MALLOC_FAILURE);
363 		return 0;
364 	}
365 	GENERAL_NAME_free(a->tsa);
366 	a->tsa = new_tsa;
367 	return 1;
368 }
369 
370 GENERAL_NAME *
371 TS_TST_INFO_get_tsa(TS_TST_INFO *a)
372 {
373 	return a->tsa;
374 }
375 
376 STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
377 {
378 	return a->extensions;
379 }
380 
381 void
382 TS_TST_INFO_ext_free(TS_TST_INFO *a)
383 {
384 	if (!a)
385 		return;
386 	sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
387 	a->extensions = NULL;
388 }
389 
390 int
391 TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
392 {
393 	return X509v3_get_ext_count(a->extensions);
394 }
395 
396 int
397 TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
398 {
399 	return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
400 }
401 
402 int
403 TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, ASN1_OBJECT *obj, int lastpos)
404 {
405 	return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
406 }
407 
408 int
409 TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
410 {
411 	return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
412 }
413 
414 X509_EXTENSION *
415 TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
416 {
417 	return X509v3_get_ext(a->extensions, loc);
418 }
419 
420 X509_EXTENSION *
421 TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
422 {
423 	return X509v3_delete_ext(a->extensions, loc);
424 }
425 
426 int
427 TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
428 {
429 	return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
430 }
431 
432 void *
433 TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
434 {
435 	return X509V3_get_d2i(a->extensions, nid, crit, idx);
436 }
437