# Path: Peach/publisher.py
# class PublisherSoftException(SoftException):
#     """
#     Recoverable exception occurred in the Publisher.
#     """
#     pass
# 
# Path: Peach/Engine/common.py
# class PeachException(HardException):
#     """
#     Peach exceptions are specialized hard exceptions.
#     The message contained in a PeachException is presentable to the user w/o any stack trace, etc.
#     Examples would be:
#         "Error: The DataModel element requires a name attribute."
#     """
#     def __init__(self, msg, module="Unknown"):
#         Exception.__init__(self, msg)
#         self.module = module
#         self.msg = msg


import sys
import ssl
import socket
import tlslite.api
from Peach.Publishers.tcp import TcpListener
from Peach.publisher import PublisherSoftException
from Peach.Engine.common import PeachException
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.



class SSL(TcpListener):
    def __init__(self, host, port, cert, pkey, timeout=0.25):
        TcpListener.__init__(self, host, port, timeout)

        self.cert = cert
        self.pkey = pkey

    def accept(self):
        print("[*] Waiting for incoming connection")
        client, addr = self._listen.accept()
        print("[*] Client:", addr[0], addr[1])

        print("[*] Wrapping socket to TLS/SSL")
        try:
            self._socket = ssl.wrap_socket(client,
                                           server_side=True,
                                           certfile=self.cert,
                                           keyfile=self.pkey,
                                           do_handshake_on_connect=False)
        except ssl.SSLError as e:

# Path: Peach/publisher.py
# class PublisherSoftException(SoftException):
#     """
#     Recoverable exception occurred in the Publisher.
#     """
#     pass
# 
# Path: Peach/Engine/common.py
# class PeachException(HardException):
#     """
#     Peach exceptions are specialized hard exceptions.
#     The message contained in a PeachException is presentable to the user w/o any stack trace, etc.
#     Examples would be:
#         "Error: The DataModel element requires a name attribute."
#     """
#     def __init__(self, msg, module="Unknown"):
#         Exception.__init__(self, msg)
#         self.module = module
#         self.msg = msg


# Path: Peach/Publishers/sslayer.py
import sys
import ssl
import socket
import tlslite.api
from Peach.Publishers.tcp import TcpListener
from Peach.publisher import PublisherSoftException
from Peach.Engine.common import PeachException

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.



class SSL(TcpListener):
    def __init__(self, host, port, cert, pkey, timeout=0.25):
        TcpListener.__init__(self, host, port, timeout)

        self.cert = cert
        self.pkey = pkey

    def accept(self):
        print("[*] Waiting for incoming connection")
        client, addr = self._listen.accept()
        print("[*] Client:", addr[0], addr[1])

        print("[*] Wrapping socket to TLS/SSL")
        try:
            self._socket = ssl.wrap_socket(client,
                                           server_side=True,
                                           certfile=self.cert,
                                           keyfile=self.pkey,
                                           do_handshake_on_connect=False)
        except ssl.SSLError as e:

            raise PeachException(str(e))