diff options
Diffstat (limited to 'src/lib/libssl/src/crypto/x509v3/v3_lib.c')
-rw-r--r-- | src/lib/libssl/src/crypto/x509v3/v3_lib.c | 190 |
1 files changed, 157 insertions, 33 deletions
diff --git a/src/lib/libssl/src/crypto/x509v3/v3_lib.c b/src/lib/libssl/src/crypto/x509v3/v3_lib.c index a0aa5de794..482ca8ccf5 100644 --- a/src/lib/libssl/src/crypto/x509v3/v3_lib.c +++ b/src/lib/libssl/src/crypto/x509v3/v3_lib.c | |||
@@ -62,38 +62,47 @@ | |||
62 | #include <openssl/conf.h> | 62 | #include <openssl/conf.h> |
63 | #include <openssl/x509v3.h> | 63 | #include <openssl/x509v3.h> |
64 | 64 | ||
65 | static STACK *ext_list = NULL; | 65 | #include "ext_dat.h" |
66 | 66 | ||
67 | static int ext_cmp(X509V3_EXT_METHOD **a, X509V3_EXT_METHOD **b); | 67 | static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
68 | |||
69 | static int ext_cmp(const X509V3_EXT_METHOD * const *a, | ||
70 | const X509V3_EXT_METHOD * const *b); | ||
68 | static void ext_list_free(X509V3_EXT_METHOD *ext); | 71 | static void ext_list_free(X509V3_EXT_METHOD *ext); |
69 | 72 | ||
70 | int X509V3_EXT_add(X509V3_EXT_METHOD *ext) | 73 | int X509V3_EXT_add(X509V3_EXT_METHOD *ext) |
71 | { | 74 | { |
72 | if(!ext_list && !(ext_list = sk_new(ext_cmp))) { | 75 | if(!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) { |
73 | X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE); | 76 | X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE); |
74 | return 0; | 77 | return 0; |
75 | } | 78 | } |
76 | if(!sk_push(ext_list, (char *)ext)) { | 79 | if(!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { |
77 | X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE); | 80 | X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE); |
78 | return 0; | 81 | return 0; |
79 | } | 82 | } |
80 | return 1; | 83 | return 1; |
81 | } | 84 | } |
82 | 85 | ||
83 | static int ext_cmp(X509V3_EXT_METHOD **a, X509V3_EXT_METHOD **b) | 86 | static int ext_cmp(const X509V3_EXT_METHOD * const *a, |
87 | const X509V3_EXT_METHOD * const *b) | ||
84 | { | 88 | { |
85 | return ((*a)->ext_nid - (*b)->ext_nid); | 89 | return ((*a)->ext_nid - (*b)->ext_nid); |
86 | } | 90 | } |
87 | 91 | ||
88 | X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) | 92 | X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) |
89 | { | 93 | { |
90 | X509V3_EXT_METHOD tmp; | 94 | X509V3_EXT_METHOD tmp, *t = &tmp, **ret; |
91 | int idx; | 95 | int idx; |
96 | if(nid < 0) return NULL; | ||
92 | tmp.ext_nid = nid; | 97 | tmp.ext_nid = nid; |
93 | if(!ext_list || (tmp.ext_nid < 0) ) return NULL; | 98 | ret = (X509V3_EXT_METHOD **) OBJ_bsearch((char *)&t, |
94 | idx = sk_find(ext_list, (char *)&tmp); | 99 | (char *)standard_exts, STANDARD_EXTENSION_COUNT, |
100 | sizeof(X509V3_EXT_METHOD *), (int (*)(const void *, const void *))ext_cmp); | ||
101 | if(ret) return *ret; | ||
102 | if(!ext_list) return NULL; | ||
103 | idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp); | ||
95 | if(idx == -1) return NULL; | 104 | if(idx == -1) return NULL; |
96 | return (X509V3_EXT_METHOD *)sk_value(ext_list, idx); | 105 | return sk_X509V3_EXT_METHOD_value(ext_list, idx); |
97 | } | 106 | } |
98 | 107 | ||
99 | X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext) | 108 | X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext) |
@@ -118,49 +127,33 @@ int X509V3_EXT_add_alias(int nid_to, int nid_from) | |||
118 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND); | 127 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND); |
119 | return 0; | 128 | return 0; |
120 | } | 129 | } |
121 | if(!(tmpext = (X509V3_EXT_METHOD *)Malloc(sizeof(X509V3_EXT_METHOD)))) { | 130 | if(!(tmpext = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) { |
122 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE); | 131 | X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE); |
123 | return 0; | 132 | return 0; |
124 | } | 133 | } |
125 | *tmpext = *ext; | 134 | *tmpext = *ext; |
126 | tmpext->ext_nid = nid_to; | 135 | tmpext->ext_nid = nid_to; |
127 | tmpext->ext_flags |= X509V3_EXT_DYNAMIC; | 136 | tmpext->ext_flags |= X509V3_EXT_DYNAMIC; |
128 | return 1; | 137 | return X509V3_EXT_add(tmpext); |
129 | } | 138 | } |
130 | 139 | ||
131 | void X509V3_EXT_cleanup(void) | 140 | void X509V3_EXT_cleanup(void) |
132 | { | 141 | { |
133 | sk_pop_free(ext_list, ext_list_free); | 142 | sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free); |
134 | ext_list = NULL; | 143 | ext_list = NULL; |
135 | } | 144 | } |
136 | 145 | ||
137 | static void ext_list_free(X509V3_EXT_METHOD *ext) | 146 | static void ext_list_free(X509V3_EXT_METHOD *ext) |
138 | { | 147 | { |
139 | if(ext->ext_flags & X509V3_EXT_DYNAMIC) Free(ext); | 148 | if(ext->ext_flags & X509V3_EXT_DYNAMIC) OPENSSL_free(ext); |
140 | } | 149 | } |
141 | 150 | ||
142 | extern X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku; | 151 | /* Legacy function: we don't need to add standard extensions |
143 | extern X509V3_EXT_METHOD v3_pkey_usage_period, v3_sxnet; | 152 | * any more because they are now kept in ext_dat.h. |
144 | extern X509V3_EXT_METHOD v3_ns_ia5_list[], v3_alt[], v3_skey_id, v3_akey_id; | 153 | */ |
145 | |||
146 | extern X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_cpols, v3_crld; | ||
147 | 154 | ||
148 | int X509V3_add_standard_extensions(void) | 155 | int X509V3_add_standard_extensions(void) |
149 | { | 156 | { |
150 | X509V3_EXT_add_list(v3_ns_ia5_list); | ||
151 | X509V3_EXT_add_list(v3_alt); | ||
152 | X509V3_EXT_add(&v3_bcons); | ||
153 | X509V3_EXT_add(&v3_nscert); | ||
154 | X509V3_EXT_add(&v3_key_usage); | ||
155 | X509V3_EXT_add(&v3_ext_ku); | ||
156 | X509V3_EXT_add(&v3_skey_id); | ||
157 | X509V3_EXT_add(&v3_akey_id); | ||
158 | X509V3_EXT_add(&v3_pkey_usage_period); | ||
159 | X509V3_EXT_add(&v3_crl_num); | ||
160 | X509V3_EXT_add(&v3_sxnet); | ||
161 | X509V3_EXT_add(&v3_crl_reason); | ||
162 | X509V3_EXT_add(&v3_cpols); | ||
163 | X509V3_EXT_add(&v3_crld); | ||
164 | return 1; | 157 | return 1; |
165 | } | 158 | } |
166 | 159 | ||
@@ -170,8 +163,139 @@ void *X509V3_EXT_d2i(X509_EXTENSION *ext) | |||
170 | { | 163 | { |
171 | X509V3_EXT_METHOD *method; | 164 | X509V3_EXT_METHOD *method; |
172 | unsigned char *p; | 165 | unsigned char *p; |
173 | if(!(method = X509V3_EXT_get(ext)) || !method->d2i) return NULL; | 166 | if(!(method = X509V3_EXT_get(ext))) return NULL; |
174 | p = ext->value->data; | 167 | p = ext->value->data; |
168 | if(method->it) return ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); | ||
175 | return method->d2i(NULL, &p, ext->value->length); | 169 | return method->d2i(NULL, &p, ext->value->length); |
176 | } | 170 | } |
177 | 171 | ||
172 | /* Get critical flag and decoded version of extension from a NID. | ||
173 | * The "idx" variable returns the last found extension and can | ||
174 | * be used to retrieve multiple extensions of the same NID. | ||
175 | * However multiple extensions with the same NID is usually | ||
176 | * due to a badly encoded certificate so if idx is NULL we | ||
177 | * choke if multiple extensions exist. | ||
178 | * The "crit" variable is set to the critical value. | ||
179 | * The return value is the decoded extension or NULL on | ||
180 | * error. The actual error can have several different causes, | ||
181 | * the value of *crit reflects the cause: | ||
182 | * >= 0, extension found but not decoded (reflects critical value). | ||
183 | * -1 extension not found. | ||
184 | * -2 extension occurs more than once. | ||
185 | */ | ||
186 | |||
187 | void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx) | ||
188 | { | ||
189 | int lastpos, i; | ||
190 | X509_EXTENSION *ex, *found_ex = NULL; | ||
191 | if(!x) { | ||
192 | if(idx) *idx = -1; | ||
193 | if(crit) *crit = -1; | ||
194 | return NULL; | ||
195 | } | ||
196 | if(idx) lastpos = *idx + 1; | ||
197 | else lastpos = 0; | ||
198 | if(lastpos < 0) lastpos = 0; | ||
199 | for(i = lastpos; i < sk_X509_EXTENSION_num(x); i++) | ||
200 | { | ||
201 | ex = sk_X509_EXTENSION_value(x, i); | ||
202 | if(OBJ_obj2nid(ex->object) == nid) { | ||
203 | if(idx) { | ||
204 | *idx = i; | ||
205 | break; | ||
206 | } else if(found_ex) { | ||
207 | /* Found more than one */ | ||
208 | if(crit) *crit = -2; | ||
209 | return NULL; | ||
210 | } | ||
211 | found_ex = ex; | ||
212 | } | ||
213 | } | ||
214 | if(found_ex) { | ||
215 | /* Found it */ | ||
216 | if(crit) *crit = X509_EXTENSION_get_critical(found_ex); | ||
217 | return X509V3_EXT_d2i(found_ex); | ||
218 | } | ||
219 | |||
220 | /* Extension not found */ | ||
221 | if(idx) *idx = -1; | ||
222 | if(crit) *crit = -1; | ||
223 | return NULL; | ||
224 | } | ||
225 | |||
226 | /* This function is a general extension append, replace and delete utility. | ||
227 | * The precise operation is governed by the 'flags' value. The 'crit' and | ||
228 | * 'value' arguments (if relevant) are the extensions internal structure. | ||
229 | */ | ||
230 | |||
231 | int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, | ||
232 | int crit, unsigned long flags) | ||
233 | { | ||
234 | int extidx = -1; | ||
235 | int errcode; | ||
236 | X509_EXTENSION *ext, *extmp; | ||
237 | unsigned long ext_op = flags & X509V3_ADD_OP_MASK; | ||
238 | |||
239 | /* If appending we don't care if it exists, otherwise | ||
240 | * look for existing extension. | ||
241 | */ | ||
242 | if(ext_op != X509V3_ADD_APPEND) | ||
243 | extidx = X509v3_get_ext_by_NID(*x, nid, -1); | ||
244 | |||
245 | /* See if extension exists */ | ||
246 | if(extidx >= 0) { | ||
247 | /* If keep existing, nothing to do */ | ||
248 | if(ext_op == X509V3_ADD_KEEP_EXISTING) | ||
249 | return 1; | ||
250 | /* If default then its an error */ | ||
251 | if(ext_op == X509V3_ADD_DEFAULT) { | ||
252 | errcode = X509V3_R_EXTENSION_EXISTS; | ||
253 | goto err; | ||
254 | } | ||
255 | /* If delete, just delete it */ | ||
256 | if(ext_op == X509V3_ADD_DELETE) { | ||
257 | if(!sk_X509_EXTENSION_delete(*x, extidx)) return -1; | ||
258 | return 1; | ||
259 | } | ||
260 | } else { | ||
261 | /* If replace existing or delete, error since | ||
262 | * extension must exist | ||
263 | */ | ||
264 | if((ext_op == X509V3_ADD_REPLACE_EXISTING) || | ||
265 | (ext_op == X509V3_ADD_DELETE)) { | ||
266 | errcode = X509V3_R_EXTENSION_NOT_FOUND; | ||
267 | goto err; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | /* If we get this far then we have to create an extension: | ||
272 | * could have some flags for alternative encoding schemes... | ||
273 | */ | ||
274 | |||
275 | ext = X509V3_EXT_i2d(nid, crit, value); | ||
276 | |||
277 | if(!ext) { | ||
278 | X509V3err(X509V3_F_X509V3_ADD_I2D, X509V3_R_ERROR_CREATING_EXTENSION); | ||
279 | return 0; | ||
280 | } | ||
281 | |||
282 | /* If extension exists replace it.. */ | ||
283 | if(extidx >= 0) { | ||
284 | extmp = sk_X509_EXTENSION_value(*x, extidx); | ||
285 | X509_EXTENSION_free(extmp); | ||
286 | if(!sk_X509_EXTENSION_set(*x, extidx, ext)) return -1; | ||
287 | return 1; | ||
288 | } | ||
289 | |||
290 | if(!*x && !(*x = sk_X509_EXTENSION_new_null())) return -1; | ||
291 | if(!sk_X509_EXTENSION_push(*x, ext)) return -1; | ||
292 | |||
293 | return 1; | ||
294 | |||
295 | err: | ||
296 | if(!(flags & X509V3_ADD_SILENT)) | ||
297 | X509V3err(X509V3_F_X509V3_ADD_I2D, errcode); | ||
298 | return 0; | ||
299 | } | ||
300 | |||
301 | IMPLEMENT_STACK_OF(X509V3_EXT_METHOD) | ||