cms/src/CCMSX509GeneralName.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2004 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 "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 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "CCMSX509GeneralName.h"
       
    21 #include <asn1dec.h>
       
    22 #include <asn1enc.h>
       
    23 
       
    24 // CONSTANTS
       
    25 const TTagType KMaxTag = 8;
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CCMSX509GeneralName::CCMSX509GeneralName
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CCMSX509GeneralName::CCMSX509GeneralName( )
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CCMSX509GeneralName::ConstructL
       
    41 // Symbian 2nd phase constructor can leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 EXPORT_C void CCMSX509GeneralName::ConstructL(
       
    45     const TTagType aTag,
       
    46     const TDesC8& aData )
       
    47     {
       
    48     SetTagL( aTag );
       
    49     SetDataL( aData );
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CCMSX509GeneralName::NewL
       
    54 // Two-phased constructor.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CCMSX509GeneralName* CCMSX509GeneralName::NewL()
       
    58 	{
       
    59 	// creating with empty/default values
       
    60 	CCMSX509GeneralName* self = NewL( 0, KNullDesC8() );
       
    61 	return self;
       
    62 	}
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CCMSX509GeneralName::NewL
       
    66 // Two-phased constructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CCMSX509GeneralName* CCMSX509GeneralName::NewL(
       
    70     const TTagType aTag,
       
    71     const TDesC8& aData )
       
    72     {
       
    73     CCMSX509GeneralName* self =
       
    74     	new( ELeave ) CCMSX509GeneralName( );
       
    75 
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL( aTag, aData );
       
    78     CleanupStack::Pop();
       
    79 
       
    80     return self;
       
    81     }
       
    82 
       
    83 // Destructor
       
    84 CCMSX509GeneralName::~CCMSX509GeneralName()
       
    85     {
       
    86     delete iData;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CCMSX509GeneralName::DecodeL
       
    91 // Decrypts raw data to this instance
       
    92 // -----------------------------------------------------------------------------
       
    93 void CCMSX509GeneralName::DecodeL( const TDesC8& aRawData )
       
    94 	{
       
    95     TASN1DecGeneric decGen( aRawData );
       
    96     decGen.InitL();
       
    97     TTagType tag = decGen.Tag();
       
    98     if( tag > KMaxTag )
       
    99         {
       
   100         User::Leave( KErrArgument );
       
   101         }
       
   102     HBufC8* data = decGen.GetContentDER().AllocL();
       
   103     iTag = tag;
       
   104     delete iData;
       
   105     iData = data;
       
   106 	}
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CCMSX509GeneralName::EncodeL
       
   110 // Encrypts this instance to descriptor
       
   111 // -----------------------------------------------------------------------------
       
   112 
       
   113 void CCMSX509GeneralName::EncodeL( HBufC8*& aResult ) const
       
   114 	{
       
   115 	CASN1EncBase* root = EncoderLC();
       
   116 
       
   117 	// encode the object in a DER encoding
       
   118     HBufC8* der = HBufC8::NewMaxLC( root->LengthDER() );
       
   119     TPtr8 pder( der->Des() );
       
   120     TUint pos = 0;
       
   121     root->WriteDERL( pder, pos );
       
   122     CleanupStack::Pop( der );
       
   123     aResult = der;
       
   124 
       
   125 	CleanupStack::PopAndDestroy( root );
       
   126 	}
       
   127 // -----------------------------------------------------------------------------
       
   128 // CCMSX509GeneralName::EncoderLC
       
   129 // Returns ASN1 encoder for this instance
       
   130 // -----------------------------------------------------------------------------
       
   131 
       
   132 CASN1EncBase* CCMSX509GeneralName::EncoderLC() const
       
   133 	{
       
   134     TASN1DecGeneric genDec( *iData );
       
   135     genDec.InitL();
       
   136     TTagType dataTag( genDec.Tag() );
       
   137     TASN1Class dataClass( genDec.Class() );
       
   138     TUint8 fullTag = *( iData->Ptr() );
       
   139     CASN1EncBase* baseEnc = NULL;
       
   140 
       
   141     // is the data constructed?
       
   142     if( ( fullTag & 0x20 ) != 0 )
       
   143         {
       
   144         baseEnc = CASN1EncEncoding::NewL( *iData );
       
   145         }
       
   146     else
       
   147         {
       
   148         TASN1DecOctetString dataDec;
       
   149         TInt pos = 0;
       
   150         HBufC8* data = dataDec.DecodeDERL( *iData, pos );
       
   151         CleanupStack::PushL( data );
       
   152         baseEnc = CASN1EncOctetString::NewL( *data );
       
   153         CleanupStack::PopAndDestroy( data );
       
   154         }
       
   155     baseEnc->SetTag( dataTag, dataClass );
       
   156     // CASN1EncExplicitTag takes ownership of dataEnc, even when Leaving
       
   157     CASN1EncExplicitTag* taggedEnc =
       
   158         CASN1EncExplicitTag::NewLC( baseEnc, iTag );
       
   159 
       
   160     return taggedEnc;
       
   161 	}
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CCMSX509GeneralName::Tag
       
   165 // Tag getter.
       
   166 // -----------------------------------------------------------------------------
       
   167 EXPORT_C TTagType CCMSX509GeneralName::Tag() const
       
   168 	{
       
   169 	return iTag;
       
   170 	}
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CCMSX509GeneralName::Data
       
   174 // Data getter.
       
   175 // -----------------------------------------------------------------------------
       
   176 EXPORT_C const TDesC8& CCMSX509GeneralName::Data() const
       
   177 	{
       
   178 	return *iData;
       
   179 	}
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CCMSX509GeneralName::SetTagL
       
   183 // Tag setter
       
   184 // -----------------------------------------------------------------------------
       
   185 EXPORT_C void CCMSX509GeneralName::SetTagL(
       
   186 	const TTagType aTag )
       
   187 	{
       
   188     if( aTag > KMaxTag )
       
   189         {
       
   190         User::Leave( KErrArgument );
       
   191         }
       
   192     iTag = aTag;
       
   193 	}
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CCMSX509GeneralName::SetDataL
       
   197 // Data setter
       
   198 // -----------------------------------------------------------------------------
       
   199 EXPORT_C void CCMSX509GeneralName::SetDataL(
       
   200 	const TDesC8& aData )
       
   201 	{
       
   202     HBufC8* data = aData.AllocL();
       
   203     delete iData;
       
   204     iData = data;
       
   205 	}
       
   206 
       
   207 //  End of File