First public contribution.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "pkcs12safebag.h"
20 #include <pkcs12bags.h>
22 using namespace PKCS12;
24 CDecPkcs12SafeBag::CDecPkcs12SafeBag()
28 EXPORT_C CDecPkcs12SafeBag* CDecPkcs12SafeBag::NewL(const TDesC8& aSafeBagData)
30 TASN1DecGeneric seqGen(aSafeBagData);
33 // Check if this is a Sequence
34 if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
36 User::Leave(KErrArgument);
40 CArrayPtrFlat<TASN1DecGeneric>* safeBagSequence = seq.DecodeDERLC(seqGen);
42 // Check for BagId, BagId is an ObjectIdentifier
43 const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0);
44 if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal)
46 User::Leave(KErrArgument);
48 TASN1DecObjectIdentifier oid;
49 HBufC* bagId = oid.DecodeDERL(*safeBagSequenceAt0);
50 CleanupStack::PushL(bagId);
51 CDecPkcs12SafeBag* safeBag = NULL;
53 // If BagType is a KeyBag.
54 if( *bagId == KPkcs12KeyBagOID )
56 safeBag = CDecPkcs12KeyBag::NewL(aSafeBagData);
58 // If BagType is a Shrouded KeyBag.
59 else if( *bagId == KPkcs12ShroudedKeyBagOID )
61 safeBag = CDecPkcs12ShroudedKeyBag::NewL(aSafeBagData);
63 // If bagType is a CertBag.
64 else if( *bagId == KPkcs12CertBagOID )
66 safeBag = CDecPkcs12CertBag::NewL(aSafeBagData);
68 // If Bag Type is a SafeContentsBag.
69 else if( *bagId == KPkcs12SafeContentsBagOID )
71 safeBag = CDecPkcs12SafeContentsBag::NewL(aSafeBagData);
73 // If Bag Type is a CrlBag.
74 else if ( *bagId == KPkcs12CrlBagOID )
76 safeBag = new(ELeave) CDecPkcs12SafeBag();
77 CleanupStack::PushL(safeBag);
78 safeBag->ConstructL(aSafeBagData);
79 CleanupStack::Pop(safeBag);
81 // If Bag Type is a Secret Bag.
82 else if ( *bagId == KPkcs12SecretBagOID )
84 safeBag = new(ELeave) CDecPkcs12SafeBag();
85 CleanupStack::PushL(safeBag);
86 safeBag->ConstructL(aSafeBagData);
87 CleanupStack::Pop(safeBag);
91 User::Leave(KErrNotSupported);
93 CleanupStack::PopAndDestroy(2,safeBagSequence); // safeBagSequence, bagId.
97 EXPORT_C CDecPkcs12SafeBag::~CDecPkcs12SafeBag()
99 iBagAttributes.ResetAndDestroy();
100 iBagAttributes.Close();
103 void CDecPkcs12SafeBag::ConstructL(const TDesC8& aSafeBagData)
105 // This is SafeBag Sequence
106 TASN1DecGeneric seqGen(aSafeBagData);
109 // Check if this is a Sequence
110 if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
112 User::Leave(KErrArgument);
115 TASN1DecSequence seq;
116 CArrayPtrFlat<TASN1DecGeneric>* safeBagSequence = seq.DecodeDERLC(seqGen);
117 const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0);
118 // Obtain the BagId from the SafeBag Sequence
119 if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal)
121 User::Leave(KErrArgument);
124 TASN1DecObjectIdentifier oid;
125 HBufC* bagId = oid.DecodeDERL(*(safeBagSequence->At(0)));
127 // If BagType is a KeyBag
128 if(*bagId == KPkcs12KeyBagOID)
132 // If BagType is a Shrouded KeyBag
133 else if( *bagId == KPkcs12ShroudedKeyBagOID )
135 iBagId = EShroudedKeyBag;
137 // If bagType is a CertBag
138 else if( *bagId == KPkcs12CertBagOID )
142 else if ( *bagId == KPkcs12CrlBagOID )
146 else if ( *bagId == KPkcs12SecretBagOID )
150 // If Bag Type is a SafeContentsBag
151 else if( *bagId == KPkcs12SafeContentsBagOID )
153 iBagId = ESafeContentsBag;
157 User::Leave(KErrNotSupported);
160 // Obtain the BagValue from the SafeBag Sequence. The SafeBag contains a KeyBag
161 // or a ShroudedKeyBag.
162 const TASN1DecGeneric* safeBagSequenceAt1 = safeBagSequence->At(1);
163 if (safeBagSequenceAt1->Tag() == EASN1EOC || safeBagSequenceAt1->Class() == EContextSpecific)
165 TASN1DecGeneric seqGen(safeBagSequence->At(1)->GetContentDER());
168 iBagValue.Set(seqGen.Encoding());
172 User::Leave(KErrArgument);
174 // Obtain the BagAttribute from the SafeBag Sequence. ATTRIBUTES ARE OPTIONAL
175 if (safeBagSequence->Count() == 3)
177 TASN1DecGeneric seqGen(*(safeBagSequence->At(2)));
180 // Check if this is a Set
181 if (seqGen.Tag() != EASN1Set || seqGen.Class() != EUniversal)
183 User::Leave(KErrArgument);
187 CArrayPtrFlat<TASN1DecGeneric>* attributeSet = decSet.NewDERLC((safeBagSequence->At(2)->Encoding()));
189 TInt attributeSetCount = attributeSet->Count();
190 for ( TInt index = 0; index < attributeSetCount; index++ )
192 const TDesC8& attribute(attributeSet->At(index)->Encoding());
193 CDecPkcs12Attribute* bagAttribute = CDecPkcs12Attribute::NewL(attribute);
194 CleanupStack::PushL(bagAttribute);
195 iBagAttributes.AppendL(bagAttribute);
196 CleanupStack::Pop(bagAttribute);
198 CleanupStack::PopAndDestroy(attributeSet); // attributeSet
200 CleanupStack::PopAndDestroy(safeBagSequence); // safeBagSequence
203 EXPORT_C CDecPkcs12SafeBag::TBagId CDecPkcs12SafeBag::BagID() const
208 EXPORT_C const TDesC8& CDecPkcs12SafeBag::BagValue() const
213 EXPORT_C const RPointerArray<CDecPkcs12Attribute>& CDecPkcs12SafeBag::BagAttributes() const
215 return iBagAttributes;