os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool_import.cpp
Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include <mctkeystore.h>
22 #include <unifiedkeystore.h>
23 #include <securityerr.h>
24 #include <mctwritablecertstore.h>
26 #include "certtool_commands.h"
27 #include "certtool_controller.h"
28 #include "keytool_commands.h"
31 const TInt KASN1SequenceTagValue = 0x30;
32 const TInt KWTLSCertificateVersionValue = 0x01;
34 /*static*/ CCertToolAdd* CCertToolAdd::NewLC(CCertToolController* aController)
36 CCertToolAdd* self = new (ELeave) CCertToolAdd(aController);
37 CleanupStack::PushL(self);
42 /*static*/ CCertToolAdd* CCertToolAdd::NewL(CCertToolController* aController)
44 CCertToolAdd* self = CCertToolAdd::NewLC(aController);
45 CleanupStack::Pop(self);
49 CCertToolAdd::~CCertToolAdd()
55 void CCertToolAdd::ConstructL()
57 //The Certificate added is deletable by default.
62 TCertificateFormat CCertToolAdd::DoRecognizeL(const TDesC8& iData)
64 // Ensure length is sufficient for checking type
65 if ( iData.Size() >= 1 )
67 // First byte of X.509 certificate is an ANS.1 SEQUENCE tag
68 if ( iData[0] == KASN1SequenceTagValue )
70 return EX509Certificate;
72 // First byte of WTLS certificate is version == 1
73 else if ( iData[0] == KWTLSCertificateVersionValue )
75 return EWTLSCertificate;
79 return EWTLSCertificate;
84 * Inserts a certificate in the certstore.
85 * If a specific certstore implementation is given using
86 * the -store command line parameter the certificate is inserted
87 * there. If no implementation is specified the first one is used.
88 * Initially we try to add the certificate as a *user* certificate
89 * if this fails (no corresponding private key in the keystore)
90 * the certificate is added a root (CA) certificate.
91 * If the option -private was present then the private key is in the
92 * keystore and the certificate will be a user certificate.
94 void CCertToolAdd::DoCommandL(CUnifiedCertStore& aCertStore, CKeyToolParameters* aParam)
98 iCertStore = &aCertStore;
100 if (!aParam->iDefault)
102 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_CERTFILE);
103 User::Leave(KErrNotFound);
104 CActiveScheduler::Stop();
107 // must get proper certstore, hard-coded 0 is no good!
108 if (aParam->iCertstoreIndex == -1)
110 aParam->iCertstoreIndex = 0;
112 if (aParam->iCertstoreIndex >= iCertStore->WritableCertStoreCount())
114 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST);
115 User::Leave(KErrCorrupt);
117 MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(aParam->iCertstoreIndex);
119 if (!iParams->iLabel)
121 iParams->iLabel = (iParams->iDefault)->AllocL();
124 // Get the certificate binary!
126 CleanupClosePushL(fs);
127 User::LeaveIfError(fs.Connect());
130 CleanupClosePushL(file);
131 TInt r = file.Open(fs, *(iParams->iDefault), EFileRead);
134 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_OPENFAIL);
143 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_EMPTYFILE);
144 User::Leave(KErrCorrupt);
147 iCertData = HBufC8::NewMaxL(fileSize);
148 TPtr8 data(iCertData->Des());
150 User::LeaveIfError(file.Read(data, fileSize));
152 // Use the recognizer to see what kind of certificate we have!
153 TCertificateFormat format = DoRecognizeL(*iCertData);
155 iState = EIntermediate;
158 * If the iIsDeletable variable of iParams is set, parse its value
159 * and set the iIsDeletable flag appropriately.
161 if(iParams->iIsDeletable)
163 HBufC* lowerCaseString = HBufC::NewLC(iParams->iIsDeletable->Length());
164 TPtr ptr(lowerCaseString->Des());
166 //Convert to lower case.
167 ptr.CopyLC(*iParams->iIsDeletable);
169 if(ptr.CompareF(_L("n")) == 0 || ptr.CompareF(_L("no")) == 0 )
171 iIsDeletable = EFalse;
173 else if (ptr.CompareF(_L("y")) != 0 && ptr.CompareF(_L("yes")) != 0)
175 //Wrong value passed.
176 User::Leave(KErrArgument);
179 CleanupStack::PopAndDestroy(lowerCaseString);
183 //wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iStatus);
184 wCertStore.Add(*iParams->iLabel, format, EUserCertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus);
185 CleanupStack::PopAndDestroy(2, &fs);
189 void CCertToolAdd::RunL()
191 if ((iStatus.Int() != KErrNone) && (iStatus.Int() != KErrPrivateKeyNotFound))
193 // A problem occured. Handle gracefully.
194 User::Leave(iStatus.Int());
200 if (iStatus.Int() == KErrPrivateKeyNotFound)
202 // Not an user certificate add as root!
204 MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(iParams->iCertstoreIndex);
205 TCertificateFormat format = DoRecognizeL(*iCertData);
207 wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus);
211 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);
212 CActiveScheduler::Stop();
218 iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);
219 CActiveScheduler::Stop();
229 TInt CCertToolAdd::RunError(TInt aError)
231 CActiveScheduler::Stop();
236 // Most likely it was there already
237 TRAP_IGNORE(iController->DisplayErrorL(_L("The given label is invalid, or already present in the certstore."), iParams->iPageWise));
240 case KErrKeyNotWeakEnough:
242 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_WEAK, iStatus.Int()));
247 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORT, iStatus.Int()));
253 void CCertToolAdd::DoCancel()
258 CCertToolAdd::CCertToolAdd(CCertToolController* aController) : CCertToolCommand(aController)
260 CActiveScheduler::Add(this);