XrdCryptosslFactory.cc

Go to the documentation of this file.
00001 // $Id: XrdCryptosslFactory.cc 30949 2009-11-02 16:37:58Z ganis $
00002 
00003 const char *XrdCryptosslFactoryCVSID = "$Id: XrdCryptosslFactory.cc 30949 2009-11-02 16:37:58Z ganis $";
00004 /******************************************************************************/
00005 /*                                                                            */
00006 /*            X r d C r y p t o S s l 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 /* Implementation of the OpenSSL crypto factory                               */
00017 /*                                                                            */
00018 /* ************************************************************************** */
00019 
00020 #include <XrdCrypto/XrdCryptosslFactory.hh>
00021 #include <XrdCrypto/XrdCryptosslAux.hh>
00022 #include <XrdCrypto/XrdCryptosslCipher.hh>
00023 #include <XrdCrypto/XrdCryptosslMsgDigest.hh>
00024 #include <XrdCrypto/XrdCryptosslRSA.hh>
00025 #include <XrdCrypto/XrdCryptosslX509.hh>
00026 #include <XrdCrypto/XrdCryptosslX509Crl.hh>
00027 #include <XrdCrypto/XrdCryptosslX509Req.hh>
00028 
00029 #include <XrdSys/XrdSysLogger.hh>
00030 #include <XrdSys/XrdSysError.hh>
00031 #include <XrdSut/XrdSutRndm.hh>
00032 #include <XrdCrypto/XrdCryptosslTrace.hh>
00033 
00034 #include <openssl/rand.h>
00035 #include <openssl/ssl.h>
00036 
00037 //
00038 // For error logging and tracing
00039 static XrdSysLogger Logger;
00040 static XrdSysError eDest(0,"cryptossl_");
00041 XrdOucTrace *sslTrace = 0;
00042 
00043 //______________________________________________________________________________
00044 XrdCryptosslFactory::XrdCryptosslFactory() :
00045                      XrdCryptoFactory("ssl",XrdCryptosslFactoryID)
00046 {
00047    // Constructor: init the needed components of the OpenSSL library
00048 
00049    // Init SSL ...
00050    SSL_library_init();
00051    //  ... and its error strings
00052    SSL_load_error_strings();
00053    // Load Ciphers
00054    OpenSSL_add_all_ciphers();
00055    // Load Msg Digests
00056    OpenSSL_add_all_digests();
00057 
00058    // Init Random machinery
00059    int klen = 32;
00060    char *ktmp = XrdSutRndm::GetBuffer(klen);
00061    if (ktmp) {
00062       // Feed the random engine
00063       RAND_seed(ktmp,klen);
00064       delete[] ktmp;
00065    }
00066 }
00067 
00068 //______________________________________________________________________________
00069 void XrdCryptosslFactory::SetTrace(kXR_int32 trace)
00070 {
00071    // Set trace flags according to 'trace'
00072 
00073    //
00074    // Initiate error logging and tracing
00075    eDest.logger(&Logger);
00076    if (!sslTrace)
00077       sslTrace = new XrdOucTrace(&eDest);
00078    if (sslTrace) {
00079       // Set debug mask
00080       sslTrace->What = 0;
00081       // Low level only
00082       if ((trace & sslTRACE_Notify))
00083          sslTrace->What |= sslTRACE_Notify;
00084       // Medium level
00085       if ((trace & sslTRACE_Debug))
00086          sslTrace->What |= (sslTRACE_Notify | sslTRACE_Debug);
00087       // High level
00088       if ((trace & sslTRACE_Dump))
00089          sslTrace->What |= sslTRACE_ALL;
00090    }
00091 }
00092 
00093 //______________________________________________________________________________
00094 XrdCryptoKDFunLen_t XrdCryptosslFactory::KDFunLen()
00095 {
00096    // Return an instance of an implementation of the PBKDF2 fun length.
00097 
00098    return &XrdCryptosslKDFunLen;
00099 }
00100 
00101 //______________________________________________________________________________
00102 XrdCryptoKDFun_t XrdCryptosslFactory::KDFun()
00103 {
00104    // Return an instance of an implementation of the PBKDF2 function.
00105 
00106    return &XrdCryptosslKDFun;
00107 }
00108 
00109 //______________________________________________________________________________
00110 bool XrdCryptosslFactory::SupportedCipher(const char *t)
00111 {
00112    // Returns true if specified cipher is supported
00113 
00114    return XrdCryptosslCipher::IsSupported(t);
00115 }
00116 
00117 //______________________________________________________________________________
00118 XrdCryptoCipher *XrdCryptosslFactory::Cipher(const char *t, int l)
00119 {
00120    // Return an instance of a ssl implementation of XrdCryptoCipher.
00121 
00122    XrdCryptoCipher *cip = new XrdCryptosslCipher(t,l);
00123    if (cip) {
00124       if (cip->IsValid())
00125          return cip;
00126       else
00127          delete cip;
00128    }
00129    return (XrdCryptoCipher *)0;
00130 }
00131 
00132 //______________________________________________________________________________
00133 XrdCryptoCipher *XrdCryptosslFactory::Cipher(const char *t, 
00134                                              int l, const char *k, 
00135                                              int liv, const char *iv)
00136 {
00137    // Return an instance of a ssl implementation of XrdCryptoCipher.
00138 
00139    XrdCryptoCipher *cip = new XrdCryptosslCipher(t,l,k,liv,iv);
00140    if (cip) {
00141       if (cip->IsValid())
00142          return cip;
00143       else
00144          delete cip;
00145    }
00146    return (XrdCryptoCipher *)0;
00147 }
00148 
00149 //______________________________________________________________________________
00150 XrdCryptoCipher *XrdCryptosslFactory::Cipher(XrdSutBucket *b)
00151 {
00152    // Return an instance of a Local implementation of XrdCryptoCipher.
00153 
00154    XrdCryptoCipher *cip = new XrdCryptosslCipher(b);
00155    if (cip) {
00156       if (cip->IsValid())
00157          return cip;
00158       else
00159          delete cip;
00160    }
00161    return (XrdCryptoCipher *)0;
00162 }
00163 
00164 //______________________________________________________________________________
00165 XrdCryptoCipher *XrdCryptosslFactory::Cipher(int b, char *p,
00166                                              int l, const char *t)
00167 {
00168    // Return an instance of a Ssl implementation of XrdCryptoCipher.
00169 
00170    XrdCryptoCipher *cip = new XrdCryptosslCipher(b,p,l,t);
00171    if (cip) {
00172       if (cip->IsValid())
00173          return cip;
00174       else
00175          delete cip;
00176    }
00177    return (XrdCryptoCipher *)0;
00178 }
00179 
00180 //______________________________________________________________________________
00181 XrdCryptoCipher *XrdCryptosslFactory::Cipher(const XrdCryptoCipher &c)
00182 {
00183    // Return an instance of a Ssl implementation of XrdCryptoCipher.
00184 
00185    XrdCryptoCipher *cip = new XrdCryptosslCipher(*((XrdCryptosslCipher *)&c));
00186    if (cip) {
00187       if (cip->IsValid())
00188          return cip;
00189       else
00190          delete cip;
00191    }
00192    return (XrdCryptoCipher *)0;
00193 }
00194 
00195 //______________________________________________________________________________
00196 bool XrdCryptosslFactory::SupportedMsgDigest(const char *dgst)
00197 {
00198    // Returns true if specified digest is supported
00199 
00200    return XrdCryptosslMsgDigest::IsSupported(dgst);
00201 }
00202 
00203 //______________________________________________________________________________
00204 XrdCryptoMsgDigest *XrdCryptosslFactory::MsgDigest(const char *dgst)
00205 {
00206    // Return an instance of a ssl implementation of XrdCryptoMsgDigest.
00207 
00208    XrdCryptoMsgDigest *md = new XrdCryptosslMsgDigest(dgst);
00209    if (md) {
00210       if (md->IsValid())
00211          return md;
00212       else
00213          delete md;
00214    }
00215    return (XrdCryptoMsgDigest *)0;
00216 }
00217 
00218 //______________________________________________________________________________
00219 XrdCryptoRSA *XrdCryptosslFactory::RSA(int bits, int exp)
00220 {
00221    // Return an instance of a ssl implementation of XrdCryptoRSA.
00222 
00223    XrdCryptoRSA *rsa = new XrdCryptosslRSA(bits,exp);
00224    if (rsa) {
00225       if (rsa->IsValid())
00226          return rsa;
00227       else
00228          delete rsa;
00229    }
00230    return (XrdCryptoRSA *)0;
00231 }
00232 
00233 //______________________________________________________________________________
00234 XrdCryptoRSA *XrdCryptosslFactory::RSA(const char *pub, int lpub)
00235 {
00236    // Return an instance of a ssl implementation of XrdCryptoRSA.
00237 
00238    XrdCryptoRSA *rsa = new XrdCryptosslRSA(pub,lpub);
00239    if (rsa) {
00240       if (rsa->IsValid())
00241          return rsa;
00242       else
00243          delete rsa;
00244    }
00245    return (XrdCryptoRSA *)0;
00246 }
00247 
00248 //______________________________________________________________________________
00249 XrdCryptoRSA *XrdCryptosslFactory::RSA(const XrdCryptoRSA &r)
00250 {
00251    // Return an instance of a Ssl implementation of XrdCryptoRSA.
00252 
00253    XrdCryptoRSA *rsa = new XrdCryptosslRSA(*((XrdCryptosslRSA *)&r));
00254    if (rsa) {
00255       if (rsa->IsValid())
00256          return rsa;
00257       else
00258          delete rsa;
00259    }
00260    return (XrdCryptoRSA *)0;
00261 }
00262 
00263 //______________________________________________________________________________
00264 XrdCryptoX509 *XrdCryptosslFactory::X509(const char *cf, const char *kf)
00265 {
00266    // Return an instance of a ssl implementation of XrdCryptoX509.
00267 
00268    XrdCryptoX509 *x509 = new XrdCryptosslX509(cf, kf);
00269    if (x509) {
00270       if (x509->Opaque())
00271          return x509;
00272       else
00273          delete x509;
00274    }
00275    return (XrdCryptoX509 *)0;
00276 }
00277 
00278 //______________________________________________________________________________
00279 XrdCryptoX509 *XrdCryptosslFactory::X509(XrdSutBucket *b)
00280 {
00281    // Return an instance of a ssl implementation of XrdCryptoX509.
00282 
00283    XrdCryptoX509 *x509 = new XrdCryptosslX509(b);
00284    if (x509) {
00285       if (x509->Opaque())
00286          return x509;
00287       else
00288          delete x509;
00289    }
00290    return (XrdCryptoX509 *)0;
00291 }
00292 
00293 //______________________________________________________________________________
00294 XrdCryptoX509Crl *XrdCryptosslFactory::X509Crl(const char *cf, int opt)
00295 {
00296    // Return an instance of a ssl implementation of XrdCryptoX509Crl.
00297 
00298    XrdCryptoX509Crl *x509Crl = new XrdCryptosslX509Crl(cf, opt);
00299    if (x509Crl) {
00300       if (x509Crl->Opaque())
00301          return x509Crl;
00302       else
00303          delete x509Crl;
00304    }
00305    return (XrdCryptoX509Crl *)0;
00306 }
00307 
00308 //______________________________________________________________________________
00309 XrdCryptoX509Crl *XrdCryptosslFactory::X509Crl(XrdCryptoX509 *ca)
00310 {
00311    // Return an instance of a ssl implementation of XrdCryptoX509Crl.
00312 
00313    XrdCryptoX509Crl *x509Crl = new XrdCryptosslX509Crl(ca);
00314    if (x509Crl) {
00315       if (x509Crl->Opaque())
00316          return x509Crl;
00317       else
00318          delete x509Crl;
00319    }
00320    return (XrdCryptoX509Crl *)0;
00321 }
00322 
00323 //______________________________________________________________________________
00324 XrdCryptoX509Req *XrdCryptosslFactory::X509Req(XrdSutBucket *b)
00325 {
00326    // Return an instance of a ssl implementation of XrdCryptoX509Crl.
00327 
00328    XrdCryptoX509Req *x509Req = new XrdCryptosslX509Req(b);
00329    if (x509Req) {
00330       if (x509Req->Opaque())
00331          return x509Req;
00332       else
00333          delete x509Req;
00334    }
00335    return (XrdCryptoX509Req *)0;
00336 }
00337 
00338 //______________________________________________________________________________
00339 XrdCryptoX509VerifyCert_t XrdCryptosslFactory::X509VerifyCert()
00340 {
00341    // Return hook to the OpenSSL implementation of the verification
00342    // function for X509 certificate.
00343 
00344    return &XrdCryptosslX509VerifyCert;
00345 }
00346 
00347 //______________________________________________________________________________
00348 XrdCryptoX509VerifyChain_t XrdCryptosslFactory::X509VerifyChain()
00349 {
00350    // Return hook to the OpenSSL implementation of the verification
00351    // function for X509 certificate chains.
00352 
00353    return &XrdCryptosslX509VerifyChain;
00354 }
00355 
00356 //______________________________________________________________________________
00357 XrdCryptoX509ExportChain_t XrdCryptosslFactory::X509ExportChain()
00358 {
00359    // Return an instance of an implementation of a function
00360    // to export a X509 certificate chain.
00361 
00362    return &XrdCryptosslX509ExportChain;
00363 }
00364 
00365 //______________________________________________________________________________
00366 XrdCryptoX509ChainToFile_t XrdCryptosslFactory::X509ChainToFile()
00367 {
00368    // Return an instance of an implementation of a function
00369    // to dump a X509 certificate chain to a file.
00370 
00371    return &XrdCryptosslX509ChainToFile;
00372 }
00373 
00374 //______________________________________________________________________________
00375 XrdCryptoX509ParseFile_t XrdCryptosslFactory::X509ParseFile()
00376 {
00377    // Return an instance of an implementation of a function
00378    // to parse a file supposed to contain for X509 certificates.
00379 
00380    return &XrdCryptosslX509ParseFile;
00381 }
00382 
00383 //______________________________________________________________________________
00384 XrdCryptoX509ParseBucket_t XrdCryptosslFactory::X509ParseBucket()
00385 {
00386    // Return an instance of an implementation of a function
00387    // to parse a file supposed to contain for X509 certificates.
00388 
00389    return &XrdCryptosslX509ParseBucket;
00390 }
00391 
00392 /******************************************************************************/
00393 /*            X r d C r y p t o S s l F a c t o r y O b j e c t               */
00394 /******************************************************************************/
00395 extern "C" {
00396 XrdCryptoFactory *XrdCryptosslFactoryObject()
00397 {
00398    // Return a pointer to the instantiated Ssl factory singleton.
00399    // Instantiate the singleton on the first call.
00400 
00401    static XrdCryptosslFactory SslCryptoFactory;
00402 
00403    return &SslCryptoFactory;
00404 }}

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