所以我想用OpenSSL加密模塊,生成與該代碼的新的CA證書:OpenSSL.crypto.X509.sign()拋出「‘字節’對象有沒有屬性‘編碼’」
#warning: this block is background information, probably not
#where my real problem is
#generate the key pair
key=OpenSSL.crypto.PKey()
key.generate_key(OpenSSL.crypto.TYPE_RSA,2048)
#print the private and public keys as PEMs
print(codecs.decode(OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM,key),'utf8'))
print(codecs.decode(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM,key),'utf8'))
#generate a new x509 certificate
ca=OpenSSL.crypto.X509()
#fill it with goodies
ca.set_version(3)
ca.set_serial_number(1)
ca.get_subject().CN = "CA.test.com"
ca.gmtime_adj_notBefore(0)
ca.gmtime_adj_notAfter(60 * 60 * 24 * 365 * 10)
ca.set_issuer(ca.get_subject())
ca.set_pubkey(key)
#print the new certificate as a PEM
print(codecs.decode(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,ca),'utf8'))
在SSLShopper certificate decoder處打印解碼的證書OK,所以我對這部分感覺非常自信。麻煩的真正開始時,我嘗試用
ca.sign(key, 'sha1')
簽署證書,因爲我得到一個「預期類型‘字節’,得到了‘海峽’,而不是」從IDE。檢查OpenSSL.crypto.X509.sign()文件,並確認它確實需要一個字節對象,切換到
digestname='sha1'.encode('utf-8')
ca.sign(key, digestname)
,我得到一個「AttributeError的:‘字節’對象有沒有屬性‘編碼’」的例外。通過代碼步進我發現異常在OpenSSL._util.byte_string(拋出),因爲
if PY3:
def byte_string(s):
return s.encode("charmap")
else:
def byte_string(s):
return s
其中PY3 =真和s = {}字節b'sha1' ,這當然不具有.encode方法。
因此開始我的士氣低落'字節'vs'str'的鬥爭。我想我不是唯一一個遇到這個問題的人,但我最好的Google-fu已經說服了我。在這一點上,我甚至不知道該讀什麼才能弄清楚這個問題。