os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool_import.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool_import.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,262 @@
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 <mctkeystore.h>
1.23 +#include <asn1dec.h>
1.24 +#include <asnpkcs.h>
1.25 +#include <unifiedkeystore.h>
1.26 +#include <securityerr.h>
1.27 +#include <mctwritablecertstore.h>
1.28 +
1.29 +#include "certtool_commands.h"
1.30 +#include "certtool_controller.h"
1.31 +#include "keytool_commands.h"
1.32 +
1.33 +
1.34 +const TInt KASN1SequenceTagValue = 0x30;
1.35 +const TInt KWTLSCertificateVersionValue = 0x01;
1.36 +
1.37 +/*static*/ CCertToolAdd* CCertToolAdd::NewLC(CCertToolController* aController)
1.38 + {
1.39 + CCertToolAdd* self = new (ELeave) CCertToolAdd(aController);
1.40 + CleanupStack::PushL(self);
1.41 + self->ConstructL();
1.42 + return self;
1.43 + }
1.44 +
1.45 +/*static*/ CCertToolAdd* CCertToolAdd::NewL(CCertToolController* aController)
1.46 + {
1.47 + CCertToolAdd* self = CCertToolAdd::NewLC(aController);
1.48 + CleanupStack::Pop(self);
1.49 + return self;
1.50 + }
1.51 +
1.52 +CCertToolAdd::~CCertToolAdd()
1.53 + {
1.54 + Cancel();
1.55 + delete iCertData;
1.56 + }
1.57 +
1.58 +void CCertToolAdd::ConstructL()
1.59 + {
1.60 + //The Certificate added is deletable by default.
1.61 + iIsDeletable = ETrue;
1.62 + }
1.63 +
1.64 +
1.65 +TCertificateFormat CCertToolAdd::DoRecognizeL(const TDesC8& iData)
1.66 + {
1.67 + // Ensure length is sufficient for checking type
1.68 + if ( iData.Size() >= 1 )
1.69 + {
1.70 + // First byte of X.509 certificate is an ANS.1 SEQUENCE tag
1.71 + if ( iData[0] == KASN1SequenceTagValue )
1.72 + {
1.73 + return EX509Certificate;
1.74 + }
1.75 + // First byte of WTLS certificate is version == 1
1.76 + else if ( iData[0] == KWTLSCertificateVersionValue )
1.77 + {
1.78 + return EWTLSCertificate;
1.79 + }
1.80 + }
1.81 + User::Leave(KErrEof);
1.82 + return EWTLSCertificate;
1.83 + }
1.84 +
1.85 +
1.86 +/**
1.87 + * Inserts a certificate in the certstore.
1.88 + * If a specific certstore implementation is given using
1.89 + * the -store command line parameter the certificate is inserted
1.90 + * there. If no implementation is specified the first one is used.
1.91 + * Initially we try to add the certificate as a *user* certificate
1.92 + * if this fails (no corresponding private key in the keystore)
1.93 + * the certificate is added a root (CA) certificate.
1.94 + * If the option -private was present then the private key is in the
1.95 + * keystore and the certificate will be a user certificate.
1.96 + */
1.97 +void CCertToolAdd::DoCommandL(CUnifiedCertStore& aCertStore, CKeyToolParameters* aParam)
1.98 + {
1.99 + Cancel();
1.100 + iParams = aParam;
1.101 + iCertStore = &aCertStore;
1.102 +
1.103 + if (!aParam->iDefault)
1.104 + {
1.105 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_CERTFILE);
1.106 + User::Leave(KErrNotFound);
1.107 + CActiveScheduler::Stop();
1.108 + }
1.109 +
1.110 + // must get proper certstore, hard-coded 0 is no good!
1.111 + if (aParam->iCertstoreIndex == -1)
1.112 + {
1.113 + aParam->iCertstoreIndex = 0;
1.114 + }
1.115 + if (aParam->iCertstoreIndex >= iCertStore->WritableCertStoreCount())
1.116 + {
1.117 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST);
1.118 + User::Leave(KErrCorrupt);
1.119 + }
1.120 + MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(aParam->iCertstoreIndex);
1.121 +
1.122 + if (!iParams->iLabel)
1.123 + {
1.124 + iParams->iLabel = (iParams->iDefault)->AllocL();
1.125 + }
1.126 +
1.127 + // Get the certificate binary!
1.128 + RFs fs;
1.129 + CleanupClosePushL(fs);
1.130 + User::LeaveIfError(fs.Connect());
1.131 +
1.132 + RFile file;
1.133 + CleanupClosePushL(file);
1.134 + TInt r = file.Open(fs, *(iParams->iDefault), EFileRead);
1.135 + if (r != KErrNone)
1.136 + {
1.137 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_OPENFAIL);
1.138 + User::Leave(r);
1.139 + }
1.140 +
1.141 + TInt fileSize = 0;
1.142 + file.Size(fileSize);
1.143 +
1.144 + if (fileSize <= 0)
1.145 + {
1.146 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_EMPTYFILE);
1.147 + User::Leave(KErrCorrupt);
1.148 + }
1.149 +
1.150 + iCertData = HBufC8::NewMaxL(fileSize);
1.151 + TPtr8 data(iCertData->Des());
1.152 + data.FillZ();
1.153 + User::LeaveIfError(file.Read(data, fileSize));
1.154 +
1.155 + // Use the recognizer to see what kind of certificate we have!
1.156 + TCertificateFormat format = DoRecognizeL(*iCertData);
1.157 +
1.158 + iState = EIntermediate;
1.159 +
1.160 + /**
1.161 + * If the iIsDeletable variable of iParams is set, parse its value
1.162 + * and set the iIsDeletable flag appropriately.
1.163 + */
1.164 + if(iParams->iIsDeletable)
1.165 + {
1.166 + HBufC* lowerCaseString = HBufC::NewLC(iParams->iIsDeletable->Length());
1.167 + TPtr ptr(lowerCaseString->Des());
1.168 +
1.169 + //Convert to lower case.
1.170 + ptr.CopyLC(*iParams->iIsDeletable);
1.171 +
1.172 + if(ptr.CompareF(_L("n")) == 0 || ptr.CompareF(_L("no")) == 0 )
1.173 + {
1.174 + iIsDeletable = EFalse;
1.175 + }
1.176 + else if (ptr.CompareF(_L("y")) != 0 && ptr.CompareF(_L("yes")) != 0)
1.177 + {
1.178 + //Wrong value passed.
1.179 + User::Leave(KErrArgument);
1.180 + }
1.181 +
1.182 + CleanupStack::PopAndDestroy(lowerCaseString);
1.183 + }
1.184 +
1.185 + SetActive();
1.186 + //wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iStatus);
1.187 + wCertStore.Add(*iParams->iLabel, format, EUserCertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus);
1.188 + CleanupStack::PopAndDestroy(2, &fs);
1.189 + }
1.190 +
1.191 +
1.192 +void CCertToolAdd::RunL()
1.193 + {
1.194 + if ((iStatus.Int() != KErrNone) && (iStatus.Int() != KErrPrivateKeyNotFound))
1.195 + {
1.196 + // A problem occured. Handle gracefully.
1.197 + User::Leave(iStatus.Int());
1.198 + }
1.199 + switch (iState)
1.200 + {
1.201 + case EIntermediate :
1.202 + {
1.203 + if (iStatus.Int() == KErrPrivateKeyNotFound)
1.204 + {
1.205 + // Not an user certificate add as root!
1.206 + iState = EFinish;
1.207 + MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(iParams->iCertstoreIndex);
1.208 + TCertificateFormat format = DoRecognizeL(*iCertData);
1.209 + SetActive();
1.210 + wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus);
1.211 + }
1.212 + else
1.213 + {
1.214 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);
1.215 + CActiveScheduler::Stop();
1.216 + }
1.217 + }
1.218 + break;
1.219 + case EFinish :
1.220 + {
1.221 + iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);
1.222 + CActiveScheduler::Stop();
1.223 + }
1.224 + break;
1.225 + default :
1.226 + {
1.227 + }
1.228 + }
1.229 +
1.230 + }
1.231 +
1.232 +TInt CCertToolAdd::RunError(TInt aError)
1.233 + {
1.234 + CActiveScheduler::Stop();
1.235 + switch (aError)
1.236 + {
1.237 + case KErrBadName:
1.238 + {
1.239 + // Most likely it was there already
1.240 + TRAP_IGNORE(iController->DisplayErrorL(_L("The given label is invalid, or already present in the certstore."), iParams->iPageWise));
1.241 + }
1.242 + break;
1.243 + case KErrKeyNotWeakEnough:
1.244 + {
1.245 + TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_WEAK, iStatus.Int()));
1.246 + }
1.247 + break;
1.248 + default:
1.249 + {
1.250 + TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORT, iStatus.Int()));
1.251 + }
1.252 + }
1.253 + return KErrNone;
1.254 + }
1.255 +
1.256 +void CCertToolAdd::DoCancel()
1.257 + {
1.258 +
1.259 + }
1.260 +
1.261 +CCertToolAdd::CCertToolAdd(CCertToolController* aController) : CCertToolCommand(aController)
1.262 + {
1.263 + CActiveScheduler::Add(this);
1.264 + }
1.265 +