-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from tianxiaoliang/master
improvement: RFC 1423 is insecure, support load tls cert without password
- Loading branch information
Showing
6 changed files
with
122 additions
and
39 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
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
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 |
---|---|---|
@@ -1,16 +1,91 @@ | ||
package tlsutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-chassis/foundation/tlsutil" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestLoadTLSCertificateFileNotExist(t *testing.T) { | ||
tlsCert, err := tlsutil.LoadTLSCertificate("abc.txt", "abc.txt", "fakepassphase", func(src string) (s string, err error) { | ||
return "fakepassphase", nil | ||
var rsaCertPEM = `-----BEGIN CERTIFICATE----- | ||
MIIDXTCCAkWgAwIBAgIJANq58YD5coE2MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV | ||
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQK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l4vQdM/nemyIk/HodgPAoGBAOAt | ||
UZ8HTTzVnzyjPq1VGe2mQBmwC1fMkpmD2/d56TIDT4vVbAsu9ge106mdB52ZvlNc | ||
nkl6KvsBcCXyJT6S/bpOINFViFAvigRpmez1rmFMqBf2OvTnELtlZ3kTn8/X1uIX | ||
bFIX2G1XAKAQ6MBCYLzvCLMTf+EkebENA4kQr0cjAoGBAJrdfY8nqgYvQBmHxgDS | ||
bYUEF5ksMQhuifDQLh035HqAUe2r0xDxHHZeOWxgQtvr25+/MjHbfXG5b8BTG+wc | ||
r8xBo46tLjOTtbMok+2QDwwa4SKR24KCWTiCXEBhIK/QbTwb/fNbkJE/oB7e6mDJ | ||
vvSt/4uVBiY4i+dgzFrGNSgnAoGBAJ/RbzkSuYu/N/DA6LQl0YBNX7FwggWsAG+V | ||
Q8JglVFkbtdf5dDrP9crV6S6IG3I55kClI4JnI6p7cv/n3HG1UB25oqWkcGowpp2 | ||
tpfqZtFTFxtOHaXu/Uy79FKrHOnOFJHG5SB5g4Af4IA8zdITAGhxeSBBrI9Ts7X3 | ||
cyfKT0tFAoGBAJvS+AZK93SLYvsRdyytevKuYMAqIO+YRBOfZ+NN9+vosGlzbmuj | ||
8zHkQQ8yeMJASbTpEAakz2ybJeN5zNELMKGUrsGVefiW0Z/5skO2toeF1PWtF0G8 | ||
FF2uvhCC6xWFT/PzYycBYMzq6gUus3aJQ+wjFnEflZYj7wGzf4tGzA5I | ||
-----END RSA PRIVATE KEY----- | ||
`) | ||
|
||
func TestLoadTLSCertificate(t *testing.T) { | ||
td := t.TempDir() | ||
f1, err := os.Create(filepath.Join(td, "key.pem")) | ||
assert.NoError(t, err) | ||
defer f1.Close() | ||
_, err = io.WriteString(f1, string(rsaKeyPEM)) | ||
assert.NoError(t, err) | ||
|
||
f2, err := os.Create(filepath.Join(td, "cert.pem")) | ||
assert.NoError(t, err) | ||
defer f1.Close() | ||
_, err = io.WriteString(f2, rsaCertPEM) | ||
assert.NoError(t, err) | ||
|
||
t.Run("given no file,should failed", func(t *testing.T) { | ||
tlsCert, err := tlsutil.LoadTLSCertificate("abc.txt", "abc.txt", "fakepassphase", func(src string) (s string, err error) { | ||
return "fakepassphase", nil | ||
}) | ||
assert.Nil(t, tlsCert) | ||
assert.Error(t, err) | ||
}) | ||
t.Run("given rsa key pair,should success", func(t *testing.T) { | ||
tlsCert, err := tlsutil.LoadTLSCertificate(f2.Name(), f1.Name(), "", nil) | ||
assert.NotNil(t, tlsCert) | ||
assert.NoError(t, err) | ||
}) | ||
assert.Nil(t, tlsCert) | ||
assert.Error(t, err) | ||
} |