【发布时间】:2015-04-21 07:11:44
【问题描述】:
我已经从 pyOpenSSL 生成了一个私钥/CSR - 下面的代码 sn-p:
键:
key = crypto.PKey()
key.generate_key(type, bits)
if os.path.exists(_keyfile):
print "Certificate file exists, aborting."
print " ", _keyfile
sys.exit(1)
else:
f = open(_keyfile, "w")
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
f.close()
return key
企业社会责任:
req = crypto.X509Req()
# Return an X509Name object representing the subject of the certificate.
req.get_subject().countryName = country
req.get_subject().stateOrProvinceName = state
req.get_subject().localityName = location
req.get_subject().organizationName = organisation
req.get_subject().organizationalUnitName = organisational_unit
req.get_subject().CN = nodename
# Add in extensions
#base_constraints = ([
# crypto.X509Extension("keyUsage", False, "Digital Signature, Non Repudiation, Key Encipherment"),
# crypto.X509Extension("basicConstraints", False, "CA:FALSE"),
#])
#x509_extensions = ([])
x509_extensions = []
# If there are SAN entries, append the base_constraints to include them.
if ss:
san_constraint = crypto.X509Extension("subjectAltName", False, ss)
x509_extensions.append(san_constraint)
req.add_extensions(x509_extensions)
# Set the public key of the certificate to pkey.
req.set_pubkey(key)
# Sign the certificate, using the key pkey and the message digest algorithm identified by the string digest.
req.sign(key, "sha1")
# Dump the certificate request req into a buffer string encoded with the type type.
if os.path.exists(_csrfile):
print "Certificate file exists, aborting."
print " ", _csrfile
sys.exit(1)
else:
f = open(_csrfile, "w")
f.write(crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))
f.close()
我从 IIS CA 得到的错误是:
符合 ASN1 错误标签值。 0x8009310b (ASN: 267)
根据Microsoft,这是由于:
当证书请求以 Unicode 编码存储在文件中时,会发生此行为。 Microsoft 证书服务不支持 Unicode 编码的文件请求文件。仅支持 ANSI 编码。
我知道,如果我在命令行上从 openssl 生成 CSR,它会被 IIS CA RESTful Web 服务接受并发出而不会出错。
我想知道是否有某种方法可以从 pyOpenSSL 生成“ANSI”编码文件 - 我不确定是密钥文件还是使用密钥文件签名的 CSR 导致了问题。
【问题讨论】:
-
现在已修复 - 更新原始问题。谢谢@yodatg
-
@jww - 完成,谢谢
标签: python unicode openssl csr pyopenssl