XrdCryptolocalCipher.cc

Go to the documentation of this file.
00001 // $Id: XrdCryptolocalCipher.cc 30949 2009-11-02 16:37:58Z ganis $
00002 
00003 const char *XrdCryptolocalCipherCVSID = "$Id: XrdCryptolocalCipher.cc 30949 2009-11-02 16:37:58Z ganis $";
00004 /******************************************************************************/
00005 /*                                                                            */
00006 /*              X r d C r y p t o L o c a l C i p h e r . c c                 */
00007 /*                                                                            */
00008 /* (c) 2005 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 /* Local implementation of XrdCryptoCipher based on PC1                       */
00017 /*                                                                            */
00018 /* ************************************************************************** */
00019 #include <string.h>
00020 
00021 #include <XrdSut/XrdSutRndm.hh>
00022 #include <XrdCrypto/XrdCryptolocalCipher.hh>
00023 #include <XrdCrypto/PC1.hh>
00024 #include <XrdCrypto/PC3.hh>
00025 
00026 // ---------------------------------------------------------------------------//
00027 //
00028 // Cipher interface
00029 //
00030 // ---------------------------------------------------------------------------//
00031 
00032 //____________________________________________________________________________
00033 XrdCryptolocalCipher::XrdCryptolocalCipher(const char *t, int l)
00034 {
00035    // Main Constructor
00036    // Complete initialization of a cipher of type t and length l
00037    // The initialization vector is also created
00038    // Used to create ciphers
00039 
00040    valid = 0;
00041    bpub  = 0;
00042    bpriv = 0;
00043 
00044    // Check and set type
00045    int len = (l > 0 && l <= kPC1LENGTH) ? l : kPC1LENGTH ;
00046 
00047    // Generate and set a new key
00048    char *ktmp = XrdSutRndm::GetBuffer(len);
00049    if (ktmp) {
00050       // Store the key
00051       SetBuffer(len,ktmp);
00052       valid = 1;
00053 
00054       // Set also the key type (should be "PC1")
00055       if (!t || (t && !strcmp(t,"default")))
00056          SetType("PC1");
00057       else
00058          SetType(t);
00059    }
00060 }
00061 
00062 //____________________________________________________________________________
00063 XrdCryptolocalCipher::XrdCryptolocalCipher(const char *t, int l, const char *k)
00064 {
00065    // Constructor.
00066    // Initialize a cipher of type t and length l using the key at k
00067    // Used to import ciphers.
00068 
00069    valid = 0;
00070    bpub  = 0;
00071    bpriv = 0;
00072 
00073    // Check and set type
00074    int len = (l <= kPC1LENGTH) ? l : kPC1LENGTH ;
00075 
00076    if (k && len > 0) {
00077       // Set the key
00078       SetBuffer(len,k);
00079 
00080       valid = 1;
00081 
00082       // Set also the type
00083       if (!t || (t && !strcmp(t,"default")))
00084          SetType("PC1");
00085       else
00086          SetType(t);
00087    }
00088 }
00089 
00090 //____________________________________________________________________________
00091 XrdCryptolocalCipher::XrdCryptolocalCipher(XrdSutBucket *bck)
00092 {
00093    // Constructor from bucket.
00094    // Initialize a cipher of type t and length l using the key at k
00095    // Used to import ciphers.
00096 
00097    valid  = 0;
00098    bpub  = 0;
00099    bpriv = 0;
00100 
00101    if (bck && bck->size > 0) {
00102       valid = 1;
00103       char *pr = bck->buffer;
00104       kXR_int32 lbuf = 0;
00105       kXR_int32 ltyp = 0;
00106       kXR_int32 lpub = 0;
00107       kXR_int32 lpri = 0;
00108       memcpy(&lbuf,pr,sizeof(kXR_int32));
00109       pr += sizeof(kXR_int32);
00110       memcpy(&ltyp,pr,sizeof(kXR_int32));
00111       pr += sizeof(kXR_int32);
00112       memcpy(&lpub,pr,sizeof(kXR_int32));
00113       pr += sizeof(kXR_int32);
00114       memcpy(&lpri,pr,sizeof(kXR_int32));
00115       pr += sizeof(kXR_int32);
00116       // main buffer
00117       if (lbuf > 0) {
00118          char *buf = new char[lbuf];
00119          if (buf) {
00120             memcpy(buf,pr,lbuf);
00121             // Set the key
00122             SetBuffer(lbuf,buf);
00123             delete[] buf;
00124          } else
00125             valid = 0;
00126          pr += lbuf;
00127       }
00128       // type
00129       if (ltyp > 0) {
00130          char *buf = new char[ltyp+1];
00131          if (buf) {
00132             memcpy(buf,pr,ltyp);
00133             pr[ltyp] = 0;
00134             // Set the key
00135             SetType(buf);
00136             delete[] buf;
00137          } else
00138             valid = 0;
00139          pr += ltyp;
00140       }
00141       // bpub
00142       if (lpub > 0) {
00143          bpub = new uchar[lpub];
00144          if (bpub) {
00145             memcpy(bpub,pr,lpub);
00146          } else
00147             valid = 0;
00148          pr += lpub;
00149       }
00150       // bpriv
00151       if (lpri > 0) {
00152          bpriv = new uchar[lpri];
00153          if (bpriv) {
00154             memcpy(bpriv,pr,lpri);
00155          } else
00156             valid = 0;
00157          pr += lpri;
00158       }
00159    }
00160 }
00161 
00162 //____________________________________________________________________________
00163 XrdCryptolocalCipher::XrdCryptolocalCipher(int bits, char *pub,
00164                                            int lpub, const char *t)
00165 {
00166    // Constructor for key agreement.
00167    // Generates private + public parts. The public can be retrieved
00168    // using Public() to send to the counterpart.
00169    // The number of random bits to be used in 'bits'.
00170    // If pub is defined (with the public info of the counterpart)
00171    // finalizes the initialization by computing the cipher.
00172    // Sets also the name to 't', if defined (should be always 'PC1').
00173    // Used for key agreement.
00174 
00175    valid  = 0;
00176    bpub  = 0;
00177    bpriv = 0;
00178    lpub = kPC3SLEN;   
00179 
00180    //
00181    // Generate local info
00182    bpub = new uchar[kPC3SLEN];   
00183    if (bpub) {
00184       bpriv = new uchar[kPC3SLEN];   
00185       if (bpriv) {
00186          // at least 128 bits
00187          bits = (bits < kPC3MINBITS) ? kPC3MINBITS : bits; 
00188          // Generate the random passwd
00189          unsigned int lrpw = bits / 8 ;
00190          uchar *rpwd = (uchar *)XrdSutRndm::GetBuffer((int)lrpw);
00191          if (rpwd) {
00192             if (PC3InitDiPuk(rpwd, lrpw, bpub, bpriv) == 0)
00193                valid = 1;
00194             bpriv[kPC3SLEN-1] = 0;
00195             delete[] rpwd; rpwd = 0;
00196          } 
00197       }
00198    }
00199    if (!valid)
00200       Cleanup();
00201    //
00202    // If we are given already the counter part, we finalize
00203    // the operations
00204    if (valid && pub) {
00205 
00206       // Convert back from hex
00207       char *tpub = new char[strlen(pub)/2+2];
00208       int tlen = 0;
00209       if (tpub)
00210          XrdSutFromHex((const char *)pub, tpub, tlen);
00211 
00212       uchar *ktmp = new uchar[kPC3KEYLEN];   
00213       if (PC3DiPukExp((uchar *)tpub, bpriv, ktmp) == 0) {
00214          // Store the key
00215          SetBuffer(kPC3KEYLEN,(char *)ktmp);
00216          // Set also the key type (should be "PC1")
00217          if (!t || (t && !strcmp(t,"default")))
00218             SetType("PC1");
00219          else
00220             SetType(t);
00221       } else {
00222          valid = 0;
00223       }
00224    }   
00225 }
00226 
00227 //____________________________________________________________________________
00228 XrdCryptolocalCipher::XrdCryptolocalCipher(const XrdCryptolocalCipher &c)
00229 {
00230    // Copy Constructor
00231 
00232    valid = c.valid;
00233    // Copy buffer
00234    SetBuffer(c.Length(),c.Buffer());
00235    // Copy Type
00236    SetType(c.Type());
00237    // Copy Buffers for key agreement
00238    if (c.bpub) {
00239       bpub = new uchar[kPC3SLEN];   
00240       if (bpub)
00241          memcpy(bpub,c.bpub,kPC3SLEN);
00242       else
00243          valid = 0;
00244    }
00245    if (c.bpriv) {
00246       bpriv = new uchar[kPC3SLEN];   
00247       if (bpriv)
00248          memcpy(bpriv,c.bpriv,kPC3SLEN);
00249       else
00250          valid = 0;
00251    }
00252 }
00253 
00254 //____________________________________________________________________________
00255 bool XrdCryptolocalCipher::Finalize(char *pub, int lpub, const char *t)
00256 {
00257    // Final initialization for key agreement.
00258    // 'pub' is the buffer sent by teh counterpart.
00259    // The private part must be defined already.
00260 
00261    lpub = kPC3SLEN;   
00262    if (valid && bpriv && pub) {
00263 
00264       // Convert back from hex
00265       char *tpub = new char[strlen(pub)/2+2];
00266       int tlen = 0;
00267       if (tpub)
00268          XrdSutFromHex((const char *)pub, tpub, tlen);
00269 
00270       uchar *ktmp = new uchar[kPC3KEYLEN];   
00271       if (PC3DiPukExp((uchar *)tpub, bpriv, ktmp) == 0) {
00272          // Store the key
00273          SetBuffer(kPC3KEYLEN,(char *)ktmp);
00274          // Set also the key type (should be "PC1")
00275          if (!t || (t && !strcmp(t,"default")))
00276             SetType("PC1");
00277          else
00278             SetType(t);
00279          return 1;
00280       } else {
00281          valid = 0;
00282       }
00283    } else {
00284       valid = 0;
00285    }
00286    return 0;
00287 }
00288 
00289 //____________________________________________________________________________
00290 void XrdCryptolocalCipher::Cleanup()
00291 {
00292    // Cleanup temporary buffers used for key agreement
00293 
00294    if (bpub) delete[] bpub; bpub = 0;
00295    if (bpriv) delete[] bpriv; bpriv = 0;
00296 }
00297 
00298 //____________________________________________________________________________
00299 char *XrdCryptolocalCipher::Public(int &lpub)
00300 {
00301    // Return pointer to information to be sent to the 
00302    // counterpart during key agreement. The allocated buffer, of size
00303    // lpub, must be deleted by the caller.
00304 
00305    if (bpub) {
00306       char *pub = new char[2*(kPC3SLEN-1)+1];
00307       if (pub) {
00308          XrdSutToHex((const char *)bpub, kPC3SLEN-1, pub);;
00309          lpub = 2*(kPC3SLEN-1);
00310          return pub;
00311       }
00312    }
00313 
00314    // Not available
00315    lpub = 0;
00316    return (char *)0;
00317 }
00318 
00319 //_____________________________________________________________________________
00320 XrdSutBucket *XrdCryptolocalCipher::AsBucket()
00321 {
00322    // Return pointer to a bucket created using the internal information
00323    // serialized
00324    // The bucket is responsible for the allocated memory
00325 
00326    XrdSutBucket *buck = (XrdSutBucket *)0;
00327 
00328    if (valid) {
00329 
00330       // Serialize .. total length
00331       kXR_int32 lbuf = Length();
00332       kXR_int32 ltyp = Type() ? strlen(Type()) : 0;
00333       kXR_int32 lpub = bpub ? kPC3SLEN : 0;
00334       kXR_int32 lpri = bpriv ? kPC3SLEN : 0;
00335       int ltot = 4*sizeof(kXR_int32) + lpub + ltyp + lpub + lpri;
00336       char *newbuf = new char[ltot];
00337       if (newbuf) {
00338          int cur = 0;
00339          memcpy(newbuf+cur,&lbuf,sizeof(kXR_int32));
00340          cur += sizeof(kXR_int32);
00341          memcpy(newbuf+cur,&ltyp,sizeof(kXR_int32));
00342          cur += sizeof(kXR_int32);
00343          memcpy(newbuf+cur,&lpub,sizeof(kXR_int32));
00344          cur += sizeof(kXR_int32);
00345          memcpy(newbuf+cur,&lpri,sizeof(kXR_int32));
00346          cur += sizeof(kXR_int32);
00347          if (Buffer()) {
00348             memcpy(newbuf+cur,Buffer(),lbuf);
00349             cur += lbuf;
00350          }
00351          if (Type()) {
00352             memcpy(newbuf+cur,Type(),ltyp);
00353             cur += ltyp;
00354          }
00355          if (bpub) {
00356             memcpy(newbuf+cur,bpub,lpub);
00357             cur += lpub;
00358          }
00359          if (bpriv) {
00360             memcpy(newbuf+cur,bpriv,lpri);
00361             cur += lpri;
00362          }
00363          // The bucket now
00364          buck = new XrdSutBucket(newbuf,ltot,kXRS_cipher);
00365       }
00366    }
00367 
00368    return buck;
00369 }
00370 
00371 //____________________________________________________________________________
00372 int XrdCryptolocalCipher::Encrypt(const char *in, int lin, char *out)
00373 {
00374    // Encrypt lin bytes at in with local cipher.
00375    // The outbut buffer must be provided by the caller for at least
00376    // EncOutLength(lin) bytes.
00377    // Returns number of meaningful bytes in out, or 0 in case of problems
00378 
00379    return PC1Encrypt((const char *)in, lin,
00380                      (const char *)Buffer(), Length(), out);
00381 }
00382 
00383 //____________________________________________________________________________
00384 int XrdCryptolocalCipher::Decrypt(const char *in, int lin, char *out)
00385 {
00386    // Decrypt lin bytes at in with local cipher.
00387    // The outbut buffer must be provided by the caller for at least
00388    // DecOutLength(lin) bytes.
00389    // Returns number of meaningful bytes in out, or 0 in case of problems
00390 
00391    return PC1Decrypt((const char *)in, lin,
00392                      (const char *)Buffer(), Length(), out);
00393 }
00394 
00395 //____________________________________________________________________________
00396 int XrdCryptolocalCipher::EncOutLength(int l)
00397 {
00398    // Required buffer size for encrypting l bytes
00399 
00400    return (2*l);
00401 }
00402 
00403 //____________________________________________________________________________
00404 int XrdCryptolocalCipher::DecOutLength(int l)
00405 {
00406    // Required buffer size for decrypting l bytes
00407 
00408    return (l/2+1);
00409 }
00410 
00411 
00412 //____________________________________________________________________________
00413 bool XrdCryptolocalCipher::IsDefaultLength() const
00414 {
00415    // Returns true if cipher has the default length
00416 
00417    return Length() == kPC1LENGTH;
00418 }

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