First public contribution.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "tvectorutils.h"
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>");
31 _LIT8(KQStart, "<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>");
40 _LIT8(KTrueVal, "ETrue");
41 _LIT8(KFalseVal, "EFalse");
43 RInteger VectorUtils::ParseIntegerL(const TDesC8& aDes)
45 HBufC8* buf = ParseBinaryL(aDes);
46 CleanupStack::PushL(buf);
47 RInteger result = RInteger::NewL(*buf);
48 CleanupStack::PopAndDestroy(buf);
53 HBufC8* VectorUtils::ParseBinaryL(const TDesC8& aDes)
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);
61 for (TInt i = 0 ; i < aDes.Length() ; i += 2)
64 tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
66 tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
73 // Print an Integer into hex, only works for positive integers
74 TDesC* VectorUtils::PrintIntegerL(const TInteger& aInt)
76 HBufC8* binary = aInt.BufferLC();
77 TDesC* result = PrintBinaryL(*binary);
78 CleanupStack::PopAndDestroy(binary);
83 // Print a binary string into hex
84 TDesC* VectorUtils::PrintBinaryL(const TDesC8& aData)
86 int length = aData.Length() * 2;
87 HBufC* buf = HBufC::NewL(length);
88 TPtr ptr = buf->Des();
89 ptr.SetLength(length);
91 for (int i = 0 ; i < aData.Length() ; ++i)
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));
103 TBool VectorUtils::ParseBoolL(const TDesC8& aDes)
105 TBool result = EFalse;
107 if (aDes == KTrueVal)
109 else if (aDes != KFalseVal)
110 User::Leave(KErrArgument);
115 CRSAPublicKey* VectorUtils::ReadRSAPublicKeyL(const TDesC8& aData)
117 TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
118 RInteger mod = ParseIntegerL(modIn);
119 CleanupStack::PushL(mod);
121 TPtrC8 pubExpIn = Input::ParseElement(aData, KPubExpStart, KPubExpEnd);
122 RInteger pubExp = ParseIntegerL(pubExpIn);
123 CleanupStack::PushL(pubExp);
125 CRSAPublicKey* result = CRSAPublicKey::NewL(mod, pubExp);
126 CleanupStack::Pop(2, &mod);
131 CRSAPrivateKeyStandard* VectorUtils::ReadRSAPrivateKeyL(const TDesC8& aData)
133 TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
134 RInteger mod = ParseIntegerL(modIn);
135 CleanupStack::PushL(mod);
137 TPtrC8 privExpIn = Input::ParseElement(aData, KPrivExpStart, KPrivExpEnd);
138 RInteger privExp = ParseIntegerL(privExpIn);
139 CleanupStack::PushL(privExp);
141 CRSAPrivateKeyStandard* result = CRSAPrivateKeyStandard::NewL(mod, privExp);
142 CleanupStack::Pop(2, &mod);
147 CRSAPrivateKeyCRT* VectorUtils::ReadRSAPrivateKeyCRTL(const TDesC8& aData)
149 TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
150 RInteger mod = ParseIntegerL(modIn);
151 CleanupStack::PushL(mod);
153 TPtrC8 pIn = Input::ParseElement(aData, KPStart, KPEnd);
154 RInteger P = ParseIntegerL(pIn);
155 CleanupStack::PushL(P);
157 TPtrC8 qIn = Input::ParseElement(aData, KQStart, KQEnd);
158 RInteger Q = ParseIntegerL(qIn);
159 CleanupStack::PushL(Q);
161 TPtrC8 dpIn = Input::ParseElement(aData, KdPStart, KdPEnd);
162 RInteger dP = ParseIntegerL(dpIn);
163 CleanupStack::PushL(dP);
165 TPtrC8 dqIn = Input::ParseElement(aData, KdQStart, KdQEnd);
166 RInteger dQ = ParseIntegerL(dqIn);
167 CleanupStack::PushL(dQ);
169 TPtrC8 qInvIn = Input::ParseElement(aData, KqInvStart, KqInvEnd);
170 RInteger qInv = ParseIntegerL(qInvIn);
171 CleanupStack::PushL(qInv);
173 CRSAPrivateKeyCRT* privKey = CRSAPrivateKeyCRT::NewL(mod, P, Q, dP, dQ, qInv);
174 CleanupStack::Pop(6, &mod);
179 CDSAPublicKey* VectorUtils::ReadDSAPublicKeyL(const TDesC8& aData)
181 TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
182 RInteger p = ParseIntegerL(pIn);
183 CleanupStack::PushL(p);
185 TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
186 RInteger q = ParseIntegerL(qIn);
187 CleanupStack::PushL(q);
189 TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
190 RInteger g = ParseIntegerL(gIn);
191 CleanupStack::PushL(g);
193 TPtrC8 yIn = Input::ParseElement(aData, _L8("<y>"));
194 RInteger y = ParseIntegerL(yIn);
195 CleanupStack::PushL(y);
197 CDSAPublicKey* result = CDSAPublicKey::NewLC(p, q, g, y);
198 CleanupStack::Pop(5, &p);
203 CDSAPrivateKey* VectorUtils::ReadDSAPrivateKeyL(const TDesC8& aData)
205 TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
206 RInteger p = ParseIntegerL(pIn);
207 CleanupStack::PushL(p);
209 TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
210 RInteger q = ParseIntegerL(qIn);
211 CleanupStack::PushL(q);
213 TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
214 RInteger g = ParseIntegerL(gIn);
215 CleanupStack::PushL(g);
217 TPtrC8 xIn = Input::ParseElement(aData, _L8("<x>"));
218 RInteger x = ParseIntegerL(xIn);
219 CleanupStack::PushL(x);
221 CDSAPrivateKey* result = CDSAPrivateKey::NewLC(p, q, g, x);
222 CleanupStack::Pop(result);
224 CleanupStack::Pop(4, &p);
229 CDSASignature* VectorUtils::ReadDSASignatureL(const TDesC8& aData)
231 TPtrC8 rIn = Input::ParseElement(aData, _L8("<r>"));
232 RInteger r = ParseIntegerL(rIn);
233 CleanupStack::PushL(r);
235 TPtrC8 sIn = Input::ParseElement(aData, _L8("<s>"));
236 RInteger s = ParseIntegerL(sIn);
237 CleanupStack::PushL(s);
239 CDSASignature* result = CDSASignature::NewLC(r, s);
240 CleanupStack::Pop(3, &r);