From 8079e04fe2f693b03bb420fb15017e161f414bb3 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Fri, 19 Dec 2014 12:19:56 -0800 Subject: [PATCH] key marshalling --- crypto/key.go | 35 +++++++++++++++++++++++++++++++++++ crypto/key_test.go | 31 ++++++++++++++++++++++++++----- crypto/rsa.go | 8 ++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crypto/key.go b/crypto/key.go index e81ef5a13..49a090723 100644 --- a/crypto/key.go +++ b/crypto/key.go @@ -5,6 +5,7 @@ package crypto import ( "bytes" + "encoding/base64" "errors" "fmt" @@ -239,6 +240,20 @@ func UnmarshalPublicKey(data []byte) (PubKey, error) { } } +// MarshalPublicKey converts a public key object into a protobuf serialized +// public key +func MarshalPublicKey(k PubKey) ([]byte, error) { + b, err := MarshalRsaPublicKey(k.(*RsaPublicKey)) + if err != nil { + return nil, err + } + pmes := new(pb.PublicKey) + typ := pb.KeyType_RSA // for now only type. + pmes.Type = &typ + pmes.Data = b + return proto.Marshal(pmes) +} + // UnmarshalPrivateKey converts a protobuf serialized private key into its // representative object func UnmarshalPrivateKey(data []byte) (PrivKey, error) { @@ -256,6 +271,26 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) { } } +// MarshalPrivateKey converts a key object into its protobuf serialized form. +func MarshalPrivateKey(k PrivKey) ([]byte, error) { + b := MarshalRsaPrivateKey(k.(*RsaPrivateKey)) + pmes := new(pb.PrivateKey) + typ := pb.KeyType_RSA // for now only type. + pmes.Type = &typ + pmes.Data = b + return proto.Marshal(pmes) +} + +// ConfigDecodeKey decodes from b64 (for config file), and unmarshals. +func ConfigDecodeKey(b string) ([]byte, error) { + return base64.StdEncoding.DecodeString(b) +} + +// ConfigEncodeKey encodes to b64 (for config file), and marshals. +func ConfigEncodeKey(b []byte) string { + return base64.StdEncoding.EncodeToString(b) +} + // KeyEqual checks whether two func KeyEqual(k1, k2 Key) bool { if k1 == k2 { diff --git a/crypto/key_test.go b/crypto/key_test.go index 112b99bee..1300be8f8 100644 --- a/crypto/key_test.go +++ b/crypto/key_test.go @@ -1,6 +1,9 @@ package crypto -import "testing" +import ( + "bytes" + "testing" +) func TestRsaKeys(t *testing.T) { sk, pk, err := GenerateKeyPair(RSA, 512) @@ -33,26 +36,44 @@ func testKeySignature(t *testing.T, sk PrivKey) { } func testKeyEncoding(t *testing.T, sk PrivKey) { - skb, err := sk.Bytes() + skbm, err := MarshalPrivateKey(sk) if err != nil { t.Fatal(err) } - _, err = UnmarshalPrivateKey(skb) + sk2, err := UnmarshalPrivateKey(skbm) if err != nil { t.Fatal(err) } + skbm2, err := MarshalPrivateKey(sk2) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(skbm, skbm2) { + t.Error("skb -> marshal -> unmarshal -> skb failed.\n", skbm, "\n", skbm2) + } + pk := sk.GetPublic() - pkb, err := pk.Bytes() + pkbm, err := MarshalPublicKey(pk) if err != nil { t.Fatal(err) } - _, err = UnmarshalPublicKey(pkb) + _, err = UnmarshalPublicKey(pkbm) if err != nil { t.Fatal(err) } + + pkbm2, err := MarshalPublicKey(pk) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(pkbm, pkbm2) { + t.Error("skb -> marshal -> unmarshal -> skb failed.\n", pkbm, "\n", pkbm2) + } } func testKeyEquals(t *testing.T, k Key) { diff --git a/crypto/rsa.go b/crypto/rsa.go index 1ef99776b..fc50bc32c 100644 --- a/crypto/rsa.go +++ b/crypto/rsa.go @@ -101,6 +101,10 @@ func UnmarshalRsaPrivateKey(b []byte) (*RsaPrivateKey, error) { return &RsaPrivateKey{sk}, nil } +func MarshalRsaPrivateKey(k *RsaPrivateKey) []byte { + return x509.MarshalPKCS1PrivateKey(k.sk) +} + func UnmarshalRsaPublicKey(b []byte) (*RsaPublicKey, error) { pub, err := x509.ParsePKIXPublicKey(b) if err != nil { @@ -112,3 +116,7 @@ func UnmarshalRsaPublicKey(b []byte) (*RsaPublicKey, error) { } return &RsaPublicKey{pk}, nil } + +func MarshalRsaPublicKey(k *RsaPublicKey) ([]byte, error) { + return x509.MarshalPKIXPublicKey(k.k) +}