|
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "UE_STD.H" |
|
17 |
|
18 #include <pbe.h> |
|
19 |
|
20 #define UNUSED_VAR(a) a = a |
|
21 |
|
22 TSecureFilter::TSecureFilter() |
|
23 {} |
|
24 |
|
25 void TSecureFilter::Set(MStreamBuf* aHost,TInt aMode) |
|
26 // |
|
27 // Set this filter up for encryption. |
|
28 // |
|
29 { |
|
30 TStreamFilter::Set(aHost,aMode); |
|
31 iIn.Zero(); |
|
32 // Make sure any header added by the en/de-cryption goes |
|
33 // straight into the output buffer |
|
34 TPtr8 buf(iBuf,sizeof(iBuf)); |
|
35 TRAPD(r, CryptL(buf,iIn)); |
|
36 UNUSED_VAR(r); |
|
37 iOut.Set(buf); |
|
38 } |
|
39 |
|
40 EXPORT_C TInt TSecureFilter::Capacity(TInt aMaxLength) |
|
41 // |
|
42 // Return the maximum guaranteed input used for aMaxLength output. |
|
43 // If we can fulfil the request from the output buffer, consume nothing, |
|
44 // otherwise return the space left in the input buffer |
|
45 // |
|
46 { |
|
47 return aMaxLength<=iOut.Length() ? 0 : KEncryptionFilterBufSize-iIn.Length(); |
|
48 } |
|
49 |
|
50 LOCAL_C TInt transfer(TDes8& aTarg,TPtrC8& aSrc) |
|
51 { |
|
52 TInt avail=aTarg.MaxLength()-aTarg.Length(); |
|
53 TInt len=Min(aSrc.Length(),avail); |
|
54 if (len) |
|
55 { |
|
56 aTarg.Append(aSrc.Left(len)); |
|
57 aSrc.Set(aSrc.Mid(len)); |
|
58 } |
|
59 return avail-len; |
|
60 } |
|
61 |
|
62 EXPORT_C TInt TSecureFilter::FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* anEnd) |
|
63 // |
|
64 // Encrypt the input buffer. |
|
65 // |
|
66 // This must consume all its input - when called during reading, it's asserted |
|
67 // that aFrom == anEnd after calling this |
|
68 // |
|
69 { |
|
70 TPtr8 dest((TUint8*)aPtr,aMaxLength); |
|
71 TPtrC8 src(aFrom,anEnd-aFrom); |
|
72 |
|
73 // Copy as much as possible from the output buffer to the destination |
|
74 TInt req=transfer(dest,iOut); |
|
75 |
|
76 // If there's input in src, copy as much as possible to the input buffer |
|
77 // iIn. If the input buffer is full, the output buffer is empty, and there |
|
78 // is space in the destination buffer, process data |
|
79 if ((src.Length()==0 || transfer(iIn,src)==0) && req) |
|
80 { // process input buffer to generate more output |
|
81 do |
|
82 { |
|
83 TPtr8 buf(iBuf,sizeof(iBuf)); |
|
84 CryptL(buf,iIn); |
|
85 iOut.Set(buf); |
|
86 iIn.Zero(); |
|
87 |
|
88 // Copy as much data as possible from the output buffer to the final |
|
89 // destination (updating iOut to point to the remainder), and as |
|
90 // much as possible from the source to the input buffer. If we have |
|
91 // completely emptied the output buffer and filled the input buffer, |
|
92 // and there is space in the destination buffer, go round again. |
|
93 } while (transfer(dest,iOut) && transfer(iIn,src)==0); |
|
94 } |
|
95 |
|
96 // Update client's src pointer to reflect what we've consumed |
|
97 aFrom=src.Ptr(); |
|
98 |
|
99 // Return the number of bytes output |
|
100 return dest.Length(); |
|
101 } |
|
102 |
|
103 TInt TSecureFilter::EmitL(const TDesC8& aDes) |
|
104 { |
|
105 TInt len=aDes.Length(); |
|
106 if (len) |
|
107 TStreamFilter::EmitL(aDes.Ptr(),len); |
|
108 return len; |
|
109 } |
|
110 |
|
111 EXPORT_C void TSecureFilter::DoSynchL() |
|
112 // |
|
113 // Pad out remaining input if necessary, encrypt and emit. |
|
114 // |
|
115 { |
|
116 if (IsCommitted()) |
|
117 return; |
|
118 // |
|
119 EmitL(iOut); |
|
120 iOut.Set(NULL,0); |
|
121 TPtr8 buf(iBuf,sizeof(iBuf)); |
|
122 CompleteL(buf,iIn); |
|
123 TStreamFilter::DoSynchL(); |
|
124 Committed(); |
|
125 } |
|
126 |
|
127 |
|
128 EXPORT_C TEncryptFilter::TEncryptFilter(): |
|
129 iKey(NULL) |
|
130 /** Constructs an empty encrypting filter object. |
|
131 |
|
132 The encrypting filter must be set up before use. |
|
133 |
|
134 @see Set() */ |
|
135 {} |
|
136 |
|
137 |
|
138 EXPORT_C void TEncryptFilter::SetL(MStreamBuf* aHost,CPBEncryptor* aKey,TInt aMode) |
|
139 /* |
|
140 Set this filter up for encryption using a Password Based Encryption object. |
|
141 @publishedPartner |
|
142 @leave KErrNoMemory. If a leave occurs, ownership of aKey is retained by the caller, |
|
143 which should thus keep aKey on the cleanup stack when calling this function. |
|
144 @param aHost The stream buffer that is the target for encrypted data. |
|
145 @param aKey A Password Based Encryption handling object. |
|
146 Ownership is transferred from the caller to this object as long as no allocation leave occurs. |
|
147 @param aMode The mode in which the stream buffer is to be used. |
|
148 By default, this is write mode as represented by EWrite. |
|
149 */ |
|
150 { |
|
151 __ASSERT_ALWAYS(aKey!=NULL,Panic(ECryptNoKey)); |
|
152 iKey=aKey; |
|
153 TSecureFilter::Set(aHost,aMode); |
|
154 } |
|
155 |
|
156 EXPORT_C TInt TEncryptFilter::CryptL(TDes8& aTarget,const TDesC8& aSource) |
|
157 { |
|
158 iKey->Process(aSource,aTarget); |
|
159 return aSource.Length(); |
|
160 } |
|
161 |
|
162 EXPORT_C void TEncryptFilter::CompleteL(TDes8& aTarget,const TDesC8& aSource) |
|
163 { |
|
164 // Encrypt and send remaining input buffer |
|
165 if (aSource.Length() > 0) |
|
166 { |
|
167 CryptL(aTarget, aSource); |
|
168 EmitL(aTarget); |
|
169 aTarget.Zero(); |
|
170 } |
|
171 |
|
172 TPtrC8 ptr; |
|
173 ptr.Set(NULL,0); |
|
174 iKey->ProcessFinalL(ptr, aTarget); |
|
175 EmitL(aTarget); |
|
176 } |
|
177 |
|
178 EXPORT_C void TEncryptFilter::DoRelease() |
|
179 { |
|
180 delete iKey; |
|
181 iKey=NULL; |
|
182 TSecureFilter::DoRelease(); |
|
183 } |
|
184 |
|
185 EXPORT_C TDecryptFilter::TDecryptFilter(): |
|
186 iKey(NULL) |
|
187 /** Constructs an empty decrypting filter object. |
|
188 |
|
189 The decrypting filter must be set up before use. |
|
190 |
|
191 @see Set() */ |
|
192 {} |
|
193 |
|
194 |
|
195 EXPORT_C void TDecryptFilter::SetL(MStreamBuf* aHost,CPBDecryptor* aKey,TInt aMode) |
|
196 /* |
|
197 Set this filter up for decryption using a Password Based Encryption object. |
|
198 @publishedPartner |
|
199 @leave KErrNoMemory. If a leave occurs, ownership of aKey is retained by the caller, |
|
200 which should thus keep aKey on the cleanup stack when calling this function. |
|
201 @param aHost The stream buffer that is the source of encrypted data. |
|
202 @param aKey A Password Based Encryption decryption object. |
|
203 Ownership is transferred from the caller to this object as long as no allocation leave occurs. |
|
204 @param aMode The mode in which the stream buffer is to be used. |
|
205 By default, this is write mode as represented by ERead. |
|
206 */ |
|
207 { |
|
208 __ASSERT_ALWAYS(aKey!=NULL,Panic(ECryptNoKey)); |
|
209 iKey=aKey; |
|
210 TSecureFilter::Set(aHost,aMode); |
|
211 } |
|
212 |
|
213 EXPORT_C TInt TDecryptFilter::CryptL(TDes8& aTarget,const TDesC8& aSource) |
|
214 { |
|
215 iKey->Process(aSource,aTarget); |
|
216 return aSource.Length(); |
|
217 } |
|
218 |
|
219 EXPORT_C void TDecryptFilter::CompleteL(TDes8& /*aTarget*/,const TDesC8& aSource) |
|
220 { |
|
221 if (aSource.Length()!=0) |
|
222 User::Leave(KErrCorrupt); |
|
223 } |
|
224 |
|
225 EXPORT_C void TDecryptFilter::DoRelease() |
|
226 { |
|
227 delete iKey; |
|
228 iKey=NULL; |
|
229 TSecureFilter::DoRelease(); |
|
230 } |
|
231 |
|
232 void HEncryptFilter::DoRelease() |
|
233 // |
|
234 // Finished with this filter. |
|
235 // |
|
236 { |
|
237 delete this; |
|
238 } |
|
239 |
|
240 void HDecryptFilter::DoRelease() |
|
241 // |
|
242 // Finished with this filter. |
|
243 // |
|
244 { |
|
245 delete this; |
|
246 } |
|
247 |