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