Domanda

Ho un certificato valido rilasciato dall'Autorità spagnola (FNMT) e voglio giocarci per saperne di più. Il file ha estensione .p12

Vorrei leggere le informazioni in esso (nome e cognome) e verificare se il certificato è valido. È possibile farlo con Pyopenssl? Immagino di dover usare il modulo Crypto in OpenSSL. Qualche aiuto o un link utile? Provare a leggere qui: http://packages.python.org/pyopenssl/openssl-crypto.html ma non molte informazioni :-(

È stato utile?

Soluzione

È abbastanza diretto da usare. Questo non è testato, ma dovrebbe funzionare:

# load OpenSSL.crypto
from OpenSSL import crypto

# open it, using password. Supply/read your own from stdin.
p12 = crypto.load_pkcs12(open("/path/to/cert.p12", 'rb').read(), passwd)

# get various properties of said file.
# note these are PyOpenSSL objects, not strings although you
# can convert them to PEM-encoded strings.
p12.get_certificate()     # (signed) certificate object
p12.get_privatekey()      # private key.
p12.get_ca_certificates() # ca chain.

Per altri esempi, dai un'occhiata al Codice di test unitario di Pyopenssl. Praticamente ogni modo in cui potresti voler usare la biblioteca è lì

Guarda anche qui o senza pubblicità qui.

Altri suggerimenti

Forse è sbagliato rispondere a una vecchia Q, ma ho pensato che potesse aiutare qualcuno che trova questa Q dopo di me. Questa soluzione funziona per Python 3 e penso che sia un po 'meglio. L'ho trovato in Il repository di Zeep ed è una classe per incapsulare l'uso.

Classe

import os
from OpenSSL import crypto

class PKCS12Manager():

    def __init__(self, p12file, passphrase):
        self.p12file = p12file
        self.unlock = passphrase
        self.webservices_dir = ''
        self.keyfile = ''
        self.certfile = ''

        # Get filename without extension
        ext = os.path.splitext(p12file)
        self.filebasename = os.path.basename(ext[0])

        self.createPrivateCertStore()
        self.p12topem()

    def getKey(self):
        return self.keyfile

    def getCert(self):
        return self.certfile

    def createPrivateCertStore(self):
        home = os.path.expanduser('~')
        webservices_dir = os.path.join(home, '.webservices')
        if not os.path.exists(webservices_dir):
            os.mkdir(webservices_dir)
        os.chmod(webservices_dir, 0o700)
        self.webservices_dir = webservices_dir

    def p12topem(self):
        p12 = crypto.load_pkcs12(open(self.p12file, 'rb').read(), bytes(self.unlock, 'utf-8'))

        # PEM formatted private key
        key = crypto.dump_privatekey(crypto.FILETYPE_PEM, p12.get_privatekey())

        self.keyfile = os.path.join(self.webservices_dir, self.filebasename + ".key.pem")
        open(self.keyfile, 'a').close()
        os.chmod(self.keyfile, 0o600)
        with open(self.keyfile, 'wb') as f:
            f.write(key)


        # PEM formatted certificate
        cert = crypto.dump_certificate(crypto.FILETYPE_PEM, p12.get_certificate())

        self.certfile = os.path.join(self.webservices_dir, self.filebasename + ".crt.pem")
        open(self.certfile, 'a').close()
        os.chmod(self.certfile, 0o644)
        with open(self.certfile, 'wb') as f:
            f.write(cert)

Utilizzo

from requests import Session
from zeep import Client
from zeep.transports import Transport

# https://github.com/mvantellingen/python-zeep/issues/824
pkcs12 = PKCS12Manager('cert.p12', 'password_for_cert')
session = Session()
session.cert = (pkcs12.getCert(), pkcs12.getKey())

transport = Transport(session=session)
client = Client('url_service', transport=transport)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top