certificate loading

This commit is contained in:
avaksru 2026-03-18 13:04:37 +03:00
parent 2960c391a9
commit a872efdb2e

View file

@ -3,8 +3,10 @@ package mqtt
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"crypto/x509"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"strings" "strings"
"time" "time"
@ -85,14 +87,29 @@ func (c *MQTTChannel) Start(ctx context.Context) error {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
InsecureSkipVerify: false, InsecureSkipVerify: false,
} }
// Load CA certificate if provided
if c.config.TLSCA != "" { if c.config.TLSCA != "" {
// Load CA cert if provided caCert, err := os.ReadFile(c.config.TLSCA)
// For simplicity, assuming file path if err != nil {
// In production, load cert properly return fmt.Errorf("failed to read CA certificate: %w", err)
} }
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
return fmt.Errorf("failed to parse CA certificate")
}
tlsConfig.RootCAs = caCertPool
}
// Load client certificate and key if provided
if c.config.TLSCert != "" && c.config.TLSKey != "" { if c.config.TLSCert != "" && c.config.TLSKey != "" {
// Load client cert cert, err := tls.LoadX509KeyPair(c.config.TLSCert, c.config.TLSKey)
if err != nil {
return fmt.Errorf("failed to load client certificate and key: %w", err)
} }
tlsConfig.Certificates = []tls.Certificate{cert}
}
opts.SetTLSConfig(tlsConfig) opts.SetTLSConfig(tlsConfig)
} }