2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/safestack.h>
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include "x509_local.h"
20 DEFINE_STACK_OF(X509_EXTENSION)
22 int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
26 return sk_X509_EXTENSION_num(x);
29 int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
34 obj = OBJ_nid2obj(nid);
37 return X509v3_get_ext_by_OBJ(x, obj, lastpos);
40 int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
41 const ASN1_OBJECT *obj, int lastpos)
51 n = sk_X509_EXTENSION_num(sk);
52 for (; lastpos < n; lastpos++) {
53 ex = sk_X509_EXTENSION_value(sk, lastpos);
54 if (OBJ_cmp(ex->object, obj) == 0)
60 int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
71 n = sk_X509_EXTENSION_num(sk);
72 for (; lastpos < n; lastpos++) {
73 ex = sk_X509_EXTENSION_value(sk, lastpos);
74 if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
80 X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
82 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
85 return sk_X509_EXTENSION_value(x, loc);
88 X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
92 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
94 ret = sk_X509_EXTENSION_delete(x, loc);
98 STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
99 X509_EXTENSION *ex, int loc)
101 X509_EXTENSION *new_ex = NULL;
103 STACK_OF(X509_EXTENSION) *sk = NULL;
106 X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
111 if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
116 n = sk_X509_EXTENSION_num(sk);
122 if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
124 if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
130 X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
132 X509_EXTENSION_free(new_ex);
133 if (x != NULL && *x == NULL)
134 sk_X509_EXTENSION_free(sk);
138 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
140 ASN1_OCTET_STRING *data)
145 obj = OBJ_nid2obj(nid);
147 X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
150 ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
152 ASN1_OBJECT_free(obj);
156 X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
157 const ASN1_OBJECT *obj, int crit,
158 ASN1_OCTET_STRING *data)
162 if ((ex == NULL) || (*ex == NULL)) {
163 if ((ret = X509_EXTENSION_new()) == NULL) {
164 X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
165 ERR_R_MALLOC_FAILURE);
171 if (!X509_EXTENSION_set_object(ret, obj))
173 if (!X509_EXTENSION_set_critical(ret, crit))
175 if (!X509_EXTENSION_set_data(ret, data))
178 if ((ex != NULL) && (*ex == NULL))
182 if ((ex == NULL) || (ret != *ex))
183 X509_EXTENSION_free(ret);
187 int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
189 if ((ex == NULL) || (obj == NULL))
191 ASN1_OBJECT_free(ex->object);
192 ex->object = OBJ_dup(obj);
193 return ex->object != NULL;
196 int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
200 ex->critical = (crit) ? 0xFF : -1;
204 int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
210 i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
216 ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
223 ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
230 int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
234 if (ex->critical > 0)