1
0

tlscert.go 6.9 KB

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