【问题标题】:Is it possible to set subjectAltName using pyOpenSSL?是否可以使用 pyOpenSSL 设置 subjectAltName?
【发布时间】:2014-06-29 11:43:48
【问题描述】:

我需要使用 pyOpenSSL 从 Python 生成 SSL 证书。有谁知道是否可以设置subjectAltName?从文档 (https://pythonhosted.org/pyOpenSSL/api/crypto.html#x509-objects) 看来并非如此。 实际上,只提供了一个 set_subject 方法。 有什么办法可以将其添加到证书中?

【问题讨论】:

    标签: python ssl openssl pyopenssl


    【解决方案1】:
    san_list = ["DNS:*.google.com", "DNS:google.ym"]
    cert.add_extensions([
        OpenSSL.crypto.X509Extension(
            "subjectAltName", False, ", ".join(san_list)
       )
    ])
    

    【讨论】:

      【解决方案2】:

      我想我会在Vans S's answer 上进行扩展,因为我一直在疯狂地试图找出为什么我的 csrgen 脚本不起作用并且我终于破解了它。可悲的是,这根本不明显(对我来说)。通常我不在乎,因为我的大多数证书都是每个证书一个名称,所以主题中的 CN 通常没问题。 但是现在 Chrome won't accept certificates without the SANs set(假设 FF/IE 很快就会跟进,如果还没有的话)现在是一个表演障碍。

      我的 Python 3 看起来像这样(其中 self 是一个继承自 crypto.X509Req 的类)。

      # Add base constraints
      self.add_extensions([
          crypto.X509Extension(
              b"keyUsage", False,
              b"Digital Signature, Non Repudiation, Key Encipherment"),
          crypto.X509Extension(
              b"basicConstraints", False, b"CA:FALSE"),
          crypto.X509Extension(
              b'extendedKeyUsage', False, b'serverAuth, clientAuth'),
      ])
      
      # If there are multiple names, add them all as SANs.
      if self.sans:
          self.add_extensions([crypto.X509Extension(
              b"subjectAltName", False, self.sans.encode())])
      

      在我看来应该可行。它运行,不产生错误,不产生警告,并生成 CSR 和密钥对,但 CSR没有 SAN 扩展

      解决方案? X509Req().add_extensions() 只能使用一次!我第二次在这里调用它似乎什么也没做。所以下面的作品。

      # Add all extensions in one go as only the first call to 
      # add_extensions actually does anything. Subsequent calls will fail 
      # silently.
      self.add_extensions([
          crypto.X509Extension(
              b"keyUsage", False,
              b"Digital Signature, Non Repudiation, Key Encipherment"),
          crypto.X509Extension(
              b"basicConstraints", False, b"CA:FALSE"),
          crypto.X509Extension(
              b'extendedKeyUsage', False, b'serverAuth, clientAuth'),
          crypto.X509Extension(
              b"subjectAltName", False, self.sans.encode())
      ])
      

      【讨论】:

        【解决方案3】:

        我最终解决了它。我错过了 subjectAltName 被认为是标准扩展名。所以可以使用pyOpenSSL的add_extensions方法添加。

        更多信息请访问https://www.openssl.org/docs/apps/x509v3_config.html#STANDARD_EXTENSIONS

        【讨论】:

        • 您是否还有从 SAN 证书获取 subjectAltNames 的代码?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 2018-11-26
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-01
        相关资源
        最近更新 更多