diff --git a/ecdsa.go b/ecdsa.go index 4531d4e..96c317f 100644 --- a/ecdsa.go +++ b/ecdsa.go @@ -9,6 +9,7 @@ import ( "encoding/asn1" "encoding/pem" "errors" + "io" "math/big" ) @@ -72,11 +73,11 @@ func (pr EcdsaPrivateKey) PrivateKey() crypto.PrivateKey { // This function implements the Pemmer interface to marshal the private key // into a pem block. -func (pr EcdsaPrivateKey) MarshalPem() (marshalledPemBlock, error) { +func (pr EcdsaPrivateKey) MarshalPem() (io.WriterTo, error) { asn1, err := x509.MarshalECPrivateKey(pr.private_key) if err != nil { return nil, err } pem_block := pem.Block{Type: PemLabelEcdsa, Bytes: asn1} - return pem.EncodeToMemory(&pem_block), nil + return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil } // This functoin loads an ecdsa public key from the asn.1 representation. @@ -91,11 +92,11 @@ func LoadPublicKeyEcdsa(raw []byte) (*EcdsaPublicKey, error) { // This function implements the Pemmer interface to marshal the public key into // a pem block. -func (pu *EcdsaPublicKey) MarshalPem() (marshalledPemBlock, error) { +func (pu *EcdsaPublicKey) MarshalPem() (io.WriterTo, error) { asn1, err := x509.MarshalPKIXPublicKey(pu.public_key) if err != nil { return nil, err } pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1} - return pem.EncodeToMemory(&pem_block), nil + return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil } // This function verifies a message using the public key, signature and hash diff --git a/pem_marshal.go b/pem_marshal.go index 14b3bd4..54ce1e0 100644 --- a/pem_marshal.go +++ b/pem_marshal.go @@ -10,6 +10,7 @@ type ( // This function writes the marshalled pem block to a writer and returns the // number of written bytes and eventual errors. -func (b marshalledPemBlock) WriteTo(stream io.Writer) (int, error) { - return stream.Write(b) +func (b marshalledPemBlock) WriteTo(stream io.Writer) (int64, error) { + numBytes, err := stream.Write(b) + return int64(numBytes), err } diff --git a/rsa.go b/rsa.go index ac649b7..bcdfe6e 100644 --- a/rsa.go +++ b/rsa.go @@ -7,6 +7,7 @@ import ( "crypto/x509" "encoding/pem" "errors" + "io" ) const ( @@ -50,10 +51,10 @@ func (pr RsaPrivateKey) PrivateKey() crypto.PrivateKey { return pr.private_key } -func (pr RsaPrivateKey) MarshalPem() (marshalledPemBlock, error) { +func (pr RsaPrivateKey) MarshalPem() (io.WriterTo, error) { asn1 := x509.MarshalPKCS1PrivateKey(pr.private_key) pem_block := pem.Block{Type: PemLabelRsa, Bytes: asn1} - return pem.EncodeToMemory(&pem_block), nil + return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil } // restore a rsa public key @@ -62,11 +63,11 @@ func LoadPublicKeyRsa(raw []byte) (*RsaPublicKey, error) { } // marshal a rsa public key into pem format -func (pu *RsaPublicKey) MarshalPem() (marshalledPemBlock, error) { +func (pu *RsaPublicKey) MarshalPem() (io.WriterTo, error) { asn1, err := x509.MarshalPKIXPublicKey(pu.public_key) if err != nil { return nil, err } pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1} - return pem.EncodeToMemory(&pem_block), nil + return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil } // verify a message with a signature using the public key diff --git a/types.go b/types.go index 43150f9..08c42d8 100644 --- a/types.go +++ b/types.go @@ -16,6 +16,7 @@ package pki import ( "crypto" + "io" ) // This label is used as the type in the pem encoding of public keys. @@ -47,6 +48,6 @@ type ( // in the pem format. The result can then be written to any structure // implementing the io.Writer interface. Pemmer interface { - MarshalPem() (marshalledPemBlock, error) + MarshalPem() (io.WriterTo, error) } )