os/security/cryptoservices/certificateandkeymgmt/asn1/bitstrenc.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2002-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 implementation for class encoding bit strings in ASN1 DER.
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file
    22 */
    23 
    24 #include <asn1enc.h>
    25 
    26 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr)
    27 	{
    28 	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    29 	CleanupStack::PushL(self);
    30 	self->ConstructL(aBitStr);
    31 	return self;
    32 	}
    33 
    34 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr)
    35 	{
    36 	CASN1EncBitString* self = NewLC(aBitStr);
    37 	CleanupStack::Pop(self);
    38 	return self;
    39 	}
    40 
    41 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr, 
    42 													 TUint aLengthBits)
    43 	{
    44 	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    45 	CleanupStack::PushL(self);
    46 	self->ConstructL(aBitStr, aLengthBits);
    47 	return self;
    48 	}
    49 
    50 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr, 
    51 													TUint aLengthBits)
    52 	{
    53 	CASN1EncBitString* self = NewLC(aBitStr, aLengthBits);
    54 	CleanupStack::Pop(self);
    55 	return self;
    56 	}
    57 
    58 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const CASN1EncBase& aEncObj)
    59 	{
    60 	CASN1EncBitString* self = NewLC(aEncObj);
    61 	CleanupStack::Pop(self);
    62 	return self;
    63 	}
    64 
    65 EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const CASN1EncBase& aEncObj)
    66 	{
    67 	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    68 	CleanupStack::PushL(self);
    69 	self->ConstructL(aEncObj);
    70 	return self;
    71 	}
    72 
    73 EXPORT_C CASN1EncBitString::~CASN1EncBitString()
    74 	{
    75 	delete iContents;
    76 	}
    77 
    78 CASN1EncBitString::CASN1EncBitString()
    79 : CASN1EncPrimitive(EASN1BitString),
    80   iPadding(0)
    81 	{
    82 	}
    83 
    84 void CASN1EncBitString::ConstructL(const TDesC8& aBitStr)
    85 	{
    86 	iContents = aBitStr.AllocL();
    87 	CASN1EncPrimitive::ConstructL();
    88 	}
    89 
    90 void CASN1EncBitString::ConstructL(const TDesC8& aBitStr, TUint aLengthBits)
    91 	{
    92 	iContents = aBitStr.AllocL();
    93 	CASN1EncPrimitive::ConstructL();
    94 	TUint totalBits = aBitStr.Length() * 8;
    95 	__ASSERT_ALWAYS(aLengthBits <= totalBits, User::Leave(KErrArgument));
    96 	iPadding = (TUint8)(totalBits - aLengthBits);
    97 	if(iPadding > 7)
    98 		User::Leave(KErrArgument);
    99 	}
   100 
   101 /**
   102  * @internalTechnology
   103  * Constructs bit string from ASN.1 encoding object.
   104  * @param aEncObj ASN.1 encoding object to wrap in bit string.
   105  * @note First produces raw DER encoding from the object, then creates 
   106  *     a bit string using other construct function.
   107  */
   108 void CASN1EncBitString::ConstructL(const CASN1EncBase& aEncObj)
   109 	{
   110 	// produce raw DER encoding from the created ASN.1 encoding
   111 	TUint len = aEncObj.LengthDER();
   112 	HBufC8* intDer = HBufC8::NewMaxLC(len);
   113 	TPtr8 ptrDer = intDer->Des();
   114 	TUint pos = 0;
   115 	aEncObj.WriteDERL(ptrDer, pos);
   116 	// wrap the produced DER encoding into a bit string
   117 	ConstructL(*intDer);
   118 	// cleanup
   119 	CleanupStack::PopAndDestroy(intDer);
   120 	}
   121 
   122 /** 
   123  * @internalTechnology 
   124  * Calculates length of DER-encoded bit string contents. For non empty
   125  * bit strings this differs from octet string in the extra leading byte 
   126  * containing the number of unused bits in the last octet.
   127  */
   128 void CASN1EncBitString::CalculateContentsLengthDER()
   129 	{
   130 	iContentsLengthDER = iContents->Length();
   131 	if (iContentsLengthDER > 0)
   132 		{
   133 		iContentsLengthDER++;
   134 		}
   135 	}
   136 
   137 /**
   138  * @internalTechnology 
   139  * Writes DER-encoded bit string contents to aBuf. Prepends 
   140  * the actual bit string octets with extra octet containing 
   141  * number of unused bits in the last octet. Before writing, 
   142  * converts contents of the bit string to big-endian form.
   143  * @param aBuf Buffer to write to; must be long enough; 
   144  *     see #CalculateContentsLengthDER method.
   145  */
   146 void CASN1EncBitString::WriteContentsDERL(TDes8& aBuf) const
   147 	{
   148 	if (iContents->Length() > 0)
   149 		{
   150 		aBuf[0] = iPadding;
   151 		TUint len = iContents->Length();
   152 		// We do not need to swap bits, as it is already done.
   153 		aBuf.Replace(1, len, *iContents);		
   154 		}
   155 	else
   156 		{
   157 		// no padding octet for the empty bit string
   158 		aBuf.SetLength(0);
   159 		}
   160 	}