linux - 如何在 Linux 上使用 Go 指定自定义 SSL 根目录

标签 linux go ssl-certificate x509

假设我正在使用我不想修改的第三方网络库。它使用标准的 http.Request 接口(interface)来发出一些 HTTPS 请求。

我在没有安装证书根的嵌入式 Linux 实例上运行它,我可以访问的唯一目录是 /data。这意味着您收到错误:

Get https://example.com/: x509: failed to load system roots and no roots provided

有没有办法真正提供根?据我所知Go looks in these directories for X509 certificate roots ( see also ):

var certFiles = []string{
    "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu/Gentoo etc.
    "/etc/pki/tls/certs/ca-bundle.crt",   // Fedora/RHEL
    "/etc/ssl/ca-bundle.pem",             // OpenSUSE
    "/etc/pki/tls/cacert.pem",            // OpenELEC
}
var certDirectories = []string{
    "/system/etc/security/cacerts", // Android
}

正如我所说,我无权访问这些,以及 root pool seems to be private所以你不能附加到它:

var (
    once        sync.Once
    systemRoots *CertPool
)
func systemRootsPool() *CertPool {
    once.Do(initSystemRoots)
    return systemRoots
}

这对 Go 来说是不可能的吗?

最佳答案

这样的事情似乎可行(我实际上只需要一个证书链,您可以通过单击 SSL 锁定图标、更多信息、查看证书、详细信息、导出、将类型更改为“X509 证书与链(PEM)”。

func initCerts() error {
    certs := x509.NewCertPool()
    pemData, err := ioutil.ReadFile("api.example.crt")
    if err != nil {
        return err
    }
    certs.AppendCertsFromPEM(pemData)

    newTlsConfig := &tls.Config{}
    newTlsConfig.RootCAs = certs

    defaultTransport := http.DefaultTransport.(*http.Transport)
    defaultTransport.TLSClientConfig = newTlsConfig
    return nil
}

我不确定,但文档建议您必须在使用任何 TLS 函数之前执行此操作,因此我将其放在 main() 的开头。

关于linux - 如何在 Linux 上使用 Go 指定自定义 SSL 根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32631289/

相关文章:

go - 如何读取cookie?

.htaccess - 如何删除https

linux - 只写映射一个 O_WRONLY 打开的文件应该工作?

arrays - 数组数组中特定位置的类型?

python - 在Python中创建假进程

go - 你能让这个 'incorrectly synchronized' 测试失败吗?

java - JDK8 -> JDK10 : PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested target

ssl - 续订过期的 SSL 证书未加载 Nginx

c++ - 轮询模式下的 CPU 使用率

linux - 如果涉及 MAP_FIXED,无限堆栈不能超过初始的 132KiB?