os/security/cryptoservices/certificateandkeymgmt/tasn1/testinvalidoid.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2006-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 * Implementation for testing object identifier decoding with invalid data
    16 *
    17 */
    18 
    19 
    20 #include "testinvalidoid.h"
    21 #include "tasn1normaltest.h"
    22 #include <asn1enc.h>
    23 #include <asn1dec.h>
    24 
    25 #include <e32cons.h>
    26 #include <e32math.h>
    27 
    28 const TInt KNumberOftests = 2;
    29 
    30 CTestInvalidOID* CTestInvalidOID::NewL(CASN1NormalTest &aASN1Action)
    31 	{
    32 	CTestInvalidOID* test = new (ELeave) CTestInvalidOID(aASN1Action);
    33 	return test;
    34 	}
    35 
    36 CTestInvalidOID::CTestInvalidOID(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
    37 	{
    38 	};
    39 
    40 
    41 void CTestInvalidOID::GetName(TDes& aBuf)
    42 	{
    43 	aBuf.Copy(_L("Test Invalid Object Identifier"));
    44 	}
    45 
    46 
    47 void CTestInvalidOID::FillParameterArray(void)
    48 	{
    49 	iParameters->Append(CTestParameter::EString);
    50 	}
    51 
    52 TBool CTestInvalidOID::PerformTestsL(CConsoleBase& aConsole)
    53 	{
    54 	TASN1DecObjectIdentifier decoder;
    55 	TInt readLength = 0;
    56 	HBufC* decodedData= NULL;
    57 	TInt err = KErrNone;
    58 	TInt testNumber = 1;
    59 	
    60 	// test some hardcoded examples of badly formed ASN1
    61 	
    62 	/* check decoding of overflow example "1.2.826.0.1.1796587.1.1.1.72057594037927942"
    63 	from DEF099095: OID parsing error can cause X509.v3 extensions to be eclipsed */
    64 	_LIT8(KASN1overflow, "\x06\x14\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x81\x80\x80\x80\x80\x80\x80\x80\x06");
    65 	TRAP( err, decodedData = decoder.DecodeDERL(KASN1overflow, readLength));
    66 	if ((err == KErrOverflow ) && ( decodedData == NULL ))
    67 		{
    68 		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
    69 		}
    70 	else
    71 		{
    72 		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
    73 		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
    74 		}		
    75 
    76 	testNumber++;
    77 	
    78 	// check 30 bit value is ok 0x20000006 = 536870918
    79 	_LIT8(KASN130bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x82\x80\x80\x80\x06");
    80 	_LIT16(KASN130bitResult, "1.2.826.0.1.1796587.1.1.1.536870918");
    81 	err = KErrNone;
    82 	TRAP( err, decodedData = decoder.DecodeDERL(KASN130bit, readLength));
    83 	TPtr pData = decodedData->Des();
    84 	if ((err == KErrNone ) && ( pData.Compare( KASN130bitResult ) ==0  )  )
    85 		{
    86 		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
    87 		delete decodedData;
    88 		}
    89 	else
    90 		{
    91 		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
    92 		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
    93 		}		
    94 
    95 	testNumber++;
    96 
    97 	// check 31 bit value is ok 0x40000006 = 1073741830
    98 	_LIT8(KASN131bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x84\x80\x80\x80\x06");
    99 	_LIT16(KASN131bitResult, "1.2.826.0.1.1796587.1.1.1.1073741830");
   100 	err = KErrNone;
   101 	readLength = 0;
   102 	TRAP( err, decodedData = decoder.DecodeDERL(KASN131bit, readLength));
   103 	pData = decodedData->Des();
   104 	if ((err == KErrNone ) && ( pData.Compare( KASN131bitResult ) ==0  ))
   105 		{
   106 		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   107 		delete decodedData;
   108 		decodedData = 0;
   109 		}
   110 	else
   111 		{
   112 		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   113 		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   114 		}		
   115 
   116 	testNumber++;
   117 
   118 	// check 32 bit value does NOT decode 0x80000006 = 2147483654
   119 	_LIT8(KASN132bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x88\x80\x80\x80\x06");
   120 	err = KErrNone;
   121 	readLength = 0;
   122 	TRAP( err, decodedData = decoder.DecodeDERL(KASN132bit, readLength));
   123 	if ((err == KErrOverflow ) && ( decodedData == NULL ))
   124 		{
   125  		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   126 		}
   127 	else
   128 		{
   129 		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   130 		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   131 		}		
   132 
   133 	testNumber++;
   134 
   135 	// check that too many ids causes KErrOverflow, "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1" 
   136 	_LIT8(KASN1ToManyIds, "\x06\x13\x2A\x86\x3A\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01");
   137 	err = KErrNone;
   138 	readLength = 0;
   139 	TRAP( err, decodedData = decoder.DecodeDERL(KASN1ToManyIds, readLength));
   140 	if ((err == KErrOverflow ) && ( decodedData == NULL ))
   141 		{
   142 		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   143 		}
   144 	else
   145 		{
   146 		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   147 		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   148 		}		
   149 
   150 	/* some more examples of Bad ASN encoding can be added here */
   151 	
   152 	
   153 	return(ETrue);
   154 	}
   155