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