Skip to content

Commit

Permalink
Add support for new ssh key cipher type
Browse files Browse the repository at this point in the history
  • Loading branch information
quexten committed Nov 6, 2024
1 parent eae9246 commit dd063cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli/agent/bitwarden/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ type Cipher struct {
Login *LoginCipher `json:"login,omitempty"`
Notes *crypto.EncString `json:"notes,omitempty"`
SecureNote *SecureNoteCipher `json:"secureNote,omitempty"`

SSHKey *SSHKeyCipher `json:"sshKey,omitempty"`

Key *crypto.EncString `json:"key,omitempty"`
}

Expand All @@ -88,8 +89,15 @@ const (
CipherCard = 3
CipherIdentity = 4
CipherNote = 2
CipherSSHKey = 5
)

type SSHKeyCipher struct {
PrivateKey crypto.EncString `json:"privateKey"`
PublicKey crypto.EncString `json:"publicKey"`
KeyFingerprint crypto.EncString `json:"keyFingerprint"`
}

type Card struct {
CardholderName crypto.EncString `json:"cardholderName"`
Brand crypto.EncString `json:"brand"`
Expand Down
2 changes: 2 additions & 0 deletions cli/agent/bitwarden/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func DoFullSync(ctx context.Context, vault *vault.Vault, config *config.Config,
vault.AddOrUpdateLogin(cipher)
case models.CipherNote:
vault.AddOrUpdateSecureNote(cipher)
case models.CipherSSHKey:
vault.AddOrUpdateSSHKey(cipher)
}
}

Expand Down
22 changes: 22 additions & 0 deletions cli/agent/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Vault struct {
Keyring *crypto.Keyring
logins map[string]models.Cipher
secureNotes map[string]models.Cipher
sshKeys map[string]models.Cipher
sshKeyNoteIDs []string
envCredentials map[string]string
lastSynced int64
Expand All @@ -31,6 +32,7 @@ func NewVault(keyring *crypto.Keyring) *Vault {
Keyring: keyring,
logins: make(map[string]models.Cipher),
secureNotes: make(map[string]models.Cipher),
sshKeys: make(map[string]models.Cipher),
sshKeyNoteIDs: make([]string, 0),
envCredentials: make(map[string]string),
lastSynced: 0,
Expand Down Expand Up @@ -92,6 +94,12 @@ func (vault *Vault) AddOrUpdateSecureNote(cipher models.Cipher) {
vault.unlockMutex()
}

func (vault *Vault) AddOrUpdateSSHKey(cipher models.Cipher) {
vault.lockMutex()
vault.sshKeys[cipher.ID.String()] = cipher
vault.unlockMutex()
}

func (vault *Vault) isEnv(cipher models.Cipher) (string, bool) {
if cipher.Type != models.CipherNote {
return "", false
Expand Down Expand Up @@ -258,6 +266,20 @@ func (vault *Vault) GetSSHKeys() []SSHKey {
PublicKey: string(publicKey),
})
}

for id, _ := range vault.sshKeys {
key, _ := vault.sshKeys[id].GetKeyForCipher(*vault.Keyring)
privKey, _ := crypto.DecryptWith(vault.sshKeys[id].SSHKey.PrivateKey, key)
pubKey, _ := crypto.DecryptWith(vault.sshKeys[id].SSHKey.PublicKey, key)
name, _ := crypto.DecryptWith(vault.sshKeys[id].Name, key)

sshKeys = append(sshKeys, SSHKey{
Name: string(name),
Key: string(privKey),
PublicKey: string(pubKey),
})
}

return sshKeys
}

Expand Down

0 comments on commit dd063cc

Please sign in to comment.