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