readKey tries to read and decode the contents of a private key file. Returns the private key, or error otherwise.
(keyFile string)
| 94 | // readKey tries to read and decode the contents of a private key file. |
| 95 | // Returns the private key, or error otherwise. |
| 96 | func readKey(keyFile string) (crypto.PrivateKey, error) { |
| 97 | b, err := os.ReadFile(keyFile) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | |
| 102 | block, _ := pem.Decode(b) |
| 103 | switch { |
| 104 | case block == nil: |
| 105 | return nil, errors.Errorf("Failed to read key block") |
| 106 | case block.Type == "EC PRIVATE KEY": |
| 107 | return x509.ParseECPrivateKey(block.Bytes) |
| 108 | case block.Type == "RSA PRIVATE KEY": |
| 109 | return x509.ParsePKCS1PrivateKey(block.Bytes) |
| 110 | } |
| 111 | return nil, errors.Errorf("Unknown PEM type: %s", block.Type) |
| 112 | } |
| 113 | |
| 114 | // readCert tries to read and decode the contents of a signed cert file. |
| 115 | // Returns the x509v3 cert, or error otherwise. |
no test coverage detected