ticket48798_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import pytest
  7. import nss
  8. from lib389 import DirSrv, Entry, tools, tasks
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. from lib389.tasks import *
  13. from lib389.utils import *
  14. # Only works in py2.7
  15. # from subprocess import check_output
  16. from subprocess import Popen
  17. logging.getLogger(__name__).setLevel(logging.DEBUG)
  18. log = logging.getLogger(__name__)
  19. class TopologyStandalone(object):
  20. def __init__(self, standalone):
  21. standalone.open()
  22. self.standalone = standalone
  23. @pytest.fixture(scope="module")
  24. def topology(request):
  25. # Creating standalone instance ...
  26. standalone = DirSrv(verbose=False)
  27. args_instance[SER_HOST] = HOST_STANDALONE
  28. args_instance[SER_PORT] = PORT_STANDALONE
  29. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  30. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  31. args_standalone = args_instance.copy()
  32. standalone.allocate(args_standalone)
  33. instance_standalone = standalone.exists()
  34. if instance_standalone:
  35. standalone.delete()
  36. standalone.create()
  37. standalone.open()
  38. # Delete each instance in the end
  39. def fin():
  40. pass
  41. #standalone.delete()
  42. request.addfinalizer(fin)
  43. # Clear out the tmp dir
  44. #standalone.clearTmpDir(__file__)
  45. return TopologyStandalone(standalone)
  46. def check_socket_dh_param_size(hostname, port):
  47. ### You know why we have to do this?
  48. # Because TLS and SSL suck. Hard. They are impossible. It's all terrible, burn it all down.
  49. cmd = "echo quit | openssl s_client -connect {HOSTNAME}:{PORT} -msg -cipher DH | grep -A 1 ServerKeyExchange".format(
  50. HOSTNAME=hostname,
  51. PORT=port)
  52. #output = check_output(cmd, shell=True)
  53. p = Popen(cmd, shell=True, stdout=PIPE)
  54. (output, _) = p.communicate()
  55. dhheader = output.split('\n')[1]
  56. # Get rid of all the other whitespace.
  57. dhheader = dhheader.replace(' ', '')
  58. # Example is 0c00040b0100ffffffffffffffffadf8
  59. # We need the bits 0100 here. Which means 256 bytes aka 256 * 8, for 2048 bit.
  60. dhheader = dhheader[8:12]
  61. # make it an int, and times 8
  62. i = int(dhheader, 16) * 8
  63. return i
  64. def test_ticket48798(topology):
  65. """
  66. Test DH param sizes offered by DS.
  67. """
  68. # Create a CA
  69. # This is a trick. The nss db that ships with DS is broken fundamentally.
  70. ## THIS ASSUMES old nss format. SQLite will bite us!
  71. for f in ('key3.db', 'cert8.db', 'key4.db', 'cert9.db', 'secmod.db', 'pkcs11.txt'):
  72. try:
  73. os.remove("%s/%s" % (topology.standalone.confdir, f ))
  74. except:
  75. pass
  76. # Check if the db exists. Should be false.
  77. assert(topology.standalone.nss_ssl._db_exists() is False)
  78. # Create it. Should work.
  79. assert(topology.standalone.nss_ssl.reinit() is True)
  80. # Check if the db exists. Should be true
  81. assert(topology.standalone.nss_ssl._db_exists() is True)
  82. # Check if ca exists. Should be false.
  83. assert(topology.standalone.nss_ssl._rsa_ca_exists() is False)
  84. # Create it. Should work.
  85. assert(topology.standalone.nss_ssl.create_rsa_ca() is True)
  86. # Check if ca exists. Should be true
  87. assert(topology.standalone.nss_ssl._rsa_ca_exists() is True)
  88. # Check if we have a server cert / key. Should be false.
  89. assert(topology.standalone.nss_ssl._rsa_key_and_cert_exists() is False)
  90. # Create it. Should work.
  91. assert(topology.standalone.nss_ssl.create_rsa_key_and_cert() is True)
  92. # Check if server cert and key exist. Should be true.
  93. assert(topology.standalone.nss_ssl._rsa_key_and_cert_exists() is True)
  94. topology.standalone.config.enable_ssl(secport=DEFAULT_SECURE_PORT, secargs={'nsSSL3Ciphers': '+all'} )
  95. topology.standalone.restart(30)
  96. # Confirm that we have a connection, and that it has DH
  97. # Open a socket to the port.
  98. # Check the security settings.
  99. size = check_socket_dh_param_size(topology.standalone.host, DEFAULT_SECURE_PORT)
  100. assert(size == 2048)
  101. # Now toggle the settings.
  102. mod = [(ldap.MOD_REPLACE, 'allowWeakDHParam', 'on')]
  103. dn_enc = 'cn=encryption,cn=config'
  104. topology.standalone.modify_s(dn_enc, mod)
  105. topology.standalone.restart(30)
  106. # Check the DH params are less than 1024.
  107. size = check_socket_dh_param_size(topology.standalone.host, DEFAULT_SECURE_PORT)
  108. assert(size == 1024)
  109. log.info('Test complete')
  110. if __name__ == '__main__':
  111. # Run isolated
  112. # -s for DEBUG mode
  113. CURRENT_FILE = os.path.realpath(__file__)
  114. pytest.main("-s %s" % CURRENT_FILE)