First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
25 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
27 CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aElements);
28 CleanupStack::Pop();//self
32 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
34 CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
35 CleanupStack::PushL(self);
36 self->ConstructL(aElements);
40 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CX500DistinguishedName& aName)
42 CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aName);
43 CleanupStack::Pop();//self
47 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CX500DistinguishedName& aName)
49 CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
50 CleanupStack::PushL(self);
51 self->ConstructL(static_cast<CArrayPtr<CX520AttributeTypeAndValue> &>(*(aName.iElements)));
55 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData)
58 return CX500DistinguishedName::NewL(aBinaryData, pos);
61 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData)
64 return CX500DistinguishedName::NewLC(aBinaryData, pos);
67 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData, TInt& aPos)
69 CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aBinaryData, aPos);
74 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData, TInt& aPos)
76 CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
77 CleanupStack::PushL(self);
78 self->ConstructL(aBinaryData, aPos);
83 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(RReadStream& aStream)
85 CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aStream);
86 CleanupStack::Pop();//self
90 EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(RReadStream& aStream)
92 CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName;
93 CleanupStack::PushL(self);
94 self->ConstructL(aStream);
98 CX500DistinguishedName::CX500DistinguishedName()
102 void CX500DistinguishedName::ConstructL(const CArrayPtr<CX520AttributeTypeAndValue>& aElements)
104 iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(1);
105 TInt count = aElements.Count();
106 for (TInt i=0;i < count; i++)
108 CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(*(aElements[i]));
109 iElements->AppendL(ava);
110 CleanupStack::Pop();//ava
114 void CX500DistinguishedName::ConstructL(RReadStream& aStream)
116 iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(1);
117 InternalizeL(aStream);
120 void CX500DistinguishedName::ConstructL(const TDesC8& aBinaryData, TInt& aPos)
122 TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos));
124 TInt end = aPos + dec.LengthDER();
125 aPos += dec.LengthDERHeader();
127 iElements = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue> (1);
128 if (dec.Tag() != EASN1Sequence)
130 User::Leave(KErrArgument);
134 TASN1DecGeneric rdn(aBinaryData.Right(aBinaryData.Length() - aPos));
136 if (rdn.Tag() != EASN1Set)
138 User::Leave(KErrArgument);
140 TInt rdnEnd = rdn.LengthDER();
141 TInt rdnPos = rdn.LengthDERHeader();//add on header
142 while (rdnPos < rdnEnd)
144 CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(rdn.Encoding(), rdnPos);
145 iElements->AppendL(ava);
146 CleanupStack::Pop();//ava
152 User::Leave(KErrArgument);
157 EXPORT_C CX500DistinguishedName::~CX500DistinguishedName()
159 if (iElements != NULL)
161 iElements->ResetAndDestroy();
166 EXPORT_C TBool CX500DistinguishedName::ExactMatchL(const CX500DistinguishedName& aName) const
168 if (iElements->Count()!=aName.Count())
170 return IsWithinSubtreeL(aName);
173 EXPORT_C HBufC* CX500DistinguishedName::ExtractFieldL(const TDesC& aFieldName) const
175 TInt count = Count();
176 for (TInt i = 0; i < count; i++)
178 const CX520AttributeTypeAndValue& ava = Element(i);
179 if (ava.Type() == aFieldName)
187 EXPORT_C HBufC* CX500DistinguishedName::DisplayNameL() const
189 HBufC* res = ExtractFieldL(KX520CommonName);
192 res = ExtractFieldL(KX520OrganizationName);
195 res = HBufC::NewL(0);
202 EXPORT_C TBool CX500DistinguishedName::IsWithinSubtreeL(const CX500DistinguishedName& aName) const
204 TInt elementsCount = aName.Count();
205 for (TInt i = 0; i < elementsCount; ++i)
207 if (!MatchElementL(*(aName.iElements->At(i))))
213 EXPORT_C TBool CX500DistinguishedName::MatchElementL(const CX520AttributeTypeAndValue& aElement) const
215 TInt count = iElements->Count();
216 for (TInt i = 0; i < count; i++)
218 if (aElement.ExactMatchL(*iElements->At(i)))
224 EXPORT_C TInt CX500DistinguishedName::Count() const
226 return iElements->Count();
229 EXPORT_C const CX520AttributeTypeAndValue& CX500DistinguishedName::Element(TInt aIndex) const
231 return *(iElements->At(aIndex));
234 EXPORT_C void CX500DistinguishedName::ExternalizeL(RWriteStream& aStream) const
236 TInt count = iElements->Count();
237 aStream.WriteInt32L(count);
238 for (TInt i = 0; i < count; i++)
240 CX520AttributeTypeAndValue* ava = iElements->At(i);
241 ava->ExternalizeL(aStream);
245 void CX500DistinguishedName::InternalizeL(RReadStream& aStream)
247 TInt count = aStream.ReadInt32L();
248 for (TInt i = 0; i < count; i++)
250 CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(aStream);
251 iElements->AppendL(ava);
256 EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1LC() const
258 CASN1EncSequence* nameSeq = CASN1EncSequence::NewLC();
259 TInt numEntries = Count();
260 for(TInt i = 0; i < numEntries; i++)
262 // Each attribute pair has to be enclosed in a SET-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);
277 EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1L() const
279 CASN1EncSequence* nameSeq = EncodeASN1LC();
280 CleanupStack::Pop(nameSeq);