sl@0: /* sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include "X500dec.h" sl@0: #include sl@0: sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CArrayPtr& aElements) sl@0: { sl@0: CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aElements); sl@0: CleanupStack::Pop();//self sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CArrayPtr& aElements) sl@0: { sl@0: CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aElements); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const CX500DistinguishedName& aName) sl@0: { sl@0: CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aName); sl@0: CleanupStack::Pop();//self sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const CX500DistinguishedName& aName) sl@0: { sl@0: CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(static_cast &>(*(aName.iElements))); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData) sl@0: { sl@0: TInt pos = 0; sl@0: return CX500DistinguishedName::NewL(aBinaryData, pos); sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData) sl@0: { sl@0: TInt pos = 0; sl@0: return CX500DistinguishedName::NewLC(aBinaryData, pos); sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(const TDesC8& aBinaryData, TInt& aPos) sl@0: { sl@0: CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aBinaryData, aPos); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(const TDesC8& aBinaryData, TInt& aPos) sl@0: { sl@0: CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aBinaryData, aPos); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewL(RReadStream& aStream) sl@0: { sl@0: CX500DistinguishedName* self = CX500DistinguishedName::NewLC(aStream); sl@0: CleanupStack::Pop();//self sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CX500DistinguishedName* CX500DistinguishedName::NewLC(RReadStream& aStream) sl@0: { sl@0: CX500DistinguishedName* self = new(ELeave) CX500DistinguishedName; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aStream); sl@0: return self; sl@0: } sl@0: sl@0: CX500DistinguishedName::CX500DistinguishedName() sl@0: { sl@0: } sl@0: sl@0: void CX500DistinguishedName::ConstructL(const CArrayPtr& aElements) sl@0: { sl@0: iElements = new(ELeave) CArrayPtrFlat(1); sl@0: TInt count = aElements.Count(); sl@0: for (TInt i=0;i < count; i++) sl@0: { sl@0: CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(*(aElements[i])); sl@0: iElements->AppendL(ava); sl@0: CleanupStack::Pop();//ava sl@0: } sl@0: } sl@0: sl@0: void CX500DistinguishedName::ConstructL(RReadStream& aStream) sl@0: { sl@0: iElements = new(ELeave) CArrayPtrFlat(1); sl@0: InternalizeL(aStream); sl@0: } sl@0: sl@0: void CX500DistinguishedName::ConstructL(const TDesC8& aBinaryData, TInt& aPos) sl@0: { sl@0: TASN1DecGeneric dec(aBinaryData.Right(aBinaryData.Length() - aPos)); sl@0: dec.InitL(); sl@0: TInt end = aPos + dec.LengthDER(); sl@0: aPos += dec.LengthDERHeader(); sl@0: sl@0: iElements = new(ELeave) CArrayPtrFlat (1); sl@0: if (dec.Tag() != EASN1Sequence) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: while (aPos < end) sl@0: { sl@0: TASN1DecGeneric rdn(aBinaryData.Right(aBinaryData.Length() - aPos)); sl@0: rdn.InitL(); sl@0: if (rdn.Tag() != EASN1Set) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: TInt rdnEnd = rdn.LengthDER(); sl@0: TInt rdnPos = rdn.LengthDERHeader();//add on header sl@0: while (rdnPos < rdnEnd) sl@0: { sl@0: CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(rdn.Encoding(), rdnPos); sl@0: iElements->AppendL(ava); sl@0: CleanupStack::Pop();//ava sl@0: } sl@0: aPos += rdnEnd; sl@0: } sl@0: if (aPos != end) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: } sl@0: sl@0: sl@0: EXPORT_C CX500DistinguishedName::~CX500DistinguishedName() sl@0: { sl@0: if (iElements != NULL) sl@0: { sl@0: iElements->ResetAndDestroy(); sl@0: delete iElements; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C TBool CX500DistinguishedName::ExactMatchL(const CX500DistinguishedName& aName) const sl@0: { sl@0: if (iElements->Count()!=aName.Count()) sl@0: return EFalse; sl@0: return IsWithinSubtreeL(aName); sl@0: } sl@0: sl@0: EXPORT_C HBufC* CX500DistinguishedName::ExtractFieldL(const TDesC& aFieldName) const sl@0: { sl@0: TInt count = Count(); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: const CX520AttributeTypeAndValue& ava = Element(i); sl@0: if (ava.Type() == aFieldName) sl@0: { sl@0: return ava.ValueL(); sl@0: } sl@0: } sl@0: return NULL; sl@0: } sl@0: sl@0: EXPORT_C HBufC* CX500DistinguishedName::DisplayNameL() const sl@0: { sl@0: HBufC* res = ExtractFieldL(KX520CommonName); sl@0: if (res == NULL) sl@0: { sl@0: res = ExtractFieldL(KX520OrganizationName); sl@0: if (res == NULL) sl@0: { sl@0: res = HBufC::NewL(0); sl@0: *res = KNullDesC; sl@0: } sl@0: } sl@0: return res; sl@0: } sl@0: sl@0: EXPORT_C TBool CX500DistinguishedName::IsWithinSubtreeL(const CX500DistinguishedName& aName) const sl@0: { sl@0: TInt elementsCount = aName.Count(); sl@0: for (TInt i = 0; i < elementsCount; ++i) sl@0: { sl@0: if (!MatchElementL(*(aName.iElements->At(i)))) sl@0: return EFalse; sl@0: } sl@0: return ETrue; sl@0: } sl@0: sl@0: EXPORT_C TBool CX500DistinguishedName::MatchElementL(const CX520AttributeTypeAndValue& aElement) const sl@0: { sl@0: TInt count = iElements->Count(); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: if (aElement.ExactMatchL(*iElements->At(i))) sl@0: return ETrue; sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: EXPORT_C TInt CX500DistinguishedName::Count() const sl@0: { sl@0: return iElements->Count(); sl@0: } sl@0: sl@0: EXPORT_C const CX520AttributeTypeAndValue& CX500DistinguishedName::Element(TInt aIndex) const sl@0: { sl@0: return *(iElements->At(aIndex)); sl@0: } sl@0: sl@0: EXPORT_C void CX500DistinguishedName::ExternalizeL(RWriteStream& aStream) const sl@0: { sl@0: TInt count = iElements->Count(); sl@0: aStream.WriteInt32L(count); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: CX520AttributeTypeAndValue* ava = iElements->At(i); sl@0: ava->ExternalizeL(aStream); sl@0: } sl@0: } sl@0: sl@0: void CX500DistinguishedName::InternalizeL(RReadStream& aStream) sl@0: { sl@0: TInt count = aStream.ReadInt32L(); sl@0: for (TInt i = 0; i < count; i++) sl@0: { sl@0: CX520AttributeTypeAndValue* ava = CX520AttributeTypeAndValue::NewLC(aStream); sl@0: iElements->AppendL(ava); sl@0: CleanupStack::Pop(); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1LC() const sl@0: { sl@0: CASN1EncSequence* nameSeq = CASN1EncSequence::NewLC(); sl@0: TInt numEntries = Count(); sl@0: for(TInt i = 0; i < numEntries; i++) sl@0: { sl@0: // Each attribute pair has to be enclosed in a SET-OF sl@0: // and SEQUENCE-OF sl@0: CASN1EncSequence* set = CASN1EncSequence::NewLC(); sl@0: // This conversion of a sequence into a set presumably sl@0: // works if the set is going to contain just one child sl@0: // item, because otherwise order of child items in a sl@0: // SET-OF becomes important. sl@0: set->SetTag(EASN1Set, EUniversal); sl@0: CASN1EncSequence* attr = Element(i).EncodeASN1LC(); sl@0: set->AddAndPopChildL(attr); sl@0: nameSeq->AddAndPopChildL(set); sl@0: } sl@0: return nameSeq; sl@0: } sl@0: sl@0: EXPORT_C CASN1EncSequence* CX500DistinguishedName::EncodeASN1L() const sl@0: { sl@0: CASN1EncSequence* nameSeq = EncodeASN1LC(); sl@0: CleanupStack::Pop(nameSeq); sl@0: return nameSeq; sl@0: }