sl@0: /* sl@0: * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "certtool_commands.h" sl@0: #include "certtool_controller.h" sl@0: #include "keytool_commands.h" sl@0: sl@0: sl@0: const TInt KASN1SequenceTagValue = 0x30; sl@0: const TInt KWTLSCertificateVersionValue = 0x01; sl@0: sl@0: /*static*/ CCertToolAdd* CCertToolAdd::NewLC(CCertToolController* aController) sl@0: { sl@0: CCertToolAdd* self = new (ELeave) CCertToolAdd(aController); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: /*static*/ CCertToolAdd* CCertToolAdd::NewL(CCertToolController* aController) sl@0: { sl@0: CCertToolAdd* self = CCertToolAdd::NewLC(aController); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CCertToolAdd::~CCertToolAdd() sl@0: { sl@0: Cancel(); sl@0: delete iCertData; sl@0: } sl@0: sl@0: void CCertToolAdd::ConstructL() sl@0: { sl@0: //The Certificate added is deletable by default. sl@0: iIsDeletable = ETrue; sl@0: } sl@0: sl@0: sl@0: TCertificateFormat CCertToolAdd::DoRecognizeL(const TDesC8& iData) sl@0: { sl@0: // Ensure length is sufficient for checking type sl@0: if ( iData.Size() >= 1 ) sl@0: { sl@0: // First byte of X.509 certificate is an ANS.1 SEQUENCE tag sl@0: if ( iData[0] == KASN1SequenceTagValue ) sl@0: { sl@0: return EX509Certificate; sl@0: } sl@0: // First byte of WTLS certificate is version == 1 sl@0: else if ( iData[0] == KWTLSCertificateVersionValue ) sl@0: { sl@0: return EWTLSCertificate; sl@0: } sl@0: } sl@0: User::Leave(KErrEof); sl@0: return EWTLSCertificate; sl@0: } sl@0: sl@0: sl@0: /** sl@0: * Inserts a certificate in the certstore. sl@0: * If a specific certstore implementation is given using sl@0: * the -store command line parameter the certificate is inserted sl@0: * there. If no implementation is specified the first one is used. sl@0: * Initially we try to add the certificate as a *user* certificate sl@0: * if this fails (no corresponding private key in the keystore) sl@0: * the certificate is added a root (CA) certificate. sl@0: * If the option -private was present then the private key is in the sl@0: * keystore and the certificate will be a user certificate. sl@0: */ sl@0: void CCertToolAdd::DoCommandL(CUnifiedCertStore& aCertStore, CKeyToolParameters* aParam) sl@0: { sl@0: Cancel(); sl@0: iParams = aParam; sl@0: iCertStore = &aCertStore; sl@0: sl@0: if (!aParam->iDefault) sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_CERTFILE); sl@0: User::Leave(KErrNotFound); sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: sl@0: // must get proper certstore, hard-coded 0 is no good! sl@0: if (aParam->iCertstoreIndex == -1) sl@0: { sl@0: aParam->iCertstoreIndex = 0; sl@0: } sl@0: if (aParam->iCertstoreIndex >= iCertStore->WritableCertStoreCount()) sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST); sl@0: User::Leave(KErrCorrupt); sl@0: } sl@0: MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(aParam->iCertstoreIndex); sl@0: sl@0: if (!iParams->iLabel) sl@0: { sl@0: iParams->iLabel = (iParams->iDefault)->AllocL(); sl@0: } sl@0: sl@0: // Get the certificate binary! sl@0: RFs fs; sl@0: CleanupClosePushL(fs); sl@0: User::LeaveIfError(fs.Connect()); sl@0: sl@0: RFile file; sl@0: CleanupClosePushL(file); sl@0: TInt r = file.Open(fs, *(iParams->iDefault), EFileRead); sl@0: if (r != KErrNone) sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_OPENFAIL); sl@0: User::Leave(r); sl@0: } sl@0: sl@0: TInt fileSize = 0; sl@0: file.Size(fileSize); sl@0: sl@0: if (fileSize <= 0) sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_EMPTYFILE); sl@0: User::Leave(KErrCorrupt); sl@0: } sl@0: sl@0: iCertData = HBufC8::NewMaxL(fileSize); sl@0: TPtr8 data(iCertData->Des()); sl@0: data.FillZ(); sl@0: User::LeaveIfError(file.Read(data, fileSize)); sl@0: sl@0: // Use the recognizer to see what kind of certificate we have! sl@0: TCertificateFormat format = DoRecognizeL(*iCertData); sl@0: sl@0: iState = EIntermediate; sl@0: sl@0: /** sl@0: * If the iIsDeletable variable of iParams is set, parse its value sl@0: * and set the iIsDeletable flag appropriately. sl@0: */ sl@0: if(iParams->iIsDeletable) sl@0: { sl@0: HBufC* lowerCaseString = HBufC::NewLC(iParams->iIsDeletable->Length()); sl@0: TPtr ptr(lowerCaseString->Des()); sl@0: sl@0: //Convert to lower case. sl@0: ptr.CopyLC(*iParams->iIsDeletable); sl@0: sl@0: if(ptr.CompareF(_L("n")) == 0 || ptr.CompareF(_L("no")) == 0 ) sl@0: { sl@0: iIsDeletable = EFalse; sl@0: } sl@0: else if (ptr.CompareF(_L("y")) != 0 && ptr.CompareF(_L("yes")) != 0) sl@0: { sl@0: //Wrong value passed. sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(lowerCaseString); sl@0: } sl@0: sl@0: SetActive(); sl@0: //wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iStatus); sl@0: wCertStore.Add(*iParams->iLabel, format, EUserCertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus); sl@0: CleanupStack::PopAndDestroy(2, &fs); sl@0: } sl@0: sl@0: sl@0: void CCertToolAdd::RunL() sl@0: { sl@0: if ((iStatus.Int() != KErrNone) && (iStatus.Int() != KErrPrivateKeyNotFound)) sl@0: { sl@0: // A problem occured. Handle gracefully. sl@0: User::Leave(iStatus.Int()); sl@0: } sl@0: switch (iState) sl@0: { sl@0: case EIntermediate : sl@0: { sl@0: if (iStatus.Int() == KErrPrivateKeyNotFound) sl@0: { sl@0: // Not an user certificate add as root! sl@0: iState = EFinish; sl@0: MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(iParams->iCertstoreIndex); sl@0: TCertificateFormat format = DoRecognizeL(*iCertData); sl@0: SetActive(); sl@0: wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus); sl@0: } sl@0: else sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS); sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: } sl@0: break; sl@0: case EFinish : sl@0: { sl@0: iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS); sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: break; sl@0: default : sl@0: { sl@0: } sl@0: } sl@0: sl@0: } sl@0: sl@0: TInt CCertToolAdd::RunError(TInt aError) sl@0: { sl@0: CActiveScheduler::Stop(); sl@0: switch (aError) sl@0: { sl@0: case KErrBadName: sl@0: { sl@0: // Most likely it was there already sl@0: TRAP_IGNORE(iController->DisplayErrorL(_L("The given label is invalid, or already present in the certstore."), iParams->iPageWise)); sl@0: } sl@0: break; sl@0: case KErrKeyNotWeakEnough: sl@0: { sl@0: TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_WEAK, iStatus.Int())); sl@0: } sl@0: break; sl@0: default: sl@0: { sl@0: TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORT, iStatus.Int())); sl@0: } sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CCertToolAdd::DoCancel() sl@0: { sl@0: sl@0: } sl@0: sl@0: CCertToolAdd::CCertToolAdd(CCertToolController* aController) : CCertToolCommand(aController) sl@0: { sl@0: CActiveScheduler::Add(this); sl@0: } sl@0: