Пример #1
0
def tlsn_send_msg(data,pk,ack_q,recipient,seq_init=100000,raw=False):
    '''Send a message <data> on an already negotiated connection ;
    wait for an acknowledgement by polling for it on Queue ack_q
    Messages are sent with sequence numbers initialised at seq_init,
    or 0 if seq_init is undef.
    Messages larger than chunk_size are split into chunks with line endings.
    After chunking, messages are encrypted to public key pk then base64 encoded.
    CRLF and EOL are appended to the end of chunks according to tlsnotary's messaging protocol.
    Return 'success' only if message was sent and ack received correctly, otherwise 'failure'.
    '''
    if not initialized:
        raise Exception("TLSN Messaging not yet instantiated")

    if not hasattr(tlsn_send_msg, "my_seq"):
        if not seq_init: seq_init = 0
        tlsn_send_msg.my_seq = seq_init #static variable. Initialized only on first function's run

    #split up data longer than chunk_size bytes
    chunks = len(data)/msg_chunk_size + 1
    if len(data)%msg_chunk_size == 0: chunks -= 1 #avoid creating an empty chunk if data length is a multiple of chunk_size

    for chunk_index in range(chunks) :
        tlsn_send_msg.my_seq += 1
        chunk = data[msg_chunk_size*chunk_index:msg_chunk_size*(chunk_index+1)]
        #encrypt and base 64 encode the chunk; if we have used a sensible chunk size
        #this will neither cause a problem for RSA nor for IRC
        encrypted_encoded_chunk = ee(chunk,pk)

        ending = ' EOL ' if chunk_index+1==chunks else ' CRLF ' #EOL for the last chunk, otherwise CRLF
        msg_to_send = ' :' + recipient + ' seq:' + str(tlsn_send_msg.my_seq) + ' ' + encrypted_encoded_chunk + ending

        if not raw:
            for i in range (3):
                b_was_message_acked = False
                #empty the ack queue. Not using while True: because sometimes an endless loop would happen TODO: find out why
                for j in range(5):
                    try: ack_q.get_nowait()
                    except: pass
                tlsn_send_raw(msg_to_send)
                try:
                    ack_check = ack_q.get(block=True, timeout=3)
                except: continue #send again because ack was not received
                #print ('ack check is: ',ack_check)
                if not str(tlsn_send_msg.my_seq) == ack_check: continue
                #else: correct ack received
                #print ('message was acked')
                b_was_message_acked = True
                break

            if not b_was_message_acked:
                return 'failure'
        else:
            tlsn_send_raw(msg_to_send)

    return 'success'
Пример #2
0
def tlsn_send_single_msg(header,data,pk,ctrprty_nick=None):
    '''send a message without acks/seq nos, but including chunking,
    encoding and encryption; just for handshakes.
    message sent is data, then encrypted and encoded and chunked data.
    If ctrprty_nick is included, this nick is included in the header to direct the message.
    (Only one side of the handshake needs this).
    '''
    header = header if not ctrprty_nick else ':'+ctrprty_nick + ' ' + header
    chunks = len(data)/msg_chunk_size + 1
    if len(data)%msg_chunk_size == 0: chunks -= 1 #avoid creating an empty chunk if data length is a multiple of chunk_size

    for chunk_index in range(chunks) :
        chunk = data[msg_chunk_size*chunk_index:msg_chunk_size*(chunk_index+1)]
        encrypted_encoded_chunk = ee(str(chunk_index)+chunk,pk)
        ending = 'EOL' if chunk_index == chunks-1 else 'CRLF'
        tlsn_send_raw(header+' '+encrypted_encoded_chunk+' '+ending)
        time.sleep(0.5)
Пример #3
0
def tlsn_send_single_msg(header, data, pk, ctrprty_nick=None):
    '''send a message without acks/seq nos, but including chunking,
    encoding and encryption; just for handshakes.
    message sent is data, then encrypted and encoded and chunked data.
    If ctrprty_nick is included, this nick is included in the header to direct the message.
    (Only one side of the handshake needs this).
    '''
    header = header if not ctrprty_nick else ':' + ctrprty_nick + ' ' + header
    chunks = len(data) / msg_chunk_size + 1
    if len(data) % msg_chunk_size == 0:
        chunks -= 1  #avoid creating an empty chunk if data length is a multiple of chunk_size

    for chunk_index in range(chunks):
        chunk = data[msg_chunk_size * chunk_index:msg_chunk_size *
                     (chunk_index + 1)]
        encrypted_encoded_chunk = ee(str(chunk_index) + chunk, pk)
        ending = 'EOL' if chunk_index == chunks - 1 else 'CRLF'
        tlsn_send_raw(header + ' ' + encrypted_encoded_chunk + ' ' + ending)
        time.sleep(0.5)
Пример #4
0
def tlsn_send_msg(data, pk, ack_q, recipient, seq_init=100000, raw=False):
    '''Send a message <data> on an already negotiated connection ;
    wait for an acknowledgement by polling for it on Queue ack_q
    Messages are sent with sequence numbers initialised at seq_init,
    or 0 if seq_init is undef.
    Messages larger than chunk_size are split into chunks with line endings.
    After chunking, messages are encrypted to public key pk then base64 encoded.
    CRLF and EOL are appended to the end of chunks according to tlsnotary's messaging protocol.
    Return 'success' only if message was sent and ack received correctly, otherwise 'failure'.
    '''
    if not initialized:
        raise Exception("TLSN Messaging not yet instantiated")

    if not hasattr(tlsn_send_msg, "my_seq"):
        if not seq_init: seq_init = 0
        tlsn_send_msg.my_seq = seq_init  #static variable. Initialized only on first function's run

    #split up data longer than chunk_size bytes
    chunks = len(data) / msg_chunk_size + 1
    if len(data) % msg_chunk_size == 0:
        chunks -= 1  #avoid creating an empty chunk if data length is a multiple of chunk_size

    for chunk_index in range(chunks):
        tlsn_send_msg.my_seq += 1
        chunk = data[msg_chunk_size * chunk_index:msg_chunk_size *
                     (chunk_index + 1)]
        #encrypt and base 64 encode the chunk; if we have used a sensible chunk size
        #this will neither cause a problem for RSA nor for IRC
        encrypted_encoded_chunk = ee(chunk, pk)

        ending = ' EOL ' if chunk_index + 1 == chunks else ' CRLF '  #EOL for the last chunk, otherwise CRLF
        msg_to_send = ' :' + recipient + ' seq:' + str(
            tlsn_send_msg.my_seq) + ' ' + encrypted_encoded_chunk + ending

        if not raw:
            for i in range(3):
                b_was_message_acked = False
                #empty the ack queue. Not using while True: because sometimes an endless loop would happen TODO: find out why
                for j in range(5):
                    try:
                        ack_q.get_nowait()
                    except:
                        pass
                tlsn_send_raw(msg_to_send)
                try:
                    ack_check = ack_q.get(block=True, timeout=3)
                except:
                    continue  #send again because ack was not received
                #print ('ack check is: ',ack_check)
                if not str(tlsn_send_msg.my_seq) == ack_check: continue
                #else: correct ack received
                #print ('message was acked')
                b_was_message_acked = True
                break

            if not b_was_message_acked:
                return 'failure'
        else:
            tlsn_send_raw(msg_to_send)

    return 'success'