cms/src/CCMSOriginatorPublicKey.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    "CCMSOriginatorPublicKey.h"
       
    21 #include "CCMSX509AlgorithmIdentifier.h"
       
    22 #include <asn1dec.h>
       
    23 #include <asn1enc.h>
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt KNumberOfSubModules = 2;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CCMSOriginatorPublicKey::CCMSOriginatorPublicKey
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CCMSOriginatorPublicKey::CCMSOriginatorPublicKey()
       
    37     {
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CCMSOriginatorPublicKey::ConstructL
       
    42 // Symbian 2nd phase constructor can leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C void CCMSOriginatorPublicKey::ConstructL(
       
    46 	const CCMSX509AlgorithmIdentifier& aAlgorithm,
       
    47 	const TDesC8& aPublicKey )
       
    48     {
       
    49 	SetAlgorithmL( aAlgorithm );
       
    50 	SetPublicKeyL( aPublicKey );
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CCMSOriginatorPublicKey::NewL
       
    55 // Two-phased constructor.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CCMSOriginatorPublicKey* CCMSOriginatorPublicKey::NewL()
       
    59 	{
       
    60 	// creating with empty/default values
       
    61 	CCMSX509AlgorithmIdentifier* algorithm =
       
    62         CCMSX509AlgorithmIdentifier::NewL( );
       
    63 	CleanupStack::PushL( algorithm );
       
    64 	CCMSOriginatorPublicKey* self = NewL( *algorithm, KNullDesC8() );
       
    65 	CleanupStack::PopAndDestroy( algorithm );
       
    66 	return self;
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CCMSOriginatorPublicKey::NewL
       
    71 // Two-phased copy constructor.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C CCMSOriginatorPublicKey* CCMSOriginatorPublicKey::NewL(
       
    75     const CCMSOriginatorPublicKey& aOriginatorPublicKey )
       
    76 	{
       
    77 	CCMSOriginatorPublicKey* self = NewLC( aOriginatorPublicKey );
       
    78     CleanupStack::Pop( self );
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CCMSOriginatorPublicKey::NewLC
       
    84 // Two-phased copy constructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 EXPORT_C CCMSOriginatorPublicKey* CCMSOriginatorPublicKey::NewLC(
       
    88     const CCMSOriginatorPublicKey& aOriginatorPublicKey )
       
    89 	{
       
    90 	CCMSOriginatorPublicKey* self = NewLC(
       
    91         aOriginatorPublicKey.Algorithm(), aOriginatorPublicKey.PublicKey() );
       
    92 	return self;
       
    93 	}
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CCMSOriginatorPublicKey::NewL
       
    97 // Two-phased constructor.
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 EXPORT_C CCMSOriginatorPublicKey* CCMSOriginatorPublicKey::NewL(
       
   101 	const CCMSX509AlgorithmIdentifier& aAlgorithm,
       
   102 	const TDesC8& aPublicKey )
       
   103     {
       
   104     CCMSOriginatorPublicKey* self = NewLC( aAlgorithm, aPublicKey );
       
   105     CleanupStack::Pop();
       
   106     return self;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CCMSOriginatorPublicKey::NewLC
       
   111 // Two-phased constructor.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C CCMSOriginatorPublicKey* CCMSOriginatorPublicKey::NewLC(
       
   115 	const CCMSX509AlgorithmIdentifier& aAlgorithm,
       
   116 	const TDesC8& aPublicKey )
       
   117     {
       
   118     CCMSOriginatorPublicKey* self =
       
   119     	new( ELeave ) CCMSOriginatorPublicKey();
       
   120 
       
   121     CleanupStack::PushL( self );
       
   122     self->ConstructL( aAlgorithm, aPublicKey );
       
   123     return self;
       
   124     }
       
   125 
       
   126 
       
   127 // Destructor
       
   128 CCMSOriginatorPublicKey::~CCMSOriginatorPublicKey()
       
   129     {
       
   130 	delete iAlgorithm;
       
   131 	delete iPublicKey;
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CCMSOriginatorPublicKey::DecodeL
       
   136 // Decrypts raw data to this instance
       
   137 // -----------------------------------------------------------------------------
       
   138 void CCMSOriginatorPublicKey::DecodeL( const TDesC8& aRawData )
       
   139 	{
       
   140     TASN1DecGeneric decGen( aRawData );
       
   141     decGen.InitL();
       
   142     TASN1DecSequence decSeq;
       
   143     CArrayPtr< TASN1DecGeneric >* itemsData =
       
   144         decSeq.DecodeDERLC( decGen );
       
   145     TInt count = itemsData->Count();
       
   146     if( count != KNumberOfSubModules )
       
   147         {
       
   148         User::Leave( KErrArgument );
       
   149         }
       
   150 	// we would not get this far if there is not 2 elements
       
   151 
       
   152 	// decoding algorithm identifier
       
   153     CCMSX509AlgorithmIdentifier* algorithm =
       
   154         CCMSX509AlgorithmIdentifier::NewL( );
       
   155     CleanupStack::PushL( algorithm );
       
   156     algorithm->DecodeL( itemsData->At( 0 )->Encoding() );
       
   157     CleanupStack::Pop( algorithm );
       
   158     delete iAlgorithm;
       
   159     iAlgorithm = algorithm;
       
   160 
       
   161 	// decoding public key
       
   162 	TASN1DecGeneric gen( *itemsData->At( 1 ) );
       
   163 	gen.InitL();
       
   164     // Symbian decoder can't handle zero length bit strings
       
   165     if( gen.LengthDERContent() > 1 )
       
   166         {
       
   167         TASN1DecBitString bitStringDecoder;
       
   168         HBufC8* tmp = bitStringDecoder.ExtractOctetStringL( gen );
       
   169         delete iPublicKey;
       
   170         iPublicKey = tmp;
       
   171         }
       
   172     else
       
   173         {
       
   174         HBufC8* tmp = KNullDesC8().AllocL();
       
   175         delete iPublicKey;
       
   176         iPublicKey = tmp;
       
   177         }
       
   178 
       
   179 	CleanupStack::PopAndDestroy( itemsData );
       
   180 	}
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CCMSOriginatorPublicKey::EncoderLC
       
   184 // Returns ASN1 encoder for this instance
       
   185 // -----------------------------------------------------------------------------
       
   186 
       
   187 CASN1EncBase* CCMSOriginatorPublicKey::EncoderLC() const
       
   188 	{
       
   189     CASN1EncSequence* root = CASN1EncSequence::NewLC();
       
   190 
       
   191     // encode the algorithm identifier
       
   192     HBufC8* encodedAlgorithm = NULL;
       
   193     iAlgorithm->EncodeL( encodedAlgorithm );
       
   194     CleanupStack::PushL( encodedAlgorithm );
       
   195     CASN1EncEncoding* algorithm = CASN1EncEncoding::NewLC( *encodedAlgorithm );
       
   196     root->AddAndPopChildL( algorithm );
       
   197     CleanupStack::PopAndDestroy( encodedAlgorithm );
       
   198 
       
   199     // add public key
       
   200     CASN1EncBitString* publicKey = CASN1EncBitString::NewLC( *iPublicKey );
       
   201     root->AddAndPopChildL( publicKey );
       
   202 
       
   203 	return root;
       
   204 	}
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CCMSOriginatorPublicKey::Algorithm
       
   208 // Algorithm identifier getter
       
   209 // -----------------------------------------------------------------------------
       
   210 EXPORT_C const CCMSX509AlgorithmIdentifier&
       
   211 CCMSOriginatorPublicKey::Algorithm() const
       
   212 	{
       
   213 	return *iAlgorithm;
       
   214 	}
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CCMSOriginatorPublicKey::PublicKey
       
   218 // PublicKey getter
       
   219 // -----------------------------------------------------------------------------
       
   220 EXPORT_C const TDesC8& CCMSOriginatorPublicKey::PublicKey() const
       
   221 	{
       
   222 	return *iPublicKey;
       
   223 	}
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CCMSOriginatorPublicKey::SetAlgorithmL
       
   227 // Algorithm setter
       
   228 // -----------------------------------------------------------------------------
       
   229 EXPORT_C void CCMSOriginatorPublicKey::SetAlgorithmL(
       
   230 	const CCMSX509AlgorithmIdentifier& aAlgorithm )
       
   231 	{
       
   232 	CCMSX509AlgorithmIdentifier* algorithm = NULL;
       
   233     const CAlgorithmIdentifier& algorithmId = aAlgorithm.AlgorithmIdentifier();
       
   234     const CAlgorithmIdentifier* digestAlgorithm = aAlgorithm.DigestAlgorithm();
       
   235     if( digestAlgorithm )
       
   236         {
       
   237         algorithm = CCMSX509AlgorithmIdentifier::NewL( algorithmId,
       
   238                                                        *digestAlgorithm );
       
   239         }
       
   240     else
       
   241         {
       
   242         algorithm = CCMSX509AlgorithmIdentifier::NewL( algorithmId );
       
   243         }
       
   244 	delete iAlgorithm;
       
   245 	iAlgorithm = algorithm;
       
   246 	}
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CCMSOriginatorPublicKey::SetPublicKeyL
       
   250 // PublicKey setter
       
   251 // -----------------------------------------------------------------------------
       
   252 EXPORT_C void CCMSOriginatorPublicKey::SetPublicKeyL(
       
   253 	const TDesC8& aPublicKey )
       
   254 	{
       
   255 	HBufC8* tmp = aPublicKey.AllocL();
       
   256 	delete iPublicKey;
       
   257 	iPublicKey = tmp;
       
   258 	}
       
   259 
       
   260 //  End of File