# Path: studio/ed25519_key_util.py
# class Ed25519KeyUtil:
# 
#     @classmethod
#     def parse_private_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 private key in OPEN SSH PRIVATE key format.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: (public key part as bytes, private key part as bytes)
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = pkl.PrivateKeyList.from_string(contents)
#             data_public = key_data[0].private.params['public']
#             data_private = key_data[0].private.params['private_public']
#             return data_public, data_private[:32]
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None, None
# 
#     @classmethod
#     def parse_public_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 public.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: public key part as bytes
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = PublicKey.from_string(contents)
#             data_public = key_data.params['public']
#             return data_public
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None
# 
# Path: studio/payload_builders/payload_builder.py
# class PayloadBuilder:
#     """
#     Abstract class representing
#     payload object construction from experiment components.
#     Result is payload ready to be submitted for execution.
#     """
#     def __init__(self, name: str):
#         self.name = name if name else 'NO NAME'
# 
#     def construct(self, experiment, config, packages):
#         raise NotImplementedError(
#             'Not implemented for payload builder {0}'.format(self.name))
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):
# 
# Path: studio/payload_builders/unencrypted_payload_builder.py
# class UnencryptedPayloadBuilder(PayloadBuilder):
#     """
#     Simple payload builder constructing
#     unencrypted experiment payloads.
#     """
#     def construct(self, experiment: Experiment, config, packages):
#         return { 'experiment': experiment.to_dict(),
#                  'config': config}
# 
# Path: studio/util/util.py
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return


from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from nacl.signing import SigningKey, VerifyKey
from sshpubkeys import SSHKey
from studio.ed25519_key_util import Ed25519KeyUtil
from studio.payload_builders.payload_builder import PayloadBuilder
from studio.util import logs
from studio.payload_builders.unencrypted_payload_builder import UnencryptedPayloadBuilder
from studio.util.util import check_for_kb_interrupt
import nacl.secret
import nacl.utils
import base64
import json
        super(EncryptedPayloadBuilder, self).__init__(name)

        # XXX Set logger verbosity level here
        self.logger = logs.get_logger(self.__class__.__name__)

        self.recipient_key_path = receiver_keypath
        self.recipient_key = None
        try:
            self.recipient_key =\
                RSA.import_key(open(self.recipient_key_path).read())
        except:
            check_for_kb_interrupt()
            msg = "FAILED to import recipient public key from: {0}"\
                .format(self.recipient_key_path)
            self.logger.error(msg)
            raise ValueError(msg)

        self.sender_key_path = sender_keypath

        self.sender_key: SigningKey = None
        self.verify_key: VerifyKey = None
        self.sender_fingerprint = None

        if self.sender_key_path is None:
            self.logger.error("Signing key path must be specified for encrypted payloads. ABORTING.")
            raise ValueError()

        # We expect ed25519 signing key in "openssh private key" format
        try:
            public_key_data, private_key_data =\

# Path: studio/ed25519_key_util.py
# class Ed25519KeyUtil:
# 
#     @classmethod
#     def parse_private_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 private key in OPEN SSH PRIVATE key format.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: (public key part as bytes, private key part as bytes)
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = pkl.PrivateKeyList.from_string(contents)
#             data_public = key_data[0].private.params['public']
#             data_private = key_data[0].private.params['private_public']
#             return data_public, data_private[:32]
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None, None
# 
#     @classmethod
#     def parse_public_key_file(cls, filepath: str, logger):
#         """
#         Parse a file with ed25519 public.
#         :param filepath: file path to a key file
#         :param logger: logger to use for messages
#         :return: public key part as bytes
#         """
#         contents: str = ''
#         try:
#             with open(filepath, "r") as f:
#                 contents = f.read()
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to read keyfile %s: %s",
#                              filepath, exc)
#             return None, None
#         try:
#             key_data = PublicKey.from_string(contents)
#             data_public = key_data.params['public']
#             return data_public
#         except Exception as exc:
#             if logger is not None:
#                 logger.error("FAILED to decode keyfile format %s: %s",
#                          filepath, exc)
#             return None
# 
# Path: studio/payload_builders/payload_builder.py
# class PayloadBuilder:
#     """
#     Abstract class representing
#     payload object construction from experiment components.
#     Result is payload ready to be submitted for execution.
#     """
#     def __init__(self, name: str):
#         self.name = name if name else 'NO NAME'
# 
#     def construct(self, experiment, config, packages):
#         raise NotImplementedError(
#             'Not implemented for payload builder {0}'.format(self.name))
# 
# Path: studio/util/logs.py
# DEBUG = logging.DEBUG
# INFO = logging.INFO
# ERROR = logging.ERROR
# def get_logger(name):
# def debug(line):
# def error(line):
# def info(line):
# 
# Path: studio/payload_builders/unencrypted_payload_builder.py
# class UnencryptedPayloadBuilder(PayloadBuilder):
#     """
#     Simple payload builder constructing
#     unencrypted experiment payloads.
#     """
#     def construct(self, experiment: Experiment, config, packages):
#         return { 'experiment': experiment.to_dict(),
#                  'config': config}
# 
# Path: studio/util/util.py
# def check_for_kb_interrupt():
#     current_exc = sys.exc_info()[1]
#     if current_exc is None:
#         return
#     if isinstance(current_exc, KeyboardInterrupt):
#         raise current_exc
#     return


# Path: studio/encrypted_payload_builder.py
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
from nacl.signing import SigningKey, VerifyKey
from sshpubkeys import SSHKey
from studio.ed25519_key_util import Ed25519KeyUtil
from studio.payload_builders.payload_builder import PayloadBuilder
from studio.util import logs
from studio.payload_builders.unencrypted_payload_builder import UnencryptedPayloadBuilder
from studio.util.util import check_for_kb_interrupt
import nacl.secret
import nacl.utils
import base64
import json

        super(EncryptedPayloadBuilder, self).__init__(name)

        # XXX Set logger verbosity level here
        self.logger = logs.get_logger(self.__class__.__name__)

        self.recipient_key_path = receiver_keypath
        self.recipient_key = None
        try:
            self.recipient_key =\
                RSA.import_key(open(self.recipient_key_path).read())
        except:
            check_for_kb_interrupt()
            msg = "FAILED to import recipient public key from: {0}"\
                .format(self.recipient_key_path)
            self.logger.error(msg)
            raise ValueError(msg)

        self.sender_key_path = sender_keypath

        self.sender_key: SigningKey = None
        self.verify_key: VerifyKey = None
        self.sender_fingerprint = None

        if self.sender_key_path is None:
            self.logger.error("Signing key path must be specified for encrypted payloads. ABORTING.")
            raise ValueError()

        # We expect ed25519 signing key in "openssh private key" format
        try:
            public_key_data, private_key_data =\

                Ed25519KeyUtil.parse_private_key_file(