cms/src/CCMSKeyIdentifier.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    "CCMSKeyIdentifier.h"
       
    21 #include <asn1dec.h>
       
    22 #include <asn1enc.h>
       
    23 
       
    24 // CONSTANTS
       
    25 const TInt KMinNumberOfSubModules = 1;
       
    26 const TInt KMaxNumberOfSubModules = 3;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CCMSKeyIdentifier::CCMSKeyIdentifier
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CCMSKeyIdentifier::CCMSKeyIdentifier()
       
    37     {
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CCMSKeyIdentifier::ConstructL
       
    42 // Symbian 2nd phase constructor can leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C void CCMSKeyIdentifier::ConstructL(
       
    46 	const TDesC8& aKeyIdentifier )
       
    47     {
       
    48 	SetKeyIdentifierL( aKeyIdentifier );
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CCMSKeyIdentifier::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C void CCMSKeyIdentifier::ConstructL(
       
    57 	const TDesC8& aKeyIdentifier,
       
    58     const TTime& aDate )
       
    59     {
       
    60 	SetKeyIdentifierL( aKeyIdentifier );
       
    61     SetDateL( aDate );
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CCMSKeyIdentifier::NewL
       
    66 // Two-phased constructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CCMSKeyIdentifier* CCMSKeyIdentifier::NewL()
       
    70 	{
       
    71 	// creating with empty/default values
       
    72 	CCMSKeyIdentifier* self = NewL( KNullDesC8() );
       
    73 	return self;
       
    74 	}
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CCMSKeyIdentifier::NewL
       
    78 // Two-phased copy constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C CCMSKeyIdentifier* CCMSKeyIdentifier::NewL(
       
    82     const TDesC8& aKeyIdentifier )
       
    83 	{
       
    84     CCMSKeyIdentifier* self =
       
    85     	new( ELeave ) CCMSKeyIdentifier();
       
    86 
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL( aKeyIdentifier );
       
    89     CleanupStack::Pop( self );
       
    90 	return self;
       
    91 	}
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CCMSKeyIdentifier::NewL
       
    95 // Two-phased copy constructor.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 EXPORT_C CCMSKeyIdentifier* CCMSKeyIdentifier::NewL(
       
    99     const TDesC8& aKeyIdentifier,
       
   100     const TTime& aDate )
       
   101 	{
       
   102     CCMSKeyIdentifier* self =
       
   103     	new( ELeave ) CCMSKeyIdentifier();
       
   104 
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL( aKeyIdentifier, aDate );
       
   107     CleanupStack::Pop( self );
       
   108 	return self;
       
   109 	}
       
   110 
       
   111 // Destructor
       
   112 CCMSKeyIdentifier::~CCMSKeyIdentifier()
       
   113     {
       
   114 	delete iKeyIdentifier;
       
   115 	delete iDate;
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CCMSKeyIdentifier::DecodeL
       
   120 // Decrypts raw data to this instance
       
   121 // -----------------------------------------------------------------------------
       
   122 void CCMSKeyIdentifier::DecodeL( const TDesC8& aRawData )
       
   123 	{
       
   124     CArrayPtr< TASN1DecGeneric >* itemsData =
       
   125         DecodeSequenceLC( aRawData, KMinNumberOfSubModules,
       
   126                           KMaxNumberOfSubModules );
       
   127 
       
   128     DecodeArrayL( itemsData );
       
   129 
       
   130     CleanupStack::PopAndDestroy( itemsData );
       
   131 	}
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CCMSKeyIdentifier::EncoderLC
       
   135 // Returns ASN1 encoder for this instance
       
   136 // -----------------------------------------------------------------------------
       
   137 
       
   138 CASN1EncBase* CCMSKeyIdentifier::EncoderLC() const
       
   139 	{
       
   140     CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   141 
       
   142     // encode the subjectKeyIdentifier
       
   143     CASN1EncOctetString* subjectKeyIdentifier =
       
   144         CASN1EncOctetString::NewLC( *iKeyIdentifier );
       
   145     root->AddAndPopChildL( subjectKeyIdentifier );
       
   146 
       
   147     // encode date if existing
       
   148     if( iDate )
       
   149         {
       
   150         CASN1EncGeneralizedTime* date =
       
   151             CASN1EncGeneralizedTime::NewLC( *iDate );
       
   152         root->AddAndPopChildL( date );
       
   153         }
       
   154 
       
   155     return root;
       
   156 	}
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CCMSKeyIdentifier::KeyIdentifier
       
   160 // Subject Key identifier getter
       
   161 // -----------------------------------------------------------------------------
       
   162 EXPORT_C const TDesC8&
       
   163 CCMSKeyIdentifier::KeyIdentifier() const
       
   164 	{
       
   165 	return *iKeyIdentifier;
       
   166 	}
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CCMSKeyIdentifier::Date
       
   170 // Date getter
       
   171 // -----------------------------------------------------------------------------
       
   172 EXPORT_C const TTime* CCMSKeyIdentifier::Date() const
       
   173 	{
       
   174 	return iDate;
       
   175 	}
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CCMSKeyIdentifier::SetKeyIdentifierL
       
   179 // KeyIdentifier setter
       
   180 // -----------------------------------------------------------------------------
       
   181 EXPORT_C void CCMSKeyIdentifier::SetKeyIdentifierL(
       
   182 	const TDesC8& aKeyIdentifier )
       
   183 	{
       
   184     HBufC8* subjectKeyIdentifier = aKeyIdentifier.AllocL();
       
   185     delete iKeyIdentifier;
       
   186     iKeyIdentifier = subjectKeyIdentifier;
       
   187 	}
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CCMSKeyIdentifier::SetDateL
       
   191 // Date setter
       
   192 // -----------------------------------------------------------------------------
       
   193 EXPORT_C void CCMSKeyIdentifier::SetDateL(
       
   194 	const TTime& aDate )
       
   195 	{
       
   196     TTime* date = new( ELeave ) TTime( aDate );
       
   197     delete iDate;
       
   198     iDate = date;
       
   199 	}
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CCMSKeyIdentifier::DecodeImplicitTagL
       
   203 // Decrypts raw data with implicit tag
       
   204 // -----------------------------------------------------------------------------
       
   205 void CCMSKeyIdentifier::DecodeImplicitTagL(
       
   206     const TDesC8& aRawData,
       
   207     const TTagType aImplicitTag )
       
   208     {
       
   209     CArrayPtr< TASN1DecGeneric >* items = NULL;
       
   210 
       
   211     // Check the tag
       
   212     TASN1DecGeneric decGen( aRawData );
       
   213     decGen.InitL();
       
   214     // Accept only given tag
       
   215     if( decGen.Tag() != aImplicitTag )
       
   216         {
       
   217         User::Leave( KErrArgument );
       
   218         }
       
   219     TASN1DecSequence decSeq;
       
   220     items = decSeq.DecodeDERLC( decGen );
       
   221     TInt itemCount = items->Count();
       
   222     if( ( itemCount > KMaxNumberOfSubModules ) ||
       
   223         ( itemCount < KMinNumberOfSubModules ) )
       
   224         {
       
   225         User::Leave( KErrArgument );
       
   226         }
       
   227     DecodeArrayL( items );
       
   228     CleanupStack::PopAndDestroy( items );
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CCMSKeyIdentifier::DecodeArrayL
       
   233 // Decodes data from an array of decoders
       
   234 // -----------------------------------------------------------------------------
       
   235 void CCMSKeyIdentifier::DecodeArrayL(
       
   236     CArrayPtr< TASN1DecGeneric >* aItems )
       
   237     {
       
   238     // Caller should have made sure that there is at least one item
       
   239     __ASSERT_DEBUG( ( aItems->Count() > 0 ), User::Invariant() );
       
   240     
       
   241     // decode subjectKeyIdentifier
       
   242     TASN1DecGeneric gen( *aItems->At( 0 ) );
       
   243     gen.InitL();
       
   244     TASN1DecOctetString octetStringDecoder;
       
   245     HBufC8* tmp =
       
   246         octetStringDecoder.DecodeDERL( gen );
       
   247     delete iKeyIdentifier;
       
   248     iKeyIdentifier = tmp;
       
   249 
       
   250     // decode date if exists
       
   251     if( aItems->Count() > 1 )
       
   252         {
       
   253         TInt pos = 0;
       
   254         TASN1DecGeneralizedTime dateDec;
       
   255         TTime date = dateDec.DecodeDERL( aItems->At( 1 )->Encoding(), pos );
       
   256         SetDateL( date );
       
   257         }
       
   258     else
       
   259         {
       
   260         delete iDate;
       
   261         iDate = NULL;
       
   262         }
       
   263 
       
   264     // possible OtherKeyAttribute is not supported / ignored.
       
   265     }
       
   266 
       
   267 //  End of File