-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParseCert.cpp
391 lines (233 loc) · 6.37 KB
/
ParseCert.cpp
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
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file: ParseCert.cpp
* @author: fisco-dev
*
* @date: 2017
*/
#include "ParseCert.h"
ParseCert::ParseCert(void)
{
}
ParseCert::~ParseCert(void)
{
}
void ParseCert::ParseInfo(ba::ssl::verify_context& ctx)
{
//subject name of the certificate
char subject_name[256];
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
X509_NAME_oneline(X509_get_subject_name(cert), subject_name, 256);
m_subjectName.insert(0,subject_name);
int err = 0;
//lifetime of the certificate
ASN1_TIME *start = NULL;
ASN1_TIME *end = NULL;
time_t ttStart = {0};
time_t ttEnd = {0};
start = X509_get_notBefore(cert);
end = X509_get_notAfter(cert);
ttStart = ASN1_TIME_get(start, &err);
ttEnd = ASN1_TIME_get(end, &err);
time_t t_Now = time(0);
localtime(&t_Now);
if (t_Now < ttStart || t_Now > ttEnd)
{
m_isExpire = true;
}
else
{
m_isExpire = false;
}
//serial number of the certificate
ASN1_INTEGER *asn1_i = NULL;
char* serial = NULL;
BIGNUM *bignum = NULL;
asn1_i = X509_get_serialNumber(cert);
bignum = ASN1_INTEGER_to_BN(asn1_i, NULL);
serial = BN_bn2hex(bignum);
m_serialNumber.insert(0,serial);
BN_free(bignum);
OPENSSL_free(serial);
//type of the certificate
BASIC_CONSTRAINTS *bcons = NULL;
int crit = 0;
bcons = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(cert, NID_basic_constraints, &crit, NULL);
m_certType = 0;
if (bcons != NULL)
{
if (bcons->ca == false)
{
m_certType = 1;
}
BASIC_CONSTRAINTS_free(bcons);
}
}
int ParseCert::mypint( const char ** s,int n,int min,int max,int * e)
{
int retval = 0;
while (n) {
if (**s < '0' || **s > '9') { *e = 1; return 0; }
retval *= 10;
retval += **s - '0';
--n; ++(*s);
}
if (retval < min || retval > max) *e = 1;
return retval;
}
time_t ParseCert::ASN1_TIME_get ( ASN1_TIME * a,int *err)
{
char days[2][12] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
int dummy;
const char *s;
int generalized;
struct tm t;
int i, year, isleap, offset;
time_t retval;
if (err == NULL) err = &dummy;
if (a->type == V_ASN1_GENERALIZEDTIME) {
generalized = 1;
} else if (a->type == V_ASN1_UTCTIME) {
generalized = 0;
} else {
*err = 1;
return 0;
}
s = (const char*)a->data; // Data should be always null terminated
if (s == NULL || s[a->length] != '\0') {
*err = 1;
return 0;
}
*err = 0;
if (generalized) {
t.tm_year = mypint(&s, 4, 0, 9999, err) - 1900;
} else {
t.tm_year = mypint(&s, 2, 0, 99, err);
if (t.tm_year < 50) t.tm_year += 100;
}
t.tm_mon = mypint(&s, 2, 1, 12, err) - 1;
t.tm_mday = mypint(&s, 2, 1, 31, err);
// NOTE: It's not yet clear, if this implementation is 100% correct
// for GeneralizedTime... but at least misinterpretation is
// impossible --- we just throw an exception
t.tm_hour = mypint(&s, 2, 0, 23, err);
t.tm_min = mypint(&s, 2, 0, 59, err);
if (*s >= '0' && *s <= '9') {
t.tm_sec = mypint(&s, 2, 0, 59, err);
} else {
t.tm_sec = 0;
}
if (*err) return 0; // Format violation
if (generalized) {
// skip fractional seconds if any
while (*s == '.' || *s == ',' || (*s >= '0' && *s <= '9')) ++s;
// special treatment for local time
if (*s == 0) {
t.tm_isdst = -1;
retval = mktime(&t); // Local time is easy :)
if (retval == (time_t)-1) {
*err = 2;
retval = 0;
}
return retval;
}
}
if (*s == 'Z') {
offset = 0;
++s;
} else if (*s == '-' || *s == '+') {
i = (*s++ == '-');
offset = mypint(&s, 2, 0, 12, err);
offset *= 60;
offset += mypint(&s, 2, 0, 59, err);
if (*err) return 0; // Format violation
if (i) offset = -offset;
} else {
*err = 1;
return 0;
}
if (*s) {
*err = 1;
return 0;
}
// And here comes the hard part --- there's no standard function to
// convert struct tm containing UTC time into time_t without
// messing global timezone settings (breaks multithreading and may
// cause other problems) and thus we have to do this "by hand"
//
// NOTE: Overflow check does not detect too big overflows, but is
// sufficient thanks to the fact that year numbers are limited to four
// digit non-negative values.
retval = t.tm_sec;
retval += (t.tm_min - offset) * 60;
retval += t.tm_hour * 3600;
retval += (t.tm_mday - 1) * 86400;
year = t.tm_year + 1900;
if ( sizeof (time_t) == 4) {
// This is just to avoid too big overflows being undetected, finer
// overflow detection is done below.
if (year < 1900 || year > 2040) *err = 2;
}
// FIXME: Does POSIX really say, that all years divisible by 4 are
// leap years (for consistency)??? Fortunately, this problem does
// not exist for 32-bit time_t and we should'nt be worried about
// this until the year of 2100 :)
isleap = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
for (i = t.tm_mon - 1; i >= 0; --i) retval += days[isleap][i] * 86400;
retval += (year - 1970) * 31536000;
if (year < 1970) {
retval -= ((1970 - year + 2) / 4) * 86400;
if ( sizeof (time_t) > 4) {
for (i = 1900; i >= year; i -= 100) {
if (i % 400 == 0) continue ;
retval += 86400;
}
}
if (retval >= 0) *err = 2;
} else {
retval += ((year - 1970 + 1) / 4) * 86400;
if ( sizeof (time_t) > 4) {
for (i = 2100; i < year; i += 100) {
// The following condition is the reason to
// start with 2100 instead of 2000
if (i % 400 == 0) continue ;
retval -= 86400;
}
}
if (retval < 0) *err = 2;
}
if (*err) retval = 0;
return retval;
}
bool ParseCert::getExpire()
{
return m_isExpire;
}
string ParseCert::getSerialNumber()
{
return m_serialNumber;
}
string ParseCert::getSubjectName()
{
return m_subjectName;
}
int ParseCert::getCertType()
{
return m_certType;
}