download asn.h
Language: C++
LOC: 205
Project Info
NNIM
Server: SourceForge
Type: cvs
...rge\n\nnim\nnim\GNU\crypto\
   3desval.dat
   3way.cpp
   3way.h
   3wayval.dat
   adler32.cpp
   adler32.h
   aes.h
   algebra.cpp
   algebra.h
   arc4.cpp
   arc4.h
   asn.cpp
   asn.h
   base64.cpp
   base64.h
   bench.cpp
   bench.h
   bfinit.cpp
   blowfish.cpp
   blowfish.h
   blum1024.dat
   blum2048.dat
   blum512.dat
   blumgold.cpp
   blumgold.h
   blumshub.cpp
   blumshub.h
   cast.cpp
   cast.h
   cast128v.dat
   cast256v.dat
   casts.cpp
   cbc.cpp
   cbc.h
   cbcmac.h
   channels.cpp
   channels.h
   config.h
   crc.cpp
   crc.h
   cryptest.dsp
   cryptest.dsw
   cryptest.ncb
   cryptlib.cpp
   cryptlib.dsp
   cryptlib.dsw
   cryptlib.h
   cryptlib.sln
   cryptlib.vcproj
   default.cpp
   default.h
   des.cpp
   des.h
   descert.dat
   dessp.cpp
   dh.cpp
   dh.h
   dh1024.dat
   dh2.cpp
   dh2.h
   dh2048.dat
   dh512.dat
   diamond.cpp
   diamond.dat
   diamond.h
   diamondt.cpp
   dmac.h
   dsa.cpp
   dsa.h
   dsa1024.dat
   dsa1024b.dat
   dsa512.dat
   ec2n.cpp
   ec2n.h
   eccrypto.cpp
   eccrypto.h
   ecp.cpp
   ecp.h
   elgamal.cpp
   elgamal.h
   elgc1024.dat
   elgc2048.dat
   elgc512.dat
   eprecomp.cpp
   eprecomp.h
   files.cpp
   files.h
   filters.cpp
   filters.h
   gf2_32.cpp
   gf2_32.h
   gf256.cpp
   gf256.h
   gf2n.cpp
   gf2n.h
   gost.cpp
   gost.h
   gostval.dat
   gzip.cpp
   gzip.h
   haval.cpp
   haval.h
   havalcer.dat
   hex.cpp
   hex.h
   hmac.h
   hrtimer.cpp
   hrtimer.h
   ida.cpp
   ida.h
   idea.cpp
   idea.h
   ideaval.dat
   integer.cpp
   integer.h
   iterhash.cpp
   iterhash.h
   lubyrack.h
   luc.cpp
   luc.h
   luc1024.dat
   luc2048.dat
   luc512.dat
   lucc1024.dat
   lucc512.dat
   lucd1024.dat
   lucd512.dat
   lucs1024.dat
   lucs512.dat
   Makefile.am
   Makefile.in
   mars.cpp
   mars.h
   marss.cpp
   marsval.dat
   md2.cpp
   md2.h
   md4.cpp
   md4.h
   md5.cpp
   md5.h
   md5mac.cpp
   md5mac.h
   mdc.h
   misc.cpp
   misc.h
   modarith.h
   modes.cpp
   modes.h
   modexppc.cpp
   modexppc.h
   mqueue.cpp
   mqueue.h
   mqv.cpp
   mqv.h
   mqv1024.dat
   mqv2048.dat
   mqv512.dat
   nbtheory.cpp
   nbtheory.h
   network.cpp
   network.h
   nr.cpp
   nr.h
   nr1024.dat
   nr2048.dat
   nr512.dat
   oaep.cpp
   oaep.h
   oids.h
   osrng.cpp
   osrng.h
   panama.cpp
   panama.h
   pch.cpp
   pch.h
   pkcspad.cpp
   pkcspad.h
   polynomi.cpp
   polynomi.h
   pssr.h
   pubkey.cpp
   pubkey.h
   queue.cpp
   queue.h
   rabi1024.dat
   rabi2048.dat
   rabi512.dat
   rabin.cpp
   rabin.h
   randpool.cpp
   randpool.h
   rc2.cpp
   rc2.h
   rc2val.dat
   rc5.cpp
   rc5.h
   rc5val.dat
   rc6.cpp
   rc6.h
   rc6val.dat
   rdtables.cpp
   rijndael.cpp
   rijndael.dat
   rijndael.h
   ripemd.cpp
   ripemd.h
   rng.cpp
   rng.h
   rsa.cpp
   rsa.h
   rsa1024.dat
   rsa2048.dat
   rsa400pb.dat
   rsa400pv.dat
   rsa512.dat
   rsa512a.dat
   rw.cpp
   rw.h
   rw1024.dat
   rw2048.dat
   rw512.dat
   safer.cpp
   safer.h
   saferval.dat
   sapphire.cpp
   sapphire.h
   seal.cpp
   seal.h
   secshare.cpp
   secshare.h
   serpent.cpp
   serpent.h
   serpentv.dat
   sha.cpp
   sha.h
   shark.cpp
   shark.h
   sharkbox.cpp
   sharkval.dat
   skipjack.cpp
   skipjack.dat
   skipjack.h
   smartptr.h
   socketft.cpp
   socketft.h
   square.cpp
   square.h
   squaretb.cpp
   squareva.dat
   tea.cpp
   tea.h
   test.cpp
   tftables.cpp
   tiger.cpp
   tiger.h
   tigertab.cpp
   trunhash.h
   twofish.cpp
   twofish.h
   twofishv.dat
   usage.dat
   validat1.cpp
   validat2.cpp
   validat3.cpp
   validate.h
   wake.cpp
   wake.h
   winpipes.cpp
   winpipes.h
   words.h
   xormac.h
   xtr.cpp
   xtr.h
   xtrcrypt.cpp
   xtrcrypt.h
   xtrdh171.dat
   xtrdh342.dat
   zdeflate.cpp
   zdeflate.h
   zinflate.cpp
   zinflate.h
   zlib.cpp
   zlib.h

#ifndef CRYPTOPP_ASN_H
#define CRYPTOPP_ASN_H

#include "filters.h"
#include "queue.h"
#include <vector>

NAMESPACE_BEGIN(CryptoPP)

// these tags and flags are not complete
enum ASNTag
{
	BOOLEAN 			= 0x01,
	INTEGER 			= 0x02,
	BIT_STRING			= 0x03,
	OCTET_STRING		= 0x04,
	TAG_NULL			= 0x05,
	OBJECT_IDENTIFIER	= 0x06,
	OBJECT_DESCRIPTOR	= 0x07,
	EXTERNAL			= 0x08,
	REAL				= 0x09,
	ENUMERATED			= 0x0a,
	UTF8_STRING			= 0x0c,
	SEQUENCE			= 0x10,
	SET 				= 0x11,
	NUMERIC_STRING		= 0x12,
	PRINTABLE_STRING 	= 0x13,
	T61_STRING			= 0x14,
	VIDEOTEXT_STRING 	= 0x15,
	IA5_STRING			= 0x16,
	UTC_TIME 			= 0x17,
	GENERALIZED_TIME 	= 0x18,
	GRAPHIC_STRING		= 0x19,
	VISIBLE_STRING		= 0x1a,
	GENERAL_STRING		= 0x1b
};

enum ASNIdFlag
{
	UNIVERSAL			= 0x00,
	DATA				= 0x01,
	HEADER				= 0x02,
	CONSTRUCTED 		= 0x20,
	APPLICATION 		= 0x40,
	CONTEXT_SPECIFIC	= 0x80,
	PRIVATE 			= 0xc0
};

#define BERDecodeError() throw BERDecodeErr()

//! BER Decoder Exception Class  
class BERDecodeErr : public Exception
{
public: 
	BERDecodeErr() : Exception("BER decode error") {}
	BERDecodeErr(const char *err) : Exception(err) {}
};

class UnknownOID : public BERDecodeErr
{
public:
	UnknownOID() : BERDecodeErr("BER decode error: unknown object identifier") {}
	UnknownOID(const char *err) : BERDecodeErr(err) {}
};

// unsigned int DERLengthEncode(unsigned int length, byte *output=0);
unsigned int DERLengthEncode(BufferedTransformation &out, unsigned int length);
// returns false if indefinite length
bool BERLengthDecode(BufferedTransformation &in, unsigned int &length);

void DEREncodeNull(BufferedTransformation &out);
void BERDecodeNull(BufferedTransformation &in);

unsigned int DEREncodeOctetString(BufferedTransformation &out, const byte *str, unsigned int strLen);
unsigned int DEREncodeOctetString(BufferedTransformation &out, const SecByteBlock &str);
unsigned int BERDecodeOctetString(BufferedTransformation &in, SecByteBlock &str);
unsigned int BERDecodeOctetString(BufferedTransformation &in, BufferedTransformation &str);

// for UTF8_STRING, PRINTABLE_STRING, and IA5_STRING
unsigned int DEREncodeTextString(BufferedTransformation &out, const std::string &str, byte asnTag);
unsigned int BERDecodeTextString(BufferedTransformation &in, std::string &str, byte asnTag);

unsigned int DEREncodeBitString(BufferedTransformation &out, const byte *str, unsigned int strLen, unsigned int unusedBits=0);
unsigned int BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);

//! Object Identifier
class OID
{
public:
	OID() {}
	OID(unsigned long v) : m_values(1, v) {}
	OID(BufferedTransformation &bt) {BERDecode(bt);}

	bool operator==(const OID &rhs) const {return m_values == rhs.m_values;}
	bool operator!=(const OID &rhs) const {return !operator==(rhs);}
	bool operator<(const OID &rhs) const {return std::lexicographical_compare(m_values.begin(), m_values.end(), rhs.m_values.begin(), rhs.m_values.end());}

	inline OID & operator+=(unsigned long rhs) {m_values.push_back(rhs); return *this;}
	inline OID operator+(unsigned long rhs) const {return OID(*this)+=rhs;}

	void DEREncode(BufferedTransformation &bt) const;
	void BERDecode(BufferedTransformation &bt);

	// throw BERDecodeErr() if decoded value doesn't equal this OID
	void BERDecodeAndCheck(BufferedTransformation &bt) const;

	std::vector<unsigned long> m_values;

private:
	static void EncodeValue(BufferedTransformation &bt, unsigned long v);
	static unsigned int DecodeValue(BufferedTransformation &bt, unsigned long &v);
};

//! BER General Decoder
class BERGeneralDecoder : public Store
{
public:
	explicit BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag);
	explicit BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag);
	~BERGeneralDecoder();

	bool IsDefiniteLength() const {return m_definiteLength;}
	unsigned int RemainingLength() const {assert(m_definiteLength); return m_length;}
	bool EndReached() const;
	byte PeekByte() const;
	void CheckByte(byte b);

	unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax);
	unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax) const;

	// call this to denote end of sequence
	void MessageEnd(int=-1);

protected:
	BufferedTransformation &m_inQueue;
	bool m_finished, m_definiteLength;
	unsigned int m_length;

private:
	unsigned int ReduceLength(unsigned int delta);
};

//! DER General Encoder
class DERGeneralEncoder : public ByteQueue
{
public:
	explicit DERGeneralEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
	explicit DERGeneralEncoder(DERGeneralEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED);
	~DERGeneralEncoder();

	// call this to denote end of sequence
	void MessageEnd(int=-1);

private:
	BufferedTransformation &m_outQueue;
	bool m_finished;

	byte m_asnTag;
};

//! BER Sequence Decoder
class BERSequenceDecoder : public BERGeneralDecoder
{
public:
	explicit BERSequenceDecoder(BufferedTransformation &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
		: BERGeneralDecoder(inQueue, asnTag) {}
	explicit BERSequenceDecoder(BERSequenceDecoder &inQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
		: BERGeneralDecoder(inQueue, asnTag) {}
};

//! DER Sequence Encoder
class DERSequenceEncoder : public DERGeneralEncoder
{
public:
	explicit DERSequenceEncoder(BufferedTransformation &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
		: DERGeneralEncoder(outQueue, asnTag) {}
	explicit DERSequenceEncoder(DERSequenceEncoder &outQueue, byte asnTag = SEQUENCE | CONSTRUCTED)
		: DERGeneralEncoder(outQueue, asnTag) {}
};

//! BER Set Decoder
class BERSetDecoder : public BERGeneralDecoder
{
public:
	explicit BERSetDecoder(BufferedTransformation &inQueue, byte asnTag = SET | CONSTRUCTED)
		: BERGeneralDecoder(inQueue, asnTag) {}
	explicit BERSetDecoder(BERSetDecoder &inQueue, byte asnTag = SET | CONSTRUCTED)
		: BERGeneralDecoder(inQueue, asnTag) {}
};

//! DER Set Encoder
class DERSetEncoder : public DERGeneralEncoder
{
public:
	explicit DERSetEncoder(BufferedTransformation &outQueue, byte asnTag = SET | CONSTRUCTED)
		: DERGeneralEncoder(outQueue, asnTag) {}
	explicit DERSetEncoder(DERSetEncoder &outQueue, byte asnTag = SET | CONSTRUCTED)
		: DERGeneralEncoder(outQueue, asnTag) {}
};

// ********************************************************

//! DER Encode Unsigned
/*! for INTEGER, BOOLEAN, and ENUM */
template <class T>
unsigned int DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag = INTEGER)
{
	byte buf[sizeof(w)+1];
	unsigned int bc;
	if (asnTag == BOOLEAN)
	{
		buf[sizeof(w)] = w ? 0xff : 0;
		bc = 1;
	}
	else
	{
		buf[0] = 0;
		for (unsigned int i=0; i<sizeof(w); i++)
			buf[i+1] = byte(w >> (sizeof(w)-1-i)*8);
		bc = sizeof(w);
		while (bc > 1 && buf[sizeof(w)+1-bc] == 0)
			--bc;
		if (buf[sizeof(w)+1-bc] & 0x80)
			++bc;
	}
	out.Put(asnTag);
	unsigned int lengthBytes = DERLengthEncode(out, bc);
	out.Put(buf+sizeof(w)+1-bc, bc);
	return 1+lengthBytes+bc;
}

//! BER Decode Unsigned
// VC60 workaround: std::numeric_limits<T>::max conflicts with MFC max macro
// CW41 workaround: std::numeric_limits<T>::max causes a template error
template <class T>
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag = INTEGER,
					   T minValue = 0, T maxValue = 0xffffffff)
{
	byte b;
	if (!in.Get(b) || b != asnTag)
		BERDecodeError();

	unsigned int bc;
	BERLengthDecode(in, bc);

	SecByteBlock buf(bc);

	if (bc != in.Get(buf, bc))
		BERDecodeError();

	const byte *ptr = buf;
	while (bc > sizeof(w) && *ptr == 0)
	{
		bc--;
		ptr++;
	}
	if (bc > sizeof(w))
		BERDecodeError();

	w = 0;
	for (unsigned int i=0; i<bc; i++)
		w = (w << 8) | ptr[i];

	if (w < minValue || w > maxValue)
		BERDecodeError();
}

NAMESPACE_END

#endif

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us