4 * Copyright (C) AB Strakt
5 * Copyright (C) Jean-Paul Calderone
6 * See LICENSE for details.
8 * Main file of the SSL sub module.
9 * See the file RATIONALE for a short explanation of why this module was written.
16 # include <sys/socket.h>
17 # include <netinet/in.h>
18 # if !(defined(__BEOS__) || defined(__CYGWIN__))
19 # include <netinet/tcp.h>
23 # include <wincrypt.h>
29 static char ssl_doc[] = "\n\
30 Main file of the SSL sub module.\n\
31 See the file RATIONALE for a short explanation of why this module was written.\n\
34 crypto_X509Obj* (*new_x509)(X509*, int);
35 crypto_X509NameObj* (*new_x509name)(X509_NAME*, int);
36 crypto_X509StoreObj* (*new_x509store)(X509_STORE*, int);
43 int _pyOpenSSL_tstate_key;
45 /* Exceptions defined by the SSL submodule */
46 PyObject *ssl_Error, /* Base class */
47 *ssl_ZeroReturnError, /* Used with SSL_get_error */
48 *ssl_WantReadError, /* ... */
49 *ssl_WantWriteError, /* ... */
50 *ssl_WantX509LookupError, /* ... */
51 *ssl_SysCallError; /* Uses (errno,errstr) */
54 /* Methods in the OpenSSL.SSL module */
55 static PyMethodDef ssl_methods[] = {
60 static struct PyModuleDef sslmodule = {
61 PyModuleDef_HEAD_INIT,
70 * Initialize SSL sub module
75 PyOpenSSL_MODINIT(SSL) {
78 static void *ssl_API[ssl_API_pointers];
79 PyObject *ssl_api_object;
83 new_x509 = crypto_X509_New;
84 new_x509name = crypto_X509Name_New;
85 new_x509store = crypto_X509Store_New;
88 HMODULE crypto = GetModuleHandle("crypto.pyd");
90 PyErr_SetString(PyExc_RuntimeError, "Unable to get crypto module");
91 PyOpenSSL_MODRETURN(NULL);
94 new_x509 = (crypto_X509Obj* (*)(X509*, int))GetProcAddress(crypto, "crypto_X509_New");
95 new_x509name = (crypto_X509NameObj* (*)(X509_NAME*, int))GetProcAddress(crypto, "crypto_X509Name_New");
96 new_x509store = (crypto_X509StoreObj* (*)(X509_STORE*, int))GetProcAddress(crypto, "crypto_X509Store_New");
98 new_x509 = crypto_X509_New;
99 new_x509name = crypto_X509Name_New;
100 new_x509store = crypto_X509Store_New;
105 ERR_load_SSL_strings();
108 module = PyModule_Create(&sslmodule);
110 module = Py_InitModule3("SSL", ssl_methods, ssl_doc);
112 if (module == NULL) {
113 PyOpenSSL_MODRETURN(NULL);
117 /* Initialize the C API pointer array */
118 ssl_API[ssl_Context_New_NUM] = (void *)ssl_Context_New;
119 ssl_API[ssl_Connection_New_NUM] = (void *)ssl_Connection_New;
120 ssl_api_object = PyCObject_FromVoidPtr((void *)ssl_API, NULL);
121 if (ssl_api_object != NULL)
122 PyModule_AddObject(module, "_C_API", ssl_api_object);
127 * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration,
128 * inserting OpenSSL.SSL.name into dict, derviving the exception from base.
130 #define ADD_EXCEPTION(_name, _base) \
132 ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\
133 if (ssl_##_name == NULL) \
135 if (PyModule_AddObject(module, #_name, ssl_##_name) != 0) \
139 ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
140 if (ssl_Error == NULL)
142 if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
145 ADD_EXCEPTION(ZeroReturnError, ssl_Error);
146 ADD_EXCEPTION(WantReadError, ssl_Error);
147 ADD_EXCEPTION(WantWriteError, ssl_Error);
148 ADD_EXCEPTION(WantX509LookupError, ssl_Error);
149 ADD_EXCEPTION(SysCallError, ssl_Error);
152 /* Method constants */
153 PyModule_AddIntConstant(module, "SSLv2_METHOD", ssl_SSLv2_METHOD);
154 PyModule_AddIntConstant(module, "SSLv3_METHOD", ssl_SSLv3_METHOD);
155 PyModule_AddIntConstant(module, "SSLv23_METHOD", ssl_SSLv23_METHOD);
156 PyModule_AddIntConstant(module, "TLSv1_METHOD", ssl_TLSv1_METHOD);
158 /* Verify constants */
159 PyModule_AddIntConstant(module, "VERIFY_NONE", SSL_VERIFY_NONE);
160 PyModule_AddIntConstant(module, "VERIFY_PEER", SSL_VERIFY_PEER);
161 PyModule_AddIntConstant(module, "VERIFY_FAIL_IF_NO_PEER_CERT",
162 SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
163 PyModule_AddIntConstant(module, "VERIFY_CLIENT_ONCE",
164 SSL_VERIFY_CLIENT_ONCE);
166 /* File type constants */
167 PyModule_AddIntConstant(module, "FILETYPE_PEM", SSL_FILETYPE_PEM);
168 PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1);
170 /* SSL option constants */
171 PyModule_AddIntConstant(module, "OP_SINGLE_DH_USE", SSL_OP_SINGLE_DH_USE);
172 PyModule_AddIntConstant(module, "OP_EPHEMERAL_RSA", SSL_OP_EPHEMERAL_RSA);
173 PyModule_AddIntConstant(module, "OP_NO_SSLv2", SSL_OP_NO_SSLv2);
174 PyModule_AddIntConstant(module, "OP_NO_SSLv3", SSL_OP_NO_SSLv3);
175 PyModule_AddIntConstant(module, "OP_NO_TLSv1", SSL_OP_NO_TLSv1);
177 /* More SSL option constants */
178 PyModule_AddIntConstant(module, "OP_MICROSOFT_SESS_ID_BUG", SSL_OP_MICROSOFT_SESS_ID_BUG);
179 PyModule_AddIntConstant(module, "OP_NETSCAPE_CHALLENGE_BUG", SSL_OP_NETSCAPE_CHALLENGE_BUG);
180 PyModule_AddIntConstant(module, "OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
181 PyModule_AddIntConstant(module, "OP_SSLREF2_REUSE_CERT_TYPE_BUG", SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
182 PyModule_AddIntConstant(module, "OP_MICROSOFT_BIG_SSLV3_BUFFER", SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
183 PyModule_AddIntConstant(module, "OP_MSIE_SSLV2_RSA_PADDING", SSL_OP_MSIE_SSLV2_RSA_PADDING);
184 PyModule_AddIntConstant(module, "OP_SSLEAY_080_CLIENT_DH_BUG", SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
185 PyModule_AddIntConstant(module, "OP_TLS_D5_BUG", SSL_OP_TLS_D5_BUG);
186 PyModule_AddIntConstant(module, "OP_TLS_BLOCK_PADDING_BUG", SSL_OP_TLS_BLOCK_PADDING_BUG);
187 PyModule_AddIntConstant(module, "OP_DONT_INSERT_EMPTY_FRAGMENTS", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
188 PyModule_AddIntConstant(module, "OP_ALL", SSL_OP_ALL);
189 PyModule_AddIntConstant(module, "OP_CIPHER_SERVER_PREFERENCE", SSL_OP_CIPHER_SERVER_PREFERENCE);
190 PyModule_AddIntConstant(module, "OP_TLS_ROLLBACK_BUG", SSL_OP_TLS_ROLLBACK_BUG);
191 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_1", SSL_OP_PKCS1_CHECK_1);
192 PyModule_AddIntConstant(module, "OP_PKCS1_CHECK_2", SSL_OP_PKCS1_CHECK_2);
193 PyModule_AddIntConstant(module, "OP_NETSCAPE_CA_DN_BUG", SSL_OP_NETSCAPE_CA_DN_BUG);
194 PyModule_AddIntConstant(module, "OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG", SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
196 /* DTLS related options. The first two of these were introduced in
197 * 2005, the third in 2007. To accomodate systems which are still using
198 * older versions, make them optional. */
199 #ifdef SSL_OP_NO_QUERY_MTU
200 PyModule_AddIntConstant(module, "OP_NO_QUERY_MTU", SSL_OP_NO_QUERY_MTU);
202 #ifdef SSL_OP_COOKIE_EXCHANGE
203 PyModule_AddIntConstant(module, "OP_COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE);
205 #ifdef SSL_OP_NO_TICKET
206 PyModule_AddIntConstant(module, "OP_NO_TICKET", SSL_OP_NO_TICKET);
209 /* For SSL_set_shutdown */
210 PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN);
211 PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN);
213 /* For set_info_callback */
214 PyModule_AddIntConstant(module, "SSL_ST_CONNECT", SSL_ST_CONNECT);
215 PyModule_AddIntConstant(module, "SSL_ST_ACCEPT", SSL_ST_ACCEPT);
216 PyModule_AddIntConstant(module, "SSL_ST_MASK", SSL_ST_MASK);
217 PyModule_AddIntConstant(module, "SSL_ST_INIT", SSL_ST_INIT);
218 PyModule_AddIntConstant(module, "SSL_ST_BEFORE", SSL_ST_BEFORE);
219 PyModule_AddIntConstant(module, "SSL_ST_OK", SSL_ST_OK);
220 PyModule_AddIntConstant(module, "SSL_ST_RENEGOTIATE", SSL_ST_RENEGOTIATE);
221 PyModule_AddIntConstant(module, "SSL_CB_LOOP", SSL_CB_LOOP);
222 PyModule_AddIntConstant(module, "SSL_CB_EXIT", SSL_CB_EXIT);
223 PyModule_AddIntConstant(module, "SSL_CB_READ", SSL_CB_READ);
224 PyModule_AddIntConstant(module, "SSL_CB_WRITE", SSL_CB_WRITE);
225 PyModule_AddIntConstant(module, "SSL_CB_ALERT", SSL_CB_ALERT);
226 PyModule_AddIntConstant(module, "SSL_CB_READ_ALERT", SSL_CB_READ_ALERT);
227 PyModule_AddIntConstant(module, "SSL_CB_WRITE_ALERT", SSL_CB_WRITE_ALERT);
228 PyModule_AddIntConstant(module, "SSL_CB_ACCEPT_LOOP", SSL_CB_ACCEPT_LOOP);
229 PyModule_AddIntConstant(module, "SSL_CB_ACCEPT_EXIT", SSL_CB_ACCEPT_EXIT);
230 PyModule_AddIntConstant(module, "SSL_CB_CONNECT_LOOP", SSL_CB_CONNECT_LOOP);
231 PyModule_AddIntConstant(module, "SSL_CB_CONNECT_EXIT", SSL_CB_CONNECT_EXIT);
232 PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_START", SSL_CB_HANDSHAKE_START);
233 PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_DONE", SSL_CB_HANDSHAKE_DONE);
235 if (!init_ssl_context(module))
237 if (!init_ssl_connection(module))
242 * Initialize this module's threading support structures.
244 _pyOpenSSL_tstate_key = PyThread_create_key();
247 PyOpenSSL_MODRETURN(module);
250 PyOpenSSL_MODRETURN(NULL);