sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "pkcs12safebag.h" sl@0: #include sl@0: sl@0: using namespace PKCS12; sl@0: sl@0: CDecPkcs12SafeBag::CDecPkcs12SafeBag() sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDecPkcs12SafeBag* CDecPkcs12SafeBag::NewL(const TDesC8& aSafeBagData) sl@0: { sl@0: TASN1DecGeneric seqGen(aSafeBagData); sl@0: seqGen.InitL(); sl@0: sl@0: // Check if this is a Sequence sl@0: if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TASN1DecSequence seq; sl@0: CArrayPtrFlat* safeBagSequence = seq.DecodeDERLC(seqGen); sl@0: sl@0: // Check for BagId, BagId is an ObjectIdentifier sl@0: const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0); sl@0: if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: TASN1DecObjectIdentifier oid; sl@0: HBufC* bagId = oid.DecodeDERL(*safeBagSequenceAt0); sl@0: CleanupStack::PushL(bagId); sl@0: CDecPkcs12SafeBag* safeBag = NULL; sl@0: sl@0: // If BagType is a KeyBag. sl@0: if( *bagId == KPkcs12KeyBagOID ) sl@0: { sl@0: safeBag = CDecPkcs12KeyBag::NewL(aSafeBagData); sl@0: } sl@0: // If BagType is a Shrouded KeyBag. sl@0: else if( *bagId == KPkcs12ShroudedKeyBagOID ) sl@0: { sl@0: safeBag = CDecPkcs12ShroudedKeyBag::NewL(aSafeBagData); sl@0: } sl@0: // If bagType is a CertBag. sl@0: else if( *bagId == KPkcs12CertBagOID ) sl@0: { sl@0: safeBag = CDecPkcs12CertBag::NewL(aSafeBagData); sl@0: } sl@0: // If Bag Type is a SafeContentsBag. sl@0: else if( *bagId == KPkcs12SafeContentsBagOID ) sl@0: { sl@0: safeBag = CDecPkcs12SafeContentsBag::NewL(aSafeBagData); sl@0: } sl@0: // If Bag Type is a CrlBag. sl@0: else if ( *bagId == KPkcs12CrlBagOID ) sl@0: { sl@0: safeBag = new(ELeave) CDecPkcs12SafeBag(); sl@0: CleanupStack::PushL(safeBag); sl@0: safeBag->ConstructL(aSafeBagData); sl@0: CleanupStack::Pop(safeBag); sl@0: } sl@0: // If Bag Type is a Secret Bag. sl@0: else if ( *bagId == KPkcs12SecretBagOID ) sl@0: { sl@0: safeBag = new(ELeave) CDecPkcs12SafeBag(); sl@0: CleanupStack::PushL(safeBag); sl@0: safeBag->ConstructL(aSafeBagData); sl@0: CleanupStack::Pop(safeBag); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: CleanupStack::PopAndDestroy(2,safeBagSequence); // safeBagSequence, bagId. sl@0: return safeBag; sl@0: } sl@0: sl@0: EXPORT_C CDecPkcs12SafeBag::~CDecPkcs12SafeBag() sl@0: { sl@0: iBagAttributes.ResetAndDestroy(); sl@0: iBagAttributes.Close(); sl@0: } sl@0: sl@0: void CDecPkcs12SafeBag::ConstructL(const TDesC8& aSafeBagData) sl@0: { sl@0: // This is SafeBag Sequence sl@0: TASN1DecGeneric seqGen(aSafeBagData); sl@0: seqGen.InitL(); sl@0: sl@0: // Check if this is a Sequence sl@0: if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TASN1DecSequence seq; sl@0: CArrayPtrFlat* safeBagSequence = seq.DecodeDERLC(seqGen); sl@0: const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0); sl@0: // Obtain the BagId from the SafeBag Sequence sl@0: if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TASN1DecObjectIdentifier oid; sl@0: HBufC* bagId = oid.DecodeDERL(*(safeBagSequence->At(0))); sl@0: sl@0: // If BagType is a KeyBag sl@0: if(*bagId == KPkcs12KeyBagOID) sl@0: { sl@0: iBagId = EKeyBag; sl@0: } sl@0: // If BagType is a Shrouded KeyBag sl@0: else if( *bagId == KPkcs12ShroudedKeyBagOID ) sl@0: { sl@0: iBagId = EShroudedKeyBag; sl@0: } sl@0: // If bagType is a CertBag sl@0: else if( *bagId == KPkcs12CertBagOID ) sl@0: { sl@0: iBagId = ECertBag; sl@0: } sl@0: else if ( *bagId == KPkcs12CrlBagOID ) sl@0: { sl@0: iBagId = ECrlBag; sl@0: } sl@0: else if ( *bagId == KPkcs12SecretBagOID ) sl@0: { sl@0: iBagId = ESecretBag; sl@0: } sl@0: // If Bag Type is a SafeContentsBag sl@0: else if( *bagId == KPkcs12SafeContentsBagOID ) sl@0: { sl@0: iBagId = ESafeContentsBag; sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: delete bagId; sl@0: // Obtain the BagValue from the SafeBag Sequence. The SafeBag contains a KeyBag sl@0: // or a ShroudedKeyBag. sl@0: const TASN1DecGeneric* safeBagSequenceAt1 = safeBagSequence->At(1); sl@0: if (safeBagSequenceAt1->Tag() == EASN1EOC || safeBagSequenceAt1->Class() == EContextSpecific) sl@0: { sl@0: TASN1DecGeneric seqGen(safeBagSequence->At(1)->GetContentDER()); sl@0: seqGen.InitL(); sl@0: sl@0: iBagValue.Set(seqGen.Encoding()); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: // Obtain the BagAttribute from the SafeBag Sequence. ATTRIBUTES ARE OPTIONAL sl@0: if (safeBagSequence->Count() == 3) sl@0: { sl@0: TASN1DecGeneric seqGen(*(safeBagSequence->At(2))); sl@0: seqGen.InitL(); sl@0: sl@0: // Check if this is a Set sl@0: if (seqGen.Tag() != EASN1Set || seqGen.Class() != EUniversal) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: // Set sl@0: TASN1DecSet decSet; sl@0: CArrayPtrFlat* attributeSet = decSet.NewDERLC((safeBagSequence->At(2)->Encoding())); sl@0: sl@0: TInt attributeSetCount = attributeSet->Count(); sl@0: for ( TInt index = 0; index < attributeSetCount; index++ ) sl@0: { sl@0: const TDesC8& attribute(attributeSet->At(index)->Encoding()); sl@0: CDecPkcs12Attribute* bagAttribute = CDecPkcs12Attribute::NewL(attribute); sl@0: CleanupStack::PushL(bagAttribute); sl@0: iBagAttributes.AppendL(bagAttribute); sl@0: CleanupStack::Pop(bagAttribute); sl@0: } sl@0: CleanupStack::PopAndDestroy(attributeSet); // attributeSet sl@0: } sl@0: CleanupStack::PopAndDestroy(safeBagSequence); // safeBagSequence sl@0: } sl@0: sl@0: EXPORT_C CDecPkcs12SafeBag::TBagId CDecPkcs12SafeBag::BagID() const sl@0: { sl@0: return iBagId; sl@0: } sl@0: sl@0: EXPORT_C const TDesC8& CDecPkcs12SafeBag::BagValue() const sl@0: { sl@0: return iBagValue; sl@0: } sl@0: sl@0: EXPORT_C const RPointerArray& CDecPkcs12SafeBag::BagAttributes() const sl@0: { sl@0: return iBagAttributes; sl@0: } sl@0: