windows - Golang HTTP x509 : certificate signed by unknown authority error

标签 windows ssl go

我正在使用 Golang 1.9.2 创建一个客户端应用程序,但在访问我的后端时遇到了一些麻烦。问题是我的应用程序在最新版本的 Windows 和 Linux 上运行良好,但是当我在 Windows XP 上运行它时(是的,不幸的是我必须支持 Windows XP,因为我们的一些客户拒绝升级他们的操作系统)我尝试执行 HTTP GET 和 HTTP POST 时出现此错误:x509: certificate signed by unknown authority .

我在 Windows XP 内部使用 Firefox ESR 浏览器和 Chromium 浏览器运行了相同的 GET 命令,并且没有人提示证书。

请注意,我的证书是有效的,并由受信任的机构签署。

我做了一些研究,发现有些人有同样的问题,并通过使用以下方法忽略 TLS 验证来解决它:

import ("net/http"; "crypto/tls")

tr := &http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify : true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://someurl:443/)

所以我将此添加到我的代码中,但它仍然无法正常工作:
// NewAPIClient - creates a new API client
func NewAPIClient() Client {
    c := &APIClient{}

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkyVerify: true},
    }
    c.client = &http.Client{Transport: tr}
    return c
}

// GetTasks - retrieves a list of tasks from the backend.
func (c *APIClient) GetTasks() ([]byte, error) {
    conf := config.GetInstance()
    url := fmt.Sprintf("%s/myurl", conf.GetConfig().APIUrl)

    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        log.WithError(err).Errorf("Error creating HTTP request")
        return nil, err
    }

    // Add headers
    req.Header.Add("Authorization", conf.GetConfig().APIToken)
    req.Header.Add("Accept", "application/json")

    log.Info("Retrieving tasks from the API")
    resp, err := c.client.Do(req)
    if err != nil {
        log.WithError(err).Errorf("Error retrieving tasks from the backend")
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode != 200 {
        errMsg := fmt.Sprintf("Received status: %s", resp.Status)
        err = errors.New(errMsg)
        log.WithError(err).Error("Error retrieving tasks from the backend")
        return nil, err
    }

    tasks, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.WithError(err).Error("Error reading tasks response body")
        return nil, err
    }

    log.Info("The tasks were successfully retrieved")

    return tasks, nil
}

有没有另一种方法来解决这个问题,而不必忽略证书验证?如果不是,我在代码中做错了什么?

最佳答案

Golang 使用操作系统证书存储。以下注释表明 Go 在 Windows 上使用 Windows 商店,similar to Linux .

// CertGetCertificateChain will traverse Windows's root stores in an attempt to build a verified certificate chain


此注释和相关代码位于以下文件中:
https://golang.org/src/crypto/x509/root_windows.go
将服务器证书、中间 CA 证书和/或根 CA 证书添加到 Windows XP 证书存储区。您可以使用 IBM 发布的以下 Windows XP 说明:

Procedure

  1. From Windows XP, select Start > Run to open the command line.
  2. Type mmc into the Run dialog box and click OK to run the Microsoft Management Console (MMC).
  3. From within MMC, select File > Add/Remove Snap-in.
  4. Click Add.
  5. Click Certificates.
  6. Click My user account.
  7. Click Finish.
  8. Click Close on the Add Standalone Snap-in dialog box.
  9. Click OK on the Add/Remove Snap-in dialog box.

引用:https://www.ibm.com/docs/en/b2b-integrator/5.2?topic=xp-install-root-certificate-in-windows
GlobalSign 和 Securely 为更现代的 Windows 版本提供了类似的说明,但上面的 IBM 链接专门针对 Windows XP。下面的 Securely 文档还包括屏幕截图。
  • Import and Export Certificate - Microsoft Windows
  • How do I manually install the Securly SSL certificate on Windows - 包括屏幕截图
  • 关于windows - Golang HTTP x509 : certificate signed by unknown authority error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464161/

    相关文章:

    go - 在golang幕后进行解释?

    python - 是否可以使用 python 和 Windows 获得对原始设备的写访问权限?

    node.js - Node http-server 在 Windows 10 Ubuntu Bash 上不起作用

    java - 类路径中找不到 JKS keystore 文件

    mongodb - 我应该保留什么 setLimit(int64) 方法来显示 Collections 中的所有记录

    go - 模拟 Go Echo 上下文

    windows - 使用 Powershell 实现 IE 自动化

    windows - 手动删除和脚本删除的区别?! (bat 或 vbs)

    node.js - 使用 NodeJS 代理传递的 Apache SSL 配置无法启动

    asp.net - 连接到 Heroku Postgres 数据库时出现 CertificateUnknown 错误