2017-04-17 18:08:24 -04:00
|
|
|
package signed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
|
2017-10-30 12:21:41 -04:00
|
|
|
"github.com/theupdateframework/notary/trustmanager"
|
|
|
|
"github.com/theupdateframework/notary/tuf/data"
|
|
|
|
"github.com/theupdateframework/notary/tuf/utils"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type edCryptoKey struct {
|
2017-08-24 18:40:24 -04:00
|
|
|
role data.RoleName
|
2017-04-17 18:08:24 -04:00
|
|
|
privKey data.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ed25519 implements a simple in memory cryptosystem for ED25519 keys
|
|
|
|
type Ed25519 struct {
|
|
|
|
keys map[string]edCryptoKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEd25519 initializes a new empty Ed25519 CryptoService that operates
|
|
|
|
// entirely in memory
|
|
|
|
func NewEd25519() *Ed25519 {
|
|
|
|
return &Ed25519{
|
|
|
|
make(map[string]edCryptoKey),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddKey allows you to add a private key
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) AddKey(role data.RoleName, gun data.GUN, k data.PrivateKey) error {
|
2017-04-17 18:08:24 -04:00
|
|
|
e.addKey(role, k)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addKey allows you to add a private key
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) addKey(role data.RoleName, k data.PrivateKey) {
|
2017-04-17 18:08:24 -04:00
|
|
|
e.keys[k.ID()] = edCryptoKey{
|
|
|
|
role: role,
|
|
|
|
privKey: k,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveKey deletes a key from the signer
|
|
|
|
func (e *Ed25519) RemoveKey(keyID string) error {
|
|
|
|
delete(e.keys, keyID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListKeys returns the list of keys IDs for the role
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) ListKeys(role data.RoleName) []string {
|
2017-04-17 18:08:24 -04:00
|
|
|
keyIDs := make([]string, 0, len(e.keys))
|
|
|
|
for id, edCryptoKey := range e.keys {
|
|
|
|
if edCryptoKey.role == role {
|
|
|
|
keyIDs = append(keyIDs, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keyIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAllKeys returns the map of keys IDs to role
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) ListAllKeys() map[string]data.RoleName {
|
|
|
|
keys := make(map[string]data.RoleName)
|
2017-04-17 18:08:24 -04:00
|
|
|
for id, edKey := range e.keys {
|
|
|
|
keys[id] = edKey.role
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create generates a new key and returns the public part
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) Create(role data.RoleName, gun data.GUN, algorithm string) (data.PublicKey, error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
if algorithm != data.ED25519Key {
|
|
|
|
return nil, errors.New("only ED25519 supported by this cryptoservice")
|
|
|
|
}
|
|
|
|
|
|
|
|
private, err := utils.GenerateED25519Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.addKey(role, private)
|
|
|
|
return data.PublicKeyFromPrivate(private), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKeys returns a map of public keys for the ids provided, when those IDs are found
|
|
|
|
// in the store.
|
|
|
|
func (e *Ed25519) PublicKeys(keyIDs ...string) (map[string]data.PublicKey, error) {
|
|
|
|
k := make(map[string]data.PublicKey)
|
|
|
|
for _, keyID := range keyIDs {
|
|
|
|
if edKey, ok := e.keys[keyID]; ok {
|
|
|
|
k[keyID] = data.PublicKeyFromPrivate(edKey.privKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKey returns a single public key based on the ID
|
|
|
|
func (e *Ed25519) GetKey(keyID string) data.PublicKey {
|
|
|
|
if privKey, _, err := e.GetPrivateKey(keyID); err == nil {
|
|
|
|
return data.PublicKeyFromPrivate(privKey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrivateKey returns a single private key and role if present, based on the ID
|
2017-08-24 18:40:24 -04:00
|
|
|
func (e *Ed25519) GetPrivateKey(keyID string) (data.PrivateKey, data.RoleName, error) {
|
2017-04-17 18:08:24 -04:00
|
|
|
if k, ok := e.keys[keyID]; ok {
|
|
|
|
return k.privKey, k.role, nil
|
|
|
|
}
|
|
|
|
return nil, "", trustmanager.ErrKeyNotFound{KeyID: keyID}
|
|
|
|
}
|