-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_certs.c
153 lines (135 loc) · 4.02 KB
/
load_certs.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
/*********************************************************************
This file is a part of FItsSec project: Implementation of ETSI TS 103 097
Copyright (C) 2015 Denis Filatov (danya.filatov()gmail.com)
This program 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 of the License, or
(at your option) any later version.
This program is distributed under GNU GPLv3 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 Foobar. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
@license GPL-3.0+ <http://www.gnu.org/licenses/gpl-3.0.txt>
In particular cases this program can be distributed under other license
by simple request to the author.
*********************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include "cstr.h"
#include "cmem.h"
#include "fitsec.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <inttypes.h>
#ifdef WIN32
#include <windows.h>
#else
#include <dirent.h>
#endif
int loadCertificates(FitSec * e, const pchar_t * _path);
static char * _data;
static int _dsize = 4096;
static FSHashedId8 _load_certificate(FitSec * e, pchar_t * path, pchar_t * fname)
{
char *data, *end;
char *vkey = NULL, *ekey = NULL;
size_t cert_len = 0, vkey_len = 0, ekey_len = 0;
pchar_t *ext;
FSHashedId8 digest = (FSHashedId8)-1;
int error = 0;
data = _data;
end = cstrnload(data, _dsize, path);
if (end > data){
cert_len = end - data;
printf("%-2s %s:", FitSec_Name(e), fname);
// look for keys
ext = cstrpathextension(fname);
pchar_cpy(ext, ".vkey");
vkey = end;
end = cstrnload(vkey, _dsize - (vkey - data), path);
if (end <= vkey){
end = vkey; vkey = NULL;
}
else{
vkey_len = end - vkey;
}
pchar_cpy(ext, ".ekey");
ekey = end;
end = cstrnload(ekey, _dsize - (ekey - data), path);
if (end <= ekey){
end = ekey; ekey = NULL;
}
else{
ekey_len = end - ekey;
}
const FSCertificate* c = FitSec_InstallCertificate(e, data, cert_len, vkey, vkey_len, ekey, ekey_len, &error);
digest = FSCertificate_Digest(c);
printf(" [%016"PRIX64"] - %s\n", cint64_hton(digest), FitSec_ErrorMessage(error));
}
else{
error = -1;
printf("%-2s: %s: Empty File\n", FitSec_Name(e), fname);
}
return digest;
}
int loadCertificates(FitSec * e, const pchar_t * _path)
{
size_t plen;
pchar_t *path;
int ccount = 0;
plen = _path ? pchar_len(_path) : 0;
path = malloc((plen + 256) * sizeof(pchar_t));
if (plen){
memcpy(path, _path, plen * sizeof(pchar_t));
while (plen && (path[plen - 1] == '/' || path[plen - 1] == '\\')) plen--;
}
if (plen == 0) path[plen++] = '.';
path[plen++] = '/';
path[plen] = 0;
_data = malloc(_dsize); // it must not be more
#ifdef WIN32
{
WIN32_FIND_DATA fd;
HANDLE h;
pchar_cpy(path + plen, "*.oer");
h = FindFirstFile(path, &fd);
if (INVALID_HANDLE_VALUE != h){
do{
if (!(fd.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)) {
pchar_cpy(path + plen, fd.cFileName);
FSHashedId8 digest = _load_certificate(e, path, path + plen);
if(digest != (FSHashedId8)-1) {
ccount++;
}
}
} while (FindNextFile(h, &fd));
FindClose(h);
}
}
#else
{
DIR * d;
struct dirent * de;
d = opendir(path);
if(d){
while((de = readdir(d))){
pchar_t * ext = pchar_rchr(de->d_name, '.');
if(ext && 0 == strcmp(ext, ".oer")){
pchar_cpy(path + plen, de->d_name);
if (0 <= _load_certificate(e, path, path + plen)){
ccount++;
}
}
}
closedir(d);
}
}
#endif
free(path);
free(_data);
FitSec_RelinkCertificates(e);
return ccount;
}