package jwt

import (
	
	
	
	
)

var (
	ErrKeyMustBePEMEncoded = errors.New("invalid key: Key must be a PEM encoded PKCS1 or PKCS8 key")
	ErrNotRSAPrivateKey    = errors.New("key is not a valid RSA private key")
	ErrNotRSAPublicKey     = errors.New("key is not a valid RSA public key")
)

// ParseRSAPrivateKeyFromPEM parses a PEM encoded PKCS1 or PKCS8 private key
func ( []byte) (*rsa.PrivateKey, error) {
	var  error

	// Parse PEM block
	var  *pem.Block
	if , _ = pem.Decode();  == nil {
		return nil, ErrKeyMustBePEMEncoded
	}

	var  interface{}
	if ,  = x509.ParsePKCS1PrivateKey(.Bytes);  != nil {
		if ,  = x509.ParsePKCS8PrivateKey(.Bytes);  != nil {
			return nil, 
		}
	}

	var  *rsa.PrivateKey
	var  bool
	if ,  = .(*rsa.PrivateKey); ! {
		return nil, ErrNotRSAPrivateKey
	}

	return , nil
}

// ParseRSAPrivateKeyFromPEMWithPassword parses a PEM encoded PKCS1 or PKCS8 private key protected with password
//
// Deprecated: This function is deprecated and should not be used anymore. It uses the deprecated x509.DecryptPEMBlock
// function, which was deprecated since RFC 1423 is regarded insecure by design. Unfortunately, there is no alternative
// in the Go standard library for now. See https://github.com/golang/go/issues/8860.
func ( []byte,  string) (*rsa.PrivateKey, error) {
	var  error

	// Parse PEM block
	var  *pem.Block
	if , _ = pem.Decode();  == nil {
		return nil, ErrKeyMustBePEMEncoded
	}

	var  interface{}

	var  []byte
	if ,  = x509.DecryptPEMBlock(, []byte());  != nil {
		return nil, 
	}

	if ,  = x509.ParsePKCS1PrivateKey();  != nil {
		if ,  = x509.ParsePKCS8PrivateKey();  != nil {
			return nil, 
		}
	}

	var  *rsa.PrivateKey
	var  bool
	if ,  = .(*rsa.PrivateKey); ! {
		return nil, ErrNotRSAPrivateKey
	}

	return , nil
}

// ParseRSAPublicKeyFromPEM parses a PEM encoded PKCS1 or PKCS8 public key
func ( []byte) (*rsa.PublicKey, error) {
	var  error

	// Parse PEM block
	var  *pem.Block
	if , _ = pem.Decode();  == nil {
		return nil, ErrKeyMustBePEMEncoded
	}

	// Parse the key
	var  interface{}
	if ,  = x509.ParsePKIXPublicKey(.Bytes);  != nil {
		if ,  := x509.ParseCertificate(.Bytes);  == nil {
			 = .PublicKey
		} else {
			return nil, 
		}
	}

	var  *rsa.PublicKey
	var  bool
	if ,  = .(*rsa.PublicKey); ! {
		return nil, ErrNotRSAPublicKey
	}

	return , nil
}