| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2022 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import logging |
| 10 |
import pytest |
| 11 |
import ssl |
| 12 |
import os |
| 13 |
import re |
| 14 |
from test389.topologies import topology_st as topo |
| 15 |
from lib389.nss_ssl import NssSsl |
| 16 |
|
| 17 |
log = logging.getLogger(__name__) |
| 18 |
|
| 19 |
|
| 20 |
def test_tls_command_returns_error_text(topo): |
| 21 |
"""CLI commands that called certutil should return the error text from |
| 22 |
certutil when something goes wrong, and not the system error code number. |
| 23 |
|
| 24 |
:id: 7f0c28d0-6e13-4ca4-bec2-4586d56b73f6 |
| 25 |
:setup: Standalone Instance |
| 26 |
:steps: |
| 27 |
1. Issue invalid "generate key and cert" command, and error text is returned |
| 28 |
2. Issue invalid "delete cert" command, and error text is returned |
| 29 |
3. Issue invalid "import ca cert" command, and error text is returned |
| 30 |
4. Issue invalid "import server cert" command, and error text is returned |
| 31 |
5. Issue invalid "import key and server cert" command, and error text is returned |
| 32 |
:expectedresults: |
| 33 |
1. Success |
| 34 |
2. Success |
| 35 |
3. Success |
| 36 |
4. Success |
| 37 |
5. Success |
| 38 |
""" |
| 39 |
|
| 40 |
# dsctl localhost tls generate-server-cert-csr -s "bad" |
| 41 |
tls = NssSsl(dirsrv=topo.standalone) |
| 42 |
try: |
| 43 |
tls.create_rsa_key_and_csr([], "bad") |
| 44 |
assert False |
| 45 |
except ValueError as e: |
| 46 |
assert '255' not in str(e) |
| 47 |
assert 'improperly formatted name' in str(e) |
| 48 |
|
| 49 |
# dsctl localhost tls remove-cert |
| 50 |
try: |
| 51 |
tls.del_cert("bad") |
| 52 |
assert False |
| 53 |
except ValueError as e: |
| 54 |
assert '255' not in str(e) |
| 55 |
assert 'could not find certificate named' in str(e) |
| 56 |
|
| 57 |
# dsctl localhost tls import-ca |
| 58 |
try: |
| 59 |
invalid_file = topo.standalone.confdir + '/dse.ldif' |
| 60 |
tls.add_cert(nickname="bad", cert_file=invalid_file) |
| 61 |
assert False |
| 62 |
except ValueError as e: |
| 63 |
assert '255' not in str(e) |
| 64 |
assert 'Unable to load certificate' in str(e) |
| 65 |
|
| 66 |
# dsctl localhost tls import-server-cert |
| 67 |
try: |
| 68 |
invalid_file = topo.standalone.confdir + '/dse.ldif' |
| 69 |
tls.import_rsa_crt(crt=invalid_file) |
| 70 |
assert False |
| 71 |
except ValueError as e: |
| 72 |
assert '255' not in str(e) |
| 73 |
assert 'error converting ascii to binary' in str(e) |
| 74 |
|
| 75 |
# dsctl localhost tls import-server-key-cert |
| 76 |
try: |
| 77 |
invalid_file = topo.standalone.confdir + '/dse.ldif' |
| 78 |
tls.add_server_key_and_cert(invalid_file, invalid_file) |
| 79 |
assert False |
| 80 |
except ValueError as e: |
| 81 |
assert '255' not in str(e) |
| 82 |
if 'OpenSSL 3' in ssl.OPENSSL_VERSION: |
| 83 |
error_message = r"Could not (read|find) private key from" |
| 84 |
else: |
| 85 |
error_message = r"unable to load private key" |
| 86 |
assert re.search(error_message, str(e)) |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == '__main__': |
| 90 |
# Run isolated |
| 91 |
# -s for DEBUG mode |
| 92 |
CURRENT_FILE = os.path.realpath(__file__) |
| 93 |
pytest.main(["-s", CURRENT_FILE]) |
| 94 |
|