Add support for SSH private key passphrases (#2331)

This adds support for encrypted SSH keys via passphrase.

Co-authored-by: Louis Ruch <louisruch@gmail.com>
Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2022-08-08 15:15:03 -04:00
1 parent 07bca9dd66
commit 02dd28f587
45 files changed
+1921 -382

No files matched your search

+6 -5
View File
@@ -2,9 +2,10 @@ package static
// These constants are the field names used in the static related field masks.
const (
nameField = "Name"
descriptionField = "Description"
usernameField = "Username"
passwordField = "Password"
privateKeyField = "PrivateKey"
nameField = "Name"
descriptionField = "Description"
usernameField = "Username"
passwordField = "Password"
privateKeyField = "PrivateKey"
PrivateKeyPassphraseField = "PrivateKeyPassphrase"
)
+12 -4
View File
@@ -14,10 +14,11 @@ type Option func(*options)
// options = how options are represented
type options struct {
withName string
withDescription string
withLimit int
withPublicId string
withName string
withDescription string
withLimit int
withPublicId string
withPrivateKeyPassphrase []byte
}
func getDefaultOptions() options {
@@ -53,3 +54,10 @@ func WithPublicId(name string) Option {
o.withPublicId = name
}
}
// WithPrivateKeyPassphrase provides an optional SSH private key passphrase to use.
func WithPrivateKeyPassphrase(with []byte) Option {
return func(o *options) {
o.withPrivateKeyPassphrase = with
}
}
@@ -26,4 +26,11 @@ func Test_GetOpts(t *testing.T) {
testOpts.withLimit = 5
assert.Equal(t, opts, testOpts)
})
t.Run("WithPrivateKeyPassphrase", func(t *testing.T) {
opts := getOpts(WithPrivateKeyPassphrase([]byte("my-pass")))
testOpts := getDefaultOptions()
assert.NotEqual(t, opts, testOpts)
testOpts.withPrivateKeyPassphrase = []byte("my-pass")
assert.Equal(t, opts, testOpts)
})
}
@@ -106,8 +106,10 @@ func (r *Repository) CreateUsernamePasswordCredential(
// assigned by this method. c must contain a valid StoreId.
//
// The private key is encrypted and a HmacSha256 of the private key is
// calculated. Only the PrivateKeyHmac is returned, the plain-text and encrypted
// private key is not returned.
// calculated. If a passphrase is supplied, it is also encrypted and an HmacSha256
// of passphrase is calculated. Only the PrivateKeyHmac (and
// PrivateKeyPassphraseHmac) is returned, the plain-text and encrypted private
// key and passphrase are not returned.
//
// Both c.Name and c.Description are optional. If c.Name is set, it must be
// unique within c.ScopeId. Both c.CreateTime and c.UpdateTime are ignored.
@@ -183,6 +185,10 @@ func (r *Repository) CreateSshPrivateKeyCredential(
newCred.PrivateKeyEncrypted = nil
newCred.PrivateKey = nil
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
newCred.PrivateKeyPassphraseEncrypted = nil
newCred.PrivateKeyPassphrase = nil
return newCred, nil
}
@@ -224,6 +230,9 @@ func (r *Repository) LookupCredential(ctx context.Context, publicId string, _ ..
// Clear private key fields, only privateKeyHmac should be returned
spkCred.PrivateKeyEncrypted = nil
spkCred.PrivateKey = nil
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
spkCred.PrivateKeyPassphraseEncrypted = nil
spkCred.PrivateKeyPassphrase = nil
cred = spkCred
}
@@ -349,9 +358,9 @@ func (r *Repository) UpdateUsernamePasswordCredential(ctx context.Context,
// new SshPrivateKeyCredential containing the updated values and a count of the
// number of records updated. c is not changed.
//
// c must contain a valid PublicId. Only Name, Description, Username and
// PrivateKey can be changed. If c.Name is set to a non-empty string, it must be
// unique within c.ScopeId.
// c must contain a valid PublicId. Only Name, Description, Username,
// PrivateKey and PrivateKeyPassphrase can be changed. If c.Name is set to a non-empty string, it
// must be unique within c.ScopeId.
//
// An attribute of c will be set to NULL in the database if the attribute in c
// is the zero value and it is included in fieldMaskPaths.
@@ -389,16 +398,18 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
case strings.EqualFold(descriptionField, f):
case strings.EqualFold(usernameField, f):
case strings.EqualFold(privateKeyField, f):
case strings.EqualFold(PrivateKeyPassphraseField, f):
default:
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, f)
}
}
dbMask, nullFields := dbcommon.BuildUpdatePaths(
map[string]interface{}{
nameField: c.Name,
descriptionField: c.Description,
usernameField: c.Username,
privateKeyField: c.PrivateKey,
nameField: c.Name,
descriptionField: c.Description,
usernameField: c.Username,
privateKeyField: c.PrivateKey,
PrivateKeyPassphraseField: c.PrivateKeyPassphrase,
},
fieldMaskPaths,
nil,
@@ -407,19 +418,30 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "missing field mask")
}
var performedEncryption bool
for _, f := range fieldMaskPaths {
if strings.EqualFold(privateKeyField, f) {
// Private key has been updated, re-encrypt and recalculate hmac
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
if err := c.encrypt(ctx, databaseWrapper); err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
if strings.EqualFold(privateKeyField, f) || strings.EqualFold(PrivateKeyPassphraseField, f) {
if !performedEncryption {
// We don't need to encrypt twice so keep track
performedEncryption = true
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
if err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
if err := c.encrypt(ctx, databaseWrapper); err != nil {
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
}
}
// Set PrivateKeyHmac and PrivateKeyEncrypted masks for update.
dbMask = append(dbMask, "PrivateKeyHmac", "PrivateKeyEncrypted")
if strings.EqualFold(privateKeyField, f) {
// Set PrivateKeyHmac and PrivateKeyEncrypted masks for update.
dbMask = append(dbMask, "PrivateKeyHmac", "PrivateKeyEncrypted")
}
if strings.EqualFold(PrivateKeyPassphraseField, f) {
// Set PassphraseHmac and PassphraseEncrypted masks for update.
dbMask = append(dbMask, "PrivateKeyPassphraseHmac", "PrivateKeyPassphraseEncrypted")
}
}
}
@@ -456,6 +478,10 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
returnedCredential.PrivateKeyEncrypted = nil
returnedCredential.PrivateKey = nil
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
returnedCredential.PrivateKeyPassphraseEncrypted = nil
returnedCredential.PrivateKeyPassphrase = nil
return returnedCredential, rowsUpdated, nil
}
@@ -499,6 +525,10 @@ func (r *Repository) ListCredentials(ctx context.Context, storeId string, opt ..
// Clear private key fields, only PrivateKeyHmac should be returned
c.PrivateKeyEncrypted = nil
c.PrivateKey = nil
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
c.PrivateKeyPassphraseEncrypted = nil
c.PrivateKeyPassphrase = nil
ret = append(ret, c)
}
@@ -16,6 +16,7 @@ import (
"github.com/hashicorp/boundary/internal/oplog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh/testdata"
)
func TestRepository_CreateUsernamePasswordCredential(t *testing.T) {
@@ -276,6 +277,18 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
},
},
},
{
name: "valid-with-passphrase",
scopeId: prj.PublicId,
cred: &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
Username: "my-user",
PrivateKey: []byte(TestSshPrivateKeyPem),
StoreId: cs.PublicId,
PrivateKeyPassphrase: []byte("passphrase"),
},
},
},
}
for _, tt := range tests {
tt := tt
@@ -298,8 +311,10 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
assert.Equal(tt.cred.Username, got.Username)
assert.Nil(got.PrivateKey)
assert.Nil(got.PrivateKeyEncrypted)
assert.Nil(got.PrivateKeyPassphrase)
assert.Nil(got.PrivateKeyPassphraseEncrypted)
// Validate password
// Validate private key and passphrase
lookupCred := allocSshPrivateKeyCredential()
lookupCred.PublicId = got.PublicId
require.NoError(rw.LookupById(ctx, lookupCred))
@@ -318,6 +333,15 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
require.NoError(err)
assert.Equal([]byte(hm), got.PrivateKeyHmac)
// Validate passphrase
assert.Equal(tt.cred.PrivateKeyPassphrase, lookupCred.PrivateKeyPassphrase)
if len(tt.cred.PrivateKeyPassphrase) > 0 {
assert.NotEmpty(got.PrivateKeyPassphraseHmac)
hm, err := crypto.HmacSha256(ctx, tt.cred.PrivateKeyPassphrase, databaseWrapper, []byte(tt.cred.StoreId), nil)
require.NoError(err)
assert.Equal([]byte(hm), got.PrivateKeyPassphraseHmac)
}
// Validate oplog
assert.NoError(db.TestVerifyOplog(t, rw, got.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
})
@@ -372,6 +396,8 @@ func TestRepository_LookupCredential(t *testing.T) {
store := TestCredentialStore(t, conn, wrapper, prj.PublicId)
upCred := TestUsernamePasswordCredential(t, conn, wrapper, "username", "password", store.PublicId, prj.PublicId)
spkCred := TestSshPrivateKeyCredential(t, conn, wrapper, "username", TestSshPrivateKeyPem, store.PublicId, prj.PublicId)
spkCredWithPass := TestSshPrivateKeyCredential(t, conn, wrapper, "username", string(testdata.PEMEncryptedKeys[0].PEMBytes),
store.PublicId, prj.PublicId, WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
tests := []struct {
name string
@@ -389,6 +415,11 @@ func TestRepository_LookupCredential(t *testing.T) {
id: spkCred.GetPublicId(),
want: spkCred,
},
{
name: "spk-valid-with-passphrase",
id: spkCredWithPass.GetPublicId(),
want: spkCredWithPass,
},
{
name: "empty-public-id",
id: "",
@@ -433,6 +464,14 @@ func TestRepository_LookupCredential(t *testing.T) {
assert.Empty(v.PrivateKey)
assert.Empty(v.PrivateKeyEncrypted)
assert.NotEmpty(v.PrivateKeyHmac)
want, ok := tt.want.(*SshPrivateKeyCredential)
require.True(ok)
assert.Empty(v.PrivateKeyPassphrase)
assert.Empty(v.PrivateKeyPassphraseEncrypted)
if len(want.PrivateKeyPassphrase) > 0 {
assert.NotEmpty(v.PrivateKeyPassphraseHmac)
}
default:
require.Fail("unknown type")
}
@@ -1175,6 +1214,13 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
}
}
changePrivateKeyPassphrase := func(d string) func(*SshPrivateKeyCredential) *SshPrivateKeyCredential {
return func(c *SshPrivateKeyCredential) *SshPrivateKeyCredential {
c.PrivateKeyPassphrase = []byte(d)
return c
}
}
combine := func(fns ...func(cs *SshPrivateKeyCredential) *SshPrivateKeyCredential) func(*SshPrivateKeyCredential) *SshPrivateKeyCredential {
return func(c *SshPrivateKeyCredential) *SshPrivateKeyCredential {
for _, fn := range fns {
@@ -1385,7 +1431,7 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
wantCount: 1,
},
{
name: "change-private-keyu",
name: "change-private-key",
orig: &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
Username: "user",
@@ -1402,6 +1448,26 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
},
wantCount: 1,
},
{
name: "change-private-key-and-passphrase",
orig: &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
Username: "user",
PrivateKey: []byte(TestSshPrivateKeyPem),
PrivateKeyPassphrase: []byte("foobar"),
},
},
chgFn: combine(changePrivateKey(testSecondarySshPrivateKeyPem), changePrivateKeyPassphrase("barfoo")),
masks: []string{"PrivateKeyPassphrase"},
want: &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
Username: "user",
PrivateKey: []byte(testSecondarySshPrivateKeyPem),
PrivateKeyPassphrase: []byte("barfoo"),
},
},
wantCount: 1,
},
{
name: "change-username-and-private-key",
orig: &SshPrivateKeyCredential{
@@ -1620,3 +1686,76 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
})
}
}
func TestSshPrivateKeyConstraints(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
assert, require := assert.New(t), require.New(t)
ctx := context.Background()
kkms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(ctx, rw, rw, kkms)
assert.NoError(err)
require.NotNil(repo)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
store := TestCredentialStore(t, conn, wrapper, prj.GetPublicId())
// Base case: should work fine
cred, err := NewSshPrivateKeyCredential(
ctx,
store.PublicId,
"foobar",
credential.PrivateKey(testdata.PEMEncryptedKeys[0].PEMBytes),
WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
require.NoError(err)
cred.PublicId, err = credential.NewSshPrivateKeyCredentialId(ctx)
require.NoError(err)
databaseWrapper, err := kkms.GetWrapper(ctx, prj.PublicId, kms.KeyPurposeDatabase)
require.NoError(cred.encrypt(ctx, databaseWrapper))
tests := []struct {
name string
nilEncrypted bool
nilHmac bool
}{
{
name: "valid",
},
{
name: "nil-encrypted",
nilEncrypted: true,
},
{
name: "nil-hmac",
nilHmac: true,
},
{
name: "valid-both-nil",
nilEncrypted: true,
nilHmac: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
c := cred.clone()
if tt.nilEncrypted {
c.PrivateKeyPassphraseEncrypted = nil
}
if tt.nilHmac {
c.PrivateKeyPassphraseHmac = nil
}
c.PublicId, err = credential.NewSshPrivateKeyCredentialId(ctx)
require.NoError(err)
err := rw.Create(ctx, c)
switch {
case !tt.nilEncrypted && !tt.nilHmac, tt.nilEncrypted && tt.nilHmac:
assert.NoError(err)
default:
assert.Error(err)
}
})
}
}
@@ -35,6 +35,9 @@ func TestRepository_Retrieve(t *testing.T) {
upCred2 := TestUsernamePasswordCredential(t, conn, wrapper, "different user", "better password", staticStore.GetPublicId(), prj.GetPublicId())
spkCred1 := TestSshPrivateKeyCredential(t, conn, wrapper, "final user", string(testdata.PEMBytes["ed25519"]), staticStore.GetPublicId(), prj.GetPublicId())
spkCred2 := TestSshPrivateKeyCredential(t, conn, wrapper, "last user", string(testdata.PEMBytes["rsa-openssh-format"]), staticStore.GetPublicId(), prj.GetPublicId())
spkCredWithPass := TestSshPrivateKeyCredential(t, conn, wrapper, "another last user",
string(testdata.PEMEncryptedKeys[0].PEMBytes), staticStore.GetPublicId(), prj.GetPublicId(),
WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
type args struct {
credIds []string
@@ -95,20 +98,20 @@ func TestRepository_Retrieve(t *testing.T) {
name: "valid-multiple-ssh-pk-creds",
args: args{
scopeId: prj.GetPublicId(),
credIds: []string{spkCred1.GetPublicId(), spkCred2.GetPublicId()},
credIds: []string{spkCred1.GetPublicId(), spkCred2.GetPublicId(), spkCredWithPass.GetPublicId()},
},
wantCreds: []credential.Static{
spkCred1, spkCred2,
spkCred1, spkCred2, spkCredWithPass,
},
},
{
name: "valid-mixed-creds",
args: args{
scopeId: prj.GetPublicId(),
credIds: []string{upCred1.GetPublicId(), spkCred1.GetPublicId(), spkCred2.GetPublicId(), upCred2.GetPublicId()},
credIds: []string{upCred1.GetPublicId(), spkCred1.GetPublicId(), spkCredWithPass.GetPublicId(), spkCred2.GetPublicId(), upCred2.GetPublicId()},
},
wantCreds: []credential.Static{
upCred1, spkCred1, spkCred2, upCred2,
upCred1, spkCred1, spkCred2, upCred2, spkCredWithPass,
},
},
}
@@ -130,6 +133,7 @@ func TestRepository_Retrieve(t *testing.T) {
UsernamePasswordCredential{}, store.UsernamePasswordCredential{},
SshPrivateKeyCredential{}, store.SshPrivateKeyCredential{}),
cmpopts.IgnoreTypes(&timestamp.Timestamp{}),
cmpopts.IgnoreFields(SshPrivateKeyCredential{}, "PassphraseUnneeded"),
cmpopts.SortSlices(func(x, y credential.Static) bool {
return x.GetPublicId() < y.GetPublicId()
}),
@@ -9,7 +9,6 @@ import (
"github.com/hashicorp/boundary/internal/libs/crypto"
"github.com/hashicorp/boundary/internal/oplog"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/go-kms-wrapping/v2/extras/structwrapping"
"golang.org/x/crypto/ssh"
"google.golang.org/protobuf/proto"
)
@@ -20,7 +19,8 @@ var _ credential.Static = (*SshPrivateKeyCredential)(nil)
// It is owned by a credential store.
type SshPrivateKeyCredential struct {
*store.SshPrivateKeyCredential
tableName string `gorm:"-"`
tableName string `gorm:"-"`
PassphraseUnneeded bool `gorm:"-"`
}
// NewSshPrivateKeyCredential creates a new in memory static Credential containing a
@@ -35,8 +35,14 @@ func NewSshPrivateKeyCredential(
) (*SshPrivateKeyCredential, error) {
const op = "static.NewSshPrivateKeyCredential"
opts := getOpts(opt...)
if len(privateKey) != 0 {
_, err := ssh.ParsePrivateKey(privateKey)
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():
@@ -46,16 +52,27 @@ func NewSshPrivateKeyCredential(
}
}
opts := getOpts(opt...)
l := &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
StoreId: storeId,
Name: opts.withName,
Description: opts.withDescription,
Username: username,
PrivateKey: privateKey,
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
}
@@ -99,28 +116,77 @@ func (c *SshPrivateKeyCredential) oplog(op oplog.OpType) oplog.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")
}
if err := structwrapping.WrapStruct(ctx, cipher, c.SshPrivateKeyCredential, nil); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
}
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 err := structwrapping.UnwrapStruct(ctx, cipher, c.SshPrivateKeyCredential, nil); err != nil {
return errors.Wrap(ctx, err, op, errors.WithCode(errors.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
}
@@ -136,3 +202,16 @@ func (c *SshPrivateKeyCredential) hmacPrivateKey(ctx context.Context, cipher wra
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
}
@@ -13,6 +13,7 @@ import (
"github.com/hashicorp/boundary/internal/libs/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh/testdata"
"google.golang.org/protobuf/testing/protocmp"
)
@@ -24,6 +25,8 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
rw := db.New(conn)
privKey := []byte(TestSshPrivateKeyPem)
ePrivKey := testdata.PEMEncryptedKeys[0].PEMBytes
ePrivKeyPass := testdata.PEMEncryptedKeys[0].EncryptionKey
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
cs := TestCredentialStore(t, conn, wrapper, prj.PublicId)
@@ -129,6 +132,33 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
},
},
},
{
name: "invalid-passphrase",
args: args{
username: "test-user",
sshPrivateKey: privKey,
storeId: cs.PublicId,
options: []Option{WithPrivateKeyPassphrase([]byte(ePrivKeyPass))},
},
wantAllocError: true,
},
{
name: "valid-with-passphrase",
args: args{
username: "test-user",
sshPrivateKey: ePrivKey,
storeId: cs.PublicId,
options: []Option{WithPrivateKeyPassphrase([]byte(ePrivKeyPass))},
},
want: &SshPrivateKeyCredential{
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
Username: "test-user",
PrivateKey: ePrivKey,
StoreId: cs.PublicId,
PrivateKeyPassphrase: []byte(ePrivKeyPass),
},
},
},
}
for _, tt := range tests {
@@ -184,12 +214,20 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
// KeyId is allocated via kms no need to validate in this test
tt.want.KeyId = got2.KeyId
got2.PrivateKeyEncrypted = nil
got2.PrivateKeyPassphraseEncrypted = nil
// encrypt also calculates the hmac, validate it is correct
hm, err := crypto.HmacSha256(ctx, got.PrivateKey, databaseWrapper, []byte(got.StoreId), nil)
require.NoError(err)
tt.want.PrivateKeyHmac = []byte(hm)
if len(got.PrivateKeyPassphrase) > 0 {
// encrypt also calculates the hmac, validate it is correct
hm, err = crypto.HmacSha256(ctx, got.PrivateKeyPassphrase, databaseWrapper, []byte(got.StoreId), nil)
require.NoError(err)
tt.want.PrivateKeyPassphraseHmac = []byte(hm)
}
assert.Empty(cmp.Diff(tt.want, got2.clone(), protocmp.Transform()))
})
}
+61 -7
View File
@@ -347,6 +347,19 @@ type SshPrivateKeyCredential struct {
// It must be set.
// @inject_tag: `gorm:"not_null"`
KeyId string `protobuf:"bytes,12,opt,name=key_id,json=keyId,proto3" json:"key_id,omitempty" gorm:"not_null"`
// private_key_passphrase is the plain-text of the passphrase of the SSH private
// key associated with the credential. We are not storing this plain-text
// private key passphrase in the database.
// @inject_tag: `gorm:"-"`
PrivateKeyPassphrase []byte `protobuf:"bytes,13,opt,name=private_key_passphrase,json=privateKeyPassphrase,proto3" json:"private_key_passphrase,omitempty" gorm:"-"`
// private_key_passphrase_encrypted is the ciphertext of the private key passphrase. It is stored in
// the database.
// @inject_tag: `gorm:"column:private_key_passphrase_encrypted;not_null"`
PrivateKeyPassphraseEncrypted []byte `protobuf:"bytes,14,opt,name=private_key_passphrase_encrypted,json=privateKeyPassphraseEncrypted,proto3" json:"private_key_passphrase_encrypted,omitempty" gorm:"column:private_key_passphrase_encrypted;not_null"`
// private_key_passphrase_hmac is a sha256-hmac of the unencrypted private key passphrase. It is recalculated
// everytime the private key passphrase is updated.
// @inject_tag: `gorm:"not_null"`
PrivateKeyPassphraseHmac []byte `protobuf:"bytes,15,opt,name=private_key_passphrase_hmac,json=privateKeyPassphraseHmac,proto3" json:"private_key_passphrase_hmac,omitempty" gorm:"not_null"`
}
func (x *SshPrivateKeyCredential) Reset() {
@@ -465,6 +478,27 @@ func (x *SshPrivateKeyCredential) GetKeyId() string {
return ""
}
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphrase() []byte {
if x != nil {
return x.PrivateKeyPassphrase
}
return nil
}
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphraseEncrypted() []byte {
if x != nil {
return x.PrivateKeyPassphraseEncrypted
}
return nil
}
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphraseHmac() []byte {
if x != nil {
return x.PrivateKeyPassphraseHmac
}
return nil
}
var File_controller_storage_credential_static_store_v1_static_proto protoreflect.FileDescriptor
var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []byte{
@@ -543,7 +577,7 @@ var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x68, 0x6d, 0x61, 0x63, 0x52, 0x0c, 0x70, 0x61, 0x73,
0x73, 0x77, 0x6f, 0x72, 0x64, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x15, 0x0a, 0x06, 0x6b, 0x65, 0x79,
0x5f, 0x69, 0x64, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6b, 0x65, 0x79, 0x49, 0x64,
0x22, 0xa1, 0x05, 0x0a, 0x17, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b,
0x22, 0xe7, 0x07, 0x0a, 0x17, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b,
0x65, 0x79, 0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x12, 0x1b, 0x0a, 0x09,
0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x08, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x49, 0x64, 0x12, 0x4b, 0x0a, 0x0b, 0x63, 0x72, 0x65,
@@ -585,12 +619,32 @@ var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x68, 0x6d, 0x61, 0x63, 0x52, 0x0e, 0x70,
0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x15, 0x0a,
0x06, 0x6b, 0x65, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6b,
0x65, 0x79, 0x49, 0x64, 0x42, 0x46, 0x5a, 0x44, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75,
0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63,
0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63,
0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
0x65, 0x79, 0x49, 0x64, 0x12, 0x73, 0x0a, 0x16, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f,
0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x18, 0x0d,
0x20, 0x01, 0x28, 0x0c, 0x42, 0x3d, 0xc2, 0xdd, 0x29, 0x39, 0x0a, 0x14, 0x50, 0x72, 0x69, 0x76,
0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65,
0x12, 0x21, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x69,
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72,
0x61, 0x73, 0x65, 0x52, 0x14, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50,
0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x12, 0x47, 0x0a, 0x20, 0x70, 0x72, 0x69,
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72,
0x61, 0x73, 0x65, 0x5f, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x18, 0x0e, 0x20,
0x01, 0x28, 0x0c, 0x52, 0x1d, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50,
0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x45, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74,
0x65, 0x64, 0x12, 0x85, 0x01, 0x0a, 0x1b, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b,
0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x5f, 0x68, 0x6d,
0x61, 0x63, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x46, 0xc2, 0xdd, 0x29, 0x42, 0x0a, 0x18,
0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73, 0x70, 0x68,
0x72, 0x61, 0x73, 0x65, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x26, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62,
0x75, 0x74, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79,
0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x5f, 0x68, 0x6d, 0x61, 0x63,
0x52, 0x18, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73,
0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x48, 0x6d, 0x61, 0x63, 0x42, 0x46, 0x5a, 0x44, 0x67, 0x69,
0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f,
0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65,
0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f,
0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f,
0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (