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