-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathcertificado.py
43 lines (32 loc) · 1.13 KB
/
certificado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
# © 2016 Danimar Ribeiro, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import tempfile
from OpenSSL import crypto
class Certificado(object):
def __init__(self, pfx, password):
self.pfx = pfx
self.password = password
def save_pfx(self):
pfx_temp = tempfile.mkstemp()[1]
arq_temp = open(pfx_temp, "wb")
arq_temp.write(self.pfx)
arq_temp.close()
return pfx_temp
def extract_cert_and_key_from_pfx(pfx, password):
pfx = crypto.load_pkcs12(pfx, password.encode())
# PEM formatted private key
key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pfx.get_privatekey())
# PEM formatted certificate
cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pfx.get_certificate())
return cert.decode(), key.decode()
def save_cert_key(cert, key):
cert_temp = tempfile.mkstemp()[1]
key_temp = tempfile.mkstemp()[1]
arq_temp = open(cert_temp, "w")
arq_temp.write(cert)
arq_temp.close()
arq_temp = open(key_temp, "w")
arq_temp.write(key)
arq_temp.close()
return cert_temp, key_temp