os/security/cryptoservices/certificateandkeymgmt/pkcs7/pkcs7contentinfo_v2.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) 2003-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 "pkcs7contentinfo_v2.h"
sl@0
    20
#include "pkcs7asn1.h"
sl@0
    21
sl@0
    22
EXPORT_C CPKCS7ContentInfo* CPKCS7ContentInfo::NewL(const TDesC8& aRawData)
sl@0
    23
	{
sl@0
    24
	CPKCS7ContentInfo* self = new (ELeave) CPKCS7ContentInfo();
sl@0
    25
	CleanupStack::PushL(self);
sl@0
    26
	self->ConstructL(aRawData);
sl@0
    27
	CleanupStack::Pop(self);
sl@0
    28
	return self;	
sl@0
    29
	}
sl@0
    30
sl@0
    31
CPKCS7ContentInfo::~CPKCS7ContentInfo()
sl@0
    32
	{
sl@0
    33
	}
sl@0
    34
sl@0
    35
EXPORT_C CPKCS7ContentInfo::TContentInfoType CPKCS7ContentInfo::ContentType() const
sl@0
    36
	{
sl@0
    37
	return iContentType;
sl@0
    38
	}
sl@0
    39
sl@0
    40
EXPORT_C const TPtrC8 CPKCS7ContentInfo::ContentData() const
sl@0
    41
	{
sl@0
    42
	return iContentData;	
sl@0
    43
	}
sl@0
    44
sl@0
    45
CPKCS7ContentInfo::CPKCS7ContentInfo(void)
sl@0
    46
	{
sl@0
    47
	}
sl@0
    48
sl@0
    49
void CPKCS7ContentInfo::ConstructL(const TDesC8& aRawData)
sl@0
    50
	{  
sl@0
    51
	const TInt minItems = 1;	// Must have OID
sl@0
    52
	const TInt maxItems = 2;	// May have data
sl@0
    53
sl@0
    54
	CArrayPtr<TASN1DecGeneric>* contentInfo = PKCS7ASN1::DecodeSequenceLC(aRawData, minItems, maxItems);
sl@0
    55
sl@0
    56
    const TASN1DecGeneric* contentInfoAt0 = contentInfo->At(0);
sl@0
    57
	// Checks its a oid id
sl@0
    58
	if(contentInfoAt0->Tag()==EASN1ObjectIdentifier || contentInfoAt0->Class() == EUniversal)
sl@0
    59
		{
sl@0
    60
		// Gets the oid
sl@0
    61
		TASN1DecObjectIdentifier oidDec;
sl@0
    62
		HBufC* oidVal = oidDec.DecodeDERL(*contentInfo->At(0));
sl@0
    63
		CleanupStack::PushL(oidVal);
sl@0
    64
		// Data itself is optional so make sure its there
sl@0
    65
		if(contentInfo->Count() == 2)
sl@0
    66
			{
sl@0
    67
			const TASN1DecGeneric* contentInfoAt1 = contentInfo->At(1);
sl@0
    68
			if ( contentInfoAt1->Tag() == 0 || contentInfoAt1->Class() == EContextSpecific )
sl@0
    69
				{
sl@0
    70
				TASN1DecGeneric decGen(contentInfoAt1->GetContentDER());
sl@0
    71
				decGen.InitL();
sl@0
    72
	            if(*oidVal == KPkcs7DataOID)
sl@0
    73
					{
sl@0
    74
					// ContentData is OctetString if ContentType is Data
sl@0
    75
					if(decGen.Tag() == EASN1OctetString)   
sl@0
    76
						{
sl@0
    77
						iContentData.Set(decGen.GetContentDER()); 
sl@0
    78
						}
sl@0
    79
					else
sl@0
    80
						{
sl@0
    81
						User::Leave(KErrArgument);
sl@0
    82
						}
sl@0
    83
					}
sl@0
    84
				else
sl@0
    85
					{
sl@0
    86
					iContentData.Set(decGen.Encoding());
sl@0
    87
					}
sl@0
    88
				}
sl@0
    89
			}
sl@0
    90
		else
sl@0
    91
			{
sl@0
    92
			iContentData.Set(KNullDesC8());
sl@0
    93
			}
sl@0
    94
		// Checks if it is data OID.
sl@0
    95
		if(*oidVal == KPkcs7DataOID)
sl@0
    96
			{
sl@0
    97
			// The Content Type is indicated by an Integer.
sl@0
    98
			// Here if Content Type is equal to Data then,it is represented by 1
sl@0
    99
			// 1 indicates the numeric value of last element in the PKCS7 Data OID.
sl@0
   100
			iContentType = EContentTypeData;
sl@0
   101
			}
sl@0
   102
		else if(*oidVal == KPkcs7SignedDataOID)
sl@0
   103
			{
sl@0
   104
			// The Content Type is indicated by an Integer.
sl@0
   105
			// Here if Content Type is equal to SignedData then,it is represented by 2
sl@0
   106
			// 2 indicates the numeric value of last element in the PKCS7 SignedData OID.
sl@0
   107
			iContentType = EContentTypeSignedData;
sl@0
   108
			}
sl@0
   109
		else if(*oidVal == KPkcs7EnvelopedDataOID) 
sl@0
   110
			{
sl@0
   111
			// The Content Type is indicated by an Integer.
sl@0
   112
			// Here if Content Type is equal to EnvelopedData then,it is represented by 3.
sl@0
   113
			// 3 indicates the numeric value of last element in the PKCS7 EnvelopedData OID.
sl@0
   114
			iContentType = EContentTypeEnvelopedData;
sl@0
   115
			}
sl@0
   116
		else if(*oidVal == KPkcs7SignedAndEnvelopedDataOID)
sl@0
   117
			{
sl@0
   118
			// The Content Type is indicated by an Integer.
sl@0
   119
			// Here if Content Type is equal to SignedAndEnvelopedData then,it is represented by 4.
sl@0
   120
			// 4 indicates the numeric value of last element in the PKCS7 SignedAndEnvelopedData OID.
sl@0
   121
			iContentType = EContentTypeSignedAndEnvelopedData;
sl@0
   122
			}
sl@0
   123
		else if(*oidVal == KPkcs7DigestedDataOID)
sl@0
   124
			{
sl@0
   125
			// The Content Type is indicated by an Integer.
sl@0
   126
			// Here if Content Type is equal to DigestedData then,it is represented by 5.
sl@0
   127
			// 5 indicates the numeric value of last element in the PKCS7 DigestedData OID.
sl@0
   128
			iContentType = EContentTypeDigestedData;
sl@0
   129
			}
sl@0
   130
		else if(*oidVal == KPkcs7EncryptedDataOID)
sl@0
   131
			{
sl@0
   132
			// The Content Type is indicated by an Integer.
sl@0
   133
			// Here if Content Type is equal to EncryptedData then,it is represented by 6
sl@0
   134
			// 6 indicates the numeric value of last element in the PKCS7 EncryptedData OID.
sl@0
   135
			iContentType = EContentTypeEncryptedData;
sl@0
   136
			}
sl@0
   137
		else
sl@0
   138
			{
sl@0
   139
			User::Leave(KErrNotSupported);
sl@0
   140
			}
sl@0
   141
		CleanupStack::PopAndDestroy(oidVal);
sl@0
   142
		}
sl@0
   143
	else
sl@0
   144
		{
sl@0
   145
		User::Leave(KErrArgument);
sl@0
   146
		}
sl@0
   147
	CleanupStack::PopAndDestroy(contentInfo);
sl@0
   148
	}