tlscert.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package tlscert
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "embed"
  6. "encoding/pem"
  7. "io"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "imuslab.com/zoraxy/mod/info/logger"
  13. "imuslab.com/zoraxy/mod/utils"
  14. )
  15. type CertCache struct {
  16. Cert *x509.Certificate
  17. PubKey string
  18. PriKey string
  19. }
  20. type Manager struct {
  21. CertStore string //Path where all the certs are stored
  22. LoadedCerts []*CertCache //A list of loaded certs
  23. Logger *logger.Logger //System wide logger for debug mesage
  24. verbal bool
  25. }
  26. //go:embed localhost.pem localhost.key
  27. var buildinCertStore embed.FS
  28. func NewManager(certStore string, verbal bool, logger *logger.Logger) (*Manager, error) {
  29. if !utils.FileExists(certStore) {
  30. os.MkdirAll(certStore, 0775)
  31. }
  32. pubKey := "./tmp/localhost.pem"
  33. priKey := "./tmp/localhost.key"
  34. //Check if this is initial setup
  35. if !utils.FileExists(pubKey) {
  36. buildInPubKey, _ := buildinCertStore.ReadFile(filepath.Base(pubKey))
  37. os.WriteFile(pubKey, buildInPubKey, 0775)
  38. }
  39. if !utils.FileExists(priKey) {
  40. buildInPriKey, _ := buildinCertStore.ReadFile(filepath.Base(priKey))
  41. os.WriteFile(priKey, buildInPriKey, 0775)
  42. }
  43. thisManager := Manager{
  44. CertStore: certStore,
  45. LoadedCerts: []*CertCache{},
  46. verbal: verbal,
  47. Logger: logger,
  48. }
  49. err := thisManager.UpdateLoadedCertList()
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &thisManager, nil
  54. }
  55. // Update domain mapping from file
  56. func (m *Manager) UpdateLoadedCertList() error {
  57. //Get a list of certificates from file
  58. domainList, err := m.ListCertDomains()
  59. if err != nil {
  60. return err
  61. }
  62. //Load each of the certificates into memory
  63. certList := []*CertCache{}
  64. for _, certname := range domainList {
  65. //Read their certificate into memory
  66. pubKey := filepath.Join(m.CertStore, certname+".pem")
  67. priKey := filepath.Join(m.CertStore, certname+".key")
  68. certificate, err := tls.LoadX509KeyPair(pubKey, priKey)
  69. if err != nil {
  70. m.Logger.PrintAndLog("tls-router", "Certificate load failed: "+certname, err)
  71. continue
  72. }
  73. for _, thisCert := range certificate.Certificate {
  74. loadedCert, err := x509.ParseCertificate(thisCert)
  75. if err != nil {
  76. //Error pasring cert, skip this byte segment
  77. m.Logger.PrintAndLog("tls-router", "Certificate parse failed: "+certname, err)
  78. continue
  79. }
  80. thisCacheEntry := CertCache{
  81. Cert: loadedCert,
  82. PubKey: pubKey,
  83. PriKey: priKey,
  84. }
  85. certList = append(certList, &thisCacheEntry)
  86. }
  87. }
  88. //Replace runtime cert array
  89. m.LoadedCerts = certList
  90. return nil
  91. }
  92. // Match cert by CN
  93. func (m *Manager) CertMatchExists(serverName string) bool {
  94. for _, certCacheEntry := range m.LoadedCerts {
  95. if certCacheEntry.Cert.VerifyHostname(serverName) == nil || certCacheEntry.Cert.Issuer.CommonName == serverName {
  96. return true
  97. }
  98. }
  99. return false
  100. }
  101. // Get cert entry by matching server name, return pubKey and priKey if found
  102. // check with CertMatchExists before calling to the load function
  103. func (m *Manager) GetCertByX509CNHostname(serverName string) (string, string) {
  104. for _, certCacheEntry := range m.LoadedCerts {
  105. if certCacheEntry.Cert.VerifyHostname(serverName) == nil || certCacheEntry.Cert.Issuer.CommonName == serverName {
  106. return certCacheEntry.PubKey, certCacheEntry.PriKey
  107. }
  108. }
  109. return "", ""
  110. }
  111. // Return a list of domains by filename
  112. func (m *Manager) ListCertDomains() ([]string, error) {
  113. filenames, err := m.ListCerts()
  114. if err != nil {
  115. return []string{}, err
  116. }
  117. //Remove certificates where there are missing public key or private key
  118. filenames = getCertPairs(filenames)
  119. return filenames, nil
  120. }
  121. // Return a list of cert files (public and private keys)
  122. func (m *Manager) ListCerts() ([]string, error) {
  123. certs, err := os.ReadDir(m.CertStore)
  124. if err != nil {
  125. return []string{}, err
  126. }
  127. filenames := make([]string, 0, len(certs))
  128. for _, cert := range certs {
  129. if !cert.IsDir() {
  130. filenames = append(filenames, cert.Name())
  131. }
  132. }
  133. return filenames, nil
  134. }
  135. // Get a certificate from disk where its certificate matches with the helloinfo
  136. func (m *Manager) GetCert(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, error) {
  137. //Check if the domain corrisponding cert exists
  138. pubKey := "./tmp/localhost.pem"
  139. priKey := "./tmp/localhost.key"
  140. if utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".pem")) && utils.FileExists(filepath.Join(m.CertStore, helloInfo.ServerName+".key")) {
  141. //Direct hit
  142. pubKey = filepath.Join(m.CertStore, helloInfo.ServerName+".pem")
  143. priKey = filepath.Join(m.CertStore, helloInfo.ServerName+".key")
  144. } else if m.CertMatchExists(helloInfo.ServerName) {
  145. //Use x509
  146. pubKey, priKey = m.GetCertByX509CNHostname(helloInfo.ServerName)
  147. } else {
  148. //Fallback to legacy method of matching certificates
  149. if m.DefaultCertExists() {
  150. //Use default.pem and default.key
  151. pubKey = filepath.Join(m.CertStore, "default.pem")
  152. priKey = filepath.Join(m.CertStore, "default.key")
  153. }
  154. }
  155. //Load the cert and serve it
  156. cer, err := tls.LoadX509KeyPair(pubKey, priKey)
  157. if err != nil {
  158. log.Println(err)
  159. return nil, nil
  160. }
  161. return &cer, nil
  162. }
  163. // Check if both the default cert public key and private key exists
  164. func (m *Manager) DefaultCertExists() bool {
  165. return utils.FileExists(filepath.Join(m.CertStore, "default.pem")) && utils.FileExists(filepath.Join(m.CertStore, "default.key"))
  166. }
  167. // Check if the default cert exists returning seperate results for pubkey and prikey
  168. func (m *Manager) DefaultCertExistsSep() (bool, bool) {
  169. return utils.FileExists(filepath.Join(m.CertStore, "default.pem")), utils.FileExists(filepath.Join(m.CertStore, "default.key"))
  170. }
  171. // Delete the cert if exists
  172. func (m *Manager) RemoveCert(domain string) error {
  173. pubKey := filepath.Join(m.CertStore, domain+".pem")
  174. priKey := filepath.Join(m.CertStore, domain+".key")
  175. if utils.FileExists(pubKey) {
  176. err := os.Remove(pubKey)
  177. if err != nil {
  178. return err
  179. }
  180. }
  181. if utils.FileExists(priKey) {
  182. err := os.Remove(priKey)
  183. if err != nil {
  184. return err
  185. }
  186. }
  187. //Update the cert list
  188. m.UpdateLoadedCertList()
  189. return nil
  190. }
  191. // Check if the given file is a valid TLS file
  192. func IsValidTLSFile(file io.Reader) bool {
  193. // Read the contents of the uploaded file
  194. contents, err := io.ReadAll(file)
  195. if err != nil {
  196. // Handle the error
  197. return false
  198. }
  199. // Parse the contents of the file as a PEM-encoded certificate or key
  200. block, _ := pem.Decode(contents)
  201. if block == nil {
  202. // The file is not a valid PEM-encoded certificate or key
  203. return false
  204. }
  205. // Parse the certificate or key
  206. if strings.Contains(block.Type, "CERTIFICATE") {
  207. // The file contains a certificate
  208. cert, err := x509.ParseCertificate(block.Bytes)
  209. if err != nil {
  210. // Handle the error
  211. return false
  212. }
  213. // Check if the certificate is a valid TLS/SSL certificate
  214. return !cert.IsCA && cert.KeyUsage&x509.KeyUsageDigitalSignature != 0 && cert.KeyUsage&x509.KeyUsageKeyEncipherment != 0
  215. } else if strings.Contains(block.Type, "PRIVATE KEY") {
  216. // The file contains a private key
  217. _, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  218. return err == nil
  219. } else {
  220. return false
  221. }
  222. }