00001
00002
00003 const char *XrdCryptolocalCipherCVSID = "$Id: XrdCryptolocalCipher.cc 30949 2009-11-02 16:37:58Z ganis $";
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00029
00030
00031
00032
00033 XrdCryptolocalCipher::XrdCryptolocalCipher(const char *t, int l)
00034 {
00035
00036
00037
00038
00039
00040 valid = 0;
00041 bpub = 0;
00042 bpriv = 0;
00043
00044
00045 int len = (l > 0 && l <= kPC1LENGTH) ? l : kPC1LENGTH ;
00046
00047
00048 char *ktmp = XrdSutRndm::GetBuffer(len);
00049 if (ktmp) {
00050
00051 SetBuffer(len,ktmp);
00052 valid = 1;
00053
00054
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
00066
00067
00068
00069 valid = 0;
00070 bpub = 0;
00071 bpriv = 0;
00072
00073
00074 int len = (l <= kPC1LENGTH) ? l : kPC1LENGTH ;
00075
00076 if (k && len > 0) {
00077
00078 SetBuffer(len,k);
00079
00080 valid = 1;
00081
00082
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
00094
00095
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(<yp,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
00117 if (lbuf > 0) {
00118 char *buf = new char[lbuf];
00119 if (buf) {
00120 memcpy(buf,pr,lbuf);
00121
00122 SetBuffer(lbuf,buf);
00123 delete[] buf;
00124 } else
00125 valid = 0;
00126 pr += lbuf;
00127 }
00128
00129 if (ltyp > 0) {
00130 char *buf = new char[ltyp+1];
00131 if (buf) {
00132 memcpy(buf,pr,ltyp);
00133 pr[ltyp] = 0;
00134
00135 SetType(buf);
00136 delete[] buf;
00137 } else
00138 valid = 0;
00139 pr += ltyp;
00140 }
00141
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
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
00167
00168
00169
00170
00171
00172
00173
00174
00175 valid = 0;
00176 bpub = 0;
00177 bpriv = 0;
00178 lpub = kPC3SLEN;
00179
00180
00181
00182 bpub = new uchar[kPC3SLEN];
00183 if (bpub) {
00184 bpriv = new uchar[kPC3SLEN];
00185 if (bpriv) {
00186
00187 bits = (bits < kPC3MINBITS) ? kPC3MINBITS : bits;
00188
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
00203
00204 if (valid && pub) {
00205
00206
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
00215 SetBuffer(kPC3KEYLEN,(char *)ktmp);
00216
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
00231
00232 valid = c.valid;
00233
00234 SetBuffer(c.Length(),c.Buffer());
00235
00236 SetType(c.Type());
00237
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
00258
00259
00260
00261 lpub = kPC3SLEN;
00262 if (valid && bpriv && pub) {
00263
00264
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
00273 SetBuffer(kPC3KEYLEN,(char *)ktmp);
00274
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
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
00302
00303
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
00315 lpub = 0;
00316 return (char *)0;
00317 }
00318
00319
00320 XrdSutBucket *XrdCryptolocalCipher::AsBucket()
00321 {
00322
00323
00324
00325
00326 XrdSutBucket *buck = (XrdSutBucket *)0;
00327
00328 if (valid) {
00329
00330
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,<yp,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
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
00375
00376
00377
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
00387
00388
00389
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
00399
00400 return (2*l);
00401 }
00402
00403
00404 int XrdCryptolocalCipher::DecOutLength(int l)
00405 {
00406
00407
00408 return (l/2+1);
00409 }
00410
00411
00412
00413 bool XrdCryptolocalCipher::IsDefaultLength() const
00414 {
00415
00416
00417 return Length() == kPC1LENGTH;
00418 }