connectors/ERPC/PSK_Adapter.py
changeset 3884 34da877021d5
child 3908 32eb6e05008a
equal deleted inserted replaced
3883:a6e7dd8bac36 3884:34da877021d5
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz, a Integrated Development Environment for
       
     5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
       
     6 #
       
     7 # Copyright (C) 2019: Edouard TISSERANT
       
     8 #
       
     9 # See COPYING file for copyrights details.
       
    10 #
       
    11 # This program is free software; you can redistribute it and/or
       
    12 # modify it under the terms of the GNU General Public License
       
    13 # as published by the Free Software Foundation; either version 2
       
    14 # of the License, or (at your option) any later version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful,
       
    17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19 # GNU General Public License for more details.
       
    20 #
       
    21 # You should have received a copy of the GNU General Public License
       
    22 # along with this program; if not, write to the Free Software
       
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    24 
       
    25 
       
    26 """
       
    27 The TLS-PSK adapter that handles SSL connections instead of regular sockets,
       
    28 but using Pre Shared Keys instead of Certificates
       
    29 """
       
    30 
       
    31 import socket
       
    32 import ssl
       
    33 
       
    34 try:
       
    35     import sslpsk
       
    36 except ImportError as e:
       
    37     sslpsk = None
       
    38 
       
    39 from erpc.transport import TCPTransport
       
    40 
       
    41 class SSLPSKClientTransport(TCPTransport):
       
    42     def __init__(self, host, port, psk):
       
    43         """ overrides TCPTransport's __init__ to wrap socket in SSl wrapper """
       
    44         super(TCPTransport, self).__init__()
       
    45         self._host = host
       
    46         self._port = port
       
    47         self._isServer = isServer
       
    48         self._sock = None
       
    49 
       
    50         if sslpsk is None:
       
    51              raise ImportError("sslpsk module is not available")
       
    52 
       
    53         raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       
    54         raw_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
       
    55         raw_sock.connect((self._host, self._port))
       
    56         self._sock = sslpsk.wrap_socket(
       
    57                 raw_sock, psk=psk, server_side=False,
       
    58                 ciphers="PSK-AES256-CBC-SHA",  # available in openssl 1.0.2
       
    59                 ssl_version=ssl.PROTOCOL_TLSv1)
       
    60 
       
    61