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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pemutil
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/binary"
"encoding/pem"
"fmt"
"math/big"
"github.com/pkg/errors"
"github.com/smallstep/cli/crypto/randutil"
"github.com/smallstep/cli/errs"
"github.com/smallstep/cli/pkg/bcrypt_pbkdf"
"github.com/smallstep/cli/ui"
"github.com/smallstep/cli/utils"
"golang.org/x/crypto/ssh"
)
const (
sshMagic = "openssh-key-v1\x00"
sshDefaultKdf = "bcrypt"
sshDefaultCiphername = "aes256-ctr"
sshDefaultKeyLength = 32
sshDefaultSaltLength = 16
sshDefaultRounds = 16
)
type openSSHPrivateKey struct {
CipherName string
KdfName string
KdfOpts string
NumKeys uint32
PubKey []byte
PrivKeyBlock []byte
}
type openSSHPrivateKeyBlock struct {
Check1 uint32
Check2 uint32
Keytype string
Rest []byte `ssh:"rest"`
}
// ParseOpenSSHPrivateKey parses a private key in OpenSSH PEM format.
//
// Implemented based on the documentation at
// https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
//
// This method is based on the implementation at
// https://github.com/golang/crypto/blob/master/ssh/keys.go
func ParseOpenSSHPrivateKey(key []byte, opts ...Options) (crypto.PrivateKey, error) {
// Populate options
ctx := newContext("PEM")
if err := ctx.apply(opts); err != nil {
return nil, err
}
if len(key) < len(sshMagic) || string(key[:len(sshMagic)]) != sshMagic {
return nil, errors.New("invalid openssh private key format")
}
remaining := key[len(sshMagic):]
var w openSSHPrivateKey
if err := ssh.Unmarshal(remaining, &w); err != nil {
return nil, err
}
if w.KdfName != "none" || w.CipherName != "none" {
if w.KdfName != sshDefaultKdf {
return nil, errors.Errorf("cannot decode encrypted private keys with %s key derivative function", w.KdfName)
}
if w.CipherName != sshDefaultCiphername {
return nil, errors.Errorf("cannot decode %s encrypted private keys", w.CipherName)
}
// Read kdf options.
buf := bytes.NewReader([]byte(w.KdfOpts))
var saltLength uint32
if err := binary.Read(buf, binary.BigEndian, &saltLength); err != nil {
return nil, errors.New("cannot decode encrypted private keys: bad format")
}
salt := make([]byte, saltLength)
if err := binary.Read(buf, binary.BigEndian, &salt); err != nil {
return nil, errors.New("cannot decode encrypted private keys: bad format")
}
var rounds uint32
if err := binary.Read(buf, binary.BigEndian, &rounds); err != nil {
return nil, errors.New("cannot decode encrypted private keys: bad format")
}
var err error
var password []byte
if len(ctx.password) > 0 {
password = ctx.password
} else {
password, err = ui.PromptPassword(fmt.Sprintf("Please enter the password to decrypt %s", ctx.filename))
if err != nil {
return nil, err
}
}
// Derive the cipher key used in the cipher.
k, err := bcrypt_pbkdf.Key(password, salt, int(rounds), sshDefaultKeyLength+aes.BlockSize)
if err != nil {
return nil, errors.Wrap(err, "error deriving password")
}
// Decrypt the private key using the derived secret.
dst := make([]byte, len(w.PrivKeyBlock))
iv := k[sshDefaultKeyLength : sshDefaultKeyLength+aes.BlockSize]
block, err := aes.NewCipher(k[:sshDefaultKeyLength])
if err != nil {
return nil, errors.Wrap(err, "error creating cipher")
}
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(dst, w.PrivKeyBlock)
w.PrivKeyBlock = dst
}
var pk1 openSSHPrivateKeyBlock
if err := ssh.Unmarshal(w.PrivKeyBlock, &pk1); err != nil {
if w.KdfName != "none" || w.CipherName != "none" {
return nil, errors.New("incorrect passphrase supplied")
}
return nil, err
}
if pk1.Check1 != pk1.Check2 {
if w.KdfName != "none" || w.CipherName != "none" {
return nil, errors.New("incorrect passphrase supplied")
}
return nil, errors.New("error decoding key: check mismatch")
}
// we only handle ed25519 and rsa keys currently
switch pk1.Keytype {
case ssh.KeyAlgoRSA:
// https://github.com/openssh/openssh-portable/blob/master/sshkey.c
key := 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"`
}{}
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil {
return nil, err
}
for i, b := range key.Pad {
if int(b) != i+1 {
return nil, errors.New("error decoding key: padding not as expected")
}
}
pk := &rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: key.N,
E: int(key.E.Int64()),
},
D: key.D,
Primes: []*big.Int{key.P, key.Q},
}
if err := pk.Validate(); err != nil {
return nil, err
}
pk.Precompute()
return pk, nil
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
key := struct {
Curve string
Pub []byte
D *big.Int
Comment string
Pad []byte `ssh:"rest"`
}{}
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil {
return nil, errors.Wrap(err, "error unmarshaling key")
}
for i, b := range key.Pad {
if int(b) != i+1 {
return nil, errors.New("error decoding key: padding not as expected")
}
}
var curve elliptic.Curve
switch key.Curve {
case "nistp256":
curve = elliptic.P256()
case "nistp384":
curve = elliptic.P384()
case "nistp521":
curve = elliptic.P521()
default:
return nil, errors.Errorf("error decoding key: unsupported elliptic curve %s", key.Curve)
}
N := curve.Params().N
X, Y := elliptic.Unmarshal(curve, key.Pub)
if X == nil || Y == nil {
return nil, errors.New("error decoding key: failed to unmarshal public key")
}
if key.D.Cmp(N) >= 0 {
return nil, errors.New("error decoding key: scalar is out of range")
}
x, y := curve.ScalarBaseMult(key.D.Bytes())
if x.Cmp(X) != 0 || y.Cmp(Y) != 0 {
return nil, errors.New("error decoding key: public key does not match private key")
}
return &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: X,
Y: Y,
},
D: key.D,
}, nil
case ssh.KeyAlgoED25519:
key := struct {
Pub []byte
Priv []byte
Comment string
Pad []byte `ssh:"rest"`
}{}
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil {
return nil, err
}
for i, b := range key.Pad {
if int(b) != i+1 {
return nil, errors.New("error decoding key: padding not as expected")
}
}
if len(key.Priv) != ed25519.PrivateKeySize {
return nil, errors.New("private key unexpected length")
}
pk := ed25519.PrivateKey(make([]byte, ed25519.PrivateKeySize))
copy(pk, key.Priv)
return pk, nil
default:
return nil, errors.Errorf("unsupported key type %s", pk1.Keytype)
}
}
// SerializeOpenSSHPrivateKey serialize a private key in the OpenSSH PEM format.
func SerializeOpenSSHPrivateKey(key crypto.PrivateKey, opts ...Options) (*pem.Block, error) {
ctx := new(context)
if err := ctx.apply(opts); err != nil {
return nil, err
}
// Random check bytes.
var check uint32
if err := binary.Read(rand.Reader, binary.BigEndian, &check); err != nil {
return nil, errors.Wrap(err, "error generating random check ")
}
w := openSSHPrivateKey{
NumKeys: 1,
}
pk1 := openSSHPrivateKeyBlock{
Check1: check,
Check2: check,
}
var blockSize int
if ctx.password == nil {
w.CipherName = "none"
w.KdfName = "none"
blockSize = 8
} else {
w.CipherName = sshDefaultCiphername
w.KdfName = sshDefaultKdf
blockSize = aes.BlockSize
}
switch k := key.(type) {
case *rsa.PrivateKey:
E := new(big.Int).SetInt64(int64(k.PublicKey.E))
// Marshal public key:
// E and N are in reversed order in the public and private key.
pubKey := struct {
KeyType string
E *big.Int
N *big.Int
}{
ssh.KeyAlgoRSA,
E, k.PublicKey.N,
}
w.PubKey = ssh.Marshal(pubKey)
// Marshal private key.
key := struct {
N *big.Int
E *big.Int
D *big.Int
Iqmp *big.Int
P *big.Int
Q *big.Int
Comment string
}{
k.PublicKey.N, E,
k.D, k.Precomputed.Qinv, k.Primes[0], k.Primes[1],
ctx.comment,
}
pk1.Keytype = ssh.KeyAlgoRSA
pk1.Rest = ssh.Marshal(key)
case *ecdsa.PrivateKey:
var curve, keyType string
switch k.Curve.Params().Name {
case "P-256":
curve = "nistp256"
keyType = ssh.KeyAlgoECDSA256
case "P-384":
curve = "nistp384"
keyType = ssh.KeyAlgoECDSA384
case "P-521":
curve = "nistp521"
keyType = ssh.KeyAlgoECDSA521
default:
return nil, errors.Errorf("error serializing key: unsupported curve %s", k.Curve.Params().Name)
}
pub := elliptic.Marshal(k.Curve, k.PublicKey.X, k.PublicKey.Y)
// Marshal public key.
pubKey := struct {
KeyType string
Curve string
Pub []byte
}{
keyType, curve, pub,
}
w.PubKey = ssh.Marshal(pubKey)
// Marshal private key.
key := struct {
Curve string
Pub []byte
D *big.Int
Comment string
}{
curve, pub, k.D,
ctx.comment,
}
pk1.Keytype = keyType
pk1.Rest = ssh.Marshal(key)
case ed25519.PrivateKey:
pub := make([]byte, ed25519.PublicKeySize)
priv := make([]byte, ed25519.PrivateKeySize)
copy(pub, k[ed25519.PublicKeySize:])
copy(priv, k)
// Marshal public key.
pubKey := struct {
KeyType string
Pub []byte
}{
ssh.KeyAlgoED25519, pub,
}
w.PubKey = ssh.Marshal(pubKey)
// Marshal private key.
key := struct {
Pub []byte
Priv []byte
Comment string
}{
pub, priv,
ctx.comment,
}
pk1.Keytype = ssh.KeyAlgoED25519
pk1.Rest = ssh.Marshal(key)
default:
return nil, errors.Errorf("unsupported key type %T", k)
}
w.PrivKeyBlock = ssh.Marshal(pk1)
// Add padding until the private key block matches the block size,
// 16 with AES encryption, 8 without.
for i, l := 0, len(w.PrivKeyBlock); (l+i)%blockSize != 0; i++ {
w.PrivKeyBlock = append(w.PrivKeyBlock, byte(i+1))
}
if ctx.password != nil {
// Create encryption key derivation the password.
salt, err := randutil.Salt(sshDefaultSaltLength)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(sshDefaultSaltLength))
binary.Write(buf, binary.BigEndian, salt)
binary.Write(buf, binary.BigEndian, uint32(sshDefaultRounds))
w.KdfOpts = buf.String()
// Derive key to encrypt the private key block.
k, err := bcrypt_pbkdf.Key(ctx.password, salt, sshDefaultRounds, sshDefaultKeyLength+aes.BlockSize)
if err != nil {
return nil, errors.Wrap(err, "error deriving decryption key")
}
// Encrypt the private key using the derived secret.
dst := make([]byte, len(w.PrivKeyBlock))
iv := k[sshDefaultKeyLength : sshDefaultKeyLength+aes.BlockSize]
block, err := aes.NewCipher(k[:sshDefaultKeyLength])
if err != nil {
return nil, errors.Wrap(err, "error creating cipher")
}
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(dst, w.PrivKeyBlock)
w.PrivKeyBlock = dst
}
b := ssh.Marshal(w)
block := &pem.Block{
Type: "OPENSSH PRIVATE KEY",
Bytes: append([]byte(sshMagic), b...),
}
if ctx.filename != "" {
if err := utils.WriteFile(ctx.filename, pem.EncodeToMemory(block), ctx.perm); err != nil {
return nil, errs.FileError(err, ctx.filename)
}
}
return block, nil
}
|