Debian Codegraph @

cmd/atomdns/handlers/global/setup_tls.go

raw
1 package global
2
3 import (
4 "crypto/tls"
5 "crypto/x509"
6 "fmt"
7 "net/mail"
8 "net/url"
9 "os"
10 "path/filepath"
11
12 "codeberg.org/miekg/dns/cmd/atomdns/internal/conffile"
13 "codeberg.org/miekg/dns/cmd/atomdns/internal/zlog"
14 "codeberg.org/miekg/dns/dnshttp"
15 "github.com/caddyserver/certmagic"
16 )
17
18 func (g *Global) SetupTLS(d *conffile.Dispenser) error {
19 certmagic.DefaultACME.Profile = "shortlived" // https://letsencrypt.org/2025/01/16/6-day-and-ip-certs
20 certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA
21 certmagic.DefaultACME.Logger = zlog.New(g.Debug)
22
23 for d.NextBlock(0) {
24 switch d.Val() {
25 case "cert":
26 args := d.RemainingArgs() // we don't have co.RemainingPaths there
27 if len(args) != 2 {
28 return d.ArgErr()
29 }
30 if !filepath.IsAbs(args[0]) {
31 args[0] = filepath.Join(g.Root, args[0])
32 }
33 if !filepath.IsAbs(args[1]) {
34 args[1] = filepath.Join(g.Root, args[1])
35 }
36 cert, err := tls.LoadX509KeyPair(args[0], args[1])
37 if err != nil {
38 return fmt.Errorf("could not load TLS certificate pair: %s", err)
39 }
40 if g.TlsConfig != nil {
41 g.TlsConfig.Certificates = []tls.Certificate{cert}
42 g.TlsConfig.NextProtos = dnshttp.NextProtos
43 } else {
44 g.TlsConfig = &tls.Config{
45 Certificates: []tls.Certificate{cert},
46 NextProtos: dnshttp.NextProtos,
47 MinVersion: tls.VersionTLS12,
48 }
49 }
50 case "rootca":
51 var roots *x509.CertPool
52 args := d.RemainingArgs() // we don't have co.RemainingPaths in d
53 if len(args) != 1 {
54 return d.ArgErr()
55 }
56 if !filepath.IsAbs(args[0]) {
57 args[0] = filepath.Join(g.Root, args[0])
58 }
59 roots, err := loadCA(args[0])
60 if err != nil {
61 return d.PropErr(err)
62 }
63 certmagic.DefaultACME.TrustedRoots = roots
64 if g.TlsConfig != nil {
65 g.TlsConfig.NextProtos = dnshttp.NextProtos
66 g.TlsConfig.RootCAs = roots
67 } else {
68 g.TlsConfig = &tls.Config{
69 NextProtos: dnshttp.NextProtos,
70 RootCAs: roots,
71 MinVersion: tls.VersionTLS12,
72 }
73 }
74 case "contact":
75 args := d.RemainingArgs()
76 if len(args) != 1 {
77 return d.ArgErr()
78 }
79 if _, err := mail.ParseAddress(args[0]); err != nil {
80 return d.PropErr(err)
81 }
82 g.TlsContact = args[0]
83 certmagic.DefaultACME.Email = g.TlsContact
84 certmagic.DefaultACME.Agreed = true
85 case "directory":
86 args := d.RemainingArgs()
87 if len(args) != 1 {
88 return d.ArgErr()
89 }
90 if !filepath.IsAbs(args[0]) {
91 args[0] = filepath.Join(g.Root, args[0])
92 }
93 g.TlsPath = args[0]
94 certmagic.Default.Storage = &certmagic.FileStorage{Path: g.TlsPath}
95 case "source":
96 args, err := d.RemainingIPs()
97 if err != nil {
98 return d.PropErr(err)
99 }
100 g.TlsIPs = args
101 case "ca":
102 args := d.RemainingArgs()
103 if len(args) != 1 {
104 return d.ArgErr()
105 }
106 if _, err := url.Parse(args[0]); err != nil {
107 return d.PropErr(err)
108 }
109 certmagic.DefaultACME.CA = args[0]
110 default:
111 return d.ArgErr()
112 }
113 }
114 return nil
115 }
116
117 func loadCA(path string) (*x509.CertPool, error) {
118 roots := x509.NewCertPool()
119 pem, err := os.ReadFile(path)
120 if err != nil {
121 return nil, err
122 }
123 ok := roots.AppendCertsFromPEM(pem)
124 if !ok {
125 return nil, fmt.Errorf("could not read root certificate: %s", err)
126 }
127 return roots, nil
128 }