我正在通过NSS / NSPR C API添加自签名的根证书.
这需要一个x509v3扩展名,主题alt名称.但是,添加此扩展或任何x509v3扩展名会导致firefox失败,并显示错误代码:sec_error_extension_value_invalid.
// Add subjectAltName x509v3 extension containing our localhost IPv4
// address of 127.0.0.1. The subjectAltName entry takes precedence over
// the CommonName (CN) entry, thus we are allowed to have a more
// descriptive name there. In addition, this is needed by Safari on Mac in
// order to properly trust the certificate.
X509V3_CTX ctx;
X509V3_set_ctx_nodb(&ctx);
X509V3_set_ctx(&ctx, m_x509, m_x509, nullptr, nullptr, 0);
// Removing this line causes the cert to be accepted by firefox:
X509_EXTENSION* ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_subject_alt_name, (char*)"DNS:127.0.0.1,IP:127.0.0.1");
if (ext) {
X509_add_ext(m_x509, ext , -1);
X509_EXTENSION_free(ext);
}
// Sign the certificate
X509_sign(m_x509, m_key->m_pkey, EVP_sha1());
这似乎是一个pkix错误,因为在about:config中设置use_mozillapkix_verification = false,或者使用ff< 31,使证书被接受OK. 这是一个pkix错误吗?或者这里有什么东西被忽视了?
解决方法:
通过https://bugzilla.mozilla.org/show_bug.cgi?id=1045973
NSS accepts v1/v2 ceritificates with v3 extensions, mozilla::pkix does not
修复:
X509_set_version(m_x509, 2L);