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_LOCKING_CALLBACKS;
13static const long Cryptography_HAS_MEM_FUNCTIONS;
14static const long Cryptography_HAS_OPENSSL_CLEANUP;
15
16static const int SSLEAY_VERSION;
17static const int SSLEAY_CFLAGS;
18static const int SSLEAY_PLATFORM;
19static const int SSLEAY_DIR;
20static const int SSLEAY_BUILT_ON;
21static const int OPENSSL_VERSION;
22static const int OPENSSL_CFLAGS;
23static const int OPENSSL_BUILT_ON;
24static const int OPENSSL_PLATFORM;
25static const int OPENSSL_DIR;
26static const int CRYPTO_MEM_CHECK_ON;
27static const int CRYPTO_MEM_CHECK_OFF;
28static const int CRYPTO_MEM_CHECK_ENABLE;
29static const int CRYPTO_MEM_CHECK_DISABLE;
30static const int CRYPTO_LOCK;
31static const int CRYPTO_UNLOCK;
32static const int CRYPTO_READ;
33static const int CRYPTO_LOCK_SSL;
34"""
35
36FUNCTIONS = """
37int CRYPTO_mem_ctrl(int);
38
39void CRYPTO_cleanup_all_ex_data(void);
40void OPENSSL_cleanup(void);
41
42/* as of 1.1.0 OpenSSL does its own locking *angelic chorus*. These functions
43   have become macros that are no ops */
44int CRYPTO_num_locks(void);
45void CRYPTO_set_locking_callback(void(*)(int, int, const char *, int));
46void (*CRYPTO_get_locking_callback(void))(int, int, const char *, int);
47
48/* SSLeay was removed in 1.1.0 */
49unsigned long SSLeay(void);
50const char *SSLeay_version(int);
51/* these functions were added to replace the SSLeay functions in 1.1.0 */
52unsigned long OpenSSL_version_num(void);
53const char *OpenSSL_version(int);
54
55/* this is a macro in 1.1.0 */
56void *OPENSSL_malloc(size_t);
57void OPENSSL_free(void *);
58
59/* This was removed in 1.1.0 */
60void CRYPTO_lock(int, int, const char *, int);
61
62/* Signature changed significantly in 1.1.0, only expose there for sanity */
63int Cryptography_CRYPTO_set_mem_functions(
64    void *(*)(size_t, const char *, int),
65    void *(*)(void *, size_t, const char *, int),
66    void (*)(void *, const char *, int));
67
68void *Cryptography_malloc_wrapper(size_t, const char *, int);
69void *Cryptography_realloc_wrapper(void *, size_t, const char *, int);
70void Cryptography_free_wrapper(void *, const char *, int);
71"""
72
73CUSTOMIZATIONS = """
74/* In 1.1.0 SSLeay has finally been retired. We bidirectionally define the
75   values so you can use either one. This is so we can use the new function
76   names no matter what OpenSSL we're running on, but users on older pyOpenSSL
77   releases won't see issues if they're running OpenSSL 1.1.0 */
78#if !defined(SSLEAY_VERSION)
79# define SSLeay                  OpenSSL_version_num
80# define SSLeay_version          OpenSSL_version
81# define SSLEAY_VERSION_NUMBER   OPENSSL_VERSION_NUMBER
82# define SSLEAY_VERSION          OPENSSL_VERSION
83# define SSLEAY_CFLAGS           OPENSSL_CFLAGS
84# define SSLEAY_BUILT_ON         OPENSSL_BUILT_ON
85# define SSLEAY_PLATFORM         OPENSSL_PLATFORM
86# define SSLEAY_DIR              OPENSSL_DIR
87#endif
88#if !defined(OPENSSL_VERSION)
89# define OpenSSL_version_num     SSLeay
90# define OpenSSL_version         SSLeay_version
91# define OPENSSL_VERSION         SSLEAY_VERSION
92# define OPENSSL_CFLAGS          SSLEAY_CFLAGS
93# define OPENSSL_BUILT_ON        SSLEAY_BUILT_ON
94# define OPENSSL_PLATFORM        SSLEAY_PLATFORM
95# define OPENSSL_DIR             SSLEAY_DIR
96#endif
97#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110
98static const long Cryptography_HAS_LOCKING_CALLBACKS = 1;
99#else
100static const long Cryptography_HAS_LOCKING_CALLBACKS = 0;
101#if !defined(CRYPTO_LOCK)
102static const long CRYPTO_LOCK = 0;
103#endif
104#if !defined(CRYPTO_UNLOCK)
105static const long CRYPTO_UNLOCK = 0;
106#endif
107#if !defined(CRYPTO_READ)
108static const long CRYPTO_READ = 0;
109#endif
110#if !defined(CRYPTO_LOCK_SSL)
111static const long CRYPTO_LOCK_SSL = 0;
112#endif
113void (*CRYPTO_lock)(int, int, const char *, int) = NULL;
114#endif
115
116#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110
117static const long Cryptography_HAS_OPENSSL_CLEANUP = 0;
118
119void (*OPENSSL_cleanup)(void) = NULL;
120
121/* This function has a significantly different signature pre-1.1.0. since it is
122 * for testing only, we don't bother to expose it on older OpenSSLs.
123 */
124static const long Cryptography_HAS_MEM_FUNCTIONS = 0;
125int (*Cryptography_CRYPTO_set_mem_functions)(
126    void *(*)(size_t, const char *, int),
127    void *(*)(void *, size_t, const char *, int),
128    void (*)(void *, const char *, int)) = NULL;
129
130#else
131static const long Cryptography_HAS_OPENSSL_CLEANUP = 1;
132static const long Cryptography_HAS_MEM_FUNCTIONS = 1;
133
134int Cryptography_CRYPTO_set_mem_functions(
135    void *(*m)(size_t, const char *, int),
136    void *(*r)(void *, size_t, const char *, int),
137    void (*f)(void *, const char *, int)
138) {
139    return CRYPTO_set_mem_functions(m, r, f);
140}
141#endif
142
143void *Cryptography_malloc_wrapper(size_t size, const char *path, int line) {
144    return malloc(size);
145}
146
147void *Cryptography_realloc_wrapper(void *ptr, size_t size, const char *path,
148                                   int line) {
149    return realloc(ptr, size);
150}
151
152void Cryptography_free_wrapper(void *ptr, const char *path, int line) {
153    free(ptr);
154}
155"""
156