gcontini
2020-01-11 95d1452eefadffaf1ec75dd0a8336bc2c387eb17
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * pc-identifiers.c
 *
 *  Created on: Apr 16, 2014
 *
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
#include "../os/os.h"
#include "pc-identifiers.h"
#include "../base/base64.h"
#include "../base/base.h"
#ifdef __linux__
#include <stdbool.h>
#include <valgrind/memcheck.h>
#else
#ifdef __MINGW32__
#include <windows.h>
#else
#include <Windows.h>
#endif
#endif
 
static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, bool use_label);
 
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, int use_mac);
 
static FUNCTION_RETURN generate_default_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers) {
    size_t adapter_num, disk_num;
    FUNCTION_RETURN result_adapterInfos, result_diskinfos, function_return;
    unsigned int caller_identifiers, i, j, k, array_index;
    DiskInfo *diskInfoPtr;
    OsAdapterInfo *adapterInfoPtr;
 
    if (identifiers == NULL || *num_identifiers == 0) {
        result_adapterInfos = getAdapterInfos(NULL, &adapter_num);
        if ((result_adapterInfos != FUNC_RET_OK) || (adapter_num == 0)) {
            return generate_disk_pc_id(identifiers, num_identifiers, false);
        }
        result_diskinfos = getDiskInfos(NULL, &disk_num);
        if ((result_diskinfos != FUNC_RET_OK) || (disk_num == 0)) {
            return generate_ethernet_pc_id(identifiers, num_identifiers, true);
        }
        *num_identifiers = disk_num * adapter_num;
        function_return = FUNC_RET_OK;
    } else {
        adapterInfoPtr = (OsAdapterInfo *)malloc((*num_identifiers) * sizeof(OsAdapterInfo));
        adapter_num = *num_identifiers;
        result_adapterInfos = getAdapterInfos(adapterInfoPtr, &adapter_num);
        if (result_adapterInfos != FUNC_RET_OK && result_adapterInfos != FUNC_RET_BUFFER_TOO_SMALL) {
            free(adapterInfoPtr);
            return generate_disk_pc_id(identifiers, num_identifiers, false);
        }
        diskInfoPtr = (DiskInfo *)malloc((*num_identifiers) * sizeof(DiskInfo));
        disk_num = *num_identifiers;
        result_diskinfos = getDiskInfos(diskInfoPtr, &disk_num);
        if (result_diskinfos != FUNC_RET_OK && result_diskinfos != FUNC_RET_BUFFER_TOO_SMALL) {
            free(diskInfoPtr);
            free(adapterInfoPtr);
            return generate_ethernet_pc_id(identifiers, num_identifiers, true);
        }
        function_return = FUNC_RET_OK;
 
        caller_identifiers = *num_identifiers;
        for (i = 0; i < disk_num; i++) {
            for (j = 0; j < adapter_num; j++) {
                array_index = i * adapter_num + j;
                if (array_index >= caller_identifiers) {
                    function_return = FUNC_RET_BUFFER_TOO_SMALL;
                    goto end;
                }
                for (k = 0; k < 6; k++)
                    identifiers[array_index][k] = diskInfoPtr[i].disk_sn[k + 2] ^ adapterInfoPtr[j].mac_address[k + 2];
            }
        }
    end:
#ifdef _MSC_VER
        *num_identifiers = min(*num_identifiers, adapter_num * disk_num);
#else
        *num_identifiers = cmin(*num_identifiers, adapter_num * disk_num);
#endif
        free(diskInfoPtr);
        free(adapterInfoPtr);
    }
    return function_return;
}
 
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, int use_mac) {
    return FUNC_RET_NOT_AVAIL;
}
 
/**
 *
 * Calculates all the possible identifiers for the current machine, for the
 * given calculation strategy requested. Pc identifiers are more than one,
 * for instance a machine with more than one disk and one network interface has
 * usually multiple identifiers.
 *
 * First 4 bit of each pc identifier are reserved 3 for the type of strategy
 * used in calculation and 1 for parity checks (not implemented here)
 *
 * @param identifiers
 * @param array_size
 * @param
 * @return
 */
 
FUNCTION_RETURN generate_pc_id(PcIdentifier *identifiers, unsigned int *array_size, LCC_IDENTIFICATION_STRATEGY strategy) {
    FUNCTION_RETURN result;
    unsigned int i, j;
    const unsigned int original_array_size = *array_size;
    unsigned char strategy_num;
    switch (strategy) {
        case STRATEGY_DEFAULT:
            result = generate_default_pc_id(identifiers, array_size);
            break;
        case STRATEGY_ETHERNET:
            result = generate_ethernet_pc_id(identifiers, array_size, true);
            break;
        case STRATEGY_IP_ADDRESS:
            result = generate_ethernet_pc_id(identifiers, array_size, false);
            break;
        case STRATEGY_DISK_NUM:
            result = generate_disk_pc_id(identifiers, array_size, false);
            break;
        case STRATEGY_DISK_LABEL:
            result = generate_disk_pc_id(identifiers, array_size, true);
            break;
        default:
            return FUNC_RET_ERROR;
    }
 
    if (result == FUNC_RET_OK && identifiers != NULL) {
        strategy_num = strategy << 5;
        for (i = 0; i < *array_size; i++) {
            // encode strategy in the first three bits of the pc_identifier
            identifiers[i][0] = (identifiers[i][0] & 15) | strategy_num;
        }
        // fill array if larger
        for (i = *array_size; i < original_array_size; i++) {
            identifiers[i][0] = STRATEGY_UNKNOWN << 5;
            for (j = 1; j < sizeof(PcIdentifier); j++) {
                identifiers[i][j] = 42;  // padding
            }
        }
    }
    return result;
}
 
char *MakeCRC(char *BitString) {
    static char Res[3];  // CRC Result
    char CRC[2];
    int i;
 
    for (i = 0; i < 2; ++i) CRC[i] = 0;  // Init before calculation
 
    for (i = 0; i < strlen(BitString); ++i) {
        char doInvert = ('1' == BitString[i]) ^ CRC[1];  // XOR required?
 
        CRC[1] = CRC[0];
        CRC[0] = doInvert;
    }
 
    for (i = 0; i < 2; ++i) Res[1 - i] = CRC[i] ? '1' : '0';  // Convert binary to ASCII
    Res[2] = 0;  // Set string terminator
 
    return (Res);
}
 
FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2, PcSignature pc_identifier_out) {
    // TODO base62 encoding, now uses base64
    PcIdentifier concat_identifiers[2];
    char *b64_data = NULL;
    int b64_size = 0;
    const size_t concatIdentifiersSize = sizeof(PcIdentifier) * 2;
    // concat_identifiers = (PcIdentifier *) malloc(concatIdentifiersSize);
    memcpy(&concat_identifiers[0], identifier1, sizeof(PcIdentifier));
    memcpy(&concat_identifiers[1], identifier2, sizeof(PcIdentifier));
    b64_data = base64(concat_identifiers, concatIdentifiersSize, &b64_size);
    if (b64_size > sizeof(PcSignature)) {
        free(b64_data);
        return FUNC_RET_BUFFER_TOO_SMALL;
    }
    sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], &b64_data[4], &b64_data[8], &b64_data[12]);
    // free(concat_identifiers);
    free(b64_data);
    return FUNC_RET_OK;
}
 
FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) { return FUNC_RET_OK; }
 
FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out, LCC_IDENTIFICATION_STRATEGY strategy) {
    FUNCTION_RETURN result;
    PcIdentifier *identifiers;
    unsigned int req_buffer_size = 0;
    result = generate_pc_id(NULL, &req_buffer_size, strategy);
    if (result != FUNC_RET_OK) {
        return result;
    }
    if (req_buffer_size == 0) {
        return FUNC_RET_ERROR;
    }
    req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size;
    identifiers = (PcIdentifier *)malloc(sizeof(PcIdentifier) * req_buffer_size);
    memset(identifiers, 0, sizeof(PcIdentifier) * req_buffer_size);
    result = generate_pc_id(identifiers, &req_buffer_size, strategy);
    if (result != FUNC_RET_OK) {
        free(identifiers);
        return result;
    }
#ifdef __linux__
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[0]);
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[1]);
#endif
    result = encode_pc_id(identifiers[0], identifiers[1], identifier_out);
#ifdef __linux__
    VALGRIND_CHECK_VALUE_IS_DEFINED(identifier_out);
#endif
    free(identifiers);
    return result;
}
 
/**
 * Extract the two pc identifiers from the user provided code.
 * @param identifier1_out
 * @param identifier2_out
 * @param str_code: the code in the string format XXXX-XXXX-XXXX-XXXX
 * @return
 */
static FUNCTION_RETURN decode_pc_id(PcIdentifier identifier1_out, PcIdentifier identifier2_out,
                                    PcSignature pc_signature_in) {
    // TODO base62 encoding, now uses base64
 
    unsigned char *concat_identifiers = NULL;
    char base64ids[17];
    int identifiers_size;
 
    sscanf(pc_signature_in, "%4s-%4s-%4s-%4s", &base64ids[0], &base64ids[4], &base64ids[8], &base64ids[12]);
    concat_identifiers = unbase64(base64ids, 16, &identifiers_size);
    if (identifiers_size > sizeof(PcIdentifier) * 2) {
        free(concat_identifiers);
        return FUNC_RET_BUFFER_TOO_SMALL;
    }
    memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier));
    memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier), sizeof(PcIdentifier));
    free(concat_identifiers);
    return FUNC_RET_OK;
}
 
static LCC_IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) {
    return (LCC_IDENTIFICATION_STRATEGY)identifier[0] >> 5;
}
 
LCC_EVENT_TYPE validate_pc_signature(PcSignature str_code) {
    PcIdentifier user_identifiers[2];
    FUNCTION_RETURN result;
    LCC_IDENTIFICATION_STRATEGY previous_strategy_id, current_strategy_id;
    PcIdentifier *calculated_identifiers = NULL;
    unsigned int calc_identifiers_size = 0;
    int i = 0, j = 0;
    // bool found;
#ifdef _DEBUG
    printf("Comparing pc identifiers: \n");
#endif
    result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code);
    if (result != FUNC_RET_OK) {
        return result;
    }
    previous_strategy_id = STRATEGY_UNKNOWN;
    // found = false;
    for (i = 0; i < 2; i++) {
        current_strategy_id = strategy_from_pc_id(user_identifiers[i]);
        if (current_strategy_id == STRATEGY_UNKNOWN && previous_strategy_id == STRATEGY_UNKNOWN && i == 1) {
            free(calculated_identifiers);
            printf("Comparing pc identifiers: %d %d %d %s\n", current_strategy_id, previous_strategy_id, i, str_code);
            return LICENSE_MALFORMED;
        } else if (current_strategy_id == STRATEGY_UNKNOWN) {
            continue;
        }
        if (current_strategy_id != previous_strategy_id) {
            if (calculated_identifiers != NULL) {
                free(calculated_identifiers);
            }
            previous_strategy_id = current_strategy_id;
            generate_pc_id(NULL, &calc_identifiers_size, current_strategy_id);
            calculated_identifiers = (PcIdentifier *)malloc(sizeof(PcIdentifier) * calc_identifiers_size);
            memset(calculated_identifiers, 0, sizeof(PcIdentifier) * calc_identifiers_size);
            generate_pc_id(calculated_identifiers, &calc_identifiers_size, current_strategy_id);
        }
        // maybe skip the byte 0
        for (j = 0; j < calc_identifiers_size; j++) {
#ifdef _DEBUG
            printf(
                "generated id: %02x%02x%02x%02x%02x%02x index %d, user_supplied id %02x%02x%02x%02x%02x%02x idx: %d\n",
                calculated_identifiers[j][0], calculated_identifiers[j][1], calculated_identifiers[j][2],
                calculated_identifiers[j][3], calculated_identifiers[j][4], calculated_identifiers[j][5], j,
                user_identifiers[i][0], user_identifiers[i][1], user_identifiers[i][2], user_identifiers[i][3],
                user_identifiers[i][4], user_identifiers[i][5], i);
 
#endif
            if (!memcmp(user_identifiers[i], calculated_identifiers[j], sizeof(PcIdentifier))) {
                free(calculated_identifiers);
                return LICENSE_OK;
            }
        }
    }
    free(calculated_identifiers);
    return IDENTIFIERS_MISMATCH;
}