| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package certloader |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "crypto" |
| 22 | "crypto/x509" |
| 23 | "encoding/pem" |
| 24 | "errors" |
| 25 | "fmt" |
| 26 | "io" |
| 27 | "os" |
| 28 | "strings" |
| 29 | |
| 30 | "golang.org/x/crypto/openpgp" |
| 31 | "golang.org/x/crypto/openpgp/armor" |
| 32 | "golang.org/x/crypto/openpgp/packet" |
| 33 | |
| 34 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 35 | ) |
| 36 | |
| 37 | // Parse and decrypt a private key. It can be a RSA or ECDA key in PKCS#1 or |
| 38 | // PKCS#8 format and DER or PEM encoding, or it can be a PGP private key. If |
| 39 | // the private key is encrypted then the given prompter will be invoked to ask |
| 40 | // for the passphrase, if provided. |
| 41 | func ParseAnyPrivateKey(blob []byte, prompt passprompt.PasswordGetter) (crypto.PrivateKey, error) { |
| 42 | if bytes.HasPrefix(blob, []byte("-----BEGIN PGP")) { |
| 43 | return parsePgpPrivateKey(blob, prompt) |
| 44 | } else if bytes.HasPrefix(blob, []byte("-----BEGIN")) { |
| 45 | var block *pem.Block |
| 46 | for { |
| 47 | block, blob = pem.Decode(blob) |
| 48 | if block == nil { |
| 49 | break |
| 50 | } else if block.Type == "PRIVATE KEY" || strings.HasSuffix(block.Type, " PRIVATE KEY") { |
| 51 | return parsePemPrivateKey(block, prompt) |
| 52 | } |
| 53 | } |
| 54 | return nil, errors.New("failed to find any private keys in PEM data") |
| 55 | } else if blob[0] == asn1Magic { |
| 56 | return parsePrivateKey(blob) |
| 57 | } else if blob[0]&0x80 != 0 { |
| 58 | return parsePgpPrivateKey(blob, prompt) |
| 59 | } else { |
| 60 | return nil, errors.New("unrecognized private key format") |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func parsePemPrivateKey(block *pem.Block, prompt passprompt.PasswordGetter) (crypto.PrivateKey, error) { |
| 65 | if !x509.IsEncryptedPEMBlock(block) { //nolint:staticcheck |
| 66 | return parsePrivateKey(block.Bytes) |
| 67 | } |
| 68 | if prompt == nil { |
| 69 | return nil, errors.New("private key is encrypted and no password was provided") |
| 70 | } |
| 71 | for { |
| 72 | password, err := prompt.GetPasswd("Password for private key: ") |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } else if password == "" { |
| 76 | return nil, errors.New("aborted") |
| 77 | } |
| 78 | keyblob, err := x509.DecryptPEMBlock(block, []byte(password)) //nolint:staticcheck |
| 79 | if err == x509.IncorrectPasswordError { |
| 80 | continue |
| 81 | } else if err != nil { |
| 82 | return nil, err |
| 83 | } else { |
| 84 | return parsePrivateKey(keyblob) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func parsePgpPrivateKey(blob []byte, prompt passprompt.PasswordGetter) (crypto.PrivateKey, error) { |
| 90 | var reader io.Reader = bytes.NewReader(blob) |
| 91 | if blob[0] == '-' { |
| 92 | block, err := armor.Decode(reader) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | reader = block.Body |
| 97 | } |
| 98 | entity, err := openpgp.ReadEntity(packet.NewReader(reader)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | if entity.PrivateKey == nil { |
| 103 | return nil, errors.New("file does not contain a private key") |
| 104 | } |
| 105 | if entity.PrivateKey.Encrypted { |
| 106 | fmt.Fprintln(os.Stderr, "Key fingerprint:", entity.PrimaryKey.KeyIdString()) |
| 107 | for name := range entity.Identities { |
| 108 | fmt.Fprintln(os.Stderr, "UID:", name) |
| 109 | } |
| 110 | fmt.Fprintln(os.Stderr) |
| 111 | for { |
| 112 | password, err := prompt.GetPasswd("Passphrase for key: ") |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } else if password == "" { |
| 116 | return nil, errors.New("Aborted") |
| 117 | } |
| 118 | err = entity.PrivateKey.Decrypt([]byte(password)) |
| 119 | if err == nil { |
| 120 | break |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | return entity.PrivateKey.PrivateKey, nil |
| 125 | } |