From b6c44317f540dac8763e720767b0e73940a0b6c5 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 12 May 2017 15:27:44 +0200 Subject: add proper pem interface This should finally resolve the completely broken and wrong API to get a pem representation of a resource. --- certificate.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'certificate.go') diff --git a/certificate.go b/certificate.go index 3adb530..b90e9fa 100644 --- a/certificate.go +++ b/certificate.go @@ -81,10 +81,18 @@ func LoadCertificateSignRequest(raw []byte) (*CertificateRequest, error) { return (*CertificateRequest)(csr), nil } +// ToPem returns a pem.Block representing the CertificateRequest. +func (c *CertificateRequest) ToPem() (pem.Block, error) { + return pem.Block{Type: PemLabelCertificateRequest, Bytes: c.Raw}, nil +} + // Return the certificate sign request as a pem block. func (c *CertificateRequest) MarshalPem() (io.WriterTo, error) { - block := &pem.Block{Type: PemLabelCertificateRequest, Bytes: c.Raw} - return marshalledPemBlock(pem.EncodeToMemory(block)), nil + if block, err := c.ToPem(); err != nil { + return nil, err + } else { + return marshalledPemBlock(pem.EncodeToMemory(&block)), nil + } } // Convert the certificate sign request to a certificate using the private key @@ -152,8 +160,16 @@ func LoadCertificate(raw []byte) (*Certificate, error) { // marshal the certificate to a pem block func (c *Certificate) MarshalPem() (io.WriterTo, error) { - block := &pem.Block{Type: PemLabelCertificate, Bytes: c.Raw} - return marshalledPemBlock(pem.EncodeToMemory(block)), nil + if block, err := c.ToPem(); err != nil { + return nil, err + } else { + return marshalledPemBlock(pem.EncodeToMemory(&block)), nil + } +} + +// ToPem returns the pem block of the certificate. +func (c *Certificate) ToPem() (pem.Block, error) { + return pem.Block{Type: PemLabelCertificate, Bytes: c.Raw}, nil } // Check if the certificate options have the required fields set. -- cgit v1.2.3-70-g09d2