| 1 | // Copyright 2019 The age Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package agessh |
| 6 | |
| 7 | import ( |
| 8 | "crypto" |
| 9 | "crypto/ed25519" |
| 10 | "crypto/rsa" |
| 11 | "fmt" |
| 12 | |
| 13 | "filippo.io/age" |
| 14 | "golang.org/x/crypto/ssh" |
| 15 | ) |
| 16 | |
| 17 | // EncryptedSSHIdentity is an age.Identity implementation based on a passphrase |
| 18 | // encrypted SSH private key. |
| 19 | // |
| 20 | // It requests the passphrase only if the public key matches a recipient stanza. |
| 21 | // If the application knows it will always have to decrypt the private key, it |
| 22 | // would be simpler to use ssh.ParseRawPrivateKeyWithPassphrase directly and |
| 23 | // pass the result to NewEd25519Identity or NewRSAIdentity. |
| 24 | type EncryptedSSHIdentity struct { |
| 25 | pubKey ssh.PublicKey |
| 26 | recipient age.Recipient |
| 27 | pemBytes []byte |
| 28 | passphrase func() ([]byte, error) |
| 29 | |
| 30 | decrypted age.Identity |
| 31 | } |
| 32 | |
| 33 | // NewEncryptedSSHIdentity returns a new EncryptedSSHIdentity. |
| 34 | // |
| 35 | // pubKey must be the public key associated with the encrypted private key, and |
| 36 | // it must have type "ssh-ed25519" or "ssh-rsa". For OpenSSH encrypted files it |
| 37 | // can be extracted from an ssh.PassphraseMissingError, otherwise it can often |
| 38 | // be found in ".pub" files. |
| 39 | // |
| 40 | // pemBytes must be a valid input to ssh.ParseRawPrivateKeyWithPassphrase. |
| 41 | // passphrase is a callback that will be invoked by Unwrap when the passphrase |
| 42 | // is necessary. |
| 43 | func NewEncryptedSSHIdentity(pubKey ssh.PublicKey, pemBytes []byte, passphrase func() ([]byte, error)) (*EncryptedSSHIdentity, error) { |
| 44 | i := &EncryptedSSHIdentity{ |
| 45 | pubKey: pubKey, |
| 46 | pemBytes: pemBytes, |
| 47 | passphrase: passphrase, |
| 48 | } |
| 49 | switch t := pubKey.Type(); t { |
| 50 | case "ssh-ed25519": |
| 51 | r, err := NewEd25519Recipient(pubKey) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | i.recipient = r |
| 56 | case "ssh-rsa": |
| 57 | r, err := NewRSARecipient(pubKey) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | i.recipient = r |
| 62 | default: |
| 63 | return nil, fmt.Errorf("unsupported SSH key type: %v", t) |
| 64 | } |
| 65 | return i, nil |
| 66 | } |
| 67 | |
| 68 | var _ age.Identity = &EncryptedSSHIdentity{} |
| 69 | |
| 70 | func (i *EncryptedSSHIdentity) Recipient() age.Recipient { |
| 71 | return i.recipient |
| 72 | } |
| 73 | |
| 74 | // Unwrap implements age.Identity. If the private key is still encrypted, and |
| 75 | // any of the stanzas match the public key, it will request the passphrase. The |
| 76 | // decrypted private key will be cached after the first successful invocation. |
| 77 | func (i *EncryptedSSHIdentity) Unwrap(stanzas []*age.Stanza) (fileKey []byte, err error) { |
| 78 | if i.decrypted != nil { |
| 79 | return i.decrypted.Unwrap(stanzas) |
| 80 | } |
| 81 | |
| 82 | var match bool |
| 83 | for _, s := range stanzas { |
| 84 | if s.Type != i.pubKey.Type() { |
| 85 | continue |
| 86 | } |
| 87 | if len(s.Args) < 1 { |
| 88 | return nil, fmt.Errorf("invalid %v recipient block", i.pubKey.Type()) |
| 89 | } |
| 90 | if s.Args[0] != sshFingerprint(i.pubKey) { |
| 91 | continue |
| 92 | } |
| 93 | match = true |
| 94 | break |
| 95 | } |
| 96 | if !match { |
| 97 | return nil, age.ErrIncorrectIdentity |
| 98 | } |
| 99 | |
| 100 | passphrase, err := i.passphrase() |
| 101 | if err != nil { |
| 102 | return nil, fmt.Errorf("failed to obtain passphrase: %v", err) |
| 103 | } |
| 104 | k, err := ssh.ParseRawPrivateKeyWithPassphrase(i.pemBytes, passphrase) |
| 105 | if err != nil { |
| 106 | return nil, fmt.Errorf("failed to decrypt SSH key file: %v", err) |
| 107 | } |
| 108 | |
| 109 | var pubKey interface { |
| 110 | Equal(x crypto.PublicKey) bool |
| 111 | } |
| 112 | var decrypted age.Identity |
| 113 | switch k := k.(type) { |
| 114 | case *ed25519.PrivateKey: |
| 115 | decrypted, err = NewEd25519Identity(*k) |
| 116 | pubKey = k.Public().(ed25519.PublicKey) |
| 117 | // ParseRawPrivateKey returns inconsistent types. See Issue 429. |
| 118 | case ed25519.PrivateKey: |
| 119 | decrypted, err = NewEd25519Identity(k) |
| 120 | pubKey = k.Public().(ed25519.PublicKey) |
| 121 | case *rsa.PrivateKey: |
| 122 | decrypted, err = NewRSAIdentity(k) |
| 123 | pubKey = &k.PublicKey |
| 124 | default: |
| 125 | return nil, fmt.Errorf("unexpected SSH key type: %T", k) |
| 126 | } |
| 127 | if err != nil { |
| 128 | return nil, fmt.Errorf("invalid SSH key: %v", err) |
| 129 | } |
| 130 | |
| 131 | if exp := i.pubKey.(ssh.CryptoPublicKey).CryptoPublicKey(); !pubKey.Equal(exp) { |
| 132 | return nil, fmt.Errorf("mismatched private and public SSH key") |
| 133 | } |
| 134 | |
| 135 | i.decrypted = decrypted |
| 136 | return i.decrypted.Unwrap(stanzas) |
| 137 | } |