【问题标题】:How to retrieve issuer alternative name for ssl certificate by openssl如何通过 openssl 检索 ssl 证书的颁发者替代名称
【发布时间】:2013-04-08 09:18:51
【问题描述】:

我可以得到像

这样的主题替代名称
 X509_NAME_get_text_by_NID(X509_get_subject_name(x), NID_subject_alt_name, hc->https_domain_name, 256)

通过将 2. 参数更改为 NID_issuer_alt_name 使用相同的方法,我希望得到类似的发行者名称;

X509_NAME_get_text_by_NID(X509_get_subject_name(x), NID_issuer_alt_name, hc->https_ca_name, 256);

但我得到的是一个空字符串。如何正确检索颁发者备用名称?

【问题讨论】:

  • 请检查给定证书是否有颁发者备用名称。我认为这是一个扩展。
  • 你不应该使用X509_NAME_get_text_by_NID,因为它受到Marlinspike 嵌入的NULL 技巧的影响。来自 OpenSSL 文档(未提及该漏洞):“应使用X509_NAME_get_index_by_NIDX509_NAME_get_index_by_OBJ,后跟X509_NAME_get_entry
  • 另见 OpenSSL 的 wiki 页面 Hostname Validation

标签: c ssl openssl


【解决方案1】:

您可以按照https://github.com/iSECPartners/ssl-conservatory 中的建议尝试以下解决方案:

static HostnameValidationResult matches_subject_alternative_name (const char *hostname, const X509 *server_cert) {
    HostnameValidationResult result = MatchNotFound;
    int i;
    int san_names_nb = -1;
    STACK_OF(GENERAL_NAME) *san_names = NULL;

    // Try to extract the names within the SAN extension from the certificate
    san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL);
    if (san_names == NULL) {
        return NoSANPresent;
    }
    san_names_nb = sk_GENERAL_NAME_num(san_names);

    // Check each name within the extension
    for (i=0; i<san_names_nb; i++) {
        const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i);

        if (current_name->type == GEN_DNS) {
            // Current name is a DNS name, let's check it
            char *dns_name = (char *) ASN1_STRING_data(current_name->d.dNSName);

            // Make sure there isn't an embedded NUL character in the DNS name
            if (ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) {
                result = MalformedCertificate;
                break;
            }
            else { // Compare expected hostname with the DNS name
                if (strcasecmp(hostname, dns_name) == 0) {
                    result = MatchFound;
                    break;
                }
            }
        }
    }
    sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);

    return result;
}

希望对你有帮助!

【讨论】:

  • 据我了解,这是为了获取主题替代名称。我需要发行人备用名称。
  • 尝试将NID_subject_alt_name 替换为NID_issuer_alt_name,我现在将尝试在我的PC 上执行此操作,看看它对我的效果如何。
  • 你的意思是对的,X509_get_ext_d2i((X509 *) server_cert, NID_issuer_alt_name, NULL, NULL) 。不过,我尝试了仍然是空字符串,但感谢您的建议。
  • 当心潜在的空指针取消引用。 sk_GENERAL_NAME_value() 调用 sk_value 可以返回 NULL。但是,您的指针 current_name 在取消引用之前不会检查 NULL。
【解决方案2】:

在您使用 NID_issuer_alt_name 常量调用 X509_NAME_get_text_by_NID 时,我会将 X509_get_subject_name(x) 替换为 X509_get_issuer_name(x)。我认为这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2019-08-04
    • 1970-01-01
    • 2021-04-22
    • 2017-07-30
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多