cms/src/CCMSSignedData.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 <x500dn.h>
       
    21 #include <asn1dec.h>
       
    22 #include <asn1enc.h>
       
    23 
       
    24 #include "CCMSSignedData.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KMinNumberOfSubModules = 4;
       
    28 const TInt KMaxNumberOfSubModules = 6;
       
    29 const TInt KDefaultGranularity = 1;
       
    30 const TInt KDefaultVersion = 1;
       
    31 const TInt KAlternativeVersion = 3;
       
    32 const TUint8 KCertificateSetTag = 0;
       
    33 const TUint8 KRevokedCertificatesTag = 1;
       
    34 
       
    35 // Defaulta id-data oid
       
    36 _LIT( KIDDataOID, "1.2.840.113549.1.7.1" );
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CCMSSignedData::CCMSSignedData
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C CCMSSignedData::CCMSSignedData()
       
    47     {
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CCMSSignedData::ConstructL
       
    52 // Symbian 2nd phase constructor can leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C void CCMSSignedData::ConstructL()
       
    56     {
       
    57 	iVersion = KDefaultVersion;
       
    58 	iDigestAlgorithmIdentifiers =
       
    59 				new( ELeave )CArrayPtrFlat< CCMSX509AlgorithmIdentifier >
       
    60 														( KDefaultGranularity );
       
    61 	iContentInfo = CCMSEncapsulatedContentInfo::NewLC();
       
    62 	CleanupStack::Pop( iContentInfo );
       
    63 	iSignerInfos =
       
    64 				new( ELeave )CArrayPtrFlat< CCMSSignerInfo >
       
    65 														( KDefaultGranularity );
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CCMSSignedData::ConstructL
       
    70 // Symbian 2nd phase constructor can leave.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 EXPORT_C void CCMSSignedData::ConstructL(
       
    74 	const CArrayPtr< CCMSX509AlgorithmIdentifier >& aDigestAlgorithmIdentifiers,
       
    75    	const CCMSEncapsulatedContentInfo& aContentInfo,
       
    76    	const CArrayPtr< CCMSSignerInfo >& aSignerInfos,
       
    77    	const CArrayPtr< CCMSCertificateChoices >* aCertificates,
       
    78    	const CArrayPtr< CCMSX509CertificateList >* aRevokedCertificates )
       
    79     {
       
    80 	SetSignerInfosL( aSignerInfos );
       
    81 	SetDigestAlgorithmIdentifiersL( aDigestAlgorithmIdentifiers );
       
    82 	SetEncapsulatedContentInfoL( aContentInfo );
       
    83 
       
    84 	SetCertificatesL( aCertificates );
       
    85 	SetRevokedCertificatesL( aRevokedCertificates );
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CCMSSignedData::NewLC
       
    90 // Two-phased constructor.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CCMSSignedData* CCMSSignedData::NewLC()
       
    94 	{
       
    95 	CCMSSignedData* self = new( ELeave ) CCMSSignedData();
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL();
       
    98     return self;
       
    99 	}
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CCMSSignedData::NewLC
       
   103 // Two-phased constructor.
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 EXPORT_C CCMSSignedData* CCMSSignedData::NewLC(
       
   107 	const CArrayPtr< CCMSX509AlgorithmIdentifier >& aDigestAlgorithmIdentifiers,
       
   108    	const CCMSEncapsulatedContentInfo& aContentInfo,
       
   109    	const CArrayPtr< CCMSSignerInfo >& aSignerInfos )
       
   110     {
       
   111     CCMSSignedData* self = new( ELeave ) CCMSSignedData();
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL( aDigestAlgorithmIdentifiers,
       
   114     				  aContentInfo,
       
   115     				  aSignerInfos,
       
   116     				  NULL,
       
   117     				  NULL );
       
   118     return self;
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CCMSSignedData::NewLC
       
   123 // Two-phased constructor.
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 EXPORT_C CCMSSignedData* CCMSSignedData::NewLC(
       
   127 	const CArrayPtr< CCMSX509AlgorithmIdentifier >& aDigestAlgorithmIdentifiers,
       
   128    	const CCMSEncapsulatedContentInfo& aContentInfo,
       
   129    	const CArrayPtr< CCMSSignerInfo >& aSignerInfos,
       
   130    	const CArrayPtr< CCMSCertificateChoices >* aCertificates,
       
   131    	const CArrayPtr< CCMSX509CertificateList >* aRevokedCertificates )
       
   132     {
       
   133     CCMSSignedData* self = new( ELeave ) CCMSSignedData();
       
   134     CleanupStack::PushL( self );
       
   135     self->ConstructL( aDigestAlgorithmIdentifiers,
       
   136     				  aContentInfo,
       
   137     				  aSignerInfos,
       
   138     				  aCertificates,
       
   139     				  aRevokedCertificates );
       
   140     return self;
       
   141     }
       
   142 
       
   143 // Destructor
       
   144 CCMSSignedData::~CCMSSignedData()
       
   145     {
       
   146 	if( iDigestAlgorithmIdentifiers )
       
   147 		{
       
   148 		iDigestAlgorithmIdentifiers->ResetAndDestroy();
       
   149 		delete iDigestAlgorithmIdentifiers;
       
   150 		}
       
   151 	delete iContentInfo;
       
   152 	if( iSignerInfos )
       
   153 		{
       
   154 		iSignerInfos->ResetAndDestroy();
       
   155 		delete iSignerInfos;
       
   156 		}
       
   157 	if( iCertificates )
       
   158 		{
       
   159 		iCertificates->ResetAndDestroy();
       
   160 		delete iCertificates;
       
   161 		}
       
   162 	if( iRevokedCertificates )
       
   163 		{
       
   164 		iRevokedCertificates->ResetAndDestroy();
       
   165 		delete iRevokedCertificates;
       
   166 		}
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CCMSSignedData::DecodeL
       
   171 // Decrypts raw data to this instance
       
   172 // -----------------------------------------------------------------------------
       
   173 void CCMSSignedData::DecodeL( const TDesC8& aRawData )
       
   174 	{
       
   175 	CArrayPtr<TASN1DecGeneric>* itemsData =
       
   176 							DecodeSequenceLC( aRawData,
       
   177 											  KMinNumberOfSubModules,
       
   178 											  KMaxNumberOfSubModules );
       
   179 	TInt pos = 0;
       
   180 	// decode Version
       
   181 	TASN1DecInteger version;
       
   182 	TInt tmpVersion;
       
   183 	tmpVersion = version.DecodeDERShortL( *itemsData->At( pos++ ) );
       
   184 
       
   185 	// decode digest algorithms
       
   186 	CArrayPtrFlat< CCMSX509AlgorithmIdentifier >* tmpAlgs =
       
   187 		new( ELeave )CArrayPtrFlat< CCMSX509AlgorithmIdentifier >
       
   188 													( KDefaultGranularity );
       
   189 	CleanupStack::PushL( tmpAlgs );
       
   190 	CleanupResetAndDestroyPushL( *tmpAlgs );
       
   191 
       
   192 	//there must be atleast 1 algorithm
       
   193 	CArrayPtr<TASN1DecGeneric>* digestAlgorithms =
       
   194 		DecodeSequenceLC( itemsData->At( pos++ )->Encoding(),
       
   195 						  1,
       
   196 						  KMaxTInt );
       
   197 	TInt algCount = digestAlgorithms->Count();
       
   198 	for( TInt i = 0; i < algCount; i++ )
       
   199 		{
       
   200 		CCMSX509AlgorithmIdentifier* alg =
       
   201 										CCMSX509AlgorithmIdentifier::NewL();
       
   202 		CleanupStack::PushL( alg );
       
   203 		alg->DecodeL( digestAlgorithms->At( i )->Encoding() );
       
   204 		tmpAlgs->AppendL( alg );
       
   205 		CleanupStack::Pop( alg );
       
   206 		}
       
   207 	CleanupStack::PopAndDestroy( digestAlgorithms );
       
   208 
       
   209 	// decode encapsulated content info
       
   210 	CCMSEncapsulatedContentInfo* tmpInfo = CCMSEncapsulatedContentInfo::NewLC();
       
   211 	tmpInfo->DecodeL( itemsData->At( pos++ )->Encoding() );
       
   212 
       
   213 	CArrayPtrFlat< CCMSCertificateChoices >* tmpCerts = NULL;
       
   214 
       
   215 	// decode possible certificate set
       
   216 	if( itemsData->At( pos )->Tag() == KCertificateSetTag )
       
   217 		{
       
   218 		tmpCerts =
       
   219 			new( ELeave )CArrayPtrFlat< CCMSCertificateChoices >
       
   220 													( KDefaultGranularity );
       
   221 		CleanupStack::PushL( tmpCerts );
       
   222 		CleanupResetAndDestroyPushL( *tmpCerts );
       
   223 		TASN1DecSequence decSeq;
       
   224 		CArrayPtr<TASN1DecGeneric>* certs =
       
   225 			decSeq.DecodeDERLC( *itemsData->At( pos++ ) );
       
   226 		TInt certCount = certs->Count();
       
   227 		for( TInt i = 0; i < certCount; i++ )
       
   228 			{
       
   229 			CCMSCertificateChoices* tmpCert =
       
   230 				CCMSCertificateChoices::NewLC();
       
   231 			tmpCert->DecodeL( certs->At( i )->Encoding() );
       
   232 			tmpCerts->AppendL( tmpCert );
       
   233 			CleanupStack::Pop( tmpCert );
       
   234 			}
       
   235 		CleanupStack::PopAndDestroy( certs );
       
   236 		}
       
   237 
       
   238 	// decode possible revoked certificates
       
   239 	CArrayPtrFlat< CCMSX509CertificateList >* tmpRevokedCertificates = NULL;
       
   240 	if( ( pos < itemsData->Count() ) &&
       
   241 		( itemsData->At( pos )->Tag() == KRevokedCertificatesTag ) )
       
   242 		{
       
   243 		tmpRevokedCertificates =
       
   244 			new( ELeave )CArrayPtrFlat< CCMSX509CertificateList >
       
   245 													( KDefaultGranularity );
       
   246 		CleanupStack::PushL( tmpRevokedCertificates );
       
   247 		CleanupResetAndDestroyPushL( *tmpRevokedCertificates );
       
   248 
       
   249 		TASN1DecSequence decSeq;
       
   250 		CArrayPtr<TASN1DecGeneric>* certs =
       
   251 			decSeq.DecodeDERLC( *itemsData->At( pos++ ) );
       
   252 
       
   253 		TInt certCount = certs->Count();
       
   254 		for( TInt i = 0; i < certCount; i++ )
       
   255 			{
       
   256 			CCMSX509CertificateList* tmpCert = CCMSX509CertificateList::NewLC();
       
   257 			tmpCert->DecodeL( certs->At( i )->Encoding() );
       
   258 			tmpRevokedCertificates->AppendL( tmpCert );
       
   259 			CleanupStack::Pop( tmpCert );
       
   260 			}
       
   261 		CleanupStack::PopAndDestroy( certs );
       
   262 		}
       
   263 
       
   264 	// decode Signer Infos
       
   265 	if( pos >= itemsData->Count() )
       
   266 		{
       
   267 		// Missing mandatory signer infos
       
   268 		User::Leave( KErrArgument );
       
   269 		}
       
   270 
       
   271 	CArrayPtrFlat< CCMSSignerInfo >* tmpSignerInfos =
       
   272 		new( ELeave )CArrayPtrFlat< CCMSSignerInfo >( KDefaultGranularity );
       
   273 	CleanupStack::PushL( tmpSignerInfos );
       
   274 	CleanupResetAndDestroyPushL( *tmpSignerInfos );
       
   275 
       
   276 	CArrayPtr<TASN1DecGeneric>* sInfos =
       
   277 		DecodeSequenceLC( itemsData->At( pos )->Encoding() );
       
   278 
       
   279 	TInt sInfoCount = sInfos->Count();
       
   280 	for( TInt j = 0; j < sInfoCount; j++ )
       
   281 		{
       
   282 		CCMSSignerInfo* tmpInfo = CCMSSignerInfo::NewL();
       
   283 		CleanupStack::PushL( tmpInfo );
       
   284 		tmpInfo->DecodeL( sInfos->At( j )->Encoding() );
       
   285 		tmpSignerInfos->AppendL( tmpInfo );
       
   286 		CleanupStack::Pop( tmpInfo );
       
   287 		}
       
   288 	CleanupStack::PopAndDestroy( sInfos );
       
   289 
       
   290 	// All done, setting new values
       
   291 	iSignerInfos->ResetAndDestroy();
       
   292 	delete iSignerInfos;
       
   293 	iSignerInfos = tmpSignerInfos;
       
   294 
       
   295 	CleanupStack::Pop( tmpSignerInfos ); // ResetAndDestroy
       
   296 	CleanupStack::Pop( tmpSignerInfos ); // Normal cleanup
       
   297 
       
   298 	if( iRevokedCertificates )
       
   299 		{
       
   300 		iRevokedCertificates->ResetAndDestroy();
       
   301 		delete iRevokedCertificates;
       
   302 		}
       
   303 	iRevokedCertificates = tmpRevokedCertificates;
       
   304 	if( tmpRevokedCertificates )
       
   305 		{
       
   306 		CleanupStack::Pop( tmpRevokedCertificates ); // ResetAndDestroy
       
   307 		CleanupStack::Pop( tmpRevokedCertificates ); // Normal cleanup
       
   308 		}
       
   309 
       
   310 	if( iCertificates )
       
   311 		{
       
   312 		iCertificates->ResetAndDestroy();
       
   313 		delete iCertificates;
       
   314 		}
       
   315 	iCertificates = tmpCerts;
       
   316 	if( tmpCerts )
       
   317 		{
       
   318 		CleanupStack::Pop( tmpCerts ); // ResetAndDestory
       
   319 		CleanupStack::Pop( tmpCerts ); // Normal cleanup
       
   320 		}
       
   321 
       
   322 	delete iContentInfo;
       
   323 	iContentInfo = tmpInfo;
       
   324 	CleanupStack::Pop( tmpInfo );
       
   325 
       
   326 	iDigestAlgorithmIdentifiers->ResetAndDestroy();
       
   327 	delete iDigestAlgorithmIdentifiers;
       
   328 	iDigestAlgorithmIdentifiers = tmpAlgs;
       
   329 	CleanupStack::Pop( tmpAlgs ); // ResetAndDestroy
       
   330 	CleanupStack::Pop( tmpAlgs ); // Normal cleanup
       
   331 
       
   332 	iVersion = tmpVersion;
       
   333 
       
   334 	CleanupStack::PopAndDestroy( itemsData );
       
   335 	}
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CCMSSignedData::EncoderLC
       
   339 // Returns ASN1 encoder for this instance
       
   340 // -----------------------------------------------------------------------------
       
   341 
       
   342 CASN1EncBase* CCMSSignedData::EncoderLC() const
       
   343 	{
       
   344 	CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   345 
       
   346 	// Add version
       
   347 	CASN1EncInt* version = CASN1EncInt::NewLC( iVersion );
       
   348 	root->AddAndPopChildL( version );
       
   349 
       
   350 	// Add digest algorithms
       
   351 	CASN1EncSequence* digestAlgorithms = CASN1EncSequence::NewLC();
       
   352 	digestAlgorithms->SetTag( EASN1Set, EUniversal );
       
   353 	TInt count = iDigestAlgorithmIdentifiers->Count();
       
   354 	if( count == 0 )
       
   355 		{
       
   356 		// there is no mandatory algorithms
       
   357 		User::Leave( KErrArgument );
       
   358 		}
       
   359 	for( TInt i = 0; i < count; i++ )
       
   360 		{
       
   361 		CCMSX509AlgorithmIdentifier* alg =
       
   362 			iDigestAlgorithmIdentifiers->At( i );
       
   363 		CASN1EncBase* algEnc = alg->EncoderLC();
       
   364 		digestAlgorithms->AddAndPopChildL( algEnc );
       
   365 		}
       
   366 	root->AddAndPopChildL( digestAlgorithms );
       
   367 
       
   368 	// add encapsulated content info
       
   369 	CASN1EncBase* encContentInfo = iContentInfo->EncoderLC();
       
   370 	root->AddAndPopChildL( encContentInfo );
       
   371 
       
   372 	// add possible certificates
       
   373 	if( iCertificates &&
       
   374 		iCertificates->Count() > 0 )
       
   375 		{
       
   376 		CASN1EncSequence* certificates = CASN1EncSequence::NewLC();
       
   377 		certificates->SetTag( KCertificateSetTag );
       
   378 		TInt certCount = iCertificates->Count();
       
   379 		for( TInt i = 0; i < certCount; i++ )
       
   380 			{
       
   381 			CCMSCertificateChoices* cert = iCertificates->At( i );
       
   382 			CASN1EncBase* certEnc = cert->EncoderLC();
       
   383 			certificates->AddAndPopChildL( certEnc );
       
   384 			}
       
   385 		root->AddAndPopChildL( certificates );
       
   386 		}
       
   387 
       
   388 	// add possible revoked certificates
       
   389 	if( iRevokedCertificates &&
       
   390 		iRevokedCertificates->Count() > 0 )
       
   391 		{
       
   392 		CASN1EncSequence* certificates = CASN1EncSequence::NewLC();
       
   393 		certificates->SetTag( KRevokedCertificatesTag );
       
   394 		TInt certCount = iRevokedCertificates->Count();
       
   395 		for( TInt i = 0; i < certCount; i++ )
       
   396 			{
       
   397 			CCMSX509CertificateList* list = iRevokedCertificates->At( i );
       
   398 			CASN1EncBase* rCerts = list->EncoderLC();
       
   399 			certificates->AddAndPopChildL( rCerts );
       
   400 			}
       
   401 		root->AddAndPopChildL( certificates );
       
   402 		}
       
   403 
       
   404 	// add signer infos
       
   405 	CASN1EncSequence* signerInfos = CASN1EncSequence::NewLC();
       
   406 	signerInfos->SetTag( EASN1Set, EUniversal );
       
   407 	TInt sCount = iSignerInfos->Count();
       
   408 	if( sCount == 0 )
       
   409 		{
       
   410 		// there is no mandatory signer infos
       
   411 		User::Leave( KErrArgument );
       
   412 		}
       
   413 
       
   414 	for( TInt j = 0; j < sCount; j++ )
       
   415 		{
       
   416 		CASN1EncBase* sInfo = iSignerInfos->At( j )->EncoderLC();
       
   417 		signerInfos->AddAndPopChildL( sInfo );
       
   418 		}
       
   419 	root->AddAndPopChildL( signerInfos );
       
   420 	return root;
       
   421 	}
       
   422 // -----------------------------------------------------------------------------
       
   423 // CCMSSignedData::Version
       
   424 // Getter for Version
       
   425 // -----------------------------------------------------------------------------
       
   426 EXPORT_C TInt CCMSSignedData::Version() const
       
   427 	{
       
   428 	return iVersion;
       
   429 	}
       
   430 // -----------------------------------------------------------------------------
       
   431 // CCMSSignedData::DigestAlgorithmIdentifiers
       
   432 // Getter for DigestAlgorithmIdentifiers
       
   433 // -----------------------------------------------------------------------------
       
   434 EXPORT_C const CArrayPtr< CCMSX509AlgorithmIdentifier >&
       
   435 	CCMSSignedData::DigestAlgorithmIdentifiers() const
       
   436 	{
       
   437 	return *iDigestAlgorithmIdentifiers;
       
   438 	}
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // CCMSSignedData::EncapsulatedContentInfo
       
   442 // Getter for EncapsulatedContentInfo
       
   443 // -----------------------------------------------------------------------------
       
   444 EXPORT_C const CCMSEncapsulatedContentInfo&
       
   445 	CCMSSignedData::EncapsulatedContentInfo() const
       
   446 	{
       
   447 	return *iContentInfo;
       
   448 	}
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // CCMSSignedData::SignerInfos
       
   452 // Getter for SignerInfos
       
   453 // -----------------------------------------------------------------------------
       
   454 EXPORT_C const CArrayPtr< CCMSSignerInfo >&
       
   455 	CCMSSignedData::SignerInfos() const
       
   456 	{
       
   457 	return *iSignerInfos;
       
   458 	}
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CCMSSignedData::Certificates
       
   462 // Getter for Certificates
       
   463 // -----------------------------------------------------------------------------
       
   464 EXPORT_C const CArrayPtr< CCMSCertificateChoices >*
       
   465 	CCMSSignedData::Certificates() const
       
   466 	{
       
   467 	return iCertificates;
       
   468 	}
       
   469 
       
   470 // -----------------------------------------------------------------------------
       
   471 // CCMSSignedData::RevokedCertificates
       
   472 // Getter for RevokedCertificates
       
   473 // -----------------------------------------------------------------------------
       
   474 EXPORT_C const CArrayPtr< CCMSX509CertificateList >*
       
   475 	CCMSSignedData::RevokedCertificates() const
       
   476 	{
       
   477 	return iRevokedCertificates;
       
   478 	}
       
   479 
       
   480 // -----------------------------------------------------------------------------
       
   481 // CCMSSignedData::SetDigestAlgorithmIdentifiersL
       
   482 // Setter for DigestAlgorithmIdentifiers, takes copy
       
   483 // -----------------------------------------------------------------------------
       
   484 EXPORT_C void CCMSSignedData::SetDigestAlgorithmIdentifiersL(
       
   485 	const CArrayPtr< CCMSX509AlgorithmIdentifier >& aDigestAlgorithmIdentifiers )
       
   486 	{
       
   487 	CArrayPtrFlat< CCMSX509AlgorithmIdentifier >* tmpAlgs =
       
   488 		new( ELeave )CArrayPtrFlat< CCMSX509AlgorithmIdentifier >( KDefaultGranularity );
       
   489 	CleanupStack::PushL( tmpAlgs );
       
   490 	TInt algCount = aDigestAlgorithmIdentifiers.Count();
       
   491 	for( TInt i = 0; i < algCount; i++ )
       
   492 		{
       
   493 		CCMSX509AlgorithmIdentifier* origAlg = aDigestAlgorithmIdentifiers[ i ];
       
   494 		CCMSX509AlgorithmIdentifier* tmpAlg = NULL;
       
   495 		if( origAlg->DigestAlgorithm() )
       
   496 			{
       
   497 			tmpAlg =
       
   498 			CCMSX509AlgorithmIdentifier::NewL( origAlg->AlgorithmIdentifier(),
       
   499 											   *origAlg->DigestAlgorithm() );
       
   500 			}
       
   501 		else
       
   502 			{
       
   503 			tmpAlg =
       
   504 			CCMSX509AlgorithmIdentifier::NewL( origAlg->AlgorithmIdentifier() );
       
   505 			}
       
   506 		CleanupStack::PushL( tmpAlg );
       
   507 		tmpAlgs->AppendL( tmpAlg );
       
   508 		CleanupStack::Pop( tmpAlg );
       
   509 		}
       
   510 	delete iDigestAlgorithmIdentifiers;
       
   511 	iDigestAlgorithmIdentifiers = tmpAlgs;
       
   512 	CleanupStack::Pop( tmpAlgs );
       
   513 	}
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // CCMSSignedData::SetEncapsulatedContentInfoL
       
   517 // Setter for EncapsulatedContentInfo, takes copy
       
   518 // -----------------------------------------------------------------------------
       
   519 EXPORT_C void CCMSSignedData::SetEncapsulatedContentInfoL(
       
   520 	const CCMSEncapsulatedContentInfo& aContentInfo )
       
   521 	{
       
   522 	CCMSEncapsulatedContentInfo* copy =
       
   523 		CCMSEncapsulatedContentInfo::NewLC( aContentInfo.ContentType(),
       
   524 											aContentInfo.Content() );
       
   525 	delete iContentInfo;
       
   526 	iContentInfo = copy;
       
   527 	CleanupStack::Pop( copy );
       
   528 	ValidateVersion();
       
   529 	}
       
   530 
       
   531 // -----------------------------------------------------------------------------
       
   532 // CCMSSignedData::SetSignerInfosL
       
   533 // Setter for SignerInfos, takes copy
       
   534 // -----------------------------------------------------------------------------
       
   535 EXPORT_C void CCMSSignedData::SetSignerInfosL(
       
   536 	const CArrayPtr< CCMSSignerInfo >& aSignerInfos )
       
   537 	{
       
   538 	TInt sigCount = aSignerInfos.Count();
       
   539 	// creating right size array directly, adding 1 to avoid panic in case that
       
   540 	// sigCount is zero.
       
   541 	CArrayPtrFlat< CCMSSignerInfo >* tmpSignInfos =
       
   542 		new( ELeave )CArrayPtrFlat< CCMSSignerInfo >( sigCount + 1 );
       
   543 	CleanupStack::PushL( tmpSignInfos );
       
   544 	CleanupResetAndDestroyPushL( *tmpSignInfos );
       
   545 	for( TInt i = 0; i < sigCount; i++ )
       
   546 		{
       
   547 		HBufC8* tmpEncoding;
       
   548 		aSignerInfos[ i ]->EncodeL( tmpEncoding );
       
   549 		CleanupStack::PushL( tmpEncoding );
       
   550 		CCMSSignerInfo* tmpSig = CCMSSignerInfo::NewL();
       
   551 		CleanupStack::PushL( tmpSig );
       
   552 		tmpSig->DecodeL( *tmpEncoding );
       
   553 		tmpSignInfos->AppendL( tmpSig );
       
   554 		CleanupStack::Pop( tmpSig );
       
   555 		CleanupStack::PopAndDestroy( tmpEncoding );
       
   556 		}
       
   557 	if( iSignerInfos )
       
   558 		{
       
   559 		iSignerInfos->ResetAndDestroy();
       
   560 		delete iSignerInfos;
       
   561 		}
       
   562 	iSignerInfos = tmpSignInfos;
       
   563 	CleanupStack::Pop( tmpSignInfos ); // ResetAndDestroy
       
   564 	CleanupStack::Pop( tmpSignInfos ); // normal cleanup
       
   565 	ValidateVersion();
       
   566 	}
       
   567 
       
   568 // -----------------------------------------------------------------------------
       
   569 // CCMSSignedData::SetCertificatesL
       
   570 // Setter for Certificates, takes copy
       
   571 // -----------------------------------------------------------------------------
       
   572 EXPORT_C void CCMSSignedData::SetCertificatesL(
       
   573 	const CArrayPtr< CCMSCertificateChoices >* aCertificates )
       
   574 	{
       
   575 	CArrayPtrFlat< CCMSCertificateChoices >* tmpCertificates = NULL;
       
   576 	if( aCertificates )
       
   577 		{
       
   578 		TInt certCount = aCertificates->Count();
       
   579 		// creating right size array directly, adding 1 to avoid panic in case 
       
   580 		// that certCount is zero.
       
   581 		tmpCertificates =
       
   582 			new( ELeave )CArrayPtrFlat< CCMSCertificateChoices >( certCount + 1 );
       
   583 		CleanupStack::PushL( tmpCertificates );
       
   584 		CleanupResetAndDestroyPushL( *tmpCertificates );
       
   585 		for( TInt i = 0; i < certCount; i++ )
       
   586 			{
       
   587 			CCMSCertificateChoices* copy =
       
   588 				CCMSCertificateChoices::NewLC();
       
   589 			CCMSCertificateChoices* orig = aCertificates->At( i );
       
   590 			if( orig->AttrCert() )
       
   591 				{
       
   592 				copy->SetAttrCertL( *orig->AttrCert() );
       
   593 				}
       
   594 			else if( orig->Certificate() )
       
   595 				{
       
   596 				copy->SetCertificateL( *orig->Certificate() );
       
   597 				}
       
   598 			else
       
   599 				{
       
   600 				User::Leave( KErrArgument );
       
   601 				}
       
   602 			tmpCertificates->AppendL( copy );
       
   603 			CleanupStack::Pop( copy );
       
   604 			}
       
   605 		CleanupStack::Pop( tmpCertificates ); // ResetAndDestroy
       
   606 		CleanupStack::Pop( tmpCertificates ); // Normal cleanup
       
   607 		}
       
   608 	if( iCertificates )
       
   609 		{
       
   610 		iCertificates->ResetAndDestroy();
       
   611 		delete iCertificates;
       
   612 		}
       
   613 	iCertificates = tmpCertificates;
       
   614 	ValidateVersion();
       
   615 	}
       
   616 
       
   617 // -----------------------------------------------------------------------------
       
   618 // CCMSSignedData::SetRevokedCertificatesL
       
   619 // Setter for RevokedCertificates, takes copy
       
   620 // -----------------------------------------------------------------------------
       
   621 EXPORT_C void CCMSSignedData::SetRevokedCertificatesL(
       
   622 	const CArrayPtr< CCMSX509CertificateList >* aRevokedCertificates )
       
   623 	{
       
   624 	CArrayPtrFlat< CCMSX509CertificateList >* tmpCerts = NULL;
       
   625 	if( aRevokedCertificates )
       
   626 		{
       
   627 		TInt certCount = aRevokedCertificates->Count();
       
   628 		// creating right size array directly, adding 1 to avoid panic in case 
       
   629 		// that certCount is zero.
       
   630 		tmpCerts =
       
   631 			new( ELeave )CArrayPtrFlat< CCMSX509CertificateList >
       
   632 														( certCount + 1 );
       
   633 		CleanupStack::PushL( tmpCerts );
       
   634 		CleanupResetAndDestroyPushL( *tmpCerts );
       
   635 		for( TInt i = 0; i < certCount; i++ )
       
   636 			{
       
   637 			CCMSX509CertificateList* copy =
       
   638 				CCMSX509CertificateList::NewLC();
       
   639 
       
   640 			HBufC8* tmpData;
       
   641 			aRevokedCertificates->At( i )->EncodeL( tmpData );
       
   642 			CleanupStack::PushL( tmpData );
       
   643 			copy->DecodeL( *tmpData );
       
   644 			CleanupStack::PopAndDestroy( tmpData );
       
   645 			tmpCerts->AppendL( copy );
       
   646 			CleanupStack::Pop( copy );
       
   647 			}
       
   648 		CleanupStack::Pop( tmpCerts ); // ResetAndDestroy
       
   649 		CleanupStack::Pop( tmpCerts ); // Normal cleanup
       
   650 		}
       
   651 
       
   652 	if( iRevokedCertificates )
       
   653 		{
       
   654 		iRevokedCertificates->ResetAndDestroy();
       
   655 		delete iRevokedCertificates;
       
   656 		}
       
   657 	iRevokedCertificates = tmpCerts;
       
   658 	}
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CCMSSignedData::ValidateVersion
       
   662 // Validates that iVersion is correct
       
   663 // -----------------------------------------------------------------------------
       
   664 void CCMSSignedData::ValidateVersion()
       
   665 	{
       
   666 	iVersion = KDefaultVersion;
       
   667 	// if the encapsulated content type is other than id-data
       
   668 	// then the value of version shall be 3.
       
   669 	if( iContentInfo &&
       
   670 		( iContentInfo->ContentType() != KIDDataOID ) )
       
   671 		{
       
   672 		iVersion = KAlternativeVersion;
       
   673 		return; // no reason to continue
       
   674 		}
       
   675 
       
   676 	// if any of the elements of SignerInfos are version 3,
       
   677 	// then the value of version shall be 3.
       
   678 	if( iSignerInfos->Count() )
       
   679 		{
       
   680 		TInt sigCount = iSignerInfos->Count();
       
   681 		for( TInt i = 0; i < sigCount; i++ )
       
   682 			{
       
   683 			if( iSignerInfos->At( i )->CMSVersion() == KAlternativeVersion )
       
   684 				{
       
   685 				iVersion = KAlternativeVersion;
       
   686 				return; // no reason to continue
       
   687 				}
       
   688 			}
       
   689 		}
       
   690 
       
   691 	// if attribute certificates are present, the
       
   692 	// then the value of version shall be 3.
       
   693 	if( iCertificates )
       
   694 		{
       
   695 		TInt certCount = iCertificates->Count();
       
   696 		for( TInt i = 0; i < certCount; i++ )
       
   697 			{
       
   698 			if( iCertificates->At( i )->AttrCert() )
       
   699 				{
       
   700 				iVersion = KAlternativeVersion;
       
   701 				return; // no reason to continue
       
   702 				}
       
   703 			}
       
   704 		}
       
   705 	}
       
   706 //  End of File