Skip to content

Commit

Permalink
feat: rename some funcs (#12)
Browse files Browse the repository at this point in the history
* rename some funcs

* remove some funcs
  • Loading branch information
shipengqi authored Sep 18, 2022
1 parent 3231da7 commit 964c1c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
54 changes: 50 additions & 4 deletions crtutil/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,35 @@ var (
// ParseCertFile parses x509.Certificate from the given file.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
// Deprecated: use ReadFileAsX509 instead.
func ParseCertFile(fpath string) (*x509.Certificate, error) {
return ReadFileAsX509(fpath)
}

// ReadFileAsX509 read x509.Certificate from the given file.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
func ReadFileAsX509(fpath string) (*x509.Certificate, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}

return ParseCertBytes(bs)
return ReadBytesAsX509(bs)
}

// ParseCertBytes parses a single x509.Certificate from the given data.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
// Deprecated: use ReadBytesAsX509 instead.
func ParseCertBytes(data []byte) (*x509.Certificate, error) {
return ReadBytesAsX509(data)
}

// ReadBytesAsX509 read x509.Certificate from the given data.
// The data is expected to be PEM Encoded and contain one certificate
// with PEM type "CERTIFICATE".
func ReadBytesAsX509(data []byte) (*x509.Certificate, error) {
if len(data) == 0 {
return nil, nil
}
Expand All @@ -46,18 +62,34 @@ func ParseCertBytes(data []byte) (*x509.Certificate, error) {
// ParseCertChainFile parses the x509.Certificate chain from the given file.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
// Deprecated: use ReadChainFileAsX509 instead.
func ParseCertChainFile(fpath string) ([]*x509.Certificate, error) {
return ReadChainFileAsX509(fpath)
}

// ReadChainFileAsX509 read the x509.Certificate chain from the given file.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
func ReadChainFileAsX509(fpath string) ([]*x509.Certificate, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return nil, err
}
return ParseCertChainBytes(bs)
return ReadChainBytesAsX509(bs)
}

// ParseCertChainBytes parses x509.Certificate chain from the given data.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
// Deprecated: use ReadChainBytesAsX509 instead.
func ParseCertChainBytes(data []byte) ([]*x509.Certificate, error) {
return ReadChainBytesAsX509(data)
}

// ReadChainBytesAsX509 read x509.Certificate chain from the given data.
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
func ReadChainBytesAsX509(data []byte) ([]*x509.Certificate, error) {
var (
certs []*x509.Certificate
cert *x509.Certificate
Expand Down Expand Up @@ -86,23 +118,37 @@ func ParseCertChainBytes(data []byte) ([]*x509.Certificate, error) {
return certs, nil
}

// CertToPEM returns a PEM encoded x509 Certificate.
// CertToPEM converts a x509.Certificate into a PEM block.
// Deprecated: use EncodeX509ToPEM instead.
func CertToPEM(cert *x509.Certificate) []byte {
return EncodeX509ToPEM(cert, nil)
}

// EncodeX509ToPEM converts a x509.Certificate into a PEM block.
func EncodeX509ToPEM(cert *x509.Certificate, headers map[string]string) []byte {
return pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
Headers: headers,
})
}

// CertChainToPEM returns a PEM encoded chain of x509 Certificates, in the order they are passed.
// CertChainToPEM converts a slice of x509.Certificate into PEM block, in the order they are passed.
// Deprecated: use EncodeX509ChainToPEM instead.
func CertChainToPEM(chain []*x509.Certificate) ([]byte, error) {
return EncodeX509ChainToPEM(chain, nil)
}

// EncodeX509ChainToPEM converts a slice of x509.Certificate into PEM block, in the order they are passed.
func EncodeX509ChainToPEM(chain []*x509.Certificate, headers map[string]string) ([]byte, error) {
var buf bytes.Buffer
for _, cert := range chain {
if err := pem.Encode(
&buf,
&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
Headers: headers,
},
); err != nil {
return nil, err
Expand Down
26 changes: 13 additions & 13 deletions crtutil/crt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,66 @@ import (
)

func TestParseCrtFile(t *testing.T) {
_, err := ParseCertFile("testdata/server.crt")
_, err := ReadFileAsX509("testdata/server.crt")
assert.NoError(t, err)
// printCrt(t, crt, "server")

t.Run("parse error with empty data", func(t *testing.T) {
_, err = ParseCertFile("testdata/server-fail.crt")
_, err = ReadFileAsX509("testdata/server-fail.crt")
assert.Error(t, err)
})
}

func TestParseCertBytes(t *testing.T) {
t.Run("empty data", func(t *testing.T) {
_, err := ParseCertBytes([]byte{})
_, err := ReadBytesAsX509([]byte{})
assert.NoError(t, err)
})
t.Run("ErrNoPEMData", func(t *testing.T) {
_, err := ParseCertBytes([]byte("sdfklhjasdfkjhasdfkjlhas"))
_, err := ReadBytesAsX509([]byte("sdfklhjasdfkjhasdfkjlhas"))
assert.ErrorIs(t, err, ErrNoPEMData)
})
}

func TestParseCrtSetFile(t *testing.T) {
crts, err := ParseCertChainFile("testdata/server-ca.crt")
crts, err := ReadChainFileAsX509("testdata/server-ca.crt")
assert.NoError(t, err)
assert.Equal(t, 2, len(crts))

crts, err = ParseCertChainFile("testdata/server-3layers.crt")
crts, err = ReadChainFileAsX509("testdata/server-3layers.crt")
assert.NoError(t, err)
assert.Equal(t, 3, len(crts))

crts, err = ParseCertChainFile("testdata/server-3layers-withcharacters.crt")
crts, err = ReadChainFileAsX509("testdata/server-3layers-withcharacters.crt")
assert.NoError(t, err)
assert.Equal(t, 3, len(crts))

t.Run("parse error with empty data", func(t *testing.T) {
_, err = ParseCertChainFile("testdata/server-fail.crt")
_, err = ReadChainFileAsX509("testdata/server-fail.crt")
assert.Error(t, err)
})
}

func TestParseCertChainBytes(t *testing.T) {
t.Run("ErrNoPEMData", func(t *testing.T) {
_, err := ParseCertChainBytes([]byte("sdfklhjasdfkjhasdfkjlhas"))
_, err := ReadChainBytesAsX509([]byte("sdfklhjasdfkjhasdfkjlhas"))
assert.ErrorIs(t, err, ErrNoPEMData)
})
}

func TestCertChainToPEM(t *testing.T) {
crts, err := ParseCertChainFile("testdata/server-3layers-withcharacters.crt")
crts, err := ReadChainFileAsX509("testdata/server-3layers-withcharacters.crt")
assert.NoError(t, err)
assert.Equal(t, 3, len(crts))
got, err := CertChainToPEM(crts)
got, err := EncodeX509ChainToPEM(crts, nil)
assert.NoError(t, err)
assert.NotEmpty(t, got)
}

func TestCertToPEM(t *testing.T) {
crt, err := ParseCertFile("testdata/server.crt")
crt, err := ReadFileAsX509("testdata/server.crt")
assert.NoError(t, err)
got := CertToPEM(crt)
got := EncodeX509ToPEM(crt, nil)
assert.NotEmpty(t, got)
}

Expand Down

0 comments on commit 964c1c9

Please sign in to comment.