Add utility functions for checking CSR and privkey

This commit is contained in:
Seth Schoen
2014-11-23 18:21:56 -08:00
parent 26e5535d21
commit ea6bec851b
+28
View File
@@ -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