@@ -24,7 +24,7 @@ type Backend struct {
2424}
2525
2626// NewBackend creates a new Backend.
27- func NewBackend (server , username , password , cafile string ) (* Backend , error ) {
27+ func NewBackend (server , username , password , cafile , certFile , certKeyFile string ) (* Backend , error ) {
2828 b := Backend {
2929 txPacketChan : make (chan gw.TXPacketBytes ),
3030 gateways : make (map [lorawan.EUI64 ]struct {}),
@@ -37,11 +37,16 @@ func NewBackend(server, username, password, cafile string) (*Backend, error) {
3737 opts .SetOnConnectHandler (b .onConnected )
3838 opts .SetConnectionLostHandler (b .onConnectionLost )
3939
40- if cafile != "" {
41- tlsconfig , err := NewTLSConfig (cafile )
42- if err == nil {
43- opts .SetTLSConfig (tlsconfig )
44- }
40+ tlsconfig , err := NewTLSConfig (cafile , certFile , certKeyFile )
41+ if err != nil {
42+ log .WithError (err ).WithFields (log.Fields {
43+ "ca_cert" : cafile ,
44+ "tls_cert" : certFile ,
45+ "tls_key" : certKeyFile ,
46+ }).Fatal ("error loading mqtt certificate files" )
47+ }
48+ if tlsconfig != nil {
49+ opts .SetTLSConfig (tlsconfig )
4550 }
4651
4752 log .WithField ("server" , server ).Info ("backend: connecting to mqtt broker" )
@@ -54,23 +59,43 @@ func NewBackend(server, username, password, cafile string) (*Backend, error) {
5459}
5560
5661// NewTLSConfig returns the TLS configuration.
57- func NewTLSConfig (cafile string ) (* tls.Config , error ) {
62+ func NewTLSConfig (cafile , certFile , certKeyFile string ) (* tls.Config , error ) {
63+ // Here are three valid options:
64+ // - Only CA
65+ // - TLS cert + key
66+ // - CA, TLS cert + key
67+
68+ if cafile == "" && certFile == "" && certKeyFile == "" {
69+ log .Info ("backend: TLS config is empty" )
70+ return nil , nil
71+ }
72+
73+ tlsConfig := & tls.Config {}
74+
5875 // Import trusted certificates from CAfile.pem.
76+ if cafile != "" {
77+ cacert , err := ioutil .ReadFile (cafile )
78+ if err != nil {
79+ log .Errorf ("backend: couldn't load cafile: %s" , err )
80+ return nil , err
81+ }
82+ certpool := x509 .NewCertPool ()
83+ certpool .AppendCertsFromPEM (cacert )
5984
60- cert , err := ioutil .ReadFile (cafile )
61- if err != nil {
62- log .Errorf ("backend: couldn't load cafile: %s" , err )
63- return nil , err
85+ tlsConfig .RootCAs = certpool // RootCAs = certs used to verify server cert.
6486 }
6587
66- certpool := x509 .NewCertPool ()
67- certpool .AppendCertsFromPEM (cert )
88+ // Import certificate and the key
89+ if certFile != "" && certKeyFile != "" {
90+ kp , err := tls .LoadX509KeyPair (certFile , certKeyFile )
91+ if err != nil {
92+ log .Errorf ("backend: couldn't load MQTT TLS key pair: %s" , err )
93+ return nil , err
94+ }
95+ tlsConfig .Certificates = []tls.Certificate {kp }
96+ }
6897
69- // Create tls.Config with desired tls properties
70- return & tls.Config {
71- // RootCAs = certs used to verify server cert.
72- RootCAs : certpool ,
73- }, nil
98+ return tlsConfig , nil
7499}
75100
76101// Close closes the backend.
0 commit comments