I am doing research on the requirements for using untrusted root certificates on the Internet and I am having some problems with SSL chain detection.
Now I detect specified certificates with this function:
def _get_root_cert(link: str):
cert = ssl.get_server_certificate((link.split('//')[1], 443))
x509 = crypto.load_certificate(crypto.FILETYPE_PEM, cert.encode())
return x509.get_issuer().get_components()[2][1].decode()
This code works fine, but for some sites (like this) I got No-Sni certificate name instead of Russian Trusted CA
. So now I need to get the full certificate chain, but I have some problems.
I've tried to get chain with this code, but it's not working as expected. Can anyone help me?
My experiments with chain detection:
import socket
import ssl
import base64
import binascii
hostname: str = 'https://mintrans.ryazan.gov.ru'
# Set up a socket connection to the site
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as sock:
# Get the certificate chain
cert_chain = sock.getpeercert(True)
# cert_chain_b64 = base64.b64encode(cert_chain).decode('utf-8')
# cert_chain_bytes = binascii.a2b_base64(cert_chain_b64)
# cert_chain_text = cert_chain_bytes.decode('utf-8')
cert_chain_text = None
try:
cert_chain_text = cert_chain.decode('utf-8')
except UnicodeDecodeError:
pass
if cert_chain_text is None:
try:
cert_chain_text = cert_chain.decode('latin-1')
except UnicodeDecodeError:
pass
if cert_chain_text is None:
try:
cert_chain_text = cert_chain.decode('windows-1252')
except UnicodeDecodeError:
pass
if cert_chain_text is None:
# Handle the error if none of the encodings work
print('Unable to decode certificate chain')
print(cert_chain_text)
I'm sorry I don't know how to proceed with python but it's quiete easy with openssl :
echo Q |openssl s_client -showcerts -verify 5 -connect google.com:443
Which result in :
verify depth is 5
CONNECTED(00000003)
depth=2 C = US, O = Google Trust Services LLC, CN = GTS Root R1
verify return:1
depth=1 C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
verify return:1
depth=0 CN = *.google.com
verify return:1
---
Certificate chain
0 s:CN = *.google.com
i:C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
-----BEGIN CERTIFICATE-----
MIIOPTCCDSWgAwIB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UjM1NSOh2z56H5CKh61eM
/V8CIQDDVDOJQ/gn2SXHlM2f+r9dDoOOWMEJ0si7+Y02USY4sgB2ALNzdwfhhFD4
Y4bWBancEQlKeS2xZwwLh9zwAw55NqWaAAABh0ZouWsAAAQDAEcwRQIgWa/o6iit
+yyx1/Ec2gT0zI6+CpKNbPaDRkMCpowgfYQCIQCfxCmN2eUJgrTKUeDS1rzDi3qe
9t/IwUFnI/ObYEqXsTANBgkqhkiG9w0BAQsFAAOCAQEA2ohY9gl5/nJbzOMfZGbI
BpOM0wtbnQk95p4IhmfV1G3WSvs9Myyf/EwgAT9ljD3UcZKcQqgC4txXbaV6xyLh
g0xQHPIL/IcfRHtiYtksYg36WrctBUdStumlwxQYc7TIfHaUffO42N3zNCTVT9Il
5ojPi1dYqP7yoSXsuqOKpVH7D827zv4vvMA6t0nYM49Pa7rXYrDuY3yrOgGml+7V
93UBNTDXxbbiWAiAc4C+K+xb36PVtn3Q24Okd8j1ABy012/IXiZM9pZbklIbW9EP
ndS9zxK191MlzkkL10nURP99f0L7LxABFjKwIFJXntrhtkcmi09rU65um5PhT0Gq
8w==
-----END CERTIFICATE-----
1 s:C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
i:C = US, O = Google Trust Services LLC, CN = GTS Root R1
-----BEGIN CERTIFICATE-----
MIIFljCCA36gAwIBAgINAgO8U1lrNMcY9QFQZjANBgkqhkiG9w0BAQsFADBHMQsw
CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU
MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMjAwODEzMDAwMDQyWhcNMjcwOTMwMDAw
MDQyWjBGMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp
Y2VzIExMQzETMBEGA1UEAxMKR1RTIENBIDFDMzCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAPWI3+dijB43+DdCkH9sh9D7ZYI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xtkOCcXCb1YyRt8OS1b887U7ZfbFAO/CVMkH8IMBHmYJvJh8VNS/UKMG2Yr
PxWhu//2m+OBmgEGcYk1KCTd4b3rGS3hSMs9WYNRtHTGnXzGsYZbr8w0xNPM1IER
lQCh9BIiAfq0g3GvjLeMcySsN1PCAJA/Ef5c7TaUEDu9Ka7ixzpiO2xj2YC/WXGs
Yye5TBeg2vZzFb8q3o/zpWwygTMD0IZRcZk0upONXbVRWPeyk+gB9lm+cZv9TSjO
z23HFtz30dZGm6fKa+l3D/2gthsjgx0QGtkJAITgRNOidSOzNIb2ILCkXhAd4FJG
AJ2xDx8hcFH1mt0G/FX0Kw4zd8NLQsLxdxP8c4CU6x+7Nz/OAipmsHMdMqUybDKw
juDEI/9bfU1lcKwrmz3O2+BtjjKAvpafkmO8l7tdufThcV4q5O8DIrGKZTqPwJNl
1IXNDw9bg1kWRxYtnCQ6yICmJhSFm/Y3m6xv+cXDBlHz4n/FsRC6UfTd
-----END CERTIFICATE-----
2 s:C = US, O = Google Trust Services LLC, CN = GTS Root R1
i:C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
-----BEGIN CERTIFICATE-----
MIIFYjCCBEqgAwIBAgIQd70NbNs2+RrqIQ/E8FjTDTANBgkqhkiG9w0BAQsFADBX
MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE
CxMHUm9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTIwMDYx
OTAwMDA0MloXDTI4MDEyODAwMDA0MlowRzELMAkGA1UEBhMCVVMxIjAgBgNVBAoT
GUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBMTEMxFDASBgNVBAMTC0dUUyBSb290IFIx
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAthECix7joXebO9y/lD63
ladAPKH9gvl9MgaCcfb2jH/76Nu8ai6Xl6OMS/kr9rH5zoQdsfnFl97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SkHrEoo9C0dhemMXoh6dFSPsjbdBZBiLg9
NR3t5P+T4Vxfq7vqfM/b5A3Ri1fyJm9bvhdGaJQ3b2t6yMAYN/olUazsaL+yyEn9
WprKASOshIArAoyZl+tJaox118fessmXn1hIVw41oeQa1v1vg4Fv74zPl6/AhSrw
9U5pCZEt4Wi4wStz6dTZ/CLANx8LZh1J7QJVj2fhMtfTJr9w4z30Z209fOU0iOMy
+qduBmpvvYuR7hZL6Dupszfnw0Skfths18dG9ZKb59UhvmaSGZRVbNQpsg3BZlvi
d0lIKO2d1xozclOzgjXPYovJJIultzkMu34qQb9Sz/yilrbCgj8=
-----END CERTIFICATE-----
---
Server certificate
subject=CN = *.google.com
issuer=C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: ECDSA
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 6780 bytes and written 382 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 256 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
DONE
So in python if you can call a system command from the shell of the machine, you can use the previous command
Please also note the '-verify' value which is the depth of the chain. For big chains with lots of intermediate, you can upgrade this number to 10 or 15.
For your special case with python maybe you can do something like :
import subprocess
cmd_str = "echo Q |openssl s_client -showcerts -verify 15 -connect google.com:443"
subprocess.run(cmd_str, shell=True)
PS : As suggested by @SteffenUllrich, if you're using an old version of openssl, you should add to the prevuious command the servername, for example in your use case :
-servername mintrans.ryazan.gov.ru
The whole command will be :
echo Q |openssl s_client -showcerts -verify 15 -connect mintrans.ryazan.gov.ru:443 -servername mintrans.ryazan.gov.ru
This is not necessary with :
$ openssl version
OpenSSL 1.1.1n 15 Mar 2022
Hope it could help