m2crypto抛出“TypeError:in method'x509_req_set_pubkey'”

时间:2009-07-31 10:37:51

标签: python m2crypto

我的小代码片段抛出以下Traceback:

Traceback (most recent call last):
  File "csr.py", line 48, in <module>
    csr.create_cert_signing_request(pubkey, cert_name)
  File "csr.py", line 17, in create_cert_signing_request
    cert_request.set_pubkey(EVP.PKey(keypair))
  File "/usr/lib64/python2.6/site-packages/M2Crypto/X509.py", line 926, in set_pubkey
    return m2.x509_req_set_pubkey( self.req, pkey.pkey )
TypeError: in method 'x509_req_set_pubkey', argument 2 of type 'EVP_PKEY *'

这是我的两个python模块:

from config import *
from keypair import *
from M2Crypto import X509

class CSR(object):
    def __init__(self):
        pass

    def create_cert_signing_request(keypair, cert_name, cert_extension_stack=None):
        # create a certificate signing request object
        cert_request = X509.Request()

        # set certificate version to 3
        cert_request.set_version(3)

        # which rsa public key should be used?
        cert_request.set_pubkey(EVP.PKey(keypair))

        # create an subject for the certificate request
        cert_request.set_subject_name(cert_name)

        if cert_extension_stack != None:
            # add the extensions to the request
            cert_request.add_extensions(cert_extension_stack)

        # sign the request using the RSA key pair
        cert_request.sign(keypair, 'sha1')

        return cert_request

if __name__ == "__main__":
    csr = CSR()
    cert_name = X509.X509_Name()
    keyp = Keypair()

    keyp.create_keypair()
    keyp.save_keypair("host.key")
    pubkey = keyp.get_keypair()

    cert_name.C = "GB"
    cert_name.ST = "Greater Manchester"
    cert_name.L = "Salford"
    cert_name.O = "COMODO CA Limited"
    cert_name.CN = "COMODO Certification Authority"
    cert_name.OU = "Information Technology"
    cert_name.Email = "contact@comodo.com"

    csr.create_cert_signing_request(pubkey, cert_name)


from M2Crypto import X509, m2, RSA, EVP
from config import *

class Keypair(object):
    def __init__(self):
        self.config = Config()
        self.keypair = EVP.PKey()

    def create_keypair(self):
        # generate an RSA key pair
        # OpenSSL book page 232
        # second argument should be a constant RSA_F4 or RSA_3
        rsa_key_pair = RSA.gen_key(int(self.config.get_attribute('CA','key_size')), m2.RSA_F4)

        # check if RSA key pair is usable
        # OpenSSL book page 232
        if rsa_key_pair.check_key() != 1:
            print 'error while generating key!'
            sys.exit()

        # EVP object which can hold either a DSA or an RSA object
        # OpenSSL book page 236
        evp_key_container = EVP.PKey()
        evp_key_container.assign_rsa(rsa_key_pair)

        self.keypair = evp_key_container

    def save_keypair(self, filename):
        self.keypair.save_key(filename, None)

    def load_keypair(self, filename):
        self.keypair = EVP.load_key(filename)

    def get_keypair(self):
        return self.keypair

    def get_public_key(self):
        return self.keypair.pkey

    def print_keypair(self):
        print self.keypair.as_pem(None)

if __name__ == "__main__":
    key = Keypair()
    key.create_keypair()
    key.save_keypair("test.key")

    print key.get_keypair()
    print key.get_public_key()

为什么我会收到此TypeError

1 个答案:

答案 0 :(得分:0)

如果我将“cert_request.set_pubkey(EVP.PKey(密钥对))”更改为“cert_request.set_pubkey(密钥对)”,我会收到以下的回溯。这让我更加困惑......

Traceback (most recent call last):
  File "csr.py", line 48, in <module>
    csr.create_cert_signing_request(pubkey, cert_name)
  File "csr.py", line 17, in create_cert_signing_request
    cert_request.set_pubkey(keypair)
  File "/usr/lib64/python2.6/site-packages/M2Crypto/X509.py", line 926, in set_pubkey
    return m2.x509_req_set_pubkey( self.req, pkey.pkey )
AttributeError: 'CSR' object has no attribute 'pkey'