|
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 "CCMSKEKRecipientInfo.h" |
|
21 #include "CCMSX509AlgorithmIdentifier.h" |
|
22 #include "CCMSKeyIdentifier.h" |
|
23 #include <asn1dec.h> |
|
24 #include <asn1enc.h> |
|
25 |
|
26 // CONSTANTS |
|
27 const TInt KNumberOfSubModules = 4; |
|
28 |
|
29 // ============================ MEMBER FUNCTIONS =============================== |
|
30 |
|
31 // ----------------------------------------------------------------------------- |
|
32 // CCMSKEKRecipientInfo::CCMSKEKRecipientInfo |
|
33 // C++ default constructor can NOT contain any code, that |
|
34 // might leave. |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 EXPORT_C CCMSKEKRecipientInfo::CCMSKEKRecipientInfo( ) |
|
38 : CCMSRecipientInfo( KCMSKEKRecipientInfoVersion ) |
|
39 { |
|
40 } |
|
41 |
|
42 // ----------------------------------------------------------------------------- |
|
43 // CCMSKEKRecipientInfo::ConstructL |
|
44 // Symbian 2nd phase constructor can leave. |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 EXPORT_C void CCMSKEKRecipientInfo::ConstructL( |
|
48 const CCMSKeyIdentifier& aKekid, |
|
49 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
50 const TDesC8& aEncryptedKey ) |
|
51 { |
|
52 BaseConstructL( aKeyEncryptionAlgorithm ); |
|
53 SetKekidL( aKekid ); |
|
54 SetEncryptedKeyL( aEncryptedKey ); |
|
55 } |
|
56 |
|
57 // ----------------------------------------------------------------------------- |
|
58 // CCMSKEKRecipientInfo::NewLC |
|
59 // Two-phased constructor. |
|
60 // ----------------------------------------------------------------------------- |
|
61 // |
|
62 EXPORT_C CCMSKEKRecipientInfo* CCMSKEKRecipientInfo::NewLC() |
|
63 { |
|
64 CCMSKEKRecipientInfo* self = |
|
65 new( ELeave ) CCMSKEKRecipientInfo( ); |
|
66 |
|
67 CleanupStack::PushL( self ); |
|
68 // creating with empty/default values |
|
69 CCMSX509AlgorithmIdentifier* algorithmIdentifier = |
|
70 CCMSX509AlgorithmIdentifier::NewL(); |
|
71 CleanupStack::PushL( algorithmIdentifier ); |
|
72 CCMSKeyIdentifier* kekid = CCMSKeyIdentifier::NewL(); |
|
73 CleanupStack::PushL( kekid ); |
|
74 self->ConstructL( *kekid, *algorithmIdentifier, KNullDesC8() ); |
|
75 CleanupStack::PopAndDestroy( 2 ); // kekid, algorithmIdentifier |
|
76 return self; |
|
77 } |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // CCMSKEKRecipientInfo::NewL |
|
81 // Two-phased constructor. |
|
82 // ----------------------------------------------------------------------------- |
|
83 // |
|
84 EXPORT_C CCMSKEKRecipientInfo* CCMSKEKRecipientInfo::NewL() |
|
85 { |
|
86 CCMSKEKRecipientInfo* self = NewLC(); |
|
87 CleanupStack::Pop( self ); |
|
88 return self; |
|
89 } |
|
90 |
|
91 // ----------------------------------------------------------------------------- |
|
92 // CCMSKEKRecipientInfo::NewL |
|
93 // Two-phased constructor. |
|
94 // ----------------------------------------------------------------------------- |
|
95 // |
|
96 EXPORT_C CCMSKEKRecipientInfo* CCMSKEKRecipientInfo::NewL( |
|
97 const CCMSKeyIdentifier& aKekid, |
|
98 const CCMSX509AlgorithmIdentifier& aKeyEncryptionAlgorithm, |
|
99 const TDesC8& aEncryptedKey ) |
|
100 { |
|
101 CCMSKEKRecipientInfo* self = |
|
102 new( ELeave ) CCMSKEKRecipientInfo( ); |
|
103 |
|
104 CleanupStack::PushL( self ); |
|
105 self->ConstructL( aKekid, aKeyEncryptionAlgorithm, |
|
106 aEncryptedKey ); |
|
107 CleanupStack::Pop( self ); |
|
108 return self; |
|
109 } |
|
110 |
|
111 // Destructor |
|
112 CCMSKEKRecipientInfo::~CCMSKEKRecipientInfo() |
|
113 { |
|
114 delete iEncryptedKey; |
|
115 delete iKekid; |
|
116 } |
|
117 |
|
118 // ----------------------------------------------------------------------------- |
|
119 // CCMSKEKRecipientInfo::DecodeL |
|
120 // Decrypts raw data to this instance |
|
121 // ----------------------------------------------------------------------------- |
|
122 void CCMSKEKRecipientInfo::DecodeL( const TDesC8& aRawData ) |
|
123 { |
|
124 TASN1DecGeneric decGen( aRawData ); |
|
125 decGen.InitL(); |
|
126 // accept only sequence or implicit [2] tag |
|
127 if( ( decGen.Tag() != EASN1Sequence ) && |
|
128 ( decGen.Tag() != KCMSKEKRecipientInfoTag ) ) |
|
129 { |
|
130 User::Leave( KErrArgument ); |
|
131 } |
|
132 TASN1DecSequence decSeq; |
|
133 CArrayPtr< TASN1DecGeneric >* itemsData = decSeq.DecodeDERLC( decGen ); |
|
134 TInt count = itemsData->Count(); |
|
135 if( count != KNumberOfSubModules ) |
|
136 { |
|
137 User::Leave( KErrArgument ); |
|
138 } |
|
139 |
|
140 // Decode version |
|
141 TASN1DecGeneric versionDec( *itemsData->At( 0 ) ); |
|
142 versionDec.InitL(); |
|
143 TASN1DecInteger intDecoder; |
|
144 TInt version = intDecoder.DecodeDERShortL( versionDec ); |
|
145 |
|
146 // decode kekid KEKIdentifier |
|
147 CCMSKeyIdentifier* keyId = CCMSKeyIdentifier::NewL(); |
|
148 CleanupStack::PushL( keyId ); |
|
149 keyId->DecodeL( itemsData->At( 1 )->Encoding() ); |
|
150 |
|
151 // decode keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier |
|
152 CCMSX509AlgorithmIdentifier* algId = CCMSX509AlgorithmIdentifier::NewL(); |
|
153 CleanupStack::PushL( algId ); |
|
154 algId->DecodeL( itemsData->At( 2 )->Encoding() ); |
|
155 |
|
156 // decode encryptedKey |
|
157 TASN1DecGeneric keyDecoder( *itemsData->At( 3 ) ); |
|
158 TASN1DecOctetString octetStringDecoder; |
|
159 keyDecoder.InitL(); |
|
160 HBufC8* tmp = octetStringDecoder.DecodeDERL( keyDecoder ); |
|
161 |
|
162 // now we have created all new members, so we can change state |
|
163 iVersion = version; |
|
164 delete iKekid; |
|
165 iKekid = keyId; |
|
166 delete iKeyEncryptionAlgorithm; |
|
167 iKeyEncryptionAlgorithm = algId; |
|
168 delete iEncryptedKey; |
|
169 iEncryptedKey = tmp; |
|
170 CleanupStack::Pop( 2 ); // algId, keyId |
|
171 CleanupStack::PopAndDestroy( itemsData ); |
|
172 } |
|
173 |
|
174 |
|
175 // ----------------------------------------------------------------------------- |
|
176 // CCMSKEKRecipientInfo::EncoderLC |
|
177 // Returns ASN1 encoder for this instance |
|
178 // ----------------------------------------------------------------------------- |
|
179 |
|
180 CASN1EncBase* CCMSKEKRecipientInfo::EncoderLC() const |
|
181 { |
|
182 CASN1EncSequence* root = CASN1EncSequence::NewLC(); |
|
183 |
|
184 // encode version |
|
185 AddVersionL( root ); |
|
186 |
|
187 // encode kekid |
|
188 HBufC8* encodedKekId = NULL; |
|
189 iKekid->EncodeL( encodedKekId ); |
|
190 CleanupStack::PushL( encodedKekId ); |
|
191 CASN1EncEncoding* kekId = CASN1EncEncoding::NewLC( *encodedKekId ); |
|
192 root->AddAndPopChildL( kekId ); |
|
193 CleanupStack::PopAndDestroy( encodedKekId ); |
|
194 |
|
195 // encode keyEncryptionAlgorithm |
|
196 AddKeyEncryptionAlgorithmL( root ); |
|
197 |
|
198 // encode encryptedKey |
|
199 CASN1EncOctetString* encryptedKey = |
|
200 CASN1EncOctetString::NewLC( *iEncryptedKey ); |
|
201 root->AddAndPopChildL( encryptedKey ); |
|
202 |
|
203 return root; |
|
204 } |
|
205 |
|
206 // ----------------------------------------------------------------------------- |
|
207 // CCMSKEKRecipientInfo::TaggedEncoderLC |
|
208 // Returns ASN1 encoder for this instance inside tag |
|
209 // ----------------------------------------------------------------------------- |
|
210 CASN1EncBase* CCMSKEKRecipientInfo::TaggedEncoderLC() const |
|
211 { |
|
212 CASN1EncBase* encoder = EncoderLC(); |
|
213 encoder->SetTag( KCMSKEKRecipientInfoTag ); |
|
214 return encoder; |
|
215 } |
|
216 |
|
217 // ----------------------------------------------------------------------------- |
|
218 // CCMSKEKRecipientInfo::EncryptedKey |
|
219 // EncryptedKey getter |
|
220 // ----------------------------------------------------------------------------- |
|
221 EXPORT_C const TDesC8& |
|
222 CCMSKEKRecipientInfo::EncryptedKey() const |
|
223 { |
|
224 return *iEncryptedKey; |
|
225 } |
|
226 |
|
227 // ----------------------------------------------------------------------------- |
|
228 // CCMSKEKRecipientInfo::Kekid |
|
229 // KEKIdentifier getter |
|
230 // ----------------------------------------------------------------------------- |
|
231 EXPORT_C const CCMSKeyIdentifier& |
|
232 CCMSKEKRecipientInfo::Kekid() const |
|
233 { |
|
234 return *iKekid; |
|
235 } |
|
236 |
|
237 // ----------------------------------------------------------------------------- |
|
238 // CCMSKEKRecipientInfo::SetEncryptedKeyL |
|
239 // EncryptedKey setter |
|
240 // ----------------------------------------------------------------------------- |
|
241 EXPORT_C void CCMSKEKRecipientInfo::SetEncryptedKeyL( |
|
242 const TDesC8& aEncryptedKey ) |
|
243 { |
|
244 HBufC8* encryptedKey = aEncryptedKey.AllocL(); |
|
245 delete iEncryptedKey; |
|
246 iEncryptedKey = encryptedKey; |
|
247 } |
|
248 |
|
249 // ----------------------------------------------------------------------------- |
|
250 // CCMSKEKRecipientInfo::SetKekidL |
|
251 // iKekid setter |
|
252 // ----------------------------------------------------------------------------- |
|
253 EXPORT_C void CCMSKEKRecipientInfo::SetKekidL( |
|
254 const CCMSKeyIdentifier& aKekid ) |
|
255 { |
|
256 CCMSKeyIdentifier* keyId = CCMSKeyIdentifier::NewL( aKekid.KeyIdentifier() ); |
|
257 CleanupStack::PushL( keyId ); |
|
258 const TTime* date = keyId->Date(); |
|
259 if( date ) |
|
260 { |
|
261 keyId->SetDateL( *date ); |
|
262 } |
|
263 CleanupStack::Pop( keyId ); |
|
264 delete iKekid; |
|
265 iKekid = keyId; |
|
266 } |
|
267 |
|
268 // End of File |