m2crypto 抛出“TypeError:在方法“x509_req_set_pubkey”中”

发布于 2024-07-29 16:38:01 字数 3470 浏览 5 评论 0原文

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

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 = "[email protected]"

    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

My little code snippet throws the following 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 *'

Here are my two python modules:

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 = "[email protected]"

    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()

Why am I getting this TypeError?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神妖 2024-08-05 16:38:01

如果我将 "cert_request.set_pubkey(EVP.PKey(keypair))" 更改为 "cert_request.set_pubkey(keypair)" 我会收到以下回溯。 这让我更加困惑...

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'

If I change "cert_request.set_pubkey(EVP.PKey(keypair))" to "cert_request.set_pubkey(keypair)" I receive the following Traceback instead. This confuses me even more...

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文