__init__.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os.path
  16. from OpenSSL import SSL
  17. def get_test_cert_file():
  18. """get the path to the test cert"""
  19. # the cert file itself is made with:
  20. #
  21. # openssl req -x509 -newkey rsa:4096 -keyout server.pem -out server.pem -days 36500 \
  22. # -nodes -subj '/CN=testserv'
  23. return os.path.join(
  24. os.path.dirname(__file__),
  25. 'server.pem',
  26. )
  27. class ServerTLSContext(object):
  28. """A TLS Context which presents our test cert."""
  29. def __init__(self):
  30. self.filename = get_test_cert_file()
  31. def getContext(self):
  32. ctx = SSL.Context(SSL.TLSv1_METHOD)
  33. ctx.use_certificate_file(self.filename)
  34. ctx.use_privatekey_file(self.filename)
  35. return ctx