7#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;
79 bool definiteLength =
false;
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);
107 size_t lengthBytes = DERLengthEncode(bt, strLen);
109 return 1+lengthBytes+strLen;
114 return DEREncodeOctetString(bt, str.
begin(), str.
size());
120 if (!bt.
Get(b) || b != OCTET_STRING)
124 if (!BERLengthDecode(bt, bc))
130 if (bc != bt.
Get(str, bc))
138 if (!bt.
Get(b) || b != OCTET_STRING)
142 if (!BERLengthDecode(bt, bc))
154 size_t lengthBytes = DERLengthEncode(bt, str.size());
155 bt.
Put((
const byte *)str.data(), str.size());
156 return 1+lengthBytes+str.size();
162 if (!bt.
Get(b) || b != asnTag)
166 if (!BERLengthDecode(bt, bc))
172 if (bc != bt.
Get(temp, bc))
175 str.assign((
char *)temp.
begin(), bc);
185 size_t lengthBytes = DERLengthEncode(bt, strLen+1);
186 bt.
Put((
byte)unusedBits);
188 return 2+lengthBytes+strLen;
194 if (!bt.
Get(b) || b != BIT_STRING)
198 if (!BERLengthDecode(bt, bc))
207 if (!bt.
Get(unused) || unused > 7)
211 if ((bc-1) != bt.
Get(str, bc-1))
222 if (decoder.IsDefiniteLength())
223 decoder.
TransferTo(encoder, decoder.RemainingLength());
226 while (!decoder.EndReached())
227 DERReencode(decoder, encoder);
229 decoder.MessageEnd();
230 encoder.MessageEnd();
236 bt.
Put((
byte)(0x80 | ((v >> i) & 0x7f)));
237 bt.
Put((
byte)(v & 0x7f));
250 if (v >> (8*
sizeof(v)-7))
263 temp.
Put(
byte(m_values[0] * 40 + m_values[1]));
264 for (
size_t i=2; i<m_values.size(); i++)
265 EncodeValue(temp, m_values[i]);
266 bt.
Put(OBJECT_IDENTIFIER);
274 if (!bt.
Get(b) || b != OBJECT_IDENTIFIER)
286 m_values[0] = b / 40;
287 m_values[1] = b % 40;
292 size_t valueLen = DecodeValue(bt, v);
293 if (valueLen > length)
295 m_values.push_back(v);
309 if (m_flags & PUT_OBJECTS)
312 return TheBitBucket();
317 if (m_nCurrentObject == m_nObjects)
323 LazyPutter lazyPutter(m_queue, inString, length);
330 if (!m_queue.
Get(m_id))
338 if (m_level > 0 && m_id == 0 && m_queue.
Peek(b) && b == 0)
342 m_state = IDENTIFIER;
346 bool definiteLength =
false;
349 m_queue.
TransferTo(CurrentTarget(), walker.GetCurrentPosition());
350 if (!((m_id & CONSTRUCTED) || definiteLength))
354 if (!(m_id & CONSTRUCTED))
357 m_state = IDENTIFIER;
364 m_lengthRemaining -= m_queue.
TransferTo(CurrentTarget(), m_lengthRemaining);
366 if (m_lengthRemaining == 0)
367 m_state = IDENTIFIER;
374 if (m_state == IDENTIFIER && m_level == 0)
379 if (m_flags & PUT_MESSANGE_END_AFTER_EACH_OBJECT)
382 if (m_nCurrentObject == m_nObjects)
384 if (m_flags & PUT_MESSANGE_END_AFTER_ALL_OBJECTS)
387 if (m_flags & PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS)
398 : m_inQueue(inQueue), m_finished(false)
404 : m_inQueue(inQueue), m_finished(false)
409void BERGeneralDecoder::Init(
byte asnTag)
412 if (!m_inQueue.
Get(b) || b != asnTag)
418 if (!m_definiteLength && !(asnTag & CONSTRUCTED))
422BERGeneralDecoder::~BERGeneralDecoder()
435bool BERGeneralDecoder::EndReached()
const
437 if (m_definiteLength)
438 return m_length == 0;
446byte BERGeneralDecoder::PeekByte()
const
454void BERGeneralDecoder::CheckByte(
byte check)
457 if (!
Get(b) || b != check)
461void BERGeneralDecoder::MessageEnd()
464 if (m_definiteLength)
472 if (m_inQueue.
GetWord16(i) != 2 || i != 0)
479 if (m_definiteLength && transferBytes > m_length)
480 transferBytes = m_length;
481 size_t blockedBytes = m_inQueue.
TransferTo2(target, transferBytes, channel, blocking);
482 ReduceLength(transferBytes);
488 if (m_definiteLength)
489 end =
STDMIN(m_length, end);
490 return m_inQueue.
CopyRangeTo2(target, begin, end, channel, blocking);
493lword BERGeneralDecoder::ReduceLength(lword delta)
495 if (m_definiteLength)
497 if (m_length < delta)
505 :
ByteQueue(), m_outQueue(outQueue), m_asnTag(asnTag), m_finished(false)
510 :
ByteQueue(), m_outQueue(outQueue), m_asnTag(asnTag), m_finished(false)
514DERGeneralEncoder::~DERGeneralEncoder()
527void DERGeneralEncoder::MessageEnd()
530 lword length = CurrentSize();
531 m_outQueue.
Put(m_asnTag);
543 bool parametersPresent = algorithm.EndReached() ? false : BERDecodeAlgorithmParameters(algorithm);
544 algorithm.MessageEnd();
547 subjectPublicKey.CheckByte(0);
548 BERDecodePublicKey(subjectPublicKey, parametersPresent, (
size_t)subjectPublicKey.RemainingLength());
549 subjectPublicKey.MessageEnd();
550 subjectPublicKeyInfo.MessageEnd();
559 DEREncodeAlgorithmParameters(algorithm);
560 algorithm.MessageEnd();
563 subjectPublicKey.
Put(0);
565 subjectPublicKey.MessageEnd();
567 subjectPublicKeyInfo.MessageEnd();
574 BERDecodeUnsigned<word32>(privateKeyInfo, version, INTEGER, 0, 0);
578 bool parametersPresent = algorithm.EndReached() ? false : BERDecodeAlgorithmParameters(algorithm);
579 algorithm.MessageEnd();
582 BERDecodePrivateKey(octetString, parametersPresent, (
size_t)privateKeyInfo.RemainingLength());
583 octetString.MessageEnd();
585 if (!privateKeyInfo.EndReached())
587 privateKeyInfo.MessageEnd();
593 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
597 DEREncodeAlgorithmParameters(algorithm);
598 algorithm.MessageEnd();
602 octetString.MessageEnd();
605 privateKeyInfo.MessageEnd();
615 m_optionalAttributes.
CopyTo(bt);
Classes and functions for working with ANS.1 objects.
bool BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
size_t DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
void DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
void BERDecodeError()
Raises a BERDecodeErr.
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another 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.
Data structure used to store byte strings.
size_t Get(byte &outByte)
Retrieve a 8-bit byte.
size_t Peek(byte &outByte) const
Peek a 8-bit byte.
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
void Put(const byte *inString, size_t length)
Input a byte buffer for processing.
Base class for all exceptions thrown by the library.
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
use this to make sure LazyPut is finalized in event of exception
void BERDecodeAndCheck(BufferedTransformation &bt) const
BER decode an OID.
void DEREncode(BufferedTransformation &bt) const
DER encode this OID.
void BERDecode(BufferedTransformation &bt)
BER decode an OID.
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
encode privateKey part of privateKeyInfo, without the OCTET STRING header
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
decode optional attributes including context-specific tag
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
virtual OID GetAlgorithmID() const =0
Retrieves the OID of the algorithm.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const
encode optional attributes including context-specific tag
virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)=0
decode privateKey part of privateKeyInfo, without the OCTET STRING header
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void New(size_type newSize)
Change size without preserving contents.
size_type size() const
Provides the count of elements in the SecBlock.
void resize(size_type newSize)
Change size and preserve contents.
virtual OID GetAlgorithmID() const =0
Retrieves the OID of the algorithm.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)=0
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
Library configuration file.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Crypto++ library namespace.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.