|
| 1 | +"""Tests for wolfssl crl (converted from CRL-verify-test.sh).""" |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import unittest |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 8 | +from wolfclu_test import WOLFSSL_BIN, CERTS_DIR, run_wolfssl |
| 9 | + |
| 10 | + |
| 11 | +def _has_crl(): |
| 12 | + """Check whether CRL support is compiled in.""" |
| 13 | + r = run_wolfssl("crl", "-CAfile", |
| 14 | + os.path.join(CERTS_DIR, "ca-cert.pem"), |
| 15 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 16 | + combined = r.stdout + r.stderr |
| 17 | + return "recompile wolfSSL with CRL support" not in combined |
| 18 | + |
| 19 | + |
| 20 | +def _has_crl_text(): |
| 21 | + """Check whether CRL -text is available (not just 'print not available').""" |
| 22 | + r = run_wolfssl("crl", "-in", os.path.join(CERTS_DIR, "crl.pem"), |
| 23 | + "-text") |
| 24 | + combined = r.stdout + r.stderr |
| 25 | + # If it says "not available", the feature is missing |
| 26 | + return "CRL print not available in version of wolfSSL" not in combined |
| 27 | + |
| 28 | + |
| 29 | +def _cleanup(*files): |
| 30 | + for f in files: |
| 31 | + if os.path.exists(f): |
| 32 | + os.remove(f) |
| 33 | + |
| 34 | + |
| 35 | +class TestCRLVerify(unittest.TestCase): |
| 36 | + """CRL verification tests.""" |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def setUpClass(cls): |
| 40 | + cls.have_crl = _has_crl() |
| 41 | + if not cls.have_crl: |
| 42 | + raise unittest.SkipTest("CRL not compiled into wolfSSL") |
| 43 | + |
| 44 | + def _clean(self, *files): |
| 45 | + for f in files: |
| 46 | + self.addCleanup(lambda p=f: _cleanup(p)) |
| 47 | + |
| 48 | + def test_crl_print(self): |
| 49 | + """CRL output should contain BEGIN marker.""" |
| 50 | + r = run_wolfssl("crl", "-CAfile", |
| 51 | + os.path.join(CERTS_DIR, "ca-cert.pem"), |
| 52 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 53 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 54 | + self.assertIn("BEGIN", r.stdout) |
| 55 | + |
| 56 | + def test_crl_noout(self): |
| 57 | + """CRL -noout should not print the CRL PEM.""" |
| 58 | + r = run_wolfssl("crl", "-noout", "-CAfile", |
| 59 | + os.path.join(CERTS_DIR, "ca-cert.pem"), |
| 60 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 61 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 62 | + self.assertNotIn("BEGIN X509 CRL", r.stdout) |
| 63 | + |
| 64 | + def test_crl_der_parse_cert_fails(self): |
| 65 | + """Parsing a certificate as CRL (DER) should fail.""" |
| 66 | + r = run_wolfssl("crl", "-inform", "DER", "-outform", "PEM", |
| 67 | + "-in", os.path.join(CERTS_DIR, "ca-cert.der")) |
| 68 | + self.assertNotEqual(r.returncode, 0) |
| 69 | + |
| 70 | + def test_crl_verify_with_wrong_ca(self): |
| 71 | + """CRL verification with a non-CA cert should fail.""" |
| 72 | + client_cert = "test_crl_client.pem" |
| 73 | + self._clean(client_cert) |
| 74 | + r = run_wolfssl("req", "-new", "-days", "3650", |
| 75 | + "-key", os.path.join(CERTS_DIR, "server-key.pem"), |
| 76 | + "-subj", |
| 77 | + "/O=wolfSSL/C=US/ST=WA/L=Seattle/CN=wolfSSL/OU=org-unit", |
| 78 | + "-out", client_cert, "-x509") |
| 79 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 80 | + |
| 81 | + r = run_wolfssl("crl", "-noout", "-CAfile", client_cert, |
| 82 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 83 | + self.assertNotEqual(r.returncode, 0) |
| 84 | + |
| 85 | + def test_crl_missing_cafile_fails(self): |
| 86 | + """CRL with nonexistent CAfile should fail.""" |
| 87 | + r = run_wolfssl("crl", "-noout", "-CAfile", |
| 88 | + os.path.join(CERTS_DIR, "ca-cer.pem"), |
| 89 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 90 | + self.assertNotEqual(r.returncode, 0) |
| 91 | + |
| 92 | + def test_crl_missing_input_fails(self): |
| 93 | + """CRL with nonexistent input file should fail.""" |
| 94 | + r = run_wolfssl("crl", "-noout", "-CAfile", |
| 95 | + os.path.join(CERTS_DIR, "ca-cert.pem"), |
| 96 | + "-in", os.path.join(CERTS_DIR, "cl.pem")) |
| 97 | + self.assertNotEqual(r.returncode, 0) |
| 98 | + |
| 99 | + def test_crl_verify_wrong_issuer_fails(self): |
| 100 | + """CRL verification with wrong issuer cert should fail.""" |
| 101 | + r = run_wolfssl("crl", "-noout", "-CAfile", |
| 102 | + os.path.join(CERTS_DIR, "client-int-cert.pem"), |
| 103 | + "-in", os.path.join(CERTS_DIR, "crl.pem")) |
| 104 | + self.assertNotEqual(r.returncode, 0) |
| 105 | + |
| 106 | + def test_crl_der_to_pem(self): |
| 107 | + """CRL DER -> PEM conversion and verification.""" |
| 108 | + out_pem = "test_crl_d2p.pem" |
| 109 | + self._clean(out_pem) |
| 110 | + |
| 111 | + r = run_wolfssl("crl", "-inform", "DER", "-outform", "PEM", |
| 112 | + "-in", os.path.join(CERTS_DIR, "crl.der"), |
| 113 | + "-out", out_pem) |
| 114 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 115 | + |
| 116 | + r2 = run_wolfssl("crl", "-noout", "-CAfile", |
| 117 | + os.path.join(CERTS_DIR, "ca-cert.pem"), |
| 118 | + "-in", out_pem) |
| 119 | + self.assertEqual(r2.returncode, 0, r2.stderr) |
| 120 | + |
| 121 | + def test_crl_der_cert_to_pem_fails(self): |
| 122 | + """Converting a cert DER as CRL should fail.""" |
| 123 | + out = "test_crl_bad.pem" |
| 124 | + self._clean(out) |
| 125 | + r = run_wolfssl("crl", "-inform", "DER", "-outform", "PEM", |
| 126 | + "-in", os.path.join(CERTS_DIR, "ca-cert.der"), |
| 127 | + "-out", out) |
| 128 | + self.assertNotEqual(r.returncode, 0) |
| 129 | + |
| 130 | + def test_crl_fail_no_output_file(self): |
| 131 | + """Failed CRL conversion should not create output file.""" |
| 132 | + out = "test_crl_nofile.pem" |
| 133 | + _cleanup(out) # ensure clean before test |
| 134 | + self._clean(out) |
| 135 | + r = run_wolfssl("crl", "-inform", "DER", "-outform", "PEM", |
| 136 | + "-in", os.path.join(CERTS_DIR, "ca-cert.der"), |
| 137 | + "-out", out) |
| 138 | + self.assertNotEqual(r.returncode, 0) |
| 139 | + self.assertFalse(os.path.isfile(out), |
| 140 | + "output file should not be created on failure") |
| 141 | + |
| 142 | + |
| 143 | +class TestCRLText(unittest.TestCase): |
| 144 | + """CRL -text output tests.""" |
| 145 | + |
| 146 | + @classmethod |
| 147 | + def setUpClass(cls): |
| 148 | + if not _has_crl(): |
| 149 | + raise unittest.SkipTest("CRL not compiled into wolfSSL") |
| 150 | + if not _has_crl_text(): |
| 151 | + raise unittest.SkipTest("CRL -text not available in this wolfSSL") |
| 152 | + |
| 153 | + def test_crl_text_noout(self): |
| 154 | + """CRL -text -noout should show CRL info.""" |
| 155 | + r = run_wolfssl("crl", "-noout", |
| 156 | + "-in", os.path.join(CERTS_DIR, "crl.pem"), |
| 157 | + "-text") |
| 158 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 159 | + self.assertIn("Certificate Revocation List (CRL):", r.stdout) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + unittest.main() |
0 commit comments