1
0

tlscert.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package tlscert
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "encoding/pem"
  6. "io"
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "imuslab.com/arozos/ReverseProxy/mod/utils"
  13. )
  14. type Manager struct {
  15. CertStore string
  16. }
  17. func NewManager(certStore string) (*Manager, error) {
  18. if !utils.FileExists(certStore) {
  19. os.MkdirAll(certStore, 0775)
  20. }
  21. thisManager := Manager{
  22. CertStore: certStore,
  23. }
  24. return &thisManager, nil
  25. }
  26. func (m *Manager) ListCertDomains() ([]string, error) {
  27. filenames, err := m.ListCerts()
  28. if err != nil {
  29. return []string{}, err
  30. }
  31. //Remove certificates where there are missing public key or private key
  32. filenames = getCertPairs(filenames)
  33. return filenames, nil
  34. }
  35. func (m *Manager) ListCerts() ([]string, error) {
  36. certs, err := ioutil.ReadDir(m.CertStore)
  37. if err != nil {
  38. return []string{}, err
  39. }
  40. filenames := make([]string, 0, len(certs))
  41. for _, cert := range certs {
  42. if !cert.IsDir() {
  43. filenames = append(filenames, cert.Name())
  44. }
  45. }
  46. return filenames, nil
  47. }
  48. func (m *Manager) GetCert(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, error) {
  49. //Check if the domain corrisponding cert exists
  50. pubKey := "./system/localhost.crt"
  51. priKey := "./system/localhost.key"
  52. if utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".crt")) && utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".key")) {
  53. pubKey = filepath.Join(m.CertStore, helloInfo.ServerName+".crt")
  54. priKey = filepath.Join(m.CertStore, helloInfo.ServerName+".key")
  55. } else {
  56. domainCerts, _ := m.ListCertDomains()
  57. cloestDomainCert := matchClosestDomainCertificate(helloInfo.ServerName, domainCerts)
  58. if cloestDomainCert != "" {
  59. //There is a matching parent domain for this subdomain. Use this instead.
  60. pubKey = filepath.Join(m.CertStore, cloestDomainCert+".crt")
  61. priKey = filepath.Join(m.CertStore, cloestDomainCert+".key")
  62. } else {
  63. log.Println("Matching certificate not found. Serving with default. Domain: ", helloInfo.ServerName)
  64. }
  65. }
  66. cer, err := tls.LoadX509KeyPair(pubKey, priKey)
  67. if err != nil {
  68. log.Println(err)
  69. return nil, nil
  70. }
  71. return &cer, nil
  72. }
  73. //Check if the given file is a valid TLS file
  74. func isValidTLSFile(file io.Reader) bool {
  75. // Read the contents of the uploaded file
  76. contents, err := io.ReadAll(file)
  77. if err != nil {
  78. // Handle the error
  79. return false
  80. }
  81. // Parse the contents of the file as a PEM-encoded certificate or key
  82. block, _ := pem.Decode(contents)
  83. if block == nil {
  84. // The file is not a valid PEM-encoded certificate or key
  85. return false
  86. }
  87. // Parse the certificate or key
  88. if strings.Contains(block.Type, "CERTIFICATE") {
  89. // The file contains a certificate
  90. cert, err := x509.ParseCertificate(block.Bytes)
  91. if err != nil {
  92. // Handle the error
  93. return false
  94. }
  95. // Check if the certificate is a valid TLS/SSL certificate
  96. return cert.IsCA == false && cert.KeyUsage&x509.KeyUsageDigitalSignature != 0 && cert.KeyUsage&x509.KeyUsageKeyEncipherment != 0
  97. } else if strings.Contains(block.Type, "PRIVATE KEY") {
  98. // The file contains a private key
  99. _, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  100. if err != nil {
  101. // Handle the error
  102. return false
  103. }
  104. return true
  105. } else {
  106. return false
  107. }
  108. }