cryptoservices/certificateandkeymgmt/pkcs7/cmscertchoice.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 <cmscertchoice.h>
       
    20 #include "pkcs7asn1.h"
       
    21 #include <asn1dec.h>
       
    22 #include <x509cert.h>
       
    23 #include <asn1enc.h>
       
    24 #include "cmsutils.h"
       
    25 
       
    26 //Implementation of CMS Certificate Choice
       
    27 
       
    28 CCmsCertificateChoice* CCmsCertificateChoice::NewL(const TDesC8& aRawData)
       
    29 	{
       
    30 	CCmsCertificateChoice* self = new (ELeave) CCmsCertificateChoice();
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL(aRawData);
       
    33 	CleanupStack::Pop(self);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 CCmsCertificateChoice::~CCmsCertificateChoice()
       
    38 	{
       
    39 	delete iCertificate;
       
    40 	delete iEncodedAttributeCertificate;
       
    41 	}
       
    42 
       
    43 CCmsCertificateChoice::CCmsCertificateChoice()
       
    44 	{
       
    45 	}
       
    46 
       
    47 void CCmsCertificateChoice::ConstructL(const TDesC8& aRawData)
       
    48 	{
       
    49 	TASN1DecGeneric decGen(aRawData);
       
    50 	decGen.InitL();
       
    51 		
       
    52 	if (decGen.Tag()==EASN1Sequence && decGen.Class()==EUniversal)
       
    53 		{
       
    54 		// x509 certificates
       
    55 		iCertificateType=ECertificateX509;
       
    56 		iCertificate = CX509Certificate::NewL(aRawData);
       
    57 		}
       
    58 	else if (decGen.Tag()==1 && decGen.Class()==EContextSpecific)
       
    59 			{
       
    60 			// Attribute certificate
       
    61 			iCertificateType=ECertificateAttribute;
       
    62 			CASN1EncEncoding* enc=CASN1EncEncoding::NewLC(aRawData, EASN1Sequence, EUniversal);
       
    63 			iEncodedAttributeCertificate=CmsUtils::CreateDEREncodingLC(*enc);
       
    64 			CleanupStack::Pop(iEncodedAttributeCertificate);
       
    65 			CleanupStack::PopAndDestroy(enc);
       
    66 			}
       
    67 		else if (decGen.Tag()==0 && decGen.Class()==EContextSpecific)
       
    68 				{
       
    69 				// extended certificates not supported
       
    70 				iCertificateType=ECertificateExtendedCerificate;
       
    71 				User::Leave(KErrNotSupported);
       
    72 				}
       
    73 			else
       
    74 				{
       
    75 				User::Leave(KErrArgument);	
       
    76 				}		
       
    77 	}
       
    78 
       
    79 EXPORT_C const CX509Certificate& CCmsCertificateChoice::Certificate(void) const
       
    80 	{
       
    81 	return *iCertificate;
       
    82 	}
       
    83 
       
    84 CCmsCertificateChoice* CCmsCertificateChoice::NewLC(const CX509Certificate& aCertificate)
       
    85 	{	
       
    86 	CCmsCertificateChoice* self = new (ELeave) CCmsCertificateChoice();
       
    87 	CleanupStack::PushL(self);
       
    88 	self->ConstructL(aCertificate);
       
    89 	return self;
       
    90 	}
       
    91 
       
    92 CCmsCertificateChoice* CCmsCertificateChoice::NewL(const CX509Certificate& aCertificate)
       
    93 	{
       
    94 	CCmsCertificateChoice* self = CCmsCertificateChoice::NewLC(aCertificate);
       
    95 	CleanupStack::Pop();
       
    96 	return self;
       
    97 	}
       
    98 
       
    99 	
       
   100 CCmsCertificateChoice* CCmsCertificateChoice::NewLC(TCertificateType aCertType, const TDesC8& aEncodedCertificate)
       
   101 	{		
       
   102 	CCmsCertificateChoice* self = new (ELeave) CCmsCertificateChoice();
       
   103 	CleanupStack::PushL(self);
       
   104 	self->ConstructL(aCertType, aEncodedCertificate);
       
   105 	return self;
       
   106 	}
       
   107 
       
   108 CCmsCertificateChoice* CCmsCertificateChoice::NewL(TCertificateType aCertType, const TDesC8& aEncodedCertificate)
       
   109 	{
       
   110 	CCmsCertificateChoice* self = CCmsCertificateChoice::NewLC(aCertType, aEncodedCertificate);
       
   111 	CleanupStack::Pop();
       
   112 	return self;
       
   113 	}
       
   114 
       
   115 void CCmsCertificateChoice::ConstructL(const CX509Certificate& aCertificate)
       
   116 	{
       
   117 	iCertificateType=ECertificateX509;
       
   118 	iCertificate=CX509Certificate::NewL(aCertificate);
       
   119 	}
       
   120 
       
   121 void CCmsCertificateChoice::ConstructL(TCertificateType aCertType, const TDesC8& aEncodedCertificate)
       
   122 	{
       
   123 	iCertificateType=aCertType;
       
   124 	if (aCertType==ECertificateAttribute)
       
   125 		{
       
   126 		iEncodedAttributeCertificate=aEncodedCertificate.AllocL();	
       
   127 		}
       
   128 	else if (aCertType==ECertificateX509)
       
   129 			{
       
   130 			iCertificate=CX509Certificate::NewL(aEncodedCertificate);	
       
   131 			}
       
   132 		 else
       
   133 			 {
       
   134 			 User::Leave(KErrNotSupported);	
       
   135 			 }
       
   136 	}
       
   137 	
       
   138 
       
   139 EXPORT_C const HBufC8* CCmsCertificateChoice::AttributeCertificate() const
       
   140 	{
       
   141 	return iEncodedAttributeCertificate;
       
   142 	}
       
   143 
       
   144 CASN1EncEncoding* CCmsCertificateChoice::EncodeASN1DERLC() const
       
   145 	{
       
   146 	CASN1EncEncoding* enc(NULL);
       
   147 	if (iCertificateType==ECertificateX509)
       
   148 		{
       
   149 		enc=CASN1EncEncoding::NewLC(iCertificate->Encoding());
       
   150 		}
       
   151 	else if (iCertificateType==ECertificateAttribute)
       
   152 			{
       
   153 			TASN1DecGeneric decGen(*iEncodedAttributeCertificate);
       
   154 			decGen.InitL();
       
   155 			if (decGen.Tag()!=EASN1Sequence)
       
   156 				{
       
   157 				User::Leave(KErrArgument);	
       
   158 				}
       
   159 			enc=CASN1EncEncoding::NewLC(*iEncodedAttributeCertificate, 1, EContextSpecific);
       
   160 			}
       
   161 		else
       
   162 			{
       
   163 			//Extended Certificate
       
   164 			User::Leave(KErrNotSupported);
       
   165 			}
       
   166 	return enc;
       
   167 	}
       
   168 
       
   169 EXPORT_C CCmsCertificateChoice::TCertificateType CCmsCertificateChoice::CertificateType()
       
   170 	{
       
   171 	return iCertificateType;
       
   172 	}