x509_vfy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <openssl/x509_vfy.h>
  7. /*
  8. * This is part of a work-around for the difficulty cffi has in dealing with
  9. * `STACK_OF(foo)` as the name of a type. We invent a new, simpler name that
  10. * will be an alias for this type and use the alias throughout. This works
  11. * together with another opaque typedef for the same name in the TYPES section.
  12. * Note that the result is an opaque type.
  13. */
  14. typedef STACK_OF(ASN1_OBJECT) Cryptography_STACK_OF_ASN1_OBJECT;
  15. typedef STACK_OF(X509_OBJECT) Cryptography_STACK_OF_X509_OBJECT;
  16. """
  17. TYPES = """
  18. static const long Cryptography_HAS_102_VERIFICATION;
  19. static const long Cryptography_HAS_110_VERIFICATION_PARAMS;
  20. static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER;
  21. typedef ... Cryptography_STACK_OF_ASN1_OBJECT;
  22. typedef ... Cryptography_STACK_OF_X509_OBJECT;
  23. typedef ... X509_OBJECT;
  24. typedef ... X509_STORE;
  25. typedef ... X509_VERIFY_PARAM;
  26. typedef ... X509_STORE_CTX;
  27. typedef int (*X509_STORE_CTX_get_issuer_fn)(X509 **, X509_STORE_CTX *, X509 *);
  28. /* While these are defined in the source as ints, they're tagged here
  29. as longs, just in case they ever grow to large, such as what we saw
  30. with OP_ALL. */
  31. /* Verification error codes */
  32. static const int X509_V_OK;
  33. static const int X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  34. static const int X509_V_ERR_UNABLE_TO_GET_CRL;
  35. static const int X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE;
  36. static const int X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE;
  37. static const int X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  38. static const int X509_V_ERR_CERT_SIGNATURE_FAILURE;
  39. static const int X509_V_ERR_CRL_SIGNATURE_FAILURE;
  40. static const int X509_V_ERR_CERT_NOT_YET_VALID;
  41. static const int X509_V_ERR_CERT_HAS_EXPIRED;
  42. static const int X509_V_ERR_CRL_NOT_YET_VALID;
  43. static const int X509_V_ERR_CRL_HAS_EXPIRED;
  44. static const int X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  45. static const int X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  46. static const int X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  47. static const int X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  48. static const int X509_V_ERR_OUT_OF_MEM;
  49. static const int X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  50. static const int X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  51. static const int X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  52. static const int X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  53. static const int X509_V_ERR_CERT_CHAIN_TOO_LONG;
  54. static const int X509_V_ERR_CERT_REVOKED;
  55. static const int X509_V_ERR_INVALID_CA;
  56. static const int X509_V_ERR_PATH_LENGTH_EXCEEDED;
  57. static const int X509_V_ERR_INVALID_PURPOSE;
  58. static const int X509_V_ERR_CERT_UNTRUSTED;
  59. static const int X509_V_ERR_CERT_REJECTED;
  60. static const int X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
  61. static const int X509_V_ERR_AKID_SKID_MISMATCH;
  62. static const int X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
  63. static const int X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
  64. static const int X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  65. static const int X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  66. static const int X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  67. static const int X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  68. static const int X509_V_ERR_INVALID_NON_CA;
  69. static const int X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  70. static const int X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
  71. static const int X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  72. static const int X509_V_ERR_INVALID_EXTENSION;
  73. static const int X509_V_ERR_INVALID_POLICY_EXTENSION;
  74. static const int X509_V_ERR_NO_EXPLICIT_POLICY;
  75. static const int X509_V_ERR_DIFFERENT_CRL_SCOPE;
  76. static const int X509_V_ERR_UNSUPPORTED_EXTENSION_FEATURE;
  77. static const int X509_V_ERR_UNNESTED_RESOURCE;
  78. static const int X509_V_ERR_PERMITTED_VIOLATION;
  79. static const int X509_V_ERR_EXCLUDED_VIOLATION;
  80. static const int X509_V_ERR_SUBTREE_MINMAX;
  81. static const int X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
  82. static const int X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX;
  83. static const int X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
  84. static const int X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  85. static const int X509_V_ERR_SUITE_B_INVALID_VERSION;
  86. static const int X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
  87. static const int X509_V_ERR_SUITE_B_INVALID_CURVE;
  88. static const int X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
  89. static const int X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
  90. static const int X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
  91. static const int X509_V_ERR_HOSTNAME_MISMATCH;
  92. static const int X509_V_ERR_EMAIL_MISMATCH;
  93. static const int X509_V_ERR_IP_ADDRESS_MISMATCH;
  94. static const int X509_V_ERR_APPLICATION_VERIFICATION;
  95. /* Verification parameters */
  96. static const long X509_V_FLAG_CB_ISSUER_CHECK;
  97. static const long X509_V_FLAG_USE_CHECK_TIME;
  98. static const long X509_V_FLAG_CRL_CHECK;
  99. static const long X509_V_FLAG_CRL_CHECK_ALL;
  100. static const long X509_V_FLAG_IGNORE_CRITICAL;
  101. static const long X509_V_FLAG_X509_STRICT;
  102. static const long X509_V_FLAG_ALLOW_PROXY_CERTS;
  103. static const long X509_V_FLAG_POLICY_CHECK;
  104. static const long X509_V_FLAG_EXPLICIT_POLICY;
  105. static const long X509_V_FLAG_INHIBIT_ANY;
  106. static const long X509_V_FLAG_INHIBIT_MAP;
  107. static const long X509_V_FLAG_NOTIFY_POLICY;
  108. static const long X509_V_FLAG_EXTENDED_CRL_SUPPORT;
  109. static const long X509_V_FLAG_USE_DELTAS;
  110. static const long X509_V_FLAG_CHECK_SS_SIGNATURE;
  111. static const long X509_V_FLAG_TRUSTED_FIRST;
  112. static const long X509_V_FLAG_SUITEB_128_LOS_ONLY;
  113. static const long X509_V_FLAG_SUITEB_192_LOS;
  114. static const long X509_V_FLAG_SUITEB_128_LOS;
  115. static const long X509_V_FLAG_PARTIAL_CHAIN;
  116. static const long X509_LU_X509;
  117. static const long X509_LU_CRL;
  118. static const long X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT;
  119. static const long X509_CHECK_FLAG_NO_WILDCARDS;
  120. static const long X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS;
  121. static const long X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS;
  122. static const long X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS;
  123. static const long X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  124. """
  125. FUNCTIONS = """
  126. int X509_verify_cert(X509_STORE_CTX *);
  127. /* X509_STORE */
  128. X509_STORE *X509_STORE_new(void);
  129. int X509_STORE_add_cert(X509_STORE *, X509 *);
  130. int X509_STORE_add_crl(X509_STORE *, X509_CRL *);
  131. int X509_STORE_load_locations(X509_STORE *, const char *, const char *);
  132. int X509_STORE_set1_param(X509_STORE *, X509_VERIFY_PARAM *);
  133. int X509_STORE_set_default_paths(X509_STORE *);
  134. int X509_STORE_set_flags(X509_STORE *, unsigned long);
  135. void X509_STORE_free(X509_STORE *);
  136. /* X509_STORE_CTX */
  137. X509_STORE_CTX *X509_STORE_CTX_new(void);
  138. void X509_STORE_CTX_cleanup(X509_STORE_CTX *);
  139. void X509_STORE_CTX_free(X509_STORE_CTX *);
  140. int X509_STORE_CTX_init(X509_STORE_CTX *, X509_STORE *, X509 *,
  141. Cryptography_STACK_OF_X509 *);
  142. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *,
  143. Cryptography_STACK_OF_X509 *);
  144. void X509_STORE_CTX_set_cert(X509_STORE_CTX *, X509 *);
  145. void X509_STORE_CTX_set_chain(X509_STORE_CTX *,Cryptography_STACK_OF_X509 *);
  146. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *);
  147. void X509_STORE_CTX_set0_param(X509_STORE_CTX *, X509_VERIFY_PARAM *);
  148. int X509_STORE_CTX_set_default(X509_STORE_CTX *, const char *);
  149. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *,
  150. int (*)(int, X509_STORE_CTX *));
  151. Cryptography_STACK_OF_X509 *X509_STORE_CTX_get_chain(X509_STORE_CTX *);
  152. Cryptography_STACK_OF_X509 *X509_STORE_CTX_get1_chain(X509_STORE_CTX *);
  153. int X509_STORE_CTX_get_error(X509_STORE_CTX *);
  154. void X509_STORE_CTX_set_error(X509_STORE_CTX *, int);
  155. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *);
  156. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *);
  157. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *, int, void *);
  158. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *, int);
  159. int X509_STORE_CTX_get1_issuer(X509 **, X509_STORE_CTX *, X509 *);
  160. /* X509_VERIFY_PARAM */
  161. X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void);
  162. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *, unsigned long);
  163. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *, unsigned long);
  164. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *);
  165. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *, int);
  166. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *, int);
  167. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *, time_t);
  168. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *, ASN1_OBJECT *);
  169. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *,
  170. Cryptography_STACK_OF_ASN1_OBJECT *);
  171. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *, int);
  172. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *);
  173. void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *);
  174. /* this CRYPTO_EX_DATA function became a macro in 1.1.0 */
  175. int X509_STORE_CTX_get_ex_new_index(long, void *, CRYPTO_EX_new *,
  176. CRYPTO_EX_dup *, CRYPTO_EX_free *);
  177. /* X509_STORE_CTX */
  178. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *,
  179. Cryptography_STACK_OF_X509_CRL *);
  180. /* X509_VERIFY_PARAM */
  181. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *, const char *,
  182. size_t);
  183. void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *, unsigned int);
  184. int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *, const char *,
  185. size_t);
  186. int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *, const unsigned char *,
  187. size_t);
  188. int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *, const char *);
  189. int sk_X509_OBJECT_num(Cryptography_STACK_OF_X509_OBJECT *);
  190. X509_OBJECT *sk_X509_OBJECT_value(Cryptography_STACK_OF_X509_OBJECT *, int);
  191. X509_VERIFY_PARAM *X509_STORE_get0_param(X509_STORE *);
  192. Cryptography_STACK_OF_X509_OBJECT *X509_STORE_get0_objects(X509_STORE *);
  193. X509 *X509_OBJECT_get0_X509(X509_OBJECT *);
  194. int X509_OBJECT_get_type(const X509_OBJECT *);
  195. /* added in 1.1.0 */
  196. X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *);
  197. X509_STORE_CTX_get_issuer_fn X509_STORE_get_get_issuer(X509_STORE *);
  198. void X509_STORE_set_get_issuer(X509_STORE *, X509_STORE_CTX_get_issuer_fn);
  199. """
  200. CUSTOMIZATIONS = """
  201. #if !CRYPTOGRAPHY_IS_LIBRESSL
  202. static const long Cryptography_HAS_102_VERIFICATION = 1;
  203. #else
  204. static const long Cryptography_HAS_102_VERIFICATION = 0;
  205. static const long X509_V_ERR_SUITE_B_INVALID_VERSION = 0;
  206. static const long X509_V_ERR_SUITE_B_INVALID_ALGORITHM = 0;
  207. static const long X509_V_ERR_SUITE_B_INVALID_CURVE = 0;
  208. static const long X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM = 0;
  209. static const long X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED = 0;
  210. static const long X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256 = 0;
  211. static const long X509_V_FLAG_SUITEB_128_LOS_ONLY = 0;
  212. static const long X509_V_FLAG_SUITEB_192_LOS = 0;
  213. static const long X509_V_FLAG_SUITEB_128_LOS = 0;
  214. #endif
  215. #if CRYPTOGRAPHY_IS_LIBRESSL
  216. static const long Cryptography_HAS_110_VERIFICATION_PARAMS = 0;
  217. #ifndef X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
  218. static const long X509_CHECK_FLAG_NEVER_CHECK_SUBJECT = 0;
  219. #endif
  220. #else
  221. static const long Cryptography_HAS_110_VERIFICATION_PARAMS = 1;
  222. #endif
  223. #if CRYPTOGRAPHY_IS_LIBRESSL
  224. static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER = 0;
  225. typedef void *X509_STORE_CTX_get_issuer_fn;
  226. X509_STORE_CTX_get_issuer_fn (*X509_STORE_get_get_issuer)(X509_STORE *) = NULL;
  227. void (*X509_STORE_set_get_issuer)(X509_STORE *,
  228. X509_STORE_CTX_get_issuer_fn) = NULL;
  229. #else
  230. static const long Cryptography_HAS_X509_STORE_CTX_GET_ISSUER = 1;
  231. #endif
  232. """