// Parse env var containing x509 rsa key, return error if parsing fails
Parse env var containing x509 rsa key, return error if parsing fails
// Parse env var containing x509 rsa key return error if parsing fails
func parseRsaPrivateKeyFromPem(privPEM string) (*rsa.PrivateKey, error) {
	block, _ := pem.Decode([]byte(privPEM))
	if block == nil {
		return nil, errors.New("failed to parse PEM block containing the key")
	}
	privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		privInterface, err := x509.ParsePKCS8PrivateKey(block.Bytes)
		if err != nil {
			return nil, err
		}
		priv, ok := privInterface.(*rsa.PrivateKey)
		if !ok {
			return nil, errors.New("cannot cast Private Key: Not an RSA key")
		}
		return priv, nil
	}
	return privKey, nil
}
func parseRsaPrivateKeyFromPem ( privPEM string ) ( * rsa . PrivateKey , error ) { block , _ := pem . Decode ( [ ] byte ( privPEM ) ) if block == nil { return nil , errors . New ( " " ) } privKey , err := x509 . ParsePKCS1PrivateKey ( block . Bytes ) if err != nil { privInterface , err := x509 . ParsePKCS8PrivateKey ( block . Bytes ) if err != nil { return nil , err } priv , ok := privInterface . ( * rsa . PrivateKey ) if ! ok { return nil , errors . New ( " " ) } return priv , nil } return privKey , nil }
Parse env var containing x509 rsa key, return error if parsing fails
Parse env var containing x509 rsa key return error if parsing fails
// Try again to parse with PKCS8 instead of PKCS1