Golang从哪里获取根证书颁发机构?

41

crypto/tls.Config.RootCAs 说明

// RootCAs defines the set of root certificate authorities
// that clients use when verifying server certificates.
// If RootCAs is nil, TLS uses the host's root CA set.
在Linux中,“主机的根CA集”从哪里获取?我需要知道这一点,以便能够全局添加另一个根CA来建立信任。

在Linux中,“主机的根CA集”从哪里获取?我需要知道这一点,以便能够全局添加另一个根CA来建立信任。


这是一个系统配置问题;Go 与此无关。对于所有编程语言都是一样的,取决于您的系统如何配置。 - joshlf
2
好的,它是如何确定我的系统配置的呢? - Ztyx
在 Darwin 上,相关文件位于 x509 包中:root_cgo_darwin.go - joshlf
3
找到答案了。写下了我的问题的答案。 - Ztyx
4个回答

68

它会在以下位置进行搜索:https://golang.org/src/crypto/x509/root_linux.go

摘录

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package x509

// Possible certificate files; stop after finding one.
var certFiles = []string{
    "/etc/ssl/certs/ca-certificates.crt",                // Debian/Ubuntu/Gentoo etc.
    "/etc/pki/tls/certs/ca-bundle.crt",                  // Fedora/RHEL 6
    "/etc/ssl/ca-bundle.pem",                            // OpenSUSE
    "/etc/pki/tls/cacert.pem",                           // OpenELEC
    "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
    "/etc/ssl/cert.pem",                                 // Alpine Linux
}

6
请注意,额外的根密钥将从在同一.go文件中定义的certDirectories目录中的文件中读取。具体来说,此列表包括/etc/ssl/certs/etc/pki/tls/certscertFilescertDirectories均可通过环境变量(分别为SSL_CERT_FILESSL_CERT_DIR)进行覆盖。 - matz

12

您还可以设置环境变量 "SSL_CERT_FILE" 以让 Golang 使用您自定义的证书文件。


它会覆盖现有的还是只是额外添加? - Samet Baskıcı
抱歉,我不记得了。我现在不再使用golang了。 - Linden X. Quan

5

4

以下是位置信息,找到一个后停止:

"/etc/ssl/certs/ca-certificates.crt",                // Debian/Ubuntu/Gentoo etc.
"/etc/pki/tls/certs/ca-bundle.crt",                  // Fedora/RHEL 6
"/etc/ssl/ca-bundle.pem",                            // OpenSUSE
"/etc/pki/tls/cacert.pem",                           // OpenELEC
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // CentOS/RHEL 7
"/etc/ssl/cert.pem",                                 // Alpine Linux

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接