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