• golang使用TLSv1.2证书


    golang使用TLSv1.2证书创建安全服务器.

    http server

    package main
    
    
    import (
    	"crypto/tls"
    	"log"
    	"net/http"
    	"time"
    )
    
    func main() {
    // crt file
    certPem := []byte(`-----BEGIN CERTIFICATE-----
    MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
    DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
    EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
    7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
    5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
    BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
    NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
    Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
    6MF9+Yw1Yy0t
    -----END CERTIFICATE-----`)
    // private key file
    	keyPem := []byte(`-----BEGIN EC PRIVATE KEY-----
    MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
    AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
    EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
    -----END EC PRIVATE KEY-----`)
    	cert, err := tls.X509KeyPair(certPem, keyPem)
    	if err != nil {
    		log.Fatal(err)
    	}
    	cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
    	srv := &http.Server{
    		TLSConfig:    cfg,
    		ReadTimeout:  time.Minute,
    		WriteTimeout: time.Minute,
    	}
    	log.Fatal(srv.ListenAndServeTLS("", "")) // => listen on 443
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    tcp server

    package main
    
    import (
    	"crypto/tls"
    	"log"
    )
    
    func main() {
    	certPem := []byte(`-----BEGIN CERTIFICATE-----
    MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
    DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
    EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
    7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
    5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
    BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
    NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
    Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
    6MF9+Yw1Yy0t
    -----END CERTIFICATE-----`)
    	keyPem := []byte(`-----BEGIN EC PRIVATE KEY-----
    MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
    AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
    EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
    -----END EC PRIVATE KEY-----`)
    	cert, err := tls.X509KeyPair(certPem, keyPem)
    	if err != nil {
    		log.Fatal(err)
    	}
    	cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
    	listener, err := tls.Listen("tcp", ":2000", cfg)
    	if err != nil {
    		log.Fatal(err)
    	}
    	_ = listener
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    生成SSL证书

    bash

    # signed.key 私钥文件
    # signed.crt 证书文件
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout signed.key -out signed.crt
    
    • 1
    • 2
    • 3

    golang API

    func generateSelfSigned(host string) ([]byte,[]byte,error){
    	validFrom := time.Now().Add(-time.Hour) // valid an hour earlier to avoid flakes due to clock skew
    	maxAge := time.Hour * 24 * 365          // one year self-signed certs
    	caKey, err := rsa.GenerateKey(rand.Reader, 2048)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	caTemplate := x509.Certificate{
    		SerialNumber: big.NewInt(1),
    		Subject: pkix.Name{
    			CommonName: fmt.Sprintf("%s-ca@%d", host, time.Now().Unix()),
    		},
    		NotBefore: validFrom,
    		NotAfter:  validFrom.Add(maxAge),
    
    		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
    		BasicConstraintsValid: true,
    		IsCA:                  true,
    	}
    	caDERBytes, err := x509.CreateCertificate(rand.Reader, &caTemplate, &caTemplate, &caKey.PublicKey, caKey)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	// Generate cert, followed by ca
    	certBuffer := bytes.Buffer{}
    	if err := pem.Encode(&certBuffer, &pem.Block{Type: CertificateBlockType, Bytes: caDERBytes}); err != nil {
    		return nil, nil, err
    	}
    
    	// Generate key
    	keyBuffer := bytes.Buffer{}
    	if err := pem.Encode(&keyBuffer, &pem.Block{Type: RSAPrivateKeyBlockType, Bytes: x509.MarshalPKCS1PrivateKey(caKey)}); err != nil {
    		return nil, nil, err
    	}
            // cA证书,私钥
    	return certBuffer.Bytes(),keyBuffer.Bytes(),nil
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    综合案例:

    一个使用SSL证书的TCP简单服务器

    package main
    
    import (
    	"bytes"
    	"crypto/rand"
    	"crypto/rsa"
    	"crypto/tls"
    	"crypto/x509"
    	"crypto/x509/pkix"
    	"encoding/pem"
    	"fmt"
    	"log"
    	"math/big"
    	"net"
    	"os"
    	"time"
    )
    const (
    	// CertificateBlockType is a possible value for pem.Block.Type.
    	CertificateBlockType = "CERTIFICATE"
    	// CertificateRequestBlockType is a possible value for pem.Block.Type.
    	CertificateRequestBlockType = "CERTIFICATE REQUEST"
    )
    const (
    	// ECPrivateKeyBlockType is a possible value for pem.Block.Type.
    	ECPrivateKeyBlockType = "EC PRIVATE KEY"
    	// RSAPrivateKeyBlockType is a possible value for pem.Block.Type.
    	RSAPrivateKeyBlockType = "RSA PRIVATE KEY"
    	// PrivateKeyBlockType is a possible value for pem.Block.Type.
    	PrivateKeyBlockType = "PRIVATE KEY"
    	// PublicKeyBlockType is a possible value for pem.Block.Type.
    	PublicKeyBlockType = "PUBLIC KEY"
    )
    
    
    func main() {
    	certPem, keyPem,err:=generateSelfSigned("localhost")
    	if err != nil {
    		log.Fatal("failed to generate self-signed cert")
    	}
    	fmt.Println("ca file content:")
    	fmt.Println(string(certPem))
    	fmt.Println("private-key file content:")
    	fmt.Println(string(keyPem))
    
            
    	cert, err := tls.X509KeyPair(certPem, keyPem)
    	if err != nil {
    		log.Fatal(err)
    	}
    	cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
    	listener, err := tls.Listen("tcp", ":2000", cfg)
    	if err != nil {
    		log.Fatal(err)
    	}
    	for {
    		conn, err := listener.Accept()
    		if err != nil {
    			log.Fatal(err)
    			os.Exit(1)
    		}
                    // create a goroutine to serve each incoming request 
    		go handleRequest(conn)
    	}
    
    }
    func handleRequest(conn net.Conn) {
    	// incoming request
    	buffer := make([]byte, 1024)
    	_, err := conn.Read(buffer)
    	if err != nil {
    		log.Fatal(err)
    	}
    	// write data to response
    	time := time.Now().Format(time.ANSIC)
    	responseStr := fmt.Sprintf("Your message is: %v. \n Received time: %v", string(buffer[:]), time)
    	conn.Write([]byte(responseStr))
    
    	// close conn
    	conn.Close()
    }
    
    func generateSelfSigned(host string) ([]byte,[]byte,error){
    	validFrom := time.Now().Add(-time.Hour) // valid an hour earlier to avoid flakes due to clock skew
    	maxAge := time.Hour * 24 * 365          // one year self-signed certs
    	caKey, err := rsa.GenerateKey(rand.Reader, 2048)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	caTemplate := x509.Certificate{
    		SerialNumber: big.NewInt(1),
    		Subject: pkix.Name{
    			CommonName: fmt.Sprintf("%s-ca@%d", host, time.Now().Unix()),
    		},
    		NotBefore: validFrom,
    		NotAfter:  validFrom.Add(maxAge),
    
    		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
    		BasicConstraintsValid: true,
    		IsCA:                  true,
    	}
    	caDERBytes, err := x509.CreateCertificate(rand.Reader, &caTemplate, &caTemplate, &caKey.PublicKey, caKey)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	// Generate cert, followed by ca
    	certBuffer := bytes.Buffer{}
    	if err := pem.Encode(&certBuffer, &pem.Block{Type: CertificateBlockType, Bytes: caDERBytes}); err != nil {
    		return nil, nil, err
    	}
    
    	// Generate key
    	keyBuffer := bytes.Buffer{}
    	if err := pem.Encode(&keyBuffer, &pem.Block{Type: RSAPrivateKeyBlockType, Bytes: x509.MarshalPKCS1PrivateKey(caKey)}); err != nil {
    		return nil, nil, err
    	}
    
    	return certBuffer.Bytes(),keyBuffer.Bytes(),nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    curl测试:

    [oneslide1@localhost context]$ curl -k https://localhost:2000
    Your message is: GET / HTTP/1.1
    User-Agent: curl/7.29.0
    Host: localhost:2000
    Accept: */*
    . 
     Received time: Mon Nov  7 18:47:05 2022[
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Linux C编译器从零开发一
    【Verilog】inout 端口信号的使用
    今日准备注销CSDN专栏
    Linux常用基本命令详解(一)
    (黑马出品_03)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
    快速了解 Java 类加载机制 & 双亲委派
    分别用Python和Go实现对文件夹及其子文件夹里的文件进行批量重命名
    GitHub上250K Stars阿里首发Java并发编程
    “通用大模型”趋势下,AI未来当如何?
    2022年认证杯SPSSPRO杯数学建模B题(第一阶段)唐宋诗的定量分析与比较研究求解全过程文档及程序
  • 原文地址:https://blog.csdn.net/qq_33745102/article/details/127736837