fix encoding/decoding of optional attributes
parent
60a5c4331c
commit
40a5b80a45
41
asn.cpp
41
asn.cpp
|
|
@ -195,6 +195,23 @@ unsigned int BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, u
|
||||||
return bc-1;
|
return bc-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DERReencode(BufferedTransformation &source, BufferedTransformation &dest)
|
||||||
|
{
|
||||||
|
byte tag;
|
||||||
|
source.Peek(tag);
|
||||||
|
BERGeneralDecoder decoder(source, tag);
|
||||||
|
DERGeneralEncoder encoder(dest, tag);
|
||||||
|
if (decoder.IsDefiniteLength())
|
||||||
|
decoder.TransferTo(encoder, decoder.RemainingLength());
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (!decoder.EndReached())
|
||||||
|
DERReencode(decoder, encoder);
|
||||||
|
}
|
||||||
|
decoder.MessageEnd();
|
||||||
|
encoder.MessageEnd();
|
||||||
|
}
|
||||||
|
|
||||||
void OID::EncodeValue(BufferedTransformation &bt, unsigned long v)
|
void OID::EncodeValue(BufferedTransformation &bt, unsigned long v)
|
||||||
{
|
{
|
||||||
for (unsigned int i=RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U)-7; i != 0; i-=7)
|
for (unsigned int i=RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U)-7; i != 0; i-=7)
|
||||||
|
|
@ -354,15 +371,16 @@ void EncodedObjectFilter::Put(const byte *inString, unsigned int length)
|
||||||
BERGeneralDecoder::BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag)
|
BERGeneralDecoder::BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag)
|
||||||
: m_inQueue(inQueue), m_finished(false)
|
: m_inQueue(inQueue), m_finished(false)
|
||||||
{
|
{
|
||||||
byte b;
|
Init(asnTag);
|
||||||
if (!m_inQueue.Get(b) || b != asnTag)
|
|
||||||
BERDecodeError();
|
|
||||||
|
|
||||||
m_definiteLength = BERLengthDecode(m_inQueue, m_length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BERGeneralDecoder::BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag)
|
BERGeneralDecoder::BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag)
|
||||||
: m_inQueue(inQueue), m_finished(false)
|
: m_inQueue(inQueue), m_finished(false)
|
||||||
|
{
|
||||||
|
Init(asnTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BERGeneralDecoder::Init(byte asnTag)
|
||||||
{
|
{
|
||||||
byte b;
|
byte b;
|
||||||
if (!m_inQueue.Get(b) || b != asnTag)
|
if (!m_inQueue.Get(b) || b != asnTag)
|
||||||
|
|
@ -370,7 +388,7 @@ BERGeneralDecoder::BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag)
|
||||||
|
|
||||||
m_definiteLength = BERLengthDecode(m_inQueue, m_length);
|
m_definiteLength = BERLengthDecode(m_inQueue, m_length);
|
||||||
if (!m_definiteLength && !(asnTag & CONSTRUCTED))
|
if (!m_definiteLength && !(asnTag & CONSTRUCTED))
|
||||||
BERDecodeError(); // cannot be primitive have indefinite length
|
BERDecodeError(); // cannot be primitive and have indefinite length
|
||||||
}
|
}
|
||||||
|
|
||||||
BERGeneralDecoder::~BERGeneralDecoder()
|
BERGeneralDecoder::~BERGeneralDecoder()
|
||||||
|
|
@ -534,6 +552,7 @@ void PKCS8PrivateKey::BERDecode(BufferedTransformation &bt)
|
||||||
BERDecodeKey2(octetString, parametersPresent, privateKeyInfo.RemainingLength());
|
BERDecodeKey2(octetString, parametersPresent, privateKeyInfo.RemainingLength());
|
||||||
octetString.MessageEnd();
|
octetString.MessageEnd();
|
||||||
|
|
||||||
|
if (!privateKeyInfo.EndReached())
|
||||||
BERDecodeOptionalAttributes(privateKeyInfo);
|
BERDecodeOptionalAttributes(privateKeyInfo);
|
||||||
privateKeyInfo.MessageEnd();
|
privateKeyInfo.MessageEnd();
|
||||||
}
|
}
|
||||||
|
|
@ -556,6 +575,16 @@ void PKCS8PrivateKey::DEREncode(BufferedTransformation &bt) const
|
||||||
privateKeyInfo.MessageEnd();
|
privateKeyInfo.MessageEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PKCS8PrivateKey::BERDecodeOptionalAttributes(BufferedTransformation &bt)
|
||||||
|
{
|
||||||
|
DERReencode(bt, m_optionalAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PKCS8PrivateKey::DEREncodeOptionalAttributes(BufferedTransformation &bt) const
|
||||||
|
{
|
||||||
|
m_optionalAttributes.CopyTo(bt);
|
||||||
|
}
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
20
asn.h
20
asn.h
|
|
@ -75,6 +75,9 @@ CRYPTOPP_DLL unsigned int BERDecodeTextString(BufferedTransformation &in, std::s
|
||||||
CRYPTOPP_DLL unsigned int DEREncodeBitString(BufferedTransformation &out, const byte *str, unsigned int strLen, unsigned int unusedBits=0);
|
CRYPTOPP_DLL unsigned int DEREncodeBitString(BufferedTransformation &out, const byte *str, unsigned int strLen, unsigned int unusedBits=0);
|
||||||
CRYPTOPP_DLL unsigned int BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);
|
CRYPTOPP_DLL unsigned int BERDecodeBitString(BufferedTransformation &in, SecByteBlock &str, unsigned int &unusedBits);
|
||||||
|
|
||||||
|
// BER decode from source and DER reencode into dest
|
||||||
|
CRYPTOPP_DLL void DERReencode(BufferedTransformation &source, BufferedTransformation &dest);
|
||||||
|
|
||||||
//! Object Identifier
|
//! Object Identifier
|
||||||
class CRYPTOPP_DLL OID
|
class CRYPTOPP_DLL OID
|
||||||
{
|
{
|
||||||
|
|
@ -147,6 +150,7 @@ protected:
|
||||||
unsigned int m_length;
|
unsigned int m_length;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void Init(byte asnTag);
|
||||||
void StoreInitialize(const NameValuePairs ¶meters) {assert(false);}
|
void StoreInitialize(const NameValuePairs ¶meters) {assert(false);}
|
||||||
unsigned int ReduceLength(unsigned int delta);
|
unsigned int ReduceLength(unsigned int delta);
|
||||||
};
|
};
|
||||||
|
|
@ -221,8 +225,8 @@ public:
|
||||||
}
|
}
|
||||||
void DEREncode(BufferedTransformation &out)
|
void DEREncode(BufferedTransformation &out)
|
||||||
{
|
{
|
||||||
if (get() != NULL)
|
if (this->get() != NULL)
|
||||||
get()->DEREncode(out);
|
this->get()->DEREncode(out);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -259,10 +263,14 @@ public:
|
||||||
void BERDecode(BufferedTransformation &bt);
|
void BERDecode(BufferedTransformation &bt);
|
||||||
void DEREncode(BufferedTransformation &bt) const;
|
void DEREncode(BufferedTransformation &bt) const;
|
||||||
|
|
||||||
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
|
//! decode optional attributes including context-specific tag
|
||||||
{} // TODO: skip optional attributes if present
|
/*! /note default implementation stores attributes to be output in DEREncodeOptionalAttributes */
|
||||||
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const
|
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt);
|
||||||
{}
|
//! encode optional attributes including context-specific tag
|
||||||
|
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ByteQueue m_optionalAttributes;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ********************************************************
|
// ********************************************************
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue