aboutsummaryrefslogtreecommitdiff
path: root/src/nfc-wallet/wallet.c
blob: 4b92f7d4ed0008f8e5a2ab84986ff61754fa4ef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 This file is part of TALER
 Copyright (C) 2019 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

 You should have received a copy of the GNU General Public License
along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/

/**
* @file wallet.c
* @brief functions to communicate with taler wallet over nfc
* @author BOSS Marco
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wallet.h"


int wallet_select_aid( nfc_device *pnd )
{
    uint8_t response[] = { 0x00, 0x00 };

    int size = sizeof( select_file ) + sizeof( taler_aid );
    uint8_t message[size];
    if( concat_message( select_file, sizeof(select_file), taler_aid, message, size ) )
        return EXIT_FAILURE;

    printf( "wallet_select_aid: Selecting Taler apk using AID: " );
    for( unsigned int i = 0; i < sizeof(taler_aid); ++i )
        printf( "%.2x", taler_aid[i] );
    printf( "\n" );

    if( nfc_initiator_transceive_bytes( pnd, message, size, response, sizeof(response), TRANSMIT_TIMEOUT ) < 0 ) {
        printf( "wallet_select_aid: Failed to select apk\n\n" );
        return EXIT_FAILURE;
    }

    return check_response( response, sizeof(response) );
}

int wallet_put_message( nfc_device *pnd, const char *msg, size_t msgSize )
{
    uint8_t response[] = { 0x00, 0x00 };

    int size = sizeof( put_data ) + msgSize;
    uint8_t message[size];
    if( concat_message( put_data, sizeof(put_data), (const uint8_t*)msg, message, size ) )
        return EXIT_FAILURE;

    printf( "wallet_put_messsage: Sending 'PUT DATA' command to wallet\n" );

    if( nfc_initiator_transceive_bytes( pnd, message, size, response, sizeof(response), TRANSMIT_TIMEOUT ) < 0 ) {
        printf( "wallet_put_message: Failed to put message\n\n" );
        return EXIT_FAILURE;
    }

    return check_response( response, sizeof(response) );
}


int wallet_transmit( nfc_device *pnd, const char *msg, size_t msgLen )
{
    if( !msg ) {
        printf( "wallet_transmit: No message to send\n\n" );
        return EXIT_FAILURE;
    }

    if( wallet_select_aid( pnd ) )
        return EXIT_FAILURE;
    printf( "wallet_transmit: Taler wallet apk selected\n\n" );
    if( wallet_put_message( pnd, msg, msgLen )  )
        return EXIT_FAILURE;
    printf( "wallet_transmit: Transmitted message to taler wallet\n\n");

    return EXIT_SUCCESS;
}

int check_response( uint8_t *response, uint8_t responseLen )
{
    if( strcmp((char*)response, APDU_SUCCESS ) == 0 ){
        printf("Transmission success\n");
    } else {
        printf("Transmission failure, return code: ");
        for( uint8_t i = 0; i < responseLen; ++i ){
            printf( "%.2x ", response[i] );
        }
        printf("\n");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

int concat_message(const uint8_t* command, size_t commandSize, const uint8_t* message, uint8_t *retMsg, size_t returnSize )
{
    if( !command || !message ){
        printf( "concat_message: command and message can't be null" );
        return EXIT_FAILURE;
    }

    uint8_t i = 0;
    for( ; i < commandSize; ++i ){
        retMsg[i] = command[i];
    }
    for( ; i < returnSize; ++i){
        retMsg[i] = message[i - commandSize];
    }

    return EXIT_SUCCESS;
}