Reputation: 6968
I have the following code in C# which does the job I need it to:
//Try create an X509 cert object
X509Certificate x509Cert = new X509Certificate("C:/Users/mryan/Documents/Code/SampleApps/bundle.p12", "passphrase");
//Serialize Cert and POST to server
string devString = Newtonsoft.Json.JsonConvert.SerializeObject(x509Cert);
result = wc.UploadString(apiRoot + "/Accounts/" + accId + "/certs", "POST", devString)
So here, devString
would be equal to:
"{\"RawData\":\"MIICiTCCAfKgAwI...OhpEV23wsm06G2s5OJk=\"}"
As far as I can tell, RawData
is a property in x509Cert which equals an array of bytes.
Is there any obvious module in python that can achieve the same result? I was looking at this answer but I can't tell if it reflects what I want to achieve.
Desirable Python Pseudocode:
#Create X509 Cert object
x509cert = module.readX509Certificate("C:/Users/mryan/Documents/Code/SampleApps/bundle.p12", "passphrase")
cert_data = {"RawData": x509Cert}
result = requests.post(apiRoot + "/Accounts/" + accId + "/certs", data=cert_data)
Any ideas about how to handle this would be great!
Upvotes: 0
Views: 14316
Reputation: 87074
If I understand your question, you need to load a PKCS#12 certificate and then upload the public key (certificate) to a server. This can be done with pyopenssl's crypto module.
import json
import requests
from OpenSSL import crypto
P12_CERT_FILE = 'C:/Users/mryan/Documents/Code/SampleApps/bundle.p12'
p12_cert = crypto.load_pkcs12(open(P12_CERT_FILE).read(), 'passphrase')
pem_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, p12_cert.get_certificate())
# remove PEM header, footer, and new lines to produce raw cert data
raw_data = ''.join(pem_cert.split('\n')[1:-2])
cert_data = json.dumps({'RawData': raw_data})
result = requests.post(apiRoot + "/Accounts/" + accId + "/certs", data=cert_data)
Upvotes: 3