-
Notifications
You must be signed in to change notification settings - Fork 34
/
license.c
482 lines (424 loc) · 8.98 KB
/
license.c
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "license.h"
#include "sha1.h"
#include "licgen.h"
#define INT_VAL_TYPE 0
#define STR_VAL_TYPE 1
#define MAC_FIX_LEN 16
#define MAX_SN_LEN 22
#define DIGEST_LEN 20
#define MAX_BUF 4096
#define LIC_PREFIX "grgrant."
#define LIC_SUFFIX ".lic"
static void sha_digest(unsigned char digest[DIGEST_LEN], unsigned char *data, int len)
{
int i;
SHA1_CTX ctx;
SHA1Init(&ctx);
for(i = 0; i<500; i++)
{
SHA1Update(&ctx, data, len);
}
SHA1Final(digest, &ctx);
/*printf("SHA1=");
for(i = 0; i < DIGEST_LEN; i++)
{
printf("%02x", digest[i]);
}
printf("\n");*/
}
static int sha_file(const char *file, int offset, unsigned char digest[DIGEST_LEN])
{
FILE *fp;
int nread, total;
int begin, end;
int len;
unsigned char buf[4096] = {0};
char *data, *pos;
fp = fopen(file, "rb");
if (fp == NULL)
return -1;
begin = fseek(fp, offset, SEEK_SET);
if (begin == -1)
{
fclose(fp);
return -1;
}
begin = ftell(fp);
if (begin == -1)
{
fclose(fp);
return -1;
}
end = fseek(fp, 0, SEEK_END);
if (end == -1)
{
fclose(fp);
return -1;
}
end = ftell(fp);
if (end == -1)
{
fclose(fp);
return -1;
}
total = end - begin;
if (total <= 0)
{
fclose(fp);
return -1;
}
data = (char*)calloc(1, total);
if (data == NULL)
{
fclose(fp);
return -1;
}
fseek(fp, offset, SEEK_SET);
pos = data;
len = total;
while (total > 0)
{
nread = fread(buf, 1, MAX_BUF, fp);
if (nread > 0)
{
memcpy(pos, buf, nread);
pos += nread;
total -= nread;
}
else
{
break;
}
}
sha_digest(digest, (unsigned char*)data, len);
fclose(fp);
free(data);
return 0;
}
int grlic_load(const char* Afname, gr_license_t* Alic)
{
int ret, nread;
uint32_t i, valtype;
uint64_t id, number;
char *str;
void *blob;
FILE *fp;
gr_license_entry_t *entry;
unsigned char digest[DIGEST_LEN];
fp = fopen(Afname, "rb");
if (fp == NULL)
return -2;
//read header
nread = fread(&Alic->head, sizeof(Alic->head), 1, fp);
if (nread != 1)
goto failure;
Alic->body.entries = (gr_license_entry_t*) calloc(Alic->head.entries, sizeof(gr_license_entry_t));
if (Alic->body.entries == NULL)
goto failure;
//read body
for (i = 0; i<Alic->head.entries; ++i)
{
entry = &(Alic->body.entries[i]);
id = 0;
nread = fread(&id, sizeof(uint64_t), 1, fp);
if (nread != 1)
goto failure;
entry->id = id ;
nread = fread(&entry->credit, sizeof(entry->credit), 1, fp);
if (nread != 1)
goto failure;
valtype = entry->credit.meta.valType;
number = 0;
str = NULL;
blob = NULL;
if (valtype == 1)
{
nread = fread(&number, sizeof(uint64_t), 1, fp);
if(nread != 1) goto failure;
if (number > entry->credit.quota.upper)
number = entry->credit.quota.upper;
if (number < entry->credit.quota.lower)
number = entry->credit.quota.lower;
entry->val.numval = number;
}
else if (valtype == 2)
{
if (entry->credit.meta.len > 0)
{
str = (char*)calloc(entry->credit.meta.len, sizeof(char));
nread = fread(str, entry->credit.meta.len, 1, fp);
if (nread != 1) goto failure;
entry->val.strval = str;
}
}
else if (valtype == 3)
{
if (entry->credit.meta.len > 0)
{
blob = (char*)calloc(entry->credit.meta.len, sizeof(char));
nread = fread(blob, entry->credit.meta.len, 1, fp);
if (nread != 1) goto failure;
entry->val.blobval = str;
}
}
}
fclose(fp);
//verify sn
ret = grlic_cmp_sn(Alic->head.sn);
if (ret == -1)
return -3;
//verify digest
memset(digest, 0, sizeof(digest));
ret = sha_file(Afname, sizeof(digest), digest);
if (ret == -1)
return -1;
if (!memcmp(digest, Alic->head.digest, DIGEST_LEN))
return 0;
else
return -1; //no match
failure:
fclose(fp);
return -1;
}
int grlic_save(const char* Afname, const gr_license_t* Alic)
{
FILE *fp;
size_t nwrite;
uint32_t i, valtype;
uint64_t id, number;
char *str;
void *blob;
unsigned char digest[DIGEST_LEN];
gr_license_entry_t *entry;
fp = fopen(Afname, "wb");
if (fp == NULL)
return -1;
//write head
nwrite = fwrite(&Alic->head, sizeof(Alic->head), 1, fp);
if (nwrite!= 1)
goto failure;
//write body
for (i = 0; i<Alic->head.entries; ++i)
{
entry = &(Alic->body.entries[i]);
id = entry->id;
nwrite = fwrite(&id, sizeof(uint64_t), 1, fp);
if (nwrite != 1)
goto failure;
nwrite = fwrite(&entry->credit, sizeof(entry->credit), 1, fp);
if (nwrite != 1)
goto failure;
valtype = entry->credit.meta.valType;
number = entry->val.numval;
str = entry->val.strval;
blob = entry->val.blobval;
if (valtype == 1)
{
if (number > entry->credit.quota.upper)
number = entry->credit.quota.upper;
if (number < entry->credit.quota.lower)
number = entry->credit.quota.lower;
nwrite = fwrite(&number, sizeof(uint64_t), 1, fp);
if(nwrite != 1) goto failure;
}
else if (valtype == 2)
{
if (entry->credit.meta.len > 0)
{
nwrite = fwrite(str, entry->credit.meta.len, 1, fp);
if (nwrite != 1) goto failure;
}
}
else if (valtype == 3)
{
if (entry->credit.meta.len > 0)
{
nwrite = fwrite(blob, entry->credit.meta.len, 1, fp);
if (nwrite != 1) goto failure;
}
}
}
fclose(fp);
//write digest
memset(digest, 0, sizeof(digest));
sha_file(Afname, sizeof(digest), digest);
fp = fopen(Afname, "r+b");
if (fp == NULL)
return -1;
nwrite = fwrite(digest, sizeof(digest), 1, fp);
if (nwrite != 1)
goto failure;
fclose(fp);
return 0;
failure:
fclose(fp);
return -1;
}
int grlic_take(gr_license_t* Alic)
{
int max = 8;
int len = 22;
int i, num, ret;
unsigned char *p = (unsigned char*)calloc(max, sizeof(unsigned char)*len);
unsigned char *sn[8];
unsigned char *pos = p;
char name[48];
for (i = 0; i<max; ++i)
{
sn[i] = pos;
pos += sizeof(unsigned char)*len;
}
num = grlic_gen_sns(sn, max);
for (i = 0; i<num; ++i)
{
memset(name, 0, sizeof(name));
sprintf(name, "%s%s%s", LIC_PREFIX, (char*)sn[i], LIC_SUFFIX);
ret = grlic_load(name, Alic);
if (ret == 0)
{
free(p);
return ret;
}
}
free(p);
return -1;
}
int grlic_gen_sn(unsigned char Asn[17])
{
int len;
unsigned char mac[17] = {0};
get_mac_sn(mac);
len = MAC_FIX_LEN;
memcpy(Asn, mac, len);
Asn[len] = '\0';
return 0;
}
int grlic_gen_sns(unsigned char **Asn, int len)
{
int max = 8;
int mac = 17;
int i, num;
unsigned char *p = (unsigned char*)calloc(max, sizeof(unsigned char)*mac);
unsigned char *sn[8];
unsigned char *pos = p;
for (i = 0; i<max; ++i)
{
sn[i] = pos;
pos += sizeof(unsigned char)*mac;
}
num = get_all_mac_sn(sn, max);
for (i = 0; i<num && i<len; ++i)
{
len = MAC_FIX_LEN;
memcpy(Asn[i], sn[i], len);
Asn[i][len] = '\0';
}
free(p);
return num;
}
int grlic_cmp_sn(unsigned char Asn[22])
{
int i, num;
int max = 8;
unsigned char *p = (unsigned char*)calloc(max, sizeof(unsigned char)*22);
unsigned char *sn[8];
unsigned char *pos = p;
for (i = 0; i<max; ++i)
{
sn[i] = pos;
pos += sizeof(unsigned char)*22;
}
num = grlic_gen_sns(sn, max);
for (i = 0; i<num; ++i)
{
if (!memcmp(sn[i], Asn, sizeof(unsigned char) * 22))
goto match;
}
free(p);
return -1;
match:
free(p);
return 0;
}
uint64_t grlic_path_id(gr_credit_path_t* Apath)
{
uint64_t id = Apath->module;
id = Apath->facet > 0 ? id * 100 + Apath->facet : id;
id = Apath->func > 0 ? id * 100 + Apath->func : id;
id = Apath->point > 0 ? id * 100 + Apath->point: id;
return id;
}
int grlic_free(gr_license_t* Alic)
{
uint32_t i, valtype;
gr_license_entry_t *entry;
for (i = 0; i<Alic->head.entries; ++i)
{
entry = &(Alic->body.entries[i]);
valtype = entry->credit.meta.valType;
if (valtype == 2)
{
free(entry->val.strval);
}
else if (valtype == 3)
{
free(entry->val.blobval);
}
}
free(Alic->body.entries);
return 0;
}
static gr_license_entry_t* get_lic_entry(gr_license_t* Alic, gr_credit_path_t* Apath)
{
uint32_t i;
uint64_t id;
gr_license_entry_t *entry;
if (!Alic || !Apath)
return NULL;
id = grlic_path_id(Apath);
if (Alic->head.entries > UINT8_MAX)
return NULL;
for (i = 0; i<Alic->head.entries; ++i)
{
entry = &(Alic->body.entries[i]);
if (entry->id == id)
{
return entry;
}
}
return NULL;
}
int grlic_get_bool(gr_license_t* Alic, gr_credit_path_t* Apath, int* Aval)
{
return grlic_get_int(Alic, Apath, Aval);
}
int grlic_get_int(gr_license_t* Alic, gr_credit_path_t* Apath, int* Aval)
{
gr_license_entry_t *entry = get_lic_entry(Alic, Apath);
if (entry == NULL)
return -1;
*Aval = (int)entry->val.numval;
return 0;
}
int grlic_get_str(gr_license_t* Alic, gr_credit_path_t* Apath, char* Astr, int* Alen)
{
gr_license_entry_t *entry;
entry = get_lic_entry(Alic, Apath);
if (entry == NULL)
return -1;
*Alen = entry->credit.meta.len;
if (entry->val.strval)
strncpy(Astr, entry->val.strval, *Alen);
return 0;
}
int grlic_get_blob(gr_license_t* Alic, gr_credit_path_t* Apath, void* Astr, int* Alen)
{
gr_license_entry_t *entry;
entry = get_lic_entry(Alic, Apath);
if (entry == NULL)
return -1;
*Alen = entry->credit.meta.len;
if (entry->val.blobval)
memcpy(Astr, entry->val.blobval, *Alen);
return 0;
}