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