add public key loader
This adds a way to restore a public key from any data source.
This commit is contained in:
parent
639a5379e9
commit
80db488cbd
10
ecdsa.go
10
ecdsa.go
|
@ -75,6 +75,16 @@ func (pr EcdsaPrivateKey) MarshalPem() (marshalledPemBlock, error) {
|
||||||
return pem.EncodeToMemory(&pem_block), nil
|
return pem.EncodeToMemory(&pem_block), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load an ecdsa public key
|
||||||
|
func LoadPublicKeyEcdsa(raw []byte) (*EcdsaPublicKey, error) {
|
||||||
|
raw_pub, err := x509.ParsePKIXPublicKey(raw)
|
||||||
|
if err != nil { return nil, err }
|
||||||
|
|
||||||
|
pub, ok := raw_pub.(*ecdsa.PublicKey)
|
||||||
|
if !ok { return nil, errors.New("Not an ecdsa key!") }
|
||||||
|
return &EcdsaPublicKey{pub}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// marshal the public key to a pem block
|
// marshal the public key to a pem block
|
||||||
func (pu *EcdsaPublicKey) MarshalPem() (marshalledPemBlock, error) {
|
func (pu *EcdsaPublicKey) MarshalPem() (marshalledPemBlock, error) {
|
||||||
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
|
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
|
||||||
|
|
|
@ -12,6 +12,10 @@ var (
|
||||||
SignatureHash = crypto.SHA512
|
SignatureHash = crypto.SHA512
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Loader func(raw []byte) (PublicKey, error)
|
||||||
|
)
|
||||||
|
|
||||||
// run the marshal test
|
// run the marshal test
|
||||||
func RunMarshalTest(pk_type string, pe Pemmer, label string, t *testing.T) ([]byte, error) {
|
func RunMarshalTest(pk_type string, pe Pemmer, label string, t *testing.T) ([]byte, error) {
|
||||||
marsh_pem, err := pe.MarshalPem()
|
marsh_pem, err := pe.MarshalPem()
|
||||||
|
@ -29,13 +33,7 @@ func RunMarshalTest(pk_type string, pe Pemmer, label string, t *testing.T) ([]by
|
||||||
}
|
}
|
||||||
|
|
||||||
// test other private key functions
|
// test other private key functions
|
||||||
func RunPrivateKeyTests(pk_type string, pk PrivateKey, t *testing.T) {
|
func RunPrivateKeyTests(pk_type string, pk PrivateKey, pu PublicKey, t *testing.T) {
|
||||||
pu := pk.Public()
|
|
||||||
|
|
||||||
// TODO check return result of the marshalled public key
|
|
||||||
_, err := RunMarshalTest(pk_type + "-public", pu, PemLabelPublic, t)
|
|
||||||
if err != nil { return }
|
|
||||||
|
|
||||||
signature, err := pk.Sign(SignatureMessage, SignatureHash)
|
signature, err := pk.Sign(SignatureMessage, SignatureHash)
|
||||||
if err != nil { t.Errorf("%s: error creating a signature: %s", pk_type, err) }
|
if err != nil { t.Errorf("%s: error creating a signature: %s", pk_type, err) }
|
||||||
|
|
||||||
|
@ -55,7 +53,13 @@ func TestEcdsaFunctions(t *testing.T) {
|
||||||
pk, err = LoadPrivateKeyEcdsa(block_bytes)
|
pk, err = LoadPrivateKeyEcdsa(block_bytes)
|
||||||
if err != nil { t.Errorf("ecdsa: pem content wrong: %s", err) }
|
if err != nil { t.Errorf("ecdsa: pem content wrong: %s", err) }
|
||||||
|
|
||||||
RunPrivateKeyTests("ecdsa", pk, t)
|
block_bytes, err = RunMarshalTest("ecdsa-public", pk.Public(), PemLabelPublic, t)
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
|
pu, err := LoadPublicKeyEcdsa(block_bytes)
|
||||||
|
if err != nil { t.Errorf("ecdsa-public: pem content wrong: %s", err) }
|
||||||
|
|
||||||
|
RunPrivateKeyTests("ecdsa", pk, pu, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test rsa private key functions
|
// test rsa private key functions
|
||||||
|
@ -69,5 +73,12 @@ func TestRsaFunctions(t *testing.T) {
|
||||||
pk, err = LoadPrivateKeyRsa(block_bytes)
|
pk, err = LoadPrivateKeyRsa(block_bytes)
|
||||||
if err != nil { t.Errorf("rsa: pem content wrong: %s", err) }
|
if err != nil { t.Errorf("rsa: pem content wrong: %s", err) }
|
||||||
|
|
||||||
RunPrivateKeyTests("rsa", pk, t)
|
|
||||||
|
block_bytes, err = RunMarshalTest("rsa-public", pk.Public(), PemLabelPublic, t)
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
|
pu, err := LoadPublicKeyRsa(block_bytes)
|
||||||
|
if err != nil { t.Errorf("rsa-public: pem content wrong: %s", err) }
|
||||||
|
|
||||||
|
RunPrivateKeyTests("rsa", pk, pu, t)
|
||||||
}
|
}
|
||||||
|
|
4
rsa.go
4
rsa.go
|
@ -56,6 +56,10 @@ func (pr RsaPrivateKey) MarshalPem() (marshalledPemBlock, error) {
|
||||||
return pem.EncodeToMemory(&pem_block), nil
|
return pem.EncodeToMemory(&pem_block), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadPublicKeyRsa(raw []byte) (*RsaPublicKey, error) {
|
||||||
|
return nil, errors.New("not implemented yet!")
|
||||||
|
}
|
||||||
|
|
||||||
func (pu *RsaPublicKey) MarshalPem() (marshalledPemBlock, error) {
|
func (pu *RsaPublicKey) MarshalPem() (marshalledPemBlock, error) {
|
||||||
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
|
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
|
|
Loading…
Reference in New Issue