tls.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package transport
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/tls"
  6. "crypto/x509"
  7. "encoding/pem"
  8. "math/big"
  9. "os"
  10. )
  11. func newCustomTLSKeyPair(certfile, keyfile string) (*tls.Certificate, error) {
  12. tlsCert, err := tls.LoadX509KeyPair(certfile, keyfile)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &tlsCert, nil
  17. }
  18. func newRandomTLSKeyPair() *tls.Certificate {
  19. key, err := rsa.GenerateKey(rand.Reader, 1024)
  20. if err != nil {
  21. panic(err)
  22. }
  23. template := x509.Certificate{SerialNumber: big.NewInt(1)}
  24. certDER, err := x509.CreateCertificate(
  25. rand.Reader,
  26. &template,
  27. &template,
  28. &key.PublicKey,
  29. key)
  30. if err != nil {
  31. panic(err)
  32. }
  33. keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
  34. certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
  35. tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
  36. if err != nil {
  37. panic(err)
  38. }
  39. return &tlsCert
  40. }
  41. // Only support one ca file to add
  42. func newCertPool(caPath string) (*x509.CertPool, error) {
  43. pool := x509.NewCertPool()
  44. caCrt, err := os.ReadFile(caPath)
  45. if err != nil {
  46. return nil, err
  47. }
  48. pool.AppendCertsFromPEM(caCrt)
  49. return pool, nil
  50. }
  51. func NewServerTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
  52. var base = &tls.Config{}
  53. if certPath == "" || keyPath == "" {
  54. // server will generate tls conf by itself
  55. cert := newRandomTLSKeyPair()
  56. base.Certificates = []tls.Certificate{*cert}
  57. } else {
  58. cert, err := newCustomTLSKeyPair(certPath, keyPath)
  59. if err != nil {
  60. return nil, err
  61. }
  62. base.Certificates = []tls.Certificate{*cert}
  63. }
  64. if caPath != "" {
  65. pool, err := newCertPool(caPath)
  66. if err != nil {
  67. return nil, err
  68. }
  69. base.ClientAuth = tls.RequireAndVerifyClientCert
  70. base.ClientCAs = pool
  71. }
  72. return base, nil
  73. }
  74. func NewClientTLSConfig(certPath, keyPath, caPath, serverName string) (*tls.Config, error) {
  75. var base = &tls.Config{}
  76. if certPath == "" || keyPath == "" {
  77. // client will not generate tls conf by itself
  78. } else {
  79. cert, err := newCustomTLSKeyPair(certPath, keyPath)
  80. if err != nil {
  81. return nil, err
  82. }
  83. base.Certificates = []tls.Certificate{*cert}
  84. }
  85. if caPath != "" {
  86. pool, err := newCertPool(caPath)
  87. if err != nil {
  88. return nil, err
  89. }
  90. base.RootCAs = pool
  91. base.ServerName = serverName
  92. base.InsecureSkipVerify = false
  93. } else {
  94. base.InsecureSkipVerify = true
  95. }
  96. return base, nil
  97. }