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:
Version: 3 (0x2)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = US, O = DigiCert Inc, CN = DigiCert TLS RSA SHA256 2020 CA1
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)
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.