os/security/cryptoservices/certificateandkeymgmt/pkcs12recog/pkcs12recog.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/pkcs12recog/pkcs12recog.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,306 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-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 "pkcs12recog.h"
    1.23 +
    1.24 +#include <apmrec.h>
    1.25 +#include <apmstd.h>
    1.26 +#include <ecom/ecom.h>
    1.27 +#include <ecom/implementationproxy.h>
    1.28 +#include <f32file.h>
    1.29 +
    1.30 +/** UID for PKCS#12 mime-type recognizer */
    1.31 +static const TUint KPkcs12RecognizerImplementationId = 0x20001520;
    1.32 +static const TUid KUidMimePkcs12Recognizer = { KPkcs12RecognizerImplementationId };
    1.33 +
    1.34 +/** 
    1.35 +Minimum buffer size assuming worst-case 3 byte lengths for variable
    1.36 +length fields. The file will never be smaller that this because it must
    1.37 +also contain either the MacData or be a signed object.
    1.38 +
    1.39 +SEQ 5 bytes
    1.40 +INTEGER 3 bytes
    1.41 +SEQ 5 bytes
    1.42 +OID 11 bytes
    1.43 +
    1.44 +total = 24
    1.45 +*/
    1.46 +static const TInt KPkcs12MinBufSize = 24;
    1.47 +
    1.48 +/** Mime-type for PKCS#12 */
    1.49 +_LIT8(KDataTypePkcs12, "application/x-pkcs12");
    1.50 +/** The number of mime-types recognised */
    1.51 +static const TInt KSupportedDataTypesTotal = 1;
    1.52 +
    1.53 +// ASN.1 / DER constants
    1.54 +/** DER encoding of an ASN.1 sequence tag */
    1.55 +static const TUint8 KDerSequenceOctet = 0x30;
    1.56 +/** DER encoding of an ASN.1 integer tag */
    1.57 +static const TUint8 KDerIntegerOctet = 0x02;
    1.58 +/** Expected PKCS#12 version number */
    1.59 +static const TInt KPkcs12Version = 3;
    1.60 +
    1.61 +/**
    1.62 +DER encoding of PKCS#7 data content type OID
    1.63 +OID = 1.2.840.113549.1.7.1
    1.64 +*/		
    1.65 +static const TUint8 KPkcs7DataOid[] = 
    1.66 +	{
    1.67 +	0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
    1.68 +	};	
    1.69 +/** Length of encoded PKCS#7 data OID */	
    1.70 +static const TUint KPkcs7DataOidLen = sizeof(KPkcs7DataOid);		
    1.71 +
    1.72 +/**
    1.73 +DER encoding of PKCS#7 signed data content type OID
    1.74 +OID = 1.2.840.113549.1.7.2
    1.75 +*/	
    1.76 +static const TUint8 KPkcs7SignedDataOid[] = 
    1.77 +	{
    1.78 +	0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02
    1.79 +	};	
    1.80 +/** Length of encoded PKCS#7 signed data OID */	
    1.81 +static const TUint KPkcs7SignedDataOidLen = sizeof(KPkcs7SignedDataOid);	
    1.82 +
    1.83 +/** PKCS#12 recognizer panic identifier */
    1.84 +_LIT(KPkcs12RecogPanic, "PKCS12RECOG");	
    1.85 +	
    1.86 +CPkcs12Recognizer::CPkcs12Recognizer()
    1.87 +	: CApaDataRecognizerType(KUidMimePkcs12Recognizer, CApaDataRecognizerType::ENormal)
    1.88 +	{
    1.89 +	iCountDataTypes = KSupportedDataTypesTotal;
    1.90 +	}
    1.91 +
    1.92 +TUint CPkcs12Recognizer::PreferredBufSize()
    1.93 +	{
    1.94 +	return KPkcs12MinBufSize;
    1.95 +	}
    1.96 +
    1.97 +TDataType CPkcs12Recognizer::SupportedDataTypeL(TInt aIndex) const
    1.98 +	{
    1.99 +	__ASSERT_DEBUG(aIndex >= 0 && aIndex < KSupportedDataTypesTotal,
   1.100 +					Panic(EPanicInvalidDataType));
   1.101 +	
   1.102 +	if (aIndex < 0 || aIndex >= KSupportedDataTypesTotal)
   1.103 +		{
   1.104 +		User::Leave(KErrArgument);
   1.105 +		}
   1.106 +						
   1.107 +	return TDataType(KDataTypePkcs12);
   1.108 +	}
   1.109 +
   1.110 +void CPkcs12Recognizer::DoRecognizeL(const TDesC& aName, const TDesC8& aBuffer)
   1.111 +	{
   1.112 +	__UHEAP_MARK;
   1.113 +	iConfidence = ENotRecognized;
   1.114 +	
   1.115 +	TPtrC8 pkcs12Buffer(aBuffer);
   1.116 +	TBuf8<KPkcs12MinBufSize> fileBuffer;
   1.117 +	
   1.118 +	if (aBuffer.Length() < KPkcs12MinBufSize)
   1.119 +		{										
   1.120 +		if (RFile* fh = FilePassedByHandleL()) 
   1.121 +			{
   1.122 +			User::LeaveIfError(fh->Read(0, fileBuffer, KPkcs12MinBufSize));
   1.123 +			}
   1.124 +		else 
   1.125 +			{
   1.126 +			// no file handle so attempt to read the file. This may
   1.127 +			// fail if the file is in a private directory
   1.128 +			RFs fs;
   1.129 +			User::LeaveIfError(fs.Connect());
   1.130 +			CleanupClosePushL(fs);
   1.131 +			RFile file;			
   1.132 +			TInt err = file.Open(fs, aName, EFileRead | EFileShareAny | EFileStream);
   1.133 +			
   1.134 +			// do nothing if the file fails to open, for any reason
   1.135 +			if (err == KErrNone)
   1.136 +				{
   1.137 +				CleanupClosePushL(file);
   1.138 +				User::LeaveIfError(file.Read(0, fileBuffer, KPkcs12MinBufSize));
   1.139 +				CleanupStack::PopAndDestroy(&file);
   1.140 +				}
   1.141 +			CleanupStack::PopAndDestroy(&fs);
   1.142 +			}		
   1.143 +		// buffer does not exist or is too small so attempt to 
   1.144 +		// read a buffer from the file		
   1.145 +		pkcs12Buffer.Set(fileBuffer);			
   1.146 +		}
   1.147 +		
   1.148 +
   1.149 +	if (pkcs12Buffer.Length() > 0 && DoRecognizeBufferL(pkcs12Buffer))
   1.150 +		{
   1.151 +		iDataType = TDataType(KDataTypePkcs12);
   1.152 +		iConfidence = EProbable;
   1.153 +		if (HasPkcs12Extension(aName)) 
   1.154 +			{
   1.155 +			iConfidence = ECertain;
   1.156 +			}
   1.157 +		}
   1.158 +	__UHEAP_MARKEND;
   1.159 + 	}
   1.160 +	
   1.161 +TBool CPkcs12Recognizer::HasPkcs12Extension(const TDesC& aName)
   1.162 +	{
   1.163 +	_LIT(KPfxExt, ".pfx");
   1.164 +	_LIT(KP12Ext, ".p12");
   1.165 +	
   1.166 +	TBool match = EFalse;
   1.167 +	
   1.168 +	// It is not possible to use a TParse/TParsePtr here because the filename
   1.169 +	// supplied when a file-handle is passed is of the form ::filename.ext and
   1.170 +	// is not recognised as a valid filename
   1.171 +	if (aName.Length() > 4)
   1.172 +		{		
   1.173 +		const TPtrC ext = aName.Right(4);
   1.174 +		if (ext.CompareF(KPfxExt) == 0 || ext.CompareF(KP12Ext) == 0)
   1.175 +			{
   1.176 +			match = ETrue;
   1.177 +			}
   1.178 +		}
   1.179 +	return match;
   1.180 +	}
   1.181 +
   1.182 +TBool CPkcs12Recognizer::DoRecognizeBufferL(const TDesC8& aBuffer)
   1.183 + 	{
   1.184 + 	TBool isPkcs12 = EFalse;
   1.185 + 	TUint offset = 0;
   1.186 + 	
   1.187 + 	if (aBuffer.Length() >= KPkcs12MinBufSize)
   1.188 + 		{
   1.189 +		// PFX
   1.190 +	 	if (ConsumeSequenceL(aBuffer, offset))
   1.191 + 			{
   1.192 + 			// Version
   1.193 + 			TInt version; 
   1.194 +			if (ConsumeIntegerL(aBuffer, offset, version)) 
   1.195 +				{
   1.196 +				if (version == KPkcs12Version)
   1.197 +					{
   1.198 +					// authSafe
   1.199 +					if (ConsumeSequenceL(aBuffer, offset))
   1.200 +						{
   1.201 +						const TPtrC8 dataOid(KPkcs7DataOid, KPkcs7DataOidLen);
   1.202 +						const TPtrC8 signedDataOid(KPkcs7SignedDataOid, KPkcs7SignedDataOidLen);
   1.203 +						// check whether content-type is data or signed data					
   1.204 +						if ((aBuffer.Mid(offset, KPkcs7DataOidLen).Compare(dataOid) == 0) ||
   1.205 +							(aBuffer.Mid(offset, KPkcs7SignedDataOidLen).Compare(signedDataOid) == 0))
   1.206 +							{
   1.207 +							isPkcs12 = ETrue;
   1.208 +							}
   1.209 +						}
   1.210 +					}
   1.211 +				}
   1.212 + 			} 		
   1.213 + 		}
   1.214 + 	return isPkcs12;
   1.215 + 	}
   1.216 +
   1.217 +TBool CPkcs12Recognizer::ConsumeSequenceL(const TDesC8& aBuffer, TUint& aOffset) const
   1.218 +	{
   1.219 +	TBool isSequence = EFalse;
   1.220 +	if (aBuffer[aOffset] == KDerSequenceOctet)
   1.221 +		{
   1.222 +		// sequence tag found, skip tag and length
   1.223 +		aOffset++;
   1.224 +		TInt length;
   1.225 +		isSequence = ConsumeLengthL(aBuffer, aOffset, length);
   1.226 +		}
   1.227 +	return isSequence;
   1.228 +	}
   1.229 +
   1.230 +TBool CPkcs12Recognizer::ConsumeLengthL(const TDesC8& aBuffer, TUint& aOffset, TInt& aLengthOctets) const
   1.231 +	{	
   1.232 +	TBool isValidLength = EFalse;	
   1.233 +	TUint8 lengthOctet = aBuffer[aOffset];
   1.234 +	if (lengthOctet & 0x80)
   1.235 +		{
   1.236 +		// The top bit is set so assume the length encoding is in long form
   1.237 +		TUint numOctets = lengthOctet & 0x7f;
   1.238 +		aOffset++;
   1.239 +		
   1.240 +		if (ConsumeBase256L(aBuffer, aOffset, numOctets, aLengthOctets))
   1.241 +			{			
   1.242 +			if (aLengthOctets >= 0)
   1.243 +				{
   1.244 +				// lengths must not be -ve
   1.245 +				isValidLength = ETrue;
   1.246 +				}
   1.247 +			}
   1.248 +		}
   1.249 +	else 
   1.250 +		{
   1.251 +		// Top bit zero so assume short form encoding i.e. one octet
   1.252 +		aLengthOctets = lengthOctet & 0x7f;
   1.253 +		aOffset++;
   1.254 +		isValidLength = ETrue;
   1.255 +		}
   1.256 +	return isValidLength;
   1.257 +	}
   1.258 +	
   1.259 +TBool CPkcs12Recognizer::ConsumeIntegerL(const TDesC8& aBuffer, TUint& aOffset, TInt& aIntVal) const
   1.260 +	{
   1.261 +	TBool isValidInteger = EFalse;
   1.262 +	if (aBuffer[aOffset] == KDerIntegerOctet) 
   1.263 +		{
   1.264 +		aOffset++;
   1.265 +		TInt length;
   1.266 +		if (ConsumeLengthL(aBuffer, aOffset, length))
   1.267 +			{			
   1.268 +			isValidInteger = ConsumeBase256L(aBuffer, aOffset, length, aIntVal);
   1.269 +			}
   1.270 +		}
   1.271 +	return isValidInteger;
   1.272 +	}
   1.273 +
   1.274 +TBool CPkcs12Recognizer::ConsumeBase256L(const TDesC8& aBuffer, TUint& aOffset, TInt aLengthOctets, TInt& aIntVal) const
   1.275 +	{	
   1.276 +	TInt isValid = EFalse;
   1.277 +	if (aLengthOctets <= 4) 
   1.278 +		{
   1.279 +		aIntVal = 0;
   1.280 +		while (aLengthOctets-- > 0)
   1.281 +			{
   1.282 +			aIntVal <<= 8;
   1.283 +			aIntVal |= aBuffer[aOffset++];
   1.284 +			}
   1.285 +		isValid = ETrue;
   1.286 +		}
   1.287 +	return isValid;	
   1.288 +	}	
   1.289 +
   1.290 +CApaDataRecognizerType* CPkcs12Recognizer::CreateRecognizerL()
   1.291 +	{
   1.292 +	return new (ELeave) CPkcs12Recognizer();
   1.293 +	}
   1.294 +
   1.295 +static const TImplementationProxy ImplementationTable[] = 
   1.296 +	{
   1.297 +		IMPLEMENTATION_PROXY_ENTRY(KPkcs12RecognizerImplementationId, CPkcs12Recognizer::CreateRecognizerL)
   1.298 +	};
   1.299 +
   1.300 +EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
   1.301 +	{
   1.302 +	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
   1.303 +	return ImplementationTable;
   1.304 +	}	
   1.305 +
   1.306 +void CPkcs12Recognizer::Panic(TPkcs12RecogPanic aReason) const
   1.307 +	{
   1.308 +	User::Panic(KPkcs12RecogPanic, aReason);
   1.309 +	}