Skip to content

Instantly share code, notes, and snippets.

@asfelix
Created May 31, 2019 12:02
Show Gist options
  • Select an option

  • Save asfelix/8a16158c19376e1441caa4d339dd1cc2 to your computer and use it in GitHub Desktop.

Select an option

Save asfelix/8a16158c19376e1441caa4d339dd1cc2 to your computer and use it in GitHub Desktop.
import sys
from OpenSSL import crypto
def generate_pem_file(string, filename):
with open(filename, 'w') as fl:
fl.write(string)
def extract_pem_certificate():
path_file = sys.argv[1]
password = sys.argv[2]
try:
certificate = open(path_file, 'r').read()
except IOError:
print('Não foi possivel encontrar o arquivo')
return False
try:
cert = crypto.load_pkcs12(certificate, password)
except crypto.Error:
print('Senha incorreta para o certificado.')
return False
private_key = cert.get_privatekey()
x509 = cert.get_certificate()
cert_pem = crypto.dump_certificate(crypto.FILETYPE_PEM, x509)
key_pem = crypto.dump_privatekey(crypto.FILETYPE_PEM, private_key)
generate_pem_file(cert_pem, 'certificate.pem')
generate_pem_file(key_pem, 'private_key.pem')
print('Certificado e chave extraidos com sucesso.')
return True
if __name__ == '__main__':
extract_pem_certificate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment