os/security/cryptoservices/certificateandkeymgmt/x500/x500dn.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 <x500dn.h>
    20 #include <x520ava.h>
    21 #include "X500dec.h"
    22 #include <asn1enc.h>
    23 
    24 
    25 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
    26 	{
    27 	CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aElements);
    28 	CleanupStack::Pop();//self
    29 	return self;
    30 	}
    31 
    32 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
    33 	{
    34 	CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
    35 	CleanupStack::PushL(self);
    36 	self->ConstructL(aElements);
    37 	return self;
    38 	}
    39 
    40 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CX500DistinguishedName& aName)
    41 	{
    42 	CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aName);
    43 	CleanupStack::Pop();//self
    44 	return self;
    45 	}
    46 
    47 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CX500DistinguishedName& aName)
    48 	{
    49 	CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
    50 	CleanupStack::PushL(self);
    51 	self->ConstructL(static_cast<CArrayPtr<CX520AttributeTypeAndValue> &>(*(aName.iElements)));
    52 	return self;
    53 	}	
    54 
    55 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData)
    56 	{
    57 	TInt pos = 0;
    58 	return CX500DistinguishedName::NewL(aBinaryData, pos);
    59 	}
    60 
    61 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData)
    62 	{
    63 	TInt pos = 0;
    64 	return CX500DistinguishedName::NewLC(aBinaryData, pos);
    65 	}
    66 
    67 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData, TInt& aPos)
    68 	{
    69 	CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aBinaryData, aPos);
    70 	CleanupStack::Pop();
    71 	return self;
    72 	}
    73 
    74 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData, TInt& aPos)
    75 	{
    76 	CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
    77 	CleanupStack::PushL(self);
    78 	self->ConstructL(aBinaryData, aPos);
    79 	return self;
    80 	}
    81 
    82 
    83 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(RReadStream& aStream)
    84 	{
    85 	CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aStream);
    86 	CleanupStack::Pop();//self
    87 	return self;
    88 	}
    89 
    90 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(RReadStream& aStream)
    91 	{
    92 	CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
    93 	CleanupStack::PushL(self);
    94 	self->ConstructL(aStream);
    95 	return self;
    96 	}
    97 
    98 CX500DistinguishedName::CX500DistinguishedName()
    99 	{
   100 	}
   101 
   102 void CX500DistinguishedName::ConstructL(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
   103 	{
   104 	iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(1);
   105 	TInt count = aElements.Count();
   106 	for (TInt i=0;i < count; i++)
   107 		{
   108 		CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(*(aElements[i]));
   109 		iElements->AppendL(ava);
   110 		CleanupStack::Pop();//ava
   111 		}
   112 	}
   113 
   114 void CX500DistinguishedName::ConstructL(RReadStream& aStream)
   115 	{
   116 	iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(1);
   117 	InternalizeL(aStream);
   118 	}
   119 
   120 void CX500DistinguishedName::ConstructL(const TDesC8& aBinaryData, TInt& aPos)
   121 	{
   122 	TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
   123 	dec.InitL();
   124 	TInt end = aPos + dec.LengthDER();
   125 	aPos += dec.LengthDERHeader();
   126 
   127 	iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue> (1);
   128 	if (dec.Tag() != EASN1Sequence)
   129 		{
   130 		User::Leave(KErrArgument);
   131 		}
   132 	while (aPos < end)
   133 		{
   134 		TASN1DecGeneric rdn(aBinaryData.Right(aBinaryData.Length() - aPos));
   135 		rdn.InitL();
   136 		if (rdn.Tag() != EASN1Set)
   137 			{
   138 			User::Leave(KErrArgument);
   139 			}
   140 		TInt rdnEnd = rdn.LengthDER();
   141 		TInt rdnPos = rdn.LengthDERHeader();//add on header
   142 		while (rdnPos < rdnEnd)
   143 			{
   144 			CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(rdn.Encoding(), rdnPos);
   145 			iElements->AppendL(ava);
   146 			CleanupStack::Pop();//ava
   147 			}
   148 		aPos += rdnEnd;
   149 		}
   150 	if (aPos != end)
   151 		{
   152 		User::Leave(KErrArgument);
   153 		}
   154 	}
   155 
   156 
   157 EXPORT_C CX500DistinguishedName::~CX500DistinguishedName()
   158 	{
   159 	if (iElements != NULL)
   160 		{
   161 		iElements->ResetAndDestroy();
   162 		delete iElements;
   163 		}
   164 	}
   165 
   166 EXPORT_C TBool CX500DistinguishedName::ExactMatchL(const CX500DistinguishedName& aName) const
   167 	{
   168 	if (iElements->Count()!=aName.Count())
   169 		return EFalse;
   170 	return IsWithinSubtreeL(aName);
   171 	}
   172 
   173 EXPORT_C HBufC* CX500DistinguishedName::ExtractFieldL(const TDesC& aFieldName) const
   174 	{
   175 	TInt count = Count();
   176 	for (TInt i = 0; i < count; i++)
   177 		{
   178 		const CX520AttributeTypeAndValue& ava = Element(i);
   179 		if (ava.Type() == aFieldName)
   180 			{
   181 			return ava.ValueL();
   182 			}
   183 		}
   184 	return NULL;
   185 	}
   186 
   187 EXPORT_C HBufC* CX500DistinguishedName::DisplayNameL() const
   188 	{
   189 	HBufC* res = ExtractFieldL(KX520CommonName);
   190 	if (res == NULL)
   191 		{
   192 		res = ExtractFieldL(KX520OrganizationName);
   193 		if (res == NULL)
   194 			{
   195 			res = HBufC::NewL(0);
   196 			*res = KNullDesC;
   197 			}
   198 		}
   199 	return res;		
   200 	}
   201 
   202 EXPORT_C TBool CX500DistinguishedName::IsWithinSubtreeL(const CX500DistinguishedName& aName) const
   203 	{
   204 	TInt elementsCount = aName.Count();
   205 	for (TInt i = 0; i < elementsCount; ++i)
   206 		{
   207 		if (!MatchElementL(*(aName.iElements->At(i))))
   208 			return EFalse;	
   209 		}
   210 	return ETrue;
   211 	}
   212 
   213 EXPORT_C TBool CX500DistinguishedName::MatchElementL(const CX520AttributeTypeAndValue& aElement) const
   214 	{
   215 	TInt count = iElements->Count();
   216 	for (TInt i = 0; i < count; i++)
   217 		{
   218 		if (aElement.ExactMatchL(*iElements->At(i)))
   219 			return ETrue;
   220 		}
   221 	return EFalse;
   222 	}
   223 
   224 EXPORT_C TInt CX500DistinguishedName::Count() const
   225 	{
   226 	return iElements->Count();
   227 	}
   228 
   229 EXPORT_C const CX520AttributeTypeAndValue& CX500DistinguishedName::Element(TInt aIndex) const
   230 	{
   231 	return *(iElements->At(aIndex));
   232 	}
   233 
   234 EXPORT_C void CX500DistinguishedName::ExternalizeL(RWriteStream& aStream) const
   235 	{
   236 	TInt count = iElements->Count();
   237 	aStream.WriteInt32L(count);
   238 	for (TInt i = 0; i < count; i++)
   239 		{
   240 		CX520AttributeTypeAndValue* ava = iElements->At(i);
   241 		ava->ExternalizeL(aStream);
   242 		}
   243 	}
   244 
   245 void CX500DistinguishedName::InternalizeL(RReadStream& aStream)
   246 	{
   247 	TInt count = aStream.ReadInt32L();
   248 	for (TInt i = 0; i < count; i++)
   249 		{
   250 		CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(aStream);
   251 		iElements->AppendL(ava);
   252 		CleanupStack::Pop();
   253 		}
   254 	}
   255 
   256 EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1LC() const
   257 	{
   258 	CASN1EncSequence* nameSeq = CASN1EncSequence::NewLC();
   259 	TInt numEntries = Count();
   260 	for(TInt i = 0; i < numEntries; i++)
   261 		{
   262 		// Each attribute pair has to be enclosed in a SET-OF 
   263 		// and SEQUENCE-OF
   264 		CASN1EncSequence* set = CASN1EncSequence::NewLC();
   265 		// This conversion of a sequence into a set presumably 
   266 		// works if the set is going to contain just one child 
   267 		// item, because otherwise order of child items in a 
   268 		// SET-OF becomes important.
   269 		set->SetTag(EASN1Set, EUniversal);
   270 		CASN1EncSequence* attr = Element(i).EncodeASN1LC();
   271 		set->AddAndPopChildL(attr);
   272 		nameSeq->AddAndPopChildL(set);
   273 		}
   274 	return nameSeq;
   275 	}
   276 
   277 EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1L() const
   278 	{
   279 	CASN1EncSequence* nameSeq = EncodeASN1LC();
   280 	CleanupStack::Pop(nameSeq);
   281 	return nameSeq;
   282 	}