72
|
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 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "tvectorutils.h"
|
|
20 |
#include "t_input.h"
|
|
21 |
|
|
22 |
// RSA
|
|
23 |
_LIT8(KModStart, "<modulus>");
|
|
24 |
_LIT8(KModEnd, "</modulus>");
|
|
25 |
_LIT8(KPubExpStart, "<publicExponent>");
|
|
26 |
_LIT8(KPubExpEnd, "</publicExponent>");
|
|
27 |
_LIT8(KPrivExpStart, "<privateExponent>");
|
|
28 |
_LIT8(KPrivExpEnd, "</privateExponent>");
|
|
29 |
_LIT8(KPStart, "<P>");
|
|
30 |
_LIT8(KPEnd, "</P>");
|
|
31 |
_LIT8(KQStart, "<Q>");
|
|
32 |
_LIT8(KQEnd, "</Q>");
|
|
33 |
_LIT8(KdPStart, "<dP>");
|
|
34 |
_LIT8(KdPEnd, "</dP>");
|
|
35 |
_LIT8(KdQStart, "<dQ>");
|
|
36 |
_LIT8(KdQEnd, "</dQ>");
|
|
37 |
_LIT8(KqInvStart, "<qInv>");
|
|
38 |
_LIT8(KqInvEnd, "</qInv>");
|
|
39 |
|
|
40 |
_LIT8(KTrueVal, "ETrue");
|
|
41 |
_LIT8(KFalseVal, "EFalse");
|
|
42 |
|
|
43 |
RInteger VectorUtils::ParseIntegerL(const TDesC8& aDes)
|
|
44 |
{
|
|
45 |
HBufC8* buf = ParseBinaryL(aDes);
|
|
46 |
CleanupStack::PushL(buf);
|
|
47 |
RInteger result = RInteger::NewL(*buf);
|
|
48 |
CleanupStack::PopAndDestroy(buf);
|
|
49 |
|
|
50 |
return result;
|
|
51 |
}
|
|
52 |
|
|
53 |
HBufC8* VectorUtils::ParseBinaryL(const TDesC8& aDes)
|
|
54 |
{
|
|
55 |
__ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
|
|
56 |
int length = aDes.Length() / 2;
|
|
57 |
HBufC8* buf = HBufC8::NewL(length);
|
|
58 |
TPtr8 ptr = buf->Des();
|
|
59 |
ptr.SetLength(length);
|
|
60 |
|
|
61 |
for (TInt i = 0 ; i < aDes.Length() ; i += 2)
|
|
62 |
{
|
|
63 |
TUint8 tmp;
|
|
64 |
tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
|
|
65 |
tmp*=16;
|
|
66 |
tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
|
|
67 |
ptr[i / 2] = tmp;
|
|
68 |
}
|
|
69 |
|
|
70 |
return buf;
|
|
71 |
}
|
|
72 |
|
|
73 |
// Print an Integer into hex, only works for positive integers
|
|
74 |
TDesC* VectorUtils::PrintIntegerL(const TInteger& aInt)
|
|
75 |
{
|
|
76 |
HBufC8* binary = aInt.BufferLC();
|
|
77 |
TDesC* result = PrintBinaryL(*binary);
|
|
78 |
CleanupStack::PopAndDestroy(binary);
|
|
79 |
|
|
80 |
return result;
|
|
81 |
}
|
|
82 |
|
|
83 |
// Print a binary string into hex
|
|
84 |
TDesC* VectorUtils::PrintBinaryL(const TDesC8& aData)
|
|
85 |
{
|
|
86 |
int length = aData.Length() * 2;
|
|
87 |
HBufC* buf = HBufC::NewL(length);
|
|
88 |
TPtr ptr = buf->Des();
|
|
89 |
ptr.SetLength(length);
|
|
90 |
|
|
91 |
for (int i = 0 ; i < aData.Length() ; ++i)
|
|
92 |
{
|
|
93 |
TUint8 val = aData[i];
|
|
94 |
TUint8 n = (TUint8) ((val & 0xf0) >> 4);
|
|
95 |
ptr[i * 2] = (TUint8) (n < 10 ? ('0' + n) : ('A' + n - 10));
|
|
96 |
n = (TUint8) (val & 0x0f);
|
|
97 |
ptr[i * 2 + 1] = (TUint8) (n < 10 ? ('0' + n) : ('A' + n - 10));
|
|
98 |
}
|
|
99 |
|
|
100 |
return buf;
|
|
101 |
}
|
|
102 |
|
|
103 |
TBool VectorUtils::ParseBoolL(const TDesC8& aDes)
|
|
104 |
{
|
|
105 |
TBool result = EFalse;
|
|
106 |
|
|
107 |
if (aDes == KTrueVal)
|
|
108 |
result = ETrue;
|
|
109 |
else if (aDes != KFalseVal)
|
|
110 |
User::Leave(KErrArgument);
|
|
111 |
|
|
112 |
return result;
|
|
113 |
}
|
|
114 |
|
|
115 |
CRSAPublicKey* VectorUtils::ReadRSAPublicKeyL(const TDesC8& aData)
|
|
116 |
{
|
|
117 |
TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
|
|
118 |
RInteger mod = ParseIntegerL(modIn);
|
|
119 |
CleanupStack::PushL(mod);
|
|
120 |
|
|
121 |
TPtrC8 pubExpIn = Input::ParseElement(aData, KPubExpStart, KPubExpEnd);
|
|
122 |
RInteger pubExp = ParseIntegerL(pubExpIn);
|
|
123 |
CleanupStack::PushL(pubExp);
|
|
124 |
|
|
125 |
CRSAPublicKey* result = CRSAPublicKey::NewL(mod, pubExp);
|
|
126 |
|
|
127 |
CleanupStack::Pop(&pubExp);
|
|
128 |
CleanupStack::Pop(&mod);
|
|
129 |
return result;
|
|
130 |
}
|
|
131 |
|
|
132 |
CRSAPrivateKeyStandard* VectorUtils::ReadRSAPrivateKeyL(const TDesC8& aData)
|
|
133 |
{
|
|
134 |
TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
|
|
135 |
RInteger mod = ParseIntegerL(modIn);
|
|
136 |
CleanupStack::PushL(mod);
|
|
137 |
|
|
138 |
TPtrC8 privExpIn = Input::ParseElement(aData, KPrivExpStart, KPrivExpEnd);
|
|
139 |
RInteger privExp = ParseIntegerL(privExpIn);
|
|
140 |
CleanupStack::PushL(privExp);
|
|
141 |
|
|
142 |
CRSAPrivateKeyStandard* result = CRSAPrivateKeyStandard::NewL(mod, privExp);
|
|
143 |
|
|
144 |
CleanupStack::Pop(&privExp);
|
|
145 |
CleanupStack::Pop(&mod);
|
|
146 |
return result;
|
|
147 |
}
|
|
148 |
|
|
149 |
CRSAPrivateKeyCRT* VectorUtils::ReadRSAPrivateKeyCRTL(const TDesC8& aData)
|
|
150 |
{
|
|
151 |
TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
|
|
152 |
RInteger mod = ParseIntegerL(modIn);
|
|
153 |
CleanupStack::PushL(mod);
|
|
154 |
|
|
155 |
TPtrC8 pIn = Input::ParseElement(aData, KPStart, KPEnd);
|
|
156 |
RInteger P = ParseIntegerL(pIn);
|
|
157 |
CleanupStack::PushL(P);
|
|
158 |
|
|
159 |
TPtrC8 qIn = Input::ParseElement(aData, KQStart, KQEnd);
|
|
160 |
RInteger Q = ParseIntegerL(qIn);
|
|
161 |
CleanupStack::PushL(Q);
|
|
162 |
|
|
163 |
TPtrC8 dpIn = Input::ParseElement(aData, KdPStart, KdPEnd);
|
|
164 |
RInteger dP = ParseIntegerL(dpIn);
|
|
165 |
CleanupStack::PushL(dP);
|
|
166 |
|
|
167 |
TPtrC8 dqIn = Input::ParseElement(aData, KdQStart, KdQEnd);
|
|
168 |
RInteger dQ = ParseIntegerL(dqIn);
|
|
169 |
CleanupStack::PushL(dQ);
|
|
170 |
|
|
171 |
TPtrC8 qInvIn = Input::ParseElement(aData, KqInvStart, KqInvEnd);
|
|
172 |
RInteger qInv = ParseIntegerL(qInvIn);
|
|
173 |
CleanupStack::PushL(qInv);
|
|
174 |
|
|
175 |
CRSAPrivateKeyCRT* privKey = CRSAPrivateKeyCRT::NewL(mod, P, Q, dP, dQ, qInv);
|
|
176 |
|
|
177 |
CleanupStack::Pop(&qInv);
|
|
178 |
CleanupStack::Pop(&dQ);
|
|
179 |
CleanupStack::Pop(&dP);
|
|
180 |
CleanupStack::Pop(&Q);
|
|
181 |
CleanupStack::Pop(&P);
|
|
182 |
CleanupStack::Pop(&mod);
|
|
183 |
return (privKey);
|
|
184 |
}
|
|
185 |
|
|
186 |
CDSAPublicKey* VectorUtils::ReadDSAPublicKeyL(const TDesC8& aData)
|
|
187 |
{
|
|
188 |
TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
|
|
189 |
RInteger p = ParseIntegerL(pIn);
|
|
190 |
CleanupStack::PushL(p);
|
|
191 |
|
|
192 |
TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
|
|
193 |
RInteger q = ParseIntegerL(qIn);
|
|
194 |
CleanupStack::PushL(q);
|
|
195 |
|
|
196 |
TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
|
|
197 |
RInteger g = ParseIntegerL(gIn);
|
|
198 |
CleanupStack::PushL(g);
|
|
199 |
|
|
200 |
TPtrC8 yIn = Input::ParseElement(aData, _L8("<y>"));
|
|
201 |
RInteger y = ParseIntegerL(yIn);
|
|
202 |
CleanupStack::PushL(y);
|
|
203 |
|
|
204 |
CDSAPublicKey* result = CDSAPublicKey::NewL(p, q, g, y);
|
|
205 |
|
|
206 |
CleanupStack::Pop(&y);
|
|
207 |
CleanupStack::Pop(&g);
|
|
208 |
CleanupStack::Pop(&q);
|
|
209 |
CleanupStack::Pop(&p);
|
|
210 |
|
|
211 |
return result;
|
|
212 |
}
|
|
213 |
|
|
214 |
CDSAPrivateKey* VectorUtils::ReadDSAPrivateKeyL(const TDesC8& aData)
|
|
215 |
{
|
|
216 |
TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
|
|
217 |
RInteger p = ParseIntegerL(pIn);
|
|
218 |
CleanupStack::PushL(p);
|
|
219 |
|
|
220 |
TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
|
|
221 |
RInteger q = ParseIntegerL(qIn);
|
|
222 |
CleanupStack::PushL(q);
|
|
223 |
|
|
224 |
TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
|
|
225 |
RInteger g = ParseIntegerL(gIn);
|
|
226 |
CleanupStack::PushL(g);
|
|
227 |
|
|
228 |
TPtrC8 xIn = Input::ParseElement(aData, _L8("<x>"));
|
|
229 |
RInteger x = ParseIntegerL(xIn);
|
|
230 |
CleanupStack::PushL(x);
|
|
231 |
|
|
232 |
CDSAPrivateKey* result = CDSAPrivateKey::NewL(p, q, g, x);
|
|
233 |
|
|
234 |
CleanupStack::Pop(&x);
|
|
235 |
CleanupStack::Pop(&g);
|
|
236 |
CleanupStack::Pop(&q);
|
|
237 |
CleanupStack::Pop(&p);
|
|
238 |
|
|
239 |
return result;
|
|
240 |
}
|
|
241 |
|
|
242 |
CDSASignature* VectorUtils::ReadDSASignatureL(const TDesC8& aData)
|
|
243 |
{
|
|
244 |
TPtrC8 rIn = Input::ParseElement(aData, _L8("<r>"));
|
|
245 |
RInteger r = ParseIntegerL(rIn);
|
|
246 |
CleanupStack::PushL(r);
|
|
247 |
|
|
248 |
TPtrC8 sIn = Input::ParseElement(aData, _L8("<s>"));
|
|
249 |
RInteger s = ParseIntegerL(sIn);
|
|
250 |
CleanupStack::PushL(s);
|
|
251 |
|
|
252 |
CDSASignature* result = CDSASignature::NewL(r, s);
|
|
253 |
|
|
254 |
CleanupStack::Pop(&s);
|
|
255 |
CleanupStack::Pop(&r);
|
|
256 |
|
|
257 |
return result;
|
|
258 |
}
|