2020-09-18 18:01:16 -04:00
|
|
|
/*
|
|
|
|
SSHSecure - a program to harden OpenSSH from defaults
|
|
|
|
Copyright (C) 2020 Brent Saner
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-09-03 19:11:42 -04:00
|
|
|
package sshkeys
|
|
|
|
|
2020-09-18 04:04:39 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/cipher"
|
|
|
|
)
|
|
|
|
|
2020-09-11 23:06:51 -04:00
|
|
|
// EncryptedSSHKeyV1 represents an encrypted private key.
|
|
|
|
type EncryptedSSHKeyV1 struct {
|
|
|
|
SSHKeyV1
|
2020-09-18 04:04:39 -04:00
|
|
|
Crypt SSHCrypt
|
2020-09-11 23:53:55 -04:00
|
|
|
KDFOpts SSHKDFOpts
|
2020-09-17 08:37:05 -04:00
|
|
|
Passphrase []byte
|
2020-09-03 19:11:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-21 01:43:22 -04:00
|
|
|
// SSHCrypt contains the encryption object Stream, the cipher object Cipher, the ephemeral salt (CryptSalt), and the ephemeral key (CryptKey).
|
2020-09-18 04:04:39 -04:00
|
|
|
// the Cipher, and the stream.
|
|
|
|
type SSHCrypt struct {
|
|
|
|
Stream cipher.Stream
|
|
|
|
Cipher cipher.Block
|
2020-09-21 01:43:22 -04:00
|
|
|
PrivateKey []byte // encryption key
|
|
|
|
CryptSalt []byte // ephemeral salt
|
|
|
|
CryptKey []byte // ephemeral key (not really used)
|
2020-09-18 04:04:39 -04:00
|
|
|
}
|
|
|
|
|
2020-09-11 23:53:55 -04:00
|
|
|
// SSHKDFOpts contains a set of KDF options.
|
|
|
|
type SSHKDFOpts struct {
|
|
|
|
Salt []byte // Also referred to as IV (initialization vector). (https://en.wikipedia.org/wiki/Initialization_vector)
|
|
|
|
Rounds uint32 // Also referred to as work factor.
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:01:16 -04:00
|
|
|
// SSHKeyV1 represents a private key.
|
2020-09-11 23:06:51 -04:00
|
|
|
// We don't bother with the legacy (pre v1) keys. Sorry not sorry.
|
|
|
|
// Patch your shit.
|
|
|
|
type SSHKeyV1 struct {
|
2020-09-18 04:04:39 -04:00
|
|
|
Magic string
|
|
|
|
DefKeyType string
|
|
|
|
KDFName string
|
2020-09-18 18:01:16 -04:00
|
|
|
CipherName string
|
2020-09-18 04:04:39 -04:00
|
|
|
KeySize uint32
|
|
|
|
Keys []SSHPrivKey // 1 by default.
|
|
|
|
Buffer bytes.Buffer
|
2020-09-11 23:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSHPubKey contains the Public key of an SSH Keypair.
|
|
|
|
type SSHPubKey struct {
|
2020-09-18 04:04:39 -04:00
|
|
|
KeyType string
|
|
|
|
Key interface{}
|
2020-09-11 23:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSHPrivKey contains the Private key of an SSH Keypair.
|
|
|
|
type SSHPrivKey struct {
|
|
|
|
PublicKey *SSHPubKey
|
2020-09-18 04:04:39 -04:00
|
|
|
BitSize uint32
|
2020-09-21 01:43:22 -04:00
|
|
|
BlockSize int
|
2020-09-18 04:04:39 -04:00
|
|
|
Key interface{}
|
|
|
|
// ED25519 keys are actually "sk + pk", where sk is the secret key and pk is the pubkey.
|
|
|
|
// We store that here.
|
|
|
|
KeyAlt []byte
|
|
|
|
Checksum []byte
|
|
|
|
Comment string
|
2020-09-03 19:11:42 -04:00
|
|
|
}
|