Symbian3/SDK/Source/GUID-5857377F-B90D-5149-9485-5919C12B8F13.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-5857377F-B90D-5149-9485-5919C12B8F13" xml:lang="en"><title>How
       
    13 to use a rule-based selector</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The following example demonstrates the use of the rule selector and the
       
    15 inclusion of a selection rule to retrieve the characteristics of a symmetric
       
    16 cipher object. </p>
       
    17 <codeblock id="GUID-827D423E-4E63-563F-8D00-C3C906A3E20C" xml:space="preserve">#include &lt;ruleselector.h&gt;
       
    18 #include &lt;cryptosymmetriccipherapi.h&gt;
       
    19 #include &lt;cryptospistateapi.h&gt;
       
    20 #include &lt;cryptospidef.h&gt;
       
    21 #include &lt;keys.h&gt;
       
    22 
       
    23 using namespace CryptoSpi;
       
    24 
       
    25 // Create a CSelectionRules collection object which is used to store the rules
       
    26 // that influence the choice of plug-in implementation selected
       
    27 
       
    28 CSelectionRules* rules = CSelectionRules::NewL();
       
    29 CleanupStack::PushL(rules);
       
    30 
       
    31 // Add a selection rule selecting symmetric ciphers with a key length of 64 bytes
       
    32     
       
    33 TInt ruleValue = 64;
       
    34             
       
    35 // Create and initialise one or more crypto parameters 
       
    36 // to store the contents of the rule value 
       
    37 CCryptoParam* ruleValueParam = CCryptoIntParam::NewL(ruleValue,KMaximumKeyLengthTypeUid);
       
    38 
       
    39 // Create a selection rule for each crypto parameter (CSelectionRuleContent
       
    40 // takes ownership of the crypto parameter), by passing in the following parameters:
       
    41 // *InterfaceScope        The Interface scope of which the rule should be applied
       
    42 // *AlgorithmScope        The Algorithm scope of which the rule should be applied
       
    43 // *CharacteristicValue   The parameter type and value of the rule
       
    44 // *Operator              The operator of the rule
       
    45 // *IsOptional            Whether this rule is optional (ETrue) or mandatory (EFalse)        
       
    46 CSelectionRuleContent* rule = CSelectionRuleContent::NewL(KSymmetricCipherInterfaceUid,
       
    47                                                           KNoneUid,
       
    48                                                           ruleValueParam,
       
    49                                                           EOpEqual,
       
    50                                                           EFalse);
       
    51 
       
    52 // Add the newly created selection rule to the CSelectionRules collection by calling
       
    53 // the 'AddSelectionRuleL' method and passing in a pointer to the constructed rule.
       
    54 // AddSelectionRuleL() checks that the type of the crypto parameters is correct and 
       
    55 // leaves with KErrNotSupported if not. For instance, a creator name parameter must 
       
    56 // be stored using a CCryptoDesC16Param. 
       
    57           
       
    58 rules-&gt;AddSelectionRuleL(rule);
       
    59                                     
       
    60 // Create an instance of the rule selector, passing in the selection rules 
       
    61 // object previously instantiated
       
    62 CRuleSelector* ruleSelector = CRuleSelector::NewL(rules);
       
    63     
       
    64 // Ownership of the rule collection object is passed to the rule selector, therefore
       
    65 // the CSelectionRules object needs to be popped off the cleanup stack before pushing on
       
    66 // the rule selector
       
    67 CleanupStack::Pop(rules);
       
    68 CleanupStack::PushL(ruleSelector);
       
    69     
       
    70 // The legacy selector is set within the framework by default. In order to set the rule
       
    71 // selector, the 'SetSelector' method of CryptoSpiStateApi needs to be called, 
       
    72 //passing a pointer to the initialized rule selector    
       
    73 CCryptoSpiStateApi::SetSelector(ruleSelector);
       
    74 
       
    75 // Create a new CryptoParams object to encapsulate the secret key string for the 
       
    76 // Symmetric Cipher implementation 
       
    77 CCryptoParams* keyParams = CCryptoParams::NewLC();
       
    78 
       
    79 // Add the secret key to the CCryptoParams object by calling the AddL method, passing in 
       
    80 // the key string and appropriate key parameter UID
       
    81 keyParams-&gt;AddL(_L8("12345678"), KSymmetricKeyParameterUid);
       
    82 
       
    83 // Create a CKey object by passing in an instance of TKeyProperty and the previously
       
    84 // created CCryptoParams object containing the secret key
       
    85 TKeyProperty keyProperty;        
       
    86 CKey* key=CKey::NewL(keyProperty,*keyParams);
       
    87 CleanupStack::PushL(key);
       
    88 
       
    89 // Create and initialise a pointer for the Symmetric Cipher implementation object        
       
    90 CSymmetricCipher* symmetricCipherImpl = NULL;
       
    91         
       
    92 // If successful, the 'CreateSymmetricCipherL' method returns KErrNone and the framework
       
    93 // creates an instance of the selected Symmetric Cipher implementation, as chosen by the
       
    94 // rule selector. The CSymmetricCipher pointer is passed by reference and set to point to
       
    95 // the new symmetric cipher object
       
    96 TRAPD(err,CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipherImpl,
       
    97                                                           KDesUid,
       
    98                                                           *key,
       
    99                                                           KCryptoModeEncryptUid,
       
   100                                                           KOperationModeNoneUid,
       
   101                                                           KPaddingModeNoneUid,
       
   102                                                           NULL));
       
   103 
       
   104 // Having successfully constructed the symmetric cipher implementation object, it is
       
   105 // possible to retrieve the plug-in characteristics associated with it
       
   106 if (symmetricCipherImpl &amp;&amp; (err == KErrNone))
       
   107  {
       
   108  CleanupStack::PushL(symmetricCipherImpl);
       
   109  // Use cipher
       
   110  CleanupStack::PopAndDestroy(symmetricCipherImpl);
       
   111  }
       
   112 
       
   113 CleanupStack::PopAndDestroy(2, keyParams);    //key, keyParams
       
   114 
       
   115 // Unselect rule selector before destruction. This causes CryptoSPI to use the 
       
   116 // legacy selector again. After calling SetSelector(), the caller 
       
   117 // keeps ownership of the selector, so after calling UnsetSelector(), the 
       
   118 // caller needs to delete the selector    
       
   119 CCryptoSpiStateApi::UnsetSelector();
       
   120     
       
   121 // Pop and destroy the rule selector
       
   122 CleanupStack::PopAndDestroy(ruleSelector);
       
   123 </codeblock>
       
   124 </conbody></concept>