File size: 833 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package sysapi

import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"os"
)

// NewTLS will generate a new `*tls.Config` for use with a client or server.
func NewTLS(caFile, certFile, keyFile string) (*tls.Config, error) {
	tlsCfg := &tls.Config{ServerName: "GoAlert"}

	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, err
	}

	tlsCfg.Certificates = append(tlsCfg.Certificates, cert)

	if caFile != "" {
		// If CA file is specified, require client auth
		caBytes, err := os.ReadFile(caFile)
		if err != nil {
			return nil, err
		}

		pool := x509.NewCertPool()
		if !pool.AppendCertsFromPEM(caBytes) {
			return nil, errors.New("failed to append CA certs from PEM")
		}

		tlsCfg.ClientAuth = tls.RequireAndVerifyClientCert
		tlsCfg.RootCAs = pool
		tlsCfg.ClientCAs = pool
	}

	return tlsCfg, nil
}