| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package pemutil |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto" |
| 10 |
"crypto/aes" |
| 11 |
"crypto/cipher" |
| 12 |
"crypto/ecdsa" |
| 13 |
"crypto/ed25519" |
| 14 |
"crypto/elliptic" |
| 15 |
"crypto/rand" |
| 16 |
"crypto/rsa" |
| 17 |
"encoding/binary" |
| 18 |
"encoding/pem" |
| 19 |
"fmt" |
| 20 |
"math/big" |
| 21 |
|
| 22 |
"github.com/pkg/errors" |
| 23 |
"github.com/smallstep/cli/crypto/randutil" |
| 24 |
"github.com/smallstep/cli/errs" |
| 25 |
"github.com/smallstep/cli/pkg/bcrypt_pbkdf" |
| 26 |
"github.com/smallstep/cli/ui" |
| 27 |
"github.com/smallstep/cli/utils" |
| 28 |
"golang.org/x/crypto/ssh" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
sshMagic = "openssh-key-v1\x00" |
| 33 |
sshDefaultKdf = "bcrypt" |
| 34 |
sshDefaultCiphername = "aes256-ctr" |
| 35 |
sshDefaultKeyLength = 32 |
| 36 |
sshDefaultSaltLength = 16 |
| 37 |
sshDefaultRounds = 16 |
| 38 |
) |
| 39 |
|
| 40 |
type openSSHPrivateKey struct { |
| 41 |
CipherName string |
| 42 |
KdfName string |
| 43 |
KdfOpts string |
| 44 |
NumKeys uint32 |
| 45 |
PubKey []byte |
| 46 |
PrivKeyBlock []byte |
| 47 |
} |
| 48 |
|
| 49 |
type openSSHPrivateKeyBlock struct { |
| 50 |
Check1 uint32 |
| 51 |
Check2 uint32 |
| 52 |
Keytype string |
| 53 |
Rest []byte `ssh:"rest"` |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func ParseOpenSSHPrivateKey(key []byte, opts ...Options) (crypto.PrivateKey, error) { |
| 64 |
|
| 65 |
ctx := newContext("PEM") |
| 66 |
if err := ctx.apply(opts); err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
|
| 70 |
if len(key) < len(sshMagic) || string(key[:len(sshMagic)]) != sshMagic { |
| 71 |
return nil, errors.New("invalid openssh private key format") |
| 72 |
} |
| 73 |
remaining := key[len(sshMagic):] |
| 74 |
|
| 75 |
var w openSSHPrivateKey |
| 76 |
if err := ssh.Unmarshal(remaining, &w); err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
|
| 80 |
if w.KdfName != "none" || w.CipherName != "none" { |
| 81 |
if w.KdfName != sshDefaultKdf { |
| 82 |
return nil, errors.Errorf("cannot decode encrypted private keys with %s key derivative function", w.KdfName) |
| 83 |
} |
| 84 |
if w.CipherName != sshDefaultCiphername { |
| 85 |
return nil, errors.Errorf("cannot decode %s encrypted private keys", w.CipherName) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
buf := bytes.NewReader([]byte(w.KdfOpts)) |
| 90 |
|
| 91 |
var saltLength uint32 |
| 92 |
if err := binary.Read(buf, binary.BigEndian, &saltLength); err != nil { |
| 93 |
return nil, errors.New("cannot decode encrypted private keys: bad format") |
| 94 |
} |
| 95 |
|
| 96 |
salt := make([]byte, saltLength) |
| 97 |
if err := binary.Read(buf, binary.BigEndian, &salt); err != nil { |
| 98 |
return nil, errors.New("cannot decode encrypted private keys: bad format") |
| 99 |
} |
| 100 |
|
| 101 |
var rounds uint32 |
| 102 |
if err := binary.Read(buf, binary.BigEndian, &rounds); err != nil { |
| 103 |
return nil, errors.New("cannot decode encrypted private keys: bad format") |
| 104 |
} |
| 105 |
|
| 106 |
var err error |
| 107 |
var password []byte |
| 108 |
if len(ctx.password) > 0 { |
| 109 |
password = ctx.password |
| 110 |
} else { |
| 111 |
password, err = ui.PromptPassword(fmt.Sprintf("Please enter the password to decrypt %s", ctx.filename)) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
k, err := bcrypt_pbkdf.Key(password, salt, int(rounds), sshDefaultKeyLength+aes.BlockSize) |
| 119 |
if err != nil { |
| 120 |
return nil, errors.Wrap(err, "error deriving password") |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
dst := make([]byte, len(w.PrivKeyBlock)) |
| 125 |
iv := k[sshDefaultKeyLength : sshDefaultKeyLength+aes.BlockSize] |
| 126 |
block, err := aes.NewCipher(k[:sshDefaultKeyLength]) |
| 127 |
if err != nil { |
| 128 |
return nil, errors.Wrap(err, "error creating cipher") |
| 129 |
} |
| 130 |
|
| 131 |
stream := cipher.NewCTR(block, iv) |
| 132 |
stream.XORKeyStream(dst, w.PrivKeyBlock) |
| 133 |
w.PrivKeyBlock = dst |
| 134 |
} |
| 135 |
|
| 136 |
var pk1 openSSHPrivateKeyBlock |
| 137 |
if err := ssh.Unmarshal(w.PrivKeyBlock, &pk1); err != nil { |
| 138 |
if w.KdfName != "none" || w.CipherName != "none" { |
| 139 |
return nil, errors.New("incorrect passphrase supplied") |
| 140 |
} |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
|
| 144 |
if pk1.Check1 != pk1.Check2 { |
| 145 |
if w.KdfName != "none" || w.CipherName != "none" { |
| 146 |
return nil, errors.New("incorrect passphrase supplied") |
| 147 |
} |
| 148 |
return nil, errors.New("error decoding key: check mismatch") |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
switch pk1.Keytype { |
| 153 |
case ssh.KeyAlgoRSA: |
| 154 |
|
| 155 |
key := struct { |
| 156 |
N *big.Int |
| 157 |
E *big.Int |
| 158 |
D *big.Int |
| 159 |
Iqmp *big.Int |
| 160 |
P *big.Int |
| 161 |
Q *big.Int |
| 162 |
Comment string |
| 163 |
Pad []byte `ssh:"rest"` |
| 164 |
}{} |
| 165 |
|
| 166 |
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil { |
| 167 |
return nil, err |
| 168 |
} |
| 169 |
|
| 170 |
for i, b := range key.Pad { |
| 171 |
if int(b) != i+1 { |
| 172 |
return nil, errors.New("error decoding key: padding not as expected") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
pk := &rsa.PrivateKey{ |
| 177 |
PublicKey: rsa.PublicKey{ |
| 178 |
N: key.N, |
| 179 |
E: int(key.E.Int64()), |
| 180 |
}, |
| 181 |
D: key.D, |
| 182 |
Primes: []*big.Int{key.P, key.Q}, |
| 183 |
} |
| 184 |
|
| 185 |
if err := pk.Validate(); err != nil { |
| 186 |
return nil, err |
| 187 |
} |
| 188 |
|
| 189 |
pk.Precompute() |
| 190 |
|
| 191 |
return pk, nil |
| 192 |
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521: |
| 193 |
key := struct { |
| 194 |
Curve string |
| 195 |
Pub []byte |
| 196 |
D *big.Int |
| 197 |
Comment string |
| 198 |
Pad []byte `ssh:"rest"` |
| 199 |
}{} |
| 200 |
|
| 201 |
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil { |
| 202 |
return nil, errors.Wrap(err, "error unmarshaling key") |
| 203 |
} |
| 204 |
|
| 205 |
for i, b := range key.Pad { |
| 206 |
if int(b) != i+1 { |
| 207 |
return nil, errors.New("error decoding key: padding not as expected") |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
var curve elliptic.Curve |
| 212 |
switch key.Curve { |
| 213 |
case "nistp256": |
| 214 |
curve = elliptic.P256() |
| 215 |
case "nistp384": |
| 216 |
curve = elliptic.P384() |
| 217 |
case "nistp521": |
| 218 |
curve = elliptic.P521() |
| 219 |
default: |
| 220 |
return nil, errors.Errorf("error decoding key: unsupported elliptic curve %s", key.Curve) |
| 221 |
} |
| 222 |
|
| 223 |
N := curve.Params().N |
| 224 |
X, Y := elliptic.Unmarshal(curve, key.Pub) |
| 225 |
if X == nil || Y == nil { |
| 226 |
return nil, errors.New("error decoding key: failed to unmarshal public key") |
| 227 |
} |
| 228 |
|
| 229 |
if key.D.Cmp(N) >= 0 { |
| 230 |
return nil, errors.New("error decoding key: scalar is out of range") |
| 231 |
} |
| 232 |
|
| 233 |
x, y := curve.ScalarBaseMult(key.D.Bytes()) |
| 234 |
if x.Cmp(X) != 0 || y.Cmp(Y) != 0 { |
| 235 |
return nil, errors.New("error decoding key: public key does not match private key") |
| 236 |
} |
| 237 |
|
| 238 |
return &ecdsa.PrivateKey{ |
| 239 |
PublicKey: ecdsa.PublicKey{ |
| 240 |
Curve: curve, |
| 241 |
X: X, |
| 242 |
Y: Y, |
| 243 |
}, |
| 244 |
D: key.D, |
| 245 |
}, nil |
| 246 |
case ssh.KeyAlgoED25519: |
| 247 |
key := struct { |
| 248 |
Pub []byte |
| 249 |
Priv []byte |
| 250 |
Comment string |
| 251 |
Pad []byte `ssh:"rest"` |
| 252 |
}{} |
| 253 |
|
| 254 |
if err := ssh.Unmarshal(pk1.Rest, &key); err != nil { |
| 255 |
return nil, err |
| 256 |
} |
| 257 |
|
| 258 |
for i, b := range key.Pad { |
| 259 |
if int(b) != i+1 { |
| 260 |
return nil, errors.New("error decoding key: padding not as expected") |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if len(key.Priv) != ed25519.PrivateKeySize { |
| 265 |
return nil, errors.New("private key unexpected length") |
| 266 |
} |
| 267 |
|
| 268 |
pk := ed25519.PrivateKey(make([]byte, ed25519.PrivateKeySize)) |
| 269 |
copy(pk, key.Priv) |
| 270 |
return pk, nil |
| 271 |
default: |
| 272 |
return nil, errors.Errorf("unsupported key type %s", pk1.Keytype) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
func SerializeOpenSSHPrivateKey(key crypto.PrivateKey, opts ...Options) (*pem.Block, error) { |
| 278 |
ctx := new(context) |
| 279 |
if err := ctx.apply(opts); err != nil { |
| 280 |
return nil, err |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
var check uint32 |
| 285 |
if err := binary.Read(rand.Reader, binary.BigEndian, &check); err != nil { |
| 286 |
return nil, errors.Wrap(err, "error generating random check ") |
| 287 |
} |
| 288 |
|
| 289 |
w := openSSHPrivateKey{ |
| 290 |
NumKeys: 1, |
| 291 |
} |
| 292 |
pk1 := openSSHPrivateKeyBlock{ |
| 293 |
Check1: check, |
| 294 |
Check2: check, |
| 295 |
} |
| 296 |
|
| 297 |
var blockSize int |
| 298 |
if ctx.password == nil { |
| 299 |
w.CipherName = "none" |
| 300 |
w.KdfName = "none" |
| 301 |
blockSize = 8 |
| 302 |
} else { |
| 303 |
w.CipherName = sshDefaultCiphername |
| 304 |
w.KdfName = sshDefaultKdf |
| 305 |
blockSize = aes.BlockSize |
| 306 |
} |
| 307 |
|
| 308 |
switch k := key.(type) { |
| 309 |
case *rsa.PrivateKey: |
| 310 |
E := new(big.Int).SetInt64(int64(k.PublicKey.E)) |
| 311 |
|
| 312 |
|
| 313 |
pubKey := struct { |
| 314 |
KeyType string |
| 315 |
E *big.Int |
| 316 |
N *big.Int |
| 317 |
}{ |
| 318 |
ssh.KeyAlgoRSA, |
| 319 |
E, k.PublicKey.N, |
| 320 |
} |
| 321 |
w.PubKey = ssh.Marshal(pubKey) |
| 322 |
|
| 323 |
|
| 324 |
key := struct { |
| 325 |
N *big.Int |
| 326 |
E *big.Int |
| 327 |
D *big.Int |
| 328 |
Iqmp *big.Int |
| 329 |
P *big.Int |
| 330 |
Q *big.Int |
| 331 |
Comment string |
| 332 |
}{ |
| 333 |
k.PublicKey.N, E, |
| 334 |
k.D, k.Precomputed.Qinv, k.Primes[0], k.Primes[1], |
| 335 |
ctx.comment, |
| 336 |
} |
| 337 |
pk1.Keytype = ssh.KeyAlgoRSA |
| 338 |
pk1.Rest = ssh.Marshal(key) |
| 339 |
case *ecdsa.PrivateKey: |
| 340 |
var curve, keyType string |
| 341 |
switch k.Curve.Params().Name { |
| 342 |
case "P-256": |
| 343 |
curve = "nistp256" |
| 344 |
keyType = ssh.KeyAlgoECDSA256 |
| 345 |
case "P-384": |
| 346 |
curve = "nistp384" |
| 347 |
keyType = ssh.KeyAlgoECDSA384 |
| 348 |
case "P-521": |
| 349 |
curve = "nistp521" |
| 350 |
keyType = ssh.KeyAlgoECDSA521 |
| 351 |
default: |
| 352 |
return nil, errors.Errorf("error serializing key: unsupported curve %s", k.Curve.Params().Name) |
| 353 |
} |
| 354 |
|
| 355 |
pub := elliptic.Marshal(k.Curve, k.PublicKey.X, k.PublicKey.Y) |
| 356 |
|
| 357 |
|
| 358 |
pubKey := struct { |
| 359 |
KeyType string |
| 360 |
Curve string |
| 361 |
Pub []byte |
| 362 |
}{ |
| 363 |
keyType, curve, pub, |
| 364 |
} |
| 365 |
w.PubKey = ssh.Marshal(pubKey) |
| 366 |
|
| 367 |
|
| 368 |
key := struct { |
| 369 |
Curve string |
| 370 |
Pub []byte |
| 371 |
D *big.Int |
| 372 |
Comment string |
| 373 |
}{ |
| 374 |
curve, pub, k.D, |
| 375 |
ctx.comment, |
| 376 |
} |
| 377 |
pk1.Keytype = keyType |
| 378 |
pk1.Rest = ssh.Marshal(key) |
| 379 |
case ed25519.PrivateKey: |
| 380 |
pub := make([]byte, ed25519.PublicKeySize) |
| 381 |
priv := make([]byte, ed25519.PrivateKeySize) |
| 382 |
copy(pub, k[ed25519.PublicKeySize:]) |
| 383 |
copy(priv, k) |
| 384 |
|
| 385 |
|
| 386 |
pubKey := struct { |
| 387 |
KeyType string |
| 388 |
Pub []byte |
| 389 |
}{ |
| 390 |
ssh.KeyAlgoED25519, pub, |
| 391 |
} |
| 392 |
w.PubKey = ssh.Marshal(pubKey) |
| 393 |
|
| 394 |
|
| 395 |
key := struct { |
| 396 |
Pub []byte |
| 397 |
Priv []byte |
| 398 |
Comment string |
| 399 |
}{ |
| 400 |
pub, priv, |
| 401 |
ctx.comment, |
| 402 |
} |
| 403 |
pk1.Keytype = ssh.KeyAlgoED25519 |
| 404 |
pk1.Rest = ssh.Marshal(key) |
| 405 |
default: |
| 406 |
return nil, errors.Errorf("unsupported key type %T", k) |
| 407 |
} |
| 408 |
|
| 409 |
w.PrivKeyBlock = ssh.Marshal(pk1) |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
for i, l := 0, len(w.PrivKeyBlock); (l+i)%blockSize != 0; i++ { |
| 414 |
w.PrivKeyBlock = append(w.PrivKeyBlock, byte(i+1)) |
| 415 |
} |
| 416 |
|
| 417 |
if ctx.password != nil { |
| 418 |
|
| 419 |
salt, err := randutil.Salt(sshDefaultSaltLength) |
| 420 |
if err != nil { |
| 421 |
return nil, err |
| 422 |
} |
| 423 |
|
| 424 |
buf := new(bytes.Buffer) |
| 425 |
binary.Write(buf, binary.BigEndian, uint32(sshDefaultSaltLength)) |
| 426 |
binary.Write(buf, binary.BigEndian, salt) |
| 427 |
binary.Write(buf, binary.BigEndian, uint32(sshDefaultRounds)) |
| 428 |
w.KdfOpts = buf.String() |
| 429 |
|
| 430 |
|
| 431 |
k, err := bcrypt_pbkdf.Key(ctx.password, salt, sshDefaultRounds, sshDefaultKeyLength+aes.BlockSize) |
| 432 |
if err != nil { |
| 433 |
return nil, errors.Wrap(err, "error deriving decryption key") |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
dst := make([]byte, len(w.PrivKeyBlock)) |
| 438 |
iv := k[sshDefaultKeyLength : sshDefaultKeyLength+aes.BlockSize] |
| 439 |
block, err := aes.NewCipher(k[:sshDefaultKeyLength]) |
| 440 |
if err != nil { |
| 441 |
return nil, errors.Wrap(err, "error creating cipher") |
| 442 |
} |
| 443 |
|
| 444 |
stream := cipher.NewCTR(block, iv) |
| 445 |
stream.XORKeyStream(dst, w.PrivKeyBlock) |
| 446 |
w.PrivKeyBlock = dst |
| 447 |
} |
| 448 |
|
| 449 |
b := ssh.Marshal(w) |
| 450 |
block := &pem.Block{ |
| 451 |
Type: "OPENSSH PRIVATE KEY", |
| 452 |
Bytes: append([]byte(sshMagic), b...), |
| 453 |
} |
| 454 |
|
| 455 |
if ctx.filename != "" { |
| 456 |
if err := utils.WriteFile(ctx.filename, pem.EncodeToMemory(block), ctx.perm); err != nil { |
| 457 |
return nil, errs.FileError(err, ctx.filename) |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
return block, nil |
| 462 |
} |