os/security/cryptoservices/certificateandkeymgmt/pkcs12/pkcs12safebag.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "pkcs12safebag.h"
sl@0
    20
#include <pkcs12bags.h>
sl@0
    21
sl@0
    22
using namespace PKCS12;
sl@0
    23
sl@0
    24
CDecPkcs12SafeBag::CDecPkcs12SafeBag()
sl@0
    25
	{
sl@0
    26
	}
sl@0
    27
sl@0
    28
EXPORT_C CDecPkcs12SafeBag* CDecPkcs12SafeBag::NewL(const TDesC8& aSafeBagData)
sl@0
    29
	{
sl@0
    30
	TASN1DecGeneric seqGen(aSafeBagData);
sl@0
    31
	seqGen.InitL();
sl@0
    32
	
sl@0
    33
	// Check if this is a Sequence
sl@0
    34
	if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
sl@0
    35
		{
sl@0
    36
		User::Leave(KErrArgument);
sl@0
    37
		}
sl@0
    38
	
sl@0
    39
	TASN1DecSequence seq;
sl@0
    40
	CArrayPtrFlat<TASN1DecGeneric>* safeBagSequence = seq.DecodeDERLC(seqGen);
sl@0
    41
sl@0
    42
	// Check for BagId, BagId is an ObjectIdentifier
sl@0
    43
	const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0);
sl@0
    44
	if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal)
sl@0
    45
		{
sl@0
    46
		User::Leave(KErrArgument);
sl@0
    47
		}
sl@0
    48
	TASN1DecObjectIdentifier oid;
sl@0
    49
  	HBufC* bagId = oid.DecodeDERL(*safeBagSequenceAt0);
sl@0
    50
	CleanupStack::PushL(bagId);
sl@0
    51
	CDecPkcs12SafeBag* safeBag = NULL;
sl@0
    52
	
sl@0
    53
	// If BagType is a KeyBag.	
sl@0
    54
	if( *bagId == KPkcs12KeyBagOID )
sl@0
    55
		{
sl@0
    56
		safeBag = CDecPkcs12KeyBag::NewL(aSafeBagData);
sl@0
    57
		}
sl@0
    58
	// If BagType is a Shrouded KeyBag.
sl@0
    59
	else if( *bagId == KPkcs12ShroudedKeyBagOID )
sl@0
    60
		{
sl@0
    61
		safeBag = CDecPkcs12ShroudedKeyBag::NewL(aSafeBagData);
sl@0
    62
		}
sl@0
    63
	// If bagType is a CertBag.
sl@0
    64
	else if( *bagId == KPkcs12CertBagOID )
sl@0
    65
		{
sl@0
    66
		safeBag = CDecPkcs12CertBag::NewL(aSafeBagData);
sl@0
    67
		}
sl@0
    68
	// If Bag Type is a SafeContentsBag.
sl@0
    69
	else if( *bagId == KPkcs12SafeContentsBagOID )
sl@0
    70
		{
sl@0
    71
		safeBag = CDecPkcs12SafeContentsBag::NewL(aSafeBagData);
sl@0
    72
		}
sl@0
    73
	// If Bag Type is a CrlBag.
sl@0
    74
	else if ( *bagId == KPkcs12CrlBagOID )
sl@0
    75
		{
sl@0
    76
		safeBag = new(ELeave) CDecPkcs12SafeBag();
sl@0
    77
		CleanupStack::PushL(safeBag);
sl@0
    78
		safeBag->ConstructL(aSafeBagData);
sl@0
    79
		CleanupStack::Pop(safeBag);
sl@0
    80
		}
sl@0
    81
	// If Bag Type is a Secret Bag.
sl@0
    82
	else if ( *bagId == KPkcs12SecretBagOID )
sl@0
    83
		{
sl@0
    84
		safeBag = new(ELeave) CDecPkcs12SafeBag();
sl@0
    85
		CleanupStack::PushL(safeBag);
sl@0
    86
		safeBag->ConstructL(aSafeBagData);
sl@0
    87
		CleanupStack::Pop(safeBag);
sl@0
    88
		}
sl@0
    89
	else
sl@0
    90
		{
sl@0
    91
		User::Leave(KErrNotSupported);	
sl@0
    92
		}	
sl@0
    93
	CleanupStack::PopAndDestroy(2,safeBagSequence); // safeBagSequence, bagId.
sl@0
    94
	return safeBag;
sl@0
    95
	}
sl@0
    96
sl@0
    97
EXPORT_C CDecPkcs12SafeBag::~CDecPkcs12SafeBag()
sl@0
    98
	{
sl@0
    99
	iBagAttributes.ResetAndDestroy();
sl@0
   100
	iBagAttributes.Close();
sl@0
   101
	}
sl@0
   102
	
sl@0
   103
void CDecPkcs12SafeBag::ConstructL(const TDesC8& aSafeBagData)
sl@0
   104
	{
sl@0
   105
	// This is SafeBag Sequence
sl@0
   106
	TASN1DecGeneric seqGen(aSafeBagData);
sl@0
   107
	seqGen.InitL();
sl@0
   108
	
sl@0
   109
	// Check if this is a Sequence
sl@0
   110
	if (seqGen.Tag() != EASN1Sequence || seqGen.Class() != EUniversal)
sl@0
   111
		{
sl@0
   112
		User::Leave(KErrArgument);
sl@0
   113
		}
sl@0
   114
	
sl@0
   115
	TASN1DecSequence seq;
sl@0
   116
	CArrayPtrFlat<TASN1DecGeneric>* safeBagSequence = seq.DecodeDERLC(seqGen);
sl@0
   117
	const TASN1DecGeneric* safeBagSequenceAt0 = safeBagSequence->At(0);
sl@0
   118
	// Obtain the BagId from the SafeBag Sequence
sl@0
   119
	 if (safeBagSequenceAt0->Tag() != EASN1ObjectIdentifier || safeBagSequenceAt0->Class() != EUniversal)
sl@0
   120
		{
sl@0
   121
		User::Leave(KErrArgument);
sl@0
   122
		}
sl@0
   123
		
sl@0
   124
	TASN1DecObjectIdentifier oid;
sl@0
   125
	HBufC* bagId = oid.DecodeDERL(*(safeBagSequence->At(0)));
sl@0
   126
sl@0
   127
	// If BagType is a KeyBag	
sl@0
   128
	if(*bagId == KPkcs12KeyBagOID)
sl@0
   129
		{
sl@0
   130
		iBagId = EKeyBag;
sl@0
   131
		}
sl@0
   132
	// If BagType is a Shrouded KeyBag	
sl@0
   133
	else if( *bagId == KPkcs12ShroudedKeyBagOID )
sl@0
   134
		{
sl@0
   135
		iBagId = EShroudedKeyBag;
sl@0
   136
		}
sl@0
   137
	// If bagType is a CertBag
sl@0
   138
	else if( *bagId == KPkcs12CertBagOID )
sl@0
   139
		{
sl@0
   140
		iBagId = ECertBag;
sl@0
   141
		}
sl@0
   142
	else if ( *bagId == KPkcs12CrlBagOID )
sl@0
   143
		{
sl@0
   144
		iBagId = ECrlBag;
sl@0
   145
		}
sl@0
   146
	else if ( *bagId == KPkcs12SecretBagOID )
sl@0
   147
		{
sl@0
   148
		iBagId = ESecretBag;
sl@0
   149
		}
sl@0
   150
	// If Bag Type is a SafeContentsBag
sl@0
   151
	else if( *bagId == KPkcs12SafeContentsBagOID )
sl@0
   152
		{
sl@0
   153
		iBagId = ESafeContentsBag;
sl@0
   154
		}
sl@0
   155
	else
sl@0
   156
		{
sl@0
   157
		User::Leave(KErrNotSupported);	
sl@0
   158
		}	
sl@0
   159
	delete bagId;	 
sl@0
   160
	// Obtain the BagValue from the SafeBag Sequence. The SafeBag contains a KeyBag 
sl@0
   161
	// or a ShroudedKeyBag.
sl@0
   162
	const TASN1DecGeneric* safeBagSequenceAt1 = safeBagSequence->At(1);
sl@0
   163
	if (safeBagSequenceAt1->Tag() == EASN1EOC || safeBagSequenceAt1->Class() == EContextSpecific)
sl@0
   164
		{
sl@0
   165
		TASN1DecGeneric seqGen(safeBagSequence->At(1)->GetContentDER());
sl@0
   166
		seqGen.InitL();
sl@0
   167
		
sl@0
   168
		iBagValue.Set(seqGen.Encoding());
sl@0
   169
		}
sl@0
   170
   	else
sl@0
   171
		{
sl@0
   172
		User::Leave(KErrArgument);
sl@0
   173
		}
sl@0
   174
	// Obtain the BagAttribute from the SafeBag Sequence. ATTRIBUTES ARE OPTIONAL
sl@0
   175
	if (safeBagSequence->Count() == 3)
sl@0
   176
		{
sl@0
   177
		TASN1DecGeneric seqGen(*(safeBagSequence->At(2)));
sl@0
   178
	    seqGen.InitL();
sl@0
   179
	    
sl@0
   180
		// Check if this is a Set
sl@0
   181
		if (seqGen.Tag() != EASN1Set || seqGen.Class() != EUniversal)
sl@0
   182
			{
sl@0
   183
			User::Leave(KErrArgument);
sl@0
   184
			}   
sl@0
   185
		// Set
sl@0
   186
		TASN1DecSet decSet;
sl@0
   187
        CArrayPtrFlat<TASN1DecGeneric>* attributeSet = decSet.NewDERLC((safeBagSequence->At(2)->Encoding()));
sl@0
   188
                
sl@0
   189
        TInt attributeSetCount = attributeSet->Count();
sl@0
   190
        for ( TInt index = 0; index < attributeSetCount; index++ )
sl@0
   191
        	{
sl@0
   192
        	const TDesC8& attribute(attributeSet->At(index)->Encoding());
sl@0
   193
        	CDecPkcs12Attribute* bagAttribute = CDecPkcs12Attribute::NewL(attribute);
sl@0
   194
        	CleanupStack::PushL(bagAttribute);
sl@0
   195
         	iBagAttributes.AppendL(bagAttribute);
sl@0
   196
         	CleanupStack::Pop(bagAttribute);
sl@0
   197
         	}
sl@0
   198
        CleanupStack::PopAndDestroy(attributeSet); // attributeSet
sl@0
   199
        }
sl@0
   200
	CleanupStack::PopAndDestroy(safeBagSequence); // safeBagSequence
sl@0
   201
	}
sl@0
   202
	
sl@0
   203
EXPORT_C CDecPkcs12SafeBag::TBagId CDecPkcs12SafeBag::BagID() const
sl@0
   204
	{
sl@0
   205
	return iBagId;
sl@0
   206
	}
sl@0
   207
 
sl@0
   208
EXPORT_C const TDesC8& CDecPkcs12SafeBag::BagValue() const
sl@0
   209
	{
sl@0
   210
	return iBagValue;
sl@0
   211
	}
sl@0
   212
sl@0
   213
EXPORT_C const RPointerArray<CDecPkcs12Attribute>& CDecPkcs12SafeBag::BagAttributes() const
sl@0
   214
	{
sl@0
   215
	return iBagAttributes;
sl@0
   216
	}
sl@0
   217