|
1 /* |
|
2 * Copyright (c) 2005-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 /** |
|
20 @file |
|
21 */ |
|
22 |
|
23 #include <e32std.h> |
|
24 #include <e32debug.h> |
|
25 |
|
26 #include "randomimpl.h" |
|
27 #include "pluginentry.h" |
|
28 #include "pluginconfig.h" |
|
29 |
|
30 #include "randsvr.h" |
|
31 #include "randcliserv.h" |
|
32 #include "randsvrimpl.h" |
|
33 |
|
34 _LIT(KRandomServerImg,"z:\\sys\\bin\\randsvr.exe"); // DLL/EXE name |
|
35 _LIT(KRandomServerConnect, "Randsvr connect"); |
|
36 _LIT(KRandomServerGet, "Randsvr get"); |
|
37 |
|
38 const TUid KServerUid3={0x100066dc}; |
|
39 |
|
40 |
|
41 using namespace SoftwareCrypto; |
|
42 |
|
43 |
|
44 CRandomImpl* CRandomImpl::NewL(void) |
|
45 { |
|
46 CRandomImpl* self = new(ELeave)CRandomImpl(); |
|
47 return self; |
|
48 } |
|
49 |
|
50 CRandomImpl* CRandomImpl::NewLC(void) |
|
51 { |
|
52 CRandomImpl* self = NewL(); |
|
53 CleanupStack::PushL(self); |
|
54 return self; |
|
55 } |
|
56 |
|
57 void CRandomImpl::GenerateRandomBytesL(TDes8& aDest) |
|
58 { |
|
59 TRandomImpl::Random(aDest); |
|
60 } |
|
61 |
|
62 CRandomImpl::CRandomImpl(void) |
|
63 { |
|
64 } |
|
65 |
|
66 void TRandomImpl::Random(TDes8& aDestination) |
|
67 { |
|
68 RRandomSessionImpl rs; |
|
69 TRAPD(ret,rs.ConnectL()); |
|
70 if (ret != KErrNone) |
|
71 { |
|
72 User::Panic(KRandomServerConnect, ret); |
|
73 } |
|
74 TInt err=rs.GetRandom(aDestination); |
|
75 if (err != KErrNone) |
|
76 { |
|
77 User::Panic(KRandomServerGet, err); |
|
78 } |
|
79 rs.Close(); |
|
80 } |
|
81 |
|
82 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
|
83 { |
|
84 TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); |
|
85 for (TInt i = 0; i < randomNum; i++) |
|
86 { |
|
87 if (KRandomCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid) |
|
88 { |
|
89 aPluginCharacteristics = KRandomCharacteristics[i]; |
|
90 break; |
|
91 } |
|
92 } |
|
93 } |
|
94 |
|
95 CExtendedCharacteristics* CRandomImpl::CreateExtendedCharacteristicsL() |
|
96 { |
|
97 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved |
|
98 // for exclusive use and are not CERTIFIED to be standards compliant. |
|
99 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); |
|
100 } |
|
101 |
|
102 const CExtendedCharacteristics* CRandomImpl::GetExtendedCharacteristicsL() |
|
103 { |
|
104 return CRandomImpl::CreateExtendedCharacteristicsL(); |
|
105 } |
|
106 |
|
107 TUid CRandomImpl::ImplementationUid() const |
|
108 { |
|
109 return KCryptoPluginRandomUid; |
|
110 } |
|
111 |
|
112 CRandomImpl::~CRandomImpl() |
|
113 { |
|
114 } |
|
115 |
|
116 void CRandomImpl::Close() |
|
117 { |
|
118 delete this; |
|
119 } |
|
120 |
|
121 // All crypto plugins must implement this, to reset |
|
122 // hardware if required. Do nothing in this version |
|
123 void CRandomImpl::Reset() |
|
124 { |
|
125 } |
|
126 |
|
127 RRandomSessionImpl::RRandomSessionImpl(void) |
|
128 { |
|
129 } |
|
130 |
|
131 static TInt StartServer() |
|
132 // Borrowed from AndrewT's server startup code. |
|
133 // Start the server process/thread which lives in an EPOCEXE object |
|
134 // |
|
135 { |
|
136 |
|
137 const TUidType serverUid(KNullUid,KNullUid,KServerUid3); |
|
138 |
|
139 // |
|
140 // EPOC and EKA2 is easy, we just create a new server process. Simultaneous |
|
141 // launching of two such processes should be detected when the second one |
|
142 // attempts to create the server object, failing with KErrAlreadyExists. |
|
143 // |
|
144 RProcess server; |
|
145 TInt r=server.Create(KRandomServerImg, KNullDesC, serverUid); |
|
146 |
|
147 if (r!=KErrNone) |
|
148 return r; |
|
149 TRequestStatus stat; |
|
150 server.Rendezvous(stat); |
|
151 if (stat!=KRequestPending) |
|
152 server.Kill(0); // abort startup |
|
153 else |
|
154 server.Resume(); // logon OK - start the server |
|
155 User::WaitForRequest(stat); // wait for start or death |
|
156 // we can't use the 'exit reason' if the server panicked as this |
|
157 // is the panic 'reason' and may be '0' which cannot be distinguished |
|
158 // from KErrNone |
|
159 r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int(); |
|
160 server.Close(); |
|
161 return r; |
|
162 |
|
163 } |
|
164 |
|
165 void RRandomSessionImpl::ConnectL(void) |
|
166 { |
|
167 TInt retry=2; |
|
168 for (;;) |
|
169 { |
|
170 // Magic number 1 below is the number of asynchronous message slots |
|
171 TInt r = CreateSession(KRandomServerName,TVersion(0,0,0), 1); |
|
172 if (r == KErrNone) return; |
|
173 // We used to leave with KErrNone, but this is inefficient and |
|
174 // provokes an emulator problem in User::Leave which causes tpbe to crash |
|
175 // if (r == KErrNone) User::Leave(r); // Connected okay |
|
176 if (r != KErrNotFound && r != KErrServerTerminated) |
|
177 User::Leave(r); // Something else happened |
|
178 if (--retry == 0) |
|
179 User::Leave(r); // Give up after a while |
|
180 r = StartServer(); // Try starting again |
|
181 if (r != KErrNone && r != KErrAlreadyExists) |
|
182 User::Leave(r); |
|
183 } |
|
184 } |
|
185 |
|
186 TInt RRandomSessionImpl::GetRandom(TDes8& aDestination) |
|
187 { |
|
188 TInt desclength = aDestination.Length(); |
|
189 for ( TInt i = 0; i < desclength; i += KRandomBlockSize) |
|
190 { |
|
191 TInt getlen = Min(KRandomBlockSize, desclength - i); |
|
192 TPtr8 buffer(&aDestination[i], KRandomBlockSize, KRandomBlockSize); |
|
193 TInt err = SendReceive(CRandomSession::KRandomRequest, TIpcArgs(&buffer, getlen)); |
|
194 if (err != KErrNone) |
|
195 { |
|
196 return err; |
|
197 } |
|
198 } |
|
199 return KErrNone; |
|
200 } |
|
201 |
|
202 // Methods which are not supported can be excluded from the coverage. |
|
203 #ifdef _BullseyeCoverage |
|
204 #pragma suppress_warnings on |
|
205 #pragma BullseyeCoverage off |
|
206 #pragma suppress_warnings off |
|
207 #endif |
|
208 |
|
209 TAny* CRandomImpl::GetExtension(TUid /*aExtensionId*/) |
|
210 { |
|
211 return NULL; |
|
212 } |