Beispiel #1
0
SecByteBlock Converter::encodeSecByteBlockWithLength(Integer key, int length)
{
    //int length = key.MinEncodedSize();
    byte * byteX;
    key.Encode(byteX, length);
    
    SecByteBlock pubKeyA;
    pubKeyA.Assign(byteX, length);

	std::cout<<"Key: " << key <<std::endl;
	std::cout<<"Decoded: " << decodeSecByteBlock(pubKeyA) <<std::endl;
    //check
    if (key != decodeSecByteBlock(pubKeyA))
        std::cout << "Error while encoding Integer to SecByteBlock" << std::endl;
    
    return pubKeyA;
}
Beispiel #2
0
void CryptoSession::parseAuthenticate2Reply(const string &bobPacket)
{
  /* Alice verifies that the random string she sent bob is included in bobs msg */
  AutoSeededRandomPool rng(false,128);

  int len;
  Storage::sectionType s;

  StringSource pct(bobPacket,true,new Base64Decoder(new Gunzip));
  len = pct.MaxRetrievable();
  byte *tmp = new byte[len];
  pct.Get(tmp,len);
  Storage r;
  r.setData(len,tmp);

  s=r.readSection(0);
  Storage::sectionType s2 = r.readSection(1);

  /* verify signature */
  StringSource pubFile(peer.getPubkey().c_str(),true,new HexDecoder);
  RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);

  if ((unsigned)s2.second.second!=pub.SignatureLength())
  {
    cout << "you puny earthling!\n";
    debug() << "Bleh!!" << endl;
  }
  SecByteBlock signature(pub.SignatureLength());
  signature.Assign(s2.second.first,s2.second.second);

  VerifierFilter *verifierFilter = new VerifierFilter(pub);
  verifierFilter->PutSignature(signature);
  StringSource f(s.second.first,s.second.second, true, verifierFilter);

  byte result = 0;
  f.Get(result);
  if (result != 1)
  {
    cout << "you deserve a horrible pimple on your nose\n";
    debug() << "Bleh!!" << endl;
  }
  else
    cout << "Signature verified\n";
  /* Alice now decrypts the signed packet */
  cout <<"Data : ";
  StringSource(s.second.first,s.second.second,true,new HexEncoder(new FileSink(cout)));
  cout << "\n";

  StringSource privFile(privKey, true, new HexDecoder);
  RSAES_OAEP_SHA_Decryptor priv(privFile);
  SecByteBlock buf;
  buf.Assign(s.second.first,s.second.second);
  byte *outstr = new byte[priv.MaxPlainTextLength()+1];
  unsigned messageLength = priv.Decrypt(buf,outstr);

  cout << messageLength << " " << s.second.second<< endl;

  Storage msg;
  msg.setData(messageLength,outstr);

  s=msg.readSection(0);
  cout << s.first << endl;
  if (s.first != IDENTITY)
  {
    cout << "you suck way too much!\n";
    debug() << "Bleh!!" << endl;
  }
  cout << s.second.first << endl;

  //  byte *sessionKey;
  //int sessionKeyLen;
  s=msg.readSection(1);
  if (s.first != RANDOMDATA)
  {
    cout << "you suck too much ass!\n";
    debug() << "Bleh!!" << endl;
  }
  int randomDataLen=s.second.second;
  byte *randomData=new byte[randomDataLen];
  memcpy(randomData,s.second.first,randomDataLen);

  cout <<"\nRandom data we sent: ";
  StringSource(randomData,randomDataLen,true,new HexEncoder(new FileSink(cout)));
  cout << "\n";

  // assert this str is equal to the random string we sent earlier.

  s = msg.readSection(2);
  if (s.first != SESSIONKEY2)
  {
    cout << "you suck way too much ass!\n";
    debug() << "Bleh!!" << endl;
  }
  sessionKey2Len=s.second.second;
  sessionKey2=new byte[sessionKey2Len];
  memcpy(sessionKey2,s.second.first,sessionKey2Len);

  cout <<"Sessionkey2: ";
  StringSource(sessionKey2,sessionKey2Len,true,new HexEncoder(new FileSink(cout)));
  cout << "\n";

  finalKeyLen=16;
  finalKey=new byte[finalKeyLen];
  for (int i=0;i<16;i++)
    finalKey[i] = sessionKey[i] ^ sessionKey2[i];

  s = msg.readSection(3);
  if (s.first != RANDOMDATA2)
  {
    cout << "you suck way too much donkey ass!\n";
    debug() << "Bleh!!" << endl;
  }
  cout << s.second.second << endl;
  int random2DataLen=s.second.second;
  byte *random2Data=new byte[random2DataLen];
  memcpy(random2Data,s.second.first,random2DataLen);

  cout <<"Random data we got from Bob: " << random2DataLen << endl;
  StringSource(random2Data,random2DataLen,true,new HexEncoder(new FileSink(cout)));
  cout << "\n\n";
  memcpy(random2,random2Data,16);
}
Beispiel #3
0
// Bob
void CryptoSession::parseAuthenticateReply(const string &alicePacket)
{

  AutoSeededRandomPool rng(false,128);

  int len;
  Storage::sectionType s;

  StringSource pct(alicePacket,true,new Base64Decoder(new Gunzip));
  len = pct.MaxRetrievable();
  byte *tmp = new byte[len];
  pct.Get(tmp,len);
  Storage r;
  r.setData(len,tmp);

  s=r.readSection(0);
  if (s.first != CERTIFICATE)
  {
    cout << "Corrupt packet\n";
    debug() << "Bleh!!" << endl;
  }
  /* Bob verifies the validity of the certificate */
  Certificate c(s.second.first,s.second.second);
  cout << "Version: "<< c.getVersion() << endl;
  cout << "Serial: "<< c.getSerial() << endl;
  cout << "Issuer: "<<  c.getIssuer() << endl;
  cout << "Subject: "<< c.getSubject() << endl;
  cout << "Pubkey Algorithm: "<< c.getPubkeyAlgorithm() << endl;
  cout << "Pubkey: "<< c.getPubkey() << endl;
  if (c.verify(""))
    cout << "Certificate verified!\n";
  else
    cout << "************ invalid certificate! **************\n";

  peer = c;

  /* Bob verifies her signature */
  s=r.readSection(1);
  Storage::sectionType s2 = r.readSection(2);

  StringSource pubFile(c.getPubkey().c_str(),true,new HexDecoder);
  RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);

  if ((unsigned)s2.second.second!=pub.SignatureLength())
  {
    cout << "Alices signature is invalid\n";
    debug() << "Bleh!!" << endl;
  }
  SecByteBlock signature(pub.SignatureLength());
  signature.Assign(s2.second.first,s2.second.second);

  VerifierFilter *verifierFilter = new VerifierFilter(pub);
  verifierFilter->PutSignature(signature);
  StringSource f(s.second.first,s.second.second, true, verifierFilter);

  byte result = 0;
  f.Get(result);
  if (result != 1)
  {
    cout << "Alices signature is invalid\n";
    debug() << "Bleh!!" << endl;
  }

  /* Bob now decrypts the signed packet */
  cout <<"Data : ";
  StringSource(s.second.first,s.second.second,true,new HexEncoder(new FileSink(cout)));
  cout << "\n";

  StringSource privFile(privKey, true, new HexDecoder);
  RSAES_OAEP_SHA_Decryptor priv(privFile);
  SecByteBlock buf;
  buf.Assign(s.second.first,s.second.second);
  byte *outstr = new byte[priv.MaxPlainTextLength()+1];
  unsigned messageLength = priv.Decrypt(buf,outstr);

  cout << messageLength << " " << s.second.second<< endl;

  Storage msg;
  msg.setData(messageLength,outstr);

  s=msg.readSection(0);
  cout << s.first << endl;
  if (s.first != IDENTITY)
  {
    cout << "you suck too much!\n";
    debug() << "Bleh!!" << endl;
  }
  cout << s.second.first << endl;

  s=msg.readSection(1);
  if (s.first != SESSIONKEY)
  {
    cout << "you suck too much ass!\n";
    debug() << "Bleh!!" << endl;
  }
  sessionKeyLen=s.second.second;
  sessionKey=new byte[sessionKeyLen];
  memcpy(sessionKey,s.second.first,sessionKeyLen);

  cout <<"\nSessionkey : ";
  StringSource(sessionKey,sessionKeyLen,true,new HexEncoder(new FileSink(cout)));
  cout << "\n";

  s = msg.readSection(2);
  cout << s.first << endl;
  if (s.first != RANDOMDATA ||s.second.second != 16)
  {
    cout << "you suck too much ass!\n";
    debug() << "Bleh!!" << endl;
  }
  memcpy(random,s.second.first,16);
}