goldwarden-vaultwarden-bitw.../cli/agent/ssh/keys.go

77 lines
2.4 KiB
Go
Raw Normal View History

2023-07-17 03:23:26 +02:00
package ssh
import (
"crypto/ed25519"
"crypto/rand"
"encoding/pem"
"io"
"github.com/mikesmitty/edkey"
2024-05-04 01:06:24 +02:00
"github.com/quexten/goldwarden/cli/agent/bitwarden/crypto"
"github.com/quexten/goldwarden/cli/agent/bitwarden/models"
2023-07-17 03:23:26 +02:00
"golang.org/x/crypto/ssh"
)
func NewSSHKeyCipher(name string, keyring *crypto.Keyring) (models.Cipher, string) {
var reader io.Reader = rand.Reader
pub, priv, err := ed25519.GenerateKey(reader)
if err != nil {
panic(err)
}
privBlock := pem.Block{
Type: "OPENSSH PRIVATE KEY",
2024-01-20 05:40:22 +01:00
Bytes: edkey.MarshalED25519PrivateKey(priv),
2023-07-17 03:23:26 +02:00
}
privatePEM := pem.EncodeToMemory(&privBlock)
publicKey, err := ssh.NewPublicKey(pub)
2024-01-20 05:40:22 +01:00
if err != nil {
log.Error("Generation of public key failed: %s", err)
}
_, err = ssh.ParsePrivateKey([]byte(string(privatePEM)))
if err != nil {
log.Error("Verification of generated private key failed: %s", err)
}
2023-07-17 03:23:26 +02:00
2023-12-22 12:43:38 +01:00
encryptedName, _ := crypto.EncryptWith([]byte(name), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedPublicKeyKey, _ := crypto.EncryptWith([]byte("public-key"), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedPublicKeyValue, _ := crypto.EncryptWith([]byte(string(ssh.MarshalAuthorizedKey(publicKey))), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedCustomTypeKey, _ := crypto.EncryptWith([]byte("custom-type"), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedCustomTypeValue, _ := crypto.EncryptWith([]byte("ssh-key"), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedPrivateKeyKey, _ := crypto.EncryptWith([]byte("private-key"), crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
encryptedPrivateKeyValue, _ := crypto.EncryptWith(privatePEM, crypto.AesCbc256_HmacSha256_B64, keyring.GetAccountKey())
2023-07-17 03:23:26 +02:00
cipher := models.Cipher{
Type: models.CipherNote,
Name: encryptedName,
Notes: &encryptedPublicKeyValue,
ID: nil,
Favorite: false,
OrganizationID: nil,
SecureNote: &models.SecureNoteCipher{
Type: 0,
},
Fields: []models.Field{
{
Type: 0,
Name: encryptedCustomTypeKey,
Value: encryptedCustomTypeValue,
},
{
Type: 0,
Name: encryptedPublicKeyKey,
Value: encryptedPublicKeyValue,
},
{
Type: 1,
Name: encryptedPrivateKeyKey,
Value: encryptedPrivateKeyValue,
},
},
}
return cipher, string(ssh.MarshalAuthorizedKey(publicKey))
}