Debian Codegraph @

lib_pypy/_cffi_ssl/_cffi_src/openssl/x509_vfy.py

raw symbols SCIP
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4
5 from __future__ import absolute_import, division, print_function
6
7 INCLUDES = """
8 #include <openssl/x509_vfy.h>
9
10 /*
11 * This is part of a work-around for the difficulty cffi has in dealing with
12 * `STACK_OF(foo)` as the name of a type. We invent a new, simpler name that
13 * will be an alias for this type and use the alias throughout. This works
14 * together with another opaque typedef for the same name in the TYPES section.
15 * Note that the result is an opaque type.
16 */
17 typedef STACK_OF(ASN1_OBJECT) Cryptography_STACK_OF_ASN1_OBJECT;
18 typedef STACK_OF(X509_OBJECT) Cryptography_STACK_OF_X509_OBJECT;
19 """
20
21 TYPES = """
22 static const long Cryptography_HAS_102_VERIFICATION_ERROR_CODES;
23 static const long Cryptography_HAS_102_VERIFICATION_PARAMS;
24 static const long Cryptography_HAS_110_VERIFICATION_PARAMS;
25 static const long Cryptography_HAS_X509_V_FLAG_TRUSTED_FIRST;
26 static const long Cryptography_HAS_X509_V_FLAG_PARTIAL_CHAIN;
27 static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER;
28
29 typedef ... Cryptography_STACK_OF_ASN1_OBJECT;
30 typedef ... Cryptography_STACK_OF_X509_OBJECT;
31
32 typedef ... X509_OBJECT;
33 typedef ... X509_STORE;
34 typedef ... X509_VERIFY_PARAM;
35 typedef ... X509_STORE_CTX;
36
37 typedef int (*X509_STORE_CTX_get_issuer_fn)(X509 **, X509_STORE_CTX *, X509 *);
38
39 /* While these are defined in the source as ints, they're tagged here
40 as longs, just in case they ever grow to large, such as what we saw
41 with OP_ALL. */
42
43 /* Verification error codes */
44 static const int X509_V_OK;
45 static const int X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
46 static const int X509_V_ERR_UNABLE_TO_GET_CRL;
47 static const int X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE;
48 static const int X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE;
49 static const int X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
50 static const int X509_V_ERR_CERT_SIGNATURE_FAILURE;
51 static const int X509_V_ERR_CRL_SIGNATURE_FAILURE;
52 static const int X509_V_ERR_CERT_NOT_YET_VALID;
53 static const int X509_V_ERR_CERT_HAS_EXPIRED;
54 static const int X509_V_ERR_CRL_NOT_YET_VALID;
55 static const int X509_V_ERR_CRL_HAS_EXPIRED;
56 static const int X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
57 static const int X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
58 static const int X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
59 static const int X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
60 static const int X509_V_ERR_OUT_OF_MEM;
61 static const int X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
62 static const int X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
63 static const int X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
64 static const int X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
65 static const int X509_V_ERR_CERT_CHAIN_TOO_LONG;
66 static const int X509_V_ERR_CERT_REVOKED;
67 static const int X509_V_ERR_INVALID_CA;
68 static const int X509_V_ERR_PATH_LENGTH_EXCEEDED;
69 static const int X509_V_ERR_INVALID_PURPOSE;
70 static const int X509_V_ERR_CERT_UNTRUSTED;
71 static const int X509_V_ERR_CERT_REJECTED;
72 static const int X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
73 static const int X509_V_ERR_AKID_SKID_MISMATCH;
74 static const int X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
75 static const int X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
76 static const int X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
77 static const int X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
78 static const int X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
79 static const int X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
80 static const int X509_V_ERR_INVALID_NON_CA;
81 static const int X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
82 static const int X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
83 static const int X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
84 static const int X509_V_ERR_INVALID_EXTENSION;
85 static const int X509_V_ERR_INVALID_POLICY_EXTENSION;
86 static const int X509_V_ERR_NO_EXPLICIT_POLICY;
87 static const int X509_V_ERR_DIFFERENT_CRL_SCOPE;
88 static const int X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE;
89 static const int X509_V_ERR_UNNESTED_RESOURCE;
90 static const int X509_V_ERR_PERMITTED_VIOLATION;
91 static const int X509_V_ERR_EXCLUDED_VIOLATION;
92 static const int X509_V_ERR_SUBTREE_MINMAX;
93 static const int X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
94 static const int X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX;
95 static const int X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
96 static const int X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
97 static const int X509_V_ERR_SUITE_B_INVALID_VERSION;
98 static const int X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
99 static const int X509_V_ERR_SUITE_B_INVALID_CURVE;
100 static const int X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
101 static const int X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
102 static const int X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
103 static const int X509_V_ERR_HOSTNAME_MISMATCH;
104 static const int X509_V_ERR_EMAIL_MISMATCH;
105 static const int X509_V_ERR_IP_ADDRESS_MISMATCH;
106 static const int X509_V_ERR_APPLICATION_VERIFICATION;
107
108 /* Verification parameters */
109 static const long X509_V_FLAG_CB_ISSUER_CHECK;
110 static const long X509_V_FLAG_USE_CHECK_TIME;
111 static const long X509_V_FLAG_CRL_CHECK;
112 static const long X509_V_FLAG_CRL_CHECK_ALL;
113 static const long X509_V_FLAG_IGNORE_CRITICAL;
114 static const long X509_V_FLAG_X509_STRICT;
115 static const long X509_V_FLAG_ALLOW_PROXY_CERTS;
116 static const long X509_V_FLAG_POLICY_CHECK;
117 static const long X509_V_FLAG_EXPLICIT_POLICY;
118 static const long X509_V_FLAG_INHIBIT_ANY;
119 static const long X509_V_FLAG_INHIBIT_MAP;
120 static const long X509_V_FLAG_NOTIFY_POLICY;
121 static const long X509_V_FLAG_EXTENDED_CRL_SUPPORT;
122 static const long X509_V_FLAG_USE_DELTAS;
123 static const long X509_V_FLAG_CHECK_SS_SIGNATURE;
124 static const long X509_V_FLAG_TRUSTED_FIRST;
125 static const long X509_V_FLAG_SUITEB_128_LOS_ONLY;
126 static const long X509_V_FLAG_SUITEB_192_LOS;
127 static const long X509_V_FLAG_SUITEB_128_LOS;
128 static const long X509_V_FLAG_PARTIAL_CHAIN;
129
130 static const long X509_LU_X509;
131 static const long X509_LU_CRL;
132
133 static const long X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT;
134 static const long X509_CHECK_FLAG_NO_WILDCARDS;
135 static const long X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS;
136 static const long X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS;
137 static const long X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS;
138 static const long X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
139 """
140
141 FUNCTIONS = """
142 int X509_verify_cert(X509_STORE_CTX *);
143
144 /* X509_STORE */
145 X509_STORE *X509_STORE_new(void);
146 int X509_STORE_add_cert(X509_STORE *, X509 *);
147 int X509_STORE_add_crl(X509_STORE *, X509_CRL *);
148 int X509_STORE_load_locations(X509_STORE *, const char *, const char *);
149 int X509_STORE_set1_param(X509_STORE *, X509_VERIFY_PARAM *);
150 int X509_STORE_set_default_paths(X509_STORE *);
151 int X509_STORE_set_flags(X509_STORE *, unsigned long);
152 void X509_STORE_free(X509_STORE *);
153
154 /* X509_STORE_CTX */
155 X509_STORE_CTX *X509_STORE_CTX_new(void);
156 void X509_STORE_CTX_cleanup(X509_STORE_CTX *);
157 void X509_STORE_CTX_free(X509_STORE_CTX *);
158 int X509_STORE_CTX_init(X509_STORE_CTX *, X509_STORE *, X509 *,
159 Cryptography_STACK_OF_X509 *);
160 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *,
161 Cryptography_STACK_OF_X509 *);
162 void X509_STORE_CTX_set_cert(X509_STORE_CTX *, X509 *);
163 void X509_STORE_CTX_set_chain(X509_STORE_CTX *,Cryptography_STACK_OF_X509 *);
164 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *);
165 void X509_STORE_CTX_set0_param(X509_STORE_CTX *, X509_VERIFY_PARAM *);
166 int X509_STORE_CTX_set_default(X509_STORE_CTX *, const char *);
167 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *,
168 int (*)(int, X509_STORE_CTX *));
169 Cryptography_STACK_OF_X509 *X509_STORE_CTX_get_chain(X509_STORE_CTX *);
170 Cryptography_STACK_OF_X509 *X509_STORE_CTX_get1_chain(X509_STORE_CTX *);
171 int X509_STORE_CTX_get_error(X509_STORE_CTX *);
172 void X509_STORE_CTX_set_error(X509_STORE_CTX *, int);
173 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *);
174 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *);
175 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *, int, void *);
176 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *, int);
177 int X509_STORE_CTX_get1_issuer(X509 **, X509_STORE_CTX *, X509 *);
178
179 /* X509_VERIFY_PARAM */
180 X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
181 int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *, unsigned long);
182 int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *, unsigned long);
183 unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *);
184 int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *, int);
185 int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *, int);
186 void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *, time_t);
187 int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *, ASN1_OBJECT *);
188 int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *,
189 Cryptography_STACK_OF_ASN1_OBJECT *);
190 void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *, int);
191 int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *);
192 void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *);
193 /* this CRYPTO_EX_DATA function became a macro in 1.1.0 */
194 int X509_STORE_CTX_get_ex_new_index(long, void *, CRYPTO_EX_new *,
195 CRYPTO_EX_dup *, CRYPTO_EX_free *);
196
197 /* X509_STORE_CTX */
198 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *,
199 Cryptography_STACK_OF_X509_CRL *);
200
201 /* X509_VERIFY_PARAM */
202 int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *, const char *,
203 size_t);
204 void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *, unsigned int);
205 int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *, const char *,
206 size_t);
207 int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *, const unsigned char *,
208 size_t);
209 int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *, const char *);
210
211 int sk_X509_OBJECT_num(Cryptography_STACK_OF_X509_OBJECT *);
212 X509_OBJECT *sk_X509_OBJECT_value(Cryptography_STACK_OF_X509_OBJECT *, int);
213 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *);
214 Cryptography_STACK_OF_X509_OBJECT *X509_STORE_get0_objects(X509_STORE *);
215 X509 *X509_OBJECT_get0_X509(X509_OBJECT *);
216 int X509_OBJECT_get_type(const X509_OBJECT *);
217
218 /* added in 1.1.0 */
219 X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *);
220 X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *);
221 void X509_STORE_set_get_issuer(X509_STORE *, X509_STORE_CTX_get_issuer_fn);
222
223 /* added in 3.3.0 */
224 Cryptography_STACK_OF_X509_OBJECT *X509_STORE_get1_objects(X509_STORE *);
225 typedef void (*sk_X509_OBJECT_freefunc)(X509_OBJECT *);
226 void sk_X509_OBJECT_pop_free(Cryptography_STACK_OF_X509_OBJECT *, sk_X509_OBJECT_freefunc);
227 void X509_OBJECT_free(X509_OBJECT *a);
228
229
230 """
231
232 CUSTOMIZATIONS = """
233 /* OpenSSL 1.0.2+ verification parameters and error codes */
234 #if CRYPTOGRAPHY_OPENSSL_102_OR_GREATER
235 static const long Cryptography_HAS_102_VERIFICATION_ERROR_CODES = 1;
236 static const long Cryptography_HAS_102_VERIFICATION_PARAMS = 1;
237 #else
238 static const long Cryptography_HAS_102_VERIFICATION_ERROR_CODES = 0;
239 static const long Cryptography_HAS_102_VERIFICATION_PARAMS = 0;
240
241 static const long X509_V_ERR_SUITE_B_INVALID_VERSION = 0;
242 static const long X509_V_ERR_SUITE_B_INVALID_ALGORITHM = 0;
243 static const long X509_V_ERR_SUITE_B_INVALID_CURVE = 0;
244 static const long X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM = 0;
245 static const long X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED = 0;
246 static const long X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256 = 0;
247 /* These 3 defines are unavailable in LibreSSL 2.5.x, but may be added
248 in the future... */
249 #ifndef X509_V_ERR_HOSTNAME_MISMATCH
250 static const long X509_V_ERR_HOSTNAME_MISMATCH = 0;
251 #endif
252 #ifndef X509_V_ERR_EMAIL_MISMATCH
253 static const long X509_V_ERR_EMAIL_MISMATCH = 0;
254 #endif
255 #ifndef X509_V_ERR_IP_ADDRESS_MISMATCH
256 static const long X509_V_ERR_IP_ADDRESS_MISMATCH = 0;
257 #endif
258 #ifndef X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
259 static const long X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT = 0;
260 #endif
261 #ifndef X509_CHECK_FLAG_NO_WILDCARDS
262 static const long X509_CHECK_FLAG_NO_WILDCARDS = 0;
263 #endif
264 #ifndef X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
265 static const long X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS = 0;
266 #endif
267 #ifndef X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
268 static const long X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS = 0;
269 #endif
270 #ifndef X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
271 static const long X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS = 0;
272 #endif
273
274 /* X509_V_FLAG_TRUSTED_FIRST is also new in 1.0.2+, but it is added separately
275 below because it shows up in some earlier 3rd party OpenSSL packages. */
276 static const long X509_V_FLAG_SUITEB_128_LOS_ONLY = 0;
277 static const long X509_V_FLAG_SUITEB_192_LOS = 0;
278 static const long X509_V_FLAG_SUITEB_128_LOS = 0;
279
280 #if !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER
281 int (*X509_VERIFY_PARAM_set1_host)(X509_VERIFY_PARAM *, const char *,
282 size_t) = NULL;
283 int (*X509_VERIFY_PARAM_set1_email)(X509_VERIFY_PARAM *, const char *,
284 size_t) = NULL;
285 int (*X509_VERIFY_PARAM_set1_ip)(X509_VERIFY_PARAM *, const unsigned char *,
286 size_t) = NULL;
287 int (*X509_VERIFY_PARAM_set1_ip_asc)(X509_VERIFY_PARAM *, const char *) = NULL;
288 void (*X509_VERIFY_PARAM_set_hostflags)(X509_VERIFY_PARAM *,
289 unsigned int) = NULL;
290 #endif
291 #endif
292
293 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 || CRYPTOGRAPHY_IS_LIBRESSL
294 static const long Cryptography_HAS_110_VERIFICATION_PARAMS = 0;
295 #ifndef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
296 static const long X509_CHECK_FLAG_NEVER_CHECK_SUBJECT = 0;
297 #endif
298 #else
299 static const long Cryptography_HAS_110_VERIFICATION_PARAMS = 1;
300 #endif
301
302 /* OpenSSL 1.0.2+ or Solaris's backport */
303 #ifdef X509_V_FLAG_PARTIAL_CHAIN
304 static const long Cryptography_HAS_X509_V_FLAG_PARTIAL_CHAIN = 1;
305 #else
306 static const long Cryptography_HAS_X509_V_FLAG_PARTIAL_CHAIN = 0;
307 static const long X509_V_FLAG_PARTIAL_CHAIN = 0;
308 #endif
309
310 /* OpenSSL 1.0.2+, *or* Fedora 20's flavor of OpenSSL 1.0.1e... */
311 #ifdef X509_V_FLAG_TRUSTED_FIRST
312 static const long Cryptography_HAS_X509_V_FLAG_TRUSTED_FIRST = 1;
313 #else
314 static const long Cryptography_HAS_X509_V_FLAG_TRUSTED_FIRST = 0;
315 static const long X509_V_FLAG_TRUSTED_FIRST = 0;
316 #endif
317
318 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 && !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER
319 Cryptography_STACK_OF_X509_OBJECT *X509_STORE_get0_objects(X509_STORE *ctx) {
320 return ctx->objs;
321 }
322 X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *store) {
323 return store->param;
324 }
325 int X509_OBJECT_get_type(const X509_OBJECT *x) {
326 return x->type;
327 }
328
329 /* from x509/x509_vfy.c */
330 X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
331 {
332 return ctx->cert;
333 }
334
335 X509 *X509_OBJECT_get0_X509(X509_OBJECT *x) {
336 return x->data.x509;
337 }
338 #endif
339
340 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110
341 static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER = 0;
342 typedef void *X509_STORE_CTX_get_issuer_fn;
343 X509_STORE_CTX_get_issuer_fn (*X509_STORE_get_get_issuer)(X509_STORE *) = NULL;
344 void (*X509_STORE_set_get_issuer)(X509_STORE *,
345 X509_STORE_CTX_get_issuer_fn) = NULL;
346 #else
347 static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER = 1;
348 #endif
349
350 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_330
351 static X509_OBJECT *x509_object_dup(const X509_OBJECT *obj)
352 {
353 int ok;
354 X509_OBJECT *ret = X509_OBJECT_new();
355 if (ret == NULL) {
356 return NULL;
357 }
358 switch (X509_OBJECT_get_type(obj)) {
359 case X509_LU_X509:
360 ok = X509_OBJECT_set1_X509(ret, X509_OBJECT_get0_X509(obj));
361 break;
362 case X509_LU_CRL:
363 /* X509_OBJECT_get0_X509_CRL was not const-correct prior to 3.0.*/
364 ok = X509_OBJECT_set1_X509_CRL(
365 ret, X509_OBJECT_get0_X509_CRL((X509_OBJECT *)obj));
366 break;
367 default:
368 /* We cannot duplicate unrecognized types in a polyfill, but it is
369 * safe to leave an empty object. The caller will ignore it. */
370 ok = 1;
371 break;
372 }
373 if (!ok) {
374 X509_OBJECT_free(ret);
375 return NULL;
376 }
377 return ret;
378 }
379
380 Cryptography_STACK_OF_X509_OBJECT *
381 X509_STORE_get1_objects(X509_STORE *store)
382 {
383 STACK_OF(X509_OBJECT) *ret;
384 if (!X509_STORE_lock(store)) {
385 return NULL;
386 }
387 ret = sk_X509_OBJECT_deep_copy(X509_STORE_get0_objects(store),
388 x509_object_dup, X509_OBJECT_free);
389 X509_STORE_unlock(store);
390 return ret;
391 }
392 #endif
393 """