【问题标题】:CSR submitted to IIS CA fails due to ASN1 value由于 ASN1 值,提交给 IIS CA 的 CSR 失败
【发布时间】: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


【解决方案1】:

感谢@yodatg,我在stackoverflow question 的帮助下解决了这个问题。

问题是由于bug in pyOpenSSL 已修复。

通过发行:

openssl asn1parse -in certificates/cert.csr

我可以看到 ASN1 值:

8:d=2  hl=2 l=   1 prim: INTEGER           :01

在一个有效的 CSR 中,它看起来像这样:

8:d=2  hl=2 l=   1 prim: INTEGER           :00

然后我更改了我的代码,在签名之前在 req 对象上包含一个 set_version 调用:

#set version - IIS CA required this
req.set_version(0)

# Set the public key of the certificate to pkey.
req.set_pubkey(priv_key)

现在已解决。

【讨论】:

    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2015-02-11
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    相关资源
    最近更新 更多