os/security/cryptoservices/certificateandkeymgmt/tcertstore/T_unifiedcertstoreadd.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tcertstore/T_unifiedcertstoreadd.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,809 @@
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 "t_unifiedcertstoreadd.h"
1.23 +#include "t_certstoredefs.h"
1.24 +#include "t_input.h"
1.25 +#include "t_certstoreout.h"
1.26 +#include <mctwritablecertstore.h>
1.27 +
1.28 +CTestAction* CAddCertificate::NewL(RFs& aFs,
1.29 + CConsoleBase& aConsole,
1.30 + Output& aOut,
1.31 + const TTestActionSpec& aTestActionSpec)
1.32 + {
1.33 + CAddCertificate* self = new(ELeave) CAddCertificate(aFs, aConsole, aOut);
1.34 + CleanupStack::PushL(self);
1.35 + self->ConstructL(aTestActionSpec);
1.36 + CleanupStack::Pop(self);
1.37 + return self;
1.38 + }
1.39 +
1.40 +CAddCertificate::~CAddCertificate()
1.41 + {
1.42 + delete iCertificate;
1.43 + delete iCertificateURL;
1.44 + delete iCertificateContent;
1.45 + delete iCertificateLabel;
1.46 + }
1.47 +
1.48 +void CAddCertificate::PerformAction(TRequestStatus& aStatus)
1.49 + {
1.50 + switch (iState)
1.51 + {
1.52 + case EAdding:
1.53 + {
1.54 + if (iNotificationSubscribed)
1.55 + {
1.56 + if (!iNotifier)
1.57 + {
1.58 + iNotifier = CCertStoreChangeNotifier::NewL(iNotifierFlag);
1.59 + iNotifier->StartNotification();
1.60 + }
1.61 + iState = ECheckNotification;
1.62 + }
1.63 + else
1.64 + {
1.65 + iState = EFinished;
1.66 + }
1.67 + MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
1.68 +
1.69 + TKeyIdentifier* issuerKeyId = &iIssuerKeyId;
1.70 + TKeyIdentifier* subjectKeyId = &iSubjectKeyId;
1.71 +
1.72 + ASSERT(iCertificateLabel);
1.73 +
1.74 + // Use the Add() with Deletable param if Deletable flag present in test data
1.75 + if (iDeletableFlagPresent)
1.76 + {
1.77 + store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType,
1.78 + subjectKeyId, issuerKeyId, *iCertificateContent,
1.79 + iDeletable, aStatus);
1.80 + }
1.81 + // otherwise, use the original Add()
1.82 + else
1.83 + {
1.84 + store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType,
1.85 + subjectKeyId, issuerKeyId, *iCertificateContent, aStatus);
1.86 + }
1.87 + }
1.88 + break;
1.89 + case ECheckNotification:
1.90 + {
1.91 + iState = EFinished;
1.92 + if (iNotifierFlag)
1.93 + {
1.94 + TRequestStatus* status = &aStatus;
1.95 + User::RequestComplete(status, KErrNone);
1.96 + }
1.97 + else
1.98 + {
1.99 + iNotifier->SetCompleteStatus(&aStatus);
1.100 + }
1.101 + break;
1.102 + }
1.103 + case EFinished:
1.104 + {
1.105 + if (aStatus == iExpectedResult)
1.106 + {
1.107 + iResult = ETrue;
1.108 + }
1.109 + else
1.110 + {
1.111 + iResult = EFalse;
1.112 + }
1.113 +
1.114 + if (aStatus != KErrNoMemory)
1.115 + {
1.116 + iFinished = ETrue;
1.117 + }
1.118 +
1.119 + TRequestStatus* status = &aStatus;
1.120 + User::RequestComplete(status, aStatus.Int());
1.121 + }
1.122 + break;
1.123 +
1.124 + default:
1.125 + User::Invariant();
1.126 + break;
1.127 + }
1.128 + }
1.129 +
1.130 +void CAddCertificate::PerformCancel()
1.131 + {
1.132 + switch (iState)
1.133 + {
1.134 + case ECheckNotification:
1.135 + case EFinished:
1.136 + {
1.137 + MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
1.138 + store.CancelAdd();
1.139 + break;
1.140 + }
1.141 + default:
1.142 + break;
1.143 + }
1.144 + }
1.145 +
1.146 +void CAddCertificate::AfterOOMFailure()
1.147 + {
1.148 + }
1.149 +
1.150 +void CAddCertificate::Reset()
1.151 + {
1.152 + iState = EAdding;
1.153 + }
1.154 +
1.155 +void CAddCertificate::DoReportAction()
1.156 + {
1.157 + iOut.writeString(_L("Adding certificate..."));
1.158 + iOut.writeNewLine();
1.159 + iOut.writeString(_L("\tLabel = "));
1.160 + iOut.writeString(*iCertificateLabel);
1.161 + iOut.writeNewLine();
1.162 + iOut.writeString(_L("\tOwner type = "));
1.163 + WriteOwnerType();
1.164 + WriteFormat();
1.165 + iOut.writeString(_L("\tSubjectKeyId: "));
1.166 + iOut.writeOctetString(iSubjectKeyId);
1.167 + iOut.writeNewLine();
1.168 + iOut.writeString(_L("\tDeletable = "));
1.169 + iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
1.170 + iOut.writeNewLine();
1.171 + iOut.writeNewLine();
1.172 + }
1.173 +
1.174 +void CAddCertificate::WriteFormat()
1.175 + {
1.176 + iOut.writeString(_L("\tFormat = "));
1.177 + switch (iCertificateFormat)
1.178 + {
1.179 + case EX509Certificate:
1.180 + iOut.writeString(_L("X.509\n"));
1.181 + break;
1.182 +
1.183 + case EWTLSCertificate:
1.184 + iOut.writeString(_L("WTLS\n"));
1.185 + break;
1.186 +
1.187 + case EX509CertificateUrl:
1.188 + iOut.writeString(_L("X.509 URL\n"));
1.189 + break;
1.190 +
1.191 + case EWTLSCertificateUrl:
1.192 + iOut.writeString(_L("WTLS URL\n"));
1.193 + break;
1.194 +
1.195 + default:
1.196 + iOut.writeString(_L("Unknown format\n"));
1.197 + break;
1.198 + }
1.199 + }
1.200 +
1.201 +void CAddCertificate::WriteOwnerType()
1.202 + {
1.203 + switch (iOwnerType)
1.204 + {
1.205 + case ECACertificate:
1.206 + iOut.writeString(_L("CA\n"));
1.207 + break;
1.208 +
1.209 + case EUserCertificate:
1.210 + iOut.writeString(_L("User"));
1.211 + break;
1.212 +
1.213 + case EPeerCertificate:
1.214 + iOut.writeString(_L("Peer"));
1.215 + break;
1.216 +
1.217 + default:
1.218 + iOut.writeString(_L("Unknown"));
1.219 + break;
1.220 + }
1.221 + }
1.222 +
1.223 +CAddCertificate::CAddCertificate(RFs& aFs, CConsoleBase& aConsole,
1.224 + Output& aOut)
1.225 +: CSubscriberAction(aFs, aConsole, aOut), iState(EAdding),
1.226 + iDeletable(ETrue), iDeletableFlagPresent(EFalse)
1.227 + {
1.228 + }
1.229 +
1.230 +void CAddCertificate::ConstructL(const TTestActionSpec& aTestActionSpec)
1.231 + {
1.232 + CSubscriberAction::ConstructL(aTestActionSpec);
1.233 +
1.234 + SetCertFormatL(Input::ParseElement(aTestActionSpec.iActionBody, KCertFormatStart));
1.235 + SetCertOwnerTypeL(Input::ParseElement(aTestActionSpec.iActionBody, KCertOwnerTypeStart));
1.236 + SetCertLabelL(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
1.237 + SetKeyId(iIssuerKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KIssuerKeyStart));
1.238 + SetKeyId(iSubjectKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KSubjectKeyStart));
1.239 + SetStoreToUse(Input::ParseElement(aTestActionSpec.iActionBody, KStoreToUseStart));
1.240 +
1.241 + TPtrC8 certFileOrURL = Input::ParseElement(aTestActionSpec.iActionBody, KCertFileStart);
1.242 +
1.243 + SetCertificateContentL(certFileOrURL);
1.244 +
1.245 + if (iCertificateFormat == EX509CertificateUrl ||
1.246 + iCertificateFormat == EWTLSCertificateUrl)
1.247 + {
1.248 + iCertificateURL = certFileOrURL.AllocL();
1.249 + }
1.250 + else
1.251 + {
1.252 + ConstructCertL(certFileOrURL);
1.253 + }
1.254 +
1.255 + // check for a possible deletable flag value for the certificate
1.256 + TInt err = KErrNone;
1.257 + TInt pos = 0;
1.258 + const TDesC8& deletableStr = Input::ParseElement(aTestActionSpec.iActionBody,
1.259 + KDeletableStart,
1.260 + KDeletableEnd,
1.261 + pos,
1.262 + err);
1.263 +
1.264 + // set the deletable attribute if a value was found for the certificate
1.265 + if (err == KErrNone)
1.266 + {
1.267 + SetDeletable(deletableStr);
1.268 + }
1.269 +
1.270 + // Setting the expected result
1.271 + HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
1.272 + TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
1.273 + Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
1.274 + CleanupStack::PopAndDestroy(result);
1.275 + }
1.276 +
1.277 +void CAddCertificate::SetKeyId(TKeyIdentifier& aKeyIdentifier, const TDesC8& aKeyInfo)
1.278 + {
1.279 + TInt size = aKeyInfo.Length();
1.280 + for (TInt i = 0; i < size; i += 2)
1.281 + {
1.282 + TInt a = (aKeyInfo[i+1] >= 'a') ? (aKeyInfo[i+1] - 'a' + 10) : (aKeyInfo[i+1] - '0');
1.283 + TInt b = (aKeyInfo[i] >= 'a') ? (aKeyInfo[i] - 'a' + 10) : (aKeyInfo[i] - '0');
1.284 + aKeyIdentifier.Append(a + b * 16);
1.285 + }
1.286 + }
1.287 +
1.288 +void CAddCertificate::SetCertFormatL(const TDesC8& aFormat)
1.289 + {
1.290 + if (aFormat == KX509)
1.291 + {
1.292 + iCertificateFormat = EX509Certificate;
1.293 + }
1.294 + else if (aFormat == KWTLS)
1.295 + {
1.296 + iCertificateFormat = EWTLSCertificate;
1.297 + }
1.298 + else if (aFormat == KX509URL)
1.299 + {
1.300 + iCertificateFormat = EX509CertificateUrl;
1.301 + }
1.302 + else if (aFormat == KWTLSURL)
1.303 + {
1.304 + iCertificateFormat = EWTLSCertificateUrl;
1.305 + }
1.306 + else if (aFormat == KUnknown)
1.307 + {
1.308 + iCertificateFormat = EUnknownCertificate;
1.309 + }
1.310 + else
1.311 + {
1.312 + iOut.write(_L("Unknown cert format: "));
1.313 + iOut.writeString(aFormat);
1.314 + iOut.writeNewLine();
1.315 + User::Leave(KErrArgument);
1.316 + }
1.317 + }
1.318 +
1.319 +void CAddCertificate::SetCertOwnerTypeL(const TDesC8& aOwnerType)
1.320 + {
1.321 + if (aOwnerType == KCACert)
1.322 + {
1.323 + iOwnerType = ECACertificate;
1.324 + }
1.325 + else if (aOwnerType == KUserCert)
1.326 + {
1.327 + iOwnerType = EUserCertificate;
1.328 + }
1.329 + else if (aOwnerType == KPeerCert)
1.330 + {
1.331 + iOwnerType = EPeerCertificate;
1.332 + }
1.333 + else if (aOwnerType == KUnknown)
1.334 + {
1.335 + // set dummy bogus owner type
1.336 + iOwnerType = static_cast<TCertificateOwnerType>(EPeerCertificate + 1);
1.337 + }
1.338 + else
1.339 + {
1.340 + iOut.write(_L("Unknown cert owner type: "));
1.341 + iOut.writeString(aOwnerType);
1.342 + iOut.writeNewLine();
1.343 + User::Leave(KErrArgument);
1.344 + }
1.345 + }
1.346 +
1.347 +void CAddCertificate::SetCertLabelL(const TDesC8& aLabel)
1.348 + {
1.349 + delete iCertificateLabel;
1.350 + iCertificateLabel = NULL;
1.351 + iCertificateLabel = HBufC::NewL(aLabel.Length());
1.352 + TPtr ptr = iCertificateLabel->Des();
1.353 + ptr.Copy(aLabel);
1.354 + }
1.355 +
1.356 +void CAddCertificate::SetStoreToUse(const TDesC8& aStoreToUse)
1.357 + {
1.358 + TLex8 lex(aStoreToUse);
1.359 + lex.Val(iStoreIndex);
1.360 + }
1.361 +
1.362 +void CAddCertificate::SetCertificateContentL(const TDesC8& aFileName)
1.363 + {
1.364 + TFileName fileName;
1.365 + fileName.Copy(aFileName);
1.366 + RFs fs;
1.367 + User::LeaveIfError(fs.Connect());
1.368 + CleanupClosePushL(fs);
1.369 + __ASSERT_DEBUG(!iCertificateContent, User::Panic(_L("CAddCertificate"), 1));
1.370 + TRAPD(err, iCertificateContent = Input::ReadFileL(fileName, fs));
1.371 + if (err != KErrNone)
1.372 + {
1.373 + iConsole.Printf(_L("Error reading file : "));
1.374 + iConsole.Printf(fileName);
1.375 + iConsole.Printf(_L("\n"));
1.376 + User::Leave(err);
1.377 + }
1.378 + CleanupStack::PopAndDestroy(); // fs
1.379 + }
1.380 +
1.381 +void CAddCertificate::SetDeletable(const TDesC8& aDeletable)
1.382 + {
1.383 + iDeletableFlagPresent = ETrue;
1.384 + if (aDeletable.Compare(KTrue)==0)
1.385 + {
1.386 + iDeletable = ETrue;
1.387 + }
1.388 + else
1.389 + {
1.390 + iDeletable = EFalse;
1.391 + }
1.392 + }
1.393 +
1.394 +void CAddCertificate::ConstructCertL(const TDesC8& aCert)
1.395 + {
1.396 + TFileName filename;
1.397 + filename.Copy(aCert);
1.398 + RFs fs;
1.399 + User::LeaveIfError(fs.Connect());
1.400 + CleanupClosePushL(fs);
1.401 + HBufC8* certBuf = 0;
1.402 + TRAPD(err, certBuf = Input::ReadFileL(filename, fs));
1.403 + if (err != KErrNone)
1.404 + {
1.405 + iConsole.Printf(_L("Error reading file : "));
1.406 + iConsole.Printf(filename);
1.407 + iConsole.Printf(_L("\n"));
1.408 + User::Leave(err);
1.409 + }
1.410 + CleanupStack::PushL(certBuf);
1.411 + switch (iCertificateFormat)
1.412 + {
1.413 + case EX509Certificate:
1.414 + iCertificate = CX509Certificate::NewL(*certBuf);
1.415 + break;
1.416 +
1.417 + case EWTLSCertificate:
1.418 + iCertificate = CWTLSCertificate::NewL(*certBuf);
1.419 + break;
1.420 +
1.421 + default:
1.422 + // Unknown format - do nothing
1.423 + break;
1.424 + }
1.425 + CleanupStack::PopAndDestroy(2);
1.426 + }
1.427 +
1.428 +void CAddCertificate::DoCheckResult(TInt aError)
1.429 + {
1.430 +
1.431 + if (iFinished)
1.432 + {
1.433 + if (iResult )
1.434 + {
1.435 + if (iExpectedResult == KErrNone )
1.436 + {
1.437 + iConsole.Write(_L("\tcertificate added successfully\n"));
1.438 + iOut.writeString(_L("\tcertificate added successfully"));
1.439 + }
1.440 + else
1.441 + {
1.442 + iConsole.Write(_L("\tcertificate not added.\n"));
1.443 + iOut.writeString(_L("\tcertificate not added."));
1.444 + }
1.445 + iOut.writeNewLine();
1.446 + iOut.writeNewLine();
1.447 + }
1.448 + else if( !iResult)
1.449 + {
1.450 + if(iExpectedResult == KErrNone )
1.451 + {
1.452 + iConsole.Write(_L("\tcertificate not added\n"));
1.453 + iOut.writeString(_L("\tcertificate not added"));
1.454 + }
1.455 + else
1.456 + {
1.457 + iConsole.Write(_L("\tcertificate should not be added\n"));
1.458 + iOut.writeString(_L("\tcertificate should not be added"));
1.459 + }
1.460 + iOut.writeNewLine();
1.461 + iOut.writeString(_L("\t"));
1.462 + iOut.writeError(aError);
1.463 + if (aError == KErrBadName)
1.464 + {
1.465 + iOut.writeString(_L(" Check that the label is unique"));
1.466 + }
1.467 + iOut.writeNewLine();
1.468 + iOut.writeNewLine();
1.469 + }
1.470 + }
1.471 + }
1.472 +//////////////////////////////////////////////////////////
1.473 +// Key import, from keystore for adding user certificates
1.474 +//////////////////////////////////////////////////////////
1.475 +
1.476 +CTestAction* CImportKey::NewL(RFs& aFs,
1.477 + CConsoleBase& aConsole,
1.478 + Output& aOut,
1.479 + const TTestActionSpec& aTestActionSpec)
1.480 +{
1.481 + CTestAction* self = CImportKey::NewLC(aFs, aConsole, aOut, aTestActionSpec);
1.482 + CleanupStack::Pop(self);
1.483 + return self;
1.484 +}
1.485 +
1.486 +CTestAction* CImportKey::NewLC(RFs& aFs,
1.487 + CConsoleBase& aConsole,
1.488 + Output& aOut,
1.489 + const TTestActionSpec& aTestActionSpec)
1.490 +{
1.491 + CImportKey* self = new (ELeave) CImportKey(aFs, aConsole, aOut);
1.492 + CleanupStack::PushL(self);
1.493 + self->ConstructL(aTestActionSpec);
1.494 + return self;
1.495 +}
1.496 +
1.497 +CImportKey::~CImportKey()
1.498 +{
1.499 + delete iLabel;
1.500 + delete iKeyData;
1.501 + if (iKeyInfo)
1.502 + {
1.503 + iKeyInfo->Release();
1.504 + }
1.505 + delete iUnifiedKeyStore;
1.506 + iFs.Close();
1.507 +}
1.508 +
1.509 +CImportKey::CImportKey(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
1.510 +: CCertStoreTestAction(aFs, aConsole, aOut),
1.511 + iState(EInitialise)
1.512 +{}
1.513 +
1.514 +
1.515 +void CImportKey::ConstructL(const TTestActionSpec& aTestActionSpec)
1.516 +{
1.517 + User::LeaveIfError(iFs.Connect());
1.518 +
1.519 + CCertStoreTestAction::ConstructL(aTestActionSpec);
1.520 +
1.521 + TInt err = KErrNone;
1.522 + TInt pos = 0;
1.523 + SetKeyDataFileL(Input::ParseElement(aTestActionSpec.iActionBody, KImportDataFile, KImportDataFileEnd, pos, err));
1.524 + for (;SetKeyUsage(Input::ParseElement(aTestActionSpec.iActionBody, KKeyUsageStart, KKeyUsageEnd, pos, err));)
1.525 + ;
1.526 +
1.527 + SetKeyLabel(Input::ParseElement(aTestActionSpec.iActionBody, KKeyLabelStart, KKeyLabelEnd, pos, err));
1.528 + for (;SetKeyAccessType(Input::ParseElement(aTestActionSpec.iActionBody, KKeyAccessTypeStart, KKeyAccessTypeEnd, pos, err));)
1.529 + ;
1.530 +
1.531 + SetKeyPassphrase(Input::ParseElement(aTestActionSpec.iActionBody, KKeyPassphraseStart, KKeyPassphraseEnd, pos, err));
1.532 +
1.533 + pos = 0;
1.534 +
1.535 + HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
1.536 + TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
1.537 + Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
1.538 + CleanupStack::PopAndDestroy(result);
1.539 +}
1.540 +
1.541 +TBool CImportKey::SetKeyUsage(const TDesC8& aKeyUsage)
1.542 + {
1.543 + TBool ret = ETrue;
1.544 + if (aKeyUsage.Compare(KAllKeyUsages)==0)
1.545 + iUsage = EPKCS15UsageAll;
1.546 + else if (aKeyUsage.Compare(KAllKeyUsagesButNR)==0)
1.547 + iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
1.548 + (TKeyUsagePKCS15)(EPKCS15UsageSignRecover) |
1.549 + (TKeyUsagePKCS15)(EPKCS15UsageDecrypt);
1.550 + else if (aKeyUsage.Compare(KDSAUsage)==0)
1.551 + iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
1.552 + (TKeyUsagePKCS15)(EPKCS15UsageSignRecover);
1.553 + else if (aKeyUsage.Compare(KDerive)==0)
1.554 + iUsage |= EPKCS15UsageDerive;
1.555 + else if (aKeyUsage.Compare(KSign)==0)
1.556 + iUsage |= EPKCS15UsageSign;
1.557 + else if (aKeyUsage.Compare(KSignRecover)==0)
1.558 + iUsage |= EPKCS15UsageSignRecover;
1.559 + else if (aKeyUsage.Compare(KDecrypt)==0)
1.560 + iUsage |= EPKCS15UsageDecrypt;
1.561 + else if (aKeyUsage.Compare(KNR)==0)
1.562 + iUsage |= EPKCS15UsageNonRepudiation;
1.563 + else if (aKeyUsage.Compare(KEncipherAndSign)==0)
1.564 + iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
1.565 + (TKeyUsagePKCS15)(EPKCS15UsageSignRecover) |
1.566 + (TKeyUsagePKCS15)(EPKCS15UsageUnwrap);
1.567 + else
1.568 + ret = EFalse;
1.569 + return ret;
1.570 + }
1.571 +
1.572 +void CImportKey::SetKeyLabel(const TDesC8& aKeyLabel)
1.573 +{
1.574 + iLabel = HBufC::NewMax(aKeyLabel.Size());
1.575 + if (iLabel)
1.576 + {
1.577 + TPtr theLabel(iLabel->Des());
1.578 + theLabel.FillZ();
1.579 + theLabel.Copy(aKeyLabel);
1.580 + }
1.581 +}
1.582 +
1.583 +
1.584 +void CImportKey::SetKeyPassphrase(const TDesC8& aPassphrase)
1.585 + {
1.586 + // If the passphrase is empty, then use "clanger" by default.
1.587 + _LIT8(KDefaultPassphrase, "clanger");
1.588 + TPtrC8 phrase(KDefaultPassphrase());
1.589 + if (aPassphrase.Length())
1.590 + {
1.591 + phrase.Set(aPassphrase);
1.592 + }
1.593 +
1.594 +
1.595 + RFs fs;
1.596 + RFile file;
1.597 + fs.Connect();
1.598 +
1.599 + // Write the passphrase straight to the file.
1.600 + TDriveUnit sysDrive (fs.GetSystemDrive());
1.601 + TBuf<24> fileName (sysDrive.Name());
1.602 + fileName.Append(_L("\\password.txt"));
1.603 +
1.604 + file.Replace(fs, fileName, EFileWrite);
1.605 + file.Write(phrase);
1.606 + file.Close();
1.607 + fs.Close();
1.608 + }
1.609 +
1.610 +void CImportKey::SetKeyDataFileL(const TDesC8& aDes)
1.611 +{
1.612 +// Now the filename itself
1.613 + TFileName fileName;
1.614 + fileName.FillZ();
1.615 + fileName.Copy(aDes);
1.616 +
1.617 + TDriveUnit sysDrive (RFs::GetSystemDrive());
1.618 + TBuf<64> buf(sysDrive.Name());
1.619 + buf.Append(_L("\\tcertstore\\data\\"));
1.620 + buf.Append(fileName);
1.621 +
1.622 + RFile file;
1.623 + TInt r = file.Open(iFs, buf, EFileRead);
1.624 + if ( (r==KErrNotFound) || (r==KErrPathNotFound) )
1.625 + {// Not on c:, try z:
1.626 + buf[0] = 'z';
1.627 + r = file.Open(iFs, buf, EFileRead);
1.628 + }
1.629 +
1.630 + User::LeaveIfError(r);
1.631 +
1.632 + CleanupClosePushL(file);
1.633 +
1.634 + TInt fileSize = 0;
1.635 + User::LeaveIfError(file.Size(fileSize));
1.636 +
1.637 + if (fileSize > 0)
1.638 + {
1.639 + iKeyData = HBufC8::NewMaxL(fileSize);
1.640 + TPtr8 data(iKeyData->Des());
1.641 + data.FillZ();
1.642 + User::LeaveIfError(file.Read(data, fileSize));
1.643 + CleanupStack::Pop(1);
1.644 + }
1.645 +
1.646 + file.Close();
1.647 +}
1.648 +
1.649 +TBool CImportKey::SetKeyAccessType(const TDesC8& aKeyAccessType)
1.650 + {
1.651 + TBool ret = ETrue;
1.652 + if (aKeyAccessType.Compare(KExtractable)==0)
1.653 + {
1.654 + iAccessType |= CCTKeyInfo::EExtractable;
1.655 + }
1.656 + else if (aKeyAccessType.Compare(KSensitive)==0)
1.657 + {
1.658 + iAccessType |= CCTKeyInfo::ESensitive;
1.659 + }
1.660 + else if (aKeyAccessType.Compare(KAlwaysSensitive)==0)
1.661 + {
1.662 + iAccessType |= CCTKeyInfo::EAlwaysSensitive;
1.663 + }
1.664 + else if (aKeyAccessType.Compare(KNeverExtractable)==0)
1.665 + {
1.666 + iAccessType |= CCTKeyInfo::ENeverExtractable;
1.667 + }
1.668 + else if (aKeyAccessType.Compare(KLocal)==0)
1.669 + {
1.670 + iAccessType |= CCTKeyInfo::ELocal;
1.671 + }
1.672 + else
1.673 + ret = EFalse;
1.674 + return ret;
1.675 + }
1.676 +
1.677 +void CImportKey::PerformAction(TRequestStatus& aStatus)
1.678 +{
1.679 + TDriveUnit sysDrive (RFs::GetSystemDrive());
1.680 + switch (iState)
1.681 + {
1.682 + case EInitialise:
1.683 + {
1.684 + if (iKeyInfo != NULL)
1.685 + {
1.686 + iKeyInfo->Release();
1.687 + iKeyInfo = NULL;
1.688 + }
1.689 +
1.690 + // Delete t_secdlg files - this will then always answer "clanger" for the passphrase
1.691 +
1.692 + TInt result;
1.693 + TBuf<24> datFile(sysDrive.Name());
1.694 + datFile.Append(_L("\\t_secdlg_in.dat"));
1.695 + result = iFs.Delete(datFile);
1.696 +
1.697 + if (result != KErrNone && result != KErrNotFound)
1.698 + {
1.699 + TRequestStatus* status = &aStatus;
1.700 + User::RequestComplete(status, result);
1.701 + return;
1.702 + }
1.703 +
1.704 + datFile.Copy(sysDrive.Name());
1.705 + datFile.Append(_L("\\t_secdlg_out.dat"));
1.706 + result = iFs.Delete(datFile);
1.707 +
1.708 + if (result != KErrNone && result != KErrNotFound)
1.709 + {
1.710 + TRequestStatus* status = &aStatus;
1.711 + User::RequestComplete(status, result);
1.712 + return;
1.713 + }
1.714 +
1.715 + TRAP(result, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs));
1.716 + if ( (result==KErrNone) && (iUnifiedKeyStore) )
1.717 + {
1.718 + iUnifiedKeyStore->Initialize(aStatus);
1.719 + iState = EImportKey;
1.720 + }
1.721 + else
1.722 + {
1.723 + aStatus = result;
1.724 + iState = EFinished;
1.725 + }
1.726 + }
1.727 + break;
1.728 + case EImportKey:
1.729 + {
1.730 + if (KErrNone==aStatus.Int())
1.731 + {
1.732 + // Currently uses the first store, change to check the script for a specific store
1.733 + iUnifiedKeyStore->ImportKey(0, iKeyData->Des(), iUsage, *iLabel, iAccessType,
1.734 + TTime(0), TTime(0), iKeyInfo, aStatus);
1.735 + }
1.736 + else
1.737 + {
1.738 + // Errors get passed to next state
1.739 + TRequestStatus* status = &aStatus;
1.740 + User::RequestComplete(status, aStatus.Int());
1.741 + }
1.742 +
1.743 + iState = EFinished;
1.744 + }
1.745 +
1.746 + break;
1.747 +
1.748 + case EFinished:
1.749 + {
1.750 + TRequestStatus* status = &aStatus;
1.751 + User::RequestComplete(status, aStatus.Int());
1.752 + if ( (aStatus == iExpectedResult) || (aStatus==KErrAlreadyExists) )
1.753 + {
1.754 + iResult = ETrue;
1.755 + }
1.756 + else
1.757 + {
1.758 + iResult = EFalse;
1.759 + }
1.760 +
1.761 + iFinished = ETrue;
1.762 + }
1.763 + break;
1.764 + }
1.765 +}
1.766 +
1.767 +void CImportKey::PerformCancel()
1.768 +{// To do when test harness cancel comes back. Currently cancel testing
1.769 +// is performed in RunL with a set of flags and a separate active object
1.770 +}
1.771 +
1.772 +void CImportKey::Reset()
1.773 +{}
1.774 +
1.775 +void CImportKey::DoReportAction()
1.776 +{
1.777 + _LIT(KImporting, "Importing key from keystore...");
1.778 + iOut.writeString(KImporting);
1.779 + TPtr theLabel(iLabel->Des());
1.780 + iOut.writeString(theLabel);
1.781 + iOut.writeNewLine();
1.782 +}
1.783 +
1.784 +
1.785 +void CImportKey::DoCheckResult(TInt aError)
1.786 +{
1.787 +
1.788 + if (iFinished)
1.789 + {
1.790 + if (aError == KErrNone)
1.791 + {
1.792 + _LIT(KSuccessful, "Key imported successfully\n");
1.793 + iConsole.Write(KSuccessful);
1.794 + iOut.writeString(KSuccessful);
1.795 + iOut.writeNewLine();
1.796 + iOut.writeNewLine();
1.797 + }
1.798 + else
1.799 + {
1.800 + if ( (aError!=iExpectedResult) && (aError!=KErrAlreadyExists) )
1.801 + {
1.802 + _LIT(KFailed, "!!!Key import failure!!!\n");
1.803 + iConsole.Write(KFailed);
1.804 + iOut.writeString(KFailed);
1.805 + }
1.806 +
1.807 + iOut.writeNewLine();
1.808 + iOut.writeNewLine();
1.809 + }
1.810 + }
1.811 +}
1.812 +