cryptoservices/asnpkcs/source/asnpkcs5.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-2009 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 the License "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 <asn1enc.h>
       
    20 #include <asn1dec.h>
       
    21 #include <pbedata.h>
       
    22 #include <rc2.h>
       
    23 
       
    24 #include "asnpkcs.h"
       
    25 
       
    26 _LIT(Kpkcs5PBES2, "1.2.840.113549.1.5.13");
       
    27 _LIT(Kpkcs5PBKDF2, "1.2.840.113549.1.5.12");
       
    28 _LIT(KDESCBC, "1.3.14.3.2.7");
       
    29 _LIT(K3DESCBC, "1.2.840.113549.3.7");
       
    30 _LIT(KRC2CBC, "1.2.840.113549.3.2");
       
    31 
       
    32 // pbe12Algorithm Ids
       
    33 _LIT(KPbeWithSHA1And128BitRC4, "1.2.840.113549.1.12.1.1");
       
    34 _LIT(KPbeWithSHA1And40BitRC4, "1.2.840.113549.1.12.1.2");
       
    35 _LIT(KPbeWithSHA1And3_KeyTripleDES_CBC, "1.2.840.113549.1.12.1.3");
       
    36 _LIT(KPbeWithSHA1And2_KeyTripleDES_CBC, "1.2.840.113549.1.12.1.4");
       
    37 _LIT(KPbeWithSHA1And128BitRC2_CBC, "1.2.840.113549.1.12.1.5");
       
    38 _LIT(KPbeWithSHA1And40BitRC2_CBC, "1.2.840.113549.1.12.1.6");
       
    39 //The size of the Initialization vector
       
    40 const TInt KIvSize = 8;
       
    41 
       
    42 
       
    43 /*
       
    44 * //For RC2
       
    45 * SEQUENCE
       
    46 * 	OID -- pkcs5PBES2
       
    47 *	SEQUENCE
       
    48 *		SEQUENCE
       
    49 *		OID -- pkcs5PBKDF2
       
    50 *		SEQUENCE
       
    51 *			OCTET STRING -- salt
       
    52 *			INTEGER -- iteration count
       
    53 *			INTEGER -- effective key length in octets
       
    54 * 	SEQUENCE
       
    55 *		OID -- algorithm id (rc2)
       
    56 *		SEQUENCE
       
    57 *			INTEGER -- RC2 parameter version 58 = 128, 160 = 40
       
    58 *			OCTET STRING -- iv
       
    59 *
       
    60 * //For DES and 3DES
       
    61 * SEQUENCE
       
    62 * 	OID -- pkcs5PBES2
       
    63 *	SEQUENCE
       
    64 *		SEQUENCE
       
    65 *		OID -- pkcs5PBKDF2
       
    66 *		SEQUENCE
       
    67 *			OCTET STRING -- salt
       
    68 *			INTEGER -- iteration count
       
    69 * 	SEQUENCE
       
    70 *		OID -- algorithm id (des, 3des)
       
    71 *		OCTET STRING -- iv
       
    72 */
       
    73 
       
    74 EXPORT_C CASN1EncSequence* TASN1EncPKCS5::EncodeDERL(const CPBEncryptParms& aParms)
       
    75 	{
       
    76 	CASN1EncSequence* seq = CASN1EncSequence::NewLC();
       
    77 	CASN1EncObjectIdentifier* pbes2 = CASN1EncObjectIdentifier::NewLC(Kpkcs5PBES2);
       
    78 	seq->AddChildL(pbes2);
       
    79 	CleanupStack::Pop(pbes2);
       
    80 
       
    81 	CASN1EncSequence* seq1 = CASN1EncSequence::NewLC();
       
    82 	seq->AddChildL(seq1);
       
    83 	CleanupStack::Pop(seq1);
       
    84 
       
    85 	CASN1EncSequence* seq2 = CASN1EncSequence::NewLC();
       
    86 	seq1->AddChildL(seq2);
       
    87 	CleanupStack::Pop(seq2);
       
    88 
       
    89 	CASN1EncObjectIdentifier* pbkdf2 = CASN1EncObjectIdentifier::NewLC(Kpkcs5PBKDF2);
       
    90 	seq2->AddChildL(pbkdf2);
       
    91 	CleanupStack::Pop(pbkdf2);
       
    92 
       
    93 	CASN1EncSequence* seq3 = CASN1EncSequence::NewLC();
       
    94 	seq2->AddChildL(seq3);
       
    95 	CleanupStack::Pop(seq3);
       
    96 
       
    97 	CASN1EncOctetString* salt = CASN1EncOctetString::NewLC(aParms.Salt());
       
    98 	seq3->AddChildL(salt);
       
    99 	CleanupStack::Pop(salt);
       
   100 
       
   101 	CASN1EncInt* iterations = CASN1EncInt::NewLC(aParms.Iterations());
       
   102 	seq3->AddChildL(iterations);
       
   103 	CleanupStack::Pop(iterations);
       
   104 
       
   105 	CASN1EncInt* keysize = 0;
       
   106 	switch(aParms.Cipher())
       
   107 		{
       
   108 		case ECipherDES_CBC:
       
   109 		case ECipher3DES_CBC: 
       
   110 			break;
       
   111 		case ECipherRC2_CBC_40:
       
   112  			keysize = CASN1EncInt::NewLC(KSSLCompatibilityBits/8);  // effective key length in *octets*
       
   113 			seq3->AddChildL(keysize);
       
   114 			CleanupStack::Pop(keysize);
       
   115 			break;
       
   116 		case ECipherRC2_CBC_128:
       
   117  			keysize = CASN1EncInt::NewLC(KSSLCompatibilityBits/8);  // effective key length in *octets*
       
   118 			seq3->AddChildL(keysize);
       
   119 			CleanupStack::Pop(keysize);
       
   120 			break;
       
   121 		case ECipherRC2_CBC_40_16:
       
   122  			keysize = CASN1EncInt::NewLC(KPkcs8CompatibilityBits/8);  // effective key length in *octets*
       
   123 			seq3->AddChildL(keysize);
       
   124 			CleanupStack::Pop(keysize);
       
   125 			break;
       
   126 		case ECipherRC2_CBC_128_16:
       
   127  			keysize = CASN1EncInt::NewLC(KPkcs8CompatibilityBits/8);  // effective key length in *octets*
       
   128 			seq3->AddChildL(keysize);
       
   129 			CleanupStack::Pop(keysize);
       
   130 			break;
       
   131 		default:
       
   132 			User::Leave(KErrNotSupported);
       
   133 			break;
       
   134 		}
       
   135 
       
   136 	CASN1EncSequence* seq4 = CASN1EncSequence::NewLC();
       
   137 	seq1->AddChildL(seq4);
       
   138 	CleanupStack::Pop(seq4);
       
   139 
       
   140 	CASN1EncObjectIdentifier* algid = 0;
       
   141 	switch(aParms.Cipher())
       
   142 		{
       
   143 		case ECipherDES_CBC:
       
   144 			algid = CASN1EncObjectIdentifier::NewLC(KDESCBC);
       
   145 			break;
       
   146 		case ECipher3DES_CBC:
       
   147 			algid = CASN1EncObjectIdentifier::NewLC(K3DESCBC);
       
   148 			break;
       
   149 		case ECipherRC2_CBC_40:
       
   150 		case ECipherRC2_CBC_128:
       
   151 		case ECipherRC2_CBC_40_16:
       
   152 		case ECipherRC2_CBC_128_16:
       
   153 			algid = CASN1EncObjectIdentifier::NewLC(KRC2CBC);
       
   154 			break;
       
   155 		default:
       
   156 			User::Leave(KErrNotSupported);
       
   157 			break;
       
   158 		}
       
   159 	seq4->AddChildL(algid);
       
   160 	CleanupStack::Pop(algid);
       
   161 
       
   162 	CASN1EncSequence* seq5 = 0;
       
   163 	CASN1EncInt* keysize1 = 0;
       
   164 	CASN1EncOctetString* iv = 0;
       
   165 	switch(aParms.Cipher())
       
   166 		{
       
   167 		case ECipherDES_CBC:
       
   168 		case ECipher3DES_CBC:
       
   169 			iv = CASN1EncOctetString::NewLC(aParms.IV());
       
   170 			seq4->AddChildL(iv);
       
   171 			CleanupStack::Pop(iv);
       
   172 			break;
       
   173 		case ECipherRC2_CBC_40:
       
   174 		case ECipherRC2_CBC_40_16:
       
   175 			seq5 = CASN1EncSequence::NewLC();
       
   176 			seq4->AddChildL(seq5);
       
   177 			CleanupStack::Pop(seq5);
       
   178 
       
   179 			keysize1 = CASN1EncInt::NewLC(160); //encoding for 40 bit
       
   180 			seq5->AddChildL(keysize1);
       
   181 			CleanupStack::Pop(keysize1);
       
   182 
       
   183 			iv = CASN1EncOctetString::NewLC(aParms.IV());
       
   184 			seq5->AddChildL(iv);
       
   185 			CleanupStack::Pop(iv);
       
   186 			break;
       
   187 		case ECipherRC2_CBC_128:
       
   188 		case ECipherRC2_CBC_128_16:
       
   189 			seq5 = CASN1EncSequence::NewLC();
       
   190 			seq4->AddChildL(seq5);
       
   191 			CleanupStack::Pop(seq5);
       
   192 			
       
   193 			keysize1 = CASN1EncInt::NewLC(58); //encoding for 128 bit
       
   194 			seq5->AddChildL(keysize1);
       
   195 			CleanupStack::Pop(keysize1);
       
   196 
       
   197 			iv = CASN1EncOctetString::NewLC(aParms.IV());
       
   198 			seq5->AddChildL(iv);
       
   199 			CleanupStack::Pop(iv);
       
   200 			break;
       
   201 		default:
       
   202 			User::Leave(KErrNotSupported);
       
   203 			break;
       
   204 		}
       
   205 	CleanupStack::Pop(seq);
       
   206 	return seq;
       
   207 	}
       
   208 
       
   209 EXPORT_C CPBEncryptParms* TASN1DecPKCS5::DecodeDERL(const TDesC8& aBinaryData)
       
   210 	{
       
   211 	TASN1DecGeneric seqGen(aBinaryData);
       
   212 	seqGen.InitL();
       
   213 	if (seqGen.Tag() != EASN1Sequence)
       
   214 		{
       
   215 		User::Leave(KErrArgument);
       
   216 		}
       
   217 	
       
   218 	//Decode the Algorithm Identifier Sequence
       
   219 	TASN1DecSequence seq;
       
   220 	CArrayPtrFlat<TASN1DecGeneric>* seqContents = seq.DecodeDERLC(seqGen);
       
   221 
       
   222 	//PbeAlgorithm Id
       
   223 	if (seqContents->At(0)->Tag() != EASN1ObjectIdentifier)
       
   224 		{
       
   225 		User::Leave(KErrArgument);
       
   226 		}	
       
   227 	CPBEncryptParms* params = NULL;	
       
   228 	TASN1DecObjectIdentifier oid;
       
   229 	HBufC* oiddes = oid.DecodeDERL(*(seqContents->At(0)));
       
   230 	CleanupStack::PushL(oiddes);
       
   231 	//Algorithm Id is a pkcs-12Pbe Algorithm Id.
       
   232 	if(*oiddes != Kpkcs5PBES2)
       
   233 		{
       
   234 		// Initialise to impossible value
       
   235 		TPBECipher cipher = (TPBECipher) -1; 
       
   236 		// Pbe12Algorithm Ids
       
   237 		if(*oiddes == KPbeWithSHA1And128BitRC4)
       
   238 			{
       
   239 			cipher = ECipherARC4_128;
       
   240 			}
       
   241 		else if(*oiddes == KPbeWithSHA1And40BitRC4)
       
   242 			{
       
   243 			cipher = ECipherARC4_40;
       
   244 			}
       
   245 		else if(*oiddes == KPbeWithSHA1And3_KeyTripleDES_CBC)
       
   246 			{
       
   247 			cipher = ECipher3DES_CBC;
       
   248 			}
       
   249 		else if(*oiddes == KPbeWithSHA1And2_KeyTripleDES_CBC)
       
   250 			{
       
   251 			cipher = ECipher2Key3DES_CBC;
       
   252 			}
       
   253 		else if(*oiddes == KPbeWithSHA1And128BitRC2_CBC)
       
   254 			{
       
   255 			cipher = ECipherRC2_CBC_128_16; 
       
   256 			}
       
   257 		else if(*oiddes == KPbeWithSHA1And40BitRC2_CBC)
       
   258 			{
       
   259 			cipher = ECipherRC2_CBC_40_5; 
       
   260 			}
       
   261 	    else
       
   262         	{
       
   263         	User::Leave(KErrNotSupported);
       
   264         	}	 
       
   265         
       
   266         TInt seqContentsCount = seqContents->Count();
       
   267 		
       
   268 		//All pkcs-12Pbe algorithms require the Algorithm Parameters.
       
   269 		//Algorithm Parameters are not OPTIONAL for pkcs-12Pbe algorithms.
       
   270 		
       
   271 		//seqContentsCount should be equal to 2.That is, the Algorithm Id 
       
   272 		//and associated Algorithm Parameters have to be present.
       
   273 		if(seqContentsCount != 2)
       
   274 			{
       
   275 			User::Leave(KErrArgument);	
       
   276 			}
       
   277 		//This if statement checks if the pkcs-12PbeParams Sequence is present in the 
       
   278 		//AlgorithmIdentifier Sequence Since pkcs-12PbeParams are OPTIONAL
       
   279 		else 
       
   280 			{
       
   281 			//Set the Initialization vector size to 8 bytes.
       
   282 			TBuf8<KIvSize> iv(KIvSize);
       
   283 			// Initialized to NULL, if salt is not present.
       
   284 			TPtrC8 salt;
       
   285 			TInt iterations; 
       
   286 
       
   287 			const TASN1DecGeneric* seqContentsAt1 = seqContents->At(1);
       
   288 			if (seqContentsAt1->Tag() != EASN1Sequence || seqContentsAt1->Class() != EUniversal)
       
   289 				{
       
   290 				User::Leave(KErrArgument);
       
   291 				}
       
   292 				
       
   293 			CArrayPtrFlat<TASN1DecGeneric>* seq1Contents = seq.DecodeDERLC(*seqContentsAt1);
       
   294 			const TASN1DecGeneric* seq1ContentsAt0 = seq1Contents->At(0);
       
   295 			if (seq1ContentsAt0->Tag() != EASN1OctetString || seq1ContentsAt0->Class() != EUniversal)
       
   296 				{
       
   297 				User::Leave(KErrArgument);
       
   298 				}
       
   299 			salt.Set(seq1ContentsAt0->GetContentDER());
       
   300 			const TASN1DecGeneric* seq1ContentsAt1 = seq1Contents->At(1);
       
   301 			if (seq1ContentsAt1->Tag() != EASN1Integer || seq1ContentsAt1->Class() != EUniversal)
       
   302 				{
       
   303 				User::Leave(KErrArgument);
       
   304 				}
       
   305 			TASN1DecInteger integer;
       
   306 			iterations = integer.DecodeDERShortL(*seq1ContentsAt1);
       
   307 			if (iterations <= 0)
       
   308 				{
       
   309 				User::Leave(KErrArgument);
       
   310 				}
       
   311 			params = CPBEncryptParms::NewL(cipher, salt, iv, iterations);
       
   312 			params->SetKdf(CPBEncryptParms::EKdfPkcs12);
       
   313 			CleanupStack::PopAndDestroy(seq1Contents);
       
   314 			}
       
   315 		}
       
   316 	//Algorithm Id is a pkcs-5Pbe Algorithm Id.
       
   317    	 else if (*oiddes == Kpkcs5PBES2)
       
   318     	{
       
   319     	if (seqContents->At(1)->Tag() != EASN1Sequence)
       
   320 			{
       
   321 			User::Leave(KErrArgument);
       
   322 			}
       
   323 		CArrayPtrFlat<TASN1DecGeneric>* seq1Contents = seq.DecodeDERLC(*(seqContents->At(1)));
       
   324 
       
   325 		if (seq1Contents->At(0)->Tag() != EASN1Sequence)
       
   326 			{
       
   327 			User::Leave(KErrArgument);
       
   328 			}
       
   329 		CArrayPtrFlat<TASN1DecGeneric>* seq2Contents = seq.DecodeDERLC(*(seq1Contents->At(0)));
       
   330 
       
   331 		if (seq2Contents->At(0)->Tag() != EASN1ObjectIdentifier)
       
   332 			{
       
   333 			User::Leave(KErrArgument);
       
   334 			}
       
   335 		HBufC* oid1des = oid.DecodeDERL(*(seq2Contents->At(0)));
       
   336 		CleanupStack::PushL(oid1des);
       
   337 		
       
   338 		if(*oid1des != Kpkcs5PBKDF2)
       
   339 			{
       
   340 			User::Leave(KErrNotSupported);
       
   341 			}
       
   342 		if (seq2Contents->At(1)->Tag() != EASN1Sequence)
       
   343 			{
       
   344 			User::Leave(KErrArgument);
       
   345 			}
       
   346 		CArrayPtrFlat<TASN1DecGeneric>* seq3Contents = seq.DecodeDERLC(*(seq2Contents->At(1)));
       
   347 
       
   348 		if (seq3Contents->At(0)->Tag() != EASN1OctetString)
       
   349 			{
       
   350 			User::Leave(KErrArgument);
       
   351 			}
       
   352 		TASN1DecOctetString octet;
       
   353 		HBufC8* salt = octet.DecodeDERL(*(seq3Contents->At(0)));
       
   354 		CleanupStack::PushL(salt);
       
   355 		
       
   356 		if (seq3Contents->At(1)->Tag() != EASN1Integer)
       
   357 			{
       
   358 			User::Leave(KErrArgument);
       
   359 			}
       
   360 		TASN1DecInteger integer;
       
   361 		TInt iterations = integer.DecodeDERShortL(*(seq3Contents->At(1)));
       
   362 
       
   363 		if (seq1Contents->At(1)->Tag() != EASN1Sequence)
       
   364 			{
       
   365 			User::Leave(KErrArgument);
       
   366 			}
       
   367 		CArrayPtrFlat<TASN1DecGeneric>* seq4Contents = seq.DecodeDERLC(*(seq1Contents->At(1)));
       
   368 	
       
   369 		TPBECipher cipher = (TPBECipher) -1; // Initialise to impossible value
       
   370 		if (seq4Contents->At(0)->Tag() != EASN1ObjectIdentifier)
       
   371 			{
       
   372 			User::Leave(KErrArgument);
       
   373 			}
       
   374 		HBufC* oid2des = oid.DecodeDERL(*(seq4Contents->At(0)));
       
   375 		CleanupStack::PushL(oid2des);
       
   376 		
       
   377 		CArrayPtrFlat<TASN1DecGeneric>* seq5Contents = 0; 
       
   378 
       
   379 		if(*oid2des == K3DESCBC)
       
   380 			{
       
   381 			cipher = ECipher3DES_CBC;
       
   382 		CleanupStack::PushL(seq5Contents);
       
   383 			}
       
   384 		else if(*oid2des == KDESCBC)
       
   385 			{
       
   386 			cipher = ECipherDES_CBC;
       
   387 		CleanupStack::PushL(seq5Contents);
       
   388 			}
       
   389 		else if(*oid2des == KRC2CBC)
       
   390 			{
       
   391 			// RC2 has an additional parameter, the effective key lenght in octets.
       
   392 			if (seq3Contents->At(2)->Tag() != EASN1Integer)
       
   393 				{
       
   394 				User::Leave(KErrArgument);
       
   395 				}		
       
   396 			TInt effectiveKeyLength =  integer.DecodeDERShortL(*(seq3Contents->At(2)));
       
   397 
       
   398 			if (seq4Contents->At(1)->Tag() != EASN1Sequence)
       
   399 				{
       
   400 				User::Leave(KErrArgument);
       
   401 				}
       
   402 			seq5Contents = seq.DecodeDERLC(*(seq4Contents->At(1)));
       
   403 			if (seq5Contents->At(0)->Tag() != EASN1Integer)
       
   404 				{
       
   405 				User::Leave(KErrArgument);
       
   406 				}
       
   407 			TInt keysize = integer.DecodeDERShortL(*(seq5Contents->At(0)));
       
   408 			switch(keysize)
       
   409 				{
       
   410 				// These values come from the PKCS#5 v2 specs
       
   411 				case 160:
       
   412 					if (effectiveKeyLength == 16)
       
   413 						{
       
   414 						cipher = ECipherRC2_CBC_40_16;					
       
   415 						}
       
   416 					else 
       
   417 						{
       
   418 						if (effectiveKeyLength == 128)
       
   419 							{
       
   420 							cipher = ECipherRC2_CBC_40;
       
   421 							}
       
   422 						else 
       
   423 							{
       
   424 							User::Leave(KErrNotSupported); // Unsupported effective key length!						
       
   425 							}
       
   426 						}
       
   427 					break;
       
   428 				case 58:
       
   429 					if (effectiveKeyLength == 16)
       
   430 						{
       
   431 						cipher = ECipherRC2_CBC_128_16;					
       
   432 						}
       
   433 					else 
       
   434 						{
       
   435 						if (effectiveKeyLength == 128)
       
   436 							{
       
   437 							cipher = ECipherRC2_CBC_128;
       
   438 							}
       
   439 						else 
       
   440 							{
       
   441 							User::Leave(KErrNotSupported); // Unsupported effective key length!						
       
   442 							}
       
   443 						}			
       
   444 					break;
       
   445 				case 120:
       
   446 					//would be RC_CBC_64 but we don't support that
       
   447 				default:
       
   448 					User::Leave(KErrNotSupported);
       
   449 					break;
       
   450 				}
       
   451 			}
       
   452 		else 
       
   453 			{
       
   454 			User::Leave(KErrNotSupported);
       
   455 			}	
       
   456 
       
   457 		HBufC8* iv = 0;
       
   458 		switch(cipher)
       
   459 			{
       
   460 			case ECipher3DES_CBC:
       
   461 			case ECipherDES_CBC:
       
   462 				if (seq4Contents->At(1)->Tag() != EASN1OctetString)
       
   463 					{
       
   464 					User::Leave(KErrArgument);
       
   465 					}
       
   466 				iv = octet.DecodeDERL(*(seq4Contents->At(1)));	
       
   467 			CleanupStack::PushL(iv);
       
   468 				break;
       
   469 			case ECipherRC2_CBC_40:
       
   470 			case ECipherRC2_CBC_128:
       
   471 			case ECipherRC2_CBC_40_16:
       
   472 			case ECipherRC2_CBC_128_16:	
       
   473 				if (seq5Contents->At(1)->Tag() != EASN1OctetString)
       
   474 					{
       
   475 					User::Leave(KErrArgument);
       
   476 					}
       
   477 				iv = octet.DecodeDERL(*(seq5Contents->At(1)));
       
   478 			CleanupStack::PushL(iv);
       
   479 				break;
       
   480 			default:
       
   481 				User::Leave(KErrNotSupported);
       
   482 				break;
       
   483 			}
       
   484 
       
   485 		params = CPBEncryptParms::NewL(cipher, *salt, *iv,
       
   486 		iterations);
       
   487 	CleanupStack::PopAndDestroy(9); //iv, seq5contents, oid2des, seq4Contents,
       
   488 	//salt, seq3Contents, oid1des, seq2Contents, seq1Contents
       
   489     	}
       
   490 	else
       
   491 		{
       
   492 		User::Leave(KErrNotSupported);
       
   493 		}
       
   494 	CleanupStack::PopAndDestroy(2, seqContents);
       
   495 	return params;
       
   496 	}