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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
package sshkeys
import (
"crypto/aes"
"crypto/cipher"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"fmt"
"math"
"math/big"
"github.com/dchest/bcrypt_pbkdf"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/ssh"
)
const keySizeAES256 = 32
// Format of private key to use when Marshaling.
type Format int
const (
// FormatOpenSSHv1 encodes a private key using OpenSSH's PROTOCOL.key format: https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
FormatOpenSSHv1 Format = iota
// FormatClassicPEM encodes private keys in PEM, with a key-specific encoding, as used by OpenSSH.
FormatClassicPEM
)
// MarshalOptions provides the Marshal function format and encryption options.
type MarshalOptions struct {
// Passphrase to encrypt private key with, if nil, the key will not be encrypted.
Passphrase []byte
// Format to encode the private key in.
Format Format
}
// Marshal converts a private key into an optionally encrypted format.
func Marshal(pk interface{}, opts *MarshalOptions) ([]byte, error) {
switch opts.Format {
case FormatOpenSSHv1:
return marshalOpenssh(pk, opts)
case FormatClassicPEM:
return marshalPem(pk, opts)
default:
return nil, fmt.Errorf("sshkeys: invalid format %d", opts.Format)
}
}
func marshalPem(pk interface{}, opts *MarshalOptions) ([]byte, error) {
var err error
var plain []byte
var pemType string
switch key := pk.(type) {
case *rsa.PrivateKey:
pemType = "RSA PRIVATE KEY"
plain = x509.MarshalPKCS1PrivateKey(key)
case *ecdsa.PrivateKey:
pemType = "EC PRIVATE KEY"
plain, err = x509.MarshalECPrivateKey(key)
if err != nil {
return nil, err
}
case *dsa.PrivateKey:
pemType = "DSA PRIVATE KEY"
plain, err = marshalDSAPrivateKey(key)
if err != nil {
return nil, err
}
case *ed25519.PrivateKey:
return nil, fmt.Errorf("sshkeys: ed25519 keys must be marshaled with FormatOpenSSHv1")
default:
return nil, fmt.Errorf("sshkeys: unsupported key type %T", pk)
}
if len(opts.Passphrase) > 0 {
block, err := x509.EncryptPEMBlock(rand.Reader, pemType, plain, opts.Passphrase, x509.PEMCipherAES128)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(block), nil
}
return pem.EncodeToMemory(&pem.Block{
Type: pemType,
Bytes: plain,
}), nil
}
type dsaOpenssl struct {
Version int
P *big.Int
Q *big.Int
G *big.Int
Pub *big.Int
Priv *big.Int
}
// https://github.com/golang/crypto/blob/master/ssh/keys.go#L793-L804
func marshalDSAPrivateKey(pk *dsa.PrivateKey) ([]byte, error) {
k := dsaOpenssl{
Version: 0,
P: pk.P,
Q: pk.Q,
G: pk.G,
Pub: pk.Y,
Priv: pk.X,
}
return asn1.Marshal(k)
}
const opensshv1Magic = "openssh-key-v1"
type opensshHeader struct {
CipherName string
KdfName string
KdfOpts string
NumKeys uint32
PubKey string
PrivKeyBlock string
}
type opensshKey struct {
Check1 uint32
Check2 uint32
Keytype string
Rest []byte `ssh:"rest"`
}
type opensshRsa struct {
N *big.Int
E *big.Int
D *big.Int
Iqmp *big.Int
P *big.Int
Q *big.Int
Comment string
Pad []byte `ssh:"rest"`
}
type opensshED25519 struct {
Pub []byte
Priv []byte
Comment string
Pad []byte `ssh:"rest"`
}
func padBytes(data []byte, blocksize int) []byte {
if blocksize != 0 {
var i byte
for i = byte(1); len(data)%blocksize != 0; i++ {
data = append(data, i&0xFF)
}
}
return data
}
func marshalOpenssh(pk interface{}, opts *MarshalOptions) ([]byte, error) {
var blocksize int
var keylen int
out := opensshHeader{
CipherName: "none",
KdfName: "none",
KdfOpts: "",
NumKeys: 1,
PubKey: "",
}
if len(opts.Passphrase) > 0 {
out.CipherName = "aes256-cbc"
out.KdfName = "bcrypt"
keylen = keySizeAES256
blocksize = aes.BlockSize
}
// Get a crypto rand in the range [0, uint32-max]
randnum, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32+1))
if err != nil {
return nil, fmt.Errorf("sshkeys: failed to get random number: %w", err)
}
// Convert it to uint32. As the content was within uint32 limits, nothing should be lost
check := uint32(randnum.Uint64())
pk1 := opensshKey{
Check1: check,
Check2: check,
}
switch key := pk.(type) {
case *rsa.PrivateKey:
k := &opensshRsa{
N: key.N,
E: big.NewInt(int64(key.E)),
D: key.D,
Iqmp: key.Precomputed.Qinv,
P: key.Primes[0],
Q: key.Primes[1],
Comment: "",
}
data := ssh.Marshal(k)
pk1.Keytype = ssh.KeyAlgoRSA
pk1.Rest = data
publicKey, err := ssh.NewPublicKey(&key.PublicKey)
if err != nil {
return nil, err
}
out.PubKey = string(publicKey.Marshal())
case ed25519.PrivateKey:
k := opensshED25519{
Pub: key.Public().(ed25519.PublicKey),
Priv: key,
}
data := ssh.Marshal(k)
pk1.Keytype = ssh.KeyAlgoED25519
pk1.Rest = data
publicKey, err := ssh.NewPublicKey(key.Public())
if err != nil {
return nil, err
}
out.PubKey = string(publicKey.Marshal())
case *ed25519.PrivateKey:
k := opensshED25519{
Pub: key.Public().(ed25519.PublicKey),
Priv: *key,
}
data := ssh.Marshal(k)
pk1.Keytype = ssh.KeyAlgoED25519
pk1.Rest = data
publicKey, err := ssh.NewPublicKey(key.Public())
if err != nil {
return nil, err
}
out.PubKey = string(publicKey.Marshal())
default:
return nil, fmt.Errorf("sshkeys: unsupported key type %T", pk)
}
if len(opts.Passphrase) > 0 {
rounds := 16
ivlen := blocksize
salt := make([]byte, blocksize)
_, err := rand.Read(salt)
if err != nil {
return nil, err
}
kdfdata, err := bcrypt_pbkdf.Key(opts.Passphrase, salt, rounds, keylen+ivlen)
if err != nil {
return nil, err
}
iv := kdfdata[keylen : ivlen+keylen]
aeskey := kdfdata[0:keylen]
block, err := aes.NewCipher(aeskey)
if err != nil {
return nil, err
}
pkblock := padBytes(ssh.Marshal(pk1), blocksize)
cbc := cipher.NewCBCEncrypter(block, iv)
cbc.CryptBlocks(pkblock, pkblock)
out.PrivKeyBlock = string(pkblock)
var opts struct {
Salt []byte
Rounds uint32
}
opts.Salt = salt
opts.Rounds = uint32(rounds)
out.KdfOpts = string(ssh.Marshal(&opts))
} else {
out.PrivKeyBlock = string(ssh.Marshal(pk1))
}
outBytes := []byte(opensshv1Magic)
outBytes = append(outBytes, 0)
outBytes = append(outBytes, ssh.Marshal(out)...)
block := &pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: outBytes,
}
return pem.EncodeToMemory(block), nil
}
|