os/security/cryptoservices/certificateandkeymgmt/asn1/oidenc.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) 2001-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
* Methods for encoding object identifiers
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "base128enc.h"
sl@0
    21
#include "panic.h"
sl@0
    22
sl@0
    23
#include <asn1enc.h>
sl@0
    24
sl@0
    25
const TInt KArrayGranularity = 5;
sl@0
    26
sl@0
    27
const TUint KSecondTermBigLimit = 175;
sl@0
    28
const TUint KFirstTermMultiplier = 40;
sl@0
    29
const TUint KFirstTermMax = 2;
sl@0
    30
sl@0
    31
sl@0
    32
EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewLC(const TDesC& aStr)
sl@0
    33
	{
sl@0
    34
	CASN1EncObjectIdentifier* self = new (ELeave) CASN1EncObjectIdentifier();
sl@0
    35
	CleanupStack::PushL(self);
sl@0
    36
	self->ConstructL(aStr);
sl@0
    37
	return self;
sl@0
    38
	}
sl@0
    39
sl@0
    40
EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewL(const TDesC& aStr)
sl@0
    41
	{
sl@0
    42
	CASN1EncObjectIdentifier* self = NewLC(aStr);
sl@0
    43
	CleanupStack::Pop(self);
sl@0
    44
	return self;
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C CASN1EncObjectIdentifier::~CASN1EncObjectIdentifier()
sl@0
    48
	{
sl@0
    49
	iData.Close();
sl@0
    50
	}
sl@0
    51
sl@0
    52
sl@0
    53
CASN1EncObjectIdentifier::CASN1EncObjectIdentifier()
sl@0
    54
: CASN1EncPrimitive(EASN1ObjectIdentifier), iData(KArrayGranularity)
sl@0
    55
	{
sl@0
    56
	}
sl@0
    57
sl@0
    58
sl@0
    59
// Takes ints in a string, delimited by '.' characters in between (not at ends)
sl@0
    60
void CASN1EncObjectIdentifier::ConstructL(const TDesC& aStr)
sl@0
    61
	{
sl@0
    62
	iData.Reset();
sl@0
    63
	TInt index = 0;
sl@0
    64
	TLex lex(aStr);
sl@0
    65
sl@0
    66
	// First term
sl@0
    67
	__ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
sl@0
    68
	TUint first;
sl@0
    69
	User::LeaveIfError(lex.Val(first));
sl@0
    70
	__ASSERT_ALWAYS(first <= KFirstTermMax, User::Leave(KErrBadDescriptor));
sl@0
    71
	// Static cast takes 8 least sig bits
sl@0
    72
	iFirstOctet = STATIC_CAST(TUint8, KFirstTermMultiplier * first);
sl@0
    73
sl@0
    74
	// A '.' to delimit
sl@0
    75
	__ASSERT_ALWAYS(!lex.Eos() && lex.Get() == '.',	User::Leave(KErrBadDescriptor));
sl@0
    76
sl@0
    77
	// Second term
sl@0
    78
	__ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
sl@0
    79
	TUint second;
sl@0
    80
	User::LeaveIfError(lex.Val(second));
sl@0
    81
	__ASSERT_ALWAYS((first < KFirstTermMax && second < KFirstTermMultiplier)
sl@0
    82
		|| (first == KFirstTermMax && second <= KSecondTermBigLimit),
sl@0
    83
		User::Leave(KErrBadDescriptor));
sl@0
    84
	// Static cast takes 8 least sig bits
sl@0
    85
	iFirstOctet = STATIC_CAST(TUint8, iFirstOctet + second);
sl@0
    86
sl@0
    87
	// Remaining terms
sl@0
    88
	while (!lex.Eos())
sl@0
    89
		{
sl@0
    90
		// Delimiter, and check we're not at end after that
sl@0
    91
		__ASSERT_ALWAYS(lex.Get() == '.' && !lex.Eos(),	User::Leave(KErrBadDescriptor));
sl@0
    92
sl@0
    93
		TUint value;
sl@0
    94
		User::LeaveIfError(lex.Val(value));
sl@0
    95
		++index;
sl@0
    96
sl@0
    97
		// Store the data away for later
sl@0
    98
		TASN1EncBase128DER encoder(value);
sl@0
    99
		User::LeaveIfError(iData.Append(encoder));
sl@0
   100
		}
sl@0
   101
sl@0
   102
	// Remainder of ConstructL is here - safe to call CalculateContentsLengthDER now.
sl@0
   103
	CASN1EncPrimitive::ConstructL();
sl@0
   104
	}
sl@0
   105
sl@0
   106
sl@0
   107
void CASN1EncObjectIdentifier::CalculateContentsLengthDER()
sl@0
   108
	{
sl@0
   109
	iContentsLengthDER = 1;
sl@0
   110
	for (TInt i = iData.Count() - 1; i >= 0; --i)
sl@0
   111
		{
sl@0
   112
		iContentsLengthDER += iData[i].LengthDER();
sl@0
   113
		}
sl@0
   114
	}
sl@0
   115
sl@0
   116
sl@0
   117
void CASN1EncObjectIdentifier::WriteContentsDERL(TDes8& aBuf) const
sl@0
   118
	{
sl@0
   119
	aBuf[0] = iFirstOctet;
sl@0
   120
	TUint cursor = 1;
sl@0
   121
	TInt count = iData.Count();
sl@0
   122
	for (TInt i = 0; i < count; ++i)
sl@0
   123
		{
sl@0
   124
		iData[i].WriteDERL(aBuf, cursor);
sl@0
   125
		}
sl@0
   126
	}