1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/oidenc.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,126 @@
1.4 +/*
1.5 +* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Methods for encoding object identifiers
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "base128enc.h"
1.24 +#include "panic.h"
1.25 +
1.26 +#include <asn1enc.h>
1.27 +
1.28 +const TInt KArrayGranularity = 5;
1.29 +
1.30 +const TUint KSecondTermBigLimit = 175;
1.31 +const TUint KFirstTermMultiplier = 40;
1.32 +const TUint KFirstTermMax = 2;
1.33 +
1.34 +
1.35 +EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewLC(const TDesC& aStr)
1.36 + {
1.37 + CASN1EncObjectIdentifier* self = new (ELeave) CASN1EncObjectIdentifier();
1.38 + CleanupStack::PushL(self);
1.39 + self->ConstructL(aStr);
1.40 + return self;
1.41 + }
1.42 +
1.43 +EXPORT_C CASN1EncObjectIdentifier* CASN1EncObjectIdentifier::NewL(const TDesC& aStr)
1.44 + {
1.45 + CASN1EncObjectIdentifier* self = NewLC(aStr);
1.46 + CleanupStack::Pop(self);
1.47 + return self;
1.48 + }
1.49 +
1.50 +EXPORT_C CASN1EncObjectIdentifier::~CASN1EncObjectIdentifier()
1.51 + {
1.52 + iData.Close();
1.53 + }
1.54 +
1.55 +
1.56 +CASN1EncObjectIdentifier::CASN1EncObjectIdentifier()
1.57 +: CASN1EncPrimitive(EASN1ObjectIdentifier), iData(KArrayGranularity)
1.58 + {
1.59 + }
1.60 +
1.61 +
1.62 +// Takes ints in a string, delimited by '.' characters in between (not at ends)
1.63 +void CASN1EncObjectIdentifier::ConstructL(const TDesC& aStr)
1.64 + {
1.65 + iData.Reset();
1.66 + TInt index = 0;
1.67 + TLex lex(aStr);
1.68 +
1.69 + // First term
1.70 + __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
1.71 + TUint first;
1.72 + User::LeaveIfError(lex.Val(first));
1.73 + __ASSERT_ALWAYS(first <= KFirstTermMax, User::Leave(KErrBadDescriptor));
1.74 + // Static cast takes 8 least sig bits
1.75 + iFirstOctet = STATIC_CAST(TUint8, KFirstTermMultiplier * first);
1.76 +
1.77 + // A '.' to delimit
1.78 + __ASSERT_ALWAYS(!lex.Eos() && lex.Get() == '.', User::Leave(KErrBadDescriptor));
1.79 +
1.80 + // Second term
1.81 + __ASSERT_ALWAYS(!lex.Eos(), User::Leave(KErrBadDescriptor));
1.82 + TUint second;
1.83 + User::LeaveIfError(lex.Val(second));
1.84 + __ASSERT_ALWAYS((first < KFirstTermMax && second < KFirstTermMultiplier)
1.85 + || (first == KFirstTermMax && second <= KSecondTermBigLimit),
1.86 + User::Leave(KErrBadDescriptor));
1.87 + // Static cast takes 8 least sig bits
1.88 + iFirstOctet = STATIC_CAST(TUint8, iFirstOctet + second);
1.89 +
1.90 + // Remaining terms
1.91 + while (!lex.Eos())
1.92 + {
1.93 + // Delimiter, and check we're not at end after that
1.94 + __ASSERT_ALWAYS(lex.Get() == '.' && !lex.Eos(), User::Leave(KErrBadDescriptor));
1.95 +
1.96 + TUint value;
1.97 + User::LeaveIfError(lex.Val(value));
1.98 + ++index;
1.99 +
1.100 + // Store the data away for later
1.101 + TASN1EncBase128DER encoder(value);
1.102 + User::LeaveIfError(iData.Append(encoder));
1.103 + }
1.104 +
1.105 + // Remainder of ConstructL is here - safe to call CalculateContentsLengthDER now.
1.106 + CASN1EncPrimitive::ConstructL();
1.107 + }
1.108 +
1.109 +
1.110 +void CASN1EncObjectIdentifier::CalculateContentsLengthDER()
1.111 + {
1.112 + iContentsLengthDER = 1;
1.113 + for (TInt i = iData.Count() - 1; i >= 0; --i)
1.114 + {
1.115 + iContentsLengthDER += iData[i].LengthDER();
1.116 + }
1.117 + }
1.118 +
1.119 +
1.120 +void CASN1EncObjectIdentifier::WriteContentsDERL(TDes8& aBuf) const
1.121 + {
1.122 + aBuf[0] = iFirstOctet;
1.123 + TUint cursor = 1;
1.124 + TInt count = iData.Count();
1.125 + for (TInt i = 0; i < count; ++i)
1.126 + {
1.127 + iData[i].WriteDERL(aBuf, cursor);
1.128 + }
1.129 + }