|
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 CleanupStack::Pop(2, &mod); |
|
127 |
|
128 return result; |
|
129 } |
|
130 |
|
131 CRSAPrivateKeyStandard* VectorUtils::ReadRSAPrivateKeyL(const TDesC8& aData) |
|
132 { |
|
133 TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd); |
|
134 RInteger mod = ParseIntegerL(modIn); |
|
135 CleanupStack::PushL(mod); |
|
136 |
|
137 TPtrC8 privExpIn = Input::ParseElement(aData, KPrivExpStart, KPrivExpEnd); |
|
138 RInteger privExp = ParseIntegerL(privExpIn); |
|
139 CleanupStack::PushL(privExp); |
|
140 |
|
141 CRSAPrivateKeyStandard* result = CRSAPrivateKeyStandard::NewL(mod, privExp); |
|
142 CleanupStack::Pop(2, &mod); |
|
143 |
|
144 return result; |
|
145 } |
|
146 |
|
147 CRSAPrivateKeyCRT* VectorUtils::ReadRSAPrivateKeyCRTL(const TDesC8& aData) |
|
148 { |
|
149 TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd); |
|
150 RInteger mod = ParseIntegerL(modIn); |
|
151 CleanupStack::PushL(mod); |
|
152 |
|
153 TPtrC8 pIn = Input::ParseElement(aData, KPStart, KPEnd); |
|
154 RInteger P = ParseIntegerL(pIn); |
|
155 CleanupStack::PushL(P); |
|
156 |
|
157 TPtrC8 qIn = Input::ParseElement(aData, KQStart, KQEnd); |
|
158 RInteger Q = ParseIntegerL(qIn); |
|
159 CleanupStack::PushL(Q); |
|
160 |
|
161 TPtrC8 dpIn = Input::ParseElement(aData, KdPStart, KdPEnd); |
|
162 RInteger dP = ParseIntegerL(dpIn); |
|
163 CleanupStack::PushL(dP); |
|
164 |
|
165 TPtrC8 dqIn = Input::ParseElement(aData, KdQStart, KdQEnd); |
|
166 RInteger dQ = ParseIntegerL(dqIn); |
|
167 CleanupStack::PushL(dQ); |
|
168 |
|
169 TPtrC8 qInvIn = Input::ParseElement(aData, KqInvStart, KqInvEnd); |
|
170 RInteger qInv = ParseIntegerL(qInvIn); |
|
171 CleanupStack::PushL(qInv); |
|
172 |
|
173 CRSAPrivateKeyCRT* privKey = CRSAPrivateKeyCRT::NewL(mod, P, Q, dP, dQ, qInv); |
|
174 CleanupStack::Pop(6, &mod); |
|
175 |
|
176 return (privKey); |
|
177 } |
|
178 |
|
179 CDSAPublicKey* VectorUtils::ReadDSAPublicKeyL(const TDesC8& aData) |
|
180 { |
|
181 TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>")); |
|
182 RInteger p = ParseIntegerL(pIn); |
|
183 CleanupStack::PushL(p); |
|
184 |
|
185 TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>")); |
|
186 RInteger q = ParseIntegerL(qIn); |
|
187 CleanupStack::PushL(q); |
|
188 |
|
189 TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>")); |
|
190 RInteger g = ParseIntegerL(gIn); |
|
191 CleanupStack::PushL(g); |
|
192 |
|
193 TPtrC8 yIn = Input::ParseElement(aData, _L8("<y>")); |
|
194 RInteger y = ParseIntegerL(yIn); |
|
195 CleanupStack::PushL(y); |
|
196 |
|
197 CDSAPublicKey* result = CDSAPublicKey::NewLC(p, q, g, y); |
|
198 CleanupStack::Pop(5, &p); |
|
199 |
|
200 return result; |
|
201 } |
|
202 |
|
203 CDSAPrivateKey* VectorUtils::ReadDSAPrivateKeyL(const TDesC8& aData) |
|
204 { |
|
205 TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>")); |
|
206 RInteger p = ParseIntegerL(pIn); |
|
207 CleanupStack::PushL(p); |
|
208 |
|
209 TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>")); |
|
210 RInteger q = ParseIntegerL(qIn); |
|
211 CleanupStack::PushL(q); |
|
212 |
|
213 TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>")); |
|
214 RInteger g = ParseIntegerL(gIn); |
|
215 CleanupStack::PushL(g); |
|
216 |
|
217 TPtrC8 xIn = Input::ParseElement(aData, _L8("<x>")); |
|
218 RInteger x = ParseIntegerL(xIn); |
|
219 CleanupStack::PushL(x); |
|
220 |
|
221 CDSAPrivateKey* result = CDSAPrivateKey::NewLC(p, q, g, x); |
|
222 CleanupStack::Pop(result); |
|
223 |
|
224 CleanupStack::Pop(4, &p); |
|
225 |
|
226 return result; |
|
227 } |
|
228 |
|
229 CDSASignature* VectorUtils::ReadDSASignatureL(const TDesC8& aData) |
|
230 { |
|
231 TPtrC8 rIn = Input::ParseElement(aData, _L8("<r>")); |
|
232 RInteger r = ParseIntegerL(rIn); |
|
233 CleanupStack::PushL(r); |
|
234 |
|
235 TPtrC8 sIn = Input::ParseElement(aData, _L8("<s>")); |
|
236 RInteger s = ParseIntegerL(sIn); |
|
237 CleanupStack::PushL(s); |
|
238 |
|
239 CDSASignature* result = CDSASignature::NewLC(r, s); |
|
240 CleanupStack::Pop(3, &r); |
|
241 |
|
242 return result; |
|
243 } |