crypto.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/crypto.h>
  7. """
  8. TYPES = """
  9. static const long Cryptography_HAS_MEM_FUNCTIONS;
  10. static const long Cryptography_HAS_OPENSSL_CLEANUP;
  11. static const int SSLEAY_VERSION;
  12. static const int SSLEAY_CFLAGS;
  13. static const int SSLEAY_PLATFORM;
  14. static const int SSLEAY_DIR;
  15. static const int SSLEAY_BUILT_ON;
  16. static const int OPENSSL_VERSION;
  17. static const int OPENSSL_CFLAGS;
  18. static const int OPENSSL_BUILT_ON;
  19. static const int OPENSSL_PLATFORM;
  20. static const int OPENSSL_DIR;
  21. """
  22. FUNCTIONS = """
  23. void OPENSSL_cleanup(void);
  24. /* SSLeay was removed in 1.1.0 */
  25. unsigned long SSLeay(void);
  26. const char *SSLeay_version(int);
  27. /* these functions were added to replace the SSLeay functions in 1.1.0 */
  28. unsigned long OpenSSL_version_num(void);
  29. const char *OpenSSL_version(int);
  30. /* this is a macro in 1.1.0 */
  31. void *OPENSSL_malloc(size_t);
  32. void OPENSSL_free(void *);
  33. /* Signature changed significantly in 1.1.0, only expose there for sanity */
  34. int Cryptography_CRYPTO_set_mem_functions(
  35. void *(*)(size_t, const char *, int),
  36. void *(*)(void *, size_t, const char *, int),
  37. void (*)(void *, const char *, int));
  38. void *Cryptography_malloc_wrapper(size_t, const char *, int);
  39. void *Cryptography_realloc_wrapper(void *, size_t, const char *, int);
  40. void Cryptography_free_wrapper(void *, const char *, int);
  41. """
  42. CUSTOMIZATIONS = """
  43. /* In 1.1.0 SSLeay has finally been retired. We bidirectionally define the
  44. values so you can use either one. This is so we can use the new function
  45. names no matter what OpenSSL we're running on, but users on older pyOpenSSL
  46. releases won't see issues if they're running OpenSSL 1.1.0 */
  47. #if !defined(SSLEAY_VERSION)
  48. # define SSLeay OpenSSL_version_num
  49. # define SSLeay_version OpenSSL_version
  50. # define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER
  51. # define SSLEAY_VERSION OPENSSL_VERSION
  52. # define SSLEAY_CFLAGS OPENSSL_CFLAGS
  53. # define SSLEAY_BUILT_ON OPENSSL_BUILT_ON
  54. # define SSLEAY_PLATFORM OPENSSL_PLATFORM
  55. # define SSLEAY_DIR OPENSSL_DIR
  56. #endif
  57. #if !defined(OPENSSL_VERSION)
  58. # define OpenSSL_version_num SSLeay
  59. # define OpenSSL_version SSLeay_version
  60. # define OPENSSL_VERSION SSLEAY_VERSION
  61. # define OPENSSL_CFLAGS SSLEAY_CFLAGS
  62. # define OPENSSL_BUILT_ON SSLEAY_BUILT_ON
  63. # define OPENSSL_PLATFORM SSLEAY_PLATFORM
  64. # define OPENSSL_DIR SSLEAY_DIR
  65. #endif
  66. #if CRYPTOGRAPHY_IS_LIBRESSL
  67. static const long Cryptography_HAS_OPENSSL_CLEANUP = 0;
  68. void (*OPENSSL_cleanup)(void) = NULL;
  69. /* This function has a significantly different signature pre-1.1.0. since it is
  70. * for testing only, we don't bother to expose it on older OpenSSLs.
  71. */
  72. static const long Cryptography_HAS_MEM_FUNCTIONS = 0;
  73. int (*Cryptography_CRYPTO_set_mem_functions)(
  74. void *(*)(size_t, const char *, int),
  75. void *(*)(void *, size_t, const char *, int),
  76. void (*)(void *, const char *, int)) = NULL;
  77. #else
  78. static const long Cryptography_HAS_OPENSSL_CLEANUP = 1;
  79. static const long Cryptography_HAS_MEM_FUNCTIONS = 1;
  80. int Cryptography_CRYPTO_set_mem_functions(
  81. void *(*m)(size_t, const char *, int),
  82. void *(*r)(void *, size_t, const char *, int),
  83. void (*f)(void *, const char *, int)
  84. ) {
  85. return CRYPTO_set_mem_functions(m, r, f);
  86. }
  87. #endif
  88. void *Cryptography_malloc_wrapper(size_t size, const char *path, int line) {
  89. return malloc(size);
  90. }
  91. void *Cryptography_realloc_wrapper(void *ptr, size_t size, const char *path,
  92. int line) {
  93. return realloc(ptr, size);
  94. }
  95. void Cryptography_free_wrapper(void *ptr, const char *path, int line) {
  96. free(ptr);
  97. }
  98. """