os/security/cryptoservices/certificateandkeymgmt/x509/X509CertChain.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) 1998-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 <x509certchain.h>
    20 
    21 EXPORT_C TValidationStatus::TValidationStatus(const TValidationError aError, const TInt aCert)
    22 	:iReason(aError), iCert(aCert)
    23 	{
    24 	}
    25 
    26 //x509 cert chain
    27 /**
    28 * If the certificate has decoded the members from TeletexString then the return value 
    29 * may be incorrect because TeletexString type is not fully supported by this library.
    30 * Instead the decode methods perform a direct conversion from 8 to 16bits by adding 
    31 * null characters in the second byte of each character. This will work as expected 
    32 * for cases where the string contains ASCII data.
    33 */
    34 EXPORT_C CArrayPtrFlat<CX509Certificate>* CX509CertChain::DecodeCertsL(const TDesC8& aBinaryData)
    35 	{
    36 	CArrayPtrFlat<CX509Certificate>* temp = new(ELeave) CArrayPtrFlat<CX509Certificate> (1);
    37 	TCleanupItem cleanupCerts(CleanupCertArray, temp);
    38 	CleanupStack::PushL(cleanupCerts);
    39 	TInt pos = 0;//start at the start
    40 	while (pos < aBinaryData.Length())
    41 		{
    42 		CX509Certificate* cert = CX509Certificate::NewLC(aBinaryData, pos);
    43 		temp->AppendL(cert);
    44 		CleanupStack::Pop();
    45 		}
    46 	CleanupStack::Pop();//temp
    47 	return temp;
    48 	}
    49 
    50 EXPORT_C CX509CertChain::~CX509CertChain()
    51 	{
    52 	if (iChain)
    53 		{
    54 		iChain->ResetAndDestroy();
    55 		delete iChain;
    56 		}
    57 	}
    58 
    59 EXPORT_C TInt CX509CertChain::Count() const
    60 	{
    61 	return iChain->Count();
    62 	}
    63 
    64 EXPORT_C const CX509Certificate& CX509CertChain::Cert(TInt aIndex) const
    65 	{
    66 	return *(iChain->At(aIndex));
    67 	}
    68 
    69 
    70 EXPORT_C TBool CX509CertChain::IsEqualL(const CX509CertChain& aOther) const
    71 	{
    72 	TInt num1 = Count();
    73 	TInt num2 = aOther.Count();
    74 	if (num1 != num2)
    75 		{
    76 		return EFalse;
    77 		}
    78 	
    79 	for (TInt i = 0; i < num1; ++i)
    80 		{
    81 		const CX509Certificate& cert1 = Cert(i);
    82 		const CX509Certificate& cert2 = aOther.Cert(i);
    83 		
    84 		if (!cert1.IsEqualL(cert2))
    85 			{
    86 			return EFalse;
    87 			}
    88 		}
    89 	
    90 	return ETrue;
    91 	}
    92 
    93 
    94 void CX509CertChain::CleanupCertArray(TAny* aArray)
    95 	{
    96 	CArrayPtrFlat<CX509Certificate>* array = REINTERPRET_CAST(CArrayPtrFlat<CX509Certificate>*, aArray);
    97 	array->ResetAndDestroy();
    98 	delete array;
    99 	}
   100 
   101 // x509 certificate warning
   102 EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::NewL(TInt aIndex)
   103 	{
   104 	CCertificateValidationWarnings* self = CCertificateValidationWarnings::NewLC(aIndex);
   105 	CleanupStack::Pop(self);
   106 	return self;
   107 	}
   108 
   109 EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::NewLC(TInt aIndex)
   110 	{
   111 	CCertificateValidationWarnings* self = new(ELeave) CCertificateValidationWarnings(aIndex);
   112 	CleanupStack::PushL(self);
   113 	return self;
   114 	}
   115 
   116 CCertificateValidationWarnings::CCertificateValidationWarnings(TInt aIndex)
   117 	: iCertIndex(aIndex)
   118 	{
   119 	}
   120 
   121 EXPORT_C CCertificateValidationWarnings::~CCertificateValidationWarnings()
   122 	{
   123 	iWarnings.Reset();
   124 	iCriticalExtsFound.ResetAndDestroy();
   125 	}
   126 
   127 EXPORT_C const RPointerArray<TDesC>& CCertificateValidationWarnings::CriticalExtensionsFound() const
   128 	{
   129 	return iCriticalExtsFound;
   130 	}
   131 
   132 EXPORT_C const RArray<TValidationStatus>& CCertificateValidationWarnings::Warnings() const
   133 	{
   134 	return iWarnings;
   135 	}
   136 
   137 EXPORT_C TInt CCertificateValidationWarnings::CertIndex() const
   138 	{
   139 	return iCertIndex;
   140 	}
   141 
   142 EXPORT_C CCertificateValidationWarnings* CCertificateValidationWarnings::InternalizeL(RReadStream& aStream)
   143 	{
   144 	// iCertIndex
   145 	TInt certIndex = aStream.ReadInt32L();
   146 			
   147 	// create a cert warning using the cert index and ref cert
   148 	CCertificateValidationWarnings* certWarning = CCertificateValidationWarnings::NewLC(certIndex);
   149 	
   150 	// iWarnings
   151 	TInt32 count = aStream.ReadInt32L();
   152 	for (TInt x=0; x<count; ++x)
   153 		{
   154 		TValidationStatus warning(EValidatedOK,0);
   155 		TPckg<TValidationStatus> pckg(warning);
   156 		aStream.ReadL(pckg);
   157 		certWarning->AppendWarningL(warning);	
   158 		}
   159 	
   160 	// iCriticalExtsFound
   161 	count = aStream.ReadInt32L();
   162 	for (TInt x=0; x<count; ++x)
   163 		{
   164 		TInt length = aStream.ReadInt32L();
   165 		HBufC* oid = HBufC::NewLC(length);
   166 		TPtr oidPtr = oid->Des();
   167 		aStream.ReadL(oidPtr, length);
   168 		certWarning->AppendCriticalExtensionWarningL(*oid);	
   169 		CleanupStack::Pop(oid);
   170 		}	
   171 
   172 	CleanupStack::Pop(certWarning);
   173 	return certWarning;
   174 	}
   175 
   176 EXPORT_C void CCertificateValidationWarnings::ExternalizeL(RWriteStream& aStream) const
   177 	{
   178 	// iCertIndex;
   179 	aStream.WriteInt32L(iCertIndex);
   180 	
   181 	// iWarnings;
   182 	TInt x;
   183 	aStream.WriteInt32L(iWarnings.Count());
   184 	for (x=0; x<iWarnings.Count(); ++x)
   185 		{
   186 		aStream.WriteL(TPckgC<TValidationStatus>(iWarnings[x]));
   187 		}	
   188 	
   189 	// iCriticalExtsFound;
   190 	aStream.WriteInt32L(iCriticalExtsFound.Count());
   191 	for (x=0; x<iCriticalExtsFound.Count(); ++x)
   192 		{
   193 		aStream.WriteInt32L((*iCriticalExtsFound[x]).Length());
   194 		aStream.WriteL(*iCriticalExtsFound[x]);
   195 		}
   196 	}
   197 
   198 EXPORT_C void CCertificateValidationWarnings::AppendWarningL(TValidationStatus aWarning)
   199 	{
   200 	iWarnings.AppendL(aWarning);
   201 	}
   202 
   203 EXPORT_C void CCertificateValidationWarnings::AppendCriticalExtensionWarningL(TDesC& aCriticalExt)
   204 	{
   205 	iCriticalExtsFound.AppendL(&aCriticalExt);
   206 	}