os/security/cryptoservices/certificateandkeymgmt/asn1/bitstrenc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/bitstrenc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-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 +* This file contains the implementation for class encoding bit strings in ASN1 DER.
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 +*/
    1.26 +
    1.27 +#include <asn1enc.h>
    1.28 +
    1.29 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr)
    1.30 +	{
    1.31 +	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    1.32 +	CleanupStack::PushL(self);
    1.33 +	self->ConstructL(aBitStr);
    1.34 +	return self;
    1.35 +	}
    1.36 +
    1.37 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr)
    1.38 +	{
    1.39 +	CASN1EncBitString* self = NewLC(aBitStr);
    1.40 +	CleanupStack::Pop(self);
    1.41 +	return self;
    1.42 +	}
    1.43 +
    1.44 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const TDesC8& aBitStr, 
    1.45 +													 TUint aLengthBits)
    1.46 +	{
    1.47 +	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    1.48 +	CleanupStack::PushL(self);
    1.49 +	self->ConstructL(aBitStr, aLengthBits);
    1.50 +	return self;
    1.51 +	}
    1.52 +
    1.53 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const TDesC8& aBitStr, 
    1.54 +													TUint aLengthBits)
    1.55 +	{
    1.56 +	CASN1EncBitString* self = NewLC(aBitStr, aLengthBits);
    1.57 +	CleanupStack::Pop(self);
    1.58 +	return self;
    1.59 +	}
    1.60 +
    1.61 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewL(const CASN1EncBase& aEncObj)
    1.62 +	{
    1.63 +	CASN1EncBitString* self = NewLC(aEncObj);
    1.64 +	CleanupStack::Pop(self);
    1.65 +	return self;
    1.66 +	}
    1.67 +
    1.68 +EXPORT_C CASN1EncBitString* CASN1EncBitString::NewLC(const CASN1EncBase& aEncObj)
    1.69 +	{
    1.70 +	CASN1EncBitString* self = new (ELeave) CASN1EncBitString();
    1.71 +	CleanupStack::PushL(self);
    1.72 +	self->ConstructL(aEncObj);
    1.73 +	return self;
    1.74 +	}
    1.75 +
    1.76 +EXPORT_C CASN1EncBitString::~CASN1EncBitString()
    1.77 +	{
    1.78 +	delete iContents;
    1.79 +	}
    1.80 +
    1.81 +CASN1EncBitString::CASN1EncBitString()
    1.82 +: CASN1EncPrimitive(EASN1BitString),
    1.83 +  iPadding(0)
    1.84 +	{
    1.85 +	}
    1.86 +
    1.87 +void CASN1EncBitString::ConstructL(const TDesC8& aBitStr)
    1.88 +	{
    1.89 +	iContents = aBitStr.AllocL();
    1.90 +	CASN1EncPrimitive::ConstructL();
    1.91 +	}
    1.92 +
    1.93 +void CASN1EncBitString::ConstructL(const TDesC8& aBitStr, TUint aLengthBits)
    1.94 +	{
    1.95 +	iContents = aBitStr.AllocL();
    1.96 +	CASN1EncPrimitive::ConstructL();
    1.97 +	TUint totalBits = aBitStr.Length() * 8;
    1.98 +	__ASSERT_ALWAYS(aLengthBits <= totalBits, User::Leave(KErrArgument));
    1.99 +	iPadding = (TUint8)(totalBits - aLengthBits);
   1.100 +	if(iPadding > 7)
   1.101 +		User::Leave(KErrArgument);
   1.102 +	}
   1.103 +
   1.104 +/**
   1.105 + * @internalTechnology
   1.106 + * Constructs bit string from ASN.1 encoding object.
   1.107 + * @param aEncObj ASN.1 encoding object to wrap in bit string.
   1.108 + * @note First produces raw DER encoding from the object, then creates 
   1.109 + *     a bit string using other construct function.
   1.110 + */
   1.111 +void CASN1EncBitString::ConstructL(const CASN1EncBase& aEncObj)
   1.112 +	{
   1.113 +	// produce raw DER encoding from the created ASN.1 encoding
   1.114 +	TUint len = aEncObj.LengthDER();
   1.115 +	HBufC8* intDer = HBufC8::NewMaxLC(len);
   1.116 +	TPtr8 ptrDer = intDer->Des();
   1.117 +	TUint pos = 0;
   1.118 +	aEncObj.WriteDERL(ptrDer, pos);
   1.119 +	// wrap the produced DER encoding into a bit string
   1.120 +	ConstructL(*intDer);
   1.121 +	// cleanup
   1.122 +	CleanupStack::PopAndDestroy(intDer);
   1.123 +	}
   1.124 +
   1.125 +/** 
   1.126 + * @internalTechnology 
   1.127 + * Calculates length of DER-encoded bit string contents. For non empty
   1.128 + * bit strings this differs from octet string in the extra leading byte 
   1.129 + * containing the number of unused bits in the last octet.
   1.130 + */
   1.131 +void CASN1EncBitString::CalculateContentsLengthDER()
   1.132 +	{
   1.133 +	iContentsLengthDER = iContents->Length();
   1.134 +	if (iContentsLengthDER > 0)
   1.135 +		{
   1.136 +		iContentsLengthDER++;
   1.137 +		}
   1.138 +	}
   1.139 +
   1.140 +/**
   1.141 + * @internalTechnology 
   1.142 + * Writes DER-encoded bit string contents to aBuf. Prepends 
   1.143 + * the actual bit string octets with extra octet containing 
   1.144 + * number of unused bits in the last octet. Before writing, 
   1.145 + * converts contents of the bit string to big-endian form.
   1.146 + * @param aBuf Buffer to write to; must be long enough; 
   1.147 + *     see #CalculateContentsLengthDER method.
   1.148 + */
   1.149 +void CASN1EncBitString::WriteContentsDERL(TDes8& aBuf) const
   1.150 +	{
   1.151 +	if (iContents->Length() > 0)
   1.152 +		{
   1.153 +		aBuf[0] = iPadding;
   1.154 +		TUint len = iContents->Length();
   1.155 +		// We do not need to swap bits, as it is already done.
   1.156 +		aBuf.Replace(1, len, *iContents);		
   1.157 +		}
   1.158 +	else
   1.159 +		{
   1.160 +		// no padding octet for the empty bit string
   1.161 +		aBuf.SetLength(0);
   1.162 +		}
   1.163 +	}