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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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)
69280304Sjkim{
70280304Sjkim    if (x == NULL)
71280304Sjkim        return (0);
72280304Sjkim    return (sk_X509_EXTENSION_num(x));
73280304Sjkim}
7455714Skris
7555714Skrisint X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
76280304Sjkim                          int lastpos)
77280304Sjkim{
78280304Sjkim    ASN1_OBJECT *obj;
7955714Skris
80280304Sjkim    obj = OBJ_nid2obj(nid);
81280304Sjkim    if (obj == NULL)
82280304Sjkim        return (-2);
83280304Sjkim    return (X509v3_get_ext_by_OBJ(x, obj, lastpos));
84280304Sjkim}
8555714Skris
86280304Sjkimint X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
87280304Sjkim                          ASN1_OBJECT *obj, int lastpos)
88280304Sjkim{
89280304Sjkim    int n;
90280304Sjkim    X509_EXTENSION *ex;
9155714Skris
92280304Sjkim    if (sk == NULL)
93280304Sjkim        return (-1);
94280304Sjkim    lastpos++;
95280304Sjkim    if (lastpos < 0)
96280304Sjkim        lastpos = 0;
97280304Sjkim    n = sk_X509_EXTENSION_num(sk);
98280304Sjkim    for (; lastpos < n; lastpos++) {
99280304Sjkim        ex = sk_X509_EXTENSION_value(sk, lastpos);
100280304Sjkim        if (OBJ_cmp(ex->object, obj) == 0)
101280304Sjkim            return (lastpos);
102280304Sjkim    }
103280304Sjkim    return (-1);
104280304Sjkim}
10555714Skris
10655714Skrisint X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
107280304Sjkim                               int lastpos)
108280304Sjkim{
109280304Sjkim    int n;
110280304Sjkim    X509_EXTENSION *ex;
11155714Skris
112280304Sjkim    if (sk == NULL)
113280304Sjkim        return (-1);
114280304Sjkim    lastpos++;
115280304Sjkim    if (lastpos < 0)
116280304Sjkim        lastpos = 0;
117280304Sjkim    n = sk_X509_EXTENSION_num(sk);
118280304Sjkim    for (; lastpos < n; lastpos++) {
119280304Sjkim        ex = sk_X509_EXTENSION_value(sk, lastpos);
120280304Sjkim        if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
121280304Sjkim            return (lastpos);
122280304Sjkim    }
123280304Sjkim    return (-1);
124280304Sjkim}
12555714Skris
12655714SkrisX509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
127280304Sjkim{
128280304Sjkim    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
129280304Sjkim        return NULL;
130280304Sjkim    else
131280304Sjkim        return sk_X509_EXTENSION_value(x, loc);
132280304Sjkim}
13355714Skris
13455714SkrisX509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
135280304Sjkim{
136280304Sjkim    X509_EXTENSION *ret;
13755714Skris
138280304Sjkim    if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
139280304Sjkim        return (NULL);
140280304Sjkim    ret = sk_X509_EXTENSION_delete(x, loc);
141280304Sjkim    return (ret);
142280304Sjkim}
14355714Skris
14455714SkrisSTACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
145280304Sjkim                                         X509_EXTENSION *ex, int loc)
146280304Sjkim{
147280304Sjkim    X509_EXTENSION *new_ex = NULL;
148280304Sjkim    int n;
149280304Sjkim    STACK_OF(X509_EXTENSION) *sk = NULL;
15055714Skris
151280304Sjkim    if (x == NULL) {
152280304Sjkim        X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
153280304Sjkim        goto err2;
154280304Sjkim    }
155160814Ssimon
156280304Sjkim    if (*x == NULL) {
157280304Sjkim        if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
158280304Sjkim            goto err;
159280304Sjkim    } else
160280304Sjkim        sk = *x;
16155714Skris
162280304Sjkim    n = sk_X509_EXTENSION_num(sk);
163280304Sjkim    if (loc > n)
164280304Sjkim        loc = n;
165280304Sjkim    else if (loc < 0)
166280304Sjkim        loc = n;
16755714Skris
168280304Sjkim    if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
169280304Sjkim        goto err2;
170280304Sjkim    if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
171280304Sjkim        goto err;
172280304Sjkim    if (*x == NULL)
173280304Sjkim        *x = sk;
174280304Sjkim    return (sk);
175280304Sjkim err:
176280304Sjkim    X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
177280304Sjkim err2:
178280304Sjkim    if (new_ex != NULL)
179280304Sjkim        X509_EXTENSION_free(new_ex);
180280304Sjkim    if (sk != NULL)
181280304Sjkim        sk_X509_EXTENSION_free(sk);
182280304Sjkim    return (NULL);
183280304Sjkim}
18455714Skris
18555714SkrisX509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
186280304Sjkim                                             int crit,
187280304Sjkim                                             ASN1_OCTET_STRING *data)
188280304Sjkim{
189280304Sjkim    ASN1_OBJECT *obj;
190280304Sjkim    X509_EXTENSION *ret;
19155714Skris
192280304Sjkim    obj = OBJ_nid2obj(nid);
193280304Sjkim    if (obj == NULL) {
194280304Sjkim        X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
195280304Sjkim        return (NULL);
196280304Sjkim    }
197280304Sjkim    ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
198280304Sjkim    if (ret == NULL)
199280304Sjkim        ASN1_OBJECT_free(obj);
200280304Sjkim    return (ret);
201280304Sjkim}
20255714Skris
20355714SkrisX509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
204280304Sjkim                                             ASN1_OBJECT *obj, int crit,
205280304Sjkim                                             ASN1_OCTET_STRING *data)
206280304Sjkim{
207280304Sjkim    X509_EXTENSION *ret;
20855714Skris
209280304Sjkim    if ((ex == NULL) || (*ex == NULL)) {
210280304Sjkim        if ((ret = X509_EXTENSION_new()) == NULL) {
211280304Sjkim            X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
212280304Sjkim                    ERR_R_MALLOC_FAILURE);
213280304Sjkim            return (NULL);
214280304Sjkim        }
215280304Sjkim    } else
216280304Sjkim        ret = *ex;
21755714Skris
218280304Sjkim    if (!X509_EXTENSION_set_object(ret, obj))
219280304Sjkim        goto err;
220280304Sjkim    if (!X509_EXTENSION_set_critical(ret, crit))
221280304Sjkim        goto err;
222280304Sjkim    if (!X509_EXTENSION_set_data(ret, data))
223280304Sjkim        goto err;
22455714Skris
225280304Sjkim    if ((ex != NULL) && (*ex == NULL))
226280304Sjkim        *ex = ret;
227280304Sjkim    return (ret);
228280304Sjkim err:
229280304Sjkim    if ((ex == NULL) || (ret != *ex))
230280304Sjkim        X509_EXTENSION_free(ret);
231280304Sjkim    return (NULL);
232280304Sjkim}
233280304Sjkim
23455714Skrisint X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
235280304Sjkim{
236280304Sjkim    if ((ex == NULL) || (obj == NULL))
237280304Sjkim        return (0);
238280304Sjkim    ASN1_OBJECT_free(ex->object);
239280304Sjkim    ex->object = OBJ_dup(obj);
240280304Sjkim    return (1);
241280304Sjkim}
24255714Skris
24355714Skrisint X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
244280304Sjkim{
245280304Sjkim    if (ex == NULL)
246280304Sjkim        return (0);
247280304Sjkim    ex->critical = (crit) ? 0xFF : -1;
248280304Sjkim    return (1);
249280304Sjkim}
25055714Skris
25155714Skrisint X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
252280304Sjkim{
253280304Sjkim    int i;
25455714Skris
255280304Sjkim    if (ex == NULL)
256280304Sjkim        return (0);
257280304Sjkim    i = M_ASN1_OCTET_STRING_set(ex->value, data->data, data->length);
258280304Sjkim    if (!i)
259280304Sjkim        return (0);
260280304Sjkim    return (1);
261280304Sjkim}
26255714Skris
26355714SkrisASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
264280304Sjkim{
265280304Sjkim    if (ex == NULL)
266280304Sjkim        return (NULL);
267280304Sjkim    return (ex->object);
268280304Sjkim}
26955714Skris
27055714SkrisASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
271280304Sjkim{
272280304Sjkim    if (ex == NULL)
273280304Sjkim        return (NULL);
274280304Sjkim    return (ex->value);
275280304Sjkim}
27655714Skris
27755714Skrisint X509_EXTENSION_get_critical(X509_EXTENSION *ex)
278280304Sjkim{
279280304Sjkim    if (ex == NULL)
280280304Sjkim        return (0);
281280304Sjkim    if (ex->critical > 0)
282280304Sjkim        return 1;
283280304Sjkim    return 0;
284280304Sjkim}
285