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