Skip to content
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

Adding cipher tag to the serverFromString #12137

Open
wants to merge 25 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e9308d3
Adding cipher tag to the serverFromString
KaviHarjani Apr 24, 2024
6e30f9f
Adding cipher tag to the serverFromString
KaviHarjani Apr 24, 2024
692e540
isort changes
KaviHarjani Apr 25, 2024
7796194
Update src/twisted/internet/endpoints.py
KaviHarjani Apr 25, 2024
a9f55ad
Fixing the test error
KaviHarjani Apr 25, 2024
1ecf280
Merge branch 'kaviharjani_12134' of github.com:KaviHarjani/twisted in…
KaviHarjani Apr 25, 2024
9889492
Test fix
KaviHarjani Apr 25, 2024
7f069f9
Removed TLS version as that is not being passed in the string anymore…
KaviHarjani Apr 25, 2024
6e3ca2e
Undo NEWS.rst update
KaviHarjani Apr 25, 2024
a0ee495
Update cipher description
KaviHarjani Apr 25, 2024
fb447c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 25, 2024
f991e5a
Update src/twisted/newsfragments/12134.feature
KaviHarjani Apr 27, 2024
fe8f46a
Cipher failing error message
KaviHarjani Apr 27, 2024
53b0d41
Limitations in test docstring
KaviHarjani Apr 27, 2024
acdf92c
Merge branch 'kaviharjani_12134' of github.com:KaviHarjani/twisted in…
KaviHarjani Apr 27, 2024
2f34e3f
Adding pyopenssl to GTK
KaviHarjani Apr 27, 2024
4255837
Adding pyopenssl in dependencies
KaviHarjani Apr 27, 2024
e458979
Using SSlError
KaviHarjani Apr 27, 2024
9a2a93b
Update test.yaml
KaviHarjani Apr 27, 2024
ce636b2
Test update
KaviHarjani Apr 27, 2024
746099b
Merge branch 'kaviharjani_12134' of github.com:KaviHarjani/twisted in…
KaviHarjani Apr 27, 2024
9fe9c61
Fixing the text in code
KaviHarjani Apr 28, 2024
d64992a
Raising SSL error except for generic one
KaviHarjani Apr 29, 2024
30078f9
Update src/twisted/internet/test/test_endpoints.py
KaviHarjani Apr 29, 2024
e94db6c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/twisted/internet/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ def _parseSSL(
privateKey="server.pem",
certKey=None,
sslmethod=None,
cipher=None,
interface="",
backlog=50,
extraCertChain=None,
Expand Down Expand Up @@ -1414,6 +1415,12 @@ def _parseSSL(
constant in C{OpenSSL.SSL}.
@type sslmethod: C{str}

@param cipher: A string containing a list of cipher values, formatted as comma-separated values,
These values can be obtained from the OpenSSL documentation at
https://www.openssl.org/docs/man1.0.2/man1/ciphers.html.
This parameter is optional and can be used to restrict the ciphers supported by your server.
@type cipher: C{str}

@param extraCertChain: The path of a file containing one or more
certificates in PEM format that establish the chain from a root CA to
the CA that signed your C{certKey}.
Expand Down Expand Up @@ -1466,6 +1473,13 @@ def _parseSSL(
dhParameters=dhParameters,
**kw,
)
if cipher:
cipherBytes = cipher.replace(",", ":").encode("ascii")
try:
cf.getContext().set_cipher_list(cipherBytes)
Copy link
Member

@adiroiban adiroiban Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that this will work.

By understanding is that context factories might not cache the context.

This is why, we might need to change the API to something like this ... but this will break all existing context factory implementation.

So I guess that this is somehow ok for now.

We can refactor once someone nees to used this code with a custom context factory.

            cf.setOpenSSLCipherList(cipherStr)

If we delegate the cipher list setting to the context factory, then we no longer need the TLS handshake tests for endpoints... we will just rely on the context factory tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adiroiban what do you suggest?
Should we go ahead with making changes to the API ?

except SSLError:
pass

return ((int(port), factory, cf), {"interface": interface, "backlog": int(backlog)})


Expand Down Expand Up @@ -1768,6 +1782,13 @@ def serverFromString(reactor, description):
file name (C{certKey}) isn't provided, the private key file is assumed to
contain the certificate as well.

For custom cipher list, you can make use of the 'cipher' keyword and a comma seperated
string as below::

serverFromString(
reactor, "ssl:443:privateKey=key.pem:certKey=crt.pem:cipher=ALL,!ADH,@STRENGTH,+RSA,-DSA,SHA1+DES"
)

You may escape colons in arguments with a backslash, which you will need to
use if you want to specify a full pathname argument on Windows::

Expand Down
38 changes: 38 additions & 0 deletions src/twisted/internet/test/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,44 @@ def test_sslNoTrailingNewlinePem(self):
ctx = server._sslContextFactory.getContext()
self.assertIsInstance(ctx, ContextType)

@skipIf(skipSSL, skipSSLReason)
def test_sslWithCustomCipher(self):
"""
A cipher list is supported, using the OpenSSL format.
The colon (:) from the OpenSSL format is replaced with a comma (,).

This test does not provides much functionality coverage as there is no API to retrieve the current configured ciper list.
Testing this functionality requires doing a TLS handshake.
"""
reactor = object()
server = endpoints.serverFromString(
reactor,
"ssl:1234:privateKey=%s:"
"certKey=%s:cipher=ALL,!ADH,@STRENGTH,+RSA,-DSA,SHA1+DES"
% (escapedPEMPathName, escapedPEMPathName),
)

self.assertIsInstance(server, endpoints.SSL4ServerEndpoint)
ctx = server._sslContextFactory.getContext()
self.assertIsInstance(ctx, ContextType)
KaviHarjani marked this conversation as resolved.
Show resolved Hide resolved

@skipIf(skipSSL, skipSSLReason)
def test_sslInvalidCustomCipher(self):
"""
A cipher list is supported, using the OpenSSL format.
The colon (:) from the OpenSSL format is replaced with a comma (,).
"""
reactor = object()

self.assertRaises(
endpoints.serverFromString(
reactor,
"ssl:1234:privateKey=%s:"
"certKey=%s:cipher=bla, blah"
% (escapedPEMPathName, escapedPEMPathName),
)
)

def test_unix(self):
"""
When passed a UNIX strports description, L{endpoint.serverFromString}
Expand Down
1 change: 1 addition & 0 deletions src/twisted/newsfragments/12134.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.internet.endpoint.serverFromString now supports defining a cipher list.