-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCryptosslX509Crl.cc
656 lines (579 loc) · 19.6 KB
/
XrdCryptosslX509Crl.cc
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/******************************************************************************/
/* */
/* X r d C r y p t o s s l X 5 0 9 C r l. c c */
/* */
/* (c) 2005 G. Ganis , CERN */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD 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 Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/* */
/******************************************************************************/
/* ************************************************************************** */
/* */
/* OpenSSL implementation of XrdCryptoX509Crl */
/* */
/* ************************************************************************** */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include "XrdCrypto/XrdCryptosslRSA.hh"
#include "XrdCrypto/XrdCryptosslX509Crl.hh"
#include "XrdCrypto/XrdCryptosslAux.hh"
#include "XrdCrypto/XrdCryptosslTrace.hh"
#include <openssl/bn.h>
#include <openssl/pem.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define X509_REVOKED_get0_revocationDate(x) (x)->revocationDate
#define X509_REVOKED_get0_serialNumber(x) (x)->serialNumber
#define X509_CRL_get0_lastUpdate X509_CRL_get_lastUpdate
#define X509_CRL_get0_nextUpdate X509_CRL_get_nextUpdate
#endif
//_____________________________________________________________________________
XrdCryptosslX509Crl::XrdCryptosslX509Crl(const char *cf, int opt)
: XrdCryptoX509Crl()
{
// Constructor certificate from file 'cf'.
EPNAME("X509Crl::XrdCryptosslX509Crl_file");
// Init private members
crl = 0; // The crl object
lastupdate = -1; // begin-validity time in secs since Epoch
nextupdate = -1; // end-validity time in secs since Epoch
issuer = ""; // issuer;
issuerhash = ""; // hash of issuer;
srcfile = ""; // source file;
nrevoked = 0; // number of revoked certificates
// Make sure file name is defined;
if (opt == 0) {
if (Init(cf) != 0) {
DEBUG("could not initialize the CRL from "<<cf);
return;
}
} else {
if (InitFromURI(cf, 0) != 0) {
DEBUG("could not initialize the CRL from URI"<<cf);
return;
}
}
}
//_____________________________________________________________________________
XrdCryptosslX509Crl::XrdCryptosslX509Crl(XrdCryptoX509 *cacert)
: XrdCryptoX509Crl()
{
// Constructor certificate from CA certificate 'cacert'. This constructor
// extracts the information about the location of the CRL cerificate from the
// CA certificate extension 'crlDistributionPoints', downloads the file and
// loads it in the cache
EPNAME("X509Crl::XrdCryptosslX509Crl_CA");
// Init private members
crl = 0; // The crl object
lastupdate = -1; // begin-validity time in secs since Epoch
nextupdate = -1; // end-validity time in secs since Epoch
issuer = ""; // issuer;
issuerhash = ""; // hash of issuer;
srcfile = ""; // source file;
nrevoked = 0; // number of revoked certificates
// The CA certificate must be defined
if (!cacert || cacert->type != XrdCryptoX509::kCA) {
DEBUG("the CA certificate is undefined or not CA! ("<<cacert<<")");
return;
}
// Get the extension
X509_EXTENSION *crlext = (X509_EXTENSION *) cacert->GetExtension("crlDistributionPoints");
if (!crlext) {
DEBUG("extension 'crlDistributionPoints' not found in the CA certificate");
return;
}
// Bio for exporting the extension
BIO *bext = BIO_new(BIO_s_mem());
ASN1_OBJECT *obj = X509_EXTENSION_get_object(crlext);
i2a_ASN1_OBJECT(bext, obj);
X509V3_EXT_print(bext, crlext, 0, 4);
// data length
char *cbio = 0;
int lbio = (int) BIO_get_mem_data(bext, &cbio);
char *buf = (char *) malloc(lbio+1);
// Read key from BIO to buf
memcpy(buf, cbio, lbio);
buf[lbio] = 0;
BIO_free(bext);
// Save it
XrdOucString uris(buf);
free(buf);
DEBUG("URI string: "<< uris);
XrdOucString uri;
int from = 0;
while ((from = uris.tokenize(uri, from, ' ')) != -1) {
if (uri.beginswith("URI:")) {
uri.replace("URI:","");
uri.replace("\n","");
if (InitFromURI(uri.c_str(), cacert->SubjectHash()) == 0) {
crluri = uri;
// We are done
break;
}
}
}
}
//_____________________________________________________________________________
XrdCryptosslX509Crl::~XrdCryptosslX509Crl()
{
// Destructor
// Cleanup CRL
if (crl)
X509_CRL_free(crl);
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::Init(const char *cf)
{
// Constructor certificate from file 'cf'.
// Return 0 on success, -1 on failure
EPNAME("X509Crl::Init");
// Make sure file name is defined;
if (!cf) {
DEBUG("file name undefined");
return -1;
}
// Make sure file exists;
struct stat st;
if (stat(cf, &st) != 0) {
if (errno == ENOENT) {
DEBUG("file "<<cf<<" does not exist - do nothing");
} else {
DEBUG("cannot stat file "<<cf<<" (errno: "<<errno<<")");
}
return -1;
}
//
// Open file in read mode
FILE *fc = fopen(cf, "r");
if (!fc) {
DEBUG("cannot open file "<<cf<<" (errno: "<<errno<<")");
return -1;
}
//
// Read the content:
if (!PEM_read_X509_CRL(fc, &crl, 0, 0)) {
DEBUG("Unable to load CRL from file");
return -1;
}
//
// Close the file
fclose(fc);
//
// Notify
DEBUG("CRL successfully loaded from "<< cf);
//
// Save source file name
srcfile = cf;
//
// Init some of the private members (the others upon need)
Issuer();
//
// Load into cache
LoadCache();
//
// Done
return 0;
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::InitFromURI(const char *uri, const char *hash)
{
// Initialize the CRL taking the file indicated by URI. Download and
// reformat the file first.
// Returns 0 on success, -1 on failure.
EPNAME("X509Crl::InitFromURI");
// Make sure file name is defined;
if (!uri) {
DEBUG("uri undefined");
return -1;
}
XrdOucString u(uri), h(hash);
if (h == "") {
int isl = u.rfind('/');
if (isl != STR_NPOS) h.assign(u, isl + 1);
}
if (h == "") h = "hashtmp";
// Create local output file path
XrdOucString outtmp(getenv("TMPDIR")), outpem;
if (outtmp.length() <= 0) outtmp = "/tmp";
if (!outtmp.endswith("/")) outtmp += "/";
outtmp += h;
outtmp += ".crltmp";
// Prepare 'wget' command
XrdOucString cmd("wget ");
cmd += uri;
cmd += " -O ";
cmd += outtmp;
// Execute 'wget'
DEBUG("executing ... "<<cmd);
if (system(cmd.c_str()) == -1) {
DEBUG("'system' could not fork to execute command '"<<cmd<<"'");
return -1;
}
struct stat st;
if (stat(outtmp.c_str(), &st) != 0) {
DEBUG("did not manage to get the CRL file from "<<uri);
return -1;
}
outpem = outtmp;
// Find out the file type
int needsopenssl = GetFileType(outtmp.c_str());
if (needsopenssl < 0) {
DEBUG("did not manage to coorectly parse "<<outtmp);
return -1;
}
if (needsopenssl > 0) {
// Put it in PEM format
outpem.replace(".crltmp", ".pem");
cmd = "openssl crl -inform DER -in ";
cmd += outtmp;
cmd += " -out ";
cmd += outpem;
cmd += " -text";
// Execute 'openssl crl'
DEBUG("executing ... "<<cmd);
if (system(cmd.c_str()) == -1) {
DEBUG("system: problem executing: "<<cmd);
return -1;
}
// Cleanup the temporary files
if (unlink(outtmp.c_str()) != 0) {
DEBUG("problems removing "<<outtmp);
}
}
// Make sure the file is there
if (stat(outpem.c_str(), &st) != 0) {
DEBUG("did not manage to change format from DER to PEM ("<<outpem<<")");
return -1;
}
// Now init from the new file
if (Init(outpem.c_str()) != 0) {
DEBUG("could not initialize the CRL from "<<outpem);
return -1;
}
// Cleanup the temporary files
unlink(outpem.c_str());
//
// Done
return 0;
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::GetFileType(const char *crlfn)
{
// Try to understand if file 'crlfn' is in DER (binary) or PEM (ASCII)
// format (assume that is not ASCII is a DER).
// Return 1 if not-PEM, 0 if PEM, -1 if any error occured
EPNAME("GetFileType");
if (!crlfn || strlen(crlfn) <= 0) {
PRINT("file name undefined!");
return -1;
}
char line[1024] = {0};
FILE *f = fopen(crlfn, "r");
if (!f) {
PRINT("could not open file "<<crlfn<<" - errno: "<<(int)errno);
return -1;
}
int rc = 1;
while (fgets(line, 1024, f)) {
// Skip empty lines at beginning
if (line[0] == '\n') continue;
// Analyse line for '-----BEGIN X509 CRL-----'
if (strstr(line, "BEGIN X509 CRL")) rc = 0;
break;
}
// Close the files
fclose(f);
// Done
return rc;
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::LoadCache()
{
// Load relevant info into the cache
// Return 0 if ok, -1 in case of error
EPNAME("LoadCache");
// The CRL must exists
if (!crl) {
DEBUG("CRL undefined");
return -1;
}
// Parse CRL
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
STACK_OF(X509_REVOKED *) rsk = X509_CRL_get_REVOKED(crl);
#else /* OPENSSL */
STACK_OF(X509_REVOKED *) *rsk = X509_CRL_get_REVOKED(crl);
#endif /* OPENSSL */
if (!rsk) {
DEBUG("could not get stack of revoked instances");
return -1;
}
// Number of revocations
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
nrevoked = sk_X509_REVOKED_num(rsk);
#else /* OPENSSL */
nrevoked = sk_num(rsk);
#endif /* OPENSSL */
DEBUG(nrevoked << "certificates have been revoked");
if (nrevoked <= 0) {
DEBUG("no valid certificate has been revoked - nothing to do");
return 0;
}
// Get serial numbers of revoked certificates
char *tagser = 0;
int i = 0;
for (; i < nrevoked; i++ ){
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
X509_REVOKED *rev = sk_X509_REVOKED_value(rsk,i);
#else /* OPENSSL */
X509_REVOKED *rev = (X509_REVOKED *)sk_value(rsk,i);
#endif /* OPENSSL */
if (rev) {
BIGNUM *bn = BN_new();
ASN1_INTEGER_to_BN(X509_REVOKED_get0_serialNumber(rev), bn);
tagser = BN_bn2hex(bn);
BN_free(bn);
TRACE(Dump, "certificate with serial number: "<<tagser<<
" has been revoked");
// Add to the cache
bool rdlock = false;
XrdSutCacheEntry *cent = cache.Get((const char *)tagser, rdlock);
if (!cent) {
DEBUG("problems getting entry in the cache");
return -1;
}
// Add revocation date
cent->mtime = XrdCryptosslASN1toUTC(X509_REVOKED_get0_revocationDate(rev));
// Set status
cent->mtime = kCE_ok;
// Release the string for the serial number
OPENSSL_free(tagser);
// Unlock the entry
cent->rwmtx.UnLock();
}
}
return 0;
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::LastUpdate()
{
// Time of last update
// If we do not have it already, try extraction
if (lastupdate < 0) {
// Make sure we have a CRL
if (crl)
// Extract UTC time in secs from Epoch
lastupdate = XrdCryptosslASN1toUTC(X509_CRL_get0_lastUpdate(crl));
}
// return what we have
return lastupdate;
}
//_____________________________________________________________________________
int XrdCryptosslX509Crl::NextUpdate()
{
// Time of next update
// If we do not have it already, try extraction
if (nextupdate < 0) {
// Make sure we have a CRL
if (crl)
// Extract UTC time in secs from Epoch
nextupdate = XrdCryptosslASN1toUTC(X509_CRL_get0_nextUpdate(crl));
}
// return what we have
return nextupdate;
}
//_____________________________________________________________________________
const char *XrdCryptosslX509Crl::Issuer()
{
// Return issuer name
EPNAME("X509Crl::Issuer");
// If we do not have it already, try extraction
if (issuer.length() <= 0) {
// Make sure we have a CRL
if (!crl) {
DEBUG("WARNING: no CRL available - cannot extract issuer name");
return (const char *)0;
}
// Extract issuer name
XrdCryptosslNameOneLine(X509_CRL_get_issuer(crl), issuer);
}
// return what we have
return (issuer.length() > 0) ? issuer.c_str() : (const char *)0;
}
//_____________________________________________________________________________
const char *XrdCryptosslX509Crl::IssuerHash(int alg)
{
// Return hash of issuer name
// Use default algorithm (X509_NAME_hash) for alg = 0, old algorithm
// (for v>=1.0.0) when alg = 1
EPNAME("X509::IssuerHash");
#if (OPENSSL_VERSION_NUMBER >= 0x10000000L && !defined(__APPLE__))
if (alg == 1) {
// md5 based
if (issueroldhash.length() <= 0) {
// Make sure we have a certificate
if (crl) {
char chash[30] = {0};
snprintf(chash, sizeof(chash),
"%08lx.0",X509_NAME_hash_old(X509_CRL_get_issuer(crl)));
issueroldhash = chash;
} else {
DEBUG("WARNING: no certificate available - cannot extract issuer hash (md5)");
}
}
// return what we have
return (issueroldhash.length() > 0) ? issueroldhash.c_str() : (const char *)0;
}
#else
if (alg == 1) { }
#endif
// If we do not have it already, try extraction
if (issuerhash.length() <= 0) {
// Make sure we have a certificate
if (crl) {
char chash[30] = {0};
snprintf(chash, sizeof(chash),
"%08lx.0",X509_NAME_hash(X509_CRL_get_issuer(crl)));
issuerhash = chash;
} else {
DEBUG("WARNING: no certificate available - cannot extract issuer hash (default)");
}
}
// return what we have
return (issuerhash.length() > 0) ? issuerhash.c_str() : (const char *)0;
}
//_____________________________________________________________________________
bool XrdCryptosslX509Crl::Verify(XrdCryptoX509 *ref)
{
// Verify certificate signature with pub key of ref cert
// We must have been initialized
if (!crl)
return 0;
// We must have something to check with
X509 *r = ref ? (X509 *)(ref->Opaque()) : 0;
EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
if (!rk)
return 0;
// Ok: we can verify
return (X509_CRL_verify(crl, rk) > 0);
}
//_____________________________________________________________________________
bool XrdCryptosslX509Crl::IsRevoked(int serialnumber, int when)
{
// Check if certificate with serialnumber is in the
// list of revocated certificates
EPNAME("IsRevoked");
// Reference time
int now = (when > 0) ? when : time(0);
// Warn if CRL should be updated
if (now > NextUpdate()) {
DEBUG("WARNING: CRL is expired: you should download the updated one");
}
// We must have something to check against
if (nrevoked <= 0) {
DEBUG("No certificate in the list");
return 0;
}
// Ok, build the tag
char tagser[20] = {0};
sprintf(tagser,"%x",serialnumber);
// Look into the cache
XrdSutCacheEntry *cent = cache.Get((const char *)tagser);
if (cent && cent->status == kCE_ok) {
// Check the revocation time
if (now > cent->mtime) {
DEBUG("certificate "<<tagser<<" has been revoked");
cent->rwmtx.UnLock();
return 1;
}
cent->rwmtx.UnLock();
}
// Certificate not revoked
return 0;
}
//_____________________________________________________________________________
bool XrdCryptosslX509Crl::IsRevoked(const char *sernum, int when)
{
// Check if certificate with 'sernum' is in the
// list of revocated certificates
EPNAME("IsRevoked");
// Reference time
int now = (when > 0) ? when : time(0);
// Warn if CRL should be updated
if (now > NextUpdate()) {
DEBUG("WARNING: CRL is expired: you should download the updated one");
}
// We must have something to check against
if (nrevoked <= 0) {
DEBUG("No certificate in the list");
return 0;
}
// Look into the cache
XrdSutCacheEntry *cent = cache.Get((const char *)sernum);
if (cent && cent->status == kCE_ok) {
// Check the revocation time
if (now > cent->mtime) {
DEBUG("certificate "<<sernum<<" has been revoked");
cent->rwmtx.UnLock();
return 1;
}
cent->rwmtx.UnLock();
}
// Certificate not revoked
return 0;
}
//_____________________________________________________________________________
void XrdCryptosslX509Crl::Dump()
{
// Dump content
EPNAME("X509Crl::Dump");
// Time strings
struct tm tst;
char stbeg[256] = {0};
time_t tbeg = LastUpdate();
localtime_r(&tbeg,&tst);
asctime_r(&tst,stbeg);
stbeg[strlen(stbeg)-1] = 0;
char stend[256] = {0};
time_t tend = NextUpdate();
localtime_r(&tend,&tst);
asctime_r(&tst,stend);
stend[strlen(stend)-1] = 0;
PRINT("+++++++++++++++ X509 CRL dump +++++++++++++++++++++++");
PRINT("+");
PRINT("+ File: "<<ParentFile());
PRINT("+");
PRINT("+ Issuer: "<<Issuer());
PRINT("+ Issuer hash: "<<IssuerHash(0));
PRINT("+");
if (IsExpired()) {
PRINT("+ Validity: (expired!)");
} else {
PRINT("+ Validity:");
}
PRINT("+ LastUpdate: "<<tbeg<<" UTC - "<<stbeg);
PRINT("+ NextUpdate: "<<tend<<" UTC - "<<stend);
PRINT("+");
PRINT("+ Number of revoked certificates: "<<nrevoked);
PRINT("+");
PRINT("+++++++++++++++++++++++++++++++++++++++++++++++++");
}