155714Skris/* crypto/x509/x509_v3.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <openssl/stack.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/asn1.h>
6355714Skris#include <openssl/objects.h>
6455714Skris#include <openssl/evp.h>
6555714Skris#include <openssl/x509.h>
6659191Skris#include <openssl/x509v3.h>
6755714Skris
6855714Skrisint X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
69296341Sdelphij{
70296341Sdelphij    if (x == NULL)
71296341Sdelphij        return (0);
72296341Sdelphij    return (sk_X509_EXTENSION_num(x));
73296341Sdelphij}
7455714Skris
7555714Skrisint X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
76296341Sdelphij                          int lastpos)
77296341Sdelphij{
78296341Sdelphij    ASN1_OBJECT *obj;
7955714Skris
80296341Sdelphij    obj = OBJ_nid2obj(nid);
81296341Sdelphij    if (obj == NULL)
82296341Sdelphij        return (-2);
83296341Sdelphij    return (X509v3_get_ext_by_OBJ(x, obj, lastpos));
84296341Sdelphij}
8555714Skris
86296341Sdelphijint X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
87296341Sdelphij                          ASN1_OBJECT *obj, int lastpos)
88296341Sdelphij{
89296341Sdelphij    int n;
90296341Sdelphij    X509_EXTENSION *ex;
9155714Skris
92296341Sdelphij    if (sk == NULL)
93296341Sdelphij        return (-1);
94296341Sdelphij    lastpos++;
95296341Sdelphij    if (lastpos < 0)
96296341Sdelphij        lastpos = 0;
97296341Sdelphij    n = sk_X509_EXTENSION_num(sk);
98296341Sdelphij    for (; lastpos < n; lastpos++) {
99296341Sdelphij        ex = sk_X509_EXTENSION_value(sk, lastpos);
100296341Sdelphij        if (OBJ_cmp(ex->object, obj) == 0)
101296341Sdelphij            return (lastpos);
102296341Sdelphij    }
103296341Sdelphij    return (-1);
104296341Sdelphij}
10555714Skris
10655714Skrisint X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
107296341Sdelphij                               int lastpos)
108296341Sdelphij{
109296341Sdelphij    int n;
110296341Sdelphij    X509_EXTENSION *ex;
11155714Skris
112296341Sdelphij    if (sk == NULL)
113296341Sdelphij        return (-1);
114296341Sdelphij    lastpos++;
115296341Sdelphij    if (lastpos < 0)
116296341Sdelphij        lastpos = 0;
117296341Sdelphij    n = sk_X509_EXTENSION_num(sk);
118296341Sdelphij    for (; lastpos < n; lastpos++) {
119296341Sdelphij        ex = sk_X509_EXTENSION_value(sk, lastpos);
120296341Sdelphij        if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
121296341Sdelphij            return (lastpos);
122296341Sdelphij    }
123296341Sdelphij    return (-1);
124296341Sdelphij}
12555714Skris
12655714SkrisX509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
127296341Sdelphij{
128296341Sdelphij    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
129296341Sdelphij        return NULL;
130296341Sdelphij    else
131296341Sdelphij        return sk_X509_EXTENSION_value(x, loc);
132296341Sdelphij}
13355714Skris
13455714SkrisX509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
135296341Sdelphij{
136296341Sdelphij    X509_EXTENSION *ret;
13755714Skris
138296341Sdelphij    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
139296341Sdelphij        return (NULL);
140296341Sdelphij    ret = sk_X509_EXTENSION_delete(x, loc);
141296341Sdelphij    return (ret);
142296341Sdelphij}
14355714Skris
14455714SkrisSTACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
145296341Sdelphij                                         X509_EXTENSION *ex, int loc)
146296341Sdelphij{
147296341Sdelphij    X509_EXTENSION *new_ex = NULL;
148296341Sdelphij    int n;
149296341Sdelphij    STACK_OF(X509_EXTENSION) *sk = NULL;
15055714Skris
151296341Sdelphij    if (x == NULL) {
152296341Sdelphij        X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
153296341Sdelphij        goto err2;
154296341Sdelphij    }
155160814Ssimon
156296341Sdelphij    if (*x == NULL) {
157296341Sdelphij        if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
158296341Sdelphij            goto err;
159296341Sdelphij    } else
160296341Sdelphij        sk = *x;
16155714Skris
162296341Sdelphij    n = sk_X509_EXTENSION_num(sk);
163296341Sdelphij    if (loc > n)
164296341Sdelphij        loc = n;
165296341Sdelphij    else if (loc < 0)
166296341Sdelphij        loc = n;
16755714Skris
168296341Sdelphij    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
169296341Sdelphij        goto err2;
170296341Sdelphij    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
171296341Sdelphij        goto err;
172296341Sdelphij    if (*x == NULL)
173296341Sdelphij        *x = sk;
174296341Sdelphij    return (sk);
175296341Sdelphij err:
176296341Sdelphij    X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
177296341Sdelphij err2:
178296341Sdelphij    if (new_ex != NULL)
179296341Sdelphij        X509_EXTENSION_free(new_ex);
180296341Sdelphij    if (sk != NULL)
181296341Sdelphij        sk_X509_EXTENSION_free(sk);
182296341Sdelphij    return (NULL);
183296341Sdelphij}
18455714Skris
18555714SkrisX509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
186296341Sdelphij                                             int crit,
187296341Sdelphij                                             ASN1_OCTET_STRING *data)
188296341Sdelphij{
189296341Sdelphij    ASN1_OBJECT *obj;
190296341Sdelphij    X509_EXTENSION *ret;
19155714Skris
192296341Sdelphij    obj = OBJ_nid2obj(nid);
193296341Sdelphij    if (obj == NULL) {
194296341Sdelphij        X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
195296341Sdelphij        return (NULL);
196296341Sdelphij    }
197296341Sdelphij    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
198296341Sdelphij    if (ret == NULL)
199296341Sdelphij        ASN1_OBJECT_free(obj);
200296341Sdelphij    return (ret);
201296341Sdelphij}
20255714Skris
20355714SkrisX509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
204296341Sdelphij                                             ASN1_OBJECT *obj, int crit,
205296341Sdelphij                                             ASN1_OCTET_STRING *data)
206296341Sdelphij{
207296341Sdelphij    X509_EXTENSION *ret;
20855714Skris
209296341Sdelphij    if ((ex == NULL) || (*ex == NULL)) {
210296341Sdelphij        if ((ret = X509_EXTENSION_new()) == NULL) {
211296341Sdelphij            X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
212296341Sdelphij                    ERR_R_MALLOC_FAILURE);
213296341Sdelphij            return (NULL);
214296341Sdelphij        }
215296341Sdelphij    } else
216296341Sdelphij        ret = *ex;
21755714Skris
218296341Sdelphij    if (!X509_EXTENSION_set_object(ret, obj))
219296341Sdelphij        goto err;
220296341Sdelphij    if (!X509_EXTENSION_set_critical(ret, crit))
221296341Sdelphij        goto err;
222296341Sdelphij    if (!X509_EXTENSION_set_data(ret, data))
223296341Sdelphij        goto err;
22455714Skris
225296341Sdelphij    if ((ex != NULL) && (*ex == NULL))
226296341Sdelphij        *ex = ret;
227296341Sdelphij    return (ret);
228296341Sdelphij err:
229296341Sdelphij    if ((ex == NULL) || (ret != *ex))
230296341Sdelphij        X509_EXTENSION_free(ret);
231296341Sdelphij    return (NULL);
232296341Sdelphij}
233296341Sdelphij
23455714Skrisint X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
235296341Sdelphij{
236296341Sdelphij    if ((ex == NULL) || (obj == NULL))
237296341Sdelphij        return (0);
238296341Sdelphij    ASN1_OBJECT_free(ex->object);
239296341Sdelphij    ex->object = OBJ_dup(obj);
240296341Sdelphij    return (1);
241296341Sdelphij}
24255714Skris
24355714Skrisint X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
244296341Sdelphij{
245296341Sdelphij    if (ex == NULL)
246296341Sdelphij        return (0);
247296341Sdelphij    ex->critical = (crit) ? 0xFF : -1;
248296341Sdelphij    return (1);
249296341Sdelphij}
25055714Skris
25155714Skrisint X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
252296341Sdelphij{
253296341Sdelphij    int i;
25455714Skris
255296341Sdelphij    if (ex == NULL)
256296341Sdelphij        return (0);
257296341Sdelphij    i = M_ASN1_OCTET_STRING_set(ex->value, data->data, data->length);
258296341Sdelphij    if (!i)
259296341Sdelphij        return (0);
260296341Sdelphij    return (1);
261296341Sdelphij}
26255714Skris
26355714SkrisASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
264296341Sdelphij{
265296341Sdelphij    if (ex == NULL)
266296341Sdelphij        return (NULL);
267296341Sdelphij    return (ex->object);
268296341Sdelphij}
26955714Skris
27055714SkrisASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
271296341Sdelphij{
272296341Sdelphij    if (ex == NULL)
273296341Sdelphij        return (NULL);
274296341Sdelphij    return (ex->value);
275296341Sdelphij}
27655714Skris
27755714Skrisint X509_EXTENSION_get_critical(X509_EXTENSION *ex)
278296341Sdelphij{
279296341Sdelphij    if (ex == NULL)
280296341Sdelphij        return (0);
281296341Sdelphij    if (ex->critical > 0)
282296341Sdelphij        return 1;
283296341Sdelphij    return 0;
284296341Sdelphij}
285