cms/src/CCMSX509AlgorithmIdentifier.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    "CCMSX509AlgorithmIdentifier.h"
       
    21 #include <x509cert.h>
       
    22 #include <asn1dec.h>
       
    23 #include <asn1enc.h>
       
    24 
       
    25 const TInt KMinNumberOfSubModules = 1;
       
    26 const TInt KMaxNumberOfSubModules = 2;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CCMSX509AlgorithmIdentifier::CCMSX509AlgorithmIdentifier
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CCMSX509AlgorithmIdentifier::CCMSX509AlgorithmIdentifier()
       
    37     {
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CCMSX509AlgorithmIdentifier::ConstructL
       
    42 // Symbian 2nd phase constructor can leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C void CCMSX509AlgorithmIdentifier::ConstructL(
       
    46 	const TAlgorithmId& aAlgorithmId )
       
    47     {
       
    48 	TAlgorithmId id = aAlgorithmId;
       
    49 	iAlgorithmIdentifier = CAlgorithmIdentifier::NewL( id, KNullDesC8() );
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CCMSX509AlgorithmIdentifier::ConstructL
       
    54 // Symbian 2nd phase constructor can leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 EXPORT_C void CCMSX509AlgorithmIdentifier::ConstructL(
       
    58 	const CAlgorithmIdentifier& aAlgorithmIdentifier )
       
    59     {
       
    60 	iAlgorithmIdentifier = CAlgorithmIdentifier::NewL( aAlgorithmIdentifier );
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CCMSX509AlgorithmIdentifier::NewL
       
    65 // Two-phased constructor.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 EXPORT_C CCMSX509AlgorithmIdentifier* CCMSX509AlgorithmIdentifier::NewL()
       
    69 	{
       
    70     return NewL( ERSA );
       
    71 	}
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CCMSX509AlgorithmIdentifier::NewL
       
    75 // Two-phased constructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C CCMSX509AlgorithmIdentifier* CCMSX509AlgorithmIdentifier::NewL(
       
    79 	const TAlgorithmId& aAlgorithmId )
       
    80     {
       
    81     CCMSX509AlgorithmIdentifier* self = new( ELeave ) CCMSX509AlgorithmIdentifier();
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL( aAlgorithmId );
       
    84 	CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CCMSX509AlgorithmIdentifier::NewL
       
    90 // Two-phased constructor.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CCMSX509AlgorithmIdentifier* CCMSX509AlgorithmIdentifier::NewL(
       
    94 	const CAlgorithmIdentifier& aAlgorithmIdentifier )
       
    95 	{
       
    96 	CCMSX509AlgorithmIdentifier* self = new( ELeave ) CCMSX509AlgorithmIdentifier();
       
    97 	CleanupStack::PushL( self );
       
    98 	self->ConstructL( aAlgorithmIdentifier );
       
    99 	CleanupStack::Pop( self );
       
   100     return self;
       
   101 	}
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CCMSX509AlgorithmIdentifier::NewL
       
   105 // Two-phased constructor.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 EXPORT_C CCMSX509AlgorithmIdentifier* CCMSX509AlgorithmIdentifier::NewL(
       
   109 	const CAlgorithmIdentifier& aAlgorithmIdentifier,
       
   110 	const CAlgorithmIdentifier& aDigestIdentifier )
       
   111 	{
       
   112 	CCMSX509AlgorithmIdentifier* self = new( ELeave ) CCMSX509AlgorithmIdentifier();
       
   113 	CleanupStack::PushL( self );
       
   114 	self->ConstructL( aAlgorithmIdentifier );
       
   115 	self->SetDigestAlgorithmL( &aDigestIdentifier );
       
   116 	CleanupStack::Pop( self );
       
   117     return self;
       
   118 	}
       
   119 
       
   120 // Destructor
       
   121 CCMSX509AlgorithmIdentifier::~CCMSX509AlgorithmIdentifier()
       
   122     {
       
   123 	delete iAlgorithmIdentifier;
       
   124 	delete iDigestAlgorithmIdentifier;
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CCMSX509AlgorithmIdentifier::DecodeL
       
   129 // Decrypts raw data to this instance
       
   130 // -----------------------------------------------------------------------------
       
   131 void CCMSX509AlgorithmIdentifier::DecodeL( const TDesC8& aRawData )
       
   132 	{
       
   133 
       
   134     CAlgorithmIdentifier* tmpAlgorithmIdentifier = NULL;
       
   135     CAlgorithmIdentifier* tmpDigestAI = NULL;
       
   136 
       
   137     // try simple algorithm
       
   138 	TRAPD( error, tmpAlgorithmIdentifier =
       
   139 					CX509AlgorithmIdentifier::NewL( aRawData ) ); 
       
   140 
       
   141 	// it is not simple algorithm identifier, trying with SigningAlgorithm
       
   142 	if( error == KErrNotSupported )
       
   143 		{
       
   144         CX509SigningAlgorithmIdentifier* tmpSigning = NULL;
       
   145 		TRAPD( error2, tmpSigning =
       
   146                CX509SigningAlgorithmIdentifier::NewL( aRawData ) );
       
   147         if( error2 == KErrNone )
       
   148             {
       
   149             CleanupStack::PushL( tmpSigning );
       
   150             tmpAlgorithmIdentifier = CAlgorithmIdentifier::NewLC(
       
   151                 tmpSigning->AsymmetricAlgorithm() );
       
   152             tmpDigestAI =
       
   153                 CAlgorithmIdentifier::NewL( tmpSigning->DigestAlgorithm() );
       
   154             CleanupStack::Pop( tmpAlgorithmIdentifier );
       
   155             CleanupStack::PopAndDestroy( tmpSigning );
       
   156             }
       
   157         // it is not SigningAlgorithm, decode manually
       
   158         else if( error2 == KErrNotSupported )
       
   159             {
       
   160             CArrayPtr<TASN1DecGeneric>* itemsData =
       
   161 							DecodeSequenceLC( aRawData,
       
   162 											  KMinNumberOfSubModules,
       
   163 											  KMaxNumberOfSubModules );
       
   164             TInt pos = 0;
       
   165             TInt count = itemsData->Count();
       
   166             TASN1DecObjectIdentifier decOID;
       
   167             HBufC* oid = decOID.DecodeDERL( *( itemsData->At( pos++ ) ) );
       
   168             CleanupStack::PushL( oid );
       
   169             if( *oid == KSHA1 )
       
   170                 {
       
   171                 TAlgorithmId algId( ESHA1 );
       
   172                 if( count > pos )
       
   173                     {
       
   174                     TASN1DecGeneric* gen = itemsData->At( pos );
       
   175                     TPtrC8 encodedParams( gen->Encoding() );
       
   176                     tmpAlgorithmIdentifier =
       
   177                         CAlgorithmIdentifier::NewL( algId, encodedParams );
       
   178                     }
       
   179                 else
       
   180                     {
       
   181                     TPtrC8 encodedParams( KNullDesC8() );
       
   182                     tmpAlgorithmIdentifier =
       
   183                         CAlgorithmIdentifier::NewL( algId, encodedParams );
       
   184                     }
       
   185                 }
       
   186             else
       
   187                 {
       
   188                 User::Leave( KErrNotSupported );
       
   189                 }
       
   190             CleanupStack::PopAndDestroy( 2 ); // oid, itemsData
       
   191             }
       
   192         else
       
   193             {
       
   194             User::Leave( error2 );
       
   195             }
       
   196 		}
       
   197 	else if( error != KErrNone )
       
   198 		{
       
   199 		User::Leave( error );
       
   200 		}
       
   201 	delete iAlgorithmIdentifier;
       
   202 	iAlgorithmIdentifier = tmpAlgorithmIdentifier;
       
   203 	delete iDigestAlgorithmIdentifier;
       
   204 	iDigestAlgorithmIdentifier = tmpDigestAI;
       
   205 	}
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CCMSX509AlgorithmIdentifier::EncoderLC
       
   209 // Returns ASN1 encoder for this instance
       
   210 // -----------------------------------------------------------------------------
       
   211 
       
   212 CASN1EncBase* CCMSX509AlgorithmIdentifier::EncoderLC() const
       
   213 	{
       
   214 	CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   215 	CASN1EncObjectIdentifier* oid = NULL;
       
   216 	if( !iDigestAlgorithmIdentifier )
       
   217 		{
       
   218 		switch ( iAlgorithmIdentifier->Algorithm() )
       
   219 			{
       
   220 			case ERSA:
       
   221 				{
       
   222 				oid = CASN1EncObjectIdentifier::NewLC( KRSA );
       
   223 				break;
       
   224 				}
       
   225 			case EDSA:
       
   226 				{
       
   227 				oid = CASN1EncObjectIdentifier::NewLC( KDSA );
       
   228 				break;
       
   229 				}
       
   230 			case EDH:
       
   231 				{
       
   232 				oid = CASN1EncObjectIdentifier::NewLC( KDH );
       
   233 				break;
       
   234 				}
       
   235 			case EMD2:
       
   236 				{
       
   237 				oid = CASN1EncObjectIdentifier::NewLC( KMD2 );
       
   238 				break;
       
   239 				}
       
   240 			case EMD5:
       
   241 				{
       
   242 				oid = CASN1EncObjectIdentifier::NewLC( KMD5 );
       
   243 				break;
       
   244 				}
       
   245 			case ESHA1:
       
   246 				{
       
   247 				oid = CASN1EncObjectIdentifier::NewLC( KSHA1 );
       
   248 				break;
       
   249 				}
       
   250 			default:
       
   251 				{
       
   252 				User::Leave( KErrArgument );
       
   253 				}
       
   254 			}
       
   255 		}
       
   256 	else
       
   257 		{
       
   258 		// only valid combinations are MD2WithRSA, MD5WithRSA
       
   259 		// SHA1WithRSA and DSAWithSHA1
       
   260 		if( iAlgorithmIdentifier->Algorithm() == ERSA )
       
   261 			{
       
   262 			switch ( iDigestAlgorithmIdentifier->Algorithm() )
       
   263 				{
       
   264 				case EMD2:
       
   265 					{
       
   266 					oid = CASN1EncObjectIdentifier::NewLC( KMD2WithRSA );
       
   267 					break;
       
   268 					}
       
   269 				case EMD5:
       
   270 					{
       
   271 					oid = CASN1EncObjectIdentifier::NewLC( KMD5WithRSA );
       
   272 					break;
       
   273 					}
       
   274 				case ESHA1:
       
   275 					{
       
   276 					oid = CASN1EncObjectIdentifier::NewLC( KSHA1WithRSA );
       
   277 					break;
       
   278 					}
       
   279 				default:
       
   280 					{
       
   281 					User::Leave( KErrArgument );
       
   282 					}
       
   283 				}
       
   284 			}
       
   285 		else if( ( iAlgorithmIdentifier->Algorithm() == EDSA ) &&
       
   286 				 ( iDigestAlgorithmIdentifier->Algorithm() == ESHA1 ) )
       
   287 			{
       
   288 			oid = CASN1EncObjectIdentifier::NewLC( KDSAWithSHA1 );
       
   289 			}
       
   290 		else
       
   291 			{
       
   292 			// not valid combination
       
   293 			User::Leave( KErrArgument );
       
   294 			}
       
   295 		}
       
   296 
       
   297 	// add algorithm id
       
   298 	root->AddAndPopChildL( oid );
       
   299 
       
   300 	// add parameters
       
   301 	CASN1EncBase* parameters = NULL;
       
   302 	if( iAlgorithmIdentifier->EncodedParams() != KNullDesC8() )
       
   303 		{
       
   304 		parameters = CASN1EncEncoding::NewLC( iAlgorithmIdentifier->EncodedParams() );
       
   305 		}
       
   306 	else
       
   307 		{
       
   308 		parameters = CASN1EncNull::NewLC();
       
   309 		}
       
   310 	root->AddAndPopChildL( parameters );
       
   311 
       
   312 	return root;
       
   313 	}
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CCMSX509AlgorithmIdentifier::AlgorithmIdentifier
       
   317 // Getter for AlgorithIdentifier
       
   318 // -----------------------------------------------------------------------------
       
   319 EXPORT_C const CAlgorithmIdentifier& CCMSX509AlgorithmIdentifier::AlgorithmIdentifier() const
       
   320 	{
       
   321 	return *iAlgorithmIdentifier;
       
   322 	}
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CCMSX509AlgorithmIdentifier::SetAlgorithmIdentifierL
       
   326 // Setter for attribute type, takes copy
       
   327 // -----------------------------------------------------------------------------
       
   328 EXPORT_C void CCMSX509AlgorithmIdentifier::SetAlgorithmIdentifierL(
       
   329 	const CAlgorithmIdentifier& aAlgorithmIdentifier )
       
   330 	{
       
   331 	CAlgorithmIdentifier* tmpAi = CAlgorithmIdentifier::NewL( aAlgorithmIdentifier );
       
   332 	delete iAlgorithmIdentifier;
       
   333 	iAlgorithmIdentifier = tmpAi;
       
   334 	}
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CCMSX509AlgorithmIdentifier::DigestAlgorithm
       
   338 // Getter for DigestAlgorithIdentifier
       
   339 // -----------------------------------------------------------------------------
       
   340 EXPORT_C const CAlgorithmIdentifier* CCMSX509AlgorithmIdentifier::DigestAlgorithm() const
       
   341 	{
       
   342 	return iDigestAlgorithmIdentifier;
       
   343 	}
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CCMSX509AlgorithmIdentifier::SetDigestAlgorithmL
       
   347 // Setter for digest algorithm identifier, takes copy
       
   348 // -----------------------------------------------------------------------------
       
   349 EXPORT_C void CCMSX509AlgorithmIdentifier::SetDigestAlgorithmL(
       
   350 	const CAlgorithmIdentifier* aAlgorithmIdentifier )
       
   351 	{
       
   352 	CAlgorithmIdentifier* tmpAi = CAlgorithmIdentifier::NewL( *aAlgorithmIdentifier );
       
   353 	delete iDigestAlgorithmIdentifier;
       
   354 	iDigestAlgorithmIdentifier = tmpAi;
       
   355 	}
       
   356 //  End of File