os/security/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool_import.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <mctkeystore.h>
    20 #include <asn1dec.h>
    21 #include <asnpkcs.h>
    22 #include <unifiedkeystore.h>
    23 #include <securityerr.h>
    24 #include <mctwritablecertstore.h>
    25 
    26 #include "certtool_commands.h"
    27 #include "certtool_controller.h"
    28 #include "keytool_commands.h"
    29 
    30 
    31 const TInt KASN1SequenceTagValue = 0x30;
    32 const TInt KWTLSCertificateVersionValue = 0x01;
    33 
    34 /*static*/ CCertToolAdd* CCertToolAdd::NewLC(CCertToolController* aController)
    35 	{
    36 	CCertToolAdd* self = new (ELeave) CCertToolAdd(aController);
    37 	CleanupStack::PushL(self);
    38 	self->ConstructL();
    39 	return self;	
    40 	}
    41 
    42 /*static*/ CCertToolAdd* CCertToolAdd::NewL(CCertToolController* aController)
    43 	{
    44 	CCertToolAdd* self = CCertToolAdd::NewLC(aController);
    45 	CleanupStack::Pop(self);
    46 	return self;	
    47 	}
    48 
    49 CCertToolAdd::~CCertToolAdd()
    50 	{
    51 	Cancel();	
    52 	delete iCertData;	
    53 	}
    54 
    55 void CCertToolAdd::ConstructL()
    56 	{
    57 	//The Certificate added is deletable by default.
    58 	iIsDeletable = ETrue;
    59 	}
    60 
    61 
    62 TCertificateFormat CCertToolAdd::DoRecognizeL(const TDesC8& iData)
    63 	{
    64 	// Ensure length is sufficient for checking type
    65 	if ( iData.Size() >= 1 )
    66 		{
    67 		// First byte of X.509 certificate is an ANS.1 SEQUENCE tag
    68 		if ( iData[0] == KASN1SequenceTagValue )
    69 			{
    70 			return EX509Certificate;
    71 			}
    72 		// First byte of WTLS certificate is version == 1
    73 		else if ( iData[0] == KWTLSCertificateVersionValue )
    74 			{
    75 			return EWTLSCertificate;
    76 			}
    77 		}
    78 	User::Leave(KErrEof);
    79 	return EWTLSCertificate;
    80 	}
    81 
    82 
    83 /**
    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.
    93  */
    94 void CCertToolAdd::DoCommandL(CUnifiedCertStore& aCertStore, CKeyToolParameters* aParam)
    95 	{
    96 	Cancel();	
    97 	iParams = aParam;	
    98 	iCertStore = &aCertStore;
    99 
   100 	if (!aParam->iDefault)
   101 		{
   102 		iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_CERTFILE);			
   103 		User::Leave(KErrNotFound);
   104 		CActiveScheduler::Stop();		
   105 		}
   106 
   107 		// must get proper certstore, hard-coded 0 is no good!
   108 		if (aParam->iCertstoreIndex == -1)
   109 			{
   110 			aParam->iCertstoreIndex = 0;
   111 			}
   112 		if (aParam->iCertstoreIndex >= iCertStore->WritableCertStoreCount())
   113 			{
   114 			iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_NOTEXIST);	
   115 			User::Leave(KErrCorrupt);
   116 			}
   117 		MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(aParam->iCertstoreIndex);
   118 
   119 		if (!iParams->iLabel)
   120 			{
   121 			iParams->iLabel	= (iParams->iDefault)->AllocL();
   122 			}
   123 			
   124 		// Get the certificate binary!
   125 		RFs fs;
   126 		CleanupClosePushL(fs);
   127 		User::LeaveIfError(fs.Connect());	
   128 	
   129 		RFile file;
   130 		CleanupClosePushL(file);	
   131 		TInt r = file.Open(fs, *(iParams->iDefault), EFileRead);
   132 		if (r != KErrNone)
   133 			{
   134 			iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_OPENFAIL);				
   135 			User::Leave(r);
   136 			}
   137 	        
   138 		TInt fileSize = 0;
   139 		file.Size(fileSize);
   140 
   141 		if (fileSize <= 0)
   142 		{
   143 			iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_EMPTYFILE);	
   144 			User::Leave(KErrCorrupt);
   145 		}
   146 
   147 		iCertData = HBufC8::NewMaxL(fileSize);
   148 		TPtr8 data(iCertData->Des());
   149 		data.FillZ();
   150 		User::LeaveIfError(file.Read(data, fileSize));
   151 
   152 		// Use the recognizer to see what kind of certificate we have!
   153 		TCertificateFormat format = DoRecognizeL(*iCertData);
   154 						
   155 		iState = EIntermediate;
   156 		
   157 		/**
   158 		 * If the iIsDeletable variable of iParams is set, parse its value
   159 		 * and set the iIsDeletable flag appropriately.
   160 		 */
   161 		if(iParams->iIsDeletable)
   162 		    {
   163 		    HBufC* lowerCaseString = HBufC::NewLC(iParams->iIsDeletable->Length());
   164 		    TPtr ptr(lowerCaseString->Des());
   165 		    
   166 		    //Convert to lower case.
   167 		    ptr.CopyLC(*iParams->iIsDeletable);
   168 		    
   169 		    if(ptr.CompareF(_L("n")) == 0 || ptr.CompareF(_L("no")) == 0 )
   170 		        {
   171 		        iIsDeletable = EFalse;
   172 		        }
   173             else if (ptr.CompareF(_L("y")) != 0  && ptr.CompareF(_L("yes")) != 0)
   174                 {
   175                 //Wrong value passed.
   176                 User::Leave(KErrArgument);     
   177                 }
   178 
   179 		    CleanupStack::PopAndDestroy(lowerCaseString);            
   180 		    }
   181 
   182 		SetActive();
   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);
   186 	}
   187 
   188 
   189 void CCertToolAdd::RunL()
   190 	{	
   191 	if ((iStatus.Int() != KErrNone) && (iStatus.Int() != KErrPrivateKeyNotFound))
   192 		{
   193 		// A problem occured. Handle gracefully.
   194 		User::Leave(iStatus.Int());
   195 		}
   196 	switch (iState)
   197 		{
   198 		case EIntermediate :
   199 			{
   200 			if (iStatus.Int() == KErrPrivateKeyNotFound)
   201 				{
   202 				// Not an user certificate add as root!
   203 				iState = EFinish;
   204 				MCTWritableCertStore& wCertStore = iCertStore->WritableCertStore(iParams->iCertstoreIndex);
   205 				TCertificateFormat format = DoRecognizeL(*iCertData);
   206 				SetActive();				
   207 				wCertStore.Add(*iParams->iLabel, format, ECACertificate, NULL, NULL, *iCertData, iIsDeletable, iStatus);				
   208 				}
   209 			else 
   210 				{
   211 				iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);			
   212 				CActiveScheduler::Stop();				
   213 				}
   214 			}
   215 			break;
   216 		case EFinish : 
   217 			{
   218 			iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORTSUCCESS);
   219 			CActiveScheduler::Stop();			
   220 			}	
   221 			break;
   222 		default :
   223 			{
   224 			}
   225 		}
   226 	
   227 	}
   228 	
   229 TInt CCertToolAdd::RunError(TInt aError)
   230 	{
   231 	CActiveScheduler::Stop();
   232 	switch (aError)
   233 		{
   234 		case KErrBadName:
   235 			{
   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));
   238 			}
   239 			break;
   240 		case KErrKeyNotWeakEnough:
   241 			{
   242 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_WEAK, iStatus.Int()));			
   243 			}
   244 			break;			
   245 		default:
   246 			{
   247 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_CERTTOOL_ERR_IMPORT, iStatus.Int()));			
   248 			}
   249 		}	
   250 	return KErrNone;
   251 	}
   252 	
   253 void CCertToolAdd::DoCancel()
   254 	{
   255 	
   256 	}
   257 	
   258 CCertToolAdd::CCertToolAdd(CCertToolController* aController) : CCertToolCommand(aController)
   259 	{
   260 	CActiveScheduler::Add(this);	
   261 	}
   262