tls.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transport
  15. import (
  16. "crypto/rand"
  17. "crypto/rsa"
  18. "crypto/tls"
  19. "crypto/x509"
  20. "encoding/pem"
  21. "math/big"
  22. "os"
  23. )
  24. func newCustomTLSKeyPair(certfile, keyfile string) (*tls.Certificate, error) {
  25. tlsCert, err := tls.LoadX509KeyPair(certfile, keyfile)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &tlsCert, nil
  30. }
  31. func newRandomTLSKeyPair() *tls.Certificate {
  32. key, err := rsa.GenerateKey(rand.Reader, 2048)
  33. if err != nil {
  34. panic(err)
  35. }
  36. template := x509.Certificate{SerialNumber: big.NewInt(1)}
  37. certDER, err := x509.CreateCertificate(
  38. rand.Reader,
  39. &template,
  40. &template,
  41. &key.PublicKey,
  42. key)
  43. if err != nil {
  44. panic(err)
  45. }
  46. keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
  47. certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
  48. tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
  49. if err != nil {
  50. panic(err)
  51. }
  52. return &tlsCert
  53. }
  54. // Only support one ca file to add
  55. func newCertPool(caPath string) (*x509.CertPool, error) {
  56. pool := x509.NewCertPool()
  57. caCrt, err := os.ReadFile(caPath)
  58. if err != nil {
  59. return nil, err
  60. }
  61. pool.AppendCertsFromPEM(caCrt)
  62. return pool, nil
  63. }
  64. func NewServerTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
  65. base := &tls.Config{}
  66. if certPath == "" || keyPath == "" {
  67. // server will generate tls conf by itself
  68. cert := newRandomTLSKeyPair()
  69. base.Certificates = []tls.Certificate{*cert}
  70. } else {
  71. cert, err := newCustomTLSKeyPair(certPath, keyPath)
  72. if err != nil {
  73. return nil, err
  74. }
  75. base.Certificates = []tls.Certificate{*cert}
  76. }
  77. if caPath != "" {
  78. pool, err := newCertPool(caPath)
  79. if err != nil {
  80. return nil, err
  81. }
  82. base.ClientAuth = tls.RequireAndVerifyClientCert
  83. base.ClientCAs = pool
  84. }
  85. return base, nil
  86. }
  87. func NewClientTLSConfig(certPath, keyPath, caPath, serverName string) (*tls.Config, error) {
  88. base := &tls.Config{}
  89. if certPath != "" && keyPath != "" {
  90. cert, err := newCustomTLSKeyPair(certPath, keyPath)
  91. if err != nil {
  92. return nil, err
  93. }
  94. base.Certificates = []tls.Certificate{*cert}
  95. }
  96. base.ServerName = serverName
  97. if caPath != "" {
  98. pool, err := newCertPool(caPath)
  99. if err != nil {
  100. return nil, err
  101. }
  102. base.RootCAs = pool
  103. base.InsecureSkipVerify = false
  104. } else {
  105. base.InsecureSkipVerify = true
  106. }
  107. return base, nil
  108. }
  109. func NewRandomPrivateKey() ([]byte, error) {
  110. key, err := rsa.GenerateKey(rand.Reader, 2048)
  111. if err != nil {
  112. return nil, err
  113. }
  114. keyPEM := pem.EncodeToMemory(&pem.Block{
  115. Type: "RSA PRIVATE KEY",
  116. Bytes: x509.MarshalPKCS1PrivateKey(key),
  117. })
  118. return keyPEM, nil
  119. }