os/security/cryptoservices/certificateandkeymgmt/inc/asn1dec.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * ASN1 decoder
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file 
    22  @publishedAll
    23  @released
    24 */
    25  
    26 #ifndef __ASN1DEC_H__
    27 #define __ASN1DEC_H__
    28 
    29 #include <e32std.h>
    30 #include <e32base.h>
    31 #include <asn1cons.h>
    32 
    33 class RInteger;
    34 
    35 
    36 class TASN1DecGeneric
    37 	{
    38 public:
    39 	IMPORT_C explicit TASN1DecGeneric(const TDesC8& aSource);
    40 	IMPORT_C void InitL();
    41 	IMPORT_C TPtrC8 Encoding() const;
    42 	IMPORT_C TInt LengthDER() const;
    43 	IMPORT_C TPtrC8 GetContentDER(void) const;
    44 	IMPORT_C TInt LengthDERContent(void) const;
    45 	IMPORT_C TInt LengthDERHeader() const;
    46 	IMPORT_C TTagType Tag() const;
    47 	IMPORT_C TASN1Class Class() const;
    48 private:
    49 	TPtrC8 iEncoding;
    50 	TInt iStartOfContents;
    51 	TInt iLength;
    52 	TTagType iExplicitTag;
    53 	TASN1Class iClass;
    54 	};
    55 
    56 class TASN1DecString
    57 	{
    58 public:
    59 protected:
    60 	IMPORT_C TASN1DecString(const TTagType aTag);
    61 	virtual TInt CheckValid(const TDesC8& aSource)=0;
    62 	};
    63 
    64 class TASN1DecBoolean
    65 	{
    66 public:
    67 	IMPORT_C TASN1DecBoolean();
    68 	IMPORT_C TBool DecodeDERL(const TDesC8& aSource,TInt& aPos);
    69 	IMPORT_C TBool DecodeDERL(const TASN1DecGeneric& Source);
    70 private:
    71 	};
    72 
    73 class TASN1DecNull
    74 	{
    75 public:
    76 	IMPORT_C TASN1DecNull();
    77 	IMPORT_C void DecodeDERL(const TDesC8& aSource,TInt& aPos);
    78 	IMPORT_C void DecodeDERL(const TASN1DecGeneric& aSource);
    79 	};
    80 
    81 class TASN1DecEnumerated
    82 	{
    83 public:
    84 	IMPORT_C TASN1DecEnumerated(void);
    85 	};
    86 
    87 class TASN1DecInteger
    88 	{
    89 public:
    90 	IMPORT_C TASN1DecInteger();
    91 	IMPORT_C TInt DecodeDERShortL(const TDesC8& aSource,TInt& aPos);
    92 	IMPORT_C TInt DecodeDERShortL(const TASN1DecGeneric& aSource);
    93 	IMPORT_C RInteger DecodeDERLongL(const TDesC8& aSource,TInt& aPos);
    94 	IMPORT_C RInteger DecodeDERLongL(const TASN1DecGeneric& aSource);
    95 	};
    96 
    97 const TInt KNumberOfIDs=15;
    98 
    99 class TASN1DecObjectIdentifier
   100 	{
   101 public:
   102 	IMPORT_C TASN1DecObjectIdentifier();
   103 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   104 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& aSource);
   105 private:
   106 	TInt DecodeContentsL(TFixedArray<TInt, KNumberOfIDs>& aOID, const TDesC8& aSource);
   107 	};
   108 
   109 class TASN1DecSet
   110 	{
   111 public:
   112 	IMPORT_C TASN1DecSet();
   113 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* NewDERLC(const TDesC8& aSource);
   114 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TDesC8& aSource,TInt& aPos);
   115 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TASN1DecGeneric& aSource);
   116 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TDesC8& aSource,TInt& aPos, TInt aMin, TInt aMax);
   117 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TASN1DecGeneric& aSource, TInt aMin, TInt aMax);
   118 protected:
   119 	/** @internalComponent */
   120 	void DecodeContentsL(const TDesC8& aSource, TInt& aPos, CArrayPtrFlat<TASN1DecGeneric>& aDest);
   121 	/** @internalComponent */
   122 	static void CleanupSet(TAny* aArray);	
   123 	};
   124 
   125 class TASN1DecGraphicString
   126 	{
   127 public:
   128 	IMPORT_C TASN1DecGraphicString();
   129 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   130 protected:
   131 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   132 private:
   133 	HBufC* DecodeContentsL(const TDesC8& aSource);
   134 	};
   135 
   136 class TASN1DecVisibleString
   137 	{
   138 public:
   139 	IMPORT_C TASN1DecVisibleString(void);
   140 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   141 protected:
   142 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   143 private:
   144 	HBufC* DecodeContentsL(const TDesC8& aSource);
   145 	};
   146 
   147 class TASN1DecGeneralizedTime
   148 	{
   149 public:
   150 	IMPORT_C TASN1DecGeneralizedTime();
   151 	IMPORT_C TTime DecodeDERL(const TDesC8& aSource, TInt& aPos);
   152 	IMPORT_C TTime DecodeDERL(const TASN1DecGeneric& aGen);
   153 private:
   154 	TTime GetTimeL(const TDesC8& aSource);
   155 	};
   156 
   157 class TASN1DecUTCTime
   158 	{
   159 public:
   160 	IMPORT_C TASN1DecUTCTime();
   161 	IMPORT_C TTime DecodeDERL(const TASN1DecGeneric& aGen);
   162 	IMPORT_C TTime DecodeDERL(const TDesC8& aSource, TInt& aPos);
   163 private:
   164 	TTime GetTimeL(const TDesC8& aSource);
   165 	};
   166 
   167 class TASN1DecBitString
   168 	{
   169 public:
   170 	IMPORT_C TASN1DecBitString();
   171 	IMPORT_C HBufC8* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   172 	IMPORT_C HBufC8* DecodeDERL(const TASN1DecGeneric& aSource);
   173 	//we need the next 2 functions because many standards encode things as bit
   174 	//strings
   175 	IMPORT_C HBufC8* ExtractOctetStringL(const TDesC8& aSource,TInt& aPos);
   176 	IMPORT_C HBufC8* ExtractOctetStringL(const TASN1DecGeneric& aSource);
   177 	};
   178 
   179 class TASN1DecIA5String
   180 	{
   181 public:
   182 	IMPORT_C TASN1DecIA5String();
   183 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   184 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& aSource);
   185 protected:
   186 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   187 private:
   188 	HBufC* DecodeContentsL(const TDesC8& aSource);
   189 	};
   190 
   191 
   192 class TASN1DecUTF8String
   193 	{
   194 public:
   195 	IMPORT_C TASN1DecUTF8String();
   196 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   197 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& aSource);
   198 protected:
   199 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   200 private:
   201 	HBufC* DecodeContentsL(const TDesC8& aSource);
   202 	};
   203 
   204 class TASN1DecGeneralString
   205 	{
   206 public:
   207 	IMPORT_C TASN1DecGeneralString();
   208 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   209 protected:
   210 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   211 private:
   212 	HBufC* DecodeContentsL(const TDesC8& aSource);
   213 	};
   214 
   215 class TASN1DecOctetString
   216 	{
   217 public:
   218 	IMPORT_C TASN1DecOctetString();
   219 	IMPORT_C HBufC8* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   220 	IMPORT_C HBufC8* DecodeDERL(const TASN1DecGeneric& aSource);
   221 protected:
   222 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   223 	};
   224 
   225 class TASN1DecNumericString
   226 	{
   227 public:
   228 	IMPORT_C TASN1DecNumericString();
   229 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   230 protected:
   231 	/** @internalComponent */
   232 	TInt CheckValid(const TDesC8& aSource);
   233 private:
   234 	HBufC* DecodeContentsL(const TDesC8& aSource);
   235 	};
   236 
   237 class TASN1DecVideotexString
   238 	{
   239 public:
   240 	IMPORT_C TASN1DecVideotexString();
   241 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   242 protected:
   243 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   244 private:
   245 	HBufC* DecodeContentsL(const TDesC8& aSource);
   246 	};
   247 
   248 class TASN1DecPrintableString
   249 	{
   250 public:
   251 	IMPORT_C TASN1DecPrintableString();
   252 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   253 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& aSource);
   254 protected:
   255 	/** @internalComponent */
   256 	TInt CheckValid(const TDesC8& aSource);
   257 private:
   258 	HBufC* DecodeContentsL(const TDesC8& aSource);
   259 	};
   260 
   261 /**
   262  * Class used for decoding Teletex strings.
   263  * The TeletexString type is not fully supported by this library.
   264  * Instead the decode methods perform a direct conversion from 8 to 16bits by adding 
   265  * null characters in the second byte of each character. This will work as expected 
   266  * for cases where the string contains ASCII data.
   267  * As this is non-destructive, the original data can be retrieved by performing 
   268  * a Copy to an 8-bit descriptor.
   269  */
   270 class TASN1DecTeletexString
   271 	{
   272 public:
   273 	IMPORT_C TASN1DecTeletexString();
   274 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   275 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& Source);
   276 protected:
   277 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   278 private:
   279 	HBufC* DecodeContentsL(const TDesC8& aSource);
   280 	};
   281 
   282 /**
   283 Class used for decoding ASN.1 BMP strings.
   284  */
   285 class TASN1DecBMPString
   286 	{
   287 public:
   288 	IMPORT_C TASN1DecBMPString();
   289 	IMPORT_C HBufC* DecodeDERL(const TDesC8& aSource,TInt& aPos);
   290 	IMPORT_C HBufC* DecodeDERL(const TASN1DecGeneric& aSource);
   291 protected:
   292 	IMPORT_C TInt CheckValid(const TDesC8& aSource);
   293 private:
   294 	HBufC* DecodeContentsL(const TDesC8& aSource);
   295 	};
   296 
   297 class TASN1DecSequence
   298 	{
   299 public:
   300 	IMPORT_C TASN1DecSequence();
   301 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TDesC8& aSource,TInt& aPos);
   302 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TASN1DecGeneric& aSource);
   303 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TDesC8& aSource,TInt& aPos, TInt aMin, TInt aMax);
   304 	IMPORT_C CArrayPtrFlat<TASN1DecGeneric>* DecodeDERLC(const TASN1DecGeneric& aSource, TInt aMin, TInt aMax);
   305 protected:
   306 	/** @internalComponent */
   307 	void DecodeContentsL(const TDesC8& aSource, CArrayPtrFlat<TASN1DecGeneric>& aDest);
   308 	/** @internalComponent */
   309 	static void CleanupSequence(TAny* aArray);
   310 	};
   311 
   312 #endif