sl@0: /*
sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: #include <x509certchain.h>
sl@0: 
sl@0: EXPORT_C TValidationStatus::TValidationStatus(const TValidationError aError, const TInt aCert)
sl@0: 	:iReason(aError), iCert(aCert)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: //x509 cert chain
sl@0: /**
sl@0: * If the certificate has decoded the members from TeletexString then the return value 
sl@0: * may be incorrect because TeletexString type is not fully supported by this library.
sl@0: * Instead the decode methods perform a direct conversion from 8 to 16bits by adding 
sl@0: * null characters in the second byte of each character. This will work as expected 
sl@0: * for cases where the string contains ASCII data.
sl@0: */
sl@0: EXPORT_C CArrayPtrFlat<CX509Certificate>* CX509CertChain::DecodeCertsL(const TDesC8& aBinaryData)
sl@0: 	{
sl@0: 	CArrayPtrFlat<CX509Certificate>* temp = new(ELeave) CArrayPtrFlat<CX509Certificate> (1);
sl@0: 	TCleanupItem cleanupCerts(CleanupCertArray, temp);
sl@0: 	CleanupStack::PushL(cleanupCerts);
sl@0: 	TInt pos = 0;//start at the start
sl@0: 	while (pos < aBinaryData.Length())
sl@0: 		{
sl@0: 		CX509Certificate* cert = CX509Certificate::NewLC(aBinaryData, pos);
sl@0: 		temp->AppendL(cert);
sl@0: 		CleanupStack::Pop();
sl@0: 		}
sl@0: 	CleanupStack::Pop();//temp
sl@0: 	return temp;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CX509CertChain::~CX509CertChain()
sl@0: 	{
sl@0: 	if (iChain)
sl@0: 		{
sl@0: 		iChain->ResetAndDestroy();
sl@0: 		delete iChain;
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: EXPORT_C TInt CX509CertChain::Count() const
sl@0: 	{
sl@0: 	return iChain->Count();
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const CX509Certificate& CX509CertChain::Cert(TInt aIndex) const
sl@0: 	{
sl@0: 	return *(iChain->At(aIndex));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: EXPORT_C TBool CX509CertChain::IsEqualL(const CX509CertChain& aOther) const
sl@0: 	{
sl@0: 	TInt num1 = Count();
sl@0: 	TInt num2 = aOther.Count();
sl@0: 	if (num1 != num2)
sl@0: 		{
sl@0: 		return EFalse;
sl@0: 		}
sl@0: 	
sl@0: 	for (TInt i = 0; i < num1; ++i)
sl@0: 		{
sl@0: 		const CX509Certificate& cert1 = Cert(i);
sl@0: 		const CX509Certificate& cert2 = aOther.Cert(i);
sl@0: 		
sl@0: 		if (!cert1.IsEqualL(cert2))
sl@0: 			{
sl@0: 			return EFalse;
sl@0: 			}
sl@0: 		}
sl@0: 	
sl@0: 	return ETrue;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CX509CertChain::CleanupCertArray(TAny* aArray)
sl@0: 	{
sl@0: 	CArrayPtrFlat<CX509Certificate>* array = REINTERPRET_CAST(CArrayPtrFlat<CX509Certificate>*, aArray);
sl@0: 	array->ResetAndDestroy();
sl@0: 	delete array;
sl@0: 	}
sl@0: 
sl@0: // x509 certificate warning
sl@0: EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::NewL(TInt aIndex)
sl@0: 	{
sl@0: 	CCertificateValidationWarnings* self = CCertificateValidationWarnings::NewLC(aIndex);
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::NewLC(TInt aIndex)
sl@0: 	{
sl@0: 	CCertificateValidationWarnings* self = new(ELeave) CCertificateValidationWarnings(aIndex);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CCertificateValidationWarnings::CCertificateValidationWarnings(TInt aIndex)
sl@0: 	: iCertIndex(aIndex)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CCertificateValidationWarnings::~CCertificateValidationWarnings()
sl@0: 	{
sl@0: 	iWarnings.Reset();
sl@0: 	iCriticalExtsFound.ResetAndDestroy();
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const RPointerArray<TDesC>& CCertificateValidationWarnings::CriticalExtensionsFound() const
sl@0: 	{
sl@0: 	return iCriticalExtsFound;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const RArray<TValidationStatus>& CCertificateValidationWarnings::Warnings() const
sl@0: 	{
sl@0: 	return iWarnings;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C TInt CCertificateValidationWarnings::CertIndex() const
sl@0: 	{
sl@0: 	return iCertIndex;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::InternalizeL(RReadStream& aStream)
sl@0: 	{
sl@0: 	// iCertIndex
sl@0: 	TInt certIndex = aStream.ReadInt32L();
sl@0: 			
sl@0: 	// create a cert warning using the cert index and ref cert
sl@0: 	CCertificateValidationWarnings* certWarning = CCertificateValidationWarnings::NewLC(certIndex);
sl@0: 	
sl@0: 	// iWarnings
sl@0: 	TInt32 count = aStream.ReadInt32L();
sl@0: 	for (TInt x=0; x<count; ++x)
sl@0: 		{
sl@0: 		TValidationStatus warning(EValidatedOK,0);
sl@0: 		TPckg<TValidationStatus> pckg(warning);
sl@0: 		aStream.ReadL(pckg);
sl@0: 		certWarning->AppendWarningL(warning);	
sl@0: 		}
sl@0: 	
sl@0: 	// iCriticalExtsFound
sl@0: 	count = aStream.ReadInt32L();
sl@0: 	for (TInt x=0; x<count; ++x)
sl@0: 		{
sl@0: 		TInt length = aStream.ReadInt32L();
sl@0: 		HBufC* oid = HBufC::NewLC(length);
sl@0: 		TPtr oidPtr = oid->Des();
sl@0: 		aStream.ReadL(oidPtr, length);
sl@0: 		certWarning->AppendCriticalExtensionWarningL(*oid);	
sl@0: 		CleanupStack::Pop(oid);
sl@0: 		}	
sl@0: 
sl@0: 	CleanupStack::Pop(certWarning);
sl@0: 	return certWarning;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CCertificateValidationWarnings::ExternalizeL(RWriteStream& aStream) const
sl@0: 	{
sl@0: 	// iCertIndex;
sl@0: 	aStream.WriteInt32L(iCertIndex);
sl@0: 	
sl@0: 	// iWarnings;
sl@0: 	TInt x;
sl@0: 	aStream.WriteInt32L(iWarnings.Count());
sl@0: 	for (x=0; x<iWarnings.Count(); ++x)
sl@0: 		{
sl@0: 		aStream.WriteL(TPckgC<TValidationStatus>(iWarnings[x]));
sl@0: 		}	
sl@0: 	
sl@0: 	// iCriticalExtsFound;
sl@0: 	aStream.WriteInt32L(iCriticalExtsFound.Count());
sl@0: 	for (x=0; x<iCriticalExtsFound.Count(); ++x)
sl@0: 		{
sl@0: 		aStream.WriteInt32L((*iCriticalExtsFound[x]).Length());
sl@0: 		aStream.WriteL(*iCriticalExtsFound[x]);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CCertificateValidationWarnings::AppendWarningL(TValidationStatus aWarning)
sl@0: 	{
sl@0: 	iWarnings.AppendL(aWarning);
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void CCertificateValidationWarnings::AppendCriticalExtensionWarningL(TDesC& aCriticalExt)
sl@0: 	{
sl@0: 	iCriticalExtsFound.AppendL(&aCriticalExt);
sl@0: 	}