1238384Sjkim/* crypto/ts/ts_req_utils.c */
2280304Sjkim/*
3280304Sjkim * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
4280304Sjkim * 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
14280304Sjkim *    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)
67280304Sjkim{
68280304Sjkim    return ASN1_INTEGER_set(a->version, version);
69280304Sjkim}
70238384Sjkim
71238384Sjkimlong TS_REQ_get_version(const TS_REQ *a)
72280304Sjkim{
73280304Sjkim    return ASN1_INTEGER_get(a->version);
74280304Sjkim}
75238384Sjkim
76238384Sjkimint TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint)
77280304Sjkim{
78280304Sjkim    TS_MSG_IMPRINT *new_msg_imprint;
79238384Sjkim
80280304Sjkim    if (a->msg_imprint == msg_imprint)
81280304Sjkim        return 1;
82280304Sjkim    new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
83280304Sjkim    if (new_msg_imprint == NULL) {
84280304Sjkim        TSerr(TS_F_TS_REQ_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
85280304Sjkim        return 0;
86280304Sjkim    }
87280304Sjkim    TS_MSG_IMPRINT_free(a->msg_imprint);
88280304Sjkim    a->msg_imprint = new_msg_imprint;
89280304Sjkim    return 1;
90280304Sjkim}
91238384Sjkim
92238384SjkimTS_MSG_IMPRINT *TS_REQ_get_msg_imprint(TS_REQ *a)
93280304Sjkim{
94280304Sjkim    return a->msg_imprint;
95280304Sjkim}
96238384Sjkim
97238384Sjkimint TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg)
98280304Sjkim{
99280304Sjkim    X509_ALGOR *new_alg;
100238384Sjkim
101280304Sjkim    if (a->hash_algo == alg)
102280304Sjkim        return 1;
103280304Sjkim    new_alg = X509_ALGOR_dup(alg);
104280304Sjkim    if (new_alg == NULL) {
105280304Sjkim        TSerr(TS_F_TS_MSG_IMPRINT_SET_ALGO, ERR_R_MALLOC_FAILURE);
106280304Sjkim        return 0;
107280304Sjkim    }
108280304Sjkim    X509_ALGOR_free(a->hash_algo);
109280304Sjkim    a->hash_algo = new_alg;
110280304Sjkim    return 1;
111280304Sjkim}
112238384Sjkim
113238384SjkimX509_ALGOR *TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a)
114280304Sjkim{
115280304Sjkim    return a->hash_algo;
116280304Sjkim}
117238384Sjkim
118238384Sjkimint TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len)
119280304Sjkim{
120280304Sjkim    return ASN1_OCTET_STRING_set(a->hashed_msg, d, len);
121280304Sjkim}
122238384Sjkim
123238384SjkimASN1_OCTET_STRING *TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a)
124280304Sjkim{
125280304Sjkim    return a->hashed_msg;
126280304Sjkim}
127238384Sjkim
128238384Sjkimint TS_REQ_set_policy_id(TS_REQ *a, ASN1_OBJECT *policy)
129280304Sjkim{
130280304Sjkim    ASN1_OBJECT *new_policy;
131238384Sjkim
132280304Sjkim    if (a->policy_id == policy)
133280304Sjkim        return 1;
134280304Sjkim    new_policy = OBJ_dup(policy);
135280304Sjkim    if (new_policy == NULL) {
136280304Sjkim        TSerr(TS_F_TS_REQ_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
137280304Sjkim        return 0;
138280304Sjkim    }
139280304Sjkim    ASN1_OBJECT_free(a->policy_id);
140280304Sjkim    a->policy_id = new_policy;
141280304Sjkim    return 1;
142280304Sjkim}
143238384Sjkim
144238384SjkimASN1_OBJECT *TS_REQ_get_policy_id(TS_REQ *a)
145280304Sjkim{
146280304Sjkim    return a->policy_id;
147280304Sjkim}
148238384Sjkim
149238384Sjkimint TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce)
150280304Sjkim{
151280304Sjkim    ASN1_INTEGER *new_nonce;
152238384Sjkim
153280304Sjkim    if (a->nonce == nonce)
154280304Sjkim        return 1;
155280304Sjkim    new_nonce = ASN1_INTEGER_dup(nonce);
156280304Sjkim    if (new_nonce == NULL) {
157280304Sjkim        TSerr(TS_F_TS_REQ_SET_NONCE, ERR_R_MALLOC_FAILURE);
158280304Sjkim        return 0;
159280304Sjkim    }
160280304Sjkim    ASN1_INTEGER_free(a->nonce);
161280304Sjkim    a->nonce = new_nonce;
162280304Sjkim    return 1;
163280304Sjkim}
164238384Sjkim
165238384Sjkimconst ASN1_INTEGER *TS_REQ_get_nonce(const TS_REQ *a)
166280304Sjkim{
167280304Sjkim    return a->nonce;
168280304Sjkim}
169238384Sjkim
170238384Sjkimint TS_REQ_set_cert_req(TS_REQ *a, int cert_req)
171280304Sjkim{
172280304Sjkim    a->cert_req = cert_req ? 0xFF : 0x00;
173280304Sjkim    return 1;
174280304Sjkim}
175238384Sjkim
176238384Sjkimint TS_REQ_get_cert_req(const TS_REQ *a)
177280304Sjkim{
178280304Sjkim    return a->cert_req ? 1 : 0;
179280304Sjkim}
180238384Sjkim
181238384SjkimSTACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a)
182280304Sjkim{
183280304Sjkim    return a->extensions;
184280304Sjkim}
185238384Sjkim
186238384Sjkimvoid TS_REQ_ext_free(TS_REQ *a)
187280304Sjkim{
188280304Sjkim    if (!a)
189280304Sjkim        return;
190280304Sjkim    sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
191280304Sjkim    a->extensions = NULL;
192280304Sjkim}
193238384Sjkim
194238384Sjkimint TS_REQ_get_ext_count(TS_REQ *a)
195280304Sjkim{
196280304Sjkim    return X509v3_get_ext_count(a->extensions);
197280304Sjkim}
198238384Sjkim
199238384Sjkimint TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos)
200280304Sjkim{
201280304Sjkim    return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
202280304Sjkim}
203238384Sjkim
204238384Sjkimint TS_REQ_get_ext_by_OBJ(TS_REQ *a, ASN1_OBJECT *obj, int lastpos)
205280304Sjkim{
206280304Sjkim    return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
207280304Sjkim}
208238384Sjkim
209238384Sjkimint TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos)
210280304Sjkim{
211280304Sjkim    return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
212280304Sjkim}
213238384Sjkim
214238384SjkimX509_EXTENSION *TS_REQ_get_ext(TS_REQ *a, int loc)
215280304Sjkim{
216280304Sjkim    return X509v3_get_ext(a->extensions, loc);
217280304Sjkim}
218238384Sjkim
219238384SjkimX509_EXTENSION *TS_REQ_delete_ext(TS_REQ *a, int loc)
220280304Sjkim{
221280304Sjkim    return X509v3_delete_ext(a->extensions, loc);
222280304Sjkim}
223238384Sjkim
224238384Sjkimint TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc)
225280304Sjkim{
226280304Sjkim    return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
227280304Sjkim}
228238384Sjkim
229238384Sjkimvoid *TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx)
230280304Sjkim{
231280304Sjkim    return X509V3_get_d2i(a->extensions, nid, crit, idx);
232280304Sjkim}
233