os/security/cryptoservices/certificateandkeymgmt/tasn1/testinvalidoid.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tasn1/testinvalidoid.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,155 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Implementation for testing object identifier decoding with invalid data
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include "testinvalidoid.h"
    1.24 +#include "tasn1normaltest.h"
    1.25 +#include <asn1enc.h>
    1.26 +#include <asn1dec.h>
    1.27 +
    1.28 +#include <e32cons.h>
    1.29 +#include <e32math.h>
    1.30 +
    1.31 +const TInt KNumberOftests = 2;
    1.32 +
    1.33 +CTestInvalidOID* CTestInvalidOID::NewL(CASN1NormalTest &aASN1Action)
    1.34 +	{
    1.35 +	CTestInvalidOID* test = new (ELeave) CTestInvalidOID(aASN1Action);
    1.36 +	return test;
    1.37 +	}
    1.38 +
    1.39 +CTestInvalidOID::CTestInvalidOID(CASN1NormalTest &aASN1Action) : CTestBase(aASN1Action)
    1.40 +	{
    1.41 +	};
    1.42 +
    1.43 +
    1.44 +void CTestInvalidOID::GetName(TDes& aBuf)
    1.45 +	{
    1.46 +	aBuf.Copy(_L("Test Invalid Object Identifier"));
    1.47 +	}
    1.48 +
    1.49 +
    1.50 +void CTestInvalidOID::FillParameterArray(void)
    1.51 +	{
    1.52 +	iParameters->Append(CTestParameter::EString);
    1.53 +	}
    1.54 +
    1.55 +TBool CTestInvalidOID::PerformTestsL(CConsoleBase& aConsole)
    1.56 +	{
    1.57 +	TASN1DecObjectIdentifier decoder;
    1.58 +	TInt readLength = 0;
    1.59 +	HBufC* decodedData= NULL;
    1.60 +	TInt err = KErrNone;
    1.61 +	TInt testNumber = 1;
    1.62 +	
    1.63 +	// test some hardcoded examples of badly formed ASN1
    1.64 +	
    1.65 +	/* check decoding of overflow example "1.2.826.0.1.1796587.1.1.1.72057594037927942"
    1.66 +	from DEF099095: OID parsing error can cause X509.v3 extensions to be eclipsed */
    1.67 +	_LIT8(KASN1overflow, "\x06\x14\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x81\x80\x80\x80\x80\x80\x80\x80\x06");
    1.68 +	TRAP( err, decodedData = decoder.DecodeDERL(KASN1overflow, readLength));
    1.69 +	if ((err == KErrOverflow ) && ( decodedData == NULL ))
    1.70 +		{
    1.71 +		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
    1.72 +		}
    1.73 +	else
    1.74 +		{
    1.75 +		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
    1.76 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
    1.77 +		}		
    1.78 +
    1.79 +	testNumber++;
    1.80 +	
    1.81 +	// check 30 bit value is ok 0x20000006 = 536870918
    1.82 +	_LIT8(KASN130bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x82\x80\x80\x80\x06");
    1.83 +	_LIT16(KASN130bitResult, "1.2.826.0.1.1796587.1.1.1.536870918");
    1.84 +	err = KErrNone;
    1.85 +	TRAP( err, decodedData = decoder.DecodeDERL(KASN130bit, readLength));
    1.86 +	TPtr pData = decodedData->Des();
    1.87 +	if ((err == KErrNone ) && ( pData.Compare( KASN130bitResult ) ==0  )  )
    1.88 +		{
    1.89 +		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
    1.90 +		delete decodedData;
    1.91 +		}
    1.92 +	else
    1.93 +		{
    1.94 +		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
    1.95 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
    1.96 +		}		
    1.97 +
    1.98 +	testNumber++;
    1.99 +
   1.100 +	// check 31 bit value is ok 0x40000006 = 1073741830
   1.101 +	_LIT8(KASN131bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x84\x80\x80\x80\x06");
   1.102 +	_LIT16(KASN131bitResult, "1.2.826.0.1.1796587.1.1.1.1073741830");
   1.103 +	err = KErrNone;
   1.104 +	readLength = 0;
   1.105 +	TRAP( err, decodedData = decoder.DecodeDERL(KASN131bit, readLength));
   1.106 +	pData = decodedData->Des();
   1.107 +	if ((err == KErrNone ) && ( pData.Compare( KASN131bitResult ) ==0  ))
   1.108 +		{
   1.109 +		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   1.110 +		delete decodedData;
   1.111 +		decodedData = 0;
   1.112 +		}
   1.113 +	else
   1.114 +		{
   1.115 +		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   1.116 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   1.117 +		}		
   1.118 +
   1.119 +	testNumber++;
   1.120 +
   1.121 +	// check 32 bit value does NOT decode 0x80000006 = 2147483654
   1.122 +	_LIT8(KASN132bit, "\x06\x10\x2A\x86\x3A\x00\x01\xED\xD3\x6B\x01\x01\x01\x88\x80\x80\x80\x06");
   1.123 +	err = KErrNone;
   1.124 +	readLength = 0;
   1.125 +	TRAP( err, decodedData = decoder.DecodeDERL(KASN132bit, readLength));
   1.126 +	if ((err == KErrOverflow ) && ( decodedData == NULL ))
   1.127 +		{
   1.128 + 		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   1.129 +		}
   1.130 +	else
   1.131 +		{
   1.132 +		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   1.133 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   1.134 +		}		
   1.135 +
   1.136 +	testNumber++;
   1.137 +
   1.138 +	// check that too many ids causes KErrOverflow, "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1" 
   1.139 +	_LIT8(KASN1ToManyIds, "\x06\x13\x2A\x86\x3A\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01");
   1.140 +	err = KErrNone;
   1.141 +	readLength = 0;
   1.142 +	TRAP( err, decodedData = decoder.DecodeDERL(KASN1ToManyIds, readLength));
   1.143 +	if ((err == KErrOverflow ) && ( decodedData == NULL ))
   1.144 +		{
   1.145 +		iASN1Action.ReportProgressL(KErrNone, testNumber, KNumberOftests);
   1.146 +		}
   1.147 +	else
   1.148 +		{
   1.149 +		aConsole.Write(_L("ERROR!\nShould NOT be able to decode \n"));
   1.150 +		iASN1Action.ReportProgressL(KErrASN1EncodingError, testNumber, KNumberOftests);
   1.151 +		}		
   1.152 +
   1.153 +	/* some more examples of Bad ASN encoding can be added here */
   1.154 +	
   1.155 +	
   1.156 +	return(ETrue);
   1.157 +	}
   1.158 +