Symbian3/SDK/Source/GUID-1AA392CB-F638-5D35-993E-4A26D67A7C98.dita
changeset 8 ae94777fff8f
equal deleted inserted replaced
7:51a74ef9ed63 8:ae94777fff8f
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-1AA392CB-F638-5D35-993E-4A26D67A7C98" xml:lang="en"><title>How
       
    13 to retrieve characteristics</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The following example demonstrates the use of the legacy selector to retrieve
       
    15 the characteristics of an asymmetric cipher object. In the example, an implementation
       
    16 object is constructed (making use of an RSA Key Pair also generated by the
       
    17 framework) and returned. The plug-in characteristics and extended characteristics
       
    18 associated with the object are then accessed. </p>
       
    19 <codeblock id="GUID-CF2DF77C-90BD-56A0-A27D-89C4DA18EDCD" xml:space="preserve">#include &lt;cryptoasymmetriccipherapi.h&gt;
       
    20 #include &lt;cryptokeypairgeneratorapi.h&gt;
       
    21 #include &lt;cryptospidef.h&gt;
       
    22 #include &lt;plugincharacteristics.h&gt;
       
    23 #include &lt;extendedcharacteristics.h&gt;
       
    24 #include &lt;keypair.h&gt;
       
    25 
       
    26 // Constant definition for the RSA key pair generator exponent value
       
    27 const TInt KKeyExponent = 65537;
       
    28 
       
    29 using namespace CryptoSpi;
       
    30 
       
    31 // Create a new CCryptoParams instance to contain the RSA Key Pair initialization data
       
    32 CCryptoParams* keyParams = CCryptoParams::NewLC(); 
       
    33                     
       
    34 // Create an RSA Key Pair and Key Pair Generator object pointers
       
    35 CKeyPair* keyPair = NULL;
       
    36 CKeyPairGenerator * keypairImpl = NULL;                
       
    37                 
       
    38 // Set the RSA Key Pair Generator initialization parameters reate an RSA key pair
       
    39 keyParams-&gt;AddL(KKeyExponent, KRsaKeyParameterEUid);
       
    40 keyParams-&gt;AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
       
    41 
       
    42 // Retrieve an instance of an RSA Key Pair Generator implementation from the framework
       
    43 CKeyPairGeneratorFactory::CreateKeyPairGeneratorL( keypairImpl,
       
    44                                                    KRSAKeyPairGeneratorUid,
       
    45                                                    keyParams);
       
    46                                             
       
    47 CleanupStack::PushL(keypairImpl);
       
    48     
       
    49 // Create an RSA Key Pair    
       
    50 keypairImpl-&gt;GenerateKeyPairL(1024, *keyParams, keyPair);
       
    51                 
       
    52 CleanupStack::PushL(keyPair);
       
    53                                         
       
    54 // Create and initialize a pointer for the asymmetric cipher implementation object        
       
    55 CAsymmetricCipher* asymmetricCipherImpl = NULL;
       
    56 
       
    57 // If successful, the 'CreateAsymmetricCipherL' method returns KErrNone and sets the 
       
    58 // asymmetricCipherImpl pointer to point to the constructed implementation
       
    59 TRAPD(err, CAsymmetricCipherFactory::CreateAsymmetricCipherL(asymmetricCipherImpl,
       
    60                                                              KRsaCipherUid,
       
    61                                                              keyPair-&gt;PrivateKey(),
       
    62                                                              KCryptoModeEncryptUid,
       
    63                                                              KPaddingModeNoneUid,
       
    64                                                              NULL));
       
    65 
       
    66 // Having successfully constructed the asymmetric cipher implementation object, 
       
    67 // it is possible to retrieve the plug-in characteristics associated with it
       
    68 if (asymmetricCipherImpl &amp;&amp; (err == KErrNone))
       
    69  {
       
    70  CleanupStack::PushL(asymmetricCipherImpl);
       
    71 
       
    72  // Retrieving common and algorithm-specific characteristics
       
    73  // Create a constant pointer of type TCharacteristics used to access the
       
    74  // asymmetric cipher plug-in's common and algorithm-specific characteristics
       
    75     const TCharacteristics* chars(NULL);
       
    76 
       
    77  // Retrieve the common and algorithm-specific characteristics by calling the
       
    78  // 'GetCharacteristicsL' method passing in TCharacteristics pointer
       
    79  asymmetricCipherImpl-&gt;GetCharacteristicsL(chars);
       
    80 
       
    81  // Static cast the characteristics to type TAsymmetricCipherCharacteristics
       
    82  const TAsymmetricCipherCharacteristics* asymmetricChars = 
       
    83   static_cast&lt;const TAsymmetricCipherCharacteristics*&gt;(chars);
       
    84 
       
    85  // Retrieve the common characteristics from the TASymmetricCipherCharacteristics
       
    86  // object
       
    87  const TCommonCharacteristics* asymmetricCommonChars = &amp;asymmetricChars-&gt;cmn;
       
    88 
       
    89     TUid implementationId;
       
    90     TBool hardwareSupported;
       
    91     TRomLitC16 creatorName;
       
    92     TInt maximumKeyLength;
       
    93     TInt32 supportedPaddingModes;
       
    94 
       
    95  // Example of extracting asymmetric common characteristics
       
    96  implementationId.iUid = asymmetricCommonChars-&gt;iImplementationUID;
       
    97  hardwareSupported = asymmetricCommonChars-&gt;iIsHardwareSupported;
       
    98  creatorName = *(asymmetricCommonChars-&gt;iCreatorName);
       
    99 
       
   100  // Example of extracting asymmetric algorithm specific characteristics
       
   101  maximumKeyLength = asymmetricChars-&gt;iMaximumKeyLength;
       
   102  supportedPaddingModes = *(asymmetricChars-&gt;iSupportedPaddingModes);
       
   103 
       
   104  //Retrieving extended characteristics
       
   105 
       
   106  // Create a constant pointer of type CExtendedCharacteristics used to store and
       
   107  // access the asymmetric cipher plug-in extended characteristics. Retrieve
       
   108  // the data by calling the 'GetExtendedCharacteristicsL' method and store the
       
   109  // returned pointer
       
   110  const CExtendedCharacteristics* extendedChars =
       
   111   asymmetricCipherImpl-&gt;GetExtendedCharacteristicsL();
       
   112 
       
   113  // Const casting the pointer to the CExtendedCharacteristics object allows it to
       
   114  // be pushed onto the cleanup stack
       
   115  CExtendedCharacteristics* ncExtendedChars =
       
   116   const_cast&lt;CExtendedCharacteristics*&gt;(extendedChars);
       
   117  CleanupStack::PushL(ncExtendedChars);
       
   118 
       
   119  TInt concurrency;
       
   120 
       
   121  // Each of the extended characteristics for the particular cryptographic
       
   122  // implementation can then be accessed by passing the UID value into the
       
   123  // appropriate 'Get' method
       
   124  concurrency = extendedChars-&gt;GetTIntCharacteristicL(KConcurrencyTypeUid);
       
   125  // Alternatively, extended characteristic retrieval can be achieved via the
       
   126  // ListExtendedCharacteristics() method of the CExtendedCharacteristics Object.
       
   127  // Using the 'ListExtendedCharacteristics' function returns a pointer to a
       
   128  // CCryptoParams object, that contains a list of all associated extended
       
   129  // characteristics
       
   130  const CCryptoParams* extendedCryptoParams = 
       
   131   extendedChars-&gt;ListExtendedCharacteristics();
       
   132 
       
   133  TBool exclusiveUse;
       
   134 
       
   135  // The extended characteristics can then be accessed in much the same way as 
       
   136  // before, making a call to the appropriate 'Get' method with the UID value of the
       
   137  // required type
       
   138  exclusiveUse = extendedCryptoParams-&gt;GetTIntL(KExclusiveUseTypeUid);
       
   139 
       
   140  CleanupStack::PopAndDestroy(2, asymmetricCipherImpl);
       
   141  }
       
   142 
       
   143 // Pop and destroy the remaining items on the cleanup stack
       
   144 CleanupStack::PopAndDestroy(3,keyParams);
       
   145     
       
   146 </codeblock>
       
   147 </conbody></concept>