|
6 | 6 | import OpenSSL.crypto as crypto |
7 | 7 | from cryptography import x509 |
8 | 8 | from cryptography.hazmat.backends import default_backend |
9 | | -import binascii |
10 | 9 |
|
11 | 10 | app = Flask(__name__) |
12 | 11 |
|
|
178 | 177 | </html> |
179 | 178 | ''' |
180 | 179 |
|
181 | | -def get_der_certificate_info(cert_path): |
| 180 | +def get_certificate_info(cert_path): |
182 | 181 | try: |
183 | 182 | with open(cert_path, 'rb') as cert_file: |
184 | 183 | cert_data = cert_file.read() |
185 | | - |
186 | | - # Parse DER format |
187 | | - cert = crypto.load_certificate(crypto.FILETYPE_ASN1, cert_data) |
188 | | - |
| 184 | + |
| 185 | + try: |
| 186 | + # Try to load as a PEM certificate first |
| 187 | + cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_data) |
| 188 | + except crypto.Error: |
| 189 | + try: |
| 190 | + # If PEM fails, try to load as a DER certificate |
| 191 | + cert = crypto.load_certificate(crypto.FILETYPE_ASN1, cert_data) |
| 192 | + except crypto.Error: |
| 193 | + return { |
| 194 | + 'name': os.path.basename(cert_path), |
| 195 | + 'path': cert_path, |
| 196 | + 'error': 'Unable to parse certificate format' |
| 197 | + } |
| 198 | + |
189 | 199 | # Extract certificate details |
190 | 200 | subject = ", ".join([f"{name.decode()}={value.decode()}" |
191 | 201 | for name, value in cert.get_subject().get_components()]) |
@@ -216,9 +226,6 @@ def get_der_certificate_info(cert_path): |
216 | 226 | ext = cert.get_extension(i) |
217 | 227 | extension_info.append(f"{ext.get_short_name().decode()}: {str(ext)}") |
218 | 228 |
|
219 | | - # For more detailed analysis, we can also use cryptography library |
220 | | - crypto_cert = x509.load_der_x509_certificate(cert_data, default_backend()) |
221 | | - |
222 | 229 | return { |
223 | 230 | 'name': os.path.basename(cert_path), |
224 | 231 | 'path': cert_path, |
@@ -291,7 +298,7 @@ def find_certificates(): |
291 | 298 |
|
292 | 299 | public_certs = [] |
293 | 300 | for cert_path in glob.glob(public_cert_path): |
294 | | - public_certs.append(get_der_certificate_info(cert_path)) |
| 301 | + public_certs.append(get_certificate_info(cert_path)) |
295 | 302 |
|
296 | 303 | private_certs = [] |
297 | 304 | for cert_path in glob.glob(private_cert_path): |
@@ -332,7 +339,7 @@ def home(): |
332 | 339 | return render_template_string(HTML_TEMPLATE, |
333 | 340 | public_certs=public_certs, |
334 | 341 | private_certs=private_certs, |
335 | | - current_time="2025-04-23 20:10:52 UTC", # Using the provided time |
| 342 | + current_time="2025-04-23 20:16:54", # Using the updated time |
336 | 343 | current_user="joerob-msft", # Using the provided username |
337 | 344 | hostname=hostname, |
338 | 345 | cert_env_var=cert_env_var, |
|
0 commit comments