title: Getting Public Key from a .pfx Certficate in Python
body: <p>I do this to process a pfx certificate, by writing out a pem file.</p>
  
  <p>How can I also pull out the public_key?  I noticed there is no get_publickey() method for the p12 object.</p>
  
  <pre><code>import contextlib
  import OpenSSL.crypto
  import os
  import requests
  import ssl
  import tempfile
  pfx_password = 'thiscertpassword' 
  tpem = 'temppem.pem' 
  pfxfile = 'fts.pfx' 
  
  
  f_pem = open(tpem, 'wb')
  pfx = open(pfxfile, 'rb').read()
  p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
  
  f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, 
  p12.get_privatekey()))
  #f_pem.write(OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM,  
  p12.get_publickey()))  # NO SUCH METHOD 
  f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, 
  p12.get_certificate()))
  </code></pre>