19 /** |
19 /** |
20 @file |
20 @file |
21 */ |
21 */ |
22 |
22 |
23 #include <e32std.h> |
23 #include <e32std.h> |
|
24 #include <e32math.h> |
24 #include <e32debug.h> |
25 #include <e32debug.h> |
25 |
26 |
26 #include "randomimpl.h" |
27 #include "randomimpl.h" |
27 #include "pluginentry.h" |
28 #include "pluginentry.h" |
28 #include "pluginconfig.h" |
29 #include "pluginconfig.h" |
29 |
30 #include "securityerr.h" |
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 |
31 |
41 using namespace SoftwareCrypto; |
32 using namespace SoftwareCrypto; |
42 |
|
43 |
33 |
44 CRandomImpl* CRandomImpl::NewL(void) |
34 CRandomImpl* CRandomImpl::NewL(void) |
45 { |
35 { |
46 CRandomImpl* self = new(ELeave)CRandomImpl(); |
36 CRandomImpl* self = new(ELeave)CRandomImpl(); |
47 return self; |
37 return self; |
52 CRandomImpl* self = NewL(); |
42 CRandomImpl* self = NewL(); |
53 CleanupStack::PushL(self); |
43 CleanupStack::PushL(self); |
54 return self; |
44 return self; |
55 } |
45 } |
56 |
46 |
57 void CRandomImpl::GenerateRandomBytesL(TDes8& aDest) |
47 void CRandomImpl::GenerateRandomBytesL(TDes8& aDestination) |
58 { |
48 { |
59 TRandomImpl::Random(aDest); |
49 // Call the Math library to populate the buffer with random data. |
|
50 TRAPD(err, Math::RandomL(aDestination)); |
|
51 if(err != KErrNone) |
|
52 { |
|
53 // As the end users are interested only in the security aspect of the output but not |
|
54 // the internal states, accordingly translate the kernel side error code if required. |
|
55 err = (err == KErrNotReady) ? KErrNotSecure : err; |
|
56 User::Leave(err); |
|
57 } |
60 } |
58 } |
61 |
59 |
62 CRandomImpl::CRandomImpl(void) |
60 CRandomImpl::CRandomImpl(void) |
63 { |
61 { |
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 } |
62 } |
81 |
63 |
82 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
64 void CRandomImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics) |
83 { |
65 { |
84 TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); |
66 TInt randomNum = sizeof(KRandomCharacteristics)/sizeof(TRandomCharacteristics*); |
122 // hardware if required. Do nothing in this version |
104 // hardware if required. Do nothing in this version |
123 void CRandomImpl::Reset() |
105 void CRandomImpl::Reset() |
124 { |
106 { |
125 } |
107 } |
126 |
108 |
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. |
109 // Methods which are not supported can be excluded from the coverage. |
203 #ifdef _BullseyeCoverage |
110 #ifdef _BullseyeCoverage |
204 #pragma suppress_warnings on |
111 #pragma suppress_warnings on |
205 #pragma BullseyeCoverage off |
112 #pragma BullseyeCoverage off |
206 #pragma suppress_warnings off |
113 #pragma suppress_warnings off |