72
|
1 |
/*
|
|
2 |
* Copyright (c) 2003-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 "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 FILES
|
|
20 |
#include <e32std.h>
|
|
21 |
#include <f32file.h>
|
|
22 |
#include <flogger.h>
|
|
23 |
#include <x509cert.h>
|
|
24 |
#include <symmetric.h>
|
|
25 |
#include <asymmetric.h>
|
|
26 |
|
|
27 |
#ifdef RD_MULTIPLE_DRIVE
|
|
28 |
#include <driveinfo.h>
|
|
29 |
#endif
|
|
30 |
|
|
31 |
#include "DrmKeyStorage.h"
|
|
32 |
|
|
33 |
|
|
34 |
// EXTERNAL DATA STRUCTURES
|
|
35 |
|
|
36 |
// EXTERNAL FUNCTION PROTOTYPES
|
|
37 |
|
|
38 |
// CONSTANTS
|
|
39 |
|
|
40 |
// MACROS
|
|
41 |
|
|
42 |
// LOCAL CONSTANTS AND MACROS
|
|
43 |
|
|
44 |
// Test logging macros
|
|
45 |
|
|
46 |
#ifdef RD_MULTIPLE_DRIVE
|
|
47 |
_LIT(KFullLogDir, "%c:\\logs\\drm\\");
|
|
48 |
#else
|
|
49 |
_LIT(KFullLogDir, "c:\\logs\\drm\\");
|
|
50 |
#endif
|
|
51 |
|
|
52 |
_LIT(KLogDir, "drm");
|
|
53 |
_LIT(KLogName, "DrmEncryptorBb5KeyStorage.log");
|
|
54 |
|
|
55 |
#define TEST_STEP(string) \
|
|
56 |
GBb5Log.WriteFormat(_L("Next Test: %S"), &string);
|
|
57 |
|
|
58 |
#define CHECK(condition) \
|
|
59 |
if (!condition) GBb5Log.WriteFormat(_L("FAIL: line %d"), __LINE__);
|
|
60 |
|
|
61 |
// MODULE DATA STRUCTURES
|
|
62 |
|
|
63 |
RFs GBb5Fs;
|
|
64 |
RFileLogger GBb5Log;
|
|
65 |
|
|
66 |
// STATIC TEST CONTENT AND RIGHTS OBJECTS
|
|
67 |
|
|
68 |
// LOCAL FUNCTION PROTOTYPES
|
|
69 |
|
|
70 |
// ==================== LOCAL FUNCTIONS ====================
|
|
71 |
|
|
72 |
HBufC8* I2OSPL(
|
|
73 |
RInteger& aInt)
|
|
74 |
{
|
|
75 |
HBufC8* r = aInt.BufferLC();
|
|
76 |
CleanupStack::Pop(r);
|
|
77 |
return r;
|
|
78 |
}
|
|
79 |
|
|
80 |
RInteger OS2IPL(
|
|
81 |
const TDesC8& aOctetStream)
|
|
82 |
{
|
|
83 |
RInteger r;
|
|
84 |
TInt i;
|
|
85 |
|
|
86 |
r = RInteger::NewL(0);
|
|
87 |
for (i = 0; i < aOctetStream.Length(); i++)
|
|
88 |
{
|
|
89 |
r *= 256;
|
|
90 |
r += aOctetStream[i];
|
|
91 |
}
|
|
92 |
return r;
|
|
93 |
}
|
|
94 |
|
|
95 |
HBufC8* RsaEncryptL(
|
|
96 |
CRSAPublicKey* aKey,
|
|
97 |
const TDesC8& aInput)
|
|
98 |
{
|
|
99 |
RInteger result;
|
|
100 |
RInteger input;
|
|
101 |
HBufC8* output;
|
|
102 |
|
|
103 |
input = OS2IPL(aInput);
|
|
104 |
CleanupClosePushL(input);
|
|
105 |
result = TInteger::ModularExponentiateL(input, aKey->E(), aKey->N());
|
|
106 |
CleanupClosePushL(result);
|
|
107 |
output = I2OSPL(result);
|
|
108 |
CleanupStack::PopAndDestroy(2); // result, input
|
|
109 |
return output;
|
|
110 |
}
|
|
111 |
|
|
112 |
LOCAL_C TUint MDrmKeyStorage_GetCertificateChainL()
|
|
113 |
{
|
|
114 |
MDrmKeyStorage* storage = NULL;
|
|
115 |
RPointerArray<HBufC8> chain;
|
|
116 |
TInt i;
|
|
117 |
TUint result = NULL;
|
|
118 |
|
|
119 |
GBb5Log.WriteFormat(_L("MDrmKeyStorage_GetCertificateChainL -> DrmKeyStorageNewL"));
|
|
120 |
TRAPD(err,storage = DrmKeyStorageNewL());
|
|
121 |
if (err != KErrNone)
|
|
122 |
{
|
|
123 |
result = err;
|
|
124 |
}
|
|
125 |
|
|
126 |
GBb5Log.WriteFormat(_L("MDrmKeyStorage_GetCertificateChainL -> SelectDefaultRootL"));
|
|
127 |
storage->SelectDefaultRootL();
|
|
128 |
|
|
129 |
GBb5Log.WriteFormat(_L("MDrmKeyStorage_GetCertificateChainL -> GetCertificateChainL"));
|
|
130 |
storage->GetCertificateChainL(chain);
|
|
131 |
|
|
132 |
for (i = 0; i < chain.Count(); i++)
|
|
133 |
{
|
|
134 |
GBb5Log.WriteFormat(_L("Certificate %d:"), i);
|
|
135 |
GBb5Log.HexDump(_S(""), _S(""), chain[i]->Ptr(), chain[i]->Length());
|
|
136 |
}
|
|
137 |
chain.ResetAndDestroy();
|
|
138 |
chain.Close();
|
|
139 |
delete storage;
|
|
140 |
return result;
|
|
141 |
}
|
|
142 |
|
|
143 |
LOCAL_C TUint MDrmKeyStorage_DecryptL()
|
|
144 |
{
|
|
145 |
MDrmKeyStorage* storage = NULL;
|
|
146 |
RPointerArray<HBufC8> chain;
|
|
147 |
CRSAPublicKey* key = NULL;
|
|
148 |
CX509Certificate* cert = NULL;
|
|
149 |
TX509KeyFactory factory;
|
|
150 |
TBuf8<128> data;
|
|
151 |
HBufC8* encData;
|
|
152 |
HBufC8* decData;
|
|
153 |
TUint result = KErrNone;
|
|
154 |
|
|
155 |
|
|
156 |
GBb5Log.WriteFormat(_L("MDrmKeyStorage_Decrypt"));
|
|
157 |
storage = DrmKeyStorageNewL();
|
|
158 |
storage->SelectDefaultRootL();
|
|
159 |
storage->GetCertificateChainL(chain);
|
|
160 |
cert = CX509Certificate::NewL(*chain[0]);
|
|
161 |
chain.ResetAndDestroy();
|
|
162 |
chain.Close();
|
|
163 |
key = factory.RSAPublicKeyL(cert->PublicKey().KeyData());
|
|
164 |
data.SetLength(128);
|
|
165 |
data.Fill(1);
|
|
166 |
GBb5Log.WriteFormat(_L("data:"));
|
|
167 |
GBb5Log.HexDump(_S(""), _S(""), &data[0], sizeof(data));
|
|
168 |
|
|
169 |
encData = RsaEncryptL(key, data);
|
|
170 |
GBb5Log.WriteFormat(_L("encrypted data:"));
|
|
171 |
GBb5Log.HexDump(_S(""), _S(""), encData->Ptr(), encData->Length());
|
|
172 |
|
|
173 |
decData = storage->RsaDecryptL(*encData);
|
|
174 |
GBb5Log.WriteFormat(_L("decrypted data :"));
|
|
175 |
GBb5Log.HexDump(_S(""), _S(""), decData->Ptr(), decData->Length());
|
|
176 |
|
|
177 |
delete cert;
|
|
178 |
delete key;
|
|
179 |
delete storage;
|
|
180 |
return result;
|
|
181 |
}
|
|
182 |
|
|
183 |
// ==================== TEST FUNCTIONS =====================
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
TUint Bb5KeyStorage()
|
|
188 |
{
|
|
189 |
TUint result = 0;
|
|
190 |
TInt catchy = 0;
|
|
191 |
result = GBb5Fs.Connect();
|
|
192 |
if( result != KErrNone )
|
|
193 |
{
|
|
194 |
return result;
|
|
195 |
}
|
|
196 |
|
|
197 |
#ifndef RD_MULTIPLE_DRIVE
|
|
198 |
|
|
199 |
GBb5Fs.MkDirAll(KFullLogDir);
|
|
200 |
|
|
201 |
#else //RD_MULTIPLE_DRIVE
|
|
202 |
|
|
203 |
TInt driveNumber( -1 );
|
|
204 |
TChar driveLetter;
|
|
205 |
DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
|
|
206 |
GBb5Fs.DriveToChar( driveNumber, driveLetter );
|
|
207 |
|
|
208 |
TFileName fullLogDir;
|
|
209 |
fullLogDir.Format( KFullLogDir, (TUint)driveLetter );
|
|
210 |
|
|
211 |
GBb5Fs.MkDirAll(fullLogDir);
|
|
212 |
|
|
213 |
#endif
|
|
214 |
|
|
215 |
result = GBb5Log.Connect();
|
|
216 |
if( result != KErrNone )
|
|
217 |
{
|
|
218 |
GBb5Fs.Close();
|
|
219 |
return result;
|
|
220 |
}
|
|
221 |
GBb5Log.CreateLog(KLogDir, KLogName, EFileLoggingModeOverwrite);
|
|
222 |
GBb5Log.Write(_L("Start %D"));
|
|
223 |
|
|
224 |
TRAPD(err,result = MDrmKeyStorage_GetCertificateChainL());
|
|
225 |
CHECK(err == KErrNone);
|
|
226 |
if (err ==KErrNone)
|
|
227 |
{
|
|
228 |
TRAP(catchy, result = MDrmKeyStorage_DecryptL());
|
|
229 |
if( catchy )
|
|
230 |
{
|
|
231 |
result = catchy;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
CHECK(err == KErrNone);
|
|
235 |
GBb5Log.CloseLog();
|
|
236 |
GBb5Fs.Close();
|
|
237 |
return result;
|
|
238 |
}
|
|
239 |
|