First public contribution.
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Declares all classes used to decode ASN.1 data, including the
34 class TASN1EncBase128DER;
38 typedef void (CASN1EncBase::* WriteFunc)(TDes8& aBuf) const;
41 * Base class for all ASN.1 types that we can encode.
46 class CASN1EncBase : public CBase
50 * Gives total number of octets in the DER encoding of this
52 * @return Number of octets in DER encoding of this object.
54 IMPORT_C TUint LengthDER() const;
57 * Writes entire DER encoding of this object into the given
59 * @param aBuf Buffer receiving the encoding.
60 * @param aPos Position to start writing at.
62 IMPORT_C void WriteDERL(TDes8& aBuf, TUint& aPos) const;
65 * Sets tag type/class of the encoding object
66 * @param aType Tag type to set
67 * @param aClass Tag class to set.
69 IMPORT_C void SetTag(const TTagType aType,
70 const TASN1Class aClass = EContextSpecific);
72 IMPORT_C ~CASN1EncBase();
77 * Sets parent for the object
78 * @param aParent Pointer to an ASN.1 object that becomes this
81 void SetParent(CASN1EncBase* aParent);
85 * Protected constructor
86 * @param aType Tag type of the new object
87 * @param aClass Tag class of the new object.
89 IMPORT_C CASN1EncBase(const TTagType aType, const TASN1Class aClass);
92 * Must call this version from derived classes in their
93 * ConstructL, but only once they're ready to have
94 * CalculateContentsLengthDER called on them.
96 IMPORT_C virtual void ConstructL();
101 * Derived classes must call this if the length of their
102 * contents changes after construction.
104 void ContentsLengthChanged();
110 * Calculates number of octets in DER length encoding. Must set
111 * value of the appropriate data member. Made protected because it is
112 * needed by CASN1EncEncoding class.
114 void CalculateLengthLengthDER();
118 * Calculates number of octets in DER tag encoding. Must set
119 * value of the appropriate data member.
121 void CalculateTagLengthDER();
124 * Calculates number of octets in DER content encoding. Must set
125 * value of the appropriate data member.
127 virtual void CalculateContentsLengthDER() = 0;
129 virtual TBool IsConstructed() const = 0;
131 // Write the octet data in each section
132 // Note that buffer *will* be big enough: these are called only
136 * Writes DER tag encoding into supplied buffer, which is
137 * already verified to be big enough.
138 * @param aBuf Buffer to write to.
140 void WriteTagDERL(TDes8& aBuf) const;
143 * Writes DER length encoding into supplied buffer, which is
144 * already verified to be big enough.
145 * @param aBuf Buffer to write to.
147 virtual void WriteLengthDER(TDes8& aBuf) const;
150 * Writes DER content encoding into supplied buffer, which is
151 * already verified to be big enough. Must be implemented by
153 * @param aBuf Buffer to write to.
155 virtual void WriteContentsDERL(TDes8& aBuf) const = 0;
158 * Helper function, used for efficiency
159 * @param aBuf Buffer to write to
160 * @param aPos Position in the buffer to start writing at
162 * @param aLength Length of data to write
163 * @param aWriteFunc Points to the function used to perform
164 * the actual write operation.
166 void WriteHelperL(TDes8& aBuf, TUint& aPos, const TUint aLength,
167 WriteFunc aWriteFunc) const;
170 // Cached length data - data set from the CalculateXxxx methods above
171 TUint iTagLengthDER; ///< Length of DER encoded tag
172 TUint iLengthLengthDER; ///< Length of DER encoded length
173 TUint iContentsLengthDER; ///< Length of DER encoded content
177 * The object owning this one (if we're held in a sequence,
180 CASN1EncBase* iParent;
183 TTagType iType; ///< Tag type of this object
184 TASN1Class iClass; ///< Tag class of this object
189 * Base class for all ASN1 constructed-type objects. Most of these
190 * are container classes, but another type is the explicit-tagging
196 class CASN1EncConstructed : public CASN1EncBase
202 * Protected constructor
203 * @param aType Tag type for the new object
204 * @param aClass Tag class for the new object.
206 CASN1EncConstructed(const TTagType aType, const TASN1Class aClass);
209 virtual const CASN1EncBase& Child(const TUint aIndex) const = 0;
210 virtual TUint NumChildren() const = 0;
214 void CalculateContentsLengthDER();
215 TBool IsConstructed() const;
216 void WriteContentsDERL(TDes8& aBuf) const;
221 * Class used to wrap other encoding objects in order to give
222 * them an explicit tag.
227 class CASN1EncExplicitTag : public CASN1EncConstructed
231 * Takes ownership of the encoder, *including* the case when
232 * this method leaves.
233 * @param aEncoder ASN.1 encoding object to wrap
234 * @param aType Tag type to assign
235 * @param aClass Tag class to assign
236 * @return Wrapped encoding object pushed on the cleanup stack.
238 IMPORT_C static CASN1EncExplicitTag* NewLC(CASN1EncBase* aEncoder,
239 const TTagType aType, const TASN1Class aClass = EContextSpecific);
242 * Takes ownership of the encoder, *including* the case when
243 * this method leaves.
244 * @param aEncoder ASN.1 encoding object to wrap
245 * @param aType Tag type to assign
246 * @param aClass Tag class to assign
247 * @return Wrapped encoding object.
249 IMPORT_C static CASN1EncExplicitTag* NewL(CASN1EncBase* aEncoder,
250 const TTagType aType, const TASN1Class aClass = EContextSpecific);
252 IMPORT_C ~CASN1EncExplicitTag(); // virtual from base
255 CASN1EncExplicitTag(CASN1EncBase* aEncoder, const TTagType aType,
256 const TASN1Class aClass);
258 // From CASN1EncConstructed
259 TUint NumChildren() const;
260 const CASN1EncBase& Child(const TUint aIndex) const;
263 CASN1EncBase* iEncoder; // Inner encoding object
268 * Base class for all ASN1 container types - sequences,
274 class CASN1EncContainer : public CASN1EncConstructed
278 * Call this to add a child object to the container.
279 * Takes ownership if and only if it doesn't Leave.
280 * Checks for null input, calls AddChildInt, calls
281 * ContentsLengthChanged().
282 * @param aChild Child ASN1 encoding object to add.
284 IMPORT_C void AddChildL(CASN1EncBase* aChild);
287 * Call this to add a child object to the container.
288 * Takes ownership if and only if it doesn't Leave.
289 * Checks for null input, calls AddChildInt, calls
290 * ContentsLengthChanged(). Pops added child object
291 * off the cleanup stack.
292 * @param aChild Child ASN1 encoding object to add.
294 IMPORT_C void AddAndPopChildL(CASN1EncBase* aChild);
297 /** @internalComponent */
298 CASN1EncContainer(const TTagType aType);
302 * Internal method, derived classes implement to add a child.
303 * No need to check for null input or call
304 * ContentsLengthChanged(). Takes ownership, but only if you
306 * @param aChild Child encoding object to add.
308 virtual void AddChildIntL(const CASN1EncBase* aChild) = 0;
312 * Class for encoding SEQUENCE and SEQUENCE-OF data types.
317 class CASN1EncSequence : public CASN1EncContainer
320 IMPORT_C static CASN1EncSequence* NewL();
321 IMPORT_C static CASN1EncSequence* NewLC();
323 IMPORT_C ~CASN1EncSequence(); // virtual from base
326 /** @internalComponent */
330 // From CASN1EncContainer
331 const CASN1EncBase& Child(const TUint aIndex) const;
332 void AddChildIntL(const CASN1EncBase* aChild);
333 TUint NumChildren() const;
336 RPointerArray<CASN1EncBase> iChildren;
340 * Class for encoding SET and SET-OF data types.
345 class CASN1EncSet : public CASN1EncContainer
349 Creates an ASN.1 Set encoder.
350 @return The fully constructed object.
352 IMPORT_C static CASN1EncSet* NewL();
355 Creates an ASN.1 Set encoder, and puts it onto the cleanup stack.
356 @return The fully constructed object.
358 IMPORT_C static CASN1EncSet* NewLC();
363 IMPORT_C ~CASN1EncSet(); // virtual from base
374 // From CASN1EncContainer
375 const CASN1EncBase& Child(const TUint aIndex) const;
376 void AddChildIntL(const CASN1EncBase* aChild);
377 TUint NumChildren() const;
380 RPointerArray<CASN1EncBase> iChildren;
386 * All ASN1 primitive type encoding classes derive from here.
391 class CASN1EncPrimitive : public CASN1EncBase
394 IMPORT_C CASN1EncPrimitive(const TTagType aType);
397 TBool IsConstructed() const; ///< Inherited from CASN1EncBase
402 * Class for encoding NULLs.
407 class CASN1EncNull : public CASN1EncPrimitive
410 IMPORT_C static CASN1EncNull* NewL();
411 IMPORT_C static CASN1EncNull* NewLC();
416 // Methods from CASN1EncBase
417 void CalculateContentsLengthDER() ;
418 void WriteContentsDERL(TDes8& aBuf) const;
423 * Class for encoding Boolean values.
428 class CASN1EncBoolean : public CASN1EncPrimitive
431 IMPORT_C static CASN1EncBoolean* NewLC(const TBool aBool);
432 IMPORT_C static CASN1EncBoolean* NewL(const TBool aBool);
435 CASN1EncBoolean(const TBool aBool);
437 // Methods from CASN1EncBase
438 void CalculateContentsLengthDER() ;
439 void WriteContentsDERL(TDes8& aBuf) const;
446 * Class for encoding TInts only. Use CASN1EncBigInt for encoding
447 * Big Integer objects.
452 class CASN1EncInt : public CASN1EncPrimitive
455 IMPORT_C static CASN1EncInt* NewLC(const TInt aInt);
456 IMPORT_C static CASN1EncInt* NewL(const TInt aInt);
459 CASN1EncInt(const TInt aInt);
461 // Methods from CASN1EncBase
462 void CalculateContentsLengthDER() ;
463 void WriteContentsDERL(TDes8& aBuf) const;
471 * Class for encoding Big Integer objects only - use CASN1EncInt
477 class CASN1EncBigInt : public CASN1EncPrimitive
481 IMPORT_C static CASN1EncBigInt* NewLC(const TInteger& aInteger);
482 IMPORT_C static CASN1EncBigInt* NewL(const TInteger& aInteger);
484 IMPORT_C ~CASN1EncBigInt(); // virtual from base
488 void ConstructL(const TInteger& aInteger);
490 // Methods from CASN1EncBase
491 void CalculateContentsLengthDER();
492 void WriteContentsDERL(TDes8& aBuf) const;
496 TPtrC8 iWriteContents;
501 * Class for encoding octet strings.
506 class CASN1EncOctetString : public CASN1EncPrimitive
510 IMPORT_C static CASN1EncOctetString* NewLC(const TDesC8& aStr);
511 IMPORT_C static CASN1EncOctetString* NewL(const TDesC8& aStr);
513 IMPORT_C ~CASN1EncOctetString(); // virtual from base
516 CASN1EncOctetString();
517 void ConstructL(const TDesC8& aStr);
519 // Methods from CASN1EncBase
520 void CalculateContentsLengthDER();
521 void WriteContentsDERL(TDes8& aBuf) const;
529 * Class for encoding printable strings.
534 class CASN1EncPrintableString : public CASN1EncPrimitive
538 Creates an ASN.1 Printable String encoder, and puts it onto the cleanup stack.
539 @return The fully constructed object.
541 IMPORT_C static CASN1EncPrintableString* NewLC(const TDesC8& aStr);
544 Creates an ASN.1 Printable String encoder.
545 @return The fully constructed object.
547 IMPORT_C static CASN1EncPrintableString* NewL(const TDesC8& aStr);
552 IMPORT_C ~CASN1EncPrintableString(); // virtual from base
555 CASN1EncPrintableString();
556 void ConstructL(const TDesC8& aStr);
557 TInt CheckValid(const TDesC8& aStr);
559 // Methods from CASN1EncBase
560 void CalculateContentsLengthDER();
561 void WriteContentsDERL(TDes8& aBuf) const;
569 * Class for encoding bit strings (keys, for example).
574 class CASN1EncBitString : public CASN1EncPrimitive
578 * Constructs a new DER bit string encoder from a bit string that
579 * does not have unused bits at the end, i.e. is octet-aligned.
580 * The passed string must be in big-endian format.
581 * @param aBitStr Octet-aligned bit string.
582 * @return A new DER bit string encoder object,
583 * which is left on the cleanup stack.
585 IMPORT_C static CASN1EncBitString* NewLC(const TDesC8& aBitStr);
588 * Constructs a new DER bit string encoder from a bit string that
589 * does not have unused bits at the end, i.e. is octet-aligned.
590 * The passed string must be in big-endian format.
591 * @param aBitStr Octet-aligned bit string.
592 * @return A new DER bit string encoder object.
594 IMPORT_C static CASN1EncBitString* NewL(const TDesC8& aBitStr);
597 * Constructs a new DER bit string encoder from a bit string that
598 * is not octet-aligned, which means it has unused bits in its last
599 * octet. The passed string must be in big-endian format.
600 * @param aBitStr Bit string.
601 * @param aLengthBits Length in bits of the passed bit string.
602 * The function will panic if aLengthBits is greater than
603 * the actual bit length of aBitString, or the difference
604 * is more that 7 bits.
605 * @return A new DER bit string encoder object which is left on the
608 IMPORT_C static CASN1EncBitString* NewLC(const TDesC8& aBitStr, TUint aLengthBits);
611 * Constructs a new DER bit string encoder from a bit string that
612 * is not octet-aligned, which means it has unused bits in its last
613 * octet. The passed string must be in big-endian format.
614 * @param aBitStr Bit string.
615 * @param aLengthBits Length in bits of the passed bit string.
616 * The function will panic if aLengthBits is greater than
617 * the actual bit length of aBitString, or the difference
618 * is more that 7 bits.
619 * @return A new DER bit string encoder object.
621 IMPORT_C static CASN1EncBitString* NewL(const TDesC8& aBitStr, TUint aLengthBits);
624 * Wraps the passed encoding object into a bit string.
625 * @param aAsnObj Encoding object to wrap.
626 * @return A new bit string containing the passed encoding object.
628 IMPORT_C static CASN1EncBitString* NewL(const CASN1EncBase& aAsnObj);
631 * Wraps the passed encoding object into a bit string.
632 * @param aAsnObj Encoding object to wrap.
633 * @return A new bit string containing the passed encoding object
634 * on the cleanup stack.
636 IMPORT_C static CASN1EncBitString* NewLC(const CASN1EncBase& aAsnObj);
638 IMPORT_C ~CASN1EncBitString(); // virtual from base
642 void ConstructL(const TDesC8& aBitStr);
643 void ConstructL(const TDesC8& aBitStr, TUint aLengthBits);
644 void ConstructL(const CASN1EncBase& aAsnObj);
646 // Methods from CASN1EncBase
647 void CalculateContentsLengthDER();
648 void WriteContentsDERL(TDes8& aBuf) const;
656 * Class for encoding object identifiers.
661 class CASN1EncObjectIdentifier : public CASN1EncPrimitive
665 * Takes ints in a string, delimited by '.' characters in
666 * between (not at ends). Takes a deep copy of the info.
667 * @param aStr OID string.
668 * @return New ASN.1 OID object on the cleanup stack.
670 IMPORT_C static CASN1EncObjectIdentifier* NewLC(const TDesC& aStr);
673 * Takes ints in a string, delimited by '.' characters in
674 * between (not at ends). Takes a deep copy of the info.
675 * @param aStr OID string.
676 * @return New ASN.1 OID object.
678 IMPORT_C static CASN1EncObjectIdentifier* NewL(const TDesC& aStr);
681 IMPORT_C ~CASN1EncObjectIdentifier(); // virtual from base
684 CASN1EncObjectIdentifier();
685 void ConstructL(const TDesC& aStr);
687 // Methods from CASN1EncBase
688 void CalculateContentsLengthDER();
689 void WriteContentsDERL(TDes8& aBuf) const;
693 RArray<TASN1EncBase128DER> iData;
699 * Class for encoding GeneralisedTime objects.
701 * Doesn't support fractions of seconds or regional time zone offsets.
706 class CASN1EncGeneralizedTime : public CASN1EncPrimitive
709 IMPORT_C static CASN1EncGeneralizedTime* NewLC(const TTime& aTime);
710 IMPORT_C static CASN1EncGeneralizedTime* NewL(const TTime& aTime);
713 CASN1EncGeneralizedTime(const TTime& aTime);
715 // Methods from CASN1EncBase
716 void CalculateContentsLengthDER();
717 void WriteContentsDERL(TDes8& aBuf) const;
724 * Class for encapsulation of already encoded data.
726 * Wraps it so that the data could be used in the ASN.1 hierarchy.
727 * It reverse-engineers and stores the encoded data, providing whatever
728 * information is needed to override pure virtual methods of the base
729 * class and write out the DER encoding in its initial form.
734 class CASN1EncEncoding : public CASN1EncBase
738 * Creates a new object from raw DER encoding and places it on the
740 * @param aEncoding Raw DER encoding.
741 * @return New wrapper object placed on the cleanup stack.
743 IMPORT_C static CASN1EncEncoding* NewLC(const TDesC8& aEncoding);
746 * Creates a new object from raw DER encoding.
747 * @param aEncoding Raw DER encoding.
748 * @return New wrapper object.
750 IMPORT_C static CASN1EncEncoding* NewL(const TDesC8& aEncoding);
752 IMPORT_C static CASN1EncEncoding* NewLC(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass);
754 IMPORT_C ~CASN1EncEncoding();
758 * Protected constructor.
760 * @param aType Tag type of the new object
761 * @param aClass Tag class of the new object.
764 IMPORT_C CASN1EncEncoding();
768 * Constructs the wrapper around the passed raw DER encoding.
769 * Calculates element sizes. Decodes it to get type and length.
770 * @param aEncoding Raw DER encoding.
772 void ConstructL(const TDesC8& aEncoding);
774 void ConstructL(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass);
776 virtual TBool IsConstructed() const;
779 * Writes DER content encoding into supplied buffer, which is
780 * already verified to be big enough.
781 * @param aBuf Buffer to write to.
783 virtual void WriteContentsDERL(TDes8& aBuf) const;
786 * Calculates number of octets in DER content encoding. Sets
787 * value of the appropriate data member.
789 virtual void CalculateContentsLengthDER();
792 HBufC8* iContents; ///< Copy of the supplied DER encoded data (contents only).
793 TASN1Class iClass; ///< ASN.1 class of the encoded object.
794 TTagType iTag; ///< ASN.1 tag of the encoding.
797 #endif // __ASN1ENC_H__