New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ftplib: Add client-side SSL session resumption #63699
Comments
According to RFC4217 (Securing FTP with TLS, aka the FTPS spec), http://tools.ietf.org/html/rfc4217.html#section-10.2 " It is reasonable for the server to insist that the data connection Note: This has an important impact on client design, but allows It appears that vsftpd server implemented exactly that by enforcing the "SSL session reuse between the control and data connection". http://scarybeastsecurity.blogspot.com/2009/02/vsftpd-210-released.html Looking at the source of Python core library ftplib.py, there isn't any regard to the idea of SSL session reuse between data connection vs. control connection (correct me if I am wrong here. I've tried FTP_TLS.transfercmd(cmd[, rest])¶, didn't work). This issue is well documented on other FTP clients that supports FTPS, I.E. WinSCP: http://winscp.net/tracker/show_bug.cgi?id=668 See test log file attached. A vsftpd server with "require_ssl_reuse" set to true in vsftpd.conf would do the trick and can be reproduced. |
Interesting, I wasn't aware of this FTP(S) feature. |
Yuck. Is there a public FTP server available somewhere with this "feature"? |
The RFC is unhelpfully lousy. It's not enough to process a "522" error, since that can be triggered for different reasons. You also somehow have to interpret the error text to detect that session reuse is indeed mandated by the server. Regardless, to progress with this we would first need to implement client-side SSL session reuse, which necessitates a bunch of additional APIs (since which session is to be reused is a decision made by user code), and a new opaque type to carry SSL_SESSION objects... (see issue bpo-8106) |
Adding Python v2.7 as also exhibiting this behavior. Some people over on Stack Overflow have done some things to work around the issue via subclassing, but I'm not sure their solutions are "correct", so much as have useful side effects. (For example, when only the server has a key/cert and the client does not, how is that handled for reuse?) http://stackoverflow.com/questions/12164470/python-ftp-tls-connection-issue |
I encountered this problem recently and could not find a fix, so i tried fixing it myself. Note that the patch attached is my first contribution to cpython as well as the first time I used the C extension mechanism. Therefore I do not consider the patch polished enough to be just merged upstream. Maybe it helps in solving this issue. The attached patch is based on: |
Based on the proof-of-concept patch I submitted a few days ago I have built a more sophisticated patch. Please review it and let me know about necessary changes. |
This is supposed to be a new feature hence the patch should be targeted against Python 3.6, definitively not 2.7. |
I have re-targeted the patch for 3.6. It is not a 1 to 1 port of the prior one, but quite similar. |
Thanks for your patch. There might be a simpler way. By default a SSLContext only caches server sessions. You can enable client session caching with: SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT) This may be sufficient for FTP over TLS since both sockets are created from the same context. The new patch has a flaw. With the new SSLSession object a user could attempt to reuse a SSLSession with a different SSLContext. That's going to break OpenSSL. From SSL_set_session(3) NOTES |
Thanks for the heads up Christian I'll try enabling client session caching. If this does not work I'll try to adapt the patch to only allow session reusing within the same context. |
Even after enabling client cache one still has to call SSL_set_session. See documentation of SSL_CTX_set_session_cache_mode point SSL_SESS_CACHE_CLIENT. I started thinking about not exposing a SSL_SESSION object to the user but rather extending wrap_socket to take an already established socket as argument and use that socket's session object. This way I can ensure that both sockets share the same SSL context I am not really convinced by this idea myself, what do you think about this? Any better ideas? |
Here is my take on the SSLSession feature. The patch provides a SSLSession type, SSLSocket.session getter/setter and SSLSocket.session_reused getter. The setter makes sure that the session can only set for client sockets from the same SSLContext and before handshake. Tests and documentation need some improvements. https://github.com/tiran/cpython/commits/feature/openssl_session |
Performance improvements with session resumption is quite noticeable. I see between 17 and 21% improvement for 10 requests GET http://pypi.python.org/pypi in a simple benchmark: session resumption: 1.264sec |
Patch LGTM. But one thing is that every time it returns a new instance of SSL.Session. That means ssl_sock.session == ssl_sock.session will always return False right now. Is it useful to make it comparable? |
Xiang, good point! I have added richcompare to SSLSession (based on session id). My branch on github implements a couple more fixes and improvements. |
Note to future me: |
Session resumption is currently broken in OpenSSL 1.1.0, openssl/openssl#1550 |
This patch implements a workaround for OpenSSL 1.1.0. |
New changeset 6f2644738876 by Christian Heimes in branch 'default': |
I have committed the feature with rudimentary documentation. I will provide more documentation and an example before 3.6.0b2. |
With this code in place, ftplib should / could also be updated to support session resumption. This would fix bugs with connections to FTP servers that require session resumption [1], [2] In ftplib.FTP_TLS.ntransfercmd, just add a reference to the current session in the wrap_socket call (maybe make this an option to do session resumption or not; I don't know if it could break something) Proposed patch is attached. [1] http://stackoverflow.com/questions/14659154/ftpes-session-reuse-required |
It's now an ftplib issue. It's too late to land a new feature in 3.7 because we have reached feature freeze. |
Using vsftpd 3.0.3 with ssl_enable=YES and require_ssl_reuse=YES I get the following error when I try to upload a file with conn.storbinary(): FTP command: Client "192.168.178.115", "STOR something.bin" This also happens when SSL reuse is disabled on the server side. |
Excuse me, but why is this issue still open and unfixed? There are already proposed fixes and this issue has been around for nearly 7 years now. |
I have provided necessary infrastructure for TLS 1.2 session resumption in the ssl module a couple of releases ago. But nobody has taken ownership of this ftplib issue and provided a full fix with regression tests and documentation update. If you like to see ftplib enhanced to support session resumption, please provide a PR with regression tests. Demanding a fix won't accelerate work on the issue. |
@christian Heimes, you are absolutely right. I'm sorry if I came off rude. Actually, I just had a rough day at work with the end of it being the realisation that this missing fix would solve my day-filling issue from today. Furthermore, I've actually returned here, just to read through the whole thread again, hoping to find a way to contribute to it's resolution. |
Hi! Do we know if anyone is working on this? If not, maybe I can make this my first contribution to Python. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: