# Path: resources/lib/nvhttp/cryptoprovider/abstractcryptoprovider.py
# class AbstractCryptoProvider(object):
#     __metaclass__ = ABCMeta
# 
#     def __init__(self, host_context_service):
#         self.host_context_service = host_context_service
#         self._current_host = None
# 
#     @abstractmethod
#     def get_cert_path(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_key_path(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_key_dir(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_client_cert(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_client_private_key(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_pem_encoded_client_cert(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def extract_cert_signature(self, cert):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @staticmethod
#     def get_key_base_path():
#         return os.path.join(os.path.expanduser('~'), '.cache/moonlight')


import os
import time
from Crypto.Util import asn1
from M2Crypto import RSA as M2RSA
from M2Crypto import X509, EVP, ASN1
from resources.lib.nvhttp.cryptoprovider.abstractcryptoprovider import AbstractCryptoProvider
    def load_file_to_bytes(self, cert_file):
        self.pem_cert_bytes = buffer(open(cert_file).read())
        return self.pem_cert_bytes

    def load_cert_key_pair(self):
        if not os.path.isfile(self.cert_file) or not os.path.isfile(self.key_file):
            return False

        self.cert = X509.load_cert(self.cert_file)
        self.private_key = M2RSA.load_key(self.key_file)
        self.load_file_to_bytes(self.cert_file)

        return True

    def validate_cert(self, cert, days=365):
        t = long(time.time())
        now = ASN1.ASN1_UTCTIME()
        now.set_time(t)
        expire = ASN1.ASN1_UTCTIME()
        expire.set_time(t + days * 24 * 60 * 60)
        cert.set_not_before(now)
        cert.set_not_after(expire)

    def generate_cert_key_pair(self):
        private_key = EVP.PKey()
        rsa = M2RSA.gen_key(2048, 65537, lambda: None)
        private_key.assign_rsa(rsa)

        req = X509.Request()
        req.set_pubkey(private_key)

# Path: resources/lib/nvhttp/cryptoprovider/abstractcryptoprovider.py
# class AbstractCryptoProvider(object):
#     __metaclass__ = ABCMeta
# 
#     def __init__(self, host_context_service):
#         self.host_context_service = host_context_service
#         self._current_host = None
# 
#     @abstractmethod
#     def get_cert_path(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_key_path(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_key_dir(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_client_cert(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_client_private_key(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def get_pem_encoded_client_cert(self):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @abstractmethod
#     def extract_cert_signature(self, cert):
#         self._current_host = self.host_context_service.get_current_context()
# 
#     @staticmethod
#     def get_key_base_path():
#         return os.path.join(os.path.expanduser('~'), '.cache/moonlight')


# Path: resources/lib/nvhttp/cryptoprovider/advancedcryptoprovider.py
import os
import time
from Crypto.Util import asn1
from M2Crypto import RSA as M2RSA
from M2Crypto import X509, EVP, ASN1
from resources.lib.nvhttp.cryptoprovider.abstractcryptoprovider import AbstractCryptoProvider

    def load_file_to_bytes(self, cert_file):
        self.pem_cert_bytes = buffer(open(cert_file).read())
        return self.pem_cert_bytes

    def load_cert_key_pair(self):
        if not os.path.isfile(self.cert_file) or not os.path.isfile(self.key_file):
            return False

        self.cert = X509.load_cert(self.cert_file)
        self.private_key = M2RSA.load_key(self.key_file)
        self.load_file_to_bytes(self.cert_file)

        return True

    def validate_cert(self, cert, days=365):
        t = long(time.time())
        now = ASN1.ASN1_UTCTIME()
        now.set_time(t)
        expire = ASN1.ASN1_UTCTIME()
        expire.set_time(t + days * 24 * 60 * 60)
        cert.set_not_before(now)
        cert.set_not_after(expire)

    def generate_cert_key_pair(self):
        private_key = EVP.PKey()
        rsa = M2RSA.gen_key(2048, 65537, lambda: None)
        private_key.assign_rsa(rsa)

        req = X509.Request()
        req.set_pubkey(private_key)

        name = req.get_subject()