Debian Codegraph @

lib_pypy/_cffi_ssl/_cffi_src/openssl/callbacks.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/ssl.h>
9 #include <openssl/x509.h>
10 #include <openssl/x509_vfy.h>
11 #include <openssl/crypto.h>
12
13 #ifdef _WIN32
14 #define WIN32_LEAN_AND_MEAN
15 #include <Windows.h>
16 #include <Wincrypt.h>
17 #include <Winsock2.h>
18 #else
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pthread.h>
22 #endif
23 """
24
25 TYPES = """
26 typedef struct {
27 char *password;
28 int length;
29 int called;
30 int error;
31 int maxsize;
32 } CRYPTOGRAPHY_PASSWORD_DATA;
33 """
34
35 FUNCTIONS = """
36 int Cryptography_setup_ssl_threads(void);
37 int Cryptography_pem_password_cb(char *, int, int, void *);
38 """
39
40 CUSTOMIZATIONS = """
41 /* This code is derived from the locking code found in the Python _ssl module's
42 locking callback for OpenSSL.
43
44 Copyright 2001-2016 Python Software Foundation; All Rights Reserved.
45
46 It has been subsequently modified to use cross platform locking without
47 using CPython APIs by Armin Rigo of the PyPy project.
48 */
49
50 #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110
51 #ifdef _WIN32
52 typedef CRITICAL_SECTION Cryptography_mutex;
53 static __inline void cryptography_mutex_init(Cryptography_mutex *mutex) {
54 InitializeCriticalSection(mutex);
55 }
56 static __inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {
57 EnterCriticalSection(mutex);
58 }
59 static __inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {
60 LeaveCriticalSection(mutex);
61 }
62 #else
63 typedef pthread_mutex_t Cryptography_mutex;
64 #define ASSERT_STATUS(call) \
65 if ((call) != 0) { \
66 perror("Fatal error in callback initialization: " #call); \
67 abort(); \
68 }
69 static inline void cryptography_mutex_init(Cryptography_mutex *mutex) {
70 #if !defined(pthread_mutexattr_default)
71 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
72 #endif
73 ASSERT_STATUS(pthread_mutex_init(mutex, pthread_mutexattr_default));
74 }
75 static inline void cryptography_mutex_lock(Cryptography_mutex *mutex) {
76 ASSERT_STATUS(pthread_mutex_lock(mutex));
77 }
78 static inline void cryptography_mutex_unlock(Cryptography_mutex *mutex) {
79 ASSERT_STATUS(pthread_mutex_unlock(mutex));
80 }
81 #endif
82
83
84 static unsigned int _ssl_locks_count = 0;
85 static Cryptography_mutex *_ssl_locks = NULL;
86
87 static void _ssl_thread_locking_function(int mode, int n, const char *file,
88 int line) {
89 /* this function is needed to perform locking on shared data
90 structures. (Note that OpenSSL uses a number of global data
91 structures that will be implicitly shared whenever multiple
92 threads use OpenSSL.) Multi-threaded applications will
93 crash at random if it is not set.
94
95 locking_function() must be able to handle up to
96 CRYPTO_num_locks() different mutex locks. It sets the n-th
97 lock if mode & CRYPTO_LOCK, and releases it otherwise.
98
99 file and line are the file number of the function setting the
100 lock. They can be useful for debugging.
101 */
102
103 if ((_ssl_locks == NULL) ||
104 (n < 0) || ((unsigned)n >= _ssl_locks_count)) {
105 return;
106 }
107
108 if (mode & CRYPTO_LOCK) {
109 cryptography_mutex_lock(_ssl_locks + n);
110 } else {
111 cryptography_mutex_unlock(_ssl_locks + n);
112 }
113 }
114
115 static void init_mutexes(void) {
116 int i;
117 for (i = 0; i < _ssl_locks_count; i++) {
118 cryptography_mutex_init(_ssl_locks + i);
119 }
120 }
121
122
123 int Cryptography_setup_ssl_threads(void) {
124 if (_ssl_locks == NULL) {
125 _ssl_locks_count = CRYPTO_num_locks();
126 _ssl_locks = calloc(_ssl_locks_count, sizeof(Cryptography_mutex));
127 if (_ssl_locks == NULL) {
128 return 0;
129 }
130 init_mutexes();
131 CRYPTO_set_locking_callback(_ssl_thread_locking_function);
132 #ifndef _WIN32
133 pthread_atfork(NULL, NULL, &init_mutexes);
134 #endif
135 }
136 return 1;
137 }
138 #else
139 int (*Cryptography_setup_ssl_threads)(void) = NULL;
140 #endif
141
142 typedef struct {
143 char *password;
144 int length;
145 int called;
146 int error;
147 int maxsize;
148 } CRYPTOGRAPHY_PASSWORD_DATA;
149
150 int Cryptography_pem_password_cb(char *buf, int size,
151 int rwflag, void *userdata) {
152 /* The password cb is only invoked if OpenSSL decides the private
153 key is encrypted. So this path only occurs if it needs a password */
154 CRYPTOGRAPHY_PASSWORD_DATA *st = (CRYPTOGRAPHY_PASSWORD_DATA *)userdata;
155 st->called += 1;
156 st->maxsize = size;
157 if (st->length == 0) {
158 st->error = -1;
159 return 0;
160 } else if (st->length < size) {
161 memcpy(buf, st->password, st->length);
162 return st->length;
163 } else {
164 st->error = -2;
165 return 0;
166 }
167 }
168 """