dousiyou1058 2015-03-06 12:23
浏览 217
已采纳

Golang SSL TCP套接字证书配置

I'm creating a Go TCP server (NOT http/s) and I'm trying to configure it to use SSL. I have a StartCom free SSL certificate which I am trying to use to accomplish this. My server code looks like this:

    cert, err := tls.LoadX509KeyPair("example.com.pem", "example.com.key")
    if err != nil {
        fmt.Println("Error loading certificate. ",err)
    }
    trustCert, err := ioutil.ReadFile("sub.class1.server.ca.pem")
    if err != nil {
        fmt.Println("Error loading trust certificate. ",err)
    }
    validationCert, err := ioutil.ReadFile("ca.pem")
    if err != nil {
        fmt.Println("Error loading validation certificate. ",err)
    }
    certs := x509.NewCertPool()
    if !certs.AppendCertsFromPEM(validationCert) {
        fmt.Println("Error installing validation certificate.")
    }
    if !certs.AppendCertsFromPEM(trustCert) {
        fmt.Println("Error installing trust certificate.")
    }

    sslConfig := tls.Config{RootCAs: certs,Certificates: []tls.Certificate{cert}}

    service := ":5555"
    tcpAddr, error := net.ResolveTCPAddr("tcp", service)
    if error != nil {
        fmt.Println("Error: Could not resolve address")
    } else {
        netListen, error := tls.Listen(tcpAddr.Network(), tcpAddr.String(), &sslConfig)
        if error != nil {
            fmt.Println(error)
        } else {
            defer netListen.Close()

            for {
                fmt.Println("Waiting for clients")
                connection, error := netListen.Accept()

I've tried switching around the order of the certs, not including some certs, etc. but the output from openssl s_client -CApath /etc/ssl/certs/ -connect localhost:5555 remains essentially the same, verify error:num=20:unable to get local issuer certificate. See here for full output. I seem to be doing something wrong with the intermediate certificates, but I have no idea what. I have been working on this for a few days, lots of googling and SO'ing, but nothing seemed to quite fit my situation. I have set up many certificates in Apache and HAProxy, but this really has me stumped.

  • 写回答

1条回答 默认 最新

  • dongzongzi0379 2015-03-06 22:14
    关注

    The RootCAs field is for clients verifying server certificates. I assume you only want to present a cert for verification, so anything you need should be loaded into the Certificates slice.

    Here is a minimal example:

    cert, err := tls.LoadX509KeyPair("example.com.pem", "example.com.key")
    if err != nil {
        log.Fatal("Error loading certificate. ", err)
    }
    
    tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}}
    
    listener, err := tls.Listen("tcp4", "127.0.0.1:5555", tlsCfg)
    if err != nil {
        log.Fatal(err)
    }
    defer listener.Close()
    
    for {
        log.Println("Waiting for clients")
        conn, err := listener.Accept()
        if err != nil {
            log.Fatal(err)
        }
        go handle(conn)
    }
    

    Even though you're not using HTTPS, it may still be useful to walk through the server setup starting at http.ListenAndServeTLS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站