-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCryptoFactory.cc
501 lines (408 loc) · 16.6 KB
/
XrdCryptoFactory.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
/******************************************************************************/
/* */
/* X r d C r y p t o F a c t o r y . c c */
/* */
/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Gerri Ganis for 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. */
/******************************************************************************/
/* ************************************************************************** */
/* */
/* Abstract interface for a crypto factory */
/* Allows to plug-in modules based on different crypto implementation */
/* (OpenSSL, Botan, ...) */
/* */
/* ************************************************************************** */
#include <string.h>
#include <dlfcn.h>
#include "XrdCrypto/XrdCryptoAux.hh"
#include "XrdCrypto/XrdCryptoTrace.hh"
#include "XrdCrypto/XrdCryptoFactory.hh"
#include "XrdCrypto/XrdCryptolocalFactory.hh"
#include "XrdOuc/XrdOucHash.hh"
#include "XrdOuc/XrdOucPinLoader.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdVersion.hh"
//
// For error logging
static XrdSysError eDest(0,"cryptofactory_");
// We have always an instance of the simple RSA implementation
static XrdCryptolocalFactory localCryptoFactory;
//____________________________________________________________________________
XrdCryptoFactory::XrdCryptoFactory(const char *n, int id)
{
// Constructor (only called by derived classes).
if (n) {
int l = strlen(n);
l = (l > (MAXFACTORYNAMELEN - 1)) ? (MAXFACTORYNAMELEN - 1) : l;
strncpy(name,n,l);
name[l] = 0; // null terminated
}
fID = id;
}
//______________________________________________________________________________
void XrdCryptoFactory::SetTrace(kXR_int32)
{
// Set flags for tracing
ABSTRACTMETHOD("XrdCryptoFactory::SetTrace");
}
//______________________________________________________________________________
bool XrdCryptoFactory::operator==(const XrdCryptoFactory factory)
{
// Compare name of 'factory' to local name: return 1 if matches, 0 if not
if (!strcmp(factory.Name(),Name()))
return 1;
return 0;
}
//______________________________________________________________________________
XrdCryptoKDFunLen_t XrdCryptoFactory::KDFunLen()
{
// Return an instance of an implementation of a Key Der function length.
ABSTRACTMETHOD("XrdCryptoFactory::KDFunLen");
return 0;
}
//______________________________________________________________________________
XrdCryptoKDFun_t XrdCryptoFactory::KDFun()
{
// Return an instance of an implementation of a Key Derivation function.
ABSTRACTMETHOD("XrdCryptoFactory::KDFun");
return 0;
}
//______________________________________________________________________________
bool XrdCryptoFactory::SupportedCipher(const char *)
{
// Returns true id specified cipher is supported by the implementation
ABSTRACTMETHOD("XrdCryptoFactory::SupportedCipher");
return 0;
}
//______________________________________________________________________________
XrdCryptoCipher *XrdCryptoFactory::Cipher(const char *, int)
{
// Return an instance of an implementation of XrdCryptoCipher.
ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
return 0;
}
//______________________________________________________________________________
XrdCryptoCipher *XrdCryptoFactory::Cipher(const char *, int, const char *,
int, const char *)
{
// Return an instance of an implementation of XrdCryptoCipher.
ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
return 0;
}
//______________________________________________________________________________
XrdCryptoCipher *XrdCryptoFactory::Cipher(XrdSutBucket *)
{
// Return an instance of an implementation of XrdCryptoCipher.
ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
return 0;
}
//______________________________________________________________________________
XrdCryptoCipher *XrdCryptoFactory::Cipher(int, char *, int, const char *)
{
// Return an instance of an implementation of XrdCryptoCipher.
ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
return 0;
}
//______________________________________________________________________________
XrdCryptoCipher *XrdCryptoFactory::Cipher(const XrdCryptoCipher &)
{
// Return an instance of an implementation of XrdCryptoCipher.
ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
return 0;
}
//______________________________________________________________________________
bool XrdCryptoFactory::SupportedMsgDigest(const char *)
{
// Returns true id specified digest is supported by the implementation
ABSTRACTMETHOD("XrdCryptoFactory::SupportedMsgDigest");
return 0;
}
//______________________________________________________________________________
XrdCryptoMsgDigest *XrdCryptoFactory::MsgDigest(const char *)
{
// Return an instance of an implementation of XrdCryptoMsgDigest.
ABSTRACTMETHOD("XrdCryptoFactory::MsgDigest");
return 0;
}
//______________________________________________________________________________
XrdCryptoRSA *XrdCryptoFactory::RSA(int, int)
{
// Return an instance of an implementation of XrdCryptoRSA.
ABSTRACTMETHOD("XrdCryptoFactory::RSA");
return 0;
}
//______________________________________________________________________________
XrdCryptoRSA *XrdCryptoFactory::RSA(const char *, int)
{
// Return an instance of an implementation of XrdCryptoRSA.
ABSTRACTMETHOD("XrdCryptoFactory::RSA");
return 0;
}
//______________________________________________________________________________
XrdCryptoRSA *XrdCryptoFactory::RSA(const XrdCryptoRSA &)
{
// Return an instance of an implementation of XrdCryptoRSA.
ABSTRACTMETHOD("XrdCryptoFactory::RSA ("<<this<<")");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509 *XrdCryptoFactory::X509(const char *, const char *)
{
// Return an instance of an implementation of XrdCryptoX509.
ABSTRACTMETHOD("XrdCryptoFactory::X509");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509 *XrdCryptoFactory::X509(XrdSutBucket *)
{
// Init XrdCryptoX509 from a bucket
ABSTRACTMETHOD("XrdCryptoFactory::X509");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509Crl *XrdCryptoFactory::X509Crl(const char *, int)
{
// Return an instance of an implementation of XrdCryptoX509Crl.
ABSTRACTMETHOD("XrdCryptoFactory::X509Crl");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509Crl *XrdCryptoFactory::X509Crl(XrdCryptoX509 *)
{
// Return an instance of an implementation of XrdCryptoX509Crl.
ABSTRACTMETHOD("XrdCryptoFactory::X509Crl");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509Req *XrdCryptoFactory::X509Req(XrdSutBucket *)
{
// Return an instance of an implementation of XrdCryptoX509Req.
ABSTRACTMETHOD("XrdCryptoFactory::X509Req");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509VerifyCert_t XrdCryptoFactory::X509VerifyCert()
{
// Return an instance of an implementation of a verification
// function for X509 certificate.
ABSTRACTMETHOD("XrdCryptoFactory::X509VerifyCert");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509VerifyChain_t XrdCryptoFactory::X509VerifyChain()
{
// Return an instance of an implementation of a verification
// function for X509 certificate chains.
ABSTRACTMETHOD("XrdCryptoFactory::X509VerifyChain");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509ExportChain_t XrdCryptoFactory::X509ExportChain()
{
// Return an instance of an implementation of a function
// to export a X509 certificate chain.
ABSTRACTMETHOD("XrdCryptoFactory::X509ExportChain");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509ChainToFile_t XrdCryptoFactory::X509ChainToFile()
{
// Return an instance of an implementation of a function
// to dump a X509 certificate chain to a file.
ABSTRACTMETHOD("XrdCryptoFactory::X509ChainToFile");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509ParseFile_t XrdCryptoFactory::X509ParseFile()
{
// Return an instance of an implementation of a function
// to parse a file supposed to contain for X509 certificates.
ABSTRACTMETHOD("XrdCryptoFactory::X509ParseFile");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509ParseBucket_t XrdCryptoFactory::X509ParseBucket()
{
// Return an instance of an implementation of a function
// to parse a bucket supposed to contain for X509 certificates.
ABSTRACTMETHOD("XrdCryptoFactory::X509ParseBucket");
return 0;
}
//______________________________________________________________________________
XrdCryptoProxyCertInfo_t XrdCryptoFactory::ProxyCertInfo()
{
// Check if the proxyCertInfo extension exists
ABSTRACTMETHOD("XrdCryptoFactory::ProxyCertInfo");
return 0;
}
//______________________________________________________________________________
XrdCryptoSetPathLenConstraint_t XrdCryptoFactory::SetPathLenConstraint()
{
// Set the path length constraint
ABSTRACTMETHOD("XrdCryptoFactory::SetPathLenConstraint");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509CreateProxy_t XrdCryptoFactory::X509CreateProxy()
{
// Create a proxy certificate
ABSTRACTMETHOD("XrdCryptoFactory::X509CreateProxy");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509CreateProxyReq_t XrdCryptoFactory::X509CreateProxyReq()
{
// Create a proxy request
ABSTRACTMETHOD("XrdCryptoFactory::X509CreateProxyReq");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509SignProxyReq_t XrdCryptoFactory::X509SignProxyReq()
{
// Sign a proxy request
ABSTRACTMETHOD("XrdCryptoFactory::X509SignProxyReq");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509CheckProxy3_t XrdCryptoFactory::X509CheckProxy3()
{
// Check consistency of a GSI 3 compliant proxy
ABSTRACTMETHOD("XrdCryptoFactory::X509CheckProxy3");
return 0;
}
//______________________________________________________________________________
XrdCryptoX509GetVOMSAttr_t XrdCryptoFactory::X509GetVOMSAttr()
{
// Get VOMS attributes, if any
ABSTRACTMETHOD("XrdCryptoFactory::X509GetVOMSAttr");
return 0;
}
/* ************************************************************************** */
/* */
/* G e t C r y p t o F a c t o r y */
/* */
/* ************************************************************************** */
//
// Structure for the local record
typedef struct {
XrdCryptoFactory *factory;
char factoryname[MAXFACTORYNAMELEN];
bool status;
} FactoryEntry;
//____________________________________________________________________________
XrdCryptoFactory *XrdCryptoFactory::GetCryptoFactory(const char *factoryid)
{
// Static method to load/locate the crypto factory named factoryid
static XrdVERSIONINFODEF(myVer,cryptoloader,XrdVNUMBER,XrdVERSION);
static FactoryEntry *factorylist = 0;
static int factorynum = 0;
static XrdOucHash<XrdOucPinLoader> plugins;
XrdCryptoFactory *(*efact)();
XrdCryptoFactory *factory;
char factobjname[80], libfn[80];
EPNAME("Factory::GetCryptoFactory");
//
// The id must be defined
if (!factoryid || !strlen(factoryid)) {
PRINT("crypto factory ID ("<<factoryid<<") undefined");
return 0;
}
//
// If the local simple implementation is required return the related pointer
if (!strcmp(factoryid,"local")) {
PRINT("local crypto factory requested");
return &localCryptoFactory;
}
//
// Check if already loaded
if (factorynum) {
int i = 0;
for ( ; i < factorynum; i++ ) {
if (!strcmp(factoryid,factorylist[i].factoryname)) {
if (factorylist[i].status) {
DEBUG(factoryid <<" crypto factory object already loaded ("
<< factorylist[i].factory << ")");
return factorylist[i].factory;
} else {
DEBUG("previous attempt to load crypto factory "
<<factoryid<<" failed - do nothing");
return 0;
}
}
}
}
//
// Create new entry for this factory in the local record
FactoryEntry *newfactorylist = new FactoryEntry[factorynum+1];
if (newfactorylist) {
int i = 0;
for ( ; i < factorynum; i++ ) {
newfactorylist[i].factory = factorylist[i].factory;
newfactorylist[i].status = factorylist[i].status;
strcpy(newfactorylist[i].factoryname,factorylist[i].factoryname);
}
newfactorylist[i].factory = 0;
newfactorylist[i].status = 0;
strcpy(newfactorylist[i].factoryname,factoryid);
// Destroy previous vector
if (factorylist) delete[] factorylist;
// Update local list
factorylist = newfactorylist;
factorynum++;
} else
PRINT("cannot create local record of loaded crypto factories");
//
// Try loading: name of routine to load
sprintf(factobjname, "XrdCrypto%sFactoryObject", factoryid);
// Create or attach to the plug-in instance
XrdOucPinLoader *plug = plugins.Find(factoryid);
if (!plug) {
// Create one and add it to the list
snprintf(libfn, sizeof(libfn)-1, "libXrdCrypto%s.so", factoryid);
libfn[sizeof(libfn)-1] = '\0';
plug = new XrdOucPinLoader(&myVer, "cryptolib", libfn);
plugins.Add(factoryid, plug);
}
if (!plug) {
PRINT("problems opening shared library " << libfn);
return 0;
}
DEBUG("shared library '" << libfn << "' loaded");
// Get the function
if (!(efact = (XrdCryptoFactory *(*)()) plug->Resolve(factobjname))) {
PRINT(plug->LastMsg());
PRINT("problems finding crypto factory object creator " << factobjname);
return 0;
}
//
// Get the factory object
if (!(factory = (*efact)())) {
PRINT("problems creating crypto factory object");
return 0;
}
//
// Update local record
factorylist[factorynum-1].factory = factory;
factorylist[factorynum-1].status = 1;
return factory;
}