7#ifndef CRYPTOPP_IMPORTS
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;
87 return definiteLength;
112 return 1+lengthBytes+strLen;
159 return 1+lengthBytes+strLen;
175 if (!bt.
Get(b) || b != asnTag)
194 if (!bt.
Get(b) || b != asnTag)
215 return 1+lengthBytes+str.
size();
221 if (!bt.
Get(b) || b != asnTag)
241 bt.
Put((
byte)unusedBits);
243 return 2+lengthBytes+strLen;
262 if (!bt.
Get(unused) || unused > 7)
277 if (decoder.IsDefiniteLength())
278 decoder.TransferTo(encoder, decoder.RemainingLength());
281 while (!decoder.EndReached())
284 decoder.MessageEnd();
285 encoder.MessageEnd();
291 if (count == 0)
return 0;
294 bt.
CopyTo(tagAndLength, count);
297 tagAndLength.
Skip(1);
310 bt.
Put((
byte)(0x80 | ((v >> i) & 0x7f)));
311 bt.
Put((
byte)(v & 0x7f));
324 if (v >> (8*
sizeof(v)-7))
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);
381std::ostream&
OID::Print(std::ostream& out)
const
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)
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);
641 algorithm.MessageEnd();
644 subjectPublicKey.CheckByte(0);
645 BERDecodePublicKey(subjectPublicKey, parametersPresent, (
size_t)subjectPublicKey.RemainingLength());
646 subjectPublicKey.MessageEnd();
647 subjectPublicKeyInfo.MessageEnd();
657 algorithm.MessageEnd();
660 subjectPublicKey.Put(0);
662 subjectPublicKey.MessageEnd();
664 subjectPublicKeyInfo.MessageEnd();
671 BERDecodeUnsigned<word32>(privateKeyInfo, version,
INTEGER, 0, 0);
676 algorithm.MessageEnd();
679 BERDecodePrivateKey(octetString, parametersPresent, (
size_t)privateKeyInfo.RemainingLength());
680 octetString.MessageEnd();
682 if (!privateKeyInfo.EndReached())
684 privateKeyInfo.MessageEnd();
690 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
695 algorithm.MessageEnd();
699 octetString.MessageEnd();
702 privateKeyInfo.MessageEnd();
712 m_optionalAttributes.
CopyTo(bt);
Classes and functions for working with ANS.1 objects.
CRYPTOPP_DLL size_t BERDecodeTextString(BufferedTransformation &bt, SecByteBlock &str, byte asnTag)
BER decode text string.
CRYPTOPP_DLL void DEREncodeNull(BufferedTransformation &bt)
DER encode NULL.
CRYPTOPP_DLL bool BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
CRYPTOPP_DLL size_t BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits)
DER decode bit string.
CRYPTOPP_DLL size_t DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0)
DER encode bit string.
CRYPTOPP_DLL size_t DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen)
DER encode octet string.
CRYPTOPP_DLL void BERDecodeNull(BufferedTransformation &bt)
BER decode NULL.
CRYPTOPP_DLL size_t BERDecodeDate(BufferedTransformation &bt, SecByteBlock &str, byte asnTag)
BER decode date.
@ CONSTRUCTED
ASN.1 Constructed flag.
CRYPTOPP_DLL size_t BERDecodePeekLength(const BufferedTransformation &bt)
BER decode size.
CRYPTOPP_DLL void DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
CRYPTOPP_DLL size_t DEREncodeTextString(BufferedTransformation &bt, const byte *str, size_t strLen, byte asnTag)
DER encode text string.
CRYPTOPP_DLL size_t DEREncodeDate(BufferedTransformation &bt, const SecByteBlock &str, byte asnTag)
DER encode date.
CRYPTOPP_DLL size_t BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BER decode octet string.
@ OCTET_STRING
ASN.1 Octet string.
@ OBJECT_IDENTIFIER
ASN.1 Object identifier.
@ BIT_STRING
ASN.1 Bit string.
CRYPTOPP_DLL size_t DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
void BERDecodeError()
Raises a BERDecodeErr.
bool EndReached() const
Determine end of stream.
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 void BERDecodeOptionalAttributes(BufferedTransformation &bt)
Decode optional attributes.
virtual bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const
Encode optional parameters.
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
Encode privateKey part of privateKeyInfo.
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.
CRYPTOPP_DLL BufferedTransformation & TheBitBucket()
An input discarding BufferedTransformation.
Utility functions for the Crypto++ library.
size_t BytePtrSize(const std::string &str)
Size of a string.
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.
const byte * ConstBytePtr(const std::string &str)
Const pointer to the first element of a string.
bool SafeConvert(T1 from, T2 &to)
Perform a conversion from from to to.
byte * BytePtr(std::string &str)
Pointer to the first element of a string.
Crypto++ library namespace.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.