How to retrieve characteristics

The following example demonstrates the use of the legacy selector to retrieve the characteristics of an asymmetric cipher object. In the example, an implementation object is constructed (making use of an RSA Key Pair also generated by the framework) and returned. The plug-in characteristics and extended characteristics associated with the object are then accessed.

#include <cryptoasymmetriccipherapi.h>
#include <cryptokeypairgeneratorapi.h>
#include <cryptospidef.h>
#include <plugincharacteristics.h>
#include <extendedcharacteristics.h>
#include <keypair.h>

// Constant definition for the RSA key pair generator exponent value
const TInt KKeyExponent = 65537;

using namespace CryptoSpi;

// Create a new CCryptoParams instance to contain the RSA Key Pair initialization data
CCryptoParams* keyParams = CCryptoParams::NewLC(); 
                    
// Create an RSA Key Pair and Key Pair Generator object pointers
CKeyPair* keyPair = NULL;
CKeyPairGenerator * keypairImpl = NULL;                
                
// Set the RSA Key Pair Generator initialization parameters reate an RSA key pair
keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);

// Retrieve an instance of an RSA Key Pair Generator implementation from the framework
CKeyPairGeneratorFactory::CreateKeyPairGeneratorL( keypairImpl,
                                                   KRSAKeyPairGeneratorUid,
                                                   keyParams);
                                            
CleanupStack::PushL(keypairImpl);
    
// Create an RSA Key Pair    
keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair);
                
CleanupStack::PushL(keyPair);
                                        
// Create and initialize a pointer for the asymmetric cipher implementation object        
CAsymmetricCipher* asymmetricCipherImpl = NULL;

// If successful, the 'CreateAsymmetricCipherL' method returns KErrNone and sets the 
// asymmetricCipherImpl pointer to point to the constructed implementation
TRAPD(err, CAsymmetricCipherFactory::CreateAsymmetricCipherL(asymmetricCipherImpl,
                                                             KRsaCipherUid,
                                                             keyPair->PrivateKey(),
                                                             KCryptoModeEncryptUid,
                                                             KPaddingModeNoneUid,
                                                             NULL));

// Having successfully constructed the asymmetric cipher implementation object, 
// it is possible to retrieve the plug-in characteristics associated with it
if (asymmetricCipherImpl && (err == KErrNone))
 {
 CleanupStack::PushL(asymmetricCipherImpl);

 // Retrieving common and algorithm-specific characteristics
 // Create a constant pointer of type TCharacteristics used to access the
 // asymmetric cipher plug-in's common and algorithm-specific characteristics
    const TCharacteristics* chars(NULL);

 // Retrieve the common and algorithm-specific characteristics by calling the
 // 'GetCharacteristicsL' method passing in TCharacteristics pointer
 asymmetricCipherImpl->GetCharacteristicsL(chars);

 // Static cast the characteristics to type TAsymmetricCipherCharacteristics
 const TAsymmetricCipherCharacteristics* asymmetricChars = 
  static_cast<const TAsymmetricCipherCharacteristics*>(chars);

 // Retrieve the common characteristics from the TASymmetricCipherCharacteristics
 // object
 const TCommonCharacteristics* asymmetricCommonChars = &asymmetricChars->cmn;

    TUid implementationId;
    TBool hardwareSupported;
    TRomLitC16 creatorName;
    TInt maximumKeyLength;
    TInt32 supportedPaddingModes;

 // Example of extracting asymmetric common characteristics
 implementationId.iUid = asymmetricCommonChars->iImplementationUID;
 hardwareSupported = asymmetricCommonChars->iIsHardwareSupported;
 creatorName = *(asymmetricCommonChars->iCreatorName);

 // Example of extracting asymmetric algorithm specific characteristics
 maximumKeyLength = asymmetricChars->iMaximumKeyLength;
 supportedPaddingModes = *(asymmetricChars->iSupportedPaddingModes);

 //Retrieving extended characteristics

 // Create a constant pointer of type CExtendedCharacteristics used to store and
 // access the asymmetric cipher plug-in extended characteristics. Retrieve
 // the data by calling the 'GetExtendedCharacteristicsL' method and store the
 // returned pointer
 const CExtendedCharacteristics* extendedChars =
  asymmetricCipherImpl->GetExtendedCharacteristicsL();

 // Const casting the pointer to the CExtendedCharacteristics object allows it to
 // be pushed onto the cleanup stack
 CExtendedCharacteristics* ncExtendedChars =
  const_cast<CExtendedCharacteristics*>(extendedChars);
 CleanupStack::PushL(ncExtendedChars);

 TInt concurrency;

 // Each of the extended characteristics for the particular cryptographic
 // implementation can then be accessed by passing the UID value into the
 // appropriate 'Get' method
 concurrency = extendedChars->GetTIntCharacteristicL(KConcurrencyTypeUid);
 // Alternatively, extended characteristic retrieval can be achieved via the
 // ListExtendedCharacteristics() method of the CExtendedCharacteristics Object.
 // Using the 'ListExtendedCharacteristics' function returns a pointer to a
 // CCryptoParams object, that contains a list of all associated extended
 // characteristics
 const CCryptoParams* extendedCryptoParams = 
  extendedChars->ListExtendedCharacteristics();

 TBool exclusiveUse;

 // The extended characteristics can then be accessed in much the same way as 
 // before, making a call to the appropriate 'Get' method with the UID value of the
 // required type
 exclusiveUse = extendedCryptoParams->GetTIntL(KExclusiveUseTypeUid);

 CleanupStack::PopAndDestroy(2, asymmetricCipherImpl);
 }

// Pop and destroy the remaining items on the cleanup stack
CleanupStack::PopAndDestroy(3,keyParams);