From 12069dc21355689a092e6a8c6e345c54175d0b2d Mon Sep 17 00:00:00 2001 From: Apoorv Deshmukh Date: Tue, 5 Sep 2023 17:45:05 +0530 Subject: [PATCH] Add support for DER certificates --- msdsn/conn_str.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/msdsn/conn_str.go b/msdsn/conn_str.go index 549b3963..2bdddb57 100644 --- a/msdsn/conn_str.go +++ b/msdsn/conn_str.go @@ -3,12 +3,13 @@ package msdsn import ( "crypto/tls" "crypto/x509" + "encoding/pem" "errors" "fmt" - "io/ioutil" "net" "net/url" "os" + "path/filepath" "strconv" "strings" "time" @@ -129,6 +130,37 @@ type Config struct { ColumnEncryption bool } +func readDERFile(filename string) ([]byte, error) { + derBytes, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + + cert, err := x509.ParseCertificate(derBytes) + if err != nil { + return nil, err + } + + pemBytes := pem.EncodeToMemory(&pem.Block{ + Type: "CERTIFICATE", + Bytes: cert.Raw, + }) + return pemBytes, nil +} + +func readCertificate(certificate string) ([]byte, error) { + certType := strings.ToLower(filepath.Ext(certificate)) + + switch certType { + case ".pem": + return os.ReadFile(certificate) + case ".der": + return readDERFile(certificate) + default: + return nil, fmt.Errorf("certificate type %s is not supported", certType) + } +} + // Build a tls.Config object from the supplied certificate. func SetupTLS(certificate string, insecureSkipVerify bool, hostInCertificate string, minTLSVersion string) (*tls.Config, error) { config := tls.Config{ @@ -146,7 +178,7 @@ func SetupTLS(certificate string, insecureSkipVerify bool, hostInCertificate str if len(certificate) == 0 { return &config, nil } - pem, err := ioutil.ReadFile(certificate) + pem, err := readCertificate(certificate) if err != nil { return nil, fmt.Errorf("cannot read certificate %q: %w", certificate, err) }