Skip to content

Commit

Permalink
feat: Cert2Der support multiple certs
Browse files Browse the repository at this point in the history
  • Loading branch information
Laisky committed Mar 14, 2023
1 parent c39f3d1 commit fdeaa43
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions crypto/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestAEADDecrypt(t *testing.T) {
args args
}{
{"", args{key, []byte("fhwkufhuweh"), []byte("laisky")}},
{"", args{key, []byte("31231"), nil}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
16 changes: 12 additions & 4 deletions crypto/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,21 @@ func Pubkey2Pem(key crypto.PublicKey) ([]byte, error) {
}

// Cert2Pem marshal x509 certificate to pem
func Cert2Pem(cert *x509.Certificate) []byte {
return CertDer2Pem(Cert2Der(cert))
func Cert2Pem(certs ...*x509.Certificate) (ret []byte) {
for i := range certs {
ret = append(ret, CertDer2Pem(Cert2Der(certs[i]))...)
}

return
}

// Cert2Der marshal private key by x509.8
func Cert2Der(cert *x509.Certificate) []byte {
return cert.Raw
func Cert2Der(cert ...*x509.Certificate) (ret []byte) {
for i := range cert {
ret = append(ret, cert[i].Raw...)
}

return
}

// Der2Cert parse sigle certificate in der
Expand Down
7 changes: 7 additions & 0 deletions crypto/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ func TestPem2Der_multi_certs(t *testing.T) {

require.Equal(t, "sgx-coordinator-inter", cs[0].Subject.CommonName)
require.Equal(t, "sgx-coordinator", cs[1].Subject.CommonName)

gotder := Cert2Der(cs...)
require.Equal(t, der, gotder)

gotder, err = Pem2Der(Cert2Pem(cs...))
require.NoError(t, err)
require.Equal(t, der, gotder)
}

func TestSecureCipherSuites(t *testing.T) {
Expand Down

0 comments on commit fdeaa43

Please sign in to comment.