How to find the server's public key size
In order to check the size of the certificate's public key, you can use the openssl
command-line tool. The commands were taken from this answer by Ari Maniatis on StackOverflow, so I would recommend upvoting his answer if it was helpful.
Essentially, there are two ways of doing it: With SNI, and without SNI. Some servers require SNI, while others don't support it.
With SNI, the command is as follows:
openssl s_client -showcerts -servername www.example.com -connect www.example.com:443 </dev/null
And without SNI, the command is as follows:
openssl s_client -showcerts -connect www.example.com:443 </dev/null
You can also use this one-liner to print the certificate information and look for the public key:
echo | openssl s_client -connect google.com:443 2>/dev/null | openssl x509 -text -noout
This will print the following output:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
02:52:16:e1:c4:99:8e:26:32:aa:5d:1d:a9:85:b4:3c
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = US, O = DigiCert Inc, CN = DigiCert TLS RSA SHA256 2020 CA1
Validity
Not Before: Dec 10 00:00:00 2021 GMT
Not After : Dec 9 23:59:59 2022 GMT
Subject: C = US, ST = California, L = Los Angeles, O = "Verizon Digital Media Services, Inc.", CN = www.example.org
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
===> RSA Public-Key: (2048 bit)
Modulus:
00:a0:30:42:56:d7:0b:a1:11:b9:f3:0b:ec:cc:f2:
4c:b0:6f:13:02:6b:cf:07:f3:85:f0:42:8f:c5:54:
98:81:6e:7a:93:38:b6:fa:46:42:b3:5c:e6:c9:3b:
59:93:61:24:43:20:f5:7a:89:c9:77:ad:ff:87:c8:
08:db:86:f5:dc:61:75:96:5f:dc:f0:08:ca:3a:b9:
5e:0f:fa:37:7c:65:6a:ca:08:27:1e:9d:d8:0a:3f:
9e:10:db:45:25:9a:03:72:ba:f5:27:d9:b0:eb:36:
d4:93:39:8c:11:6c:5f:33:14:58:e5:c0:88:c5:1f:
7a:21:14:cc:d2:a7:5f:1c:73:1f:d9:03:20:6e:7a:
08:ef:17:4e:e3:be:28:c0:4f:e0:71:63:21:04:77:
8f:8f:4b:2b:e8:0b:a2:be:97:7e:50:6f:b8:3b:37:
63:7f:a4:0c:99:ff:96:a2:c3:7f:ca:7c:21:ba:fd:
90:d1:3f:05:a4:34:70:d6:84:8e:a5:00:dc:29:7c:
fd:96:cb:43:ae:39:8f:2d:c6:ad:d8:c2:1d:9b:e4:
5f:9c:51:9c:8b:fe:6d:49:62:5b:c7:cd:1e:18:96:
ce:c6:2a:07:b7:71:80:60:72:ac:57:12:00:90:43:
0f:23:be:a9:70:71:d6:e5:7b:85:a3:4d:05:88:21:
c7:23
Exponent: 65537 (0x10001)
[...]
As you can see, example.com uses an 2048 bit RSA public key. You can also add | grep -B 1 "Public-Key"
to just display the public key length and the key type. This is necessary, as EC Certificates have significantly shorter public key lengths, yet are considered just as secure.
How to reject weak public keys
Browsers will do that automatically. It seems Chrome and others automatically mark certificates with 1024 bit RSA keys as "insecure" and display a warning to the user.