diff --git a/letsencrypt/client/crypto_util.py b/letsencrypt/client/crypto_util.py index 017b13e85..db6270dec 100644 --- a/letsencrypt/client/crypto_util.py +++ b/letsencrypt/client/crypto_util.py @@ -198,3 +198,31 @@ def get_cert_info(filename): "pub_key": "RSA " + str(cert.get_pubkey().size() * 8), } +def valid_csr(csr_filename): + """Check if csr_filename is a valid CSR. (Currently, could raise + non-X.509-related errors such as IOError associated with problems + reading the file.)""" + try: + csr = M2Crypto.X509.load_request(csr_filename) + return bool(csr.verify(csr.get_pubkey())) + except M2Crypto.X509.X509Error: + return False + +def valid_privkey(privkey_filename): + """Check if privkey_filename is a valid RSA private key. (Currently, + could raise non-RSA-related errors such as IOError associated with + problems reading the file.)""" + try: + privkey = M2Crypto.RSA.load_key(privkey_filename) + return bool(privkey.check_key()) + except M2Crypto.RSA.RSAError: + return False + +def csr_matches_pubkey(csr_filename, privkey_filename): + """Check if the private key in the file corresponds to the subject + public key in the CSR.""" + csr = M2Crypto.X509.load_request(csr_filename) + privkey = M2Crypto.RSA.load_key(privkey_filename) + csr_pub = csr.get_pubkey().get_rsa().pub() + privkey_pub = privkey.pub() + return csr_pub == privkey_pub