Skip to content

Commit 42e8cd1

Browse files
authored
Update app.py
1 parent b95b484 commit 42e8cd1

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

app.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import OpenSSL.crypto as crypto
77
from cryptography import x509
88
from cryptography.hazmat.backends import default_backend
9-
import binascii
109

1110
app = Flask(__name__)
1211

@@ -178,14 +177,25 @@
178177
</html>
179178
'''
180179

181-
def get_der_certificate_info(cert_path):
180+
def get_certificate_info(cert_path):
182181
try:
183182
with open(cert_path, 'rb') as cert_file:
184183
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+
189199
# Extract certificate details
190200
subject = ", ".join([f"{name.decode()}={value.decode()}"
191201
for name, value in cert.get_subject().get_components()])
@@ -216,9 +226,6 @@ def get_der_certificate_info(cert_path):
216226
ext = cert.get_extension(i)
217227
extension_info.append(f"{ext.get_short_name().decode()}: {str(ext)}")
218228

219-
# For more detailed analysis, we can also use cryptography library
220-
crypto_cert = x509.load_der_x509_certificate(cert_data, default_backend())
221-
222229
return {
223230
'name': os.path.basename(cert_path),
224231
'path': cert_path,
@@ -291,7 +298,7 @@ def find_certificates():
291298

292299
public_certs = []
293300
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))
295302

296303
private_certs = []
297304
for cert_path in glob.glob(private_cert_path):
@@ -332,7 +339,7 @@ def home():
332339
return render_template_string(HTML_TEMPLATE,
333340
public_certs=public_certs,
334341
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
336343
current_user="joerob-msft", # Using the provided username
337344
hostname=hostname,
338345
cert_env_var=cert_env_var,

0 commit comments

Comments
 (0)