2021-11-03 17:41:03 +00:00
|
|
|
package publickey
|
2021-11-02 07:43:52 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-04 07:55:00 +00:00
|
|
|
"bytes"
|
2021-11-02 07:43:52 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EdDSAPublicKeyHeader struct {
|
|
|
|
Curve string `json:"crv"`
|
|
|
|
X string `json:"x"`
|
|
|
|
}
|
|
|
|
|
2021-12-04 07:55:00 +00:00
|
|
|
func ParseEdDSAPublicKey(data json.RawMessage) (*ed25519.PublicKey, error) {
|
2021-11-02 07:43:52 +00:00
|
|
|
var header EdDSAPublicKeyHeader
|
|
|
|
|
2021-12-04 07:55:00 +00:00
|
|
|
r := bytes.NewReader(data)
|
|
|
|
dec := json.NewDecoder(r)
|
|
|
|
|
|
|
|
err := dec.Decode(&header)
|
2021-11-02 07:43:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Curve != "Ed25519" {
|
|
|
|
return nil, fmt.Errorf("Invalid/Unsupported curve type %s", header.Curve)
|
|
|
|
}
|
|
|
|
|
2021-12-05 09:38:31 +00:00
|
|
|
if header.X == "" {
|
|
|
|
return nil, fmt.Errorf("Non-existent curve point")
|
|
|
|
}
|
|
|
|
|
2021-11-02 07:43:52 +00:00
|
|
|
rawKey, err := base64.RawURLEncoding.DecodeString(header.X)
|
2021-12-05 09:38:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rawKey) != ed25519.PublicKeySize {
|
|
|
|
return nil, fmt.Errorf("Invalid public key")
|
|
|
|
}
|
2021-11-02 07:43:52 +00:00
|
|
|
|
|
|
|
key := ed25519.PublicKey(rawKey)
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|