7#ifndef CRYPTOPP_IMPORTS
18NAMESPACE_BEGIN(CryptoPP)
34 bt.Put(
byte(length >> (j-1)*8));
50 definiteLength =
true;
55 unsigned int lengthBytes = b & 0x7f;
59 definiteLength =
false;
63 definiteLength =
true;
67 if (length >> (8*(
sizeof(length)-1)))
73 length = (length << 8) | b;
82 bool definiteLength =
false;
83 if (!BERLengthDecode(bt, lw, definiteLength))
87 return definiteLength;
102 if (!BERLengthDecode(in, length) || length != 0)
110 size_t lengthBytes = DERLengthEncode(bt, strLen);
112 return 1+lengthBytes+strLen;
117 return DEREncodeOctetString(bt, ConstBytePtr(str), BytePtrSize(str));
127 if (!BERLengthDecode(bt, bc))
133 if (bc != bt.
Get(BytePtr(str), bc))
145 if (!BERLengthDecode(bt, bc))
157 size_t lengthBytes = DERLengthEncode(bt, strLen);
159 return 1+lengthBytes+strLen;
164 return DEREncodeTextString(bt, ConstBytePtr(str), BytePtrSize(str), asnTag);
169 return DEREncodeTextString(bt, ConstBytePtr(str), BytePtrSize(str), asnTag);
175 if (!bt.
Get(b) || b != asnTag)
179 if (!BERLengthDecode(bt, bc))
185 if (bc != bt.
Get(BytePtr(str), BytePtrSize(str)))
194 if (!bt.
Get(b) || b != asnTag)
198 if (!BERLengthDecode(bt, bc))
204 if (bc != bt.
Get(BytePtr(str), BytePtrSize(str)))
213 size_t lengthBytes = DERLengthEncode(bt, str.
size());
214 bt.
Put(ConstBytePtr(str), BytePtrSize(str));
215 return 1+lengthBytes+str.
size();
221 if (!bt.
Get(b) || b != asnTag)
225 if (!BERLengthDecode(bt, bc))
231 if (bc != bt.
Get(BytePtr(str), BytePtrSize(str)))
240 size_t lengthBytes = DERLengthEncode(bt, strLen+1);
241 bt.
Put((
byte)unusedBits);
243 return 2+lengthBytes+strLen;
253 if (!BERLengthDecode(bt, bc))
262 if (!bt.
Get(unused) || unused > 7)
266 if ((bc-1) != bt.
Get(BytePtr(str), bc-1))
282 DERReencode(decoder, encoder);
291 if (count == 0)
return 0;
294 bt.
CopyTo(tagAndLength, count);
297 tagAndLength.
Skip(1);
301 if (!BERLengthDecode(tagAndLength, length))
310 bt.
Put((
byte)(0x80 | ((v >> i) & 0x7f)));
311 bt.
Put((
byte)(v & 0x7f));
324 if (v >> (8*
sizeof(v)-7))
335 CRYPTOPP_ASSERT(m_values.size() >= 2);
337 temp.
Put(
byte(m_values[0] * 40 + m_values[1]));
338 for (
size_t i=2; i<m_values.size(); i++)
339 EncodeValue(temp, m_values[i]);
360 m_values[0] = b / 40;
361 m_values[1] = b % 40;
366 size_t valueLen = DecodeValue(bt, v);
367 if (valueLen > length)
369 m_values.push_back(v);
383 std::ostringstream oss;
384 for (
size_t i = 0; i < m_values.size(); ++i)
387 if (i+1 < m_values.size())
390 return out << oss.str();
395 if (m_flags & PUT_OBJECTS)
398 return TheBitBucket();
403 if (m_nCurrentObject == m_nObjects)
409 LazyPutter lazyPutter(m_queue, inString, length);
416 if (!m_queue.
Get(m_id))
424 if (m_level > 0 && m_id == 0 && m_queue.
Peek(b) && b == 0)
428 m_state = IDENTIFIER;
432 bool definiteLength =
false;
435 m_queue.
TransferTo(CurrentTarget(), walker.GetCurrentPosition());
443 m_state = IDENTIFIER;
450 m_lengthRemaining -= m_queue.
TransferTo(CurrentTarget(), m_lengthRemaining);
452 if (m_lengthRemaining == 0)
453 m_state = IDENTIFIER;
460 if (m_state == IDENTIFIER && m_level == 0)
465 if (m_flags & PUT_MESSANGE_END_AFTER_EACH_OBJECT)
468 if (m_nCurrentObject == m_nObjects)
470 if (m_flags & PUT_MESSANGE_END_AFTER_ALL_OBJECTS)
473 if (m_flags & PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS)
484 : m_inQueue(inQueue), m_length(0), m_finished(false)
490 : m_inQueue(inQueue), m_length(0), m_finished(false)
496 : m_inQueue(inQueue), m_length(0), m_finished(false)
501void BERGeneralDecoder::Init(
byte asnTag)
504 if (!m_inQueue.
Get(b) || b != asnTag)
514BERGeneralDecoder::~BERGeneralDecoder()
529 if (m_definiteLength)
530 return m_length == 0;
549 if (!
Get(b) || b != check)
556 if (m_definiteLength)
564 if (m_inQueue.
GetWord16(i) != 2 || i != 0)
571 if (m_definiteLength && transferBytes > m_length)
572 transferBytes = m_length;
573 size_t blockedBytes = m_inQueue.
TransferTo2(target, transferBytes, channel, blocking);
574 ReduceLength(transferBytes);
580 if (m_definiteLength)
581 end =
STDMIN(m_length, end);
582 return m_inQueue.
CopyRangeTo2(target, begin, end, channel, blocking);
585lword BERGeneralDecoder::ReduceLength(
lword delta)
587 if (m_definiteLength)
589 if (m_length < delta)
597 : m_outQueue(outQueue), m_asnTag(DefaultTag), m_finished(false)
602 : m_outQueue(outQueue), m_asnTag(asnTag), m_finished(false)
607 : m_outQueue(outQueue), m_asnTag(asnTag), m_finished(false)
611DERGeneralEncoder::~DERGeneralEncoder()
628 m_outQueue.
Put(m_asnTag);
660 subjectPublicKey.
Put(0);
671 BERDecodeUnsigned<word32>(privateKeyInfo, version,
INTEGER, 0, 0);
690 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
712 m_optionalAttributes.
CopyTo(bt);
Classes and functions for working with ANS.1 objects.
CRYPTOPP_DLL bool CRYPTOPP_API BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
CRYPTOPP_DLL void CRYPTOPP_API DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
@ CONSTRUCTED
ASN.1 Constructed flag.
@ OCTET_STRING
ASN.1 Octet string.
@ OBJECT_IDENTIFIER
ASN.1 Object identifier.
@ BIT_STRING
ASN.1 Bit string.
void BERDecodeError()
Raises a BERDecodeErr.
CRYPTOPP_DLL size_t CRYPTOPP_API DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
bool EndReached() const
Determine end of stream.
lword RemainingLength() const
Determine remaining length.
bool IsDefiniteLength() const
Determine length encoding.
BERGeneralDecoder(BufferedTransformation &inQueue)
Construct an ASN.1 decoder.
void MessageEnd()
Signals the end of messages to the object.
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes 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 to another BufferedTransformation.
byte PeekByte() const
Determine next octet.
void CheckByte(byte b)
Determine next octet.
Data structure used to store byte strings.
lword CurrentSize() const
Determine data size.
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.
DERGeneralEncoder(BufferedTransformation &outQueue)
Construct an ASN.1 encoder.
void MessageEnd()
Signals the end of messages to the object.
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.
Helper class to finalize Puts on ByteQueue.
void BERDecodeAndCheck(BufferedTransformation &bt) const
BER decode an OID.
void DEREncode(BufferedTransformation &bt) const
DER encode this OID.
std::ostream & Print(std::ostream &out) const
Print an OID.
void BERDecode(BufferedTransformation &bt)
BER decode an OID.
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
Decode optional parameters.
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
Encode optional parameters.
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
Encode privateKey part of privateKeyInfo.
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
Decode optional attributes.
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.
virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)=0
Decode privateKey part of privateKeyInfo.
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.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0
Encode subjectPublicKey part of subjectPublicKeyInfo.
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
Encode algorithm parameters.
virtual bool BERDecodeAlgorithmParameters(BufferedTransformation &bt)
Decode algorithm parameters.
Library configuration file.
unsigned int word32
32-bit unsigned datatype
unsigned short word16
16-bit unsigned datatype
word64 lword
Large word type.
Abstract base classes that provide a uniform interface to this library.
Utility functions for the Crypto++ library.
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.