Beispiel #1
0
def service_ticket():

    # M5 = [{ID_C + (T_A ou T_R) + S_R + N3}K_c_s + T_c_s]

    if 'K_c_s' not in variables:
        input('Error: K_c_s not defined.')
        return

    request = {
        'ID_C': variables['ID_C'],
        'T_A': variables['T_A'],
        'S_R': variables['ID_S'],
        'N3': ha.random(16)
    }
    request = json.dumps(request)

    print('------------------------------------------')
    print('Message:')
    print(request)

    encrypted_message, nonce, tag = ha.aes_encrypt(request, variables['K_c_s'])
    request = {
        'encrypted_message': encrypted_message,
        'nonce': nonce,
        'tag': tag,
    }
    request = {'message': request, 'T_c_s': variables['T_c_s']}

    print('------------------------------------------')
    print('Saving M5 Service Ticket...')
    print(request)
    print('------------------------------------------')

    db.save_database('service_ticket', request)
    input('Press enter to return to the main menu.')
Beispiel #2
0
# In[]
import hashing_algorithms as ha

# In[]
ciphertext, nonce, tag = ha.aes_encrypt('teste', 'key')
print(ciphertext, nonce, tag)

# In[]
ha.aes_decrypt(ciphertext, 'key', nonce, tag)
Beispiel #3
0
def as_conn():
    try:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, AS_PORT))
            data = s.recv(1024)
            print(data.decode('utf-8'))

            variables['ID_C'] = input('Name: ')
            variables['ID_S'] = 'service'  #input('Service: ')
            variables['T_R'] = 100  #input('Time: ')
            variables['K_c'] = ha.hash(
                input('Enter your password: '******'utf-8')

            message = {
                'ID_S': variables['ID_S'],
                'T_R': variables['T_R'],
                'N1': ha.random(16)
            }
            message = json.dumps(message)

            print('------------------------------------------')
            print('Message:')
            print(message)
            print('------------------------------------------')
            print('K_c:')
            print(variables['K_c'])

            encrypted_message, nonce, tag = ha.aes_encrypt(
                message, variables['K_c'])

            request = {
                #'action': 'auth',
                'ID_C': variables['ID_C'],
                'message': {
                    'encrypted_message': encrypted_message,
                    'nonce': nonce,
                    'tag': tag
                }
            }
            print('------------------------------------------')
            print('Sending M1 to server...')
            print(request)

            s.sendall(json.dumps(request).encode('utf-8'))

            data = s.recv(1024)

            if data:
                try:
                    data = data.decode('utf-8')
                    AS_response = json.loads(data)
                    response = AS_response['response']

                    print('------------------------------------------')
                    print('Received M2 from server:')
                    print(data)

                    decrypted = ha.aes_decrypt(response['encrypted_message'],
                                               variables['K_c'],
                                               response['nonce'],
                                               response['tag'])

                    decrypted = json.loads(decrypted)

                    print('------------------------------------------')
                    print('Decrypted response:')
                    print(decrypted)
                    print('------------------------------------------')

                    variables['K_c_tgs'] = decrypted['K_c_tgs']
                    variables['T_c_tgs'] = AS_response['T_c_tgs']
                except:
                    print(data)
            else:
                print('Server error.')

            input('Press enter to return to the main menu.')

    except ConnectionRefusedError:
        print('Error! AS Server unavailable. Try again later.')
        input('Press enter to return to the main menu.')
Beispiel #4
0
def tgs_conn():

    if 'K_c_tgs' not in variables:
        input('Error: K_c_tgs not defined.')
        return

    try:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((HOST, TGS_PORT))
            data = s.recv(1024)
            print(data.decode('utf-8'))

            message = {
                'ID_C': variables['ID_C'],
                #'ID_C': 'teste',
                'ID_S': variables['ID_S'],
                'T_R': variables['T_R'],
                #'T_R': 1,
                'N2': ha.random(16)
            }
            message = json.dumps(message)

            print('------------------------------------------')
            print('Message:')
            print(message)
            print('------------------------------------------')
            print('K_c_tgs:')
            print(variables['K_c_tgs'])

            encrypted_message, nonce, tag = ha.aes_encrypt(
                message, variables['K_c_tgs'])

            request = {
                'message': {
                    'encrypted_message': encrypted_message,
                    'nonce': nonce,
                    'tag': tag
                },
                'T_c_tgs': variables['T_c_tgs']
            }

            print('------------------------------------------')
            print('Sending M3 to server...')
            print(request)

            s.sendall(json.dumps(request).encode('utf-8'))

            data = s.recv(1024)

            if data:
                try:
                    data = data.decode('utf-8')
                    TGS_response = json.loads(data)
                    response = TGS_response['response']
                    T_c_s = TGS_response['T_c_s']

                    print('------------------------------------------')
                    print('Received M4 from server:')
                    print(data)

                    decrypted = ha.aes_decrypt(response['encrypted_message'],
                                               variables['K_c_tgs'],
                                               response['nonce'],
                                               response['tag'])

                    decrypted = json.loads(decrypted)

                    print('------------------------------------------')
                    print('Decrypted response:')
                    print(decrypted)
                    print('------------------------------------------')

                    variables['K_c_s'] = decrypted['K_c_s']
                    variables['T_c_s'] = T_c_s
                    variables['T_A'] = str(decrypted['T_A'])
                except:
                    print(data)
            else:
                print('Server error.')

            input('Press enter to return to the main menu.')

    except ConnectionRefusedError:
        print('Error! AS Server unavailable. Try again later.')
        input('Press enter to return to the main menu.')
Beispiel #5
0
def worker(conn, mask):
    data = conn.recv(1024)
    if data:
        request = json.loads(data.decode('utf-8'))
        print('------------------------------------------')
        print(str(conn.getpeername()) + ' sent M3: ' + str(request))

        T_c_tgs = request['T_c_tgs']
        message = request['message']

        decrypted_T_c_tgs = ha.aes_decrypt(T_c_tgs['encrypted_message'],
                                           TGS_db['TGS'], T_c_tgs['nonce'],
                                           T_c_tgs['tag'])
        decrypted_T_c_tgs = json.loads(decrypted_T_c_tgs)

        print('------------------------------------------')
        print('Decrypted TGS ticket from the AS server: ')
        print(decrypted_T_c_tgs)

        decrypted_message = ha.aes_decrypt(message['encrypted_message'],
                                           decrypted_T_c_tgs['K_c_tgs'],
                                           message['nonce'], message['tag'])
        decrypted_message = json.loads(decrypted_message)

        print('------------------------------------------')
        print('Decrypted message from client with the AS token key: ')
        print(decrypted_message)

        if decrypted_message['ID_S'] in TGS_db:

            if decrypted_T_c_tgs['ID_C'] != decrypted_message[
                    'ID_C'] or decrypted_T_c_tgs['T_R'] != decrypted_message[
                        'T_R']:
                print('Error validating data.')
                conn.sendall(b'Error validating data.')
                return

            K_c_s = ha.random()

            T_a = datetime.now() + timedelta(0, decrypted_T_c_tgs['T_R'])

            response = {
                'K_c_s': K_c_s,
                'T_A': str(T_a),
                'N2': decrypted_message['N2']
            }

            print('------------------------------------------')
            print('Response:')
            print(response)
            print('------------------------------------------')
            print('K_c_tgs:')
            print(decrypted_T_c_tgs['K_c_tgs'])

            encrypted_message, nonce, tag = ha.aes_encrypt(
                json.dumps(response), decrypted_T_c_tgs['K_c_tgs'])
            response = {
                'encrypted_message': encrypted_message,
                'nonce': nonce,
                'tag': tag
            }

            T_c_s = {
                'ID_C': decrypted_T_c_tgs['ID_C'],
                'T_A': str(T_a),
                'K_c_s': K_c_s
            }

            print('------------------------------------------')
            print('T_c_s:')
            print(T_c_s)
            print('------------------------------------------')
            print('K_s:')
            print(TGS_db[decrypted_message['ID_S']])

            encrypted_message, nonce, tag = ha.aes_encrypt(
                json.dumps(T_c_s), TGS_db[decrypted_message['ID_S']])
            T_c_s = {
                'encrypted_message': encrypted_message,
                'nonce': nonce,
                'tag': tag
            }

            print('------------------------------------------')
            print('Sending M4 to client...')
            print('Response: ' + str(response))
            print('T_c_s: ' + str(T_c_s))
            print('------------------------------------------')

            conn.sendall(
                json.dumps({
                    'response': response,
                    'T_c_s': T_c_s
                }).encode('utf-8'))
        else:
            conn.sendall(b'Unknown service.')

    else:
        print('Ending client ' + str(conn.getpeername()) + ' connection...')
        sel.unregister(conn)
        conn.close()
Beispiel #6
0
def worker(conn, mask):
    data = conn.recv(1024)
    if data:
        request = json.loads(data.decode('utf-8'))
        print('------------------------------------------')
        print(str(conn.getpeername()) + ' sent M1: ' + str(request))
        message = request['message']

        if request['ID_C'] in AS_db:

            decrypted = ha.aes_decrypt(message['encrypted_message'],
                                       AS_db[request['ID_C']],
                                       message['nonce'], message['tag'])

            print('------------------------------------------')
            print('K_c:')
            print(AS_db[request['ID_C']])

            if decrypted:
                decrypted = json.loads(decrypted)

                print('------------------------------------------')
                print('Decrypted:')
                print(decrypted)

                K_c_tgs = ha.random()

                response = {'K_c_tgs': K_c_tgs, 'N1': decrypted['N1']}

                print('------------------------------------------')
                print('Response:')
                print(response)

                encrypted_response, nonce, tag = ha.aes_encrypt(
                    json.dumps(response), AS_db[request['ID_C']])
                response = {
                    'encrypted_message': encrypted_response,
                    'nonce': nonce,
                    'tag': tag
                }

                T_c_tgs = {
                    'ID_C': request['ID_C'],
                    'T_R': decrypted['T_R'],
                    'K_c_tgs': K_c_tgs
                }

                print('------------------------------------------')
                print('T_c_tgs:')
                print(T_c_tgs)
                print('------------------------------------------')
                print('K_tgs:')
                print(AS_db['TGS'])

                encrypted_T_c_tgs, nonce, tag = ha.aes_encrypt(
                    json.dumps(T_c_tgs), AS_db['TGS'])
                T_c_tgs = {
                    'encrypted_message': encrypted_T_c_tgs,
                    'nonce': nonce,
                    'tag': tag
                }

                print('------------------------------------------')
                print('Sending M2 to client...')
                print('Response: ' + str(response))
                print('T_c_tgs: ' + str(T_c_tgs))
                print('------------------------------------------')

                conn.sendall(
                    json.dumps({
                        'response': response,
                        'T_c_tgs': T_c_tgs
                    }).encode('utf-8'))
            else:
                conn.sendall(b'Error validating encrypted message.')

            #print(decrypted)

        else:
            conn.sendall(b'Unknown method/client.')

    else:
        print('Ending client ' + str(conn.getpeername()) + ' connection...')
        sel.unregister(conn)
        conn.close()