debughelpers.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Debug helpers */
  2. #ifndef SSL3_MT_CHANGE_CIPHER_SPEC
  3. /* Dummy message type for handling CCS like a normal handshake message
  4. * not defined in OpenSSL 1.0.2
  5. */
  6. #define SSL3_MT_CHANGE_CIPHER_SPEC 0x0101
  7. #endif
  8. static void
  9. _PySSL_msg_callback(int write_p, int version, int content_type,
  10. const void *buf, size_t len, SSL *ssl, void *arg)
  11. {
  12. const char *cbuf = (const char *)buf;
  13. PyGILState_STATE threadstate;
  14. PyObject *res = NULL;
  15. PySSLSocket *ssl_obj = NULL; /* ssl._SSLSocket, borrowed ref */
  16. PyObject *ssl_socket = NULL; /* ssl.SSLSocket or ssl.SSLObject */
  17. int msg_type;
  18. threadstate = PyGILState_Ensure();
  19. ssl_obj = (PySSLSocket *)SSL_get_app_data(ssl);
  20. assert(Py_IS_TYPE(ssl_obj, get_state_sock(ssl_obj)->PySSLSocket_Type));
  21. if (ssl_obj->ctx->msg_cb == NULL) {
  22. PyGILState_Release(threadstate);
  23. return;
  24. }
  25. if (ssl_obj->owner)
  26. ssl_socket = PyWeakref_GetObject(ssl_obj->owner);
  27. else if (ssl_obj->Socket)
  28. ssl_socket = PyWeakref_GetObject(ssl_obj->Socket);
  29. else
  30. ssl_socket = (PyObject *)ssl_obj;
  31. Py_INCREF(ssl_socket);
  32. /* assume that OpenSSL verifies all payload and buf len is of sufficient
  33. length */
  34. switch(content_type) {
  35. case SSL3_RT_CHANGE_CIPHER_SPEC:
  36. msg_type = SSL3_MT_CHANGE_CIPHER_SPEC;
  37. break;
  38. case SSL3_RT_ALERT:
  39. /* byte 0: level */
  40. /* byte 1: alert type */
  41. msg_type = (int)cbuf[1];
  42. break;
  43. case SSL3_RT_HANDSHAKE:
  44. msg_type = (int)cbuf[0];
  45. break;
  46. #ifdef SSL3_RT_HEADER
  47. case SSL3_RT_HEADER:
  48. /* frame header encodes version in bytes 1..2 */
  49. version = cbuf[1] << 8 | cbuf[2];
  50. msg_type = (int)cbuf[0];
  51. break;
  52. #endif
  53. #ifdef SSL3_RT_INNER_CONTENT_TYPE
  54. case SSL3_RT_INNER_CONTENT_TYPE:
  55. msg_type = (int)cbuf[0];
  56. break;
  57. #endif
  58. default:
  59. /* never SSL3_RT_APPLICATION_DATA */
  60. msg_type = -1;
  61. break;
  62. }
  63. res = PyObject_CallFunction(
  64. ssl_obj->ctx->msg_cb, "Osiiiy#",
  65. ssl_socket, write_p ? "write" : "read",
  66. version, content_type, msg_type,
  67. buf, len
  68. );
  69. if (res == NULL) {
  70. ssl_obj->exc = PyErr_GetRaisedException();
  71. } else {
  72. Py_DECREF(res);
  73. }
  74. Py_XDECREF(ssl_socket);
  75. PyGILState_Release(threadstate);
  76. }
  77. static PyObject *
  78. _PySSLContext_get_msg_callback(PySSLContext *self, void *c) {
  79. if (self->msg_cb != NULL) {
  80. return Py_NewRef(self->msg_cb);
  81. } else {
  82. Py_RETURN_NONE;
  83. }
  84. }
  85. static int
  86. _PySSLContext_set_msg_callback(PySSLContext *self, PyObject *arg, void *c) {
  87. Py_CLEAR(self->msg_cb);
  88. if (arg == Py_None) {
  89. SSL_CTX_set_msg_callback(self->ctx, NULL);
  90. }
  91. else {
  92. if (!PyCallable_Check(arg)) {
  93. SSL_CTX_set_msg_callback(self->ctx, NULL);
  94. PyErr_SetString(PyExc_TypeError,
  95. "not a callable object");
  96. return -1;
  97. }
  98. self->msg_cb = Py_NewRef(arg);
  99. SSL_CTX_set_msg_callback(self->ctx, _PySSL_msg_callback);
  100. }
  101. return 0;
  102. }
  103. static void
  104. _PySSL_keylog_callback(const SSL *ssl, const char *line)
  105. {
  106. PyGILState_STATE threadstate;
  107. PySSLSocket *ssl_obj = NULL; /* ssl._SSLSocket, borrowed ref */
  108. int res, e;
  109. threadstate = PyGILState_Ensure();
  110. ssl_obj = (PySSLSocket *)SSL_get_app_data(ssl);
  111. assert(Py_IS_TYPE(ssl_obj, get_state_sock(ssl_obj)->PySSLSocket_Type));
  112. PyThread_type_lock lock = get_state_sock(ssl_obj)->keylog_lock;
  113. assert(lock != NULL);
  114. if (ssl_obj->ctx->keylog_bio == NULL) {
  115. return;
  116. }
  117. /*
  118. * The lock is neither released on exit nor on fork(). The lock is
  119. * also shared between all SSLContexts although contexts may write to
  120. * their own files. IMHO that's good enough for a non-performance
  121. * critical debug helper.
  122. */
  123. PySSL_BEGIN_ALLOW_THREADS
  124. PyThread_acquire_lock(lock, 1);
  125. res = BIO_printf(ssl_obj->ctx->keylog_bio, "%s\n", line);
  126. e = errno;
  127. (void)BIO_flush(ssl_obj->ctx->keylog_bio);
  128. PyThread_release_lock(lock);
  129. PySSL_END_ALLOW_THREADS
  130. if (res == -1) {
  131. errno = e;
  132. PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
  133. ssl_obj->ctx->keylog_filename);
  134. ssl_obj->exc = PyErr_GetRaisedException();
  135. }
  136. PyGILState_Release(threadstate);
  137. }
  138. static PyObject *
  139. _PySSLContext_get_keylog_filename(PySSLContext *self, void *c) {
  140. if (self->keylog_filename != NULL) {
  141. return Py_NewRef(self->keylog_filename);
  142. } else {
  143. Py_RETURN_NONE;
  144. }
  145. }
  146. static int
  147. _PySSLContext_set_keylog_filename(PySSLContext *self, PyObject *arg, void *c) {
  148. FILE *fp;
  149. /* Reset variables and callback first */
  150. SSL_CTX_set_keylog_callback(self->ctx, NULL);
  151. Py_CLEAR(self->keylog_filename);
  152. if (self->keylog_bio != NULL) {
  153. BIO *bio = self->keylog_bio;
  154. self->keylog_bio = NULL;
  155. PySSL_BEGIN_ALLOW_THREADS
  156. BIO_free_all(bio);
  157. PySSL_END_ALLOW_THREADS
  158. }
  159. if (arg == Py_None) {
  160. /* None disables the callback */
  161. return 0;
  162. }
  163. /* _Py_fopen_obj() also checks that arg is of proper type. */
  164. fp = _Py_fopen_obj(arg, "a" PY_STDIOTEXTMODE);
  165. if (fp == NULL)
  166. return -1;
  167. self->keylog_bio = BIO_new_fp(fp, BIO_CLOSE | BIO_FP_TEXT);
  168. if (self->keylog_bio == NULL) {
  169. PyErr_SetString(get_state_ctx(self)->PySSLErrorObject,
  170. "Can't malloc memory for keylog file");
  171. return -1;
  172. }
  173. self->keylog_filename = Py_NewRef(arg);
  174. /* Write a header for seekable, empty files (this excludes pipes). */
  175. PySSL_BEGIN_ALLOW_THREADS
  176. if (BIO_tell(self->keylog_bio) == 0) {
  177. BIO_puts(self->keylog_bio,
  178. "# TLS secrets log file, generated by OpenSSL / Python\n");
  179. (void)BIO_flush(self->keylog_bio);
  180. }
  181. PySSL_END_ALLOW_THREADS
  182. SSL_CTX_set_keylog_callback(self->ctx, _PySSL_keylog_callback);
  183. return 0;
  184. }