/******************************************************************************/ /* */ /* X r d C r y p t o t e s t . 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 . */ /* */ /* 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. */ /******************************************************************************/ // // Test program for XrdCrypto // #include #include #include #include "XrdOuc/XrdOucString.hh" #include "XrdSut/XrdSutAux.hh" #include "XrdSut/XrdSutBucket.hh" #include "XrdCrypto/XrdCryptoAux.hh" #include "XrdCrypto/XrdCryptoFactory.hh" #include "XrdCrypto/XrdCryptoCipher.hh" #include "XrdCrypto/XrdCryptoMsgDigest.hh" #include "XrdCrypto/XrdCryptoRSA.hh" #include "XrdCrypto/XrdCryptoX509.hh" // // Globals #define PRINT(x) {cerr <\n",outname); printf(" e.g. %s ssl\n",outname); printf(" Assuming = ssl\n\n"); } else { strcpy(cryptomod,argv[1]); } bool local = !strcmp(cryptomod,"local"); // // Load the crypto factory if (!(gCryptoFactory = XrdCryptoFactory::GetCryptoFactory(cryptomod))) { PRINT(outname<<": cannot instantiate factory "<SetTrace(cryptoTRACE_Debug); // // Message Digest of a simple message PRINT(outname<<": --------------------------------------------------- "); PRINT(outname<<": Testing MD ... "); XrdCryptoMsgDigest *MD_1 = gCryptoFactory->MsgDigest("md5"); if (MD_1) { MD_1->Update("prova",strlen("prova")); MD_1->Final(); // Check result char MD5prova[128] = "189bbbb00c5f1fb7fba9ad9285f193d1"; if (strncmp(MD_1->AsHexString(),MD5prova,MD_1->Length())) { PRINT(outname<<": MD mismatch: "); PRINT(outname<<": got: "<AsHexString()); PRINT(outname<<": instead of: "<Cipher("bf-cbc"); if (BF_1) { PRINT(outname<<": cipher length: "<Length()); PRINT(outname<<": cipher hex: "<AsHexString()); char tm_1[64] = "Test message for cipher - 001"; PRINT(outname<<": Test message: "<EncOutLength(ltm_1)]; if (tmp_1) { int ltmp = BF_1->Encrypt(tm_1,ltm_1,tmp_1); char tm_2[128] = {0}; XrdSutToHex(tmp_1,ltmp,&tm_2[0]); PRINT(outname<<": cipher encrypted (hex):"); PRINT(tm_2); char *tm_3 = new char[BF_1->DecOutLength(ltmp)]; int lfin = BF_1->Decrypt(tmp_1,ltmp,tm_3); delete[] tmp_1; if (tm_3) { PRINT(outname<<": cipher decrypted: "<Encrypt(Bck1); PRINT(outname<<": length of encryption: "<Decrypt(Bck1); PRINT(outname<<": length of decryption: "<KDFun(); if (KDFun) { const char *pass = "pippo"; int plen = strlen(pass); const char *salt = "$$10000$derek"; int slen = strlen(salt); char key[128]; char KDFunprova[128] = {0}; bool matching = 0; if (local) { int klen = (*KDFun)(pass,plen,salt,slen,key,0); PRINT(outname<<": key is: "<< key<< " ("<RSA(1024); if (TestRSA_1) { XrdCryptoRSA *CpyRSA = gCryptoFactory->RSA(*TestRSA_1); if (CpyRSA) CpyRSA->Dump(); char RSApubexp[4096]; TestRSA_1->ExportPublic(RSApubexp,4096); PRINT(outname<<": public export:"<GetPublen()); PRINT(outname<<": --------------------------------------------------- "); char RSApriexp[4096]; TestRSA_1->ExportPrivate(RSApriexp,4096); PRINT(outname<<": private export:"<GetPrilen()); PRINT(outname<<": --------------------------------------------------- "); PRINT(outname<<": --------------------------------------------------- "); PRINT(outname<<": testing import/export "); XrdCryptoRSA *TestRSA_2 = gCryptoFactory->RSA(1024); TestRSA_2->ImportPublic(RSApubexp,strlen(RSApubexp)); TestRSA_2->ImportPrivate(RSApriexp,strlen(RSApriexp)); PRINT(outname<<": --------------------------------------------------- "); char buf_1[128] = "Here I am ... in test"; int lin = strlen(buf_1); char buf_2[4096]; PRINT(outname<<": encrypting (public): "<EncryptPublic(buf_1,strlen(buf_1),buf_2,512); char buf_2_hex[4096]; XrdSutToHex(buf_2,lout1,buf_2_hex); PRINT(outname<<": output has "<DecryptPrivate(buf_2,lout1,buf_3,512); PRINT(outname<<": got: "< lin) { PRINT(outname<<": RSA public enc / private dec length mismatch: "); PRINT(outname<<": got: "<EncryptPrivate(buf_1,strlen(buf_1),buf_2,512); XrdSutToHex(buf_2,lout1,buf_2_hex); PRINT(outname<<": output has "<DecryptPublic(buf_2,lout1,buf_3,512); PRINT(outname<<": got: "< lin) { PRINT(outname<<": RSA private enc / public dec length mismatch: "); PRINT(outname<<": got: "<EncryptPrivate(Bck1); PRINT(outname<<": length of private encryption: "<DecryptPublic(Bck1); PRINT(outname<<": length of public decryption: "<EncryptPublic(Bck2); PRINT(outname<<": length of public encryption: "<DecryptPrivate(Bck2); PRINT(outname<<": length of private decryption: "<RSA(2048); lout1 = TestRSA_1->EncryptPrivate(buf_1,strlen(buf_1),buf_2,4096); lout2 = TestRSA_1->DecryptPublic(buf_2,lout1,buf_3,4096); if (memcmp(buf_1,buf_3,lin)) { PRINT(outname<<": RSA private enc / public dec mismatch: "< lin) { PRINT(outname<<": RSA private enc / public dec length mismatch: "<Cipher(0,0,0); if (CF_1 && CF_1->IsValid()) { // Get public part and save it to a buffer if (!(bp1 = CF_1->Public(lp1))) { PRINT(outname<<": CF_1 cipher: problems getting public part "); exit(1); } } else { PRINT(outname<<": CF_1 cipher object could not be instantiated: "); } // Get a third cipher directly from constructor char *bp3 = 0; int lp3 = 0; PRINT(outname<<": CF_3: instantiate ... with pub"); if (!local) PRINT(bp1); XrdCryptoCipher *CF_3 = gCryptoFactory->Cipher(0,bp1,lp1); if (CF_3 && CF_3->IsValid()) { // Get public part and save it to a buffer if (!(bp3 = CF_3->Public(lp3))) { PRINT(outname<<": CF_3 cipher: problems getting public part "); exit(1); } } else { PRINT(outname<<": CF_3 cipher object could not be instantiated: "); } // Complete initialization if (CF_1 && CF_1->IsValid() && bp3) { PRINT(outname<<": CF_1: finalize ... with pub"); if (!local) PRINT(bp3); CF_1->Finalize(bp3,lp3,"default"); } else { PRINT(outname<<": CF_1 cipher object could not be finalized "); } // Test matching now if (CF_1 && CF_1->IsValid() && CF_3 && CF_3->IsValid()) { char chex[128] = {0}; XrdSutToHex(CF_1->Buffer(),CF_1->Length(),&chex[0]); PRINT(outname<<": cipher 1 encrypted (hex):"); PRINT(chex); PRINT(outname<<": cipher 1 used length: "<Length()); XrdSutToHex(CF_3->Buffer(),CF_3->Length(),&chex[0]); PRINT(outname<<": cipher 3 encrypted (hex):"); PRINT(chex); PRINT(outname<<": cipher 3 used length: "<Length()); if (CF_1->Length() == CF_3->Length()) { if (!memcmp(CF_1->Buffer(),CF_3->Buffer(),CF_3->Length())) { PRINT(outname<<": ciphers match !"); } else { PRINT(outname<<": ciphers DO NOT match !"); } } } // Encryption if (CF_1 && CF_1->IsValid() && CF_3 && CF_3->IsValid()) { char tm_1[64] = "Test message for cipher - 001"; PRINT(outname<<": Test message: "<EncOutLength(ltm_1)]; if (tmp_1) { int ltmp = CF_1->Encrypt(tm_1,ltm_1,tmp_1); char tm_2[128] = {0}; XrdSutToHex(tmp_1,ltmp,&tm_2[0]); PRINT(outname<<": cipher encrypted (hex):"); PRINT(tm_2); char *tm_3 = new char[CF_3->DecOutLength(ltmp)+1]; int lfin = CF_3->Decrypt(tmp_1,ltmp,tm_3); delete[] tmp_1; if (tm_3) { tm_3[lfin] = 0; PRINT(outname<<": cipher decrypted: "<ID() == 1) { PRINT(outname<<": --------------------------------------------------- "); PRINT(outname<<": Testing X509 functionality ... "); XrdCryptoX509 *x509 = gCryptoFactory->X509("/home/ganis/.globus/usercert.pem"); if (x509) { x509->Dump(); } } PRINT(outname<<": --------------------------------------------------- "); exit(0); }