-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_ca_certificates.py
More file actions
229 lines (177 loc) · 6.78 KB
/
Copy pathtest_ca_certificates.py
File metadata and controls
229 lines (177 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Test script to download and test CA certificate CN extraction.
This script downloads various CA intermediate certificates from different
Certificate Authorities and tests the CN extraction and sanitization logic
to ensure NetScaler-compatible object names are generated.
"""
import sys
import os
import tempfile
import requests
from OpenSSL import crypto
# Add src directory to path to import our module
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from netscaler_certbot_hook.cli import get_certificate_cn
# CA Certificates to test
CA_CERTIFICATES = {
'Let\'s Encrypt R10': 'https://letsencrypt.org/certs/2024/r10.pem',
'Let\'s Encrypt R11': 'https://letsencrypt.org/certs/2024/r11.pem',
'Let\'s Encrypt E5': 'https://letsencrypt.org/certs/2024/e5.pem',
'Let\'s Encrypt E6': 'https://letsencrypt.org/certs/2024/e6.pem',
'ZeroSSL RSA': 'http://zerossl.crt.sectigo.com/ZeroSSLRSADomainSecureSiteCA.crt',
'GoDaddy G2': 'https://certs.godaddy.com/repository/gdig2.crt.pem',
# Add more CAs as needed
}
def download_certificate(url):
"""Download certificate from URL.
Args:
url (str): URL to certificate file
Returns:
str: Path to downloaded certificate file
Raises:
Exception: If download fails
"""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
# Create temporary file
# Handle both PEM (text) and DER (binary) formats
content = response.content
# Try to decode as text (PEM)
try:
content_text = content.decode('utf-8')
# If it's PEM format, it should contain BEGIN CERTIFICATE
if 'BEGIN CERTIFICATE' in content_text:
with tempfile.NamedTemporaryFile(mode='w', suffix='.pem', delete=False) as f:
f.write(content_text)
return f.name
except UnicodeDecodeError:
pass
# If not PEM, assume DER and convert to PEM
cert = crypto.load_certificate(crypto.FILETYPE_ASN1, content)
pem_data = crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8')
with tempfile.NamedTemporaryFile(mode='w', suffix='.pem', delete=False) as f:
f.write(pem_data)
return f.name
except Exception as e:
raise Exception(f"Failed to download {url}: {e}")
def get_raw_cn(cert_file):
"""Get raw CN without sanitization.
Args:
cert_file (str): Path to certificate file
Returns:
str: Raw Common Name from certificate
"""
with open(cert_file, 'r') as f:
cert_data = f.read()
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_data)
subject = cert.get_subject()
return subject.CN
def validate_netscaler_name(name):
"""Validate if name is NetScaler-compatible.
NetScaler object names must:
- Begin with ASCII alphanumeric or underscore (_)
- Contain only: alphanumeric, underscore, hash (#), period (.), space,
colon (:), at (@), equals (=), hyphen (-)
- Have minimum length = 1
Args:
name (str): Object name to validate
Returns:
tuple: (is_valid, warnings)
"""
warnings = []
is_valid = True
# Check minimum length
if len(name) < 1:
warnings.append("Name must have at least 1 character")
is_valid = False
return is_valid, warnings
# Check first character (must be alphanumeric or underscore)
if not (name[0].isalnum() or name[0] == '_'):
warnings.append(f"Must start with alphanumeric or underscore, got '{name[0]}'")
is_valid = False
# Check allowed characters (conservative whitelist)
allowed_chars = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_- ')
invalid_chars = [c for c in name if c not in allowed_chars]
if invalid_chars:
warnings.append(f"Invalid chars: {set(invalid_chars)}")
is_valid = False
# Check maximum length (NetScaler limit for certificate names is 31 chars)
if len(name) > 31:
warnings.append(f"Name too long ({len(name)} chars, max 31)")
is_valid = False
return is_valid, warnings
def main():
"""Main test function."""
print("=" * 80)
print("CA Certificate CN Extraction Test")
print("=" * 80)
print()
results = []
for ca_name, url in CA_CERTIFICATES.items():
print(f"Testing: {ca_name}")
print(f"URL: {url}")
try:
# Download certificate
cert_file = download_certificate(url)
# Get raw CN
raw_cn = get_raw_cn(cert_file)
print(f" Raw CN: '{raw_cn}'")
# Get sanitized CN using our function
sanitized_cn = get_certificate_cn(cert_file)
print(f" Sanitized: '{sanitized_cn}'")
# Validate
is_valid, warnings = validate_netscaler_name(sanitized_cn)
status = "✓ OK" if is_valid else "✗ FAIL"
if warnings:
print(f" Warnings: {', '.join(warnings)}")
print(f" Status: {status}")
results.append({
'ca': ca_name,
'raw_cn': raw_cn,
'sanitized': sanitized_cn,
'length': len(sanitized_cn),
'valid': is_valid,
'warnings': warnings
})
# Cleanup
os.unlink(cert_file)
except Exception as e:
print(f" ERROR: {e}")
results.append({
'ca': ca_name,
'raw_cn': 'ERROR',
'sanitized': 'ERROR',
'length': 0,
'valid': False,
'warnings': [str(e)]
})
print()
# Print summary table
print("=" * 80)
print("SUMMARY")
print("=" * 80)
print()
print(f"{'CA':<25} {'Raw CN':<35} {'Sanitized':<20} {'Len':<5} {'Status':<10}")
print("-" * 80)
for result in results:
status = "✓" if result['valid'] else "✗"
raw_cn_truncated = result['raw_cn'][:33] + '...' if len(result['raw_cn']) > 35 else result['raw_cn']
sanitized_truncated = result['sanitized'][:18] + '...' if len(result['sanitized']) > 20 else result['sanitized']
print(f"{result['ca']:<25} {raw_cn_truncated:<35} {sanitized_truncated:<20} {result['length']:<5} {status:<10}")
if result['warnings']:
for warning in result['warnings']:
print(f" ⚠ {warning}")
print()
print("=" * 80)
# Check if all passed
all_valid = all(r['valid'] for r in results)
if all_valid:
print("✓ All tests passed!")
return 0
else:
print("✗ Some tests failed - review sanitization logic")
return 1
if __name__ == '__main__':
sys.exit(main())