1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // ParseKey converts the binary contents of a private key file
16 // to an *rsa.PrivateKey. It detects whether the private key is in a
17 // PEM container or not. If so, it extracts the the private key
18 // from PEM container before conversion. It only supports PEM
19 // containers with no passphrase.
20 func ParseKey(key []byte) (*rsa.PrivateKey, error) {
21 block, _ := pem.Decode(key)
25 parsedKey, err := x509.ParsePKCS8PrivateKey(key)
27 parsedKey, err = x509.ParsePKCS1PrivateKey(key)
29 return nil, fmt.Errorf("private key should be a PEM or plain PKCS1 or PKCS8; parse error: %v", err)
32 parsed, ok := parsedKey.(*rsa.PrivateKey)
34 return nil, errors.New("private key is invalid")