title: Verify signature on X509 certificate in Python
body: <p>I have an RSA public key and a signed X509 certificate. How can I check that the key signed the certificate? (My example happens to be a self-signed certificate.)</p>
  
  <p>Here's what I'm doing now:</p>
  
  <p>Generate self-signed cert and convert to DER encoding with openssl cli:</p>
  
  <p>(I'm expecting DER in my real application)</p>
  
  <pre><code>openssl req -x509 -newkey rsa:2048 -keyout selfsigned.key -nodes -out selfsigned.cert -sha256 -days 1000
  openssl x509 -outform der -in selfsigned.cert -out self.der
  </code></pre>
  
  <p>Decode it to a <code>Crypto.Util.asn1.DerSequence</code> instance.</p>
  
  <pre><code>&gt;&gt;&gt; from Crypto.Util.asn1 import DerSequence
  &gt;&gt;&gt; from Crypto.PublicKey import RSA
  &gt;&gt;&gt; der = open('self.der').read()
  &gt;&gt;&gt; cert = DerSequence()
  &gt;&gt;&gt; cert.decode(der)
  905L
  &gt;&gt;&gt; # according to RFC5280, this is a 3-length sequence:
  &gt;&gt;&gt; # tbsCertificate, signatureAlgorithm, signatureValue
  &gt;&gt;&gt; # "tbs" == "to be signed"
  &gt;&gt;&gt; len(cert)
  3
  </code></pre>
  
  <p>Then I pull the RSA public key out:</p>
  
  <pre><code>&gt;&gt;&gt; tbscert = DerSequence()
  &gt;&gt;&gt; tbscert.decode(cert[0])
  &gt;&gt;&gt; subjectPublicKeyInfo = tbscert[6]
  &gt;&gt;&gt; rsa_key = RSA.importKey(subjectPublicKeyInfo)
  &gt;&gt;&gt; &gt;&gt;&gt; rsa_key
  &lt;_RSAobj @0x7fb27287d128 n(2048),e&gt;
  </code></pre>
  
  <p>Then I pull the signature out</p>
  
  <p>This is very annoying. I'm using another library to decode the DER again because this one gives me a slightly more convenient representation of the "bit string" encoding of the signature value. For now, I copy and paste the base-2 string representation of the value into <code>int()</code> to get a <code>long</code> (what the <code>RSA.verify()</code> method expects).</p>
  
  <pre><code>&gt;&gt;&gt; from pyasn1_modules import  rfc2437,rfc2459
  &gt;&gt;&gt; from pyasn1.codec.der import decoder
  &gt;&gt;&gt; cert2,rest = decoder.decode(der, asn1Spec=rfc2459.Certificate())
  &gt;&gt;&gt; sig_bits = cert2.getComponentByName("signatureValue")
  &gt;&gt;&gt; sig_bits
  BitString("'10101110101100111010100000111001000110111111101001110000100111110111011111111110011010110101001001110110111011011001110001010000111001001001110111001110011101000000100000001100001101000111000110010100110101000110111101110001011011001001100011110011010101011100000001010111101110111001110011010011110101101001110101110100011111011111110001110001110100000110110100010000001010111010000100101110101001110000111001100010011110110100101001010000101001110101101111100111001111000010111001110000000101000000011110011110000110000101101101110110101101110101011110101111111000101011001000010000110100111111001011111100110011011011001001111110000000110100000001101000011010010100001100110100001001000111001011111100000011000101001100001010101001111000001010100101010101000100001000100011101111100101010010001111001110100001011110101100010111001010100010001011100000100101110001011101100010010111100010111110010010110111111100100100101010010000000010001011111110011000011000101000001000000000000011111101110010101100100000010111111110101000110010101111100101011101000010010110101000101101110001001101101000110101110011101011111100010000101001111011100100100010101001011011110001110000000000010011011111011110100000111001100010100000100001101111110010000111100110110110000001010010110011010111100101110101000001111001010011101001101101101101011000100010011110000101010110111011100010100011110101100110101011010111000011111000001111111111000101101101011110010111100101011100010000111011101101010101001011101101111011001001110000010011001111010001011110000011001011000110100011100000100100111000111100000010000001001001010001100000010011110100000111010010100001101001111111001111110111010001101010110100001100111010101000010000101000000111100001001001000100011100110010110101001110111101000101101011011100000010010000100111001100001110010101000100000010010111110001100011110010000100001000101100011000011000110010110011100010100010111011011111111010000001001100000100011010000000110111100111010101001110101000011111011000100010111101100110100010101111000110111110'B")
  &gt;&gt;&gt; bit_string = '10101110101100111010100000111001000110111111101001110000100111110111011111111110011010110101001001110110111011011001110001010000111001001001110111001110011101000000100000001100001101000111000110010100110101000110111101110001011011001001100011110011010101011100000001010111101110111001110011010011110101101001110101110100011111011111110001110001110100000110110100010000001010111010000100101110101001110000111001100010011110110100101001010000101001110101101111100111001111000010111001110000000101000000011110011110000110000101101101110110101101110101011110101111111000101011001000010000110100111111001011111100110011011011001001111110000000110100000001101000011010010100001100110100001001000111001011111100000011000101001100001010101001111000001010100101010101000100001000100011101111100101010010001111001110100001011110101100010111001010100010001011100000100101110001011101100010010111100010111110010010110111111100100100101010010000000010001011111110011000011000101000001000000000000011111101110010101100100000010111111110101000110010101111100101011101000010010110101000101101110001001101101000110101110011101011111100010000101001111011100100100010101001011011110001110000000000010011011111011110100000111001100010100000100001101111110010000111100110110110000001010010110011010111100101110101000001111001010011101001101101101101011000100010011110000101010110111011100010100011110101100110101011010111000011111000001111111111000101101101011110010111100101011100010000111011101101010101001011101101111011001001110000010011001111010001011110000011001011000110100011100000100100111000111100000010000001001001010001100000010011110100000111010010100001101001111111001111110111010001101010110100001100111010101000010000101000000111100001001001000100011100110010110101001110111101000101101011011100000010010000100111001100001110010101000100000010010111110001100011110010000100001000101100011000011000110010110011100010100010111011011111111010000001001100000100011010000000110111100111010101001110101000011111011000100010111101100110100010101111000110111110'
  &gt;&gt;&gt; len(bit_string)
  2048
  &gt;&gt;&gt; sig_long = int(bit_string, 2)
  22054057292543290008991218833668878365914778519473463062473060546762899555976103489048033910135613221569150796460758806399269198735780309519101363051388009338597879536630494212385605300708879019160215628821483902624509955250980351374010304684207884550324020859785789812498991361733361061223150200173076263554090698006436248180914014712709890577579243572383188197634606581121383593473899061397708617253275982314075801792358481980896751043809539358665686019958496887281091997170247998458556812030465091755579654010246474389968142047627934047174316731806191431717418170761689395728146445291177267566370799362894264463806L
  </code></pre>
  
  <p>Then I compute the SHA256 hash of the "to be signed certificate":</p>
  
  <pre><code>&gt;&gt;&gt; import Crypto.Hash.SHA256
  &gt;&gt;&gt; comp_hash = Crypto.Hash.SHA256.new(cert[0]).digest()
  &gt;&gt;&gt; comp_hash
  '\xa3t\x84\xd6\xf5\xfe\x16\xb9\xdb(&amp;\x12\xb3m^+\x94\xa7bZ\xf9s\xf7\xbay\xa1j\xa3Y\xea\xa8\x7f'
  </code></pre>
  
  <p>Then the <code>verify()</code> method tells me the signature doesn't match.</p>
  
  <pre><code>&gt;&gt;&gt; rsa_key.verify(comp_hash, (sig_long, None))
  False
  </code></pre>
  
  <p>I hope there's a better way (this doesn't even work), but I've spent hours looking at PyCrypto and PyOpenSSL and haven't found it.</p>
  
  <h1>edits</h1>
  
  <p>This similar S.O. question from a few years ago has no answer: <a href="https://stackoverflow.com/questions/18701737/verify-ssl-x-509-certificate-is-signed-by-another-certificate?rq=1">Verify SSL/X.509 certificate is signed by another certificate</a></p>