Exemplo n.º 1
0
string SignLicense(AutoSeededRandomPool &rng, string strContents, string pass)
{
	//Read private key
	string encPrivKey;
	StringSink encPrivKeySink(encPrivKey);
	FileSource file("secondary-privkey-enc.txt", true, new Base64Decoder);
	file.CopyTo(encPrivKeySink);

	//Read initialization vector
	byte iv[AES::BLOCKSIZE];
	CryptoPP::ByteQueue bytesIv;
	FileSource file2("secondary-privkey-iv.txt", true, new Base64Decoder);
	file2.TransferTo(bytesIv);
	bytesIv.MessageEnd();
	bytesIv.Get(iv, AES::BLOCKSIZE);

	//Hash the pass phrase to create 128 bit key
	string hashedPass;
	RIPEMD128 hash;
	StringSource(pass, true, new HashFilter(hash, new StringSink(hashedPass)));

	//Decrypt private key
	byte test[encPrivKey.length()];
	CFB_Mode<AES>::Decryption cfbDecryption((const unsigned char*)hashedPass.c_str(), hashedPass.length(), iv);
	cfbDecryption.ProcessData(test, (byte *)encPrivKey.c_str(), encPrivKey.length());
	StringSource privateKeySrc(test, encPrivKey.length(), true, NULL);

	//Decode key
	RSA::PrivateKey privateKey;
	privateKey.Load(privateKeySrc);

	//Sign message
	RSASSA_PKCS1v15_SHA_Signer privkey(privateKey);
	SecByteBlock sbbSignature(privkey.SignatureLength());
	privkey.SignMessage(
		rng,
		(byte const*) strContents.data(),
		strContents.size(),
		sbbSignature);

	//Save result
	string out;
	Base64Encoder enc(new StringSink(out));
	enc.Put(sbbSignature, sbbSignature.size());
	enc.MessageEnd();

	return out;
}
Exemplo n.º 2
0
RSA::PrivateKey CRSAEncryptDecrypt::getPrivateKey(std::string pathToKeyStorage)
{
   LOG4CPLUS_INFO(msLogger, "getPrivate()");
   timeval begin, end;
   double elapsedTime;
   gettimeofday(&begin, NULL);
   if(!privateKeyGenerated)
   {
      //try to read it from file
      std::string filePath = pathToKeyStorage+"/private.k";
      LOG4CPLUS_INFO(msLogger, "getPrivate(): filepath="+filePath);

      ifstream inFile;
      size_t size = 0;

      inFile.open(filePath.c_str(), ios::in|ios::binary|ios::ate );
      if(inFile.good()) // the file is there and is readable
      {
         LOG4CPLUS_INFO(msLogger, "getPrivate(): retrieving previously stored data");
         char* signedData = 0;
         inFile.seekg(0, ios::end); // set the pointer to the end
         size = inFile.tellg() ; // get the length of the file
         inFile.seekg(0, ios::beg); // set the pointer to the beginning

         signedData = new char[size]; 
         inFile.read( signedData, size );
         inFile.close();
         UInt8 * unSignedData = new UInt8[size];
         memcpy(unSignedData, signedData, size);
         CryptoPP::ByteQueue queue;
         queue.Put(unSignedData,  size);
         rsaPrivate.Load(queue);
         delete[] signedData;
         delete[] unSignedData;
      } 
      else // the file is not there
      {
         LOG4CPLUS_INFO(msLogger, "getPrivate(): no key stored, generating");
         AutoSeededRandomPool rnd;
         rsaPrivate.GenerateRandomWithKeySize(rnd, 2048);
         // storing in file
         LOG4CPLUS_INFO(msLogger, "getPrivate(): saving generated file");
         CryptoPP::ByteQueue queue;
         rsaPrivate.Save(queue);
         UInt32 queueSize = queue.CurrentSize();
         UInt8* outgoingData = new UInt8[queueSize];
         queue.Get(outgoingData, queueSize);
         std::ofstream outputBufferHere(filePath.c_str(), std::ios::binary|std::ios::out);
         char * signedOutputData = new char[queueSize];
         memcpy(signedOutputData, outgoingData, queueSize);
         outputBufferHere.write(signedOutputData, queueSize);
         outputBufferHere.close();
         delete[] outgoingData;
         delete[] signedOutputData;
      }
      privateKeyGenerated = true;
   }

   gettimeofday(&end, NULL);
   elapsedTime = (end.tv_sec - begin.tv_sec) * 1000.0;      // sec to ms
   elapsedTime += (end.tv_usec - begin.tv_usec) / 1000.0;   // us to ms
   std::ostringstream s;
   s << "(" << elapsedTime << ") ms";
   LOG4CPLUS_INFO(msLogger, "Time elapsed generating private key: ");
   LOG4CPLUS_INFO(msLogger, s.str());
   return rsaPrivate;
}