1238384Sjkim/* crypto/ts/ts_req_utils.c */
2238384Sjkim/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3238384Sjkim * project 2002.
4238384Sjkim */
5238384Sjkim/* ====================================================================
6238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7238384Sjkim *
8238384Sjkim * Redistribution and use in source and binary forms, with or without
9238384Sjkim * modification, are permitted provided that the following conditions
10238384Sjkim * are met:
11238384Sjkim *
12238384Sjkim * 1. Redistributions of source code must retain the above copyright
13238384Sjkim *    notice, this list of conditions and the following disclaimer.
14238384Sjkim *
15238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
16238384Sjkim *    notice, this list of conditions and the following disclaimer in
17238384Sjkim *    the documentation and/or other materials provided with the
18238384Sjkim *    distribution.
19238384Sjkim *
20238384Sjkim * 3. All advertising materials mentioning features or use of this
21238384Sjkim *    software must display the following acknowledgment:
22238384Sjkim *    "This product includes software developed by the OpenSSL Project
23238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24238384Sjkim *
25238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26238384Sjkim *    endorse or promote products derived from this software without
27238384Sjkim *    prior written permission. For written permission, please contact
28238384Sjkim *    licensing@OpenSSL.org.
29238384Sjkim *
30238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
31238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
32238384Sjkim *    permission of the OpenSSL Project.
33238384Sjkim *
34238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
35238384Sjkim *    acknowledgment:
36238384Sjkim *    "This product includes software developed by the OpenSSL Project
37238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38238384Sjkim *
39238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
51238384Sjkim * ====================================================================
52238384Sjkim *
53238384Sjkim * This product includes cryptographic software written by Eric Young
54238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
55238384Sjkim * Hudson (tjh@cryptsoft.com).
56238384Sjkim *
57238384Sjkim */
58238384Sjkim
59238384Sjkim#include <stdio.h>
60238384Sjkim#include "cryptlib.h"
61238384Sjkim#include <openssl/objects.h>
62238384Sjkim#include <openssl/x509v3.h>
63238384Sjkim#include <openssl/ts.h>
64238384Sjkim
65238384Sjkimint TS_REQ_set_version(TS_REQ *a, long version)
66238384Sjkim	{
67238384Sjkim	return ASN1_INTEGER_set(a->version, version);
68238384Sjkim	}
69238384Sjkim
70238384Sjkimlong TS_REQ_get_version(const TS_REQ *a)
71238384Sjkim	{
72238384Sjkim	return ASN1_INTEGER_get(a->version);
73238384Sjkim	}
74238384Sjkim
75238384Sjkimint TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint)
76238384Sjkim	{
77238384Sjkim	TS_MSG_IMPRINT *new_msg_imprint;
78238384Sjkim
79238384Sjkim	if (a->msg_imprint == msg_imprint)
80238384Sjkim		return 1;
81238384Sjkim	new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
82238384Sjkim	if (new_msg_imprint == NULL)
83238384Sjkim		{
84238384Sjkim		TSerr(TS_F_TS_REQ_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
85238384Sjkim		return 0;
86238384Sjkim		}
87238384Sjkim	TS_MSG_IMPRINT_free(a->msg_imprint);
88238384Sjkim	a->msg_imprint = new_msg_imprint;
89238384Sjkim	return 1;
90238384Sjkim	}
91238384Sjkim
92238384SjkimTS_MSG_IMPRINT *TS_REQ_get_msg_imprint(TS_REQ *a)
93238384Sjkim	{
94238384Sjkim	return a->msg_imprint;
95238384Sjkim	}
96238384Sjkim
97238384Sjkimint TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg)
98238384Sjkim	{
99238384Sjkim	X509_ALGOR *new_alg;
100238384Sjkim
101238384Sjkim	if (a->hash_algo == alg)
102238384Sjkim		return 1;
103238384Sjkim	new_alg = X509_ALGOR_dup(alg);
104238384Sjkim	if (new_alg == NULL)
105238384Sjkim		{
106238384Sjkim		TSerr(TS_F_TS_MSG_IMPRINT_SET_ALGO, ERR_R_MALLOC_FAILURE);
107238384Sjkim		return 0;
108238384Sjkim		}
109238384Sjkim	X509_ALGOR_free(a->hash_algo);
110238384Sjkim	a->hash_algo = new_alg;
111238384Sjkim	return 1;
112238384Sjkim	}
113238384Sjkim
114238384SjkimX509_ALGOR *TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a)
115238384Sjkim	{
116238384Sjkim	return a->hash_algo;
117238384Sjkim	}
118238384Sjkim
119238384Sjkimint TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len)
120238384Sjkim	{
121238384Sjkim	return ASN1_OCTET_STRING_set(a->hashed_msg, d, len);
122238384Sjkim	}
123238384Sjkim
124238384SjkimASN1_OCTET_STRING *TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a)
125238384Sjkim	{
126238384Sjkim	return a->hashed_msg;
127238384Sjkim	}
128238384Sjkim
129238384Sjkimint TS_REQ_set_policy_id(TS_REQ *a, ASN1_OBJECT *policy)
130238384Sjkim	{
131238384Sjkim	ASN1_OBJECT *new_policy;
132238384Sjkim
133238384Sjkim	if (a->policy_id == policy)
134238384Sjkim		return 1;
135238384Sjkim	new_policy = OBJ_dup(policy);
136238384Sjkim	if (new_policy == NULL)
137238384Sjkim		{
138238384Sjkim		TSerr(TS_F_TS_REQ_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
139238384Sjkim		return 0;
140238384Sjkim		}
141238384Sjkim	ASN1_OBJECT_free(a->policy_id);
142238384Sjkim	a->policy_id = new_policy;
143238384Sjkim	return 1;
144238384Sjkim	}
145238384Sjkim
146238384SjkimASN1_OBJECT *TS_REQ_get_policy_id(TS_REQ *a)
147238384Sjkim	{
148238384Sjkim	return a->policy_id;
149238384Sjkim	}
150238384Sjkim
151238384Sjkimint TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce)
152238384Sjkim	{
153238384Sjkim	ASN1_INTEGER *new_nonce;
154238384Sjkim
155238384Sjkim	if (a->nonce == nonce)
156238384Sjkim		return 1;
157238384Sjkim	new_nonce = ASN1_INTEGER_dup(nonce);
158238384Sjkim	if (new_nonce == NULL)
159238384Sjkim		{
160238384Sjkim		TSerr(TS_F_TS_REQ_SET_NONCE, ERR_R_MALLOC_FAILURE);
161238384Sjkim		return 0;
162238384Sjkim		}
163238384Sjkim	ASN1_INTEGER_free(a->nonce);
164238384Sjkim	a->nonce = new_nonce;
165238384Sjkim	return 1;
166238384Sjkim	}
167238384Sjkim
168238384Sjkimconst ASN1_INTEGER *TS_REQ_get_nonce(const TS_REQ *a)
169238384Sjkim	{
170238384Sjkim	return a->nonce;
171238384Sjkim	}
172238384Sjkim
173238384Sjkimint TS_REQ_set_cert_req(TS_REQ *a, int cert_req)
174238384Sjkim	{
175238384Sjkim	a->cert_req = cert_req ? 0xFF : 0x00;
176238384Sjkim	return 1;
177238384Sjkim	}
178238384Sjkim
179238384Sjkimint TS_REQ_get_cert_req(const TS_REQ *a)
180238384Sjkim	{
181238384Sjkim	return a->cert_req ? 1 : 0;
182238384Sjkim	}
183238384Sjkim
184238384SjkimSTACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a)
185238384Sjkim	{
186238384Sjkim	return a->extensions;
187238384Sjkim	}
188238384Sjkim
189238384Sjkimvoid TS_REQ_ext_free(TS_REQ *a)
190238384Sjkim	{
191238384Sjkim	if (!a) return;
192238384Sjkim	sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
193238384Sjkim	a->extensions = NULL;
194238384Sjkim	}
195238384Sjkim
196238384Sjkimint TS_REQ_get_ext_count(TS_REQ *a)
197238384Sjkim	{
198238384Sjkim	return X509v3_get_ext_count(a->extensions);
199238384Sjkim	}
200238384Sjkim
201238384Sjkimint TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos)
202238384Sjkim	{
203238384Sjkim	return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
204238384Sjkim	}
205238384Sjkim
206238384Sjkimint TS_REQ_get_ext_by_OBJ(TS_REQ *a, ASN1_OBJECT *obj, int lastpos)
207238384Sjkim	{
208238384Sjkim	return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
209238384Sjkim	}
210238384Sjkim
211238384Sjkimint TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos)
212238384Sjkim	{
213238384Sjkim	return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
214238384Sjkim	}
215238384Sjkim
216238384SjkimX509_EXTENSION *TS_REQ_get_ext(TS_REQ *a, int loc)
217238384Sjkim	{
218238384Sjkim	return X509v3_get_ext(a->extensions,loc);
219238384Sjkim	}
220238384Sjkim
221238384SjkimX509_EXTENSION *TS_REQ_delete_ext(TS_REQ *a, int loc)
222238384Sjkim	{
223238384Sjkim	return X509v3_delete_ext(a->extensions,loc);
224238384Sjkim	}
225238384Sjkim
226238384Sjkimint TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc)
227238384Sjkim	{
228238384Sjkim	return X509v3_add_ext(&a->extensions,ex,loc) != NULL;
229238384Sjkim	}
230238384Sjkim
231238384Sjkimvoid *TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx)
232238384Sjkim	{
233238384Sjkim	return X509V3_get_d2i(a->extensions, nid, crit, idx);
234238384Sjkim	}
235