mirror of
https://github.com/FiloSottile/age.git
synced 2026-09-20 15:04:26 +00:00
cmd/age: initial support for SSH identities and recipients
Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
committed by
Filippo Valsorda
parent
7f61cf23bf
commit
dd0939ffaa
@@ -160,6 +160,28 @@ func NewSSHEd25519Recipient(pk ssh.PublicKey) (*SSHEd25519Recipient, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func ParseSSHRecipient(s string) (Recipient, error) {
|
||||
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(s))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("malformed SSH recipient: %q: %v", s, err)
|
||||
}
|
||||
|
||||
var r Recipient
|
||||
switch t := pubKey.Type(); t {
|
||||
case "ssh-rsa":
|
||||
r, err = NewSSHRSARecipient(pubKey)
|
||||
case "ssh-ed25519":
|
||||
r, err = NewSSHEd25519Recipient(pubKey)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown SSH recipient type: %q", t)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("malformed SSH recipient: %q: %v", s, err)
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
var curve25519P, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10)
|
||||
|
||||
func ed25519PublicKeyToCurve25519(pk ed25519.PublicKey) []byte {
|
||||
@@ -260,6 +282,22 @@ func NewSSHEd25519Identity(key ed25519.PrivateKey) (*SSHEd25519Identity, error)
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func ParseSSHIdentity(pemBytes []byte) (Identity, error) {
|
||||
k, err := ssh.ParseRawPrivateKey(pemBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch k := k.(type) {
|
||||
case *ed25519.PrivateKey:
|
||||
return NewSSHEd25519Identity(*k)
|
||||
case *rsa.PrivateKey:
|
||||
return NewSSHRSAIdentity(k)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unsupported SSH identity type: %T", k)
|
||||
}
|
||||
|
||||
func ed25519PrivateKeyToCurve25519(pk ed25519.PrivateKey) []byte {
|
||||
h := sha512.New()
|
||||
h.Write(pk[:32])
|
||||
|
||||
Reference in New Issue
Block a user