创建并使用https证书

前言

https要比http更安全些,因此可以配置Nginx服务器使用证书,客户端就会去第三方平台校验证书。
但是我们自己的服务器和客户端只是想要加个密而已,也没必要跑去第三方平台校验证书,省钱方便。
因此研究了一下生成证书和使用证书的笔记。

产生证书

网上很多都是用openssl命令行去产生,有点麻烦,主要是不想记一堆命令,因此找到一个非常简单的项目。
项目地址:https://github.com/michaelklishin/tls-gen,需要环境有python3、openssl、make这三个工具。
使用方法cd tls-gen/basic & make CN=www.janbar.com,这样客户端必须用该域名进行访问,并且要带上对应证书。
最终产生如下三个文件,testca/cacert.pem是客户端使用,server/cert.pem,server/key.pem是服务器使用。

testca/
  cacert.pem

server/
  cert.pem
  key.pem

记得在c:\Windows\System32\drivers\etc\hosts增加127.0.0.1 www.janbar.com,准备证书源码文件cert.go,内容如下:


var (
    certPEMBlock = []byte(`-----BEGIN CERTIFICATE-----
MIIDfjCCAmagAwIBAgIBATANBgkqhkiG9w0BAQsFADAxMSAwHgYDVQQDDBdUTFNH
ZW5TZWxmU2lnbmVkdFJvb3RDQTENMAsGA1UEBwwEJCQkJDAeFw0yMTAxMjExMDEx
MjJaFw0zMTAxMTkxMDExMjJaMCoxFzAVBgNVBAMMDnd3dy5qYW5iYXIuY29tMQ8w
DQYDVQQKDAZzZXJ2ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDj
pGglwTB/B1N07ZhwaP3erxDZDYhtDqir7fiuZQxgmWzQ5UeU7gp5ntu8bLXpFmVS
s7si0WyOKbGoPi8Q/cvkZix3zkClAvS7vM2zW+0qa3Fp4dYRg42cjYAgenfpC0M5
fKHhnBKjzWFOiTOyJIPcNpvhAcV9dvQIdgX64g/+M2J3pMK2+tU5Z49Nc1KqyUS/
/zFo4C+HAk7Wbkc3Kgx8t4OZo0ddTdmLsPfRIU3hxqehRxhk7OccHBBb6JOVSF21
3h6kWwauN9djiOixpsS3jr1SVEGrhZk6zaOtZ+MSOg410pr3u79kdIuCYGNhvdDW
soyNB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ATzhxyglarRNrneZKM96KFm3c
z99HzxBxX71hKBF6kV6GKdKQkl4Ocx6HLiHiIWviTFr0qNVNcafrdDwWa9dRg5da
xKOOE9XoLYHWDmDaKgAIz4x5tDUDhAT4u7hkHQACz4TMlQG6L2uXPI0IiTv5185w
vgNNpx2V3d9rlvq6AOWjhSrZ9rfuSnt3UbRjKmbZAGcaFb3Gqr7MdIZuYOB9CN7H
o0wKNxKk00o7jav298tHinDdKvwYdAf8IgkEaHgBeFsPfNy8VGe4XWNdVT7HkzpO
PHM7SBK+cEpKnylKkEdKb9ubSCe8NA4RpuKbeo36IS6Bcfmr
-----END CERTIFICATE-----`)
)

测试https服务器

测试代码文件test_https.go如下:

package main

import (
    "crypto/tls"
    "crypto/x509"
    "io"
    "net/http"
    "os"
    "time"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir(".")))

    go func() {
        cf, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
        if err != nil {
            panic(err)
        }
        err = (&http.Server{TLSConfig: &tls.Config{
            Certificates: []tls.Certificate{cf},
        }}).ListenAndServeTLS("", "")
        if err != nil {
            panic(err)
        }
    }()
    time.Sleep(time.Second)
    // 上面是http服务器的运行

    // 下面是http客服端访问
    roots := x509.NewCertPool()
    ok := roots.AppendCertsFromPEM(rootPEM)
    if !ok {
        panic("failed to parse root certificate")
    }

    client := &http.Client{Transport: &http.Transport{
        TLSClientConfig: &tls.Config{RootCAs: roots},
    }}

    req, err := http.NewRequest(http.MethodGet, "https://www.janbar.com", nil)
    if err != nil {
        panic(err)
    }
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    io.Copy(os.Stdout, resp.Body)
    resp.Body.Close()
}

执行go run test_https.go cert.go效果如下,显示了运行目录下的文件列表:

<pre>
<a href="test.exe">test.exe</a>
<a href="test.go">test.go</a>
</pre>

用tls加密tcp连接

测试代码test_tcp.go文件,源码如下:

package main

import (
    "crypto/tls"
    "crypto/x509"
    "fmt"
    "net"
    "time"
)

func main() {
    addr := "www.janbar.com:8080"
    go func() {
        err := tcpServer(addr)
        if err != nil {
            panic(err)
        }
    }()
    time.Sleep(time.Second)
    err := tcpClient(addr)
    if err != nil {
        panic(err)
    }
}

func tcpServer(addr string) error {
    cf, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
    if err != nil {
        return err
    }
    s, err := tls.Listen("tcp", addr, &tls.Config{Certificates: []tls.Certificate{cf}})
    if err != nil {
        return err
    }
    defer s.Close()

    client := func(c net.Conn) error {
        tNow := time.Now().String()
        buf := make([]byte, len(tNow))
        n, err := c.Read(buf)
        if err != nil {
            return err
        }
        fmt.Printf("server [%s]\n", buf[:n])
        _, err = c.Write([]byte(tNow))
        return err
    }

    for {
        l, err := s.Accept()
        if err != nil {
            return err
        }
        go func(c net.Conn) {
            defer c.Close()
            if err := client(c); err != nil {
                fmt.Println(err)
            }
        }(l)
    }
}

func tcpClient(addr string) error {
    roots := x509.NewCertPool()
    ok := roots.AppendCertsFromPEM(rootPEM)
    if !ok {
        panic("failed to parse root certificate")
    }
    c, err := tls.Dial("tcp", addr, &tls.Config{RootCAs: roots})
    if err != nil {
        return err
    }
    defer c.Close()

    tNow := time.Now().String()
    c.Write([]byte(tNow))
    buf := make([]byte, len(tNow))
    n, err := c.Read(buf)
    if err != nil {
        return err
    }
    fmt.Printf("client [%s]\n", buf[:n])
    return nil
}

执行go run test_tcp.go cert.go效果如下:

server [2021-01-21 21:09:51.7942154 +0800 CST m=+1.011929701]
client [2021-01-21 21:09:51.7852425 +0800 CST m=+1.002956801]

总结

以前使用的tcp连接没有任何加密,现在想想还是不安全。用上证书,起码会校验访问域名和证书等信息,多少会安全一些。
以上都是用来测试等等,如果需要正式使用证书,还是去各大网站花钱申请吧。我一直用万网的免费证书,但是有效期只有一年额。

posted @ 2021-01-21 21:14  janbar  阅读(2455)  评论(0编辑  收藏  举报