]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
1212818e | 2 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
9aeaf1b4 | 3 | * |
4286ca47 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
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 | |
9aeaf1b4 | 8 | */ |
d2e9e320 | 9 | |
9aeaf1b4 DSH |
10 | /* X509 v3 extension utilities */ |
11 | ||
e527ba09 | 12 | #include <stdio.h> |
b39fc560 | 13 | #include "internal/cryptlib.h" |
ec577822 BM |
14 | #include <openssl/conf.h> |
15 | #include <openssl/x509v3.h> | |
9aeaf1b4 | 16 | |
08cba610 DSH |
17 | #include "ext_dat.h" |
18 | ||
852c2ed2 RS |
19 | DEFINE_STACK_OF(X509V3_EXT_METHOD) |
20 | DEFINE_STACK_OF(X509_EXTENSION) | |
21 | ||
0d3b0afe | 22 | static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
9aeaf1b4 | 23 | |
0f113f3e MC |
24 | static int ext_cmp(const X509V3_EXT_METHOD *const *a, |
25 | const X509V3_EXT_METHOD *const *b); | |
9aeaf1b4 DSH |
26 | static void ext_list_free(X509V3_EXT_METHOD *ext); |
27 | ||
6b691a5c | 28 | int X509V3_EXT_add(X509V3_EXT_METHOD *ext) |
9aeaf1b4 | 29 | { |
75ebbd9a RS |
30 | if (ext_list == NULL |
31 | && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) { | |
0f113f3e MC |
32 | X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE); |
33 | return 0; | |
34 | } | |
35 | if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { | |
36 | X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE); | |
37 | return 0; | |
38 | } | |
39 | return 1; | |
9aeaf1b4 DSH |
40 | } |
41 | ||
0f113f3e MC |
42 | static int ext_cmp(const X509V3_EXT_METHOD *const *a, |
43 | const X509V3_EXT_METHOD *const *b) | |
9aeaf1b4 | 44 | { |
0f113f3e | 45 | return ((*a)->ext_nid - (*b)->ext_nid); |
9aeaf1b4 DSH |
46 | } |
47 | ||
0f113f3e MC |
48 | DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *, |
49 | const X509V3_EXT_METHOD *, ext); | |
babb3798 | 50 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *, |
0f113f3e | 51 | const X509V3_EXT_METHOD *, ext); |
babb3798 | 52 | |
f12d6273 | 53 | #include "standard_exts.h" |
8cf27823 | 54 | |
babb3798 | 55 | const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) |
9aeaf1b4 | 56 | { |
0f113f3e MC |
57 | X509V3_EXT_METHOD tmp; |
58 | const X509V3_EXT_METHOD *t = &tmp, *const *ret; | |
59 | int idx; | |
1687aa76 | 60 | |
0f113f3e MC |
61 | if (nid < 0) |
62 | return NULL; | |
63 | tmp.ext_nid = nid; | |
64 | ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT); | |
65 | if (ret) | |
66 | return *ret; | |
67 | if (!ext_list) | |
68 | return NULL; | |
69 | idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp); | |
0f113f3e | 70 | return sk_X509V3_EXT_METHOD_value(ext_list, idx); |
9aeaf1b4 DSH |
71 | } |
72 | ||
babb3798 | 73 | const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext) |
9aeaf1b4 | 74 | { |
0f113f3e | 75 | int nid; |
4903abd5 | 76 | if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef) |
0f113f3e MC |
77 | return NULL; |
78 | return X509V3_EXT_get_nid(nid); | |
9aeaf1b4 DSH |
79 | } |
80 | ||
6b691a5c | 81 | int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist) |
9aeaf1b4 | 82 | { |
0f113f3e MC |
83 | for (; extlist->ext_nid != -1; extlist++) |
84 | if (!X509V3_EXT_add(extlist)) | |
85 | return 0; | |
86 | return 1; | |
9aeaf1b4 DSH |
87 | } |
88 | ||
6b691a5c | 89 | int X509V3_EXT_add_alias(int nid_to, int nid_from) |
9aeaf1b4 | 90 | { |
0f113f3e MC |
91 | const X509V3_EXT_METHOD *ext; |
92 | X509V3_EXT_METHOD *tmpext; | |
93 | ||
75ebbd9a RS |
94 | if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) { |
95 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, X509V3_R_EXTENSION_NOT_FOUND); | |
0f113f3e MC |
96 | return 0; |
97 | } | |
75ebbd9a | 98 | if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL) { |
0f113f3e MC |
99 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, ERR_R_MALLOC_FAILURE); |
100 | return 0; | |
101 | } | |
102 | *tmpext = *ext; | |
103 | tmpext->ext_nid = nid_to; | |
104 | tmpext->ext_flags |= X509V3_EXT_DYNAMIC; | |
105 | return X509V3_EXT_add(tmpext); | |
9aeaf1b4 DSH |
106 | } |
107 | ||
6b691a5c | 108 | void X509V3_EXT_cleanup(void) |
9aeaf1b4 | 109 | { |
0f113f3e MC |
110 | sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free); |
111 | ext_list = NULL; | |
9aeaf1b4 DSH |
112 | } |
113 | ||
6b691a5c | 114 | static void ext_list_free(X509V3_EXT_METHOD *ext) |
9aeaf1b4 | 115 | { |
0f113f3e MC |
116 | if (ext->ext_flags & X509V3_EXT_DYNAMIC) |
117 | OPENSSL_free(ext); | |
9aeaf1b4 DSH |
118 | } |
119 | ||
0f113f3e MC |
120 | /* |
121 | * Legacy function: we don't need to add standard extensions any more because | |
122 | * they are now kept in ext_dat.h. | |
08cba610 | 123 | */ |
0ca5f8b1 | 124 | |
6b691a5c | 125 | int X509V3_add_standard_extensions(void) |
9aeaf1b4 | 126 | { |
0f113f3e | 127 | return 1; |
9aeaf1b4 | 128 | } |
0be9747b DSH |
129 | |
130 | /* Return an extension internal structure */ | |
131 | ||
f5fedc04 | 132 | void *X509V3_EXT_d2i(X509_EXTENSION *ext) |
0be9747b | 133 | { |
0f113f3e MC |
134 | const X509V3_EXT_METHOD *method; |
135 | const unsigned char *p; | |
4903abd5 DSH |
136 | ASN1_STRING *extvalue; |
137 | int extlen; | |
0f113f3e | 138 | |
75ebbd9a | 139 | if ((method = X509V3_EXT_get(ext)) == NULL) |
0f113f3e | 140 | return NULL; |
4903abd5 | 141 | extvalue = X509_EXTENSION_get_data(ext); |
17ebf85a | 142 | p = ASN1_STRING_get0_data(extvalue); |
4903abd5 | 143 | extlen = ASN1_STRING_length(extvalue); |
0f113f3e | 144 | if (method->it) |
4903abd5 DSH |
145 | return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it)); |
146 | return method->d2i(NULL, &p, extlen); | |
0be9747b DSH |
147 | } |
148 | ||
3a83462d MC |
149 | /*- |
150 | * Get critical flag and decoded version of extension from a NID. | |
4654ef98 DSH |
151 | * The "idx" variable returns the last found extension and can |
152 | * be used to retrieve multiple extensions of the same NID. | |
153 | * However multiple extensions with the same NID is usually | |
154 | * due to a badly encoded certificate so if idx is NULL we | |
155 | * choke if multiple extensions exist. | |
156 | * The "crit" variable is set to the critical value. | |
157 | * The return value is the decoded extension or NULL on | |
158 | * error. The actual error can have several different causes, | |
159 | * the value of *crit reflects the cause: | |
160 | * >= 0, extension found but not decoded (reflects critical value). | |
161 | * -1 extension not found. | |
162 | * -2 extension occurs more than once. | |
163 | */ | |
164 | ||
84de54b9 | 165 | void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, |
0f113f3e | 166 | int *idx) |
4654ef98 | 167 | { |
0f113f3e MC |
168 | int lastpos, i; |
169 | X509_EXTENSION *ex, *found_ex = NULL; | |
1687aa76 | 170 | |
0f113f3e MC |
171 | if (!x) { |
172 | if (idx) | |
173 | *idx = -1; | |
174 | if (crit) | |
175 | *crit = -1; | |
176 | return NULL; | |
177 | } | |
178 | if (idx) | |
179 | lastpos = *idx + 1; | |
180 | else | |
181 | lastpos = 0; | |
182 | if (lastpos < 0) | |
183 | lastpos = 0; | |
184 | for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) { | |
185 | ex = sk_X509_EXTENSION_value(x, i); | |
4903abd5 | 186 | if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) { |
0f113f3e MC |
187 | if (idx) { |
188 | *idx = i; | |
189 | found_ex = ex; | |
190 | break; | |
191 | } else if (found_ex) { | |
192 | /* Found more than one */ | |
193 | if (crit) | |
194 | *crit = -2; | |
195 | return NULL; | |
196 | } | |
197 | found_ex = ex; | |
198 | } | |
199 | } | |
200 | if (found_ex) { | |
201 | /* Found it */ | |
202 | if (crit) | |
203 | *crit = X509_EXTENSION_get_critical(found_ex); | |
204 | return X509V3_EXT_d2i(found_ex); | |
205 | } | |
206 | ||
207 | /* Extension not found */ | |
208 | if (idx) | |
209 | *idx = -1; | |
210 | if (crit) | |
211 | *crit = -1; | |
212 | return NULL; | |
4654ef98 | 213 | } |
0d3b0afe | 214 | |
0f113f3e MC |
215 | /* |
216 | * This function is a general extension append, replace and delete utility. | |
57d2f217 DSH |
217 | * The precise operation is governed by the 'flags' value. The 'crit' and |
218 | * 'value' arguments (if relevant) are the extensions internal structure. | |
219 | */ | |
220 | ||
28ddfc61 | 221 | int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, |
0f113f3e | 222 | int crit, unsigned long flags) |
57d2f217 | 223 | { |
1687aa76 F |
224 | int errcode, extidx = -1; |
225 | X509_EXTENSION *ext = NULL, *extmp; | |
226 | STACK_OF(X509_EXTENSION) *ret = NULL; | |
0f113f3e MC |
227 | unsigned long ext_op = flags & X509V3_ADD_OP_MASK; |
228 | ||
229 | /* | |
230 | * If appending we don't care if it exists, otherwise look for existing | |
231 | * extension. | |
232 | */ | |
233 | if (ext_op != X509V3_ADD_APPEND) | |
234 | extidx = X509v3_get_ext_by_NID(*x, nid, -1); | |
235 | ||
236 | /* See if extension exists */ | |
237 | if (extidx >= 0) { | |
238 | /* If keep existing, nothing to do */ | |
239 | if (ext_op == X509V3_ADD_KEEP_EXISTING) | |
240 | return 1; | |
241 | /* If default then its an error */ | |
242 | if (ext_op == X509V3_ADD_DEFAULT) { | |
243 | errcode = X509V3_R_EXTENSION_EXISTS; | |
244 | goto err; | |
245 | } | |
246 | /* If delete, just delete it */ | |
247 | if (ext_op == X509V3_ADD_DELETE) { | |
248 | if (!sk_X509_EXTENSION_delete(*x, extidx)) | |
249 | return -1; | |
250 | return 1; | |
251 | } | |
252 | } else { | |
253 | /* | |
254 | * If replace existing or delete, error since extension must exist | |
255 | */ | |
256 | if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || | |
257 | (ext_op == X509V3_ADD_DELETE)) { | |
258 | errcode = X509V3_R_EXTENSION_NOT_FOUND; | |
259 | goto err; | |
260 | } | |
261 | } | |
262 | ||
263 | /* | |
264 | * If we get this far then we have to create an extension: could have | |
265 | * some flags for alternative encoding schemes... | |
266 | */ | |
267 | ||
268 | ext = X509V3_EXT_i2d(nid, crit, value); | |
269 | ||
270 | if (!ext) { | |
271 | X509V3err(X509V3_F_X509V3_ADD1_I2D, | |
272 | X509V3_R_ERROR_CREATING_EXTENSION); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | /* If extension exists replace it.. */ | |
277 | if (extidx >= 0) { | |
278 | extmp = sk_X509_EXTENSION_value(*x, extidx); | |
279 | X509_EXTENSION_free(extmp); | |
280 | if (!sk_X509_EXTENSION_set(*x, extidx, ext)) | |
281 | return -1; | |
282 | return 1; | |
283 | } | |
284 | ||
1687aa76 | 285 | ret = *x; |
75ebbd9a | 286 | if (*x == NULL |
1687aa76 F |
287 | && (ret = sk_X509_EXTENSION_new_null()) == NULL) |
288 | goto m_fail; | |
289 | if (!sk_X509_EXTENSION_push(ret, ext)) | |
290 | goto m_fail; | |
0f113f3e | 291 | |
1687aa76 | 292 | *x = ret; |
0f113f3e MC |
293 | return 1; |
294 | ||
1687aa76 F |
295 | m_fail: |
296 | /* X509V3err(X509V3_F_X509V3_ADD1_I2D, ERR_R_MALLOC_FAILURE); */ | |
297 | if (ret != *x) | |
298 | sk_X509_EXTENSION_free(ret); | |
299 | X509_EXTENSION_free(ext); | |
300 | return -1; | |
301 | ||
0f113f3e MC |
302 | err: |
303 | if (!(flags & X509V3_ADD_SILENT)) | |
304 | X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode); | |
305 | return 0; | |
57d2f217 | 306 | } |