cms/src/CCMSAttribute.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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    "CCMSAttribute.h"
       
    21 #include "TCMSTimeUtil.h"
       
    22 #include <x500dn.h>
       
    23 #include <asn1dec.h>
       
    24 #include <asn1enc.h>
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KMaxNumberOfSubModules = 3;
       
    28 const TInt KMinNumberOfSubModules = 2;
       
    29 const TInt KDefaultGranularity = 1;
       
    30 
       
    31 // CMS SignedAttributes useful types
       
    32 _LIT( KContentTypeOID, "1.2.840.113549.1.9.3" );
       
    33 _LIT( KMessageDigestOID, "1.2.840.113549.1.9.4" );
       
    34 _LIT( KSignTimeOID, "1.2.840.113549.1.9.5" );
       
    35 
       
    36 // ============================= LOCAL FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CMSCreateEncoderLC Creates an encoder based on the gived OID value.
       
    40 // Returns: Encoder for the given encoding
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CASN1EncBase* CMSCreateEncoderLC( const TDesC& aAttributeType, const TDesC8& aEncoding )
       
    44     {
       
    45     CASN1EncBase* retVal = NULL;
       
    46     if( aAttributeType == KContentTypeOID )
       
    47         {
       
    48         // encoding data, have to decode it before encoding it again
       
    49         // because CASN1EncEncoding changes TagType to Constructed
       
    50         TASN1DecObjectIdentifier dataDec;
       
    51         TInt pos = 0;
       
    52         HBufC* data = dataDec.DecodeDERL( aEncoding, pos );
       
    53         CleanupStack::PushL( data );
       
    54         retVal = CASN1EncObjectIdentifier::NewL( *data );
       
    55         CleanupStack::PopAndDestroy( data );
       
    56         CleanupStack::PushL( retVal );
       
    57         }
       
    58     else if( aAttributeType == KMessageDigestOID )
       
    59         {
       
    60         TASN1DecOctetString dataDec;
       
    61         TInt pos = 0;
       
    62         HBufC8* data = dataDec.DecodeDERL( aEncoding, pos );
       
    63         CleanupStack::PushL( data );
       
    64         retVal = CASN1EncOctetString::NewL( *data );
       
    65         CleanupStack::PopAndDestroy( data );
       
    66         CleanupStack::PushL( retVal );
       
    67         }
       
    68     else if( aAttributeType == KSignTimeOID )
       
    69         {
       
    70         TTime time = TCMSTimeUtil::ConvertToTimeL( aEncoding );
       
    71         retVal = TCMSTimeUtil::ConvertToEncoderLC( time );
       
    72         }
       
    73     else
       
    74         {
       
    75         // default is to wrap inside encencoder
       
    76         retVal = CASN1EncEncoding::NewLC( aEncoding );
       
    77         }
       
    78     return retVal;
       
    79     }
       
    80 
       
    81 // ============================ MEMBER FUNCTIONS ===============================
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CCMSAttribute::CCMSAttribute
       
    85 // C++ default constructor can NOT contain any code, that
       
    86 // might leave.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C CCMSAttribute::CCMSAttribute()
       
    90     {
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CCMSAttribute::ConstructL
       
    95 // Symbian 2nd phase constructor can leave.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 EXPORT_C void CCMSAttribute::ConstructL()
       
    99     {
       
   100 	// creating with empty values
       
   101 	iAttributeType = KNullDesC().AllocL();
       
   102 
       
   103 	iAttributeValues =
       
   104 		new(ELeave) CDesC8ArrayFlat( KDefaultGranularity );
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CCMSAttribute::ConstructL
       
   109 // Symbian 2nd phase constructor can leave.
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 EXPORT_C void CCMSAttribute::ConstructL(
       
   113 	const TDesC& aAttributeType,
       
   114 	const CDesC8Array& aAttributeValues )
       
   115     {
       
   116 	SetAttributeTypeL( aAttributeType );
       
   117 	SetAttributeValuesL( aAttributeValues );
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CCMSAttribute::ConstructL
       
   122 // Symbian 2nd phase constructor can leave.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C void CCMSAttribute::ConstructL(
       
   126 	const TDesC& aAttributeType,
       
   127 	const TDesC8& aAttributeValue )
       
   128     {
       
   129 	SetAttributeTypeL( aAttributeType );
       
   130 	iAttributeValues =
       
   131 		new(ELeave) CDesC8ArrayFlat( KDefaultGranularity );
       
   132 	iAttributeValues->AppendL( aAttributeValue );
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CCMSAttribute::NewLC
       
   137 // Two-phased constructor.
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C CCMSAttribute* CCMSAttribute::NewLC()
       
   141 	{
       
   142 	CCMSAttribute* self = new( ELeave ) CCMSAttribute();
       
   143     CleanupStack::PushL( self );
       
   144     self->ConstructL();
       
   145     return self;
       
   146 	}
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CCMSAttribute::NewL
       
   150 // Two-phased constructor.
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 EXPORT_C CCMSAttribute* CCMSAttribute::NewL()
       
   154 	{
       
   155 	CCMSAttribute* self = NewLC();
       
   156     CleanupStack::Pop( self );
       
   157     return self;
       
   158 	}
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CCMSAttribute::NewL
       
   162 // Two-phased constructor.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 EXPORT_C CCMSAttribute* CCMSAttribute::NewL(
       
   166 	const TDesC& aAttributeType,
       
   167 	const CDesC8Array& aAttributeValues )
       
   168 	{
       
   169 	CCMSAttribute* self = NewLC( aAttributeType, aAttributeValues );
       
   170 	CleanupStack::Pop( self );
       
   171     return self;
       
   172 	}
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CCMSAttribute::NewLC
       
   176 // Two-phased constructor.
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 EXPORT_C CCMSAttribute* CCMSAttribute::NewLC(
       
   180 	const TDesC& aAttributeType,
       
   181 	const CDesC8Array& aAttributeValues )
       
   182     {
       
   183     CCMSAttribute* self = new( ELeave ) CCMSAttribute();
       
   184     CleanupStack::PushL( self );
       
   185     self->ConstructL( aAttributeType, aAttributeValues );
       
   186     return self;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CCMSAttribute::NewLC
       
   191 // Two-phased constructor.
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 EXPORT_C CCMSAttribute* CCMSAttribute::NewLC(
       
   195 	const TDesC& aAttributeType,
       
   196 	const TDesC8& aAttributeValue )
       
   197 	{
       
   198 	CCMSAttribute* self = new( ELeave ) CCMSAttribute();
       
   199 	CleanupStack::PushL( self );
       
   200 	self->ConstructL( aAttributeType, aAttributeValue );
       
   201     return self;
       
   202 	}
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CCMSAttribute::NewL
       
   206 // Two-phased constructor.
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 EXPORT_C CCMSAttribute* CCMSAttribute::NewL(
       
   210 	const TDesC& aAttributeType,
       
   211 	const TDesC8& aAttributeValue )
       
   212 	{
       
   213 	CCMSAttribute* self = NewLC( aAttributeType, aAttributeValue );
       
   214 	CleanupStack::Pop( self );
       
   215     return self;
       
   216 	}
       
   217 
       
   218 // Destructor
       
   219 CCMSAttribute::~CCMSAttribute()
       
   220     {
       
   221 	delete iAttributeType;
       
   222 	delete iAttributeValues;
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CCMSAttribute::DecodeL
       
   227 // Decrypts raw data to this instance
       
   228 // -----------------------------------------------------------------------------
       
   229 void CCMSAttribute::DecodeL( const TDesC8& aRawData )
       
   230 	{
       
   231 	CArrayPtr<TASN1DecGeneric>* itemsData =
       
   232         DecodeSequenceLC( aRawData,
       
   233                           KMinNumberOfSubModules,
       
   234                           KMaxNumberOfSubModules );
       
   235 	// we would not get this far if there is not 2 or 3 elements
       
   236 	// decoding attribute type
       
   237 	TASN1DecObjectIdentifier decOid;
       
   238 	HBufC* oid = decOid.DecodeDERL( *itemsData->At( 0 ) );
       
   239 	delete iAttributeType;
       
   240 	iAttributeType = oid;
       
   241 
       
   242 	// deocoding attribute values
       
   243 	TASN1DecSet decSet;
       
   244 	CArrayPtrFlat<TASN1DecGeneric>* attValues =
       
   245 					decSet.DecodeDERLC( *itemsData->At( 1 ) );
       
   246 	TInt numOfAttValues = attValues->Count();
       
   247 	CDesC8ArrayFlat* tmpArray = new( ELeave ) CDesC8ArrayFlat( numOfAttValues );
       
   248 	CleanupStack::PushL( tmpArray );
       
   249 	for( TInt i = 0; i < numOfAttValues; i++ )
       
   250 		{
       
   251 		tmpArray->AppendL( attValues->At( i )->Encoding() );
       
   252 		}
       
   253 	delete iAttributeValues;
       
   254 	iAttributeValues = tmpArray;
       
   255 	// compress to get rid of possible unneccessary slots
       
   256 	iAttributeValues->Compress();
       
   257 
       
   258     // possible context values are ignored
       
   259 
       
   260 	CleanupStack::Pop( tmpArray );
       
   261 	CleanupStack::PopAndDestroy( attValues );
       
   262 	CleanupStack::PopAndDestroy( itemsData );
       
   263 	}
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CCMSAttribute::EncoderLC
       
   267 // Returns ASN1 encoder for this instance
       
   268 // -----------------------------------------------------------------------------
       
   269 
       
   270 CASN1EncBase* CCMSAttribute::EncoderLC() const
       
   271 	{
       
   272 	CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   273 
       
   274 	// add attribute type
       
   275 	CASN1EncObjectIdentifier* oid = CASN1EncObjectIdentifier::NewLC( *iAttributeType );
       
   276 	root->AddAndPopChildL( oid );
       
   277 
       
   278 	// add attribute values
       
   279 	CASN1EncSequence* values = CASN1EncSequence::NewLC();
       
   280 
       
   281 	// change tag to SET
       
   282 	values->SetTag( EASN1Set, EUniversal );
       
   283 
       
   284 	TInt numOfValues = iAttributeValues->Count();
       
   285 	for( TInt i = 0; i < numOfValues; i++ )
       
   286 		{
       
   287         CASN1EncBase* enc = CMSCreateEncoderLC( *iAttributeType, ( *iAttributeValues )[ i ] );
       
   288 		values->AddAndPopChildL( enc );
       
   289 		}
       
   290 
       
   291 	root->AddAndPopChildL( values );
       
   292 	return root;
       
   293 	}
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CCMSAttribute::AttributeType
       
   297 // Getter for attribute type
       
   298 // -----------------------------------------------------------------------------
       
   299 EXPORT_C const TDesC& CCMSAttribute::AttributeType() const
       
   300 	{
       
   301 	return *iAttributeType;
       
   302 	}
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CCMSAttribute::AttributeValues
       
   306 // Getter for attribute values
       
   307 // -----------------------------------------------------------------------------
       
   308 EXPORT_C const CDesC8Array& CCMSAttribute::AttributeValues() const
       
   309 	{
       
   310 	return *iAttributeValues;
       
   311 	}
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CCMSAttribute::SetAttributeTypeL
       
   315 // Setter for attribute type, takes copy
       
   316 // -----------------------------------------------------------------------------
       
   317 EXPORT_C void CCMSAttribute::SetAttributeTypeL( const TDesC& aAttributeType )
       
   318 	{
       
   319 	HBufC* tmp = aAttributeType.AllocL();
       
   320 	delete iAttributeType;
       
   321 	iAttributeType = tmp;
       
   322 	}
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CCMSAttribute::SetAttributeTypeL
       
   326 // Setter for attribute values, takes copy
       
   327 // -----------------------------------------------------------------------------
       
   328 EXPORT_C void CCMSAttribute::SetAttributeValuesL(
       
   329 	const CDesC8Array& aAttributeValues )
       
   330 	{
       
   331 	TInt numOfValues = aAttributeValues.Count();
       
   332 	// Making 1 extra slot
       
   333 	CDesC8ArrayFlat* tmp = new(ELeave) CDesC8ArrayFlat( numOfValues + 1 );
       
   334 	CleanupStack::PushL( tmp );
       
   335 	for( TInt i = 0; i < numOfValues; i++ )
       
   336 		{
       
   337 		tmp->AppendL( aAttributeValues[ i ] );
       
   338 		}
       
   339 
       
   340 	// Compressing
       
   341 	tmp->Compress();
       
   342 	CleanupStack::Pop( tmp );
       
   343 	delete iAttributeValues;
       
   344 	iAttributeValues = tmp;
       
   345 	}
       
   346 
       
   347 //  End of File