From ea6bec851b8ce980930fc7439b4fd636d34ab527 Mon Sep 17 00:00:00 2001 From: Seth Schoen Date: Sun, 23 Nov 2014 18:21:56 -0800 Subject: [PATCH] Add utility functions for checking CSR and privkey --- letsencrypt/client/crypto_util.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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