os/security/cryptoservices/certificateandkeymgmt/asn1/oiddec.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
* This file contains the implementation of the ASN1 Object ID base class.
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include "asn1dec.h"
sl@0
    21
sl@0
    22
const TInt KMaxOIDLength = 164;
sl@0
    23
sl@0
    24
EXPORT_C TASN1DecObjectIdentifier::TASN1DecObjectIdentifier(void)
sl@0
    25
	{
sl@0
    26
	}
sl@0
    27
sl@0
    28
EXPORT_C HBufC* TASN1DecObjectIdentifier::DecodeDERL(const TDesC8& aSource,TInt& aPos)
sl@0
    29
	{
sl@0
    30
	TASN1DecGeneric gen(aSource.Right(aSource.Length() - aPos));
sl@0
    31
	gen.InitL();
sl@0
    32
	HBufC* res = DecodeDERL(gen);
sl@0
    33
	aPos+=gen.LengthDER();
sl@0
    34
	return res;
sl@0
    35
	}
sl@0
    36
sl@0
    37
EXPORT_C HBufC* TASN1DecObjectIdentifier::DecodeDERL(const TASN1DecGeneric& aSource)
sl@0
    38
	{
sl@0
    39
	TFixedArray<TInt, KNumberOfIDs>* oid = new(ELeave) TFixedArray<TInt, KNumberOfIDs>;
sl@0
    40
	CleanupStack::PushL(oid);
sl@0
    41
	TInt count = DecodeContentsL(*oid, aSource.GetContentDER());
sl@0
    42
	HBufC* res = HBufC::NewLC(KMaxOIDLength);
sl@0
    43
	TPtr pRes = res->Des();
sl@0
    44
	if (count > 0)
sl@0
    45
		{
sl@0
    46
		pRes.AppendNum((*oid)[0]);
sl@0
    47
		for (TInt i = 1; i < count; i++)
sl@0
    48
			{
sl@0
    49
			pRes.Append('.');
sl@0
    50
			pRes.AppendNum((*oid)[i]);
sl@0
    51
			}
sl@0
    52
		}
sl@0
    53
	CleanupStack::Pop();//res
sl@0
    54
	CleanupStack::PopAndDestroy();//oid
sl@0
    55
	return res;
sl@0
    56
	}
sl@0
    57
sl@0
    58
TInt TASN1DecObjectIdentifier::DecodeContentsL(TFixedArray<TInt, KNumberOfIDs>& aArray, const TDesC8& aSource)
sl@0
    59
	{
sl@0
    60
	TInt l = aSource.Length();
sl@0
    61
	TInt i=0;
sl@0
    62
	TInt SubID=0;
sl@0
    63
	TInt SIDn=0;
sl@0
    64
	if(l < 1)
sl@0
    65
		{
sl@0
    66
		User::Leave(KErrArgument);
sl@0
    67
		}
sl@0
    68
	SubID+=aSource[i];
sl@0
    69
	i++;
sl@0
    70
	if (SubID>=80)
sl@0
    71
		{
sl@0
    72
		aArray[SIDn++]=2;
sl@0
    73
		SubID-=80;
sl@0
    74
		}
sl@0
    75
	else
sl@0
    76
		{
sl@0
    77
		if (SubID>=40)
sl@0
    78
			{
sl@0
    79
			aArray[SIDn++]=1;
sl@0
    80
			SubID-=40;
sl@0
    81
			}
sl@0
    82
		else
sl@0
    83
			{
sl@0
    84
			aArray[SIDn++]=0;
sl@0
    85
			}
sl@0
    86
		}
sl@0
    87
	aArray[SIDn++]=SubID;
sl@0
    88
	for (;i < l;)
sl@0
    89
		{
sl@0
    90
		SubID=0;
sl@0
    91
		while (aSource[i]&0x80)
sl@0
    92
			{
sl@0
    93
			// if shifting by 7 does not increase the value its overflowed
sl@0
    94
			if ( SubID && ((SubID << 7) <= SubID) )
sl@0
    95
				{
sl@0
    96
				User::Leave(KErrOverflow);
sl@0
    97
				}
sl@0
    98
				
sl@0
    99
			SubID<<=7;
sl@0
   100
			SubID+=aSource[i]&0x7f;
sl@0
   101
			if(i == (l-1))
sl@0
   102
				{
sl@0
   103
				User::Leave(KErrArgument);
sl@0
   104
				}
sl@0
   105
			i++;
sl@0
   106
			}
sl@0
   107
		if ( SubID && ((SubID << 7) <= SubID) )
sl@0
   108
			{
sl@0
   109
			User::Leave(KErrOverflow);
sl@0
   110
			}
sl@0
   111
		SubID<<=7;
sl@0
   112
		SubID+=aSource[i];
sl@0
   113
		
sl@0
   114
		i++;
sl@0
   115
		if (SIDn >= KNumberOfIDs) 
sl@0
   116
			{
sl@0
   117
			User::Leave(KErrOverflow);
sl@0
   118
			}
sl@0
   119
		aArray[SIDn++]=SubID;
sl@0
   120
		}
sl@0
   121
	return SIDn;
sl@0
   122
	}