Created
August 1, 2018 14:42
-
-
Save kkirsanov/e92060838e4aa5594e5c9270d9dd2d8e to your computer and use it in GitHub Desktop.
Работа с сертификатами
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from OpenSSL import crypto | |
def createKeyPair(): | |
pkey = crypto.PKey() | |
pkey.generate_key(crypto.TYPE_DSA, 2048) | |
pubKeyString = crypto.dump_publickey(crypto.FILETYPE_PEM, pkey).decode('latin1') | |
privateKeyString = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey).decode('latin1') | |
return pubKeyString, privateKeyString | |
def mk_req(pub: str, priv: str, crt_name: str): | |
prvkey = crypto.load_privatekey(crypto.FILETYPE_PEM, priv.encode('latin1')) | |
pubkey = crypto.load_publickey(crypto.FILETYPE_PEM, pub.encode('latin1')) | |
req = crypto.X509Req() | |
subj = req.get_subject() | |
name = dict(C='RU', ST='Moscow', L='MOSCOW', O='RFI BANK JST', OU='PPMK', CN=crt_name) | |
for key, value in name.items(): | |
setattr(subj, key, value) | |
req.set_pubkey(pubkey) | |
req.sign(prvkey, 'sha256') | |
return crypto.dump_certificate_request(crypto.FILETYPE_PEM, req).decode('latin1') | |
""" | |
public_key, private_key = createKeyPair() | |
print(public_key) | |
print(private_key) | |
req = mk_req(public_key, private_key, "asd") | |
print(req) | |
""" | |
# првоерка на бою | |
with open('/home/kkirsanov/1/vip.gdz.ru.key', 'rb') as f: | |
prv_txt = f.read() | |
prvkey = crypto.load_privatekey(crypto.FILETYPE_PEM, prv_txt) | |
with open('/home/kkirsanov/1/vip.gdz.cer', 'rb') as f: | |
crt_text = f.read() | |
crt = crypto.load_certificate(crypto.FILETYPE_PEM, crt_text) | |
with open('vip.gdz.pfx', "wb") as f: | |
p12 = crypto.PKCS12() | |
p12.set_certificate(crt) | |
p12.set_privatekey(prvkey) | |
f.write(p12.export()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment