cms/src/CCMSCertificateChoices.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2004 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 "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 FILES
       
    20 #include    "CCMSCertificateChoices.h"
       
    21 #include "CCMSX509Certificate.h"
       
    22 #include "CCMSX509AttributeCertificate.h"
       
    23 #include <asn1dec.h>
       
    24 #include <asn1enc.h>
       
    25 
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CCMSCertificateChoices::CCMSCertificateChoices
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CCMSCertificateChoices::CCMSCertificateChoices()
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CCMSCertificateChoices::ConstructL
       
    41 // Symbian 2nd phase constructor can leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 EXPORT_C void CCMSCertificateChoices::ConstructL(
       
    45     const CCMSX509Certificate& aCertificate )
       
    46     {
       
    47     SetCertificateL( aCertificate );
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CCMSCertificateChoices::ConstructL
       
    52 // Symbian 2nd phase constructor can leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C void CCMSCertificateChoices::ConstructL(
       
    56     const CCMSX509AttributeCertificate& aAttrCert )
       
    57     {
       
    58     SetAttrCertL( aAttrCert );
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CCMSCertificateChoices::NewL
       
    63 // Two-phased constructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewL()
       
    67 	{
       
    68 	// creating with empty/default values
       
    69     CCMSX509Certificate* certificate = CCMSX509Certificate::NewL( );
       
    70 	CleanupStack::PushL( certificate );
       
    71 	CCMSCertificateChoices* self = NewL( *certificate );
       
    72 	CleanupStack::PopAndDestroy( certificate );
       
    73 	return self;
       
    74 	}
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CCMSCertificateChoices::NewLC
       
    78 // Two-phased constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewLC()
       
    82 	{
       
    83 	// creating with empty/default values
       
    84     CCMSX509Certificate* certificate = CCMSX509Certificate::NewL( );
       
    85 	CleanupStack::PushL( certificate );
       
    86 	CCMSCertificateChoices* self = NewL( *certificate );
       
    87 	CleanupStack::PopAndDestroy( certificate );
       
    88     CleanupStack::PushL( self );
       
    89 	return self;
       
    90 	}
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CCMSCertificateChoices::NewL
       
    94 // Two-phased constructor.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewL(
       
    98     const CCMSX509Certificate& aCertificate )
       
    99     {
       
   100     CCMSCertificateChoices* self = NewLC( aCertificate );
       
   101     CleanupStack::Pop( self );
       
   102 
       
   103     return self;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CCMSCertificateChoices::NewLC
       
   108 // Two-phased constructor.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewLC(
       
   112     const CCMSX509Certificate& aCertificate )
       
   113     {
       
   114     CCMSCertificateChoices* self =
       
   115     	new( ELeave ) CCMSCertificateChoices();
       
   116 
       
   117     CleanupStack::PushL( self );
       
   118     self->ConstructL( aCertificate );
       
   119 
       
   120     return self;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CCMSCertificateChoices::NewL
       
   125 // Two-phased constructor.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewL(
       
   129     const CCMSX509AttributeCertificate& aAttrCert )
       
   130     {
       
   131     CCMSCertificateChoices* self = NewLC( aAttrCert );
       
   132     CleanupStack::Pop( self );
       
   133 
       
   134     return self;
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CCMSCertificateChoices::NewLC
       
   139 // Two-phased constructor.
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C CCMSCertificateChoices* CCMSCertificateChoices::NewLC(
       
   143     const CCMSX509AttributeCertificate& aAttrCert )
       
   144     {
       
   145     CCMSCertificateChoices* self =
       
   146     	new( ELeave ) CCMSCertificateChoices();
       
   147 
       
   148     CleanupStack::PushL( self );
       
   149     self->ConstructL( aAttrCert );
       
   150 
       
   151     return self;
       
   152     }
       
   153 
       
   154 // Destructor
       
   155 CCMSCertificateChoices::~CCMSCertificateChoices()
       
   156     {
       
   157     delete iCertificate;
       
   158     delete iAttrCert;
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CCMSCertificateChoices::DecodeL
       
   163 // Decrypts raw data to this instance
       
   164 // -----------------------------------------------------------------------------
       
   165 void CCMSCertificateChoices::DecodeL( const TDesC8& aRawData )
       
   166 	{
       
   167     
       
   168     TASN1DecGeneric decGen( aRawData );
       
   169     decGen.InitL();
       
   170     switch( decGen.Tag() )
       
   171         {
       
   172         case EASN1Sequence:
       
   173             {
       
   174             // check class and decode certificate
       
   175             if( decGen.Class() != EUniversal )
       
   176                 {
       
   177                 User::Leave( KErrArgument );
       
   178                 }
       
   179             CCMSX509Certificate* certificate = CCMSX509Certificate::NewL();
       
   180             CleanupStack::PushL( certificate );
       
   181             certificate->DecodeL( aRawData );
       
   182             delete iAttrCert;
       
   183             iAttrCert = NULL;
       
   184             delete iCertificate;
       
   185             iCertificate = certificate;
       
   186             CleanupStack::Pop( certificate );
       
   187             break;
       
   188             }
       
   189         case KCMSAttrCertTag:
       
   190             {
       
   191             // check class and decode attrCert
       
   192             if( decGen.Class() != EContextSpecific )
       
   193                 {
       
   194                 User::Leave( KErrArgument );
       
   195                 }
       
   196             CCMSX509AttributeCertificate* attrCert =
       
   197                 CCMSX509AttributeCertificate::NewL();
       
   198             CleanupStack::PushL( attrCert );
       
   199             attrCert->DecodeImplicitTagL( aRawData, KCMSAttrCertTag );
       
   200             delete iCertificate;
       
   201             iCertificate = NULL;
       
   202             delete iAttrCert;
       
   203             iAttrCert = attrCert;
       
   204             CleanupStack::Pop( attrCert );
       
   205             break;
       
   206             }
       
   207         default:
       
   208             {
       
   209             User::Leave( KErrArgument );
       
   210             }
       
   211         }
       
   212 	}
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CCMSCertificateChoices::EncodeL
       
   216 // Encrypts this instance to descriptor
       
   217 // -----------------------------------------------------------------------------
       
   218 
       
   219 void CCMSCertificateChoices::EncodeL( HBufC8*& aResult ) const
       
   220 	{
       
   221 	CASN1EncBase* root = EncoderLC();
       
   222 
       
   223 	// encode the object in a DER encoding
       
   224     HBufC8* der = HBufC8::NewMaxLC( root->LengthDER() );
       
   225     TPtr8 pder( der->Des() );
       
   226     TUint pos = 0;
       
   227     root->WriteDERL( pder, pos );
       
   228     CleanupStack::Pop( der );
       
   229     aResult = der;
       
   230 
       
   231 	CleanupStack::PopAndDestroy( root );
       
   232 	}
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CCMSCertificateChoices::EncoderLC
       
   236 // Returns ASN1 encoder for this instance
       
   237 // -----------------------------------------------------------------------------
       
   238 
       
   239 CASN1EncBase* CCMSCertificateChoices::EncoderLC() const
       
   240 	{
       
   241     if( iCertificate )
       
   242         {
       
   243         return iCertificate->EncoderLC();
       
   244         }
       
   245     else
       
   246         {
       
   247         CASN1EncBase* encoder = iAttrCert->EncoderLC();
       
   248         encoder->SetTag( KCMSAttrCertTag );
       
   249         return encoder;
       
   250         }
       
   251 	}
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CCMSCertificateChoices::Certificate
       
   255 // certificate getter.
       
   256 // -----------------------------------------------------------------------------
       
   257 EXPORT_C const CCMSX509Certificate*
       
   258 CCMSCertificateChoices::Certificate() const
       
   259 	{
       
   260 	return iCertificate;
       
   261 	}
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CCMSCertificateChoices::AttrCert
       
   265 // attrCert getter.
       
   266 // -----------------------------------------------------------------------------
       
   267 EXPORT_C const CCMSX509AttributeCertificate*
       
   268 CCMSCertificateChoices::AttrCert() const
       
   269 	{
       
   270 	return iAttrCert;
       
   271 	}
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CCMSCertificateChoices::SetCertificateL
       
   275 // Certificate setter
       
   276 // -----------------------------------------------------------------------------
       
   277 EXPORT_C void CCMSCertificateChoices::SetCertificateL(
       
   278 	const CCMSX509Certificate& aCertificate )
       
   279 	{
       
   280     CCMSX509Certificate* certificate = CCMSX509Certificate::NewL(
       
   281         aCertificate.SerialNumber(),
       
   282         aCertificate.Signature(),
       
   283         aCertificate.Issuer(),
       
   284         aCertificate.Validity(),
       
   285         aCertificate.Subject(),
       
   286         aCertificate.SubjectPublicKeyInfo(),
       
   287         aCertificate.AlgorithmIdentifier(),
       
   288         aCertificate.Encrypted() );
       
   289     CleanupStack::PushL( certificate );
       
   290     certificate->SetVersion( aCertificate.Version() );
       
   291     const TDesC8* issuerUniqueIdentifier = aCertificate.IssuerUniqueIdentifier();
       
   292     if( issuerUniqueIdentifier )
       
   293         {
       
   294         certificate->SetIssuerUniqueIdentifierL( *issuerUniqueIdentifier );
       
   295         }
       
   296     const TDesC8* subjectUniqueIdentifier =
       
   297         aCertificate.SubjectUniqueIdentifier();
       
   298     if( subjectUniqueIdentifier )
       
   299         {
       
   300         certificate->SetSubjectUniqueIdentifierL( *subjectUniqueIdentifier );
       
   301         }
       
   302     delete iAttrCert;
       
   303     iAttrCert = NULL;
       
   304     delete iCertificate;
       
   305     iCertificate = certificate;
       
   306     CleanupStack::Pop( certificate );
       
   307 	}
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CCMSCertificateChoices::SetAttrCertL
       
   311 // AttrCert setter
       
   312 // -----------------------------------------------------------------------------
       
   313 EXPORT_C void CCMSCertificateChoices::SetAttrCertL(
       
   314 	const CCMSX509AttributeCertificate& aAttrCert )
       
   315 	{
       
   316     CCMSX509AttributeCertificate* attrCert = CCMSX509AttributeCertificate::NewL(
       
   317         aAttrCert.Info(),
       
   318         aAttrCert.AlgorithmIdentifier(),
       
   319         aAttrCert.Encrypted() );
       
   320     delete iCertificate;
       
   321     iCertificate = NULL;
       
   322     delete iAttrCert;
       
   323     iAttrCert = attrCert;
       
   324 	}
       
   325 
       
   326 //  End of File