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