6 #ifndef CRYPTOPP_IMPORTS 31 bt.Put(byte(length >> (j-1)*8));
47 definiteLength =
true;
52 unsigned int lengthBytes = b & 0x7f;
56 definiteLength =
false;
60 definiteLength =
true;
64 if (length >> (8*(
sizeof(length)-1)))
70 length = (length << 8) | b;
80 if (!BERLengthDecode(bt, lw, definiteLength))
84 return definiteLength;
96 if (!in.
Get(b) || b != TAG_NULL)
99 if (!BERLengthDecode(in, length) || length != 0)
106 bt.
Put(OCTET_STRING);
109 return 1+lengthBytes+strLen;
120 if (!bt.
Get(b) || b != OCTET_STRING)
124 if (!BERLengthDecode(bt, bc))
128 if (bc != bt.
Get(str, bc))
136 if (!bt.
Get(b) || b != OCTET_STRING)
140 if (!BERLengthDecode(bt, bc))
151 bt.
Put((
const byte *)str.data(), str.size());
152 return 1+lengthBytes+str.size();
158 if (!bt.
Get(b) || b != asnTag)
162 if (!BERLengthDecode(bt, bc))
166 if (bc != bt.
Get(temp, bc))
168 str.assign((
char *)temp.
begin(), bc);
177 bt.
Put((byte)unusedBits);
179 return 2+lengthBytes+strLen;
185 if (!bt.
Get(b) || b != BIT_STRING)
189 if (!BERLengthDecode(bt, bc))
197 if ((bc-1) != bt.
Get(str, bc-1))
208 if (decoder.IsDefiniteLength())
209 decoder.
TransferTo(encoder, decoder.RemainingLength());
212 while (!decoder.EndReached())
213 DERReencode(decoder, encoder);
215 decoder.MessageEnd();
216 encoder.MessageEnd();
222 bt.
Put((byte)(0x80 | ((v >> i) & 0x7f)));
223 bt.
Put((byte)(v & 0x7f));
236 if (v >> (8*
sizeof(v)-7))
247 assert(m_values.size() >= 2);
249 temp.
Put(byte(m_values[0] * 40 + m_values[1]));
250 for (
size_t i=2; i<m_values.size(); i++)
251 EncodeValue(temp, m_values[i]);
252 bt.
Put(OBJECT_IDENTIFIER);
260 if (!bt.
Get(b) || b != OBJECT_IDENTIFIER)
264 if (!BERLengthDecode(bt, length) || length < 1)
272 m_values[0] = b / 40;
273 m_values[1] = b % 40;
278 size_t valueLen = DecodeValue(bt, v);
279 if (valueLen > length)
281 m_values.push_back(v);
295 if (m_flags & PUT_OBJECTS)
296 return *AttachedTransformation();
301 void EncodedObjectFilter::Put(
const byte *inString,
size_t length)
303 if (m_nCurrentObject == m_nObjects)
305 AttachedTransformation()->Put(inString, length);
309 LazyPutter lazyPutter(m_queue, inString, length);
311 while (m_queue.AnyRetrievable())
316 if (!m_queue.Get(m_id))
318 m_queue.TransferTo(CurrentTarget(), 1);
323 if (m_level > 0 && m_id == 0 && m_queue.Peek(b) && b == 0)
325 m_queue.TransferTo(CurrentTarget(), 1);
327 m_state = IDENTIFIER;
332 if (!BERLengthDecode(walker, m_lengthRemaining, definiteLength))
334 m_queue.TransferTo(CurrentTarget(), walker.GetCurrentPosition());
335 if (!((m_id & CONSTRUCTED) || definiteLength))
339 if (!(m_id & CONSTRUCTED))
342 m_state = IDENTIFIER;
348 m_lengthRemaining -= m_queue.TransferTo(CurrentTarget(), m_lengthRemaining);
350 if (m_lengthRemaining == 0)
351 m_state = IDENTIFIER;
358 if (m_state == IDENTIFIER && m_level == 0)
363 if (m_flags & PUT_MESSANGE_END_AFTER_EACH_OBJECT)
364 AttachedTransformation()->MessageEnd();
366 if (m_nCurrentObject == m_nObjects)
368 if (m_flags & PUT_MESSANGE_END_AFTER_ALL_OBJECTS)
369 AttachedTransformation()->MessageEnd();
371 if (m_flags & PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS)
372 AttachedTransformation()->MessageSeriesEnd();
374 m_queue.TransferAllTo(*AttachedTransformation());
382 : m_inQueue(inQueue), m_finished(
false)
388 : m_inQueue(inQueue), m_finished(
false)
393 void BERGeneralDecoder::Init(byte asnTag)
396 if (!m_inQueue.Get(b) || b != asnTag)
399 if (!BERLengthDecode(m_inQueue, m_length, m_definiteLength))
402 if (!m_definiteLength && !(asnTag & CONSTRUCTED))
406 BERGeneralDecoder::~BERGeneralDecoder()
419 bool BERGeneralDecoder::EndReached()
const 421 if (m_definiteLength)
422 return m_length == 0;
426 return (m_inQueue.PeekWord16(i)==2 && i==0);
430 byte BERGeneralDecoder::PeekByte()
const 438 void BERGeneralDecoder::CheckByte(byte check)
441 if (!Get(b) || b != check)
445 void BERGeneralDecoder::MessageEnd()
448 if (m_definiteLength)
456 if (m_inQueue.GetWord16(i) != 2 || i != 0)
463 if (m_definiteLength && transferBytes > m_length)
464 transferBytes = m_length;
465 size_t blockedBytes = m_inQueue.TransferTo2(target, transferBytes, channel, blocking);
466 ReduceLength(transferBytes);
472 if (m_definiteLength)
473 end =
STDMIN(m_length, end);
474 return m_inQueue.CopyRangeTo2(target, begin, end, channel, blocking);
477 lword BERGeneralDecoder::ReduceLength(lword delta)
479 if (m_definiteLength)
481 if (m_length < delta)
489 :
ByteQueue(), m_outQueue(outQueue), m_finished(
false), m_asnTag(asnTag)
496 :
ByteQueue(), m_outQueue(outQueue), m_finished(
false), m_asnTag(asnTag)
500 DERGeneralEncoder::~DERGeneralEncoder()
513 void DERGeneralEncoder::MessageEnd()
516 lword length = CurrentSize();
517 m_outQueue.Put(m_asnTag);
519 TransferTo(m_outQueue);
528 GetAlgorithmID().BERDecodeAndCheck(algorithm);
529 bool parametersPresent = algorithm.EndReached() ?
false : BERDecodeAlgorithmParameters(algorithm);
530 algorithm.MessageEnd();
533 subjectPublicKey.CheckByte(0);
534 BERDecodePublicKey(subjectPublicKey, parametersPresent, (
size_t)subjectPublicKey.RemainingLength());
535 subjectPublicKey.MessageEnd();
536 subjectPublicKeyInfo.MessageEnd();
544 GetAlgorithmID().DEREncode(algorithm);
545 DEREncodeAlgorithmParameters(algorithm);
546 algorithm.MessageEnd();
549 subjectPublicKey.
Put(0);
550 DEREncodePublicKey(subjectPublicKey);
551 subjectPublicKey.MessageEnd();
553 subjectPublicKeyInfo.MessageEnd();
560 BERDecodeUnsigned<word32>(privateKeyInfo, version, INTEGER, 0, 0);
563 GetAlgorithmID().BERDecodeAndCheck(algorithm);
564 bool parametersPresent = algorithm.EndReached() ?
false : BERDecodeAlgorithmParameters(algorithm);
565 algorithm.MessageEnd();
568 BERDecodePrivateKey(octetString, parametersPresent, (
size_t)privateKeyInfo.RemainingLength());
569 octetString.MessageEnd();
571 if (!privateKeyInfo.EndReached())
572 BERDecodeOptionalAttributes(privateKeyInfo);
573 privateKeyInfo.MessageEnd();
579 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
582 GetAlgorithmID().DEREncode(algorithm);
583 DEREncodeAlgorithmParameters(algorithm);
584 algorithm.MessageEnd();
587 DEREncodePrivateKey(octetString);
588 octetString.MessageEnd();
590 DEREncodeOptionalAttributes(privateKeyInfo);
591 privateKeyInfo.MessageEnd();
596 DERReencode(bt, m_optionalAttributes);
601 m_optionalAttributes.CopyTo(bt);
Base class for all exceptions thrown by Crypto++.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from → to is safe to perform.
use this to make sure LazyPut is finalized in event of exception
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
void resize(size_type newSize)
Change size and preserve contents.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
BufferedTransformation & TheBitBucket()
An input discarding BufferedTransformation.
size_t DERLengthEncode(BufferedTransformation &out, lword length)
DER Length.
size_type size() const
Provides the count of elements in the SecBlock.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Library configuration file.
void New(size_type newSize)
Change size without preserving contents.
SecByteBlock is a SecBlock<byte> typedef.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const
encode optional attributes including context-specific tag
size_t DEREncodeBitString(BufferedTransformation &out, const byte *str, size_t strLen, unsigned int unusedBits=0)
ASN BitString.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Classes and functions for working with ANS.1 objects.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
size_t DEREncodeOctetString(BufferedTransformation &out, const byte *str, size_t strLen)
ASN Strings.
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
decode optional attributes including context-specific tag
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Crypto++ library namespace.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.