cryptoservices/certificateandkeymgmt/pkcs12/pkcs12safebag.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2005-2009 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 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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "pkcs12safebag.h"
       
    20 #include <pkcs12bags.h>
       
    21 
       
    22 using namespace PKCS12;
       
    23 
       
    24 CDecPkcs12SafeBag::CDecPkcs12SafeBag()
       
    25 	{
       
    26 	}
       
    27 
       
    28 EXPORT_C CDecPkcs12SafeBag* CDecPkcs12SafeBag::NewL(const TDesC8& aSafeBagData)
       
    29 	{
       
    30 	TASN1DecGeneric seqGen(aSafeBagData);
       
    31 	seqGen.InitL();
       
    32 	
       
    33 	// Check if this is a Sequence
       
    34 	if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
       
    35 		{
       
    36 		User::Leave(KErrArgument);
       
    37 		}
       
    38 	
       
    39 	TASN1DecSequence seq;
       
    40 	CArrayPtrFlat<TASN1DecGeneric>* safeBagSequence = seq.DecodeDERLC(seqGen);
       
    41 
       
    42 	// Check for BagId, BagId is an ObjectIdentifier
       
    43 	const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0);
       
    44 	if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal)
       
    45 		{
       
    46 		User::Leave(KErrArgument);
       
    47 		}
       
    48 	TASN1DecObjectIdentifier oid;
       
    49   	HBufC* bagId = oid.DecodeDERL(*safeBagSequenceAt0);
       
    50 	CleanupStack::PushL(bagId);
       
    51 	CDecPkcs12SafeBag* safeBag = NULL;
       
    52 	
       
    53 	// If BagType is a KeyBag.	
       
    54 	if( *bagId == KPkcs12KeyBagOID )
       
    55 		{
       
    56 		safeBag = CDecPkcs12KeyBag::NewL(aSafeBagData);
       
    57 		}
       
    58 	// If BagType is a Shrouded KeyBag.
       
    59 	else if( *bagId == KPkcs12ShroudedKeyBagOID )
       
    60 		{
       
    61 		safeBag = CDecPkcs12ShroudedKeyBag::NewL(aSafeBagData);
       
    62 		}
       
    63 	// If bagType is a CertBag.
       
    64 	else if( *bagId == KPkcs12CertBagOID )
       
    65 		{
       
    66 		safeBag = CDecPkcs12CertBag::NewL(aSafeBagData);
       
    67 		}
       
    68 	// If Bag Type is a SafeContentsBag.
       
    69 	else if( *bagId == KPkcs12SafeContentsBagOID )
       
    70 		{
       
    71 		safeBag = CDecPkcs12SafeContentsBag::NewL(aSafeBagData);
       
    72 		}
       
    73 	// If Bag Type is a CrlBag.
       
    74 	else if ( *bagId == KPkcs12CrlBagOID )
       
    75 		{
       
    76 		safeBag = new(ELeave) CDecPkcs12SafeBag();
       
    77 		CleanupStack::PushL(safeBag);
       
    78 		safeBag->ConstructL(aSafeBagData);
       
    79 		CleanupStack::Pop(safeBag);
       
    80 		}
       
    81 	// If Bag Type is a Secret Bag.
       
    82 	else if ( *bagId == KPkcs12SecretBagOID )
       
    83 		{
       
    84 		safeBag = new(ELeave) CDecPkcs12SafeBag();
       
    85 		CleanupStack::PushL(safeBag);
       
    86 		safeBag->ConstructL(aSafeBagData);
       
    87 		CleanupStack::Pop(safeBag);
       
    88 		}
       
    89 	else
       
    90 		{
       
    91 		User::Leave(KErrNotSupported);	
       
    92 		}	
       
    93 	CleanupStack::PopAndDestroy(2,safeBagSequence); // safeBagSequence, bagId.
       
    94 	return safeBag;
       
    95 	}
       
    96 
       
    97 EXPORT_C CDecPkcs12SafeBag::~CDecPkcs12SafeBag()
       
    98 	{
       
    99 	iBagAttributes.ResetAndDestroy();
       
   100 	iBagAttributes.Close();
       
   101 	}
       
   102 	
       
   103 void CDecPkcs12SafeBag::ConstructL(const TDesC8& aSafeBagData)
       
   104 	{
       
   105 	// This is SafeBag Sequence
       
   106 	TASN1DecGeneric seqGen(aSafeBagData);
       
   107 	seqGen.InitL();
       
   108 	
       
   109 	// Check if this is a Sequence
       
   110 	if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
       
   111 		{
       
   112 		User::Leave(KErrArgument);
       
   113 		}
       
   114 	
       
   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)
       
   120 		{
       
   121 		User::Leave(KErrArgument);
       
   122 		}
       
   123 		
       
   124 	TASN1DecObjectIdentifier oid;
       
   125 	HBufC* bagId = oid.DecodeDERL(*(safeBagSequence->At(0)));
       
   126 
       
   127 	// If BagType is a KeyBag	
       
   128 	if(*bagId == KPkcs12KeyBagOID)
       
   129 		{
       
   130 		iBagId = EKeyBag;
       
   131 		}
       
   132 	// If BagType is a Shrouded KeyBag	
       
   133 	else if( *bagId == KPkcs12ShroudedKeyBagOID )
       
   134 		{
       
   135 		iBagId = EShroudedKeyBag;
       
   136 		}
       
   137 	// If bagType is a CertBag
       
   138 	else if( *bagId == KPkcs12CertBagOID )
       
   139 		{
       
   140 		iBagId = ECertBag;
       
   141 		}
       
   142 	else if ( *bagId == KPkcs12CrlBagOID )
       
   143 		{
       
   144 		iBagId = ECrlBag;
       
   145 		}
       
   146 	else if ( *bagId == KPkcs12SecretBagOID )
       
   147 		{
       
   148 		iBagId = ESecretBag;
       
   149 		}
       
   150 	// If Bag Type is a SafeContentsBag
       
   151 	else if( *bagId == KPkcs12SafeContentsBagOID )
       
   152 		{
       
   153 		iBagId = ESafeContentsBag;
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		User::Leave(KErrNotSupported);	
       
   158 		}	
       
   159 	delete bagId;	 
       
   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)
       
   164 		{
       
   165 		TASN1DecGeneric seqGen(safeBagSequence->At(1)->GetContentDER());
       
   166 		seqGen.InitL();
       
   167 		
       
   168 		iBagValue.Set(seqGen.Encoding());
       
   169 		}
       
   170    	else
       
   171 		{
       
   172 		User::Leave(KErrArgument);
       
   173 		}
       
   174 	// Obtain the BagAttribute from the SafeBag Sequence. ATTRIBUTES ARE OPTIONAL
       
   175 	if (safeBagSequence->Count() == 3)
       
   176 		{
       
   177 		TASN1DecGeneric seqGen(*(safeBagSequence->At(2)));
       
   178 	    seqGen.InitL();
       
   179 	    
       
   180 		// Check if this is a Set
       
   181 		if (seqGen.Tag() != EASN1Set || seqGen.Class() != EUniversal)
       
   182 			{
       
   183 			User::Leave(KErrArgument);
       
   184 			}   
       
   185 		// Set
       
   186 		TASN1DecSet decSet;
       
   187         CArrayPtrFlat<TASN1DecGeneric>* attributeSet = decSet.NewDERLC((safeBagSequence->At(2)->Encoding()));
       
   188                 
       
   189         TInt attributeSetCount = attributeSet->Count();
       
   190         for ( TInt index = 0; index < attributeSetCount; index++ )
       
   191         	{
       
   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);
       
   197          	}
       
   198         CleanupStack::PopAndDestroy(attributeSet); // attributeSet
       
   199         }
       
   200 	CleanupStack::PopAndDestroy(safeBagSequence); // safeBagSequence
       
   201 	}
       
   202 	
       
   203 EXPORT_C CDecPkcs12SafeBag::TBagId CDecPkcs12SafeBag::BagID() const
       
   204 	{
       
   205 	return iBagId;
       
   206 	}
       
   207  
       
   208 EXPORT_C const TDesC8& CDecPkcs12SafeBag::BagValue() const
       
   209 	{
       
   210 	return iBagValue;
       
   211 	}
       
   212 
       
   213 EXPORT_C const RPointerArray<CDecPkcs12Attribute>& CDecPkcs12SafeBag::BagAttributes() const
       
   214 	{
       
   215 	return iBagAttributes;
       
   216 	}