1238384Sjkim/* crypto/ts/ts_req_utils.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4296341Sdelphij * 2002.
5238384Sjkim */
6238384Sjkim/* ====================================================================
7238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8238384Sjkim *
9238384Sjkim * Redistribution and use in source and binary forms, with or without
10238384Sjkim * modification, are permitted provided that the following conditions
11238384Sjkim * are met:
12238384Sjkim *
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15238384Sjkim *
16238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
17238384Sjkim *    notice, this list of conditions and the following disclaimer in
18238384Sjkim *    the documentation and/or other materials provided with the
19238384Sjkim *    distribution.
20238384Sjkim *
21238384Sjkim * 3. All advertising materials mentioning features or use of this
22238384Sjkim *    software must display the following acknowledgment:
23238384Sjkim *    "This product includes software developed by the OpenSSL Project
24238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25238384Sjkim *
26238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27238384Sjkim *    endorse or promote products derived from this software without
28238384Sjkim *    prior written permission. For written permission, please contact
29238384Sjkim *    licensing@OpenSSL.org.
30238384Sjkim *
31238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
32238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
33238384Sjkim *    permission of the OpenSSL Project.
34238384Sjkim *
35238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
36238384Sjkim *    acknowledgment:
37238384Sjkim *    "This product includes software developed by the OpenSSL Project
38238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39238384Sjkim *
40238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
52238384Sjkim * ====================================================================
53238384Sjkim *
54238384Sjkim * This product includes cryptographic software written by Eric Young
55238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
56238384Sjkim * Hudson (tjh@cryptsoft.com).
57238384Sjkim *
58238384Sjkim */
59238384Sjkim
60238384Sjkim#include <stdio.h>
61238384Sjkim#include "cryptlib.h"
62238384Sjkim#include <openssl/objects.h>
63238384Sjkim#include <openssl/x509v3.h>
64238384Sjkim#include <openssl/ts.h>
65238384Sjkim
66238384Sjkimint TS_REQ_set_version(TS_REQ *a, long version)
67296341Sdelphij{
68296341Sdelphij    return ASN1_INTEGER_set(a->version, version);
69296341Sdelphij}
70238384Sjkim
71238384Sjkimlong TS_REQ_get_version(const TS_REQ *a)
72296341Sdelphij{
73296341Sdelphij    return ASN1_INTEGER_get(a->version);
74296341Sdelphij}
75238384Sjkim
76238384Sjkimint TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint)
77296341Sdelphij{
78296341Sdelphij    TS_MSG_IMPRINT *new_msg_imprint;
79238384Sjkim
80296341Sdelphij    if (a->msg_imprint == msg_imprint)
81296341Sdelphij        return 1;
82296341Sdelphij    new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
83296341Sdelphij    if (new_msg_imprint == NULL) {
84296341Sdelphij        TSerr(TS_F_TS_REQ_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
85296341Sdelphij        return 0;
86296341Sdelphij    }
87296341Sdelphij    TS_MSG_IMPRINT_free(a->msg_imprint);
88296341Sdelphij    a->msg_imprint = new_msg_imprint;
89296341Sdelphij    return 1;
90296341Sdelphij}
91238384Sjkim
92238384SjkimTS_MSG_IMPRINT *TS_REQ_get_msg_imprint(TS_REQ *a)
93296341Sdelphij{
94296341Sdelphij    return a->msg_imprint;
95296341Sdelphij}
96238384Sjkim
97238384Sjkimint TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg)
98296341Sdelphij{
99296341Sdelphij    X509_ALGOR *new_alg;
100238384Sjkim
101296341Sdelphij    if (a->hash_algo == alg)
102296341Sdelphij        return 1;
103296341Sdelphij    new_alg = X509_ALGOR_dup(alg);
104296341Sdelphij    if (new_alg == NULL) {
105296341Sdelphij        TSerr(TS_F_TS_MSG_IMPRINT_SET_ALGO, ERR_R_MALLOC_FAILURE);
106296341Sdelphij        return 0;
107296341Sdelphij    }
108296341Sdelphij    X509_ALGOR_free(a->hash_algo);
109296341Sdelphij    a->hash_algo = new_alg;
110296341Sdelphij    return 1;
111296341Sdelphij}
112238384Sjkim
113238384SjkimX509_ALGOR *TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a)
114296341Sdelphij{
115296341Sdelphij    return a->hash_algo;
116296341Sdelphij}
117238384Sjkim
118238384Sjkimint TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len)
119296341Sdelphij{
120296341Sdelphij    return ASN1_OCTET_STRING_set(a->hashed_msg, d, len);
121296341Sdelphij}
122238384Sjkim
123238384SjkimASN1_OCTET_STRING *TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a)
124296341Sdelphij{
125296341Sdelphij    return a->hashed_msg;
126296341Sdelphij}
127238384Sjkim
128238384Sjkimint TS_REQ_set_policy_id(TS_REQ *a, ASN1_OBJECT *policy)
129296341Sdelphij{
130296341Sdelphij    ASN1_OBJECT *new_policy;
131238384Sjkim
132296341Sdelphij    if (a->policy_id == policy)
133296341Sdelphij        return 1;
134296341Sdelphij    new_policy = OBJ_dup(policy);
135296341Sdelphij    if (new_policy == NULL) {
136296341Sdelphij        TSerr(TS_F_TS_REQ_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
137296341Sdelphij        return 0;
138296341Sdelphij    }
139296341Sdelphij    ASN1_OBJECT_free(a->policy_id);
140296341Sdelphij    a->policy_id = new_policy;
141296341Sdelphij    return 1;
142296341Sdelphij}
143238384Sjkim
144238384SjkimASN1_OBJECT *TS_REQ_get_policy_id(TS_REQ *a)
145296341Sdelphij{
146296341Sdelphij    return a->policy_id;
147296341Sdelphij}
148238384Sjkim
149238384Sjkimint TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce)
150296341Sdelphij{
151296341Sdelphij    ASN1_INTEGER *new_nonce;
152238384Sjkim
153296341Sdelphij    if (a->nonce == nonce)
154296341Sdelphij        return 1;
155296341Sdelphij    new_nonce = ASN1_INTEGER_dup(nonce);
156296341Sdelphij    if (new_nonce == NULL) {
157296341Sdelphij        TSerr(TS_F_TS_REQ_SET_NONCE, ERR_R_MALLOC_FAILURE);
158296341Sdelphij        return 0;
159296341Sdelphij    }
160296341Sdelphij    ASN1_INTEGER_free(a->nonce);
161296341Sdelphij    a->nonce = new_nonce;
162296341Sdelphij    return 1;
163296341Sdelphij}
164238384Sjkim
165238384Sjkimconst ASN1_INTEGER *TS_REQ_get_nonce(const TS_REQ *a)
166296341Sdelphij{
167296341Sdelphij    return a->nonce;
168296341Sdelphij}
169238384Sjkim
170238384Sjkimint TS_REQ_set_cert_req(TS_REQ *a, int cert_req)
171296341Sdelphij{
172296341Sdelphij    a->cert_req = cert_req ? 0xFF : 0x00;
173296341Sdelphij    return 1;
174296341Sdelphij}
175238384Sjkim
176238384Sjkimint TS_REQ_get_cert_req(const TS_REQ *a)
177296341Sdelphij{
178296341Sdelphij    return a->cert_req ? 1 : 0;
179296341Sdelphij}
180238384Sjkim
181238384SjkimSTACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a)
182296341Sdelphij{
183296341Sdelphij    return a->extensions;
184296341Sdelphij}
185238384Sjkim
186238384Sjkimvoid TS_REQ_ext_free(TS_REQ *a)
187296341Sdelphij{
188296341Sdelphij    if (!a)
189296341Sdelphij        return;
190296341Sdelphij    sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
191296341Sdelphij    a->extensions = NULL;
192296341Sdelphij}
193238384Sjkim
194238384Sjkimint TS_REQ_get_ext_count(TS_REQ *a)
195296341Sdelphij{
196296341Sdelphij    return X509v3_get_ext_count(a->extensions);
197296341Sdelphij}
198238384Sjkim
199238384Sjkimint TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos)
200296341Sdelphij{
201296341Sdelphij    return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
202296341Sdelphij}
203238384Sjkim
204238384Sjkimint TS_REQ_get_ext_by_OBJ(TS_REQ *a, ASN1_OBJECT *obj, int lastpos)
205296341Sdelphij{
206296341Sdelphij    return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
207296341Sdelphij}
208238384Sjkim
209238384Sjkimint TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos)
210296341Sdelphij{
211296341Sdelphij    return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
212296341Sdelphij}
213238384Sjkim
214238384SjkimX509_EXTENSION *TS_REQ_get_ext(TS_REQ *a, int loc)
215296341Sdelphij{
216296341Sdelphij    return X509v3_get_ext(a->extensions, loc);
217296341Sdelphij}
218238384Sjkim
219238384SjkimX509_EXTENSION *TS_REQ_delete_ext(TS_REQ *a, int loc)
220296341Sdelphij{
221296341Sdelphij    return X509v3_delete_ext(a->extensions, loc);
222296341Sdelphij}
223238384Sjkim
224238384Sjkimint TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc)
225296341Sdelphij{
226296341Sdelphij    return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
227296341Sdelphij}
228238384Sjkim
229238384Sjkimvoid *TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx)
230296341Sdelphij{
231296341Sdelphij    return X509V3_get_d2i(a->extensions, nid, crit, idx);
232296341Sdelphij}
233