Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x509_v3.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5 #include <openssl/err.h>
6 #include <openssl/evp.h>
7 #include <openssl/obj.h>
8 #include <openssl/stack.h>
9 #include <openssl/x509.h>
10
11 #include "internal.h"
12
13
14 int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x) {
15 if (x == NULL) {
16 return 0;
17 }
18 return (int)sk_X509_EXTENSION_num(x);
19 }
20
21 int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
22 int lastpos) {
23 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
24 if (obj == NULL) {
25 return -1;
26 }
27 return X509v3_get_ext_by_OBJ(x, obj, lastpos);
28 }
29
30 int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
31 const ASN1_OBJECT *obj, int lastpos) {
32 if (sk == NULL) {
33 return -1;
34 }
35 lastpos++;
36 if (lastpos < 0) {
37 lastpos = 0;
38 }
39 int n = (int)sk_X509_EXTENSION_num(sk);
40 for (; lastpos < n; lastpos++) {
41 const X509_EXTENSION *ex = sk_X509_EXTENSION_value(sk, lastpos);
42 if (OBJ_cmp(ex->object, obj) == 0) {
43 return lastpos;
44 }
45 }
46 return -1;
47 }
48
49 int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
50 int lastpos) {
51 if (sk == NULL) {
52 return -1;
53 }
54
55 lastpos++;
56 if (lastpos < 0) {
57 lastpos = 0;
58 }
59
60 crit = !!crit;
61 int n = (int)sk_X509_EXTENSION_num(sk);
62 for (; lastpos < n; lastpos++) {
63 const X509_EXTENSION *ex = sk_X509_EXTENSION_value(sk, lastpos);
64 if (X509_EXTENSION_get_critical(ex) == crit) {
65 return lastpos;
66 }
67 }
68 return -1;
69 }
70
71 X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc) {
72 if (x == NULL || loc < 0 || sk_X509_EXTENSION_num(x) <= (size_t)loc) {
73 return NULL;
74 } else {
75 return sk_X509_EXTENSION_value(x, loc);
76 }
77 }
78
79 X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc) {
80 X509_EXTENSION *ret;
81
82 if (x == NULL || loc < 0 || sk_X509_EXTENSION_num(x) <= (size_t)loc) {
83 return NULL;
84 }
85 ret = sk_X509_EXTENSION_delete(x, loc);
86 return ret;
87 }
88
89 STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
90 const X509_EXTENSION *ex, int loc) {
91 X509_EXTENSION *new_ex = NULL;
92 STACK_OF(X509_EXTENSION) *sk = NULL;
93 int free_sk = 0;
94
95 if (x == NULL) {
96 OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
97 goto err;
98 }
99
100 if (*x == NULL) {
101 if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
102 goto err;
103 }
104 free_sk = 1;
105 } else {
106 sk = *x;
107 }
108
109 int n = (int)sk_X509_EXTENSION_num(sk);
110 if (loc > n) {
111 loc = n;
112 } else if (loc < 0) {
113 loc = n;
114 }
115
116 if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
117 goto err;
118 }
119 if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
120 goto err;
121 }
122 if (*x == NULL) {
123 *x = sk;
124 }
125 return sk;
126
127 err:
128 X509_EXTENSION_free(new_ex);
129 if (free_sk) {
130 sk_X509_EXTENSION_free(sk);
131 }
132 return NULL;
133 }
134
135 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
136 int crit,
137 const ASN1_OCTET_STRING *data) {
138 const ASN1_OBJECT *obj;
139 X509_EXTENSION *ret;
140
141 obj = OBJ_nid2obj(nid);
142 if (obj == NULL) {
143 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
144 return NULL;
145 }
146 ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
147 return ret;
148 }
149
150 X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
151 const ASN1_OBJECT *obj, int crit,
152 const ASN1_OCTET_STRING *data) {
153 X509_EXTENSION *ret;
154
155 if ((ex == NULL) || (*ex == NULL)) {
156 if ((ret = X509_EXTENSION_new()) == NULL) {
157 return NULL;
158 }
159 } else {
160 ret = *ex;
161 }
162
163 if (!X509_EXTENSION_set_object(ret, obj)) {
164 goto err;
165 }
166 if (!X509_EXTENSION_set_critical(ret, crit)) {
167 goto err;
168 }
169 if (!X509_EXTENSION_set_data(ret, data)) {
170 goto err;
171 }
172
173 if ((ex != NULL) && (*ex == NULL)) {
174 *ex = ret;
175 }
176 return ret;
177 err:
178 if ((ex == NULL) || (ret != *ex)) {
179 X509_EXTENSION_free(ret);
180 }
181 return NULL;
182 }
183
184 int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj) {
185 if ((ex == NULL) || (obj == NULL)) {
186 return 0;
187 }
188 ASN1_OBJECT_free(ex->object);
189 ex->object = OBJ_dup(obj);
190 return ex->object != NULL;
191 }
192
193 int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit) {
194 if (ex == NULL) {
195 return 0;
196 }
197 // The critical field is DEFAULT FALSE, so non-critical extensions should omit
198 // the value.
199 ex->critical = crit ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_NONE;
200 return 1;
201 }
202
203 int X509_EXTENSION_set_data(X509_EXTENSION *ex, const ASN1_OCTET_STRING *data) {
204 int i;
205
206 if (ex == NULL) {
207 return 0;
208 }
209 i = ASN1_OCTET_STRING_set(ex->value, data->data, data->length);
210 if (!i) {
211 return 0;
212 }
213 return 1;
214 }
215
216 ASN1_OBJECT *X509_EXTENSION_get_object(const X509_EXTENSION *ex) {
217 if (ex == NULL) {
218 return NULL;
219 }
220 return ex->object;
221 }
222
223 ASN1_OCTET_STRING *X509_EXTENSION_get_data(const X509_EXTENSION *ex) {
224 if (ex == NULL) {
225 return NULL;
226 }
227 return ex->value;
228 }
229
230 int X509_EXTENSION_get_critical(const X509_EXTENSION *ex) {
231 if (ex == NULL) {
232 return 0;
233 }
234 if (ex->critical > 0) {
235 return 1;
236 }
237 return 0;
238 }