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
36 class TASN1EncBase128DER;
40 typedef void (CASN1EncBase::* WriteFunc)(TDes8& aBuf) const;
43 * Base class for all ASN.1 types that we can encode.
48 class CASN1EncBase : public CBase
52 * Gives total number of octets in the DER encoding of this
54 * @return Number of octets in DER encoding of this object.
56 IMPORT_C TUint LengthDER() const;
59 * Writes entire DER encoding of this object into the given
61 * @param aBuf Buffer receiving the encoding.
62 * @param aPos Position to start writing at.
64 IMPORT_C void WriteDERL(TDes8& aBuf, TUint& aPos) const;
67 * Sets tag type/class of the encoding object
68 * @param aType Tag type to set
69 * @param aClass Tag class to set.
71 IMPORT_C void SetTag(const TTagType aType,
72 const TASN1Class aClass = EContextSpecific);
74 IMPORT_C ~CASN1EncBase();
79 * Sets parent for the object
80 * @param aParent Pointer to an ASN.1 object that becomes this
83 void SetParent(CASN1EncBase* aParent);
87 * Protected constructor
88 * @param aType Tag type of the new object
89 * @param aClass Tag class of the new object.
91 IMPORT_C CASN1EncBase(const TTagType aType, const TASN1Class aClass);
94 * Must call this version from derived classes in their
95 * ConstructL, but only once they're ready to have
96 * CalculateContentsLengthDER called on them.
98 IMPORT_C virtual void ConstructL();
103 * Derived classes must call this if the length of their
104 * contents changes after construction.
106 void ContentsLengthChanged();
112 * Calculates number of octets in DER length encoding. Must set
113 * value of the appropriate data member. Made protected because it is
114 * needed by CASN1EncEncoding class.
116 void CalculateLengthLengthDER();
120 * Calculates number of octets in DER tag encoding. Must set
121 * value of the appropriate data member.
123 void CalculateTagLengthDER();
126 * Calculates number of octets in DER content encoding. Must set
127 * value of the appropriate data member.
129 virtual void CalculateContentsLengthDER() = 0;
131 virtual TBool IsConstructed() const = 0;
133 // Write the octet data in each section
134 // Note that buffer *will* be big enough: these are called only
138 * Writes DER tag encoding into supplied buffer, which is
139 * already verified to be big enough.
140 * @param aBuf Buffer to write to.
142 void WriteTagDERL(TDes8& aBuf) const;
145 * Writes DER length encoding into supplied buffer, which is
146 * already verified to be big enough.
147 * @param aBuf Buffer to write to.
149 virtual void WriteLengthDER(TDes8& aBuf) const;
152 * Writes DER content encoding into supplied buffer, which is
153 * already verified to be big enough. Must be implemented by
155 * @param aBuf Buffer to write to.
157 virtual void WriteContentsDERL(TDes8& aBuf) const = 0;
160 * Helper function, used for efficiency
161 * @param aBuf Buffer to write to
162 * @param aPos Position in the buffer to start writing at
164 * @param aLength Length of data to write
165 * @param aWriteFunc Points to the function used to perform
166 * the actual write operation.
168 void WriteHelperL(TDes8& aBuf, TUint& aPos, const TUint aLength,
169 WriteFunc aWriteFunc) const;
172 // Cached length data - data set from the CalculateXxxx methods above
173 TUint iTagLengthDER; ///< Length of DER encoded tag
174 TUint iLengthLengthDER; ///< Length of DER encoded length
175 TUint iContentsLengthDER; ///< Length of DER encoded content
179 * The object owning this one (if we're held in a sequence,
182 CASN1EncBase* iParent;
185 TTagType iType; ///< Tag type of this object
186 TASN1Class iClass; ///< Tag class of this object
191 * Base class for all ASN1 constructed-type objects. Most of these
192 * are container classes, but another type is the explicit-tagging
198 class CASN1EncConstructed : public CASN1EncBase
204 * Protected constructor
205 * @param aType Tag type for the new object
206 * @param aClass Tag class for the new object.
208 CASN1EncConstructed(const TTagType aType, const TASN1Class aClass);
211 virtual const CASN1EncBase& Child(const TUint aIndex) const = 0;
212 virtual TUint NumChildren() const = 0;
216 void CalculateContentsLengthDER();
217 TBool IsConstructed() const;
218 void WriteContentsDERL(TDes8& aBuf) const;
223 * Class used to wrap other encoding objects in order to give
224 * them an explicit tag.
229 class CASN1EncExplicitTag : public CASN1EncConstructed
233 * Takes ownership of the encoder, *including* the case when
234 * this method leaves.
235 * @param aEncoder ASN.1 encoding object to wrap
236 * @param aType Tag type to assign
237 * @param aClass Tag class to assign
238 * @return Wrapped encoding object pushed on the cleanup stack.
240 IMPORT_C static CASN1EncExplicitTag* NewLC(CASN1EncBase* aEncoder,
241 const TTagType aType, const TASN1Class aClass = EContextSpecific);
244 * Takes ownership of the encoder, *including* the case when
245 * this method leaves.
246 * @param aEncoder ASN.1 encoding object to wrap
247 * @param aType Tag type to assign
248 * @param aClass Tag class to assign
249 * @return Wrapped encoding object.
251 IMPORT_C static CASN1EncExplicitTag* NewL(CASN1EncBase* aEncoder,
252 const TTagType aType, const TASN1Class aClass = EContextSpecific);
254 IMPORT_C ~CASN1EncExplicitTag(); // virtual from base
257 CASN1EncExplicitTag(CASN1EncBase* aEncoder, const TTagType aType,
258 const TASN1Class aClass);
260 // From CASN1EncConstructed
261 TUint NumChildren() const;
262 const CASN1EncBase& Child(const TUint aIndex) const;
265 CASN1EncBase* iEncoder; // Inner encoding object
270 * Base class for all ASN1 container types - sequences,
276 class CASN1EncContainer : public CASN1EncConstructed
280 * Call this to add a child object to the container.
281 * Takes ownership if and only if it doesn't Leave.
282 * Checks for null input, calls AddChildInt, calls
283 * ContentsLengthChanged().
284 * @param aChild Child ASN1 encoding object to add.
286 IMPORT_C void AddChildL(CASN1EncBase* aChild);
289 * Call this to add a child object to the container.
290 * Takes ownership if and only if it doesn't Leave.
291 * Checks for null input, calls AddChildInt, calls
292 * ContentsLengthChanged(). Pops added child object
293 * off the cleanup stack.
294 * @param aChild Child ASN1 encoding object to add.
296 IMPORT_C void AddAndPopChildL(CASN1EncBase* aChild);
299 /** @internalComponent */
300 CASN1EncContainer(const TTagType aType);
304 * Internal method, derived classes implement to add a child.
305 * No need to check for null input or call
306 * ContentsLengthChanged(). Takes ownership, but only if you
308 * @param aChild Child encoding object to add.
310 virtual void AddChildIntL(const CASN1EncBase* aChild) = 0;
314 * Class for encoding SEQUENCE and SEQUENCE-OF data types.
319 class CASN1EncSequence : public CASN1EncContainer
322 IMPORT_C static CASN1EncSequence* NewL();
323 IMPORT_C static CASN1EncSequence* NewLC();
325 IMPORT_C ~CASN1EncSequence(); // virtual from base
328 /** @internalComponent */
332 // From CASN1EncContainer
333 const CASN1EncBase& Child(const TUint aIndex) const;
334 void AddChildIntL(const CASN1EncBase* aChild);
335 TUint NumChildren() const;
338 RPointerArray<CASN1EncBase> iChildren;
342 * Class for encoding SET and SET-OF data types.
347 class CASN1EncSet : public CASN1EncContainer
351 Creates an ASN.1 Set encoder.
352 @return The fully constructed object.
354 IMPORT_C static CASN1EncSet* NewL();
357 Creates an ASN.1 Set encoder, and puts it onto the cleanup stack.
358 @return The fully constructed object.
360 IMPORT_C static CASN1EncSet* NewLC();
365 IMPORT_C ~CASN1EncSet(); // virtual from base
376 // From CASN1EncContainer
377 const CASN1EncBase& Child(const TUint aIndex) const;
378 void AddChildIntL(const CASN1EncBase* aChild);
379 TUint NumChildren() const;
382 RPointerArray<CASN1EncBase> iChildren;
388 * All ASN1 primitive type encoding classes derive from here.
393 class CASN1EncPrimitive : public CASN1EncBase
396 IMPORT_C CASN1EncPrimitive(const TTagType aType);
399 TBool IsConstructed() const; ///< Inherited from CASN1EncBase
404 * Class for encoding NULLs.
409 class CASN1EncNull : public CASN1EncPrimitive
412 IMPORT_C static CASN1EncNull* NewL();
413 IMPORT_C static CASN1EncNull* NewLC();
418 // Methods from CASN1EncBase
419 void CalculateContentsLengthDER() ;
420 void WriteContentsDERL(TDes8& aBuf) const;
425 * Class for encoding Boolean values.
430 class CASN1EncBoolean : public CASN1EncPrimitive
433 IMPORT_C static CASN1EncBoolean* NewLC(const TBool aBool);
434 IMPORT_C static CASN1EncBoolean* NewL(const TBool aBool);
437 CASN1EncBoolean(const TBool aBool);
439 // Methods from CASN1EncBase
440 void CalculateContentsLengthDER() ;
441 void WriteContentsDERL(TDes8& aBuf) const;
448 * Class for encoding TInts only. Use CASN1EncBigInt for encoding
449 * Big Integer objects.
454 class CASN1EncInt : public CASN1EncPrimitive
457 IMPORT_C static CASN1EncInt* NewLC(const TInt aInt);
458 IMPORT_C static CASN1EncInt* NewL(const TInt aInt);
461 CASN1EncInt(const TInt aInt);
463 // Methods from CASN1EncBase
464 void CalculateContentsLengthDER() ;
465 void WriteContentsDERL(TDes8& aBuf) const;
473 * Class for encoding Big Integer objects only - use CASN1EncInt
479 class CASN1EncBigInt : public CASN1EncPrimitive
483 IMPORT_C static CASN1EncBigInt* NewLC(const TInteger& aInteger);
484 IMPORT_C static CASN1EncBigInt* NewL(const TInteger& aInteger);
486 IMPORT_C ~CASN1EncBigInt(); // virtual from base
490 void ConstructL(const TInteger& aInteger);
492 // Methods from CASN1EncBase
493 void CalculateContentsLengthDER();
494 void WriteContentsDERL(TDes8& aBuf) const;
498 TPtrC8 iWriteContents;
503 * Class for encoding octet strings.
508 class CASN1EncOctetString : public CASN1EncPrimitive
512 IMPORT_C static CASN1EncOctetString* NewLC(const TDesC8& aStr);
513 IMPORT_C static CASN1EncOctetString* NewL(const TDesC8& aStr);
515 IMPORT_C ~CASN1EncOctetString(); // virtual from base
518 CASN1EncOctetString();
519 void ConstructL(const TDesC8& aStr);
521 // Methods from CASN1EncBase
522 void CalculateContentsLengthDER();
523 void WriteContentsDERL(TDes8& aBuf) const;
531 * Class for encoding printable strings.
536 class CASN1EncPrintableString : public CASN1EncPrimitive
540 Creates an ASN.1 Printable String encoder, and puts it onto the cleanup stack.
541 @return The fully constructed object.
543 IMPORT_C static CASN1EncPrintableString* NewLC(const TDesC8& aStr);
546 Creates an ASN.1 Printable String encoder.
547 @return The fully constructed object.
549 IMPORT_C static CASN1EncPrintableString* NewL(const TDesC8& aStr);
554 IMPORT_C ~CASN1EncPrintableString(); // virtual from base
557 CASN1EncPrintableString();
558 void ConstructL(const TDesC8& aStr);
559 TInt CheckValid(const TDesC8& aStr);
561 // Methods from CASN1EncBase
562 void CalculateContentsLengthDER();
563 void WriteContentsDERL(TDes8& aBuf) const;
571 * Class for encoding bit strings (keys, for example).
576 class CASN1EncBitString : public CASN1EncPrimitive
580 * Constructs a new DER bit string encoder from a bit string that
581 * does not have unused bits at the end, i.e. is octet-aligned.
582 * The passed string must be in big-endian format.
583 * @param aBitStr Octet-aligned bit string.
584 * @return A new DER bit string encoder object,
585 * which is left on the cleanup stack.
587 IMPORT_C static CASN1EncBitString* NewLC(const TDesC8& aBitStr);
590 * Constructs a new DER bit string encoder from a bit string that
591 * does not have unused bits at the end, i.e. is octet-aligned.
592 * The passed string must be in big-endian format.
593 * @param aBitStr Octet-aligned bit string.
594 * @return A new DER bit string encoder object.
596 IMPORT_C static CASN1EncBitString* NewL(const TDesC8& aBitStr);
599 * Constructs a new DER bit string encoder from a bit string that
600 * is not octet-aligned, which means it has unused bits in its last
601 * octet. The passed string must be in big-endian format.
602 * @param aBitStr Bit string.
603 * @param aLengthBits Length in bits of the passed bit string.
604 * The function will panic if aLengthBits is greater than
605 * the actual bit length of aBitString, or the difference
606 * is more that 7 bits.
607 * @return A new DER bit string encoder object which is left on the
610 IMPORT_C static CASN1EncBitString* NewLC(const TDesC8& aBitStr, TUint aLengthBits);
613 * Constructs a new DER bit string encoder from a bit string that
614 * is not octet-aligned, which means it has unused bits in its last
615 * octet. The passed string must be in big-endian format.
616 * @param aBitStr Bit string.
617 * @param aLengthBits Length in bits of the passed bit string.
618 * The function will panic if aLengthBits is greater than
619 * the actual bit length of aBitString, or the difference
620 * is more that 7 bits.
621 * @return A new DER bit string encoder object.
623 IMPORT_C static CASN1EncBitString* NewL(const TDesC8& aBitStr, TUint aLengthBits);
626 * Wraps the passed encoding object into a bit string.
627 * @param aAsnObj Encoding object to wrap.
628 * @return A new bit string containing the passed encoding object.
630 IMPORT_C static CASN1EncBitString* NewL(const CASN1EncBase& aAsnObj);
633 * Wraps the passed encoding object into a bit string.
634 * @param aAsnObj Encoding object to wrap.
635 * @return A new bit string containing the passed encoding object
636 * on the cleanup stack.
638 IMPORT_C static CASN1EncBitString* NewLC(const CASN1EncBase& aAsnObj);
640 IMPORT_C ~CASN1EncBitString(); // virtual from base
644 void ConstructL(const TDesC8& aBitStr);
645 void ConstructL(const TDesC8& aBitStr, TUint aLengthBits);
646 void ConstructL(const CASN1EncBase& aAsnObj);
648 // Methods from CASN1EncBase
649 void CalculateContentsLengthDER();
650 void WriteContentsDERL(TDes8& aBuf) const;
658 * Class for encoding object identifiers.
663 class CASN1EncObjectIdentifier : public CASN1EncPrimitive
667 * Takes ints in a string, delimited by '.' characters in
668 * between (not at ends). Takes a deep copy of the info.
669 * @param aStr OID string.
670 * @return New ASN.1 OID object on the cleanup stack.
672 IMPORT_C static CASN1EncObjectIdentifier* NewLC(const TDesC& aStr);
675 * Takes ints in a string, delimited by '.' characters in
676 * between (not at ends). Takes a deep copy of the info.
677 * @param aStr OID string.
678 * @return New ASN.1 OID object.
680 IMPORT_C static CASN1EncObjectIdentifier* NewL(const TDesC& aStr);
683 IMPORT_C ~CASN1EncObjectIdentifier(); // virtual from base
686 CASN1EncObjectIdentifier();
687 void ConstructL(const TDesC& aStr);
689 // Methods from CASN1EncBase
690 void CalculateContentsLengthDER();
691 void WriteContentsDERL(TDes8& aBuf) const;
695 RArray<TASN1EncBase128DER> iData;
701 * Class for encoding GeneralisedTime objects.
703 * Doesn't support fractions of seconds or regional time zone offsets.
708 class CASN1EncGeneralizedTime : public CASN1EncPrimitive
711 IMPORT_C static CASN1EncGeneralizedTime* NewLC(const TTime& aTime);
712 IMPORT_C static CASN1EncGeneralizedTime* NewL(const TTime& aTime);
715 CASN1EncGeneralizedTime(const TTime& aTime);
717 // Methods from CASN1EncBase
718 void CalculateContentsLengthDER();
719 void WriteContentsDERL(TDes8& aBuf) const;
726 * Class for encapsulation of already encoded data.
728 * Wraps it so that the data could be used in the ASN.1 hierarchy.
729 * It reverse-engineers and stores the encoded data, providing whatever
730 * information is needed to override pure virtual methods of the base
731 * class and write out the DER encoding in its initial form.
736 class CASN1EncEncoding : public CASN1EncBase
740 * Creates a new object from raw DER encoding and places it on the
742 * @param aEncoding Raw DER encoding.
743 * @return New wrapper object placed on the cleanup stack.
745 IMPORT_C static CASN1EncEncoding* NewLC(const TDesC8& aEncoding);
748 * Creates a new object from raw DER encoding.
749 * @param aEncoding Raw DER encoding.
750 * @return New wrapper object.
752 IMPORT_C static CASN1EncEncoding* NewL(const TDesC8& aEncoding);
754 IMPORT_C static CASN1EncEncoding* NewLC(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass);
756 IMPORT_C ~CASN1EncEncoding();
760 * Protected constructor.
762 * @param aType Tag type of the new object
763 * @param aClass Tag class of the new object.
766 IMPORT_C CASN1EncEncoding();
770 * Constructs the wrapper around the passed raw DER encoding.
771 * Calculates element sizes. Decodes it to get type and length.
772 * @param aEncoding Raw DER encoding.
774 void ConstructL(const TDesC8& aEncoding);
776 void ConstructL(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass);
778 virtual TBool IsConstructed() const;
781 * Writes DER content encoding into supplied buffer, which is
782 * already verified to be big enough.
783 * @param aBuf Buffer to write to.
785 virtual void WriteContentsDERL(TDes8& aBuf) const;
788 * Calculates number of octets in DER content encoding. Sets
789 * value of the appropriate data member.
791 virtual void CalculateContentsLengthDER();
794 HBufC8* iContents; ///< Copy of the supplied DER encoded data (contents only).
795 TASN1Class iClass; ///< ASN.1 class of the encoded object.
796 TTagType iTag; ///< ASN.1 tag of the encoding.
799 #endif // __ASN1ENC_H__