summaryrefslogtreecommitdiff
path: root/asn.cpp
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2004-06-19 08:26:29 +0000
committerweidai <weidai11@users.noreply.github.com>2004-06-19 08:26:29 +0000
commit40a5b80a4522a9b3ad3820b5a953bb4f3611e009 (patch)
tree413c52db9c4dd5123bf22d4ff9f1e6026353c905 /asn.cpp
parent60a5c4331c83e8a4fc6b4399e97263a0b9f14a8b (diff)
downloadcryptopp-git-40a5b80a4522a9b3ad3820b5a953bb4f3611e009.tar.gz
fix encoding/decoding of optional attributes
Diffstat (limited to 'asn.cpp')
-rw-r--r--asn.cpp43
1 files changed, 36 insertions, 7 deletions
diff --git a/asn.cpp b/asn.cpp
index 41211ce2..272cec37 100644
--- a/asn.cpp
+++ b/asn.cpp
@@ -195,6 +195,23 @@ unsigned int BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, u
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)
{
for (unsigned int i=RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U)-7; i != 0; i-=7)
@@ -354,23 +371,24 @@ void EncodedObjectFilter::Put(const byte *inString, unsigned int length)
BERGeneralDecoder::BERGeneralDecoder(BufferedTransformation &inQueue, byte asnTag)
: m_inQueue(inQueue), m_finished(false)
{
- byte b;
- if (!m_inQueue.Get(b) || b != asnTag)
- BERDecodeError();
-
- m_definiteLength = BERLengthDecode(m_inQueue, m_length);
+ Init(asnTag);
}
BERGeneralDecoder::BERGeneralDecoder(BERGeneralDecoder &inQueue, byte asnTag)
: m_inQueue(inQueue), m_finished(false)
{
+ Init(asnTag);
+}
+
+void BERGeneralDecoder::Init(byte asnTag)
+{
byte b;
if (!m_inQueue.Get(b) || b != asnTag)
BERDecodeError();
m_definiteLength = BERLengthDecode(m_inQueue, m_length);
if (!m_definiteLength && !(asnTag & CONSTRUCTED))
- BERDecodeError(); // cannot be primitive have indefinite length
+ BERDecodeError(); // cannot be primitive and have indefinite length
}
BERGeneralDecoder::~BERGeneralDecoder()
@@ -534,7 +552,8 @@ void PKCS8PrivateKey::BERDecode(BufferedTransformation &bt)
BERDecodeKey2(octetString, parametersPresent, privateKeyInfo.RemainingLength());
octetString.MessageEnd();
- BERDecodeOptionalAttributes(privateKeyInfo);
+ if (!privateKeyInfo.EndReached())
+ BERDecodeOptionalAttributes(privateKeyInfo);
privateKeyInfo.MessageEnd();
}
@@ -556,6 +575,16 @@ void PKCS8PrivateKey::DEREncode(BufferedTransformation &bt) const
privateKeyInfo.MessageEnd();
}
+void PKCS8PrivateKey::BERDecodeOptionalAttributes(BufferedTransformation &bt)
+{
+ DERReencode(bt, m_optionalAttributes);
+}
+
+void PKCS8PrivateKey::DEREncodeOptionalAttributes(BufferedTransformation &bt) const
+{
+ m_optionalAttributes.CopyTo(bt);
+}
+
NAMESPACE_END
#endif