|
1 /* |
|
2 * Copyright (c) 2004 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 "CCMSKeyTransRecipientInfo.h" |
|
21 #include "CCMSIssuerAndSerialNumber.h" |
|
22 #include "CCMSX509AlgorithmIdentifier.h" |
|
23 #include <asn1dec.h> |
|
24 #include <asn1enc.h> |
|
25 |
|
26 // CONSTANTS |
|
27 const TInt KNumberOfSubModules = 4; |
|
28 const TTagType KSubjectKeyIdentifierTag = 0; |
|
29 |
|
30 // ============================ MEMBER FUNCTIONS =============================== |
|
31 |
|
32 // ----------------------------------------------------------------------------- |
|
33 // CCMSKeyTransRecipientInfo::CCMSKeyTransRecipientInfo |
|
34 // C++ default constructor can NOT contain any code, that |
|
35 // might leave. |
|
36 // ----------------------------------------------------------------------------- |
|
37 // |
|
38 EXPORT_C CCMSKeyTransRecipientInfo::CCMSKeyTransRecipientInfo( TInt aVersion) |
|
39 : CCMSRecipientInfo( aVersion ) |
|
40 { |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // CCMSKeyTransRecipientInfo::ConstructL |
|
45 // Symbian 2nd phase constructor can leave. |
|
46 // ----------------------------------------------------------------------------- |
|
47 // |
|
48 EXPORT_C void CCMSKeyTransRecipientInfo::ConstructL( |
|
49 const CCMSIssuerAndSerialNumber& aIssuerAndSerialNumber, |
|
50 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
51 const TDesC8& aEncryptedKey ) |
|
52 { |
|
53 BaseConstructL( aKeyEncryptionAlgorithm ); |
|
54 SetIssuerAndSerialNumberL( aIssuerAndSerialNumber ); |
|
55 SetEncryptedKeyL( aEncryptedKey ); |
|
56 } |
|
57 |
|
58 // ----------------------------------------------------------------------------- |
|
59 // CCMSKeyTransRecipientInfo::ConstructL |
|
60 // Symbian 2nd phase constructor can leave. |
|
61 // ----------------------------------------------------------------------------- |
|
62 // |
|
63 EXPORT_C void CCMSKeyTransRecipientInfo::ConstructL( |
|
64 const TDesC8& aSubjectKeyIdentifier, |
|
65 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
66 const TDesC8& aEncryptedKey ) |
|
67 { |
|
68 BaseConstructL( aKeyEncryptionAlgorithm ); |
|
69 SetSubjectKeyIdentifierL( aSubjectKeyIdentifier ); |
|
70 SetEncryptedKeyL( aEncryptedKey ); |
|
71 } |
|
72 |
|
73 // ----------------------------------------------------------------------------- |
|
74 // CCMSKeyTransRecipientInfo::NewLC |
|
75 // Two-phased constructor. |
|
76 // ----------------------------------------------------------------------------- |
|
77 // |
|
78 EXPORT_C CCMSKeyTransRecipientInfo* CCMSKeyTransRecipientInfo::NewLC() |
|
79 { |
|
80 // creating with empty/default values |
|
81 CCMSKeyTransRecipientInfo* self = |
|
82 new( ELeave ) CCMSKeyTransRecipientInfo( |
|
83 KCMSKeyTransRecipientInfoSubjectKeyIdentifierVersion ); |
|
84 CleanupStack::PushL( self ); |
|
85 CCMSX509AlgorithmIdentifier* algorithmIdentifier = |
|
86 CCMSX509AlgorithmIdentifier::NewL(); |
|
87 CleanupStack::PushL( algorithmIdentifier ); |
|
88 self->ConstructL( KNullDesC8(), *algorithmIdentifier, KNullDesC8() ); |
|
89 CleanupStack::PopAndDestroy( algorithmIdentifier ); |
|
90 return self; |
|
91 } |
|
92 |
|
93 // ----------------------------------------------------------------------------- |
|
94 // CCMSKeyTransRecipientInfo::NewL |
|
95 // Two-phased constructor. |
|
96 // ----------------------------------------------------------------------------- |
|
97 // |
|
98 EXPORT_C CCMSKeyTransRecipientInfo* CCMSKeyTransRecipientInfo::NewL() |
|
99 { |
|
100 // creating with empty/default values |
|
101 CCMSKeyTransRecipientInfo* self = NewLC(); |
|
102 CleanupStack::Pop( self ); |
|
103 return self; |
|
104 } |
|
105 |
|
106 // ----------------------------------------------------------------------------- |
|
107 // CCMSKeyTransRecipientInfo::NewL |
|
108 // Two-phased constructor. |
|
109 // ----------------------------------------------------------------------------- |
|
110 // |
|
111 EXPORT_C CCMSKeyTransRecipientInfo* CCMSKeyTransRecipientInfo::NewL( |
|
112 const CCMSIssuerAndSerialNumber& aIssuerAndSerialNumber, |
|
113 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
114 const TDesC8& aEncryptedKey ) |
|
115 { |
|
116 CCMSKeyTransRecipientInfo* self = |
|
117 new( ELeave ) CCMSKeyTransRecipientInfo( |
|
118 KCMSKeyTransRecipientInfoIssuerAndSerialNumberVersion ); |
|
119 |
|
120 CleanupStack::PushL( self ); |
|
121 self->ConstructL( aIssuerAndSerialNumber, aKeyEncryptionAlgorithm, |
|
122 aEncryptedKey ); |
|
123 CleanupStack::Pop( self ); |
|
124 return self; |
|
125 } |
|
126 |
|
127 // ----------------------------------------------------------------------------- |
|
128 // CCMSKeyTransRecipientInfo::NewLC |
|
129 // Two-phased constructor. |
|
130 // ----------------------------------------------------------------------------- |
|
131 // |
|
132 EXPORT_C CCMSKeyTransRecipientInfo* CCMSKeyTransRecipientInfo::NewLC( |
|
133 const TDesC8& aSubjectKeyIdentifier, |
|
134 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
135 const TDesC8& aEncryptedKey ) |
|
136 { |
|
137 CCMSKeyTransRecipientInfo* self = |
|
138 new( ELeave ) CCMSKeyTransRecipientInfo( |
|
139 KCMSKeyTransRecipientInfoSubjectKeyIdentifierVersion ); |
|
140 |
|
141 CleanupStack::PushL( self ); |
|
142 self->ConstructL( aSubjectKeyIdentifier, aKeyEncryptionAlgorithm, |
|
143 aEncryptedKey ); |
|
144 return self; |
|
145 } |
|
146 |
|
147 // ----------------------------------------------------------------------------- |
|
148 // CCMSKeyTransRecipientInfo::NewL |
|
149 // Two-phased constructor. |
|
150 // ----------------------------------------------------------------------------- |
|
151 // |
|
152 EXPORT_C CCMSKeyTransRecipientInfo* CCMSKeyTransRecipientInfo::NewL( |
|
153 const TDesC8& aSubjectKeyIdentifier, |
|
154 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
155 const TDesC8& aEncryptedKey ) |
|
156 { |
|
157 CCMSKeyTransRecipientInfo* self = |
|
158 NewLC( aSubjectKeyIdentifier, aKeyEncryptionAlgorithm, aEncryptedKey ); |
|
159 |
|
160 CleanupStack::Pop( self ); |
|
161 return self; |
|
162 } |
|
163 |
|
164 |
|
165 // Destructor |
|
166 CCMSKeyTransRecipientInfo::~CCMSKeyTransRecipientInfo() |
|
167 { |
|
168 delete iEncryptedKey; |
|
169 delete iIssuerAndSerialNumber; |
|
170 delete iSubjectKeyIdentifier; |
|
171 } |
|
172 |
|
173 // ----------------------------------------------------------------------------- |
|
174 // CCMSKeyTransRecipientInfo::DecodeL |
|
175 // Decrypts raw data to this instance |
|
176 // ----------------------------------------------------------------------------- |
|
177 void CCMSKeyTransRecipientInfo::DecodeL( const TDesC8& aRawData ) |
|
178 { |
|
179 CArrayPtr< TASN1DecGeneric >* itemsData = |
|
180 DecodeSequenceLC( aRawData, KNumberOfSubModules, KNumberOfSubModules ); |
|
181 |
|
182 // Decode version |
|
183 TASN1DecGeneric versionDec( *itemsData->At( 0 ) ); |
|
184 versionDec.InitL(); |
|
185 TASN1DecInteger intDecoder; |
|
186 TInt version = intDecoder.DecodeDERShortL( versionDec ); |
|
187 |
|
188 // decode rid RecipientIdentifier |
|
189 TASN1DecOctetString octetStringDecoder; |
|
190 TASN1DecGeneric ridDec( *itemsData->At( 1 ) ); |
|
191 HBufC8* keyIdentifier = NULL; |
|
192 CCMSIssuerAndSerialNumber* issuer = NULL; |
|
193 switch( version ) |
|
194 { |
|
195 case KCMSKeyTransRecipientInfoSubjectKeyIdentifierVersion: |
|
196 { |
|
197 if( ridDec.Tag() != KSubjectKeyIdentifierTag ) |
|
198 { |
|
199 User::Leave( KErrArgument ); |
|
200 } |
|
201 TInt pos = 0; |
|
202 keyIdentifier = octetStringDecoder.DecodeDERL( |
|
203 ridDec.GetContentDER(), pos ); |
|
204 CleanupStack::PushL( keyIdentifier ); |
|
205 break; |
|
206 } |
|
207 case KCMSKeyTransRecipientInfoIssuerAndSerialNumberVersion: |
|
208 { |
|
209 issuer = CCMSIssuerAndSerialNumber::NewL(); |
|
210 CleanupStack::PushL( issuer ); |
|
211 issuer->DecodeL( ridDec.Encoding() ); |
|
212 break; |
|
213 } |
|
214 default: |
|
215 { |
|
216 User::Leave( KErrArgument ); |
|
217 } |
|
218 } |
|
219 |
|
220 // decode keyEncryptionAlgorithm |
|
221 CCMSX509AlgorithmIdentifier* algId = CCMSX509AlgorithmIdentifier::NewL(); |
|
222 CleanupStack::PushL( algId ); |
|
223 algId->DecodeL( itemsData->At( 2 )->Encoding() ); |
|
224 |
|
225 // decode encryptedKey |
|
226 TASN1DecGeneric keyDecoder( *itemsData->At( 3 ) ); |
|
227 keyDecoder.InitL(); |
|
228 HBufC8* tmp = octetStringDecoder.DecodeDERL( keyDecoder ); |
|
229 |
|
230 // now we have created all new members, so we can change state |
|
231 iVersion = version; |
|
232 delete iSubjectKeyIdentifier; |
|
233 iSubjectKeyIdentifier = keyIdentifier; |
|
234 delete iIssuerAndSerialNumber; |
|
235 iIssuerAndSerialNumber = issuer; |
|
236 delete iKeyEncryptionAlgorithm; |
|
237 iKeyEncryptionAlgorithm = algId; |
|
238 delete iEncryptedKey; |
|
239 iEncryptedKey = tmp; |
|
240 CleanupStack::Pop( 2 ); // keyIdentifier/issuer, algId |
|
241 CleanupStack::PopAndDestroy( itemsData ); |
|
242 } |
|
243 |
|
244 // ----------------------------------------------------------------------------- |
|
245 // CCMSKeyTransRecipientInfo::EncoderLC |
|
246 // Returns ASN1 encoder for this instance |
|
247 // ----------------------------------------------------------------------------- |
|
248 |
|
249 CASN1EncBase* CCMSKeyTransRecipientInfo::EncoderLC() const |
|
250 { |
|
251 CASN1EncSequence* root = CASN1EncSequence::NewLC(); |
|
252 |
|
253 // encode version |
|
254 AddVersionL( root ); |
|
255 |
|
256 // encode rid RecipientIdentifier |
|
257 if( iIssuerAndSerialNumber ) |
|
258 { |
|
259 // encode issuerAndSerialNumber |
|
260 HBufC8* encodedIssuer = NULL; |
|
261 iIssuerAndSerialNumber->EncodeL( encodedIssuer ); |
|
262 CleanupStack::PushL( encodedIssuer ); |
|
263 CASN1EncEncoding* issuer = CASN1EncEncoding::NewLC( *encodedIssuer ); |
|
264 root->AddAndPopChildL( issuer ); |
|
265 CleanupStack::PopAndDestroy( encodedIssuer ); |
|
266 } |
|
267 else |
|
268 { |
|
269 // encode subjectKeyIdentifier [0] SubjectKeyIdentifier |
|
270 CASN1EncOctetString* subjectKeyIdentifier = |
|
271 CASN1EncOctetString::NewL( *iSubjectKeyIdentifier ); |
|
272 CASN1EncExplicitTag* taggedIdentifier = |
|
273 CASN1EncExplicitTag::NewLC( subjectKeyIdentifier, KSubjectKeyIdentifierTag ); |
|
274 root->AddAndPopChildL( taggedIdentifier ); |
|
275 } |
|
276 |
|
277 // encode keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier |
|
278 AddKeyEncryptionAlgorithmL( root ); |
|
279 |
|
280 // encode encryptedKey |
|
281 CASN1EncOctetString* encryptedKey = |
|
282 CASN1EncOctetString::NewLC( *iEncryptedKey ); |
|
283 root->AddAndPopChildL( encryptedKey ); |
|
284 |
|
285 return root; |
|
286 } |
|
287 |
|
288 // ----------------------------------------------------------------------------- |
|
289 // CCMSKeyTransRecipientInfo::TaggedEncoderLC |
|
290 // Returns ASN1 encoder for this instance |
|
291 // ----------------------------------------------------------------------------- |
|
292 CASN1EncBase* CCMSKeyTransRecipientInfo::TaggedEncoderLC() const |
|
293 { |
|
294 return EncoderLC(); |
|
295 } |
|
296 |
|
297 // ----------------------------------------------------------------------------- |
|
298 // CCMSKeyTransRecipientInfo::EncryptedKey |
|
299 // EncryptedKey getter |
|
300 // ----------------------------------------------------------------------------- |
|
301 EXPORT_C const TDesC8& |
|
302 CCMSKeyTransRecipientInfo::EncryptedKey() const |
|
303 { |
|
304 return *iEncryptedKey; |
|
305 } |
|
306 |
|
307 // ----------------------------------------------------------------------------- |
|
308 // CCMSKeyTransRecipientInfo::IssuerAndSerialNumber |
|
309 // IssuerAndSerialNumber getter |
|
310 // ----------------------------------------------------------------------------- |
|
311 EXPORT_C const CCMSIssuerAndSerialNumber* |
|
312 CCMSKeyTransRecipientInfo::IssuerAndSerialNumber() const |
|
313 { |
|
314 return iIssuerAndSerialNumber; |
|
315 } |
|
316 |
|
317 // ----------------------------------------------------------------------------- |
|
318 // CCMSKeyTransRecipientInfo::SubjectKeyIdentifier |
|
319 // SubjectKeyIdentifier getter |
|
320 // ----------------------------------------------------------------------------- |
|
321 EXPORT_C const TDesC8* |
|
322 CCMSKeyTransRecipientInfo::SubjectKeyIdentifier() const |
|
323 { |
|
324 return iSubjectKeyIdentifier; |
|
325 } |
|
326 |
|
327 // ----------------------------------------------------------------------------- |
|
328 // CCMSKeyTransRecipientInfo::SetEncryptedKeyL |
|
329 // EncryptedKey setter |
|
330 // ----------------------------------------------------------------------------- |
|
331 EXPORT_C void CCMSKeyTransRecipientInfo::SetEncryptedKeyL( |
|
332 const TDesC8& aEncryptedKey ) |
|
333 { |
|
334 HBufC8* encryptedKey = aEncryptedKey.AllocL(); |
|
335 delete iEncryptedKey; |
|
336 iEncryptedKey = encryptedKey; |
|
337 } |
|
338 |
|
339 // ----------------------------------------------------------------------------- |
|
340 // CCMSKeyTransRecipientInfo::SetIssuerAndSerialNumberL |
|
341 // IssuerAndSerialNumber setter, deletes also SubjectKeyIdentifier and |
|
342 // sets version to KVersionIssuerAndSerialNumber |
|
343 // ----------------------------------------------------------------------------- |
|
344 EXPORT_C void CCMSKeyTransRecipientInfo::SetIssuerAndSerialNumberL( |
|
345 const CCMSIssuerAndSerialNumber& aIssuerAndSerialNumber ) |
|
346 { |
|
347 CCMSIssuerAndSerialNumber* issuer = CCMSIssuerAndSerialNumber::NewL( |
|
348 aIssuerAndSerialNumber.IssuerName(), |
|
349 aIssuerAndSerialNumber.SerialNumber() ); |
|
350 delete iSubjectKeyIdentifier; |
|
351 iSubjectKeyIdentifier = NULL; |
|
352 delete iIssuerAndSerialNumber; |
|
353 iIssuerAndSerialNumber = issuer; |
|
354 iVersion = KCMSKeyTransRecipientInfoIssuerAndSerialNumberVersion; |
|
355 } |
|
356 |
|
357 // ----------------------------------------------------------------------------- |
|
358 // CCMSKeyTransRecipientInfo::SetSubjectKeyIdentifierL |
|
359 // SubjectKeyIdentifier setter. Copies parameters, deletes also |
|
360 // IssuerAndSerialNumber and set version to KVersionSubjectKeyIdentifier |
|
361 // ----------------------------------------------------------------------------- |
|
362 EXPORT_C void CCMSKeyTransRecipientInfo::SetSubjectKeyIdentifierL( |
|
363 const TDesC8& aSubjectKeyIdentifier ) |
|
364 { |
|
365 HBufC8* identifier = aSubjectKeyIdentifier.AllocL(); |
|
366 delete iIssuerAndSerialNumber; |
|
367 iIssuerAndSerialNumber = NULL; |
|
368 delete iSubjectKeyIdentifier; |
|
369 iSubjectKeyIdentifier = identifier; |
|
370 iVersion = KCMSKeyTransRecipientInfoSubjectKeyIdentifierVersion; |
|
371 } |
|
372 |
|
373 // End of File |