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;
84 return definiteLength;
96 if (!in.
Get(b) || b != TAG_NULL)
106 bt.
Put(OCTET_STRING);
109 return 1+lengthBytes+strLen;
120 if (!bt.
Get(b) || b != OCTET_STRING)
130 if (bc != bt.
Get(str, bc))
138 if (!bt.
Get(b) || b != OCTET_STRING)
155 bt.
Put((
const byte *)str.data(), str.size());
156 return 1+lengthBytes+str.size();
162 if (!bt.
Get(b) || b != asnTag)
172 if (bc != bt.
Get(temp, bc))
174 str.assign((
char *)temp.
begin(), bc);
183 bt.
Put((byte)unusedBits);
185 return 2+lengthBytes+strLen;
191 if (!bt.
Get(b) || b != BIT_STRING)
207 if ((bc-1) != bt.
Get(str, bc-1))
218 if (decoder.IsDefiniteLength())
219 decoder.
TransferTo(encoder, decoder.RemainingLength());
222 while (!decoder.EndReached())
225 decoder.MessageEnd();
226 encoder.MessageEnd();
232 bt.
Put((byte)(0x80 | ((v >> i) & 0x7f)));
233 bt.
Put((byte)(v & 0x7f));
246 if (v >> (8*
sizeof(v)-7))
257 assert(m_values.size() >= 2);
259 temp.
Put(byte(m_values[0] * 40 + m_values[1]));
260 for (
size_t i=2; i<m_values.size(); i++)
261 EncodeValue(temp, m_values[i]);
262 bt.
Put(OBJECT_IDENTIFIER);
270 if (!bt.
Get(b) || b != OBJECT_IDENTIFIER)
282 m_values[0] = b / 40;
283 m_values[1] = b % 40;
288 size_t valueLen = DecodeValue(bt, v);
289 if (valueLen > length)
291 m_values.push_back(v);
305 if (m_flags & PUT_OBJECTS)
306 return *AttachedTransformation();
313 if (m_nCurrentObject == m_nObjects)
315 AttachedTransformation()->Put(inString, length);
319 LazyPutter lazyPutter(m_queue, inString, length);
321 while (m_queue.AnyRetrievable())
326 if (!m_queue.Get(m_id))
328 m_queue.TransferTo(CurrentTarget(), 1);
333 if (m_level > 0 && m_id == 0 && m_queue.Peek(b) && b == 0)
335 m_queue.TransferTo(CurrentTarget(), 1);
337 m_state = IDENTIFIER;
344 m_queue.TransferTo(CurrentTarget(), walker.GetCurrentPosition());
345 if (!((m_id & CONSTRUCTED) || definiteLength))
349 if (!(m_id & CONSTRUCTED))
352 m_state = IDENTIFIER;
358 m_lengthRemaining -= m_queue.TransferTo(CurrentTarget(), m_lengthRemaining);
360 if (m_lengthRemaining == 0)
361 m_state = IDENTIFIER;
368 if (m_state == IDENTIFIER && m_level == 0)
373 if (m_flags & PUT_MESSANGE_END_AFTER_EACH_OBJECT)
374 AttachedTransformation()->MessageEnd();
376 if (m_nCurrentObject == m_nObjects)
378 if (m_flags & PUT_MESSANGE_END_AFTER_ALL_OBJECTS)
379 AttachedTransformation()->MessageEnd();
381 if (m_flags & PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS)
382 AttachedTransformation()->MessageSeriesEnd();
384 m_queue.TransferAllTo(*AttachedTransformation());
392 : m_inQueue(inQueue), m_finished(
false)
398 : m_inQueue(inQueue), m_finished(
false)
403 void BERGeneralDecoder::Init(byte asnTag)
406 if (!m_inQueue.Get(b) || b != asnTag)
412 if (!m_definiteLength && !(asnTag & CONSTRUCTED))
416 BERGeneralDecoder::~BERGeneralDecoder()
429 bool BERGeneralDecoder::EndReached()
const 431 if (m_definiteLength)
432 return m_length == 0;
436 return (m_inQueue.PeekWord16(i)==2 && i==0);
440 byte BERGeneralDecoder::PeekByte()
const 448 void BERGeneralDecoder::CheckByte(byte check)
451 if (!Get(b) || b != check)
455 void BERGeneralDecoder::MessageEnd()
458 if (m_definiteLength)
466 if (m_inQueue.GetWord16(i) != 2 || i != 0)
473 if (m_definiteLength && transferBytes > m_length)
474 transferBytes = m_length;
475 size_t blockedBytes = m_inQueue.TransferTo2(target, transferBytes, channel, blocking);
476 ReduceLength(transferBytes);
482 if (m_definiteLength)
483 end =
STDMIN(m_length, end);
484 return m_inQueue.CopyRangeTo2(target, begin, end, channel, blocking);
487 lword BERGeneralDecoder::ReduceLength(lword delta)
489 if (m_definiteLength)
491 if (m_length < delta)
499 :
ByteQueue(), m_outQueue(outQueue), m_finished(
false), m_asnTag(asnTag)
506 :
ByteQueue(), m_outQueue(outQueue), m_finished(
false), m_asnTag(asnTag)
510 DERGeneralEncoder::~DERGeneralEncoder()
523 void DERGeneralEncoder::MessageEnd()
526 lword length = CurrentSize();
527 m_outQueue.Put(m_asnTag);
529 TransferTo(m_outQueue);
538 GetAlgorithmID().BERDecodeAndCheck(algorithm);
539 bool parametersPresent = algorithm.EndReached() ?
false : BERDecodeAlgorithmParameters(algorithm);
540 algorithm.MessageEnd();
543 subjectPublicKey.CheckByte(0);
544 BERDecodePublicKey(subjectPublicKey, parametersPresent, (
size_t)subjectPublicKey.RemainingLength());
545 subjectPublicKey.MessageEnd();
546 subjectPublicKeyInfo.MessageEnd();
554 GetAlgorithmID().DEREncode(algorithm);
555 DEREncodeAlgorithmParameters(algorithm);
556 algorithm.MessageEnd();
559 subjectPublicKey.
Put(0);
560 DEREncodePublicKey(subjectPublicKey);
561 subjectPublicKey.MessageEnd();
563 subjectPublicKeyInfo.MessageEnd();
570 BERDecodeUnsigned<word32>(privateKeyInfo, version, INTEGER, 0, 0);
573 GetAlgorithmID().BERDecodeAndCheck(algorithm);
574 bool parametersPresent = algorithm.EndReached() ?
false : BERDecodeAlgorithmParameters(algorithm);
575 algorithm.MessageEnd();
578 BERDecodePrivateKey(octetString, parametersPresent, (
size_t)privateKeyInfo.RemainingLength());
579 octetString.MessageEnd();
581 if (!privateKeyInfo.EndReached())
582 BERDecodeOptionalAttributes(privateKeyInfo);
583 privateKeyInfo.MessageEnd();
589 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
592 GetAlgorithmID().DEREncode(algorithm);
593 DEREncodeAlgorithmParameters(algorithm);
594 algorithm.MessageEnd();
597 DEREncodePrivateKey(octetString);
598 octetString.MessageEnd();
600 DEREncodeOptionalAttributes(privateKeyInfo);
601 privateKeyInfo.MessageEnd();
611 m_optionalAttributes.CopyTo(bt);
Base class for all exceptions thrown by the library.
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const
encode optional attributes including context-specific tag
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
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.
void DEREncodeNull(BufferedTransformation &bt)
DER encode NULL.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
BufferedTransformation & TheBitBucket()
An input discarding BufferedTransformation.
Library configuration file.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
void New(size_type newSize)
Change size without preserving contents.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
void BERDecodeNull(BufferedTransformation &bt)
BER decode NULL.
size_t BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BER decode octet string.
void DEREncode(BufferedTransformation &bt) const
DER encode this OID.
size_t DEREncodeTextString(BufferedTransformation &bt, const std::string &str, byte asnTag)
DER encode text string.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
void BERDecode(BufferedTransformation &bt)
BER decode an OID.
void BERDecodeError()
Raises a BERDecodeErr.
Data structure used to store byte strings.
Classes and functions for working with ANS.1 objects.
size_t DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
void Put(const byte *inString, size_t length)
Input a byte buffer for processing.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
void BERDecodeAndCheck(BufferedTransformation &bt) const
BER decode an OID.
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
decode optional attributes including context-specific tag
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
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.
size_t DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen)
DER encode octet string.
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.
size_t BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits)
DER decode bit string.
Crypto++ library namespace.
size_t BERDecodeTextString(BufferedTransformation &bt, std::string &str, byte asnTag)
BER decode text string.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
size_type size() const
Provides the count of elements in the SecBlock.
bool BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
size_t DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0)
DER encode bit string.