Debian Codegraph @

agessh/encrypted_keys.go

raw symbols SCIP
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 agesshscip-go gomod filippo.io/age . `filippo.io/age/agessh`/
6
7 import (
8 "cryptoscip-go gomod github.com/golang/go/src . crypto/"
9 "crypto/ed25519scip-go gomod github.com/golang/go/src . `crypto/ed25519`/"
10 "crypto/rsascip-go gomod github.com/golang/go/src . `crypto/rsa`/"
11 "fmtscip-go gomod github.com/golang/go/src . fmt/"
12
13 "filippo.io/agescip-go gomod filippo.io/age . `filippo.io/age`/"
14 "golang.org/x/crypto/sshscip-go gomod golang.org/x/crypto v0.55.0 `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 EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity# struct {
25 pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey. sshscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/.PublicKeyscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/PublicKey#
26 recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#recipient. agescip-go gomod filippo.io/age . `filippo.io/age`/.Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient#
27 pemBytesscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pemBytes. []byte
28 passphrasescip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#passphrase. func() ([]byte, error)
29
30 decryptedscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#decrypted. agescip-go gomod filippo.io/age . `filippo.io/age`/.Identityscip-go gomod filippo.io/age . `filippo.io/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 NewEncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewEncryptedSSHIdentity().(pubKey sshscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/.PublicKeyscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/PublicKey#, pemBytes []byte, passphrase func() ([]byte, error)) (*EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#, error) {
44 i := &EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#{
45 pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey.: pubKey,
46 pemBytesscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pemBytes.: pemBytes,
47 passphrasescip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#passphrase.: passphrase,
48 }
49 switch t := pubKey.Typescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/PublicKey#Type().(); t {
50 case "ssh-ed25519":
51 r, err := NewEd25519Recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewEd25519Recipient().(pubKey)
52 if err != nil {
53 return nil, err
54 }
55 i.recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#recipient. = r
56 case "ssh-rsa":
57 r, err := NewRSARecipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewRSARecipient().(pubKey)
58 if err != nil {
59 return nil, err
60 }
61 i.recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#recipient. = r
62 default:
63 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("unsupported SSH key type: %v", t)
64 }
65 return i, nil
66 }
67
68 var _ agescip-go gomod filippo.io/age . `filippo.io/age`/.Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity# = &EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#{}
69
70 func (i *EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#) Recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#Recipient().() agescip-go gomod filippo.io/age . `filippo.io/age`/.Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient# {
71 return i.recipientscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#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 *EncryptedSSHIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#) Unwrapscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#Unwrap().(stanzas []*agescip-go gomod filippo.io/age . `filippo.io/age`/.Stanzascip-go gomod filippo.io/age . `filippo.io/age`/Stanza#) (fileKey []byte, err error) {
78 if i.decryptedscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#decrypted. != nil {
79 return i.decryptedscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#decrypted..Unwrapscip-go gomod filippo.io/age . `filippo.io/age`/Identity#Unwrap.(stanzas)
80 }
81
82 var match bool
83 for _, s := range stanzas {
84 if s.Typescip-go gomod filippo.io/age . `filippo.io/age`/Stanza#Type. != i.pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey..Typescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/PublicKey#Type().() {
85 continue
86 }
87 if len(s.Argsscip-go gomod filippo.io/age . `filippo.io/age`/Stanza#Args.) < 1 {
88 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("invalid %v recipient block", i.pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey..Typescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/PublicKey#Type().())
89 }
90 if s.Argsscip-go gomod filippo.io/age . `filippo.io/age`/Stanza#Args.[0] != sshFingerprintscip-go gomod filippo.io/age . `filippo.io/age/agessh`/sshFingerprint().(i.pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey.) {
91 continue
92 }
93 match = true
94 break
95 }
96 if !match {
97 return nil, agescip-go gomod filippo.io/age . `filippo.io/age`/.ErrIncorrectIdentityscip-go gomod filippo.io/age . `filippo.io/age`/ErrIncorrectIdentity.
98 }
99
100 passphrase, err := i.passphrasescip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#passphrase.()
101 if err != nil {
102 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to obtain passphrase: %v", err)
103 }
104 k, err := sshscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/.ParseRawPrivateKeyWithPassphrasescip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/ParseRawPrivateKeyWithPassphrase().(i.pemBytesscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pemBytes., passphrase)
105 if err != nil {
106 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to decrypt SSH key file: %v", err)
107 }
108
109 var pubKey interface {
110 Equal(x cryptoscip-go gomod github.com/golang/go/src . crypto/.PublicKeyscip-go gomod github.com/golang/go/src . crypto/PublicKey#) bool
111 }
112 var decrypted agescip-go gomod filippo.io/age . `filippo.io/age`/.Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity#
113 switch k := k.(type) {
114 case *ed25519scip-go gomod github.com/golang/go/src . `crypto/ed25519`/.PrivateKeyscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PrivateKey#:
115 decrypted, err = NewEd25519Identityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewEd25519Identity().(*k)
116 pubKey = k.Publicscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PrivateKey#Public().().(ed25519scip-go gomod github.com/golang/go/src . `crypto/ed25519`/.PublicKeyscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PublicKey#)
117 // ParseRawPrivateKey returns inconsistent types. See Issue 429.
118 case ed25519scip-go gomod github.com/golang/go/src . `crypto/ed25519`/.PrivateKeyscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PrivateKey#:
119 decrypted, err = NewEd25519Identityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewEd25519Identity().(k)
120 pubKey = k.Publicscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PrivateKey#Public().().(ed25519scip-go gomod github.com/golang/go/src . `crypto/ed25519`/.PublicKeyscip-go gomod github.com/golang/go/src . `crypto/ed25519`/PublicKey#)
121 case *rsascip-go gomod github.com/golang/go/src . `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src . `crypto/rsa`/PrivateKey#:
122 decrypted, err = NewRSAIdentityscip-go gomod filippo.io/age . `filippo.io/age/agessh`/NewRSAIdentity().(k)
123 pubKey = &k.PublicKeyscip-go gomod github.com/golang/go/src . `crypto/rsa`/PrivateKey#PublicKey.
124 default:
125 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("unexpected SSH key type: %T", k)
126 }
127 if err != nil {
128 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("invalid SSH key: %v", err)
129 }
130
131 if exp := i.pubKeyscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#pubKey..(sshscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/.CryptoPublicKeyscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/CryptoPublicKey#).CryptoPublicKeyscip-go gomod golang.org/x/crypto v0.55.0 `golang.org/x/crypto/ssh`/CryptoPublicKey#CryptoPublicKey().(); !pubKey.Equal(exp) {
132 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("mismatched private and public SSH key")
133 }
134
135 i.decryptedscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#decrypted. = decrypted
136 return i.decryptedscip-go gomod filippo.io/age . `filippo.io/age/agessh`/EncryptedSSHIdentity#decrypted..Unwrapscip-go gomod filippo.io/age . `filippo.io/age`/Identity#Unwrap.(stanzas)
137 }