-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathssh_private_key_credential.go
217 lines (191 loc) · 6.65 KB
/
ssh_private_key_credential.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package static
import (
"context"
"github.com/hashicorp/boundary/internal/credential"
"github.com/hashicorp/boundary/internal/credential/static/store"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/libs/crypto"
"github.com/hashicorp/boundary/internal/oplog"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"golang.org/x/crypto/ssh"
"google.golang.org/protobuf/proto"
)
var _ credential.Static = (*SshPrivateKeyCredential)(nil)
// A SshPrivateKeyCredential contains the credential with a username and private key.
// It is owned by a credential store.
type SshPrivateKeyCredential struct {
*store.SshPrivateKeyCredential
tableName string `gorm:"-"`
PassphraseUnneeded bool `gorm:"-"`
}
// NewSshPrivateKeyCredential creates a new in memory static Credential containing a
// username and private key that is assigned to storeId. Name and description are the only
// valid options. All other options are ignored.
func NewSshPrivateKeyCredential(
ctx context.Context,
storeId string,
username string,
privateKey credential.PrivateKey,
opt ...Option,
) (*SshPrivateKeyCredential, error) {
const op = "static.NewSshPrivateKeyCredential"
opts := getOpts(opt...)
if len(privateKey) != 0 {
var err error
if len(opts.withPrivateKeyPassphrase) == 0 {
_, err = ssh.ParsePrivateKey(privateKey)
} else {
_, err = ssh.ParsePrivateKeyWithPassphrase(privateKey, opts.withPrivateKeyPassphrase)
}
switch {
case err == nil:
case err.Error() == (&ssh.PassphraseMissingError{}).Error():
// This is okay, if it's brokered and the client can use it, no worries
default:
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter))
}
}
l := &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
StoreId: storeId,
Name: opts.withName,
Description: opts.withDescription,
Username: username,
PrivateKey: privateKey,
PrivateKeyPassphrase: opts.withPrivateKeyPassphrase,
},
}
// If a private key was given and no passphrase was given and everything is
// okay, we can nil out any passphrase we have, so set this as a hint
if len(privateKey) != 0 && len(opts.withPrivateKeyPassphrase) == 0 {
l.PassphraseUnneeded = true
// These shouldn't be set, but safety
l.PrivateKeyPassphrase = nil
l.PrivateKeyPassphraseEncrypted = nil
l.PrivateKeyPassphraseHmac = nil
}
return l, nil
}
func allocSshPrivateKeyCredential() *SshPrivateKeyCredential {
return &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{},
}
}
func (c *SshPrivateKeyCredential) clone() *SshPrivateKeyCredential {
cp := proto.Clone(c.SshPrivateKeyCredential)
return &SshPrivateKeyCredential{
SshPrivateKeyCredential: cp.(*store.SshPrivateKeyCredential),
}
}
// TableName returns the table name.
func (c *SshPrivateKeyCredential) TableName() string {
if c.tableName != "" {
return c.tableName
}
return "credential_static_ssh_private_key_credential"
}
// SetTableName sets the table name.
func (c *SshPrivateKeyCredential) SetTableName(n string) {
c.tableName = n
}
func (c *SshPrivateKeyCredential) oplog(op oplog.OpType) oplog.Metadata {
metadata := oplog.Metadata{
"resource-public-id": []string{c.PublicId},
"resource-type": []string{"credential-static-ssh-private-key"},
"op-type": []string{op.String()},
}
if c.StoreId != "" {
metadata["store-id"] = []string{c.StoreId}
}
return metadata
}
func (c *SshPrivateKeyCredential) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "static.(SshPrivateKeyCredential).encrypt"
if len(c.PrivateKey) == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "no private key defined")
}
keyId, err := cipher.KeyId(ctx)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("error reading cipher key id"))
}
c.KeyId = keyId
// Encrypt private key
blobInfo, err := cipher.Encrypt(ctx, c.PrivateKey)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
protoBytes, err := proto.Marshal(blobInfo)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encode))
}
c.PrivateKeyEncrypted = protoBytes
if err := c.hmacPrivateKey(ctx, cipher); err != nil {
return errors.Wrap(ctx, err, op)
}
if len(c.PrivateKeyPassphrase) > 0 {
// Encrypt passphrase
blobInfo, err := cipher.Encrypt(ctx, c.PrivateKeyPassphrase)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
protoBytes, err := proto.Marshal(blobInfo)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encode))
}
c.PrivateKeyPassphraseEncrypted = protoBytes
if err := c.hmacPrivateKeyPassphrase(ctx, cipher); err != nil {
return errors.Wrap(ctx, err, op)
}
}
return nil
}
func (c *SshPrivateKeyCredential) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "static.(SshPrivateKeyCredential).decrypt"
if len(c.PrivateKeyEncrypted) > 0 {
dec := new(wrapping.BlobInfo)
if err := proto.Unmarshal(c.PrivateKeyEncrypted, dec); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decode))
}
pt, err := cipher.Decrypt(ctx, dec)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
}
c.PrivateKey = pt
}
if len(c.PrivateKeyPassphraseEncrypted) > 0 {
dec := new(wrapping.BlobInfo)
if err := proto.Unmarshal(c.PrivateKeyPassphraseEncrypted, dec); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decode))
}
pt, err := cipher.Decrypt(ctx, dec)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
}
c.PrivateKeyPassphrase = pt
}
return nil
}
func (c *SshPrivateKeyCredential) hmacPrivateKey(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "static.(SshPrivateKeyCredential).hmacPrivateKey"
if cipher == nil {
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
}
hm, err := crypto.HmacSha256(ctx, c.PrivateKey, cipher, []byte(c.StoreId), nil)
if err != nil {
return errors.Wrap(ctx, err, op)
}
c.PrivateKeyHmac = []byte(hm)
return nil
}
func (c *SshPrivateKeyCredential) hmacPrivateKeyPassphrase(ctx context.Context, cipher wrapping.Wrapper) error {
const op = "static.(SshPrivateKeyCredential).hmacPrivateKeyPassphrase"
if cipher == nil {
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
}
hm, err := crypto.HmacSha256(ctx, c.PrivateKeyPassphrase, cipher, []byte(c.StoreId), nil)
if err != nil {
return errors.Wrap(ctx, err, op)
}
c.PrivateKeyPassphraseHmac = []byte(hm)
return nil
}