1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/tpadding/tpaddingSSLv3.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,349 @@
1.4 +/*
1.5 +* Copyright (c) 2004-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 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <padding.h>
1.23 +#include "tpaddingSSLv3.h"
1.24 +#include <securityerr.h>
1.25 +
1.26 +CTestPadSSLv3::CTestPadSSLv3()
1.27 + {
1.28 + SetTestStepName(KPadSSLv3);
1.29 + }
1.30 +
1.31 +CTestPadSSLv3::~CTestPadSSLv3()
1.32 + {
1.33 + }
1.34 +
1.35 +TVerdict CTestPadSSLv3::doTestStepL()
1.36 + {
1.37 + SetTestStepResult(EPass);
1.38 + __UHEAP_MARK;
1.39 +
1.40 + INFO_PRINTF1(_L("Test of padding with type SSLv3"));
1.41 +
1.42 + TInt blockSize;
1.43 + TInt textSize;
1.44 +
1.45 + if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
1.46 + {
1.47 + INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
1.48 + return EFail;
1.49 + }
1.50 +
1.51 + if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
1.52 + {
1.53 + INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
1.54 + return EFail;
1.55 + }
1.56 +
1.57 + if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
1.58 + {
1.59 + TestSSLv3Padding(blockSize);
1.60 + }
1.61 + else
1.62 + {
1.63 + if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
1.64 + {
1.65 + INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
1.66 + return EFail;
1.67 + }
1.68 + if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
1.69 + {
1.70 + INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
1.71 + return EFail;
1.72 + }
1.73 + TestSSLv3CorruptPadding(blockSize, textSize);
1.74 + }
1.75 + __UHEAP_MARKEND;
1.76 + return TestStepResult();
1.77 + }
1.78 +
1.79 +void CTestPadSSLv3::TestSSLv3Padding(TInt aBlockSize)
1.80 + {
1.81 + CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
1.82 +
1.83 + for (TInt i = 0 ; i <= aBlockSize; i++)
1.84 + {
1.85 + HBufC8 *padInData = HBufC8::NewLC(i);
1.86 + HBufC8 *padOutData = HBufC8::NewLC(i+(aBlockSize-i%aBlockSize));
1.87 + TPtr8 in(padInData->Des());
1.88 + TPtr8 out(padOutData->Des());
1.89 + TInt j;
1.90 +
1.91 + for (j = 0; j < i; j++)
1.92 + {
1.93 + TInt text('a'+j%25);
1.94 + in.Append(text);
1.95 + }
1.96 + TRAPD(err, padding->DoPadL(in, out));
1.97 + INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
1.98 + TEST(err == KErrNone);
1.99 +
1.100 + TInt totalLength = out.Length();
1.101 + TInt paddingLength = aBlockSize - in.Length()%aBlockSize;
1.102 + // Test that the total length is a multiple of blockSize
1.103 + TEST((totalLength % aBlockSize) == 0);
1.104 +
1.105 + // Test that the padding bytes are equal in value to the paddingLength,
1.106 + // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
1.107 + // This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
1.108 + for (TInt i = paddingLength; i > 0 ; i--)
1.109 + {
1.110 + TEST(out[out.Length()-i] == paddingLength - 1);
1.111 + }
1.112 +
1.113 + // Test that the data has not been corrupted
1.114 + TEST(in == out.Left(out.Length() - paddingLength));
1.115 +
1.116 + CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
1.117 + }
1.118 + CleanupStack::PopAndDestroy(padding);
1.119 + }
1.120 +
1.121 +void CTestPadSSLv3::TestSSLv3CorruptPadding(TInt aBlockSize, TInt aTextSize)
1.122 + {
1.123 + CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
1.124 +
1.125 + HBufC8 *padInData = HBufC8::NewLC(aTextSize);
1.126 + TInt paddingBytes = 0;
1.127 + //If BlockSize is 0, Divide by 0 is undefined
1.128 + if(aBlockSize != 0)
1.129 + {
1.130 + paddingBytes = (aBlockSize - aTextSize % aBlockSize);
1.131 + }
1.132 + HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
1.133 + TPtr8 in(padInData->Des());
1.134 + TPtr8 out(padOutData->Des());
1.135 + TInt j;
1.136 +
1.137 + for (j = 0; j < aTextSize; j++)
1.138 + {
1.139 + TInt text('a'+j%25);
1.140 + in.Append(text);
1.141 + }
1.142 + TRAPD(err, padding->DoPadL(in, out));
1.143 +
1.144 + if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
1.145 + {
1.146 + TEST(err == KErrArgument);
1.147 + }
1.148 + else if(iExpectedResult.Compare(_L("Valid")) ==0)
1.149 + {
1.150 + TEST(err == KErrNone);
1.151 + }
1.152 + else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
1.153 + {
1.154 + TEST(err == KErrInvalidPadding);
1.155 + }
1.156 +
1.157 + TInt totalLength = out.Length();
1.158 + TInt paddingLength = 0;
1.159 + //If BlockSize is 0, Divide by 0 is undefined
1.160 + if(aBlockSize != 0)
1.161 + {
1.162 + paddingLength = aBlockSize - in.Length()%aBlockSize;
1.163 + }
1.164 + //If BlockSize is 0, Divide by 0 is undefined
1.165 + if(aBlockSize != 0)
1.166 + {
1.167 + // Test that the total length is a multiple of blockSize
1.168 + TEST((totalLength % aBlockSize) == 0);
1.169 + }
1.170 +
1.171 + // Test that the padding bytes are equal in value to the paddingLength,
1.172 + // ie, if padding length is 5 the 5 last octets in the out array should be 0x05
1.173 + // This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
1.174 + for (TInt i = paddingLength; i > 0 ; i--)
1.175 + {
1.176 + TEST(out[out.Length()-i] == paddingLength - 1);
1.177 + }
1.178 +
1.179 + if(aBlockSize > 0)
1.180 + {
1.181 + // Test that the data has not been corrupted
1.182 + TEST(in == out.Left(out.Length() - paddingLength));
1.183 + }
1.184 +
1.185 + CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
1.186 + CleanupStack::PopAndDestroy(padding);
1.187 + }
1.188 +
1.189 +CTestUnpadSSLv3::CTestUnpadSSLv3()
1.190 + {
1.191 + SetTestStepName(KUnpadSSLv3);
1.192 + }
1.193 +
1.194 +CTestUnpadSSLv3::~CTestUnpadSSLv3()
1.195 + {
1.196 + }
1.197 +
1.198 +TVerdict CTestUnpadSSLv3::doTestStepL()
1.199 + {
1.200 + SetTestStepResult(EPass);
1.201 + __UHEAP_MARK;
1.202 +
1.203 + INFO_PRINTF1(_L("Test of unpadding with type SSLv3"));
1.204 +
1.205 + TInt blockSize;
1.206 + TInt textSize;
1.207 + if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
1.208 + {
1.209 + INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
1.210 + return EFail;
1.211 + }
1.212 +
1.213 + if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
1.214 + {
1.215 + INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
1.216 + return EFail;
1.217 + }
1.218 +
1.219 + if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
1.220 + {
1.221 + TestSSLv3Unpadding(blockSize);
1.222 + }
1.223 + else
1.224 + {
1.225 + if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
1.226 + {
1.227 + INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
1.228 + return EFail;
1.229 + }
1.230 +
1.231 + if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
1.232 + {
1.233 + INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
1.234 + return EFail;
1.235 + }
1.236 + TestSSLv3CorruptUnpadding(blockSize, textSize);
1.237 + }
1.238 +
1.239 + __UHEAP_MARKEND;
1.240 + return TestStepResult();
1.241 + }
1.242 +
1.243 +void CTestUnpadSSLv3::TestSSLv3Unpadding(TInt aBlockSize)
1.244 + {
1.245 + CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
1.246 +
1.247 + for (TInt i = 0 ; i <= aBlockSize; i++)
1.248 + {
1.249 + HBufC8 *padInData = HBufC8::NewLC(i+(aBlockSize - i%aBlockSize));
1.250 + HBufC8 *padOutData = HBufC8::NewLC(i);
1.251 + HBufC8 *padCompareData = HBufC8::NewLC(i);
1.252 + TPtr8 in(padInData->Des());
1.253 + TPtr8 out(padOutData->Des());
1.254 + TPtr8 comp(padCompareData->Des());
1.255 + TInt j;
1.256 +
1.257 + // build up a padded string here
1.258 + for (j = 0; j < i; j++)
1.259 + {
1.260 + TInt text('a'+j%25);
1.261 + in.Append(text);
1.262 + }
1.263 + comp.Append(in);
1.264 +
1.265 + TInt paddingBytes = aBlockSize - i%aBlockSize;
1.266 + in.SetLength(in.Length() + paddingBytes);
1.267 +
1.268 + // pad with arbitary data, to test unpadding of SSL 3.0 padded data
1.269 + in[in.Length()-1] = (TUint8) (paddingBytes - 1);
1.270 + for (j = 2; j <= paddingBytes; j++)
1.271 + {
1.272 + in[in.Length()-j] = (TUint8) ('a' + j%25);
1.273 + }
1.274 + TRAPD(err, padding->UnPadL(in, out));
1.275 + INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
1.276 + TEST(err == KErrNone);
1.277 +
1.278 + // test if the unpadding was correct.
1.279 + TEST(out == comp);
1.280 +
1.281 + CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
1.282 + }
1.283 + CleanupStack::PopAndDestroy(padding);
1.284 + }
1.285 +
1.286 +void CTestUnpadSSLv3::TestSSLv3CorruptUnpadding(TInt aBlockSize, TInt aTextSize)
1.287 + {
1.288 + CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
1.289 +
1.290 + TInt paddingBytes = 0;
1.291 + //If BlockSize is 0, Divide by 0 is undefined
1.292 + if(aBlockSize != 0)
1.293 + {
1.294 + paddingBytes = aBlockSize - aTextSize%aBlockSize;
1.295 + }
1.296 + HBufC8 *padInData = HBufC8::NewLC(aTextSize+ paddingBytes);
1.297 + HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
1.298 + HBufC8 *padCompareData = HBufC8::NewLC(aTextSize);
1.299 + TPtr8 in(padInData->Des());
1.300 + TPtr8 out(padOutData->Des());
1.301 + TPtr8 comp(padCompareData->Des());
1.302 + TInt j;
1.303 +
1.304 + if(in.Length() < aTextSize)
1.305 + {
1.306 + for (j = 0; j < in.Length(); j++)
1.307 + {
1.308 + TInt text('a'+j%25);
1.309 + in.Append(text);
1.310 + }
1.311 + }
1.312 + else
1.313 + {
1.314 + // build up a padded string here
1.315 + for (j = 0; j < aTextSize; j++)
1.316 + {
1.317 + TInt text('a'+j%25);
1.318 + in.Append(text);
1.319 + }
1.320 + }
1.321 + comp.Append(in);
1.322 +
1.323 + in.SetLength(in.Length() + paddingBytes);
1.324 +
1.325 + if(aBlockSize > 0)
1.326 + {
1.327 + // pad with arbitary data, to test unpadding of SSL 3.0 padded data
1.328 + in[in.Length()-1] = (TUint8) (paddingBytes - 1);
1.329 + for (j = 2; j <= paddingBytes; j++)
1.330 + {
1.331 + in[in.Length()-j] = (TUint8) ('a' + j%25);
1.332 + }
1.333 + }
1.334 + TRAPD(err, padding->UnPadL(in, out));
1.335 +
1.336 + if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
1.337 + {
1.338 + TEST(err == KErrArgument);
1.339 + }
1.340 + else if(iExpectedResult.Compare(_L("Valid")) ==0)
1.341 + {
1.342 + TEST(err == KErrNone);
1.343 + }
1.344 + else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
1.345 + {
1.346 + TEST(err == KErrInvalidPadding);
1.347 + }
1.348 + // test if the unpadding was correct.
1.349 + TEST(out == comp);
1.350 + CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
1.351 + CleanupStack::PopAndDestroy(padding);
1.352 + }