/
cert.go
68 lines (52 loc) · 1.43 KB
/
cert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package rpcserver
import (
"crypto/tls"
"github.com/BoltzExchange/boltz-lnd/logger"
"github.com/BoltzExchange/boltz-lnd/utils"
"github.com/lightningnetwork/lnd/cert"
"os"
"time"
)
func loadCertificate(
tlsCertPath string,
tlsKeyPath string,
tlsDisableAutofill bool,
) (*tls.Config, error) {
if !utils.FileExists(tlsCertPath) || !utils.FileExists(tlsKeyPath) {
logger.Warning("Could not find TLS certificate or key")
logger.Info("Generating new TLS certificate and key")
defaultAutogenValidity := 14 * 30 * 24 * time.Hour
certBytes, keyBytes, err := cert.GenCertPair(
"boltz",
[]string{},
[]string{},
tlsDisableAutofill,
defaultAutogenValidity,
)
if err != nil {
return nil, err
}
err = cert.WriteCertPair(tlsCertPath, tlsKeyPath, certBytes, keyBytes)
if err != nil {
return nil, err
}
}
certData, x590cert, err := cert.LoadCert(tlsCertPath, tlsKeyPath)
logger.Info("Loaded TLS certificate and key")
if err != nil {
return nil, err
}
if isOutdated, _ := cert.IsOutdated(x590cert, []string{}, []string{}, tlsDisableAutofill); isOutdated {
logger.Warning("TLS certificate is outdated. Removing files and generating new one")
err := os.Remove(tlsCertPath)
if err != nil {
return nil, err
}
err = os.Remove(tlsKeyPath)
if err != nil {
return nil, err
}
return loadCertificate(tlsCertPath, tlsKeyPath, tlsDisableAutofill)
}
return cert.TLSConfFromCert(certData), nil
}