-
-
Notifications
You must be signed in to change notification settings - Fork 276
Add support for TLS/SSL mutual authentication #396
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2941cd0
Add support for TLS/SSL mutual authentication
bb52b92
Fix indentation errors in handlers.py
933257d
Update 'print' statements to use logger
128b61f
Remove print/log statements
c3675b9
Clean up client-certfile support
aa41ce3
Updates per comments
0928806
Flake8 fixes
099630f
Small flake8 fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| -----BEGIN CERTIFICATE----- | ||
| MIIDqzCCApOgAwIBAgIBADANBgkqhkiG9w0BAQsFADBwMQswCQYDVQQGEwJVUzEO | ||
| MAwGA1UECAwFVGV4YXMxEDAOBgNVBAcMB0hvdXN0b24xDDAKBgNVBAoMA1NMQjEM | ||
| MAoGA1UECwwDU1dUMQwwCgYDVQQDDANzd3QxFTATBgkqhkiG9w0BCQEWBmJwbG9z | ||
| dDAeFw0xNjA4MjMxMzQyMDZaFw0xNzA4MjMxMzQyMDZaMHAxCzAJBgNVBAYTAlVT | ||
| MQ4wDAYDVQQIDAVUZXhhczEQMA4GA1UEBwwHSG91c3RvbjEMMAoGA1UECgwDU0xC | ||
| MQwwCgYDVQQLDANTV1QxDDAKBgNVBAMMA3N3dDEVMBMGCSqGSIb3DQEJARYGYnBs | ||
| b3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArNnEnnUGpXLqnTTx | ||
| 3td4OWQoKFBppifL6+r5y839CWT6GR3Xj5OlcJPXMKBO40H0StHOctshPL0/ZZeC | ||
| sNHT6O6c47nBc/pw3YX4GJjLJno0k1+xTSvmk+yhTF/i1ThDIq2YrlGWXHyo/jOe | ||
| gJc0T3L2u1Ivx+iOEAIP9uqBpAi3rhfZKBvVdDXb5J0TqouXt4jx5l8Fq577D9y4 | ||
| W61nmj7FlquxClhGIgsNbMtlBIMkALNLq3kY+TqYatjRy6aS6mb55TfObjP+IOgz | ||
| 8Jln937hb89eJopirwKMzD1EnJgBamMPNJjIhDQlHklMwkgynIKnSJghbjKBusaE | ||
| 2xZEPQIDAQABo1AwTjAdBgNVHQ4EFgQU2hMnpneH1sZ79iWoBLue1+7f4NwwHwYD | ||
| VR0jBBgwFoAU2hMnpneH1sZ79iWoBLue1+7f4NwwDAYDVR0TBAUwAwEB/zANBgkq | ||
| hkiG9w0BAQsFAAOCAQEAdmPairr/lt63J3dfTSFNJTytvV1WW7Ak8NwH1hdheaYy | ||
| Tx9ffjRIZv9WyHEWb/1YCkKo08LqgnVh07HfW0JY1hqD0SwoHtPexTIgBnOsKvCr | ||
| q1gQjFuDg2wVV7cecYPQFYv9jweIe62OCapKl8PjmXii+qnxY/Qbbyx9bGYbR1k4 | ||
| KJm073WwiqXXCS1JgOj9WH3I1Qa2Ptb6RO+Woy7ItA5ftQSp4EMTwhygOK0j0w9V | ||
| 11MAPUtZ8/rTiD17HHVzKfbNmx4E6dtBV9E/gn464lrdNhxEaeN2wW42FU+CzeVa | ||
| /aYQbQTSXpI5tX7QTAcQrMAqp75EBMdYj5+9bRXIvw== | ||
| -----END CERTIFICATE----- | ||
| -----BEGIN PRIV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1XC+Dwiw75Dsv/nqhekFqaQczYZlUrKa+mkdofV | ||
| i8DbXHies6qTwvYUIorhaiAoY+iRwJ/6d20oep+3fDSPPcVj7PakZpCK0sAesWX7 | ||
| 09muN0vLALcCgYAKr0iPkHQFEX3MlJdhvX417yBwwFn6ECK3I3NmNrX/4f1juJ8Y | ||
| 1z9jwdMNv+oTQkpKv5rZCpWZVkIkVPEhQG38Qsg0hLDEiBvsbj0zv+ahqAEW5AE0 | ||
| tnSA4k0Nhneq15/d6pnoROMrZk/kr6MQpFvGgn3CKHtjRQunwsTY4ELyeQKBgQCN | ||
| zlNGqvJmOhs5msc5Dly4hMncv7DahUXQrJtWkHTZajJgxE3ncQxoIdgHMF2iE0w8 | ||
| +V7NNTtxtxSTyPzkBEbMc9pEfvNsQ3xo+XvmOV34ebqHml/UF+iEfJQOVHXCOMiV | ||
| Zc0bTMvB0L3jrNiEzXV4X8V2Ytn+X9LavCxC4ta9lQKBgHTQNDz+qdiLoeX68HKp | ||
| b5jd1H7nozWdcbcAO5tKNbSZvnY05QCjC+WkuIeUkKc2zcctIy306/iFRApguElm | ||
| z8sm8NnJIkJeRx2XmEE5Lcn64se3ml7qVlcFaYrVW8hDrrvlUAwzu+ZoPVD3DSiQ | ||
| EOnPOa2mEwR9YPyPaKdq+fkt | ||
| -----END PRIVATE KEY----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright (C) 2007-2016 Giampaolo Rodola' <g.rodola@gmail.com>. | ||
| # Use of this source code is governed by MIT license that can be | ||
| # found in the LICENSE file. | ||
| # | ||
| # | ||
| # Does not follow naming convention of other tests because this | ||
| # CANNOT be run in the same test suite with test_functional_ssl. | ||
| # The test parallelism causes SSL errors when there should be none | ||
| # Please run these tests separately | ||
|
|
||
| import ftplib | ||
| import os | ||
| import sys | ||
|
|
||
| import OpenSSL # requires "pip install pyopenssl" | ||
|
|
||
| from pyftpdlib.handlers import TLS_FTPHandler | ||
| from pyftpdlib.test import configure_logging | ||
| from pyftpdlib.test import remove_test_files | ||
| from pyftpdlib.test import ThreadedTestFTPd | ||
| from pyftpdlib.test import TIMEOUT | ||
| from pyftpdlib.test import unittest | ||
| from pyftpdlib.test import VERBOSITY | ||
| from _ssl import SSLError | ||
|
|
||
|
|
||
| FTPS_SUPPORT = hasattr(ftplib, 'FTP_TLS') | ||
| if sys.version_info < (2, 7): | ||
| FTPS_UNSUPPORT_REASON = "requires python 2.7+" | ||
| else: | ||
| FTPS_UNSUPPORT_REASON = "FTPS test skipped" | ||
|
|
||
| CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
| 'keycert.pem')) | ||
| CLIENT_CERTFILE = os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
| 'clientcert.pem')) | ||
|
|
||
| del OpenSSL | ||
|
|
||
|
|
||
| if FTPS_SUPPORT: | ||
| class FTPSClient(ftplib.FTP_TLS): | ||
| """A modified version of ftplib.FTP_TLS class which implicitly | ||
| secure the data connection after login(). | ||
| """ | ||
|
|
||
| def login(self, *args, **kwargs): | ||
| ftplib.FTP_TLS.login(self, *args, **kwargs) | ||
| self.prot_p() | ||
|
|
||
| class FTPSServerAuth(ThreadedTestFTPd): | ||
| """A threaded FTPS server that forces client certificate | ||
| authentication used for functional testing. | ||
| """ | ||
| handler = TLS_FTPHandler | ||
| handler.certfile = CERTFILE | ||
| handler.client_certfile = CLIENT_CERTFILE | ||
|
|
||
|
|
||
| # ===================================================================== | ||
| # dedicated FTPS tests with client authentication | ||
| # ===================================================================== | ||
|
|
||
|
|
||
| @unittest.skipUnless(FTPS_SUPPORT, FTPS_UNSUPPORT_REASON) | ||
| class TestFTPS(unittest.TestCase): | ||
| """Specific tests for TLS_FTPHandler class.""" | ||
|
|
||
| def setUp(self): | ||
| self.server = FTPSServerAuth() | ||
| self.server.start() | ||
|
|
||
| def tearDown(self): | ||
| self.client.close() | ||
| self.server.stop() | ||
|
|
||
| def assertRaisesWithMsg(self, excClass, msg, callableObj, *args, **kwargs): | ||
| try: | ||
| callableObj(*args, **kwargs) | ||
| except excClass as err: | ||
| if str(err) == msg: | ||
| return | ||
| raise self.failureException("%s != %s" % (str(err), msg)) | ||
| else: | ||
| if hasattr(excClass, '__name__'): | ||
| excName = excClass.__name__ | ||
| else: | ||
| excName = str(excClass) | ||
| raise self.failureException("%s not raised" % excName) | ||
|
|
||
| def test_auth_client_cert(self): | ||
| self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CLIENT_CERTFILE) | ||
| self.client.connect(self.server.host, self.server.port) | ||
| # secured | ||
| try: | ||
| self.client.login() | ||
| except Exception: | ||
| self.fail("login with certificate should work") | ||
|
|
||
| def test_auth_client_nocert(self): | ||
| self.client = ftplib.FTP_TLS(timeout=TIMEOUT) | ||
| self.client.connect(self.server.host, self.server.port) | ||
| try: | ||
| self.client.login() | ||
| except SSLError as e: | ||
| # client should not be able to log in | ||
| if "SSLV3_ALERT_HANDSHAKE_FAILURE" in e.reason: | ||
| pass | ||
| else: | ||
| self.fail("Incorrect SSL error with" + | ||
| " missing client certificate") | ||
| else: | ||
| self.fail("Client able to log in with no certificate") | ||
|
|
||
| def test_auth_client_badcert(self): | ||
| self.client = ftplib.FTP_TLS(timeout=TIMEOUT, certfile=CERTFILE) | ||
| self.client.connect(self.server.host, self.server.port) | ||
| try: | ||
| self.client.login() | ||
| except Exception as e: | ||
| # client should not be able to log in | ||
| if "TLSV1_ALERT_UNKNOWN_CA" in e.reason: | ||
| pass | ||
| else: | ||
| self.fail("Incorrect SSL error with bad client certificate") | ||
| else: | ||
| self.fail("Client able to log in with bad certificate") | ||
|
|
||
| configure_logging() | ||
| remove_test_files() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main(verbosity=VERBOSITY) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is called when a client connects, as I suppose, then this should be a
classmethod.