Initial import to Tizen
[profile/ivi/python-pyOpenSSL.git] / OpenSSL / ssl / ssl.c
1 /*
2  * ssl.c
3  *
4  * Copyright (C) AB Strakt
5  * Copyright (C) Jean-Paul Calderone
6  * See LICENSE for details.
7  *
8  * Main file of the SSL sub module.
9  * See the file RATIONALE for a short explanation of why this module was written.
10  *
11  * Reviewed 2001-07-23
12  */
13 #include <Python.h>
14
15 #ifndef MS_WINDOWS
16 #  include <sys/socket.h>
17 #  include <netinet/in.h>
18 #  if !(defined(__BEOS__) || defined(__CYGWIN__))
19 #    include <netinet/tcp.h>
20 #  endif
21 #else
22 #  include <winsock.h>
23 #  include <wincrypt.h>
24 #endif
25
26 #define SSL_MODULE
27 #include "ssl.h"
28
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\
32 ";
33
34 crypto_X509Obj* (*new_x509)(X509*, int);
35 crypto_X509NameObj* (*new_x509name)(X509_NAME*, int);
36 crypto_X509StoreObj* (*new_x509store)(X509_STORE*, int);
37
38
39 #ifndef PY3
40 void **crypto_API;
41 #endif
42
43 int _pyOpenSSL_tstate_key;
44
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)     */
52
53
54 /* Methods in the OpenSSL.SSL module */
55 static PyMethodDef ssl_methods[] = {
56     { NULL, NULL }
57 };
58
59 #ifdef PY3
60 static struct PyModuleDef sslmodule = {
61     PyModuleDef_HEAD_INIT,
62     "SSL",
63     ssl_doc,
64     -1,
65     ssl_methods
66 };
67 #endif
68
69 /*
70  * Initialize SSL sub module
71  *
72  * Arguments: None
73  * Returns:   None
74  */
75 PyOpenSSL_MODINIT(SSL) {
76     PyObject *module;
77 #ifndef PY3
78     static void *ssl_API[ssl_API_pointers];
79     PyObject *ssl_api_object;
80
81     import_crypto();
82
83     new_x509 = crypto_X509_New;
84     new_x509name = crypto_X509Name_New;
85     new_x509store = crypto_X509Store_New;
86 #else
87 #   ifdef _WIN32
88     HMODULE crypto = GetModuleHandle("crypto.pyd");
89     if (crypto == NULL) {
90         PyErr_SetString(PyExc_RuntimeError, "Unable to get crypto module");
91         PyOpenSSL_MODRETURN(NULL);
92     }
93
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");
97 #   else
98     new_x509 = crypto_X509_New;
99     new_x509name = crypto_X509Name_New;
100     new_x509store = crypto_X509Store_New;
101 #   endif
102 #endif
103
104     SSL_library_init();
105     ERR_load_SSL_strings();
106
107 #ifdef PY3
108     module = PyModule_Create(&sslmodule);
109 #else
110     module = Py_InitModule3("SSL", ssl_methods, ssl_doc);
111 #endif
112     if (module == NULL) {
113         PyOpenSSL_MODRETURN(NULL);
114     }
115
116 #ifndef PY3
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);
123 #endif
124
125     /* Exceptions */
126 /*
127  * ADD_EXCEPTION(dict,name,base) expands to a correct Exception declaration,
128  * inserting OpenSSL.SSL.name into dict, derviving the exception from base.
129  */
130 #define ADD_EXCEPTION(_name, _base)                                    \
131 do {                                                                          \
132     ssl_##_name = PyErr_NewException("OpenSSL.SSL."#_name, _base, NULL);\
133     if (ssl_##_name == NULL)                                            \
134         goto error;                                                           \
135     if (PyModule_AddObject(module, #_name, ssl_##_name) != 0)           \
136         goto error;                                                           \
137 } while (0)
138
139     ssl_Error = PyErr_NewException("OpenSSL.SSL.Error", NULL, NULL);
140     if (ssl_Error == NULL)
141         goto error;
142     if (PyModule_AddObject(module, "Error", ssl_Error) != 0)
143         goto error;
144
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);
150 #undef ADD_EXCEPTION
151
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);
157
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);
165
166     /* File type constants */
167     PyModule_AddIntConstant(module, "FILETYPE_PEM",  SSL_FILETYPE_PEM);
168     PyModule_AddIntConstant(module, "FILETYPE_ASN1", SSL_FILETYPE_ASN1);
169
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);
176
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);
195
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);
201 #endif
202 #ifdef SSL_OP_COOKIE_EXCHANGE
203     PyModule_AddIntConstant(module, "OP_COOKIE_EXCHANGE", SSL_OP_COOKIE_EXCHANGE);
204 #endif
205 #ifdef SSL_OP_NO_TICKET
206     PyModule_AddIntConstant(module, "OP_NO_TICKET", SSL_OP_NO_TICKET);
207 #endif
208
209     /* For SSL_set_shutdown */
210     PyModule_AddIntConstant(module, "SENT_SHUTDOWN", SSL_SENT_SHUTDOWN);
211     PyModule_AddIntConstant(module, "RECEIVED_SHUTDOWN", SSL_RECEIVED_SHUTDOWN);
212
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);
234
235     if (!init_ssl_context(module))
236         goto error;
237     if (!init_ssl_connection(module))
238         goto error;
239
240 #ifdef WITH_THREAD
241     /*
242      * Initialize this module's threading support structures.
243      */
244     _pyOpenSSL_tstate_key = PyThread_create_key();
245 #endif
246
247     PyOpenSSL_MODRETURN(module);
248
249 error:
250     PyOpenSSL_MODRETURN(NULL);
251     ;
252 }