cryptoservices/certificateandkeymgmt/pkcs7/cmscontentinfo.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2006-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 <cmscontentinfo.h>
       
    20 #include <asn1dec.h>
       
    21 #include <asn1enc.h> 
       
    22 #include "pkcs7asn1.h"
       
    23 #include "cmsutils.h"
       
    24 
       
    25 //
       
    26 //Implementation of CMS ContentInfo Identity
       
    27 //
       
    28 
       
    29 EXPORT_C CCmsContentInfo* CCmsContentInfo::NewLC(const TDesC8& aRawData)
       
    30 	{
       
    31 	CCmsContentInfo* self = new (ELeave) CCmsContentInfo();
       
    32 	CleanupStack::PushL(self);
       
    33 	self->ConstructL(aRawData);
       
    34 	return self;			
       
    35 	}
       
    36 
       
    37 EXPORT_C CCmsContentInfo* CCmsContentInfo::NewL(const TDesC8& aRawData)
       
    38 	{
       
    39 	CCmsContentInfo* self = NewLC(aRawData);
       
    40 	CleanupStack::Pop(self);
       
    41 	return self;			
       
    42 	}
       
    43 
       
    44 EXPORT_C CCmsContentInfo* CCmsContentInfo::NewL(TCmsContentInfoType aContentInfoType, const TDesC8& aContentData)
       
    45 	{
       
    46 	CCmsContentInfo* self = NewLC(aContentInfoType, aContentData);
       
    47 	CleanupStack::Pop(self);
       
    48 	return self;					
       
    49 	}
       
    50 
       
    51 EXPORT_C CCmsContentInfo* CCmsContentInfo::NewLC(TCmsContentInfoType aContentInfoType, const TDesC8& aContentData)
       
    52 	{
       
    53 	CCmsContentInfo* self = new (ELeave) CCmsContentInfo(aContentInfoType, aContentData);
       
    54 	CleanupStack::PushL(self);
       
    55 	return self;					
       
    56 	}
       
    57 	
       
    58 EXPORT_C CCmsContentInfo::~CCmsContentInfo()
       
    59 	{
       
    60 		
       
    61 	}
       
    62 CCmsContentInfo::CCmsContentInfo()
       
    63 	{
       
    64 	}
       
    65 
       
    66 CCmsContentInfo::CCmsContentInfo(TCmsContentInfoType aContentInfoType, const TDesC8& aContentData)
       
    67 :iContentType(aContentInfoType)
       
    68 	{
       
    69 	iContentData.Set(aContentData);
       
    70 	}
       
    71 
       
    72 void CCmsContentInfo::ConstructL(const TDesC8& aRawData)
       
    73 	{
       
    74 	const TInt minItems = 2;	// Must have OID
       
    75 	const TInt maxItems = 2;	// Must have data
       
    76 	CArrayPtr<TASN1DecGeneric>* contentInfo = PKCS7ASN1::DecodeSequenceLC(aRawData, minItems, maxItems);
       
    77 	
       
    78 	//Decode Content Type	
       
    79 	iContentType=(TCmsContentInfoType)(CmsUtils::DecodeContentTypeL(contentInfo->At(0)));
       
    80 
       
    81 	//Decode Content Data
       
    82 	const TASN1DecGeneric* contentInfoAt1 = contentInfo->At(1);
       
    83 	if ( contentInfoAt1->Tag() == 0 || contentInfoAt1->Class() == EContextSpecific )
       
    84 		{
       
    85 		TASN1DecGeneric decGen(contentInfoAt1->GetContentDER());
       
    86 		decGen.InitL();
       
    87 		if (iContentType==EContentTypeData)
       
    88 			{
       
    89 			if (decGen.Tag()!=EASN1OctetString || decGen.Class()!=EUniversal)
       
    90 				{
       
    91 				User::Leave(KErrArgument);	
       
    92 				}
       
    93 			else
       
    94 				{
       
    95 				iContentData.Set(decGen.GetContentDER());	
       
    96 				}
       
    97 			}
       
    98 		else
       
    99 			{
       
   100 			iContentData.Set(decGen.Encoding());				
       
   101 			}
       
   102 		}
       
   103 	else
       
   104 		{
       
   105 		User::Leave(KErrArgument);	
       
   106 		}
       
   107 		
       
   108 	CleanupStack::PopAndDestroy(contentInfo);
       
   109 	}
       
   110 	
       
   111 EXPORT_C CASN1EncSequence* CCmsContentInfo::EncodeASN1DERLC() const
       
   112 	{
       
   113 	// the root sequence contains the OID and the content data
       
   114 	CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   115 	
       
   116 	//Encode the OID
       
   117 	CASN1EncObjectIdentifier* oid = CmsUtils::EncodeContentTypeLC(iContentType);	 	
       
   118 	root->AddAndPopChildL(oid);
       
   119 
       
   120 	CASN1EncBase* enc(NULL);
       
   121 	if (iContentType==EContentTypeData)
       
   122 		{
       
   123 		enc=CASN1EncOctetString::NewL(iContentData);			
       
   124 		}
       
   125 	else
       
   126 		{
       
   127 		//Encode the  Content
       
   128 		//iContentData already encoded in sequence
       
   129 		//so just rebuild the structure
       
   130 		enc = CASN1EncEncoding::NewL(iContentData);
       
   131 		}
       
   132 	// Add [0] EXPLICT
       
   133 	CASN1EncExplicitTag* Enc=CASN1EncExplicitTag::NewLC(enc, 0);
       
   134 	root->AddAndPopChildL(Enc);					
       
   135 		
       
   136 	return root;
       
   137 	}
       
   138 
       
   139 EXPORT_C const TPtrC8 CCmsContentInfo::ContentData() const
       
   140 	{
       
   141 	return iContentData;	
       
   142 	}
       
   143 
       
   144 EXPORT_C TCmsContentInfoType CCmsContentInfo::ContentType() const
       
   145 	{
       
   146 	return iContentType;	
       
   147 	}
       
   148 
       
   149 	
       
   150 //	
       
   151 //Implementation of CMS EncapsulatedContentInfo Identity	
       
   152 //
       
   153 CEncapsulatedContentInfo* CEncapsulatedContentInfo::NewLC(const TDesC8& aRawData)
       
   154 	{
       
   155 	CEncapsulatedContentInfo* self = new (ELeave) CEncapsulatedContentInfo();
       
   156 	CleanupStack::PushL(self);
       
   157 	self->ConstructL(aRawData);
       
   158 	return self;			
       
   159 	}
       
   160 
       
   161 CEncapsulatedContentInfo* CEncapsulatedContentInfo::NewL(const TDesC8& aRawData)
       
   162 	{
       
   163 	CEncapsulatedContentInfo* self = NewLC(aRawData);
       
   164 	CleanupStack::Pop(self);
       
   165 	return self;			
       
   166 	}
       
   167 
       
   168 CEncapsulatedContentInfo* CEncapsulatedContentInfo::NewLC(TCmsContentInfoType aContentInfoType, TBool aIsEContentDataPresent, const TDesC8& aContentData)
       
   169 	{
       
   170 	CEncapsulatedContentInfo* self = new (ELeave) CEncapsulatedContentInfo(aContentInfoType, aIsEContentDataPresent, aContentData);
       
   171 	CleanupStack::PushL(self);
       
   172 	return self;					
       
   173 	}
       
   174 
       
   175 CEncapsulatedContentInfo* CEncapsulatedContentInfo::NewL(TCmsContentInfoType aContentInfoType, TBool aIsEContentDataPresent, const TDesC8& aContentData)
       
   176 	{
       
   177 	CEncapsulatedContentInfo* self = NewLC(aContentInfoType, aIsEContentDataPresent, aContentData);
       
   178 	CleanupStack::Pop(self);
       
   179 	return self;	
       
   180 	}
       
   181 
       
   182 CEncapsulatedContentInfo::CEncapsulatedContentInfo()
       
   183 	{
       
   184 	}
       
   185 
       
   186 CEncapsulatedContentInfo::CEncapsulatedContentInfo(TCmsContentInfoType aContentInfoType, TBool aIsEContentDataPresent, const TDesC8& aContentData)
       
   187 :iContentType(aContentInfoType),
       
   188  iIsContentDataPresent(aIsEContentDataPresent)
       
   189 	{
       
   190 	if (aIsEContentDataPresent)
       
   191 		{
       
   192 		iContentData.Set(aContentData);			
       
   193 		}
       
   194 	}
       
   195 
       
   196 void CEncapsulatedContentInfo::ConstructL(const TDesC8& aRawData)
       
   197 	{	
       
   198 	const TInt minItems = 1;	// Must have OID
       
   199 	const TInt maxItems = 2;	// Must have data
       
   200 	CArrayPtr<TASN1DecGeneric>* contentInfo = PKCS7ASN1::DecodeSequenceLC(aRawData, minItems, maxItems);
       
   201 	
       
   202 	//Decode Content Type	
       
   203 	iContentType=(TCmsContentInfoType)(CmsUtils::DecodeContentTypeL(contentInfo->At(0)));
       
   204 	
       
   205 	//Decode Content Data
       
   206 	if(contentInfo->Count() == 2)
       
   207 		{
       
   208 		iIsContentDataPresent=ETrue;
       
   209 		const TASN1DecGeneric* contentInfoAt1 = contentInfo->At(1);
       
   210 		
       
   211 		//Decode [0] Explicit
       
   212 		if ( contentInfoAt1->Tag() == 0 || contentInfoAt1->Class() == EContextSpecific )
       
   213 			{
       
   214 			//Decode Wrapper Octet string	
       
   215 			TASN1DecGeneric decGen(contentInfoAt1->GetContentDER());
       
   216 			decGen.InitL();
       
   217 			if(decGen.Tag() == EASN1OctetString && decGen.Class() == EUniversal)
       
   218 				{
       
   219 				iContentData.Set(decGen.GetContentDER());
       
   220 				}
       
   221 			else
       
   222 				{
       
   223 				//Wrapper is not an Octect String
       
   224 				User::Leave(KErrArgument);
       
   225 				}			
       
   226 			}
       
   227 		else
       
   228 			{
       
   229 			//Not [0] Explicit
       
   230 			User::Leave(KErrArgument);
       
   231 			}
       
   232 		}
       
   233 	else
       
   234 		{
       
   235 		//No optional data
       
   236 		iContentData.Set(KNullDesC8());	
       
   237 		}
       
   238 	CleanupStack::PopAndDestroy(contentInfo);
       
   239 	}	
       
   240 	
       
   241 	
       
   242 CASN1EncSequence* CEncapsulatedContentInfo::EncodeASN1DERLC() const
       
   243 	{
       
   244 	// the root sequence contains the OID and the content data
       
   245 	CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   246 	
       
   247 	//Encode the OID
       
   248 	CASN1EncObjectIdentifier* oid = CmsUtils::EncodeContentTypeLC(iContentType);	 	
       
   249 	root->AddAndPopChildL(oid);
       
   250 	
       
   251 	//Encode the Content
       
   252 	if (iIsContentDataPresent)
       
   253 		{
       
   254 		//Wrapper Octect String
       
   255 		CASN1EncOctetString* octetString=CASN1EncOctetString::NewL(iContentData);
       
   256 		
       
   257 		// Add [0] EXPLICT
       
   258 		CASN1EncExplicitTag* Enc=CASN1EncExplicitTag::NewLC(octetString, 0);
       
   259 		root->AddAndPopChildL(Enc);		
       
   260 		}
       
   261 			
       
   262 	return root;
       
   263 	}
       
   264 
       
   265 
       
   266 EXPORT_C TBool CEncapsulatedContentInfo::IsContentDataPresent() const
       
   267 	{
       
   268 	return iIsContentDataPresent;	
       
   269 	}
       
   270 
       
   271 EXPORT_C const TPtrC8 CEncapsulatedContentInfo::ContentData() const
       
   272 	{
       
   273 	return iContentData;	
       
   274 	}
       
   275 
       
   276 EXPORT_C TCmsContentInfoType CEncapsulatedContentInfo::ContentType() const
       
   277 	{
       
   278 	return iContentType;	
       
   279 	}