Crypto++
5.6.4
Free C++ class library of cryptographic schemes
|
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)
204 if (!bt.
Get(unused) || unused > 7)
208 if ((bc-1) != bt.
Get(str, bc-1))
219 if (decoder.IsDefiniteLength())
220 decoder.
TransferTo(encoder, decoder.RemainingLength());
223 while (!decoder.EndReached())
226 decoder.MessageEnd();
227 encoder.MessageEnd();
233 bt.
Put((
byte)(0x80 | ((v >> i) & 0x7f)));
234 bt.
Put((
byte)(v & 0x7f));
247 if (v >> (8*
sizeof(v)-7))
258 assert(m_values.size() >= 2);
260 temp.
Put(
byte(m_values[0] * 40 + m_values[1]));
261 for (
size_t i=2; i<m_values.size(); i++)
262 EncodeValue(temp, m_values[i]);
263 bt.
Put(OBJECT_IDENTIFIER);
271 if (!bt.
Get(b) || b != OBJECT_IDENTIFIER)
283 m_values[0] = b / 40;
284 m_values[1] = b % 40;
289 size_t valueLen = DecodeValue(bt, v);
290 if (valueLen > length)
292 m_values.push_back(v);
306 if (m_flags & PUT_OBJECTS)
314 if (m_nCurrentObject == m_nObjects)
320 LazyPutter lazyPutter(m_queue, inString, length);
327 if (!m_queue.
Get(m_id))
334 if (m_level > 0 && m_id == 0 && m_queue.
Peek(b) && b == 0)
338 m_state = IDENTIFIER;
345 m_queue.
TransferTo(CurrentTarget(), walker.GetCurrentPosition());
346 if (!((m_id & CONSTRUCTED) || definiteLength))
350 if (!(m_id & CONSTRUCTED))
353 m_state = IDENTIFIER;
359 m_lengthRemaining -= m_queue.
TransferTo(CurrentTarget(), m_lengthRemaining);
361 if (m_lengthRemaining == 0)
362 m_state = IDENTIFIER;
369 if (m_state == IDENTIFIER && m_level == 0)
374 if (m_flags & PUT_MESSANGE_END_AFTER_EACH_OBJECT)
377 if (m_nCurrentObject == m_nObjects)
379 if (m_flags & PUT_MESSANGE_END_AFTER_ALL_OBJECTS)
382 if (m_flags & PUT_MESSANGE_SERIES_END_AFTER_ALL_OBJECTS)
393 : m_inQueue(inQueue), m_finished(false)
399 : m_inQueue(inQueue), m_finished(false)
404 void BERGeneralDecoder::Init(
byte asnTag)
407 if (!m_inQueue.
Get(b) || b != asnTag)
413 if (!m_definiteLength && !(asnTag & CONSTRUCTED))
417 BERGeneralDecoder::~BERGeneralDecoder()
430 bool BERGeneralDecoder::EndReached()
const
432 if (m_definiteLength)
433 return m_length == 0;
441 byte BERGeneralDecoder::PeekByte()
const
449 void BERGeneralDecoder::CheckByte(
byte check)
452 if (!
Get(b) || b != check)
456 void BERGeneralDecoder::MessageEnd()
459 if (m_definiteLength)
467 if (m_inQueue.
GetWord16(i) != 2 || i != 0)
474 if (m_definiteLength && transferBytes > m_length)
475 transferBytes = m_length;
476 size_t blockedBytes = m_inQueue.
TransferTo2(target, transferBytes, channel, blocking);
477 ReduceLength(transferBytes);
483 if (m_definiteLength)
484 end =
STDMIN(m_length, end);
485 return m_inQueue.
CopyRangeTo2(target, begin, end, channel, blocking);
488 lword BERGeneralDecoder::ReduceLength(lword delta)
490 if (m_definiteLength)
492 if (m_length < delta)
500 :
ByteQueue(), m_outQueue(outQueue), m_finished(false), m_asnTag(asnTag)
507 :
ByteQueue(), m_outQueue(outQueue), m_finished(false), m_asnTag(asnTag)
511 DERGeneralEncoder::~DERGeneralEncoder()
524 void DERGeneralEncoder::MessageEnd()
527 lword length = CurrentSize();
528 m_outQueue.
Put(m_asnTag);
540 bool parametersPresent = algorithm.EndReached() ? false : BERDecodeAlgorithmParameters(algorithm);
541 algorithm.MessageEnd();
544 subjectPublicKey.CheckByte(0);
545 BERDecodePublicKey(subjectPublicKey, parametersPresent, (
size_t)subjectPublicKey.RemainingLength());
546 subjectPublicKey.MessageEnd();
547 subjectPublicKeyInfo.MessageEnd();
556 DEREncodeAlgorithmParameters(algorithm);
557 algorithm.MessageEnd();
560 subjectPublicKey.
Put(0);
562 subjectPublicKey.MessageEnd();
564 subjectPublicKeyInfo.MessageEnd();
571 BERDecodeUnsigned<word32>(privateKeyInfo, version, INTEGER, 0, 0);
575 bool parametersPresent = algorithm.EndReached() ? false : BERDecodeAlgorithmParameters(algorithm);
576 algorithm.MessageEnd();
579 BERDecodePrivateKey(octetString, parametersPresent, (
size_t)privateKeyInfo.RemainingLength());
580 octetString.MessageEnd();
582 if (!privateKeyInfo.EndReached())
584 privateKeyInfo.MessageEnd();
590 DEREncodeUnsigned<word32>(privateKeyInfo, 0);
594 DEREncodeAlgorithmParameters(algorithm);
595 algorithm.MessageEnd();
599 octetString.MessageEnd();
602 privateKeyInfo.MessageEnd();
612 m_optionalAttributes.
CopyTo(bt);
size_t BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BER decode octet string.
use this to make sure LazyPut is finalized in event of exception
iterator begin()
Provides an iterator pointing to the first element in the memory block.
size_t BERDecodeTextString(BufferedTransformation &bt, std::string &str, byte asnTag)
BER decode text string.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
void Put(const byte *inString, size_t length)
Input a byte buffer for processing.
void BERDecodeNull(BufferedTransformation &bt)
BER decode NULL.
Data structure used to store byte strings.
void BERDecode(BufferedTransformation &bt)
BER decode an OID.
virtual OID GetAlgorithmID() const =0
Retrieves the OID of the algorithm.
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.
virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt)
decode optional attributes including context-specific tag
void DEREncode(BufferedTransformation &bt) const
DER encode this OID.
void DEREncodeNull(BufferedTransformation &bt)
DER encode NULL.
virtual OID GetAlgorithmID() const =0
Retrieves the OID of the algorithm.
size_t DEREncodeBitString(BufferedTransformation &bt, const byte *str, size_t strLen, unsigned int unusedBits=0)
DER encode bit string.
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
encode privateKey part of privateKeyInfo, without the OCTET STRING header
void resize(size_type newSize)
Change size and preserve contents.
void BERDecodeError()
Raises a BERDecodeErr.
size_t BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits)
DER decode bit string.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
void DERReencode(BufferedTransformation &bt, BufferedTransformation &dest)
BER decode and DER re-encode.
Base class for all exceptions thrown by the library.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
size_t Peek(byte &outByte) const
Peek a 8-bit byte.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
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
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Classes and functions for working with ANS.1 objects.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
void New(size_type newSize)
Change size without preserving contents.
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
size_t DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen)
DER encode octet 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.
virtual void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)=0
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
size_t DEREncodeTextString(BufferedTransformation &bt, const std::string &str, byte asnTag)
DER encode text string.
bool BERLengthDecode(BufferedTransformation &bt, size_t &length)
BER decode a length.
bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
virtual void DEREncodePublicKey(BufferedTransformation &bt) const =0
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header
BufferedTransformation & TheBitBucket()
An input discarding BufferedTransformation.
Crypto++ library namespace.
size_t DERLengthEncode(BufferedTransformation &bt, lword length)
DER encode a length.
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Library configuration file.
void BERDecodeAndCheck(BufferedTransformation &bt) const
BER decode an OID.
size_t Get(byte &outByte)
Retrieve a 8-bit byte.