sign all files in a profile

make RageFile filters for use with Crypt++
This commit is contained in:
Chris Danford
2004-02-15 04:47:32 +00:00
parent e80e0a2a25
commit 3d69b6e0ec
12 changed files with 496 additions and 131 deletions
+161 -48
View File
@@ -1,67 +1,180 @@
#include "sha.h"
#include "global.h"
#include "CryptHelpers.h"
// crypt headers
#include "files.h"
#include "hex.h"
#include "channels.h"
#include "rsa.h"
#include "md5.h"
#include "randpool.h"
#include <memory>
using namespace CryptoPP;
using namespace std;
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
void RageFileStore::StoreInitialize(const NameValuePairs &parameters)
{
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
HexEncoder privFile(new FileSink(privFilename));
priv.DEREncode(privFile);
privFile.MessageEnd();
RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder pubFile(new FileSink(pubFilename));
pub.DEREncode(pubFile);
pubFile.MessageEnd();
const char *fileName;
if (parameters.GetValue("InputFileName", fileName))
{
if( !m_file.Open(fileName, RageFile::READ) )
throw OpenErr(fileName);
}
else
{
ASSERT(0);
}
m_waiting = false;
}
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename)
unsigned long RageFileStore::MaxRetrievable() const
{
FileSource privFile(privFilename, true, new HexDecoder);
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
RandomNumberGenerator &rng = RandomPool();
FileSource f(messageFilename, true, new SignerFilter(rng, priv, new HexEncoder(new FileSink(signatureFilename))));
if( !m_file.IsOpen() )
return 0;
return m_file.GetFileSize() - m_file.Tell();
}
bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename)
unsigned int RageFileStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
if( !m_file.IsOpen() )
{
transferBytes = 0;
return 0;
}
FileSource signatureFile(signatureFilename, true, new HexDecoder);
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
return false;
SecByteBlock signature(pub.SignatureLength());
signatureFile.Get(signature, signature.size());
unsigned long size=transferBytes;
transferBytes = 0;
VerifierFilter *verifierFilter = new VerifierFilter(pub);
verifierFilter->Put(signature, pub.SignatureLength());
FileSource f(messageFilename, true, verifierFilter);
if (m_waiting)
goto output;
return verifierFilter->GetLastResult();
while( size && !m_file.AtEOF() )
{
{
unsigned int spaceSize = 1024;
m_space = HelpCreatePutSpace(target, channel, 1, (unsigned int)STDMIN(size, (unsigned long)UINT_MAX), spaceSize);
m_len = m_file.Read( (char *)m_space, STDMIN(size, (unsigned long)spaceSize));
}
unsigned int blockedBytes;
output:
blockedBytes = target.ChannelPutModifiable2(channel, m_space, m_len, 0, blocking);
m_waiting = blockedBytes > 0;
if (m_waiting)
return blockedBytes;
size -= m_len;
transferBytes += m_len;
}
if (!m_file.AtEOF())
throw ReadErr();
return 0;
}
void DigestFile(const char *filename)
unsigned int RageFileStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const
{
MD5 md5;
HashFilter md5Filter(md5);
if( !m_file.IsOpen() )
return 0;
auto_ptr<ChannelSwitch> channelSwitch(new ChannelSwitch);
channelSwitch->AddDefaultRoute(md5Filter);
FileSource(filename, true, channelSwitch.release());
if (begin == 0 && end == 1)
{
int current = m_file.Tell();
byte result;
m_file.Read( &result, 1 );
if (m_file.AtEOF()) // GCC workaround: 2.95.2 doesn't have char_traits<char>::eof()
{
m_file.Seek( current );
return 0;
}
else
{
unsigned int blockedBytes = target.ChannelPut(channel, byte(result), blocking);
begin += 1-blockedBytes;
m_file.Seek( current );
return blockedBytes;
}
m_file.Seek( current );
}
HexEncoder encoder(new FileSink(cout), false);
cout << "\nMD5: ";
md5Filter.TransferTo(encoder);
// TODO: figure out what happens on cin
int current = m_file.Tell();
int endPosition = m_file.GetFileSize();
m_file.Seek( endPosition );
int newPosition = current + (streamoff)begin;
if (newPosition >= endPosition)
{
m_file.Seek(current);
return 0; // don't try to seek beyond the end of file
}
m_file.Seek(newPosition);
unsigned long total = 0;
try
{
assert(!m_waiting);
unsigned long copyMax = end-begin;
unsigned int blockedBytes = const_cast<RageFileStore *>(this)->TransferTo2(target, copyMax, channel, blocking);
begin += copyMax;
if (blockedBytes)
{
const_cast<RageFileStore *>(this)->m_waiting = false;
return blockedBytes;
}
}
catch(...)
{
m_file.ClearError();
m_file.Seek(current);
throw;
}
m_file.ClearError();
m_file.Seek(current);
return 0;
}
void RageFileSink::IsolatedInitialize(const NameValuePairs &parameters)
{
const char *fileName;
if (parameters.GetValue("OutputFileName", fileName))
{
ios::openmode binary = parameters.GetValueWithDefault("OutputBinaryMode", true) ? ios::binary : ios::openmode(0);
if( !m_file.Open( fileName, RageFile::WRITE ) ) // trucates existing data
throw OpenErr(fileName);
}
else
{
ASSERT(0);
}
}
bool RageFileSink::IsolatedFlush(bool hardFlush, bool blocking)
{
if (!m_file.IsOpen())
throw Err("FileSink: output stream not opened");
m_file.Flush();
if( !m_file.GetError().empty() )
throw WriteErr();
return false;
}
unsigned int RageFileSink::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
{
if (!m_file.IsOpen())
throw Err("FileSink: output stream not opened");
m_file.Write((const char *)inString, length);
if (messageEnd)
m_file.Flush();
if( !m_file.GetError().empty() )
throw WriteErr();
return 0;
}