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