Skip to content

Commit

Permalink
style: Add a newline at the end of PEM conversions
Browse files Browse the repository at this point in the history
- Correctly import the `context` module.
- Improve verification tests on certificates generated with `NewX509Cert`.
- Add a newline character at the end of PEM files when converting private keys, public keys, certificates, and CSRs.
- Include the `WithX509CertCommonName` parameter to improve test management.
  • Loading branch information
Laisky committed Jan 3, 2024
1 parent 9fd4713 commit 1ef7494
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
10 changes: 5 additions & 5 deletions crypto/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Prikey2Pubkey(prikey crypto.PrivateKey) (pubkey crypto.PublicKey) {
return prikey.(interface{ Public() crypto.PublicKey }).Public() // nolint:forcetypeassert // panic if not support
}

// Prikey2Pem marshal private key to pem
// Prikey2Pem marshal private key to pem, tailing with line break
func Prikey2Pem(key crypto.PrivateKey) ([]byte, error) {
der, err := Prikey2Der(key)
if err != nil {
Expand All @@ -129,7 +129,7 @@ func Pubkey2Der(key crypto.PublicKey) ([]byte, error) {
return x509.MarshalPKIXPublicKey(key)
}

// Pubkey2Pem marshal public key to pem
// Pubkey2Pem marshal public key to pem, tailing with line break
func Pubkey2Pem(key crypto.PublicKey) ([]byte, error) {
der, err := Pubkey2Der(key)
if err != nil {
Expand All @@ -139,7 +139,7 @@ func Pubkey2Pem(key crypto.PublicKey) ([]byte, error) {
return PubkeyDer2Pem(der), nil
}

// Cert2Pem marshal x509 certificate to pem
// Cert2Pem marshal x509 certificate to pem, tailing with line break
func Cert2Pem(certs ...*x509.Certificate) (ret []byte) {
for i := range certs {
ret = append(ret, CertDer2Pem(Cert2Der(certs[i]))...)
Expand Down Expand Up @@ -325,12 +325,12 @@ func PubkeyDer2Pem(pubkeyInDer []byte) (prikeyInPem []byte) {
return pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubkeyInDer})
}

// CertDer2Pem convert certificate in der to pem
// CertDer2Pem convert certificate in der to pem, tailing with line break
func CertDer2Pem(certInDer []byte) (certInPem []byte) {
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certInDer})
}

// CSRDer2Pem convert CSR in der to pem
// CSRDer2Pem convert CSR in der to pem, tailing with line break
func CSRDer2Pem(CSRInDer []byte) (CSRInPem []byte) {
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: CSRInDer})
}
Expand Down
23 changes: 17 additions & 6 deletions crypto/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestTLSPrivatekey(t *testing.T) {

pem, err := Prikey2Pem(prikey)
require.NoError(t, err)
require.Equal(t, "\n", string(pem[len(pem)-1]))

_, err = Pem2Der(append(pem, '\n'))
require.NoError(t, err)
Expand Down Expand Up @@ -152,6 +153,7 @@ func TestTLSPrivatekey(t *testing.T) {
require.NoError(t, err)

pem := Cert2Pem(cert)
require.Equal(t, "\n", string(pem[len(pem)-1]))
cert, err = Pem2Cert(pem)
require.NoError(t, err)
require.Equal(t, der, Cert2Der(cert))
Expand Down Expand Up @@ -200,6 +202,7 @@ func TestTLSPublickey(t *testing.T) {

pem, err := Pubkey2Pem(pubkey)
require.NoError(t, err)
require.Equal(t, "\n", string(pem[len(pem)-1]))

der2, err := Pem2Der(pem)
require.NoError(t, err)
Expand Down Expand Up @@ -260,16 +263,21 @@ func TestSecureCipherSuites(t *testing.T) {
func TestVerifyCertByPrikey(t *testing.T) {
t.Parallel()

prikey, certDer, err := NewRSAPrikeyAndCert(RSAPrikeyBits3072)
prikey, certDer, err := NewRSAPrikeyAndCert(RSAPrikeyBits3072,
WithX509CertCommonName("TestVerifyCertByPrikey"),
)
require.NoError(t, err)

certPem := CertDer2Pem(certDer)
require.Equal(t, "\n", string(certPem[len(certPem)-1]))

err = VerifyCertByPrikey(certPem, prikey)
require.NoError(t, err)

t.Run("different cert", func(t *testing.T) {
_, certDer2, err := NewRSAPrikeyAndCert(RSAPrikeyBits3072)
_, certDer2, err := NewRSAPrikeyAndCert(RSAPrikeyBits3072,
WithX509CertCommonName("laisky-test"),
)
require.NoError(t, err)
certPem2 := CertDer2Pem(certDer2)
err = VerifyCertByPrikey(certPem2, prikey)
Expand All @@ -291,6 +299,7 @@ func TestDer2CSR(t *testing.T) {
require.NoError(t, err)

pem := CSRDer2Pem(csrDer)
require.Equal(t, "\n", string(pem[len(pem)-1]))

csr2, err := Pem2CSR(pem)
require.NoError(t, err)
Expand All @@ -306,6 +315,7 @@ func Test_UseCaAsClientTlsCert(t *testing.T) {
defer cancel()

rootprikeyPem, rootcaDer, err := NewRSAPrikeyAndCert(RSAPrikeyBits4096,
WithX509CertCommonName("laisky-test"),
WithX509CertIsCA(),
)
require.NoError(t, err)
Expand All @@ -324,7 +334,7 @@ func Test_UseCaAsClientTlsCert(t *testing.T) {
prikey, err := NewRSAPrikey(RSAPrikeyBits4096)
require.NoError(t, err)

csrDer, err := NewX509CSR(prikey)
csrDer, err := NewX509CSR(prikey, WithX509CSRCommonName("laisky-test"))
require.NoError(t, err)

certDer, err := NewX509CertByCSR(rootca, rootcaPrikey, csrDer)
Expand Down Expand Up @@ -378,7 +388,7 @@ func Test_UseCaAsClientTlsCert(t *testing.T) {
prikey, err := NewRSAPrikey(RSAPrikeyBits4096)
require.NoError(t, err)

csrDer, err := NewX509CSR(prikey)
csrDer, err := NewX509CSR(prikey, WithX509CSRCommonName("laisky-test"))
require.NoError(t, err)

certDer, err := NewX509CertByCSR(rootca, rootcaPrikey, csrDer,
Expand Down Expand Up @@ -411,6 +421,7 @@ func Test_UseCaAsServerTlsCert(t *testing.T) {
defer cancel()

rootprikeyPem, rootcaDer, err := NewRSAPrikeyAndCert(RSAPrikeyBits4096,
WithX509CertCommonName("laisky-test"),
WithX509CertIsCA(),
)
require.NoError(t, err)
Expand All @@ -429,7 +440,7 @@ func Test_UseCaAsServerTlsCert(t *testing.T) {
prikey, err := NewRSAPrikey(RSAPrikeyBits4096)
require.NoError(t, err)

csrDer, err := NewX509CSR(prikey)
csrDer, err := NewX509CSR(prikey, WithX509CSRCommonName("laisky-test"))
require.NoError(t, err)

certDer, err := NewX509CertByCSR(rootca, rootcaPrikey, csrDer,
Expand Down Expand Up @@ -489,7 +500,7 @@ func Test_UseCaAsServerTlsCert(t *testing.T) {
prikey, err := NewRSAPrikey(RSAPrikeyBits4096)
require.NoError(t, err)

csrDer, err := NewX509CSR(prikey)
csrDer, err := NewX509CSR(prikey, WithX509CSRCommonName("laisky-test"))
require.NoError(t, err)

certDer, err := NewX509CertByCSR(rootca, rootcaPrikey, csrDer)
Expand Down

0 comments on commit 1ef7494

Please sign in to comment.