xmlsecurityengine/xmlsecwrapper/src/xmlsecwencrypt.cpp
changeset 0 e35f40988205
child 24 74f0b3eb154c
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /*
       
     2 * Copyright (c) 2009 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: Class with methods needed in encryption and decryption process.        
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 // XML Engine
       
    21 #include <libxml2_tree.h>
       
    22 #include <libxml2_xmlmemory.h>
       
    23 #include <libxml2_globals.h>
       
    24 
       
    25 #include <xmlengmem.h>
       
    26 #include <xmlengxestd.h>
       
    27 #include <xmlengutils.h>
       
    28 
       
    29 #include <xmlengdom.h>
       
    30 
       
    31 // XML Sec
       
    32 #include "xmlsec_crypto.h"
       
    33 #include "xmlsec_xmlsec.h"
       
    34 #include "xmlsec_xmltree.h"
       
    35 #include "xmlsec_xmlenc.h"
       
    36 #include "xmlsec_templates.h"
       
    37 #include "xmlsecwinternalutils.h"
       
    38 
       
    39 #include "xmlsecwerrors.h"
       
    40 #include "xmlsecwdefs.h"
       
    41 #include "xmlsecwencrypt.h"
       
    42 #include "xmlsecwkeymanager.h"
       
    43 #include "xmlsecwtemplate.h"
       
    44 #include "xmlsecwglobalstate.h"
       
    45 
       
    46 const TInt K3DESKeySize = 24;
       
    47 const TInt KAESKeySize256 = 32;
       
    48 
       
    49 namespace Encrypt
       
    50     {
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Reset encryption ctxt
       
    54 // ---------------------------------------------------------------------------
       
    55 //  
       
    56 void ResetCtx(xmlSecEncCtxPtr aCtx, CXmlSecKeyManager* aMngr)
       
    57     {
       
    58     xmlSecKeyPtr tmpKey = NULL;
       
    59     if(!aMngr)
       
    60         {
       
    61         tmpKey = aCtx->encKey;
       
    62         aCtx->encKey = NULL;
       
    63         }
       
    64     // reset ctx
       
    65     xmlSecEncCtxReset(aCtx);
       
    66     if(aMngr)
       
    67         {
       
    68         xmlSecEncCtxInitialize(aCtx,aMngr->GetKeyManagerPtr());    
       
    69         }    
       
    70     
       
    71     aCtx->encKey = tmpKey;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Read key from buffer
       
    76 // ---------------------------------------------------------------------------
       
    77 //  
       
    78 xmlSecKeyPtr ReadKeyFromBufferL(const TDesC8& aKeyFile,
       
    79                                 const TDesC8& aKeyName,
       
    80                                 CXmlSecEncrypt::TXmlSecKeyType aAlgorithm)
       
    81     {
       
    82     if(aKeyFile.Length() == 0)
       
    83         {
       
    84         User::Leave(KErrWrongParameter);
       
    85         }
       
    86     char* name = XmlEngXmlCharFromDes8L(aKeyName); 
       
    87     CleanupStack::PushL(name);       
       
    88     char* tmpName = NULL;
       
    89     if(aKeyName.Length())
       
    90         {
       
    91         tmpName = name;
       
    92         }
       
    93     xmlSecKeyPtr keyPtr = NULL;
       
    94     // read key and check size
       
    95     if (aAlgorithm == CXmlSecEncrypt::EAES256)
       
    96         {
       
    97         if(aKeyFile.Size() != KAESKeySize256)
       
    98             {
       
    99             User::Leave(KErrKey);
       
   100             }
       
   101         keyPtr = xmlSecKeyReadMemory(xmlSecKeyDataAesId, aKeyFile.Ptr(), aKeyFile.Size());
       
   102         }	    
       
   103 	else if (aAlgorithm == CXmlSecEncrypt::E3DES)
       
   104 	    {
       
   105 	    if(aKeyFile.Size() != K3DESKeySize)
       
   106             {
       
   107             User::Leave(KErrKey);
       
   108             }
       
   109         keyPtr = xmlSecKeyReadMemory(xmlSecKeyDataDesId, aKeyFile.Ptr(), aKeyFile.Size());
       
   110 	    }        
       
   111     XmlEngOOMTestL();
       
   112     XmlSecErrorFlagTestL();
       
   113 
       
   114     if(!keyPtr) 
       
   115         {
       
   116         User::Leave(KErrKey);
       
   117         }
       
   118     // set key name
       
   119     if(xmlSecKeySetName(keyPtr, (const unsigned char*) tmpName) < 0) 
       
   120         {
       
   121         xmlSecKeyDestroy(keyPtr);
       
   122         XmlEngOOMTestL();
       
   123         XmlSecErrorFlagTestL();
       
   124         User::Leave(KErrKey);
       
   125         }
       
   126     CleanupStack::PopAndDestroy(name);
       
   127     return keyPtr;
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // Read key from file
       
   132 // ---------------------------------------------------------------------------
       
   133 //  
       
   134 xmlSecKeyPtr ReadKeyFromFileL(const TDesC8& aKeyFile,
       
   135                               const TDesC8& aKeyName,
       
   136                               CXmlSecEncrypt::TXmlSecKeyType aKeyFormat)
       
   137     {
       
   138     if(aKeyFile.Length() == 0)
       
   139         {
       
   140         User::Leave(KErrWrongParameter);
       
   141         }
       
   142     RFs fs;
       
   143     User::LeaveIfError(fs.Connect());
       
   144     CleanupClosePushL(fs);    
       
   145     RFile keyFile;
       
   146     TInt size;
       
   147     RBuf keyName;
       
   148     // read key from file
       
   149     keyName.CreateL(KMaxFileName);
       
   150     CleanupClosePushL(keyName);
       
   151     keyName.Copy(aKeyFile);
       
   152     User::LeaveIfError(keyFile.Open(fs, keyName, EFileRead | EFileShareReadersOnly));
       
   153     CleanupStack::PopAndDestroy(&keyName);
       
   154     CleanupClosePushL(keyFile);
       
   155     User::LeaveIfError(keyFile.Size(size));
       
   156     HBufC8* key = HBufC8::NewLC(size);
       
   157     TPtr8 keyPtrD = key->Des();
       
   158     User::LeaveIfError(keyFile.Read(keyPtrD, size)); 
       
   159     xmlSecKeyPtr keyPtr = ReadKeyFromBufferL(*key,aKeyName,aKeyFormat);
       
   160     CleanupStack::PopAndDestroy(key);
       
   161     CleanupStack::PopAndDestroy(&keyFile);
       
   162     CleanupStack::PopAndDestroy(&fs);
       
   163     return keyPtr;
       
   164     }
       
   165   
       
   166     } // namespace Encrypt
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Two phase constructor
       
   170 // ---------------------------------------------------------------------------
       
   171 //  
       
   172 EXPORT_C CXmlSecEncrypt* CXmlSecEncrypt::NewLC()
       
   173     {
       
   174     CXmlSecEncrypt* self = new (ELeave) CXmlSecEncrypt;
       
   175     CleanupStack::PushL(self);
       
   176     self->ConstructL();
       
   177     return self;
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // Two phase constructor
       
   182 // ---------------------------------------------------------------------------
       
   183 //   
       
   184 EXPORT_C CXmlSecEncrypt* CXmlSecEncrypt::NewL()
       
   185     {
       
   186     CXmlSecEncrypt* self = CXmlSecEncrypt::NewLC();
       
   187     CleanupStack::Pop(self);
       
   188     return self;
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // Encrypt data from buffer
       
   193 // ---------------------------------------------------------------------------
       
   194 // 
       
   195 EXPORT_C RXmlEngDocument CXmlSecEncrypt::EncryptDataL(const TDesC8& aData)
       
   196     {
       
   197     if(!aData.Length())
       
   198         {
       
   199         User::Leave(KErrWrongParameter);
       
   200         }
       
   201     if(iTemplate.IsNull())
       
   202         {
       
   203         User::Leave(KErrTemplate);
       
   204         }
       
   205     xmlSecEncCtxPtr tmpCtx = ENC_CTX;
       
   206     
       
   207     Encrypt::ResetCtx(tmpCtx,NULL);    
       
   208            
       
   209     xmlNodePtr node = NULL;
       
   210     // Copy tmpl file
       
   211     xmlDocPtr result = xmlCopyDoc(INTERNAL_DOCPTR(iTemplate), 1);
       
   212     if(!result) 
       
   213         {
       
   214         User::Leave(KErrNoMemory);
       
   215         }
       
   216     // find start node
       
   217     node = xmlSecFindNode(xmlDocGetRootElement(result), xmlSecNodeEncryptedData, xmlSecEncNs);
       
   218     if(!node) 
       
   219         {
       
   220         xmlFreeDoc(result);
       
   221         User::Leave(KErrTemplate);
       
   222         }
       
   223 
       
   224     // encrypt the data
       
   225     if(xmlSecEncCtxBinaryEncrypt(tmpCtx, node, aData.Ptr(), aData.Length()) < 0) 
       
   226         {
       
   227         xmlFreeDoc(result);
       
   228         XmlEngOOMTestL();
       
   229         XmlSecErrorFlagTestL();
       
   230         User::Leave(KErrEncrypt);
       
   231         }
       
   232     RXmlEngDocument doc;
       
   233     XmlSecGlobalState* gs = XmlSecGetTls();
       
   234     doc.OpenL(*gs->iDOMImpl,result);
       
   235     return doc;
       
   236 	}
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // Encrypt xml document
       
   240 // ---------------------------------------------------------------------------
       
   241 // 
       
   242 EXPORT_C void CXmlSecEncrypt::EncryptXmlDocumentL(RXmlEngDocument& aDocument)
       
   243     {
       
   244     if(aDocument.IsNull())
       
   245         {
       
   246         User::Leave(KErrWrongParameter);
       
   247         }
       
   248     EncryptXmlNodeL(aDocument.DocumentElement());
       
   249     }
       
   250    
       
   251 // ---------------------------------------------------------------------------
       
   252 // Encrypt xml node
       
   253 // ---------------------------------------------------------------------------
       
   254 // 
       
   255 EXPORT_C void CXmlSecEncrypt::EncryptXmlNodeL(TXmlEngElement aNode)
       
   256     {
       
   257     if(aNode.IsNull())
       
   258         {
       
   259         User::Leave(KErrWrongParameter);
       
   260         }
       
   261     if(iTemplate.IsNull())
       
   262         {
       
   263         User::Leave(KErrTemplate);
       
   264         }
       
   265     xmlSecEncCtxPtr tmpCtx = ENC_CTX;
       
   266     
       
   267     Encrypt::ResetCtx(tmpCtx,NULL);    
       
   268     
       
   269     xmlNodePtr node = NULL;
       
   270     // find start node
       
   271     node = xmlSecFindNode(xmlDocGetRootElement(INTERNAL_DOCPTR(iTemplate)),
       
   272                                 xmlSecNodeEncryptedData, xmlSecEncNs);
       
   273     if(!node) 
       
   274         {
       
   275         User::Leave(KErrTemplate);
       
   276         }
       
   277     
       
   278     xmlNodePtr result = xmlCopyNode(node, 1);
       
   279     if(!result) 
       
   280         {
       
   281         XmlEngOOMTestL();
       
   282         User::Leave(KErrNoMemory);
       
   283         }
       
   284     // encrypt the data
       
   285     if(xmlSecEncCtxXmlEncrypt(tmpCtx, result, INTERNAL_NODEPTR(aNode)) < 0) 
       
   286         {
       
   287         xmlFreeNode( result );
       
   288         XmlEngOOMTestL();
       
   289         XmlSecErrorFlagTestL();
       
   290         User::Leave(KErrEncrypt);
       
   291         }
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // Encrypt xml node
       
   296 // ---------------------------------------------------------------------------
       
   297 // 
       
   298 EXPORT_C void CXmlSecEncrypt::EncryptXmlNodeKeyFromFileL(TXmlEngElement aNode,
       
   299                                                    RXmlEngDocument& aTemplate,
       
   300                                                    const TDesC8& aKeyFile,
       
   301                                                    const TDesC8& aKeyName,
       
   302                                                    TXmlSecKeyType aKeyFormat)
       
   303     {
       
   304     SetKeyFromFileL(aKeyFile,aKeyName,aKeyFormat);
       
   305     SetTemplateL(aTemplate);  
       
   306     EncryptXmlNodeL(aNode);
       
   307     }
       
   308     
       
   309 // ---------------------------------------------------------------------------
       
   310 // Encrypt xml document
       
   311 // ---------------------------------------------------------------------------
       
   312 // 
       
   313 EXPORT_C void CXmlSecEncrypt::EncryptXmlDocumentKeyFromFileL(RXmlEngDocument& aDoc,
       
   314                                                        RXmlEngDocument& aTemplate,
       
   315                                                        const TDesC8& aKeyFile,
       
   316                                                        const TDesC8& aKeyName,
       
   317                                                        TXmlSecKeyType aKeyFormat)
       
   318     {
       
   319     SetKeyFromFileL(aKeyFile,aKeyName,aKeyFormat);
       
   320     SetTemplateL(aTemplate);  
       
   321     EncryptXmlDocumentL(aDoc);
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // Encrypt xml node
       
   326 // ---------------------------------------------------------------------------
       
   327 // 
       
   328 EXPORT_C void CXmlSecEncrypt::EncryptXmlNodeKeyFromBufferL(TXmlEngElement aNode,
       
   329                                                      RXmlEngDocument& aTemplate,
       
   330                                                      const TDesC8& aKey,
       
   331                                                      const TDesC8& aKeyName,
       
   332                                                      TXmlSecKeyType aKeyFormat)
       
   333     {
       
   334     SetKeyFromBufferL(aKey,aKeyName,aKeyFormat);
       
   335     SetTemplateL(aTemplate);  
       
   336     EncryptXmlNodeL(aNode);
       
   337     }
       
   338     
       
   339 // ---------------------------------------------------------------------------
       
   340 // Encrypt xml document
       
   341 // ---------------------------------------------------------------------------
       
   342 // 
       
   343 EXPORT_C void CXmlSecEncrypt::EncryptXmlDocumentKeyFromBufferL(RXmlEngDocument& aDoc,
       
   344                                                          RXmlEngDocument& aTemplate,
       
   345                                                          const TDesC8& aKey,
       
   346                                                          const TDesC8& aKeyName,
       
   347                                                          TXmlSecKeyType aKeyFormat)
       
   348     {
       
   349     SetKeyFromBufferL(aKey,aKeyName,aKeyFormat);
       
   350     SetTemplateL(aTemplate);  
       
   351     EncryptXmlDocumentL(aDoc);
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // Decrypt xml node
       
   356 // ---------------------------------------------------------------------------
       
   357 // 
       
   358 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlNodeKeyFromFileL(TXmlEngElement aNode,
       
   359                                                       const TDesC8& aKeyFile,
       
   360                                                       const TDesC8& aKeyName,
       
   361                                                       TXmlSecKeyType aKeyFormat)
       
   362     {
       
   363     SetKeyFromFileL(aKeyFile,aKeyName,aKeyFormat);
       
   364     return DecryptXmlNodeL(aNode);
       
   365     }
       
   366     
       
   367 // ---------------------------------------------------------------------------
       
   368 // Decrypt xml document
       
   369 // ---------------------------------------------------------------------------
       
   370 // 
       
   371 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlDocumentKeyFromFileL(RXmlEngDocument& aDoc,
       
   372                                                           const TDesC8& aKeyFile,
       
   373                                                           const TDesC8& aKeyName,
       
   374                                                           TXmlSecKeyType aKeyFormat)
       
   375     {
       
   376     SetKeyFromFileL(aKeyFile,aKeyName,aKeyFormat);
       
   377     return DecryptXmlDocumentL(aDoc);
       
   378     }
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 // Decrypt xml node
       
   382 // ---------------------------------------------------------------------------
       
   383 // 
       
   384 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlNodeKeyFromBufferL(TXmlEngElement aNode,
       
   385                                                         const TDesC8& aKey,
       
   386                                                         const TDesC8& aKeyName,
       
   387                                                         TXmlSecKeyType aKeyFormat)
       
   388     {
       
   389     SetKeyFromBufferL(aKey,aKeyName,aKeyFormat);
       
   390     return DecryptXmlNodeL(aNode);
       
   391     }
       
   392     
       
   393 // ---------------------------------------------------------------------------
       
   394 // Decrypt xml document
       
   395 // ---------------------------------------------------------------------------
       
   396 // 
       
   397 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlDocumentKeyFromBufferL(RXmlEngDocument& aDoc,
       
   398                                                             const TDesC8& aKey,
       
   399                                                             const TDesC8& aKeyName,
       
   400                                                             TXmlSecKeyType aKeyFormat)
       
   401     {
       
   402     SetKeyFromBufferL(aKey,aKeyName,aKeyFormat);
       
   403     return DecryptXmlDocumentL(aDoc);
       
   404     }
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // Decrypt xml node
       
   408 // ---------------------------------------------------------------------------
       
   409 //    
       
   410 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlNodeL(TXmlEngElement aNode)
       
   411     {
       
   412     if(aNode.IsNull())
       
   413         {
       
   414         User::Leave(KErrWrongParameter);
       
   415         }
       
   416     
       
   417     xmlNodePtr node = NULL;
       
   418     xmlNodePtr root = INTERNAL_NODEPTR(aNode);
       
   419     // find start node
       
   420     node = xmlSecFindNode(root,xmlSecNodeEncryptedData, xmlSecEncNs);
       
   421     if(!node) 
       
   422         {
       
   423         User::Leave(KErrWrongParameter);    
       
   424         }
       
   425     
       
   426     xmlSecEncCtxPtr tmpCtx = ENC_CTX;
       
   427     
       
   428     Encrypt::ResetCtx(tmpCtx,NULL);    
       
   429     // decrypt data
       
   430     if((xmlSecEncCtxDecrypt(tmpCtx, node) < 0) 
       
   431             || (!tmpCtx->result))
       
   432         {
       
   433         XmlEngOOMTestL();
       
   434         XmlSecErrorFlagTestL();
       
   435         User::Leave(KErrDecrypt);
       
   436         }
       
   437     // if result is data not xml part it is returned as hbufc8*
       
   438     if(!tmpCtx->resultReplaced)
       
   439         {
       
   440         xmlSecByte* result = xmlSecBufferGetData(tmpCtx->result);
       
   441         if(result)
       
   442             {
       
   443             TPtrC8 ptr(result,tmpCtx->result->size);
       
   444             return ptr.AllocL();
       
   445             }
       
   446         }
       
   447     return NULL;
       
   448     }
       
   449 
       
   450 // ---------------------------------------------------------------------------
       
   451 // Decrypt xml document
       
   452 // ---------------------------------------------------------------------------
       
   453 //     
       
   454 EXPORT_C HBufC8* CXmlSecEncrypt::DecryptXmlDocumentL(RXmlEngDocument& aDocument)
       
   455     {
       
   456     if(aDocument.IsNull())
       
   457         {
       
   458         User::Leave(KErrWrongParameter);
       
   459         }
       
   460     return DecryptXmlNodeL(aDocument.DocumentElement());
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // Set template from file
       
   465 // ---------------------------------------------------------------------------
       
   466 // 
       
   467 EXPORT_C void CXmlSecEncrypt::SetTemplateFromFileL(const TDesC8& aTemplate)
       
   468     {
       
   469     XmlSecTemplate::SetTemplateFromFileL(iTemplate,aTemplate);
       
   470     }
       
   471     
       
   472 // ---------------------------------------------------------------------------
       
   473 // Set template from file
       
   474 // ---------------------------------------------------------------------------
       
   475 // 
       
   476 EXPORT_C void CXmlSecEncrypt::SetTemplateFromFileL(RFs& aRFs, const TDesC8& aTemplate)
       
   477     {
       
   478     XmlSecTemplate::SetTemplateFromFileL(iTemplate,aTemplate,aRFs);
       
   479     }
       
   480 
       
   481 // ---------------------------------------------------------------------------
       
   482 // Set template from buffer
       
   483 // ---------------------------------------------------------------------------
       
   484 // 
       
   485 EXPORT_C void CXmlSecEncrypt::SetTemplateFromBufferL(const TDesC8& aTemplate)
       
   486     {
       
   487     XmlSecTemplate::SetTemplateFromBufferL(iTemplate,aTemplate);
       
   488     }
       
   489  
       
   490 // ---------------------------------------------------------------------------
       
   491 // Set template from DOM tree
       
   492 // ---------------------------------------------------------------------------
       
   493 // 
       
   494 EXPORT_C void CXmlSecEncrypt::SetTemplateL(const RXmlEngDocument& aTemplate)
       
   495     {
       
   496     XmlSecTemplate::SetTemplateL(iTemplate,aTemplate);
       
   497     }
       
   498 
       
   499 // ---------------------------------------------------------------------------
       
   500 // Set key info node in template
       
   501 // ---------------------------------------------------------------------------
       
   502 // 
       
   503 EXPORT_C void CXmlSecEncrypt::SetKeyInfoL(const TDesC8& aKeyName)
       
   504     {
       
   505     XmlSecTemplate::SetKeyInfoL(iTemplate,aKeyName);    
       
   506     }
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // Set key info node in template
       
   510 // ---------------------------------------------------------------------------
       
   511 // 
       
   512 EXPORT_C void CXmlSecEncrypt::SetKeyInfoL(TXmlEngElement aKeyProp)
       
   513     {
       
   514     XmlSecTemplate::SetKeyInfoL(iTemplate,aKeyProp);
       
   515     }
       
   516     
       
   517 // ---------------------------------------------------------------------------
       
   518 // Create encryption template
       
   519 // ---------------------------------------------------------------------------
       
   520 // 
       
   521 EXPORT_C const RXmlEngDocument& CXmlSecEncrypt::CreateTemplateL(TXmlSecKeyType aAlgorithm, 
       
   522                                                                 TXmlSecDataType aDataType)
       
   523     {
       
   524     if(iTemplate.NotNull())
       
   525         {
       
   526         iTemplate.Close();
       
   527         }
       
   528     xmlNodePtr node = NULL;
       
   529     // create encryption template to encrypt XML file and replace 
       
   530     // its content with encryption result
       
   531     const xmlChar* mimeType = NULL;
       
   532     if(aDataType == CXmlSecEncrypt::ENode)
       
   533         {
       
   534         mimeType = xmlSecTypeEncElement;
       
   535         }
       
   536     else if (aDataType == CXmlSecEncrypt::ENodeContent)
       
   537         {
       
   538         mimeType = xmlSecTypeEncContent;
       
   539         }
       
   540     
       
   541     if (aAlgorithm == EAES256)
       
   542         {
       
   543         node = xmlSecTmplEncDataCreate(NULL, xmlSecTransformAes256CbcId,
       
   544 				NULL, mimeType, NULL, NULL);
       
   545         }
       
   546 	else if (aAlgorithm == E3DES)
       
   547 	    {
       
   548 	    node = xmlSecTmplEncDataCreate(NULL, xmlSecTransformDes3CbcId,
       
   549 			    NULL, mimeType, NULL, NULL);
       
   550 	    }		
       
   551     if(!node) 
       
   552         {
       
   553         XmlEngOOMTestL();
       
   554         XmlSecErrorFlagTestL();
       
   555 	    User::Leave(KErrTemplate);
       
   556 	    }
       
   557 
       
   558     // we want to put encrypted data in the <enc:CipherValue/> node
       
   559     if(!xmlSecTmplEncDataEnsureCipherValue(node)) 
       
   560         {
       
   561         xmlFreeNode( node );
       
   562         XmlEngOOMTestL();
       
   563         XmlSecErrorFlagTestL();
       
   564         User::Leave(KErrTemplate);
       
   565 	    }
       
   566 
       
   567     // add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to put key name in the signed document
       
   568     xmlNodePtr keyInfo = xmlSecTmplEncDataEnsureKeyInfo(node, NULL);
       
   569     if(!keyInfo) 
       
   570         {
       
   571         xmlFreeNode( node );
       
   572         XmlEngOOMTestL();
       
   573         XmlSecErrorFlagTestL();
       
   574         User::Leave(KErrTemplate);
       
   575         }
       
   576     
       
   577     XmlSecGlobalState* gs = XmlSecGetTls();
       
   578     CleanupStack::PushL(TCleanupItem(LibxmlNodeCleanup,(TAny*)node)); 
       
   579     iTemplate.OpenL(*gs->iDOMImpl,TXmlEngElement(node));
       
   580     CleanupStack::Pop(node);
       
   581     return iTemplate;
       
   582     }
       
   583 
       
   584 // ---------------------------------------------------------------------------
       
   585 // Return current encryption template
       
   586 // ---------------------------------------------------------------------------
       
   587 // 
       
   588 EXPORT_C const RXmlEngDocument& CXmlSecEncrypt::CurrentTemplate() const
       
   589     {
       
   590     return iTemplate;
       
   591     }
       
   592 
       
   593 // ---------------------------------------------------------------------------
       
   594 // Destroy current encryption template
       
   595 // ---------------------------------------------------------------------------
       
   596 // 
       
   597 EXPORT_C void CXmlSecEncrypt::DestroyCurrentTemplate()
       
   598     {
       
   599     XmlSecTemplate::DestroyTemplate(iTemplate);
       
   600     }
       
   601 
       
   602 // ---------------------------------------------------------------------------
       
   603 // Set key from file
       
   604 // ---------------------------------------------------------------------------
       
   605 // 
       
   606 EXPORT_C void CXmlSecEncrypt::SetKeyFromFileL(const TDesC8& aKeyFile,
       
   607                                         const TDesC8& aKeyName,
       
   608                                         TXmlSecKeyType aAlgorithm)
       
   609     {
       
   610     xmlSecEncCtxPtr tmp = ENC_CTX;
       
   611     if(tmp->encKey)
       
   612         {
       
   613         xmlSecKeyDestroy(tmp->encKey);
       
   614         tmp->encKey = NULL;
       
   615         }
       
   616     tmp->encKey = Encrypt::ReadKeyFromFileL(aKeyFile,aKeyName,aAlgorithm);
       
   617     }
       
   618 
       
   619 // ---------------------------------------------------------------------------
       
   620 // Set key from buffer
       
   621 // ---------------------------------------------------------------------------
       
   622 // 
       
   623 EXPORT_C void CXmlSecEncrypt::SetKeyFromBufferL(const TDesC8& aKeyFile,
       
   624                                           const TDesC8& aKeyName,
       
   625                                           TXmlSecKeyType aAlgorithm)
       
   626     {
       
   627     xmlSecEncCtxPtr tmp = ENC_CTX;
       
   628     if(tmp->encKey)
       
   629         {
       
   630         xmlSecKeyDestroy(tmp->encKey);
       
   631         tmp->encKey = NULL;
       
   632         }
       
   633     tmp->encKey = Encrypt::ReadKeyFromBufferL(aKeyFile,aKeyName,aAlgorithm);
       
   634     }
       
   635     
       
   636 // ---------------------------------------------------------------------------
       
   637 // Constructor
       
   638 // ---------------------------------------------------------------------------
       
   639 // 
       
   640 CXmlSecEncrypt::CXmlSecEncrypt()
       
   641     {
       
   642     iTemplate = RXmlEngDocument();
       
   643     }
       
   644 
       
   645 // ---------------------------------------------------------------------------
       
   646 // Second phase constructor
       
   647 // ---------------------------------------------------------------------------
       
   648 // 
       
   649 void CXmlSecEncrypt::ConstructL()
       
   650     {
       
   651     // create encryption context
       
   652     iEncCtx = xmlSecEncCtxCreate(NULL);
       
   653     if(!iEncCtx)
       
   654         {
       
   655         User::Leave(KErrNoMemory);
       
   656 	    }
       
   657     }
       
   658 
       
   659 // ---------------------------------------------------------------------------
       
   660 // Destructor
       
   661 // ---------------------------------------------------------------------------
       
   662 // 
       
   663 CXmlSecEncrypt::~CXmlSecEncrypt()
       
   664     {
       
   665     if(iTemplate.NotNull())
       
   666         {
       
   667         iTemplate.Close();
       
   668         }
       
   669     if(iEncCtx)
       
   670         {
       
   671         xmlSecEncCtxDestroy(ENC_CTX);
       
   672         }
       
   673     }