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