xref: /openbsd/lib/libcrypto/ts/ts_rsp_utils.c (revision 1ec3c770)
1 /* $OpenBSD: ts_rsp_utils.c,v 1.11 2023/07/07 19:37:54 beck 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 #include "ts_local.h"
67 
68 /* Function definitions. */
69 
70 int
TS_RESP_set_status_info(TS_RESP * a,TS_STATUS_INFO * status_info)71 TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
72 {
73 	TS_STATUS_INFO *new_status_info;
74 
75 	if (a->status_info == status_info)
76 		return 1;
77 	new_status_info = TS_STATUS_INFO_dup(status_info);
78 	if (new_status_info == NULL) {
79 		TSerror(ERR_R_MALLOC_FAILURE);
80 		return 0;
81 	}
82 	TS_STATUS_INFO_free(a->status_info);
83 	a->status_info = new_status_info;
84 
85 	return 1;
86 }
87 LCRYPTO_ALIAS(TS_RESP_set_status_info);
88 
89 TS_STATUS_INFO *
TS_RESP_get_status_info(TS_RESP * a)90 TS_RESP_get_status_info(TS_RESP *a)
91 {
92 	return a->status_info;
93 }
94 LCRYPTO_ALIAS(TS_RESP_get_status_info);
95 
96 const ASN1_UTF8STRING *
TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO * si)97 TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO *si)
98 {
99 	return si->failure_info;
100 }
101 LCRYPTO_ALIAS(TS_STATUS_INFO_get0_failure_info);
102 
STACK_OF(ASN1_UTF8STRING)103 const STACK_OF(ASN1_UTF8STRING) *
104 TS_STATUS_INFO_get0_text(const TS_STATUS_INFO *si)
105 {
106 	return si->text;
107 }
108 LCRYPTO_ALIAS(TS_STATUS_INFO_get0_text);
109 
110 const ASN1_INTEGER *
TS_STATUS_INFO_get0_status(const TS_STATUS_INFO * si)111 TS_STATUS_INFO_get0_status(const TS_STATUS_INFO *si)
112 {
113 	return si->status;
114 }
115 LCRYPTO_ALIAS(TS_STATUS_INFO_get0_status);
116 
117 int
TS_STATUS_INFO_set_status(TS_STATUS_INFO * si,int i)118 TS_STATUS_INFO_set_status(TS_STATUS_INFO *si, int i)
119 {
120 	return ASN1_INTEGER_set(si->status, i);
121 }
122 LCRYPTO_ALIAS(TS_STATUS_INFO_set_status);
123 
124 /* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
125 void
TS_RESP_set_tst_info(TS_RESP * a,PKCS7 * p7,TS_TST_INFO * tst_info)126 TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
127 {
128 	/* Set new PKCS7 and TST_INFO objects. */
129 	PKCS7_free(a->token);
130 	a->token = p7;
131 	TS_TST_INFO_free(a->tst_info);
132 	a->tst_info = tst_info;
133 }
134 LCRYPTO_ALIAS(TS_RESP_set_tst_info);
135 
136 PKCS7 *
TS_RESP_get_token(TS_RESP * a)137 TS_RESP_get_token(TS_RESP *a)
138 {
139 	return a->token;
140 }
141 LCRYPTO_ALIAS(TS_RESP_get_token);
142 
143 TS_TST_INFO *
TS_RESP_get_tst_info(TS_RESP * a)144 TS_RESP_get_tst_info(TS_RESP *a)
145 {
146 	return a->tst_info;
147 }
148 LCRYPTO_ALIAS(TS_RESP_get_tst_info);
149 
150 int
TS_TST_INFO_set_version(TS_TST_INFO * a,long version)151 TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
152 {
153 	return ASN1_INTEGER_set(a->version, version);
154 }
155 LCRYPTO_ALIAS(TS_TST_INFO_set_version);
156 
157 long
TS_TST_INFO_get_version(const TS_TST_INFO * a)158 TS_TST_INFO_get_version(const TS_TST_INFO *a)
159 {
160 	return ASN1_INTEGER_get(a->version);
161 }
162 LCRYPTO_ALIAS(TS_TST_INFO_get_version);
163 
164 int
TS_TST_INFO_set_policy_id(TS_TST_INFO * a,ASN1_OBJECT * policy)165 TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
166 {
167 	ASN1_OBJECT *new_policy;
168 
169 	if (a->policy_id == policy)
170 		return 1;
171 	new_policy = OBJ_dup(policy);
172 	if (new_policy == NULL) {
173 		TSerror(ERR_R_MALLOC_FAILURE);
174 		return 0;
175 	}
176 	ASN1_OBJECT_free(a->policy_id);
177 	a->policy_id = new_policy;
178 	return 1;
179 }
180 LCRYPTO_ALIAS(TS_TST_INFO_set_policy_id);
181 
182 ASN1_OBJECT *
TS_TST_INFO_get_policy_id(TS_TST_INFO * a)183 TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
184 {
185 	return a->policy_id;
186 }
187 LCRYPTO_ALIAS(TS_TST_INFO_get_policy_id);
188 
189 int
TS_TST_INFO_set_msg_imprint(TS_TST_INFO * a,TS_MSG_IMPRINT * msg_imprint)190 TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
191 {
192 	TS_MSG_IMPRINT *new_msg_imprint;
193 
194 	if (a->msg_imprint == msg_imprint)
195 		return 1;
196 	new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
197 	if (new_msg_imprint == NULL) {
198 		TSerror(ERR_R_MALLOC_FAILURE);
199 		return 0;
200 	}
201 	TS_MSG_IMPRINT_free(a->msg_imprint);
202 	a->msg_imprint = new_msg_imprint;
203 	return 1;
204 }
205 LCRYPTO_ALIAS(TS_TST_INFO_set_msg_imprint);
206 
207 TS_MSG_IMPRINT *
TS_TST_INFO_get_msg_imprint(TS_TST_INFO * a)208 TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
209 {
210 	return a->msg_imprint;
211 }
212 LCRYPTO_ALIAS(TS_TST_INFO_get_msg_imprint);
213 
214 int
TS_TST_INFO_set_serial(TS_TST_INFO * a,const ASN1_INTEGER * serial)215 TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
216 {
217 	ASN1_INTEGER *new_serial;
218 
219 	if (a->serial == serial)
220 		return 1;
221 	new_serial = ASN1_INTEGER_dup(serial);
222 	if (new_serial == NULL) {
223 		TSerror(ERR_R_MALLOC_FAILURE);
224 		return 0;
225 	}
226 	ASN1_INTEGER_free(a->serial);
227 	a->serial = new_serial;
228 	return 1;
229 }
230 LCRYPTO_ALIAS(TS_TST_INFO_set_serial);
231 
232 const ASN1_INTEGER *
TS_TST_INFO_get_serial(const TS_TST_INFO * a)233 TS_TST_INFO_get_serial(const TS_TST_INFO *a)
234 {
235 	return a->serial;
236 }
237 LCRYPTO_ALIAS(TS_TST_INFO_get_serial);
238 
239 int
TS_TST_INFO_set_time(TS_TST_INFO * a,const ASN1_GENERALIZEDTIME * gtime)240 TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
241 {
242 	ASN1_GENERALIZEDTIME *new_time;
243 
244 	if (a->time == gtime)
245 		return 1;
246 	new_time = ASN1_STRING_dup(gtime);
247 	if (new_time == NULL) {
248 		TSerror(ERR_R_MALLOC_FAILURE);
249 		return 0;
250 	}
251 	ASN1_GENERALIZEDTIME_free(a->time);
252 	a->time = new_time;
253 	return 1;
254 }
255 LCRYPTO_ALIAS(TS_TST_INFO_set_time);
256 
257 const ASN1_GENERALIZEDTIME *
TS_TST_INFO_get_time(const TS_TST_INFO * a)258 TS_TST_INFO_get_time(const TS_TST_INFO *a)
259 {
260 	return a->time;
261 }
262 LCRYPTO_ALIAS(TS_TST_INFO_get_time);
263 
264 int
TS_TST_INFO_set_accuracy(TS_TST_INFO * a,TS_ACCURACY * accuracy)265 TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
266 {
267 	TS_ACCURACY *new_accuracy;
268 
269 	if (a->accuracy == accuracy)
270 		return 1;
271 	new_accuracy = TS_ACCURACY_dup(accuracy);
272 	if (new_accuracy == NULL) {
273 		TSerror(ERR_R_MALLOC_FAILURE);
274 		return 0;
275 	}
276 	TS_ACCURACY_free(a->accuracy);
277 	a->accuracy = new_accuracy;
278 	return 1;
279 }
280 LCRYPTO_ALIAS(TS_TST_INFO_set_accuracy);
281 
282 TS_ACCURACY *
TS_TST_INFO_get_accuracy(TS_TST_INFO * a)283 TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
284 {
285 	return a->accuracy;
286 }
287 LCRYPTO_ALIAS(TS_TST_INFO_get_accuracy);
288 
289 int
TS_ACCURACY_set_seconds(TS_ACCURACY * a,const ASN1_INTEGER * seconds)290 TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
291 {
292 	ASN1_INTEGER *new_seconds;
293 
294 	if (a->seconds == seconds)
295 		return 1;
296 	new_seconds = ASN1_INTEGER_dup(seconds);
297 	if (new_seconds == NULL) {
298 		TSerror(ERR_R_MALLOC_FAILURE);
299 		return 0;
300 	}
301 	ASN1_INTEGER_free(a->seconds);
302 	a->seconds = new_seconds;
303 	return 1;
304 }
305 LCRYPTO_ALIAS(TS_ACCURACY_set_seconds);
306 
307 const ASN1_INTEGER *
TS_ACCURACY_get_seconds(const TS_ACCURACY * a)308 TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
309 {
310 	return a->seconds;
311 }
312 LCRYPTO_ALIAS(TS_ACCURACY_get_seconds);
313 
314 int
TS_ACCURACY_set_millis(TS_ACCURACY * a,const ASN1_INTEGER * millis)315 TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
316 {
317 	ASN1_INTEGER *new_millis = NULL;
318 
319 	if (a->millis == millis)
320 		return 1;
321 	if (millis != NULL) {
322 		new_millis = ASN1_INTEGER_dup(millis);
323 		if (new_millis == NULL) {
324 			TSerror(ERR_R_MALLOC_FAILURE);
325 			return 0;
326 		}
327 	}
328 	ASN1_INTEGER_free(a->millis);
329 	a->millis = new_millis;
330 	return 1;
331 }
332 LCRYPTO_ALIAS(TS_ACCURACY_set_millis);
333 
334 const ASN1_INTEGER *
TS_ACCURACY_get_millis(const TS_ACCURACY * a)335 TS_ACCURACY_get_millis(const TS_ACCURACY *a)
336 {
337 	return a->millis;
338 }
339 LCRYPTO_ALIAS(TS_ACCURACY_get_millis);
340 
341 int
TS_ACCURACY_set_micros(TS_ACCURACY * a,const ASN1_INTEGER * micros)342 TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
343 {
344 	ASN1_INTEGER *new_micros = NULL;
345 
346 	if (a->micros == micros)
347 		return 1;
348 	if (micros != NULL) {
349 		new_micros = ASN1_INTEGER_dup(micros);
350 		if (new_micros == NULL) {
351 			TSerror(ERR_R_MALLOC_FAILURE);
352 			return 0;
353 		}
354 	}
355 	ASN1_INTEGER_free(a->micros);
356 	a->micros = new_micros;
357 	return 1;
358 }
359 LCRYPTO_ALIAS(TS_ACCURACY_set_micros);
360 
361 const ASN1_INTEGER *
TS_ACCURACY_get_micros(const TS_ACCURACY * a)362 TS_ACCURACY_get_micros(const TS_ACCURACY *a)
363 {
364 	return a->micros;
365 }
366 LCRYPTO_ALIAS(TS_ACCURACY_get_micros);
367 
368 int
TS_TST_INFO_set_ordering(TS_TST_INFO * a,int ordering)369 TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
370 {
371 	a->ordering = ordering ? 0xFF : 0x00;
372 	return 1;
373 }
374 LCRYPTO_ALIAS(TS_TST_INFO_set_ordering);
375 
376 int
TS_TST_INFO_get_ordering(const TS_TST_INFO * a)377 TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
378 {
379 	return a->ordering ? 1 : 0;
380 }
381 LCRYPTO_ALIAS(TS_TST_INFO_get_ordering);
382 
383 int
TS_TST_INFO_set_nonce(TS_TST_INFO * a,const ASN1_INTEGER * nonce)384 TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
385 {
386 	ASN1_INTEGER *new_nonce;
387 
388 	if (a->nonce == nonce)
389 		return 1;
390 	new_nonce = ASN1_INTEGER_dup(nonce);
391 	if (new_nonce == NULL) {
392 		TSerror(ERR_R_MALLOC_FAILURE);
393 		return 0;
394 	}
395 	ASN1_INTEGER_free(a->nonce);
396 	a->nonce = new_nonce;
397 	return 1;
398 }
399 LCRYPTO_ALIAS(TS_TST_INFO_set_nonce);
400 
401 const ASN1_INTEGER *
TS_TST_INFO_get_nonce(const TS_TST_INFO * a)402 TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
403 {
404 	return a->nonce;
405 }
406 LCRYPTO_ALIAS(TS_TST_INFO_get_nonce);
407 
408 int
TS_TST_INFO_set_tsa(TS_TST_INFO * a,GENERAL_NAME * tsa)409 TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
410 {
411 	GENERAL_NAME *new_tsa;
412 
413 	if (a->tsa == tsa)
414 		return 1;
415 	new_tsa = GENERAL_NAME_dup(tsa);
416 	if (new_tsa == NULL) {
417 		TSerror(ERR_R_MALLOC_FAILURE);
418 		return 0;
419 	}
420 	GENERAL_NAME_free(a->tsa);
421 	a->tsa = new_tsa;
422 	return 1;
423 }
424 LCRYPTO_ALIAS(TS_TST_INFO_set_tsa);
425 
426 GENERAL_NAME *
TS_TST_INFO_get_tsa(TS_TST_INFO * a)427 TS_TST_INFO_get_tsa(TS_TST_INFO *a)
428 {
429 	return a->tsa;
430 }
431 LCRYPTO_ALIAS(TS_TST_INFO_get_tsa);
432 
STACK_OF(X509_EXTENSION)433 STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
434 {
435 	return a->extensions;
436 }
437 LCRYPTO_ALIAS(TS_TST_INFO_get_exts);
438 
439 void
TS_TST_INFO_ext_free(TS_TST_INFO * a)440 TS_TST_INFO_ext_free(TS_TST_INFO *a)
441 {
442 	if (!a)
443 		return;
444 	sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
445 	a->extensions = NULL;
446 }
447 LCRYPTO_ALIAS(TS_TST_INFO_ext_free);
448 
449 int
TS_TST_INFO_get_ext_count(TS_TST_INFO * a)450 TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
451 {
452 	return X509v3_get_ext_count(a->extensions);
453 }
454 LCRYPTO_ALIAS(TS_TST_INFO_get_ext_count);
455 
456 int
TS_TST_INFO_get_ext_by_NID(TS_TST_INFO * a,int nid,int lastpos)457 TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
458 {
459 	return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
460 }
461 LCRYPTO_ALIAS(TS_TST_INFO_get_ext_by_NID);
462 
463 int
TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO * a,const ASN1_OBJECT * obj,int lastpos)464 TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, int lastpos)
465 {
466 	return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
467 }
468 LCRYPTO_ALIAS(TS_TST_INFO_get_ext_by_OBJ);
469 
470 int
TS_TST_INFO_get_ext_by_critical(TS_TST_INFO * a,int crit,int lastpos)471 TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
472 {
473 	return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
474 }
475 LCRYPTO_ALIAS(TS_TST_INFO_get_ext_by_critical);
476 
477 X509_EXTENSION *
TS_TST_INFO_get_ext(TS_TST_INFO * a,int loc)478 TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
479 {
480 	return X509v3_get_ext(a->extensions, loc);
481 }
482 LCRYPTO_ALIAS(TS_TST_INFO_get_ext);
483 
484 X509_EXTENSION *
TS_TST_INFO_delete_ext(TS_TST_INFO * a,int loc)485 TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
486 {
487 	return X509v3_delete_ext(a->extensions, loc);
488 }
489 LCRYPTO_ALIAS(TS_TST_INFO_delete_ext);
490 
491 int
TS_TST_INFO_add_ext(TS_TST_INFO * a,X509_EXTENSION * ex,int loc)492 TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
493 {
494 	return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
495 }
496 LCRYPTO_ALIAS(TS_TST_INFO_add_ext);
497 
498 void *
TS_TST_INFO_get_ext_d2i(TS_TST_INFO * a,int nid,int * crit,int * idx)499 TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
500 {
501 	return X509V3_get_d2i(a->extensions, nid, crit, idx);
502 }
503 LCRYPTO_ALIAS(TS_TST_INFO_get_ext_d2i);
504