os/security/cryptoservices/certificateandkeymgmt/asn1/bitstrdec.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.
     1 /*
     2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * This file contains the object implementing the Bitstring ASN1 object.
    16 * There is no handling for constructed form of Bitstring right now.
    17 *
    18 */
    19 
    20 
    21 #include "asn1dec.h"
    22 
    23 EXPORT_C TASN1DecBitString::TASN1DecBitString(void)
    24 	{
    25 	}
    26 
    27 EXPORT_C HBufC8* TASN1DecBitString::DecodeDERL(const TDesC8& /*aSource*/,TInt& /*aPos*/)
    28 	{
    29 	User::Leave(KErrNotSupported);
    30 	return NULL;
    31 	}
    32 
    33 EXPORT_C HBufC8* TASN1DecBitString::DecodeDERL(const TASN1DecGeneric& /*aSource*/)
    34 	{
    35 	User::Leave(KErrNotSupported);
    36 	return NULL;
    37 	}
    38 
    39 EXPORT_C HBufC8* TASN1DecBitString::ExtractOctetStringL(const TDesC8& aSource,TInt& aPos)
    40 	{
    41 	TASN1DecGeneric gen(aSource.Right(aSource.Length() - aPos));
    42 	gen.InitL();
    43 	HBufC8* res = ExtractOctetStringL(gen);
    44 	aPos+=gen.LengthDER();
    45 	return res;
    46 	}
    47 
    48 EXPORT_C HBufC8* TASN1DecBitString::ExtractOctetStringL(const TASN1DecGeneric& aSource)
    49 	{
    50 	TPtrC8 encoding = aSource.GetContentDER();
    51 	
    52 	// Special case. The empty bit string. (No padding length octet)
    53 	if (encoding.Length() == 0 )
    54 		{
    55 		return encoding.AllocL();				
    56 		}
    57 	
    58 	// Length must either be 0 for the empty string or >= 2 because
    59 	// of the padding length octet.
    60 	if (encoding.Length() == 1)
    61 		{
    62 		User::Leave(KErrArgument);
    63 		}
    64 		
    65 	TUint8 paddingLength = encoding[0];
    66 	if (paddingLength > 7)
    67 		{
    68 		User::Leave(KErrArgument);
    69 		}
    70 	TPtrC8 pEncoding = encoding.Right(encoding.Length()-1);
    71 	HBufC8* octetStr = pEncoding.AllocL();
    72 	TPtr8 pEncKey = octetStr->Des();
    73 
    74 	//stop stupid compiler warning
    75 	TUint8 mask =(TUint8)(255 << paddingLength);
    76 	//stop stupid compiler warning
    77 	pEncKey[pEncKey.Length()-1] = (TUint8) ((pEncKey[pEncKey.Length()-1]) & mask);
    78 	return octetStr;
    79 	}