XrdCryptoFactory.cc

Go to the documentation of this file.
00001 // $Id: XrdCryptoFactory.cc 38011 2011-02-08 18:35:57Z ganis $
00002 
00003 const char *XrdCryptoFactoryCVSID = "$Id: XrdCryptoFactory.cc 38011 2011-02-08 18:35:57Z ganis $";
00004 /******************************************************************************/
00005 /*                                                                            */
00006 /*                  X r d C r y p t o F a c t o r y . c c                     */
00007 /*                                                                            */
00008 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00009 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00010 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00011 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00012 /******************************************************************************/
00013 
00014 /* ************************************************************************** */
00015 /*                                                                            */
00016 /* Abstract interface for a crypto factory                                    */
00017 /* Allows to plug-in modules based on different crypto implementation         */
00018 /* (OpenSSL, Botan, ...)                                                      */
00019 /*                                                                            */
00020 /* ************************************************************************** */
00021 #include <string.h>
00022 #include <dlfcn.h>
00023 
00024 #include <XrdCrypto/XrdCryptoAux.hh>
00025 #include <XrdCrypto/XrdCryptoTrace.hh>
00026 #include <XrdCrypto/XrdCryptoFactory.hh>
00027 #include <XrdCrypto/XrdCryptolocalFactory.hh>
00028 
00029 #ifndef LT_MODULE_EXT
00030 #define LT_MODULE_EXT ".so"
00031 #endif
00032 
00033 // We have always an instance of the simple RSA implementation
00034 static XrdCryptolocalFactory localCryptoFactory;
00035 
00036 //____________________________________________________________________________
00037 XrdCryptoFactory::XrdCryptoFactory(const char *n, int id)
00038 {
00039    // Constructor (only called by derived classes).
00040 
00041    if (n) {
00042       int l = strlen(n);
00043       l = (l > (MAXFACTORYNAMELEN - 1)) ? (MAXFACTORYNAMELEN - 1) : l;  
00044       strncpy(name,n,l);
00045       name[l] = 0;  // null terminated
00046    }
00047    fID = id;
00048 }
00049 
00050 //______________________________________________________________________________
00051 void XrdCryptoFactory::SetTrace(kXR_int32)
00052 {
00053    // Set flags for tracing
00054 
00055    ABSTRACTMETHOD("XrdCryptoFactory::SetTrace");
00056 }
00057 
00058 //______________________________________________________________________________
00059 bool XrdCryptoFactory::operator==(const XrdCryptoFactory factory)
00060 {
00061    // Compare name of 'factory' to local name: return 1 if matches, 0 if not
00062 
00063    if (!strcmp(factory.Name(),Name()))
00064       return 1;
00065    return 0;
00066 }
00067 
00068 //______________________________________________________________________________
00069 XrdCryptoKDFunLen_t XrdCryptoFactory::KDFunLen()
00070 {
00071    // Return an instance of an implementation of a Key Der function length.
00072 
00073    ABSTRACTMETHOD("XrdCryptoFactory::KDFunLen");
00074    return 0;
00075 }
00076 
00077 //______________________________________________________________________________
00078 XrdCryptoKDFun_t XrdCryptoFactory::KDFun()
00079 {
00080    // Return an instance of an implementation of a Key Derivation function.
00081 
00082    ABSTRACTMETHOD("XrdCryptoFactory::KDFun");
00083    return 0;
00084 }
00085 
00086 //______________________________________________________________________________
00087 bool XrdCryptoFactory::SupportedCipher(const char *)
00088 {
00089    // Returns true id specified cipher is supported by the implementation
00090 
00091    ABSTRACTMETHOD("XrdCryptoFactory::SupportedCipher");
00092    return 0;
00093 }
00094 
00095 //______________________________________________________________________________
00096 XrdCryptoCipher *XrdCryptoFactory::Cipher(const char *, int)
00097 {
00098    // Return an instance of an implementation of XrdCryptoCipher.
00099 
00100    ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
00101    return 0;
00102 }
00103 
00104 //______________________________________________________________________________
00105 XrdCryptoCipher *XrdCryptoFactory::Cipher(const char *, int, const char *, 
00106                                           int, const char *)
00107 {
00108    // Return an instance of an implementation of XrdCryptoCipher.
00109 
00110    ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
00111    return 0;
00112 }
00113 
00114 //______________________________________________________________________________
00115 XrdCryptoCipher *XrdCryptoFactory::Cipher(XrdSutBucket *)
00116 {
00117    // Return an instance of an implementation of XrdCryptoCipher.
00118 
00119    ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
00120    return 0;
00121 }
00122 
00123 //______________________________________________________________________________
00124 XrdCryptoCipher *XrdCryptoFactory::Cipher(int, char *, int, const char *)
00125 {
00126    // Return an instance of an implementation of XrdCryptoCipher.
00127 
00128    ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
00129    return 0;
00130 }
00131 
00132 //______________________________________________________________________________
00133 XrdCryptoCipher *XrdCryptoFactory::Cipher(const XrdCryptoCipher &)
00134 {
00135    // Return an instance of an implementation of XrdCryptoCipher.
00136 
00137    ABSTRACTMETHOD("XrdCryptoFactory::Cipher");
00138    return 0;
00139 }
00140 
00141 //______________________________________________________________________________
00142 bool XrdCryptoFactory::SupportedMsgDigest(const char *)
00143 {
00144    // Returns true id specified digest is supported by the implementation
00145 
00146    ABSTRACTMETHOD("XrdCryptoFactory::SupportedMsgDigest");
00147    return 0;
00148 }
00149 
00150 //______________________________________________________________________________
00151 XrdCryptoMsgDigest *XrdCryptoFactory::MsgDigest(const char *)
00152 {
00153    // Return an instance of an implementation of XrdCryptoMsgDigest.
00154 
00155    ABSTRACTMETHOD("XrdCryptoFactory::MsgDigest");
00156    return 0;
00157 }
00158 
00159 //______________________________________________________________________________
00160 XrdCryptoRSA *XrdCryptoFactory::RSA(int, int)
00161 {
00162    // Return an instance of an implementation of XrdCryptoRSA.
00163 
00164    ABSTRACTMETHOD("XrdCryptoFactory::RSA");
00165    return 0;
00166 }
00167 
00168 //______________________________________________________________________________
00169 XrdCryptoRSA *XrdCryptoFactory::RSA(const char *, int)
00170 {
00171    // Return an instance of an implementation of XrdCryptoRSA.
00172 
00173    ABSTRACTMETHOD("XrdCryptoFactory::RSA");
00174    return 0;
00175 
00176 }
00177 
00178 //______________________________________________________________________________
00179 XrdCryptoRSA *XrdCryptoFactory::RSA(const XrdCryptoRSA &)
00180 {
00181    // Return an instance of an implementation of XrdCryptoRSA.
00182 
00183    ABSTRACTMETHOD("XrdCryptoFactory::RSA ("<<this<<")");
00184    return 0;
00185 }
00186 
00187 //______________________________________________________________________________
00188 XrdCryptoX509 *XrdCryptoFactory::X509(const char *, const char *)
00189 {
00190    // Return an instance of an implementation of XrdCryptoX509.
00191 
00192    ABSTRACTMETHOD("XrdCryptoFactory::X509");
00193    return 0;
00194 }
00195 
00196 //______________________________________________________________________________
00197 XrdCryptoX509 *XrdCryptoFactory::X509(XrdSutBucket *)
00198 {
00199    // Init XrdCryptoX509 from a bucket
00200 
00201    ABSTRACTMETHOD("XrdCryptoFactory::X509");
00202    return 0;
00203 }
00204 
00205 //______________________________________________________________________________
00206 XrdCryptoX509Crl *XrdCryptoFactory::X509Crl(const char *, int)
00207 {
00208    // Return an instance of an implementation of XrdCryptoX509Crl.
00209 
00210    ABSTRACTMETHOD("XrdCryptoFactory::X509Crl");
00211    return 0;
00212 }
00213 
00214 //______________________________________________________________________________
00215 XrdCryptoX509Crl *XrdCryptoFactory::X509Crl(XrdCryptoX509 *)
00216 {
00217    // Return an instance of an implementation of XrdCryptoX509Crl.
00218 
00219    ABSTRACTMETHOD("XrdCryptoFactory::X509Crl");
00220    return 0;
00221 }
00222 
00223 //______________________________________________________________________________
00224 XrdCryptoX509Req *XrdCryptoFactory::X509Req(XrdSutBucket *)
00225 {
00226    // Return an instance of an implementation of XrdCryptoX509Req.
00227 
00228    ABSTRACTMETHOD("XrdCryptoFactory::X509Req");
00229    return 0;
00230 }
00231 
00232 //______________________________________________________________________________
00233 XrdCryptoX509VerifyCert_t XrdCryptoFactory::X509VerifyCert()
00234 {
00235    // Return an instance of an implementation of a verification
00236    // function for X509 certificate.
00237 
00238    ABSTRACTMETHOD("XrdCryptoFactory::X509VerifyCert");
00239    return 0;
00240 }
00241 
00242 //______________________________________________________________________________
00243 XrdCryptoX509VerifyChain_t XrdCryptoFactory::X509VerifyChain()
00244 {
00245    // Return an instance of an implementation of a verification
00246    // function for X509 certificate chains.
00247 
00248    ABSTRACTMETHOD("XrdCryptoFactory::X509VerifyChain");
00249    return 0;
00250 }
00251 
00252 //______________________________________________________________________________
00253 XrdCryptoX509ExportChain_t XrdCryptoFactory::X509ExportChain()
00254 {
00255    // Return an instance of an implementation of a function
00256    // to export a X509 certificate chain.
00257 
00258    ABSTRACTMETHOD("XrdCryptoFactory::X509ExportChain");
00259    return 0;
00260 }
00261 
00262 //______________________________________________________________________________
00263 XrdCryptoX509ChainToFile_t XrdCryptoFactory::X509ChainToFile()
00264 {
00265    // Return an instance of an implementation of a function
00266    // to dump a X509 certificate chain to a file.
00267 
00268    ABSTRACTMETHOD("XrdCryptoFactory::X509ChainToFile");
00269    return 0;
00270 }
00271 
00272 //______________________________________________________________________________
00273 XrdCryptoX509ParseFile_t XrdCryptoFactory::X509ParseFile()
00274 {
00275    // Return an instance of an implementation of a function
00276    // to parse a file supposed to contain for X509 certificates.
00277 
00278    ABSTRACTMETHOD("XrdCryptoFactory::X509ParseFile");
00279    return 0;
00280 }
00281 
00282 //______________________________________________________________________________
00283 XrdCryptoX509ParseBucket_t XrdCryptoFactory::X509ParseBucket()
00284 {
00285    // Return an instance of an implementation of a function
00286    // to parse a bucket supposed to contain for X509 certificates.
00287 
00288    ABSTRACTMETHOD("XrdCryptoFactory::X509ParseBucket");
00289    return 0;
00290 }
00291 
00292 /* ************************************************************************** */
00293 /*                                                                            */
00294 /*                    G e t C r y p t o F a c t o r y                         */
00295 /*                                                                            */
00296 /* ************************************************************************** */
00297 
00298 //
00299 // Structure for the local record
00300 typedef struct {
00301    XrdCryptoFactory *factory;
00302    char              factoryname[MAXFACTORYNAMELEN];
00303    bool              status; 
00304 } FactoryEntry;
00305 
00306 //____________________________________________________________________________
00307 XrdCryptoFactory *XrdCryptoFactory::GetCryptoFactory(const char *factoryid)
00308 {
00309    // Static method to load/locate the crypto factory named factoryid
00310  
00311    static FactoryEntry  *factorylist = 0;
00312    static int            factorynum = 0;
00313    XrdCryptoFactory     *(*efact)();
00314    void *libhandle;
00315    XrdCryptoFactory *factory;
00316    char factobjname[80], libfn[80], *libloc;
00317    EPNAME("Factory::GetCryptoFactory");
00318 
00319    //
00320    // The id must be defined
00321    if (!factoryid || !strlen(factoryid)) {
00322       DEBUG("crypto factory ID ("<<factoryid<<") undefined");
00323       return 0;
00324    }
00325 
00326    //
00327    // If the local simple implementation is required return the related pointer
00328    if (!strcmp(factoryid,"local")) {
00329       DEBUG("local crypto factory requested");
00330       return &localCryptoFactory;
00331    }
00332 
00333    // 
00334    // Check if already loaded
00335    if (factorynum) {
00336       int i = 0;
00337       for ( ; i < factorynum; i++ ) {
00338          if (!strcmp(factoryid,factorylist[i].factoryname)) {
00339             if (factorylist[i].status) {
00340                DEBUG(factoryid <<" crypto factory object already loaded ("
00341                                << factorylist[i].factory << ")");
00342                return factorylist[i].factory;
00343             } else {
00344                DEBUG("previous attempt to load crypto factory "
00345                      <<factoryid<<" failed - do nothing");
00346                return 0;
00347             }
00348          }
00349       }
00350    }
00351 
00352    //
00353    // Create new entry for this factory in the local record
00354    FactoryEntry *newfactorylist = new FactoryEntry[factorynum+1];
00355    if (newfactorylist) {
00356       int i = 0;
00357       for ( ; i < factorynum; i++ ) {
00358          newfactorylist[i].factory = factorylist[i].factory;
00359          newfactorylist[i].status = factorylist[i].status;
00360          strcpy(newfactorylist[i].factoryname,factorylist[i].factoryname);
00361       }
00362       newfactorylist[i].factory = 0;
00363       newfactorylist[i].status = 0;
00364       strcpy(newfactorylist[i].factoryname,factoryid);
00365 
00366       // Destroy previous vector
00367       if (factorylist) delete[] factorylist;
00368 
00369       // Update local list
00370       factorylist = newfactorylist;
00371       factorynum++;
00372    } else
00373       DEBUG("cannot create local record of loaded crypto factories");
00374 
00375    //
00376    // Try loading: name of routine to load
00377    sprintf(factobjname, "XrdCrypto%sFactoryObject", factoryid);
00378 
00379    //
00380    // Form library name
00381    snprintf(libfn, sizeof(libfn)-1, "libXrdCrypto%s", LT_MODULE_EXT);
00382    libfn[sizeof(libfn)-1] = '\0';
00383 
00384    //
00385    // Determine path
00386    libloc = libfn;
00387    DEBUG("loading " <<factoryid <<" crypto factory object from " <<libloc);
00388 
00389    //
00390    // Try opening the crypto module
00391    if (!(libhandle = dlopen(libloc, RTLD_NOW))) {
00392       DEBUG("problems opening shared library " << libloc
00393              << "(error: "<< dlerror() << ")");
00394       return 0;
00395    }
00396 
00397    //
00398    // Get the factory object creator
00399    if (!(efact = (XrdCryptoFactory *(*)())dlsym(libhandle, factobjname))) {
00400 
00401       //
00402       // Try also specific library name
00403       snprintf(libfn, sizeof(libfn)-1, "libXrdCrypto%s%s", factoryid, LT_MODULE_EXT);
00404       libfn[sizeof(libfn)-1] = '\0';
00405       
00406       //
00407       // Determine path
00408       libloc = libfn;
00409       DEBUG("loading " <<factoryid <<" crypto factory object from " <<libloc);
00410       
00411       //
00412       // Try opening the crypto module
00413       if (!(libhandle = dlopen(libloc, RTLD_NOW))) {
00414          DEBUG("problems opening shared library " << libloc
00415                 << "(error: "<< dlerror() << ")");
00416          return 0;
00417       }
00418 
00419 
00420       //
00421       // Get the factory object creator
00422       if (!(efact = (XrdCryptoFactory *(*)())dlsym(libhandle, factobjname))) {
00423          DEBUG("problems finding crypto factory object creator " << factobjname);
00424          return 0;
00425       }
00426    }
00427 
00428    //
00429    // Get the factory object
00430    if (!(factory = (*efact)())) {
00431       DEBUG("problems creating crypto factory object");
00432       return 0;
00433    }
00434 
00435    //
00436    // Update local record
00437    factorylist[factorynum-1].factory = factory;
00438    factorylist[factorynum-1].status = 1;
00439 
00440    return factory;
00441 }

Generated on Tue Jul 5 14:46:33 2011 for ROOT_528-00b_version by  doxygen 1.5.1