forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Rahul Somasundaram <[email protected]>
- Loading branch information
Showing
8 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package aghtls_test | ||
|
||
import ( | ||
"crypto/tls" | ||
"testing" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghtest" | ||
"github.com/AdguardTeam/AdGuardHome/internal/aghtls" | ||
"github.com/AdguardTeam/golibs/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
aghtest.DiscardLogOutput(m) | ||
} | ||
|
||
func TestParseCiphers(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
wantErrMsg string | ||
want []uint16 | ||
in []string | ||
}{{ | ||
name: "nil", | ||
wantErrMsg: "", | ||
want: nil, | ||
in: nil, | ||
}, { | ||
name: "empty", | ||
wantErrMsg: "", | ||
want: []uint16{}, | ||
in: []string{}, | ||
}, {}, { | ||
name: "one", | ||
wantErrMsg: "", | ||
want: []uint16{tls.TLS_AES_128_GCM_SHA256}, | ||
in: []string{"TLS_AES_128_GCM_SHA256"}, | ||
}, { | ||
name: "several", | ||
wantErrMsg: "", | ||
want: []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384}, | ||
in: []string{"TLS_AES_128_GCM_SHA256", "TLS_AES_256_GCM_SHA384"}, | ||
}, { | ||
name: "bad", | ||
wantErrMsg: `unknown cipher "bad_cipher"`, | ||
want: nil, | ||
in: []string{"bad_cipher"}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := aghtls.ParseCiphers(tc.in) | ||
testutil.AssertErrorMsg(t, tc.wantErrMsg, err) | ||
assert.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package aghtls | ||
|
||
import ( | ||
"crypto/x509" | ||
) | ||
|
||
// SystemRootCAs tries to load root certificates from the operating system. It | ||
// returns nil in case nothing is found so that Go' crypto/x509 can use its | ||
// default algorithm to find system root CA list. | ||
// | ||
// See https://github.com/AdguardTeam/AdGuardHome/issues/1311. | ||
func SystemRootCAs() (roots *x509.CertPool) { | ||
return rootCAs() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build linux | ||
|
||
package aghtls | ||
|
||
import ( | ||
"crypto/x509" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/AdguardTeam/golibs/errors" | ||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
func rootCAs() (roots *x509.CertPool) { | ||
// Directories with the system root certificates, which aren't supported by | ||
// Go's crypto/x509. | ||
dirs := []string{ | ||
// Entware. | ||
"/opt/etc/ssl/certs", | ||
} | ||
|
||
roots = x509.NewCertPool() | ||
for _, dir := range dirs { | ||
dirEnts, err := os.ReadDir(dir) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
continue | ||
} | ||
|
||
// TODO(a.garipov): Improve error handling here and in other places. | ||
log.Error("aghtls: opening directory %q: %s", dir, err) | ||
} | ||
|
||
var rootsAdded bool | ||
for _, de := range dirEnts { | ||
var certData []byte | ||
rootFile := filepath.Join(dir, de.Name()) | ||
certData, err = os.ReadFile(rootFile) | ||
if err != nil { | ||
log.Error("aghtls: reading root cert: %s", err) | ||
} else { | ||
if roots.AppendCertsFromPEM(certData) { | ||
rootsAdded = true | ||
} else { | ||
log.Error("aghtls: could not add root from %q", rootFile) | ||
} | ||
} | ||
} | ||
|
||
if rootsAdded { | ||
return roots | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !linux | ||
|
||
package aghtls | ||
|
||
import "crypto/x509" | ||
|
||
func rootCAs() (roots *x509.CertPool) { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.