summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3/v3_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509v3/v3_lib.c')
-rw-r--r--src/lib/libcrypto/x509v3/v3_lib.c88
1 files changed, 66 insertions, 22 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_lib.c b/src/lib/libcrypto/x509v3/v3_lib.c
index a0aa5de794..4242d130a2 100644
--- a/src/lib/libcrypto/x509v3/v3_lib.c
+++ b/src/lib/libcrypto/x509v3/v3_lib.c
@@ -62,6 +62,8 @@
62#include <openssl/conf.h> 62#include <openssl/conf.h>
63#include <openssl/x509v3.h> 63#include <openssl/x509v3.h>
64 64
65#include "ext_dat.h"
66
65static STACK *ext_list = NULL; 67static STACK *ext_list = NULL;
66 68
67static int ext_cmp(X509V3_EXT_METHOD **a, X509V3_EXT_METHOD **b); 69static int ext_cmp(X509V3_EXT_METHOD **a, X509V3_EXT_METHOD **b);
@@ -87,10 +89,15 @@ static int ext_cmp(X509V3_EXT_METHOD **a, X509V3_EXT_METHOD **b)
87 89
88X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) 90X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
89{ 91{
90 X509V3_EXT_METHOD tmp; 92 X509V3_EXT_METHOD tmp, *t = &tmp, **ret;
91 int idx; 93 int idx;
94 if(nid < 0) return NULL;
92 tmp.ext_nid = nid; 95 tmp.ext_nid = nid;
93 if(!ext_list || (tmp.ext_nid < 0) ) return NULL; 96 ret = (X509V3_EXT_METHOD **) OBJ_bsearch((char *)&t,
97 (char *)standard_exts, STANDARD_EXTENSION_COUNT,
98 sizeof(X509V3_EXT_METHOD *), (int (*)())ext_cmp);
99 if(ret) return *ret;
100 if(!ext_list) return NULL;
94 idx = sk_find(ext_list, (char *)&tmp); 101 idx = sk_find(ext_list, (char *)&tmp);
95 if(idx == -1) return NULL; 102 if(idx == -1) return NULL;
96 return (X509V3_EXT_METHOD *)sk_value(ext_list, idx); 103 return (X509V3_EXT_METHOD *)sk_value(ext_list, idx);
@@ -125,7 +132,7 @@ int X509V3_EXT_add_alias(int nid_to, int nid_from)
125 *tmpext = *ext; 132 *tmpext = *ext;
126 tmpext->ext_nid = nid_to; 133 tmpext->ext_nid = nid_to;
127 tmpext->ext_flags |= X509V3_EXT_DYNAMIC; 134 tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
128 return 1; 135 return X509V3_EXT_add(tmpext);
129} 136}
130 137
131void X509V3_EXT_cleanup(void) 138void X509V3_EXT_cleanup(void)
@@ -139,28 +146,12 @@ static void ext_list_free(X509V3_EXT_METHOD *ext)
139 if(ext->ext_flags & X509V3_EXT_DYNAMIC) Free(ext); 146 if(ext->ext_flags & X509V3_EXT_DYNAMIC) Free(ext);
140} 147}
141 148
142extern X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku; 149/* Legacy function: we don't need to add standard extensions
143extern X509V3_EXT_METHOD v3_pkey_usage_period, v3_sxnet; 150 * any more because they are now kept in ext_dat.h.
144extern X509V3_EXT_METHOD v3_ns_ia5_list[], v3_alt[], v3_skey_id, v3_akey_id; 151 */
145
146extern X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_cpols, v3_crld;
147 152
148int X509V3_add_standard_extensions(void) 153int X509V3_add_standard_extensions(void)
149{ 154{
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; 155 return 1;
165} 156}
166 157
@@ -175,3 +166,56 @@ void *X509V3_EXT_d2i(X509_EXTENSION *ext)
175 return method->d2i(NULL, &p, ext->value->length); 166 return method->d2i(NULL, &p, ext->value->length);
176} 167}
177 168
169/* Get critical flag and decoded version of extension from a NID.
170 * The "idx" variable returns the last found extension and can
171 * be used to retrieve multiple extensions of the same NID.
172 * However multiple extensions with the same NID is usually
173 * due to a badly encoded certificate so if idx is NULL we
174 * choke if multiple extensions exist.
175 * The "crit" variable is set to the critical value.
176 * The return value is the decoded extension or NULL on
177 * error. The actual error can have several different causes,
178 * the value of *crit reflects the cause:
179 * >= 0, extension found but not decoded (reflects critical value).
180 * -1 extension not found.
181 * -2 extension occurs more than once.
182 */
183
184void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
185{
186 int lastpos, i;
187 X509_EXTENSION *ex, *found_ex = NULL;
188 if(!x) {
189 if(idx) *idx = -1;
190 if(crit) *crit = -1;
191 return NULL;
192 }
193 if(idx) lastpos = *idx + 1;
194 else lastpos = 0;
195 if(lastpos < 0) lastpos = 0;
196 for(i = lastpos; i < sk_X509_EXTENSION_num(x); i++)
197 {
198 ex = sk_X509_EXTENSION_value(x, i);
199 if(OBJ_obj2nid(ex->object) == nid) {
200 if(idx) {
201 *idx = i;
202 break;
203 } else if(found_ex) {
204 /* Found more than one */
205 if(crit) *crit = -2;
206 return NULL;
207 }
208 found_ex = ex;
209 }
210 }
211 if(found_ex) {
212 /* Found it */
213 if(crit) *crit = found_ex->critical;
214 return X509V3_EXT_d2i(found_ex);
215 }
216
217 /* Extension not found */
218 if(idx) *idx = -1;
219 if(crit) *crit = -1;
220 return NULL;
221}