xmlsecurityengine/xmlseccrypto/src/xmlsecc_crypto.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /** 
       
     2  * XMLSec library
       
     3  *
       
     4  * This is free software; see Copyright file in the source
       
     5  * distribution for preciese wording.
       
     6  * 
       
     7  * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
       
     8  * Portion Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
       
     9  */
       
    10 #include "xmlsecc_globals.h"
       
    11 
       
    12 #include <e32def.h>
       
    13 #include <string.h>
       
    14 #include <random.h>
       
    15 #include "xmlsecc_config.h"
       
    16 #include "xmlsec_xmlsec.h"
       
    17 #include "xmlsec_keys.h"
       
    18 #include "xmlsec_transforms.h"
       
    19 #include "xmlsec_errors.h"
       
    20 #include "xmlsec_dl.h"
       
    21 #include "xmlsec_private.h"
       
    22 
       
    23 #include "xmlsecc_app.h"
       
    24 #include "xmlsecc_crypto.h"
       
    25 #include "xmlsecc_cryptowrapper.h"
       
    26 #include "xmlsecc_evpwrapper.h"
       
    27 #include "xmlsecc_x509.h"
       
    28 
       
    29 static xmlSecCryptoDLFunctionsPtr gXmlSecSymbianCryptoFunctions = NULL;
       
    30 
       
    31 /**
       
    32  * xmlSecCryptoGetFunctions_symbiancrypto:
       
    33  *
       
    34  * Gets the pointer to xmlsec-symbiancrypto functions table.
       
    35  *
       
    36  * Returns the xmlsec-symbiancrypto functions table or NULL if an error occurs.
       
    37  */
       
    38 xmlSecCryptoDLFunctionsPtr
       
    39 xmlSecCryptoGetFunctions_symbiancrypto(void) {
       
    40     static xmlSecCryptoDLFunctions functions;
       
    41     
       
    42     if(gXmlSecSymbianCryptoFunctions) {
       
    43 	return(gXmlSecSymbianCryptoFunctions);
       
    44     }
       
    45 
       
    46     memset(&functions, 0, sizeof(functions));
       
    47     gXmlSecSymbianCryptoFunctions = &functions;
       
    48 
       
    49     /**  
       
    50      * Crypto Init/shutdown
       
    51      */
       
    52     gXmlSecSymbianCryptoFunctions->cryptoInit 			= xmlSecSymbianCryptoInit;
       
    53     gXmlSecSymbianCryptoFunctions->cryptoShutdown 		= xmlSecSymbianCryptoShutdown;
       
    54     gXmlSecSymbianCryptoFunctions->cryptoKeysMngrInit 	= xmlSecSymbianCryptoKeysMngrInit;
       
    55 
       
    56     /**
       
    57      * Key data ids
       
    58      */
       
    59 #ifndef XMLSEC_NO_AES    
       
    60     gXmlSecSymbianCryptoFunctions->keyDataAesGetKlass	= xmlSecSymbianCryptoKeyDataAesGetKlass;
       
    61 #endif /* XMLSEC_NO_AES */
       
    62 
       
    63 #ifndef XMLSEC_NO_DES    
       
    64     gXmlSecSymbianCryptoFunctions->keyDataDesGetKlass 	= xmlSecSymbianCryptoKeyDataDesGetKlass;
       
    65 #endif /* XMLSEC_NO_DES */
       
    66 
       
    67 #ifndef XMLSEC_NO_HMAC  
       
    68     gXmlSecSymbianCryptoFunctions->keyDataHmacGetKlass 	= xmlSecSymbianCryptoKeyDataHmacGetKlass;
       
    69 #endif /* XMLSEC_NO_HMAC */   
       
    70 
       
    71 #ifndef XMLSEC_NO_RSA
       
    72 	gXmlSecSymbianCryptoFunctions->keyDataRsaGetKlass 	= xmlSecSymbianCryptoKeyDataRsaGetKlass;
       
    73 #endif /* XMLSEC_NO_RSA */ 
       
    74 
       
    75 #ifndef XMLSEC_NO_X509
       
    76     gXmlSecSymbianCryptoFunctions->keyDataX509GetKlass 	 = xmlSecSymbianCryptoKeyDataX509GetKlass;
       
    77     gXmlSecSymbianCryptoFunctions->keyDataRawX509CertGetKlass 	
       
    78                                               = xmlSecSymbianCryptoKeyDataRawX509CertGetKlass;
       
    79 #endif /* XMLSEC_NO_X509 */
       
    80 
       
    81     /**
       
    82      * Key data store ids
       
    83      */
       
    84 #ifndef XMLSEC_NO_X509
       
    85     gXmlSecSymbianCryptoFunctions->x509StoreGetKlass 	= xmlSecSymbianCryptoX509StoreGetKlass;
       
    86 #endif /* XMLSEC_NO_X509 */
       
    87 
       
    88     /**
       
    89      * Crypto transforms ids
       
    90      */
       
    91 #ifndef XMLSEC_NO_AES    
       
    92     gXmlSecSymbianCryptoFunctions->transformAes128CbcGetKlass 	
       
    93                                            = xmlSecSymbianCryptoTransformAes128CbcGetKlass;
       
    94     gXmlSecSymbianCryptoFunctions->transformAes192CbcGetKlass 	
       
    95                                            = xmlSecSymbianCryptoTransformAes192CbcGetKlass;
       
    96     gXmlSecSymbianCryptoFunctions->transformAes256CbcGetKlass 	
       
    97                                            = xmlSecSymbianCryptoTransformAes256CbcGetKlass;
       
    98 #endif /* XMLSEC_NO_AES */
       
    99 
       
   100 #ifndef XMLSEC_NO_DES    
       
   101     gXmlSecSymbianCryptoFunctions->transformDes3CbcGetKlass 
       
   102                                             = xmlSecSymbianCryptoTransformDes3CbcGetKlass;
       
   103 #endif /* XMLSEC_NO_DES */
       
   104 
       
   105 #ifndef XMLSEC_NO_HMAC
       
   106     gXmlSecSymbianCryptoFunctions->transformHmacSha1GetKlass 		
       
   107                                             = xmlSecSymbianCryptoTransformHmacSha1GetKlass;
       
   108     gXmlSecSymbianCryptoFunctions->transformHmacRipemd160GetKlass 	
       
   109                                             = xmlSecSymbianCryptoTransformHmacRipemd160GetKlass;
       
   110     gXmlSecSymbianCryptoFunctions->transformHmacMd5GetKlass 		
       
   111                                             = xmlSecSymbianCryptoTransformHmacMd5GetKlass;
       
   112 #endif /* XMLSEC_NO_HMAC */
       
   113 
       
   114 #ifndef XMLSEC_NO_SHA1    
       
   115     gXmlSecSymbianCryptoFunctions->transformSha1GetKlass 
       
   116                                             = xmlSecSymbianCryptoTransformSha1GetKlass;
       
   117 #endif /* XMLSEC_NO_SHA1 */
       
   118 
       
   119 
       
   120 #ifndef XMLSEC_NO_RSA
       
   121     gXmlSecSymbianCryptoFunctions->transformRsaSha1GetKlass 		
       
   122                                             = xmlSecSymbianCryptoTransformRsaSha1GetKlass;
       
   123 #endif /* XMLSEC_NO_RSA */
       
   124 
       
   125 
       
   126 
       
   127 
       
   128     /**
       
   129      * High level routines form xmlsec command line utility
       
   130      */ 
       
   131     gXmlSecSymbianCryptoFunctions->cryptoAppInit 			
       
   132                                                 = xmlSecSymbianCryptoAppInit;
       
   133     gXmlSecSymbianCryptoFunctions->cryptoAppShutdown 			
       
   134                                                 = xmlSecSymbianCryptoAppShutdown;
       
   135     gXmlSecSymbianCryptoFunctions->cryptoAppDefaultKeysMngrInit 	
       
   136                                                 = xmlSecSymbianCryptoAppDefaultKeysMngrInit;
       
   137     gXmlSecSymbianCryptoFunctions->cryptoAppDefaultKeysMngrAdoptKey 	
       
   138                                                 = xmlSecSymbianCryptoAppDefaultKeysMngrAdoptKey;
       
   139     gXmlSecSymbianCryptoFunctions->cryptoAppDefaultKeysMngrLoad 	
       
   140                                                 = xmlSecSymbianCryptoAppDefaultKeysMngrLoad;
       
   141     gXmlSecSymbianCryptoFunctions->cryptoAppDefaultKeysMngrSave 	
       
   142                                                 = xmlSecSymbianCryptoAppDefaultKeysMngrSave;
       
   143 #ifndef XMLSEC_NO_X509
       
   144     gXmlSecSymbianCryptoFunctions->cryptoAppKeysMngrCertLoad 		
       
   145                                                 = xmlSecSymbianCryptoAppKeysMngrCertLoad;
       
   146     gXmlSecSymbianCryptoFunctions->cryptoAppPkcs12Load  		
       
   147                                                 = xmlSecSymbianCryptoAppPkcs12Load; 
       
   148     gXmlSecSymbianCryptoFunctions->cryptoAppKeyCertLoad 		
       
   149                                                 = xmlSecSymbianCryptoAppKeyCertLoad;
       
   150 #endif /* XMLSEC_NO_X509 */
       
   151     gXmlSecSymbianCryptoFunctions->cryptoAppKeyLoadWithName		
       
   152                                                 = xmlSecSymbianCryptoAppKeyLoadWithName;     
       
   153     gXmlSecSymbianCryptoFunctions->cryptoAppDefaultPwdCallback		
       
   154                                            = (void*)xmlSecSymbianCryptoAppGetDefaultPwdCallback();
       
   155 
       
   156     return(gXmlSecSymbianCryptoFunctions);
       
   157 }
       
   158 
       
   159 
       
   160 /**
       
   161  * xmlSecSymbianCryptoInit:
       
   162  * 
       
   163  * XMLSec library specific crypto engine initialization. 
       
   164  *
       
   165  * Returns 0 on success or a negative value otherwise.
       
   166  */
       
   167 EXPORT_C
       
   168 int 
       
   169 xmlSecSymbianCryptoInit (void)  {
       
   170     /* Check loaded xmlsec library version */
       
   171     if(xmlSecCheckVersionExact() != 1) {
       
   172 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   173 		    NULL,
       
   174 		    "xmlSecCheckVersionExact",
       
   175 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   176 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   177 	return(-1);
       
   178     }
       
   179 
       
   180     if (sc_pkey_init() != 0)
       
   181     {
       
   182 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   183 		    NULL,
       
   184 		    "sc_pkey_init",
       
   185 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   186 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   187 	return(-1);
       
   188     
       
   189     }
       
   190 
       
   191     /* register our klasses */
       
   192     if(xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms(
       
   193                                                 xmlSecCryptoGetFunctions_symbiancrypto()) < 0) {
       
   194 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   195 		    NULL,
       
   196 		    "xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms",
       
   197 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   198 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   199 	return(-1);
       
   200     }
       
   201     
       
   202     return(0);
       
   203 }
       
   204 
       
   205 /**
       
   206  * xmlSecSymbianCryptoShutdown:
       
   207  * 
       
   208  * XMLSec library specific crypto engine shutdown. 
       
   209  *
       
   210  * Returns 0 on success or a negative value otherwise.
       
   211  */
       
   212 EXPORT_C
       
   213 int 
       
   214 xmlSecSymbianCryptoShutdown(void) {
       
   215 	sc_pkey_shutdown();
       
   216     return(0);
       
   217 }
       
   218 
       
   219 /**
       
   220  * xmlSecSymbianCryptoKeysMngrInit:
       
   221  * @mngr:		the pointer to keys manager.
       
   222  *
       
   223  * Adds SymbianCrypto specific key data stores in keys manager.
       
   224  *
       
   225  * Returns 0 on success or a negative value otherwise.
       
   226  */
       
   227 EXPORT_C
       
   228 int
       
   229 xmlSecSymbianCryptoKeysMngrInit(xmlSecKeysMngrPtr mngr) {
       
   230     int ret;
       
   231     
       
   232     xmlSecAssert2(mngr, -1);
       
   233 
       
   234     /* Implementation from OpenSSL */
       
   235 #ifndef XMLSEC_NO_X509
       
   236     /* create x509 store if needed */
       
   237     if(!xmlSecKeysMngrGetDataStore(mngr, xmlSecSymbianCryptoX509StoreId)) {
       
   238 	xmlSecKeyDataStorePtr x509Store;
       
   239 
       
   240     x509Store = xmlSecKeyDataStoreCreate(xmlSecSymbianCryptoX509StoreId);
       
   241 	if(!x509Store) {
       
   242 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   243 			NULL,
       
   244 			"xmlSecKeyDataStoreCreate",
       
   245 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   246 			"xmlSecOpenSSLX509StoreId");
       
   247 	    return(-1);   
       
   248 	}
       
   249     
       
   250     ret = xmlSecKeysMngrAdoptDataStore(mngr, x509Store);
       
   251     if(ret < 0) {
       
   252 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   253 			NULL,
       
   254 			"xmlSecKeysMngrAdoptDataStore",
       
   255 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   256 			XMLSEC_ERRORS_NO_MESSAGE);
       
   257 	    xmlSecKeyDataStoreDestroy(x509Store);
       
   258 	    return(-1); 
       
   259 	}
       
   260     }
       
   261 #endif /* XMLSEC_NO_X509 */        
       
   262     return(0);
       
   263 }
       
   264 
       
   265 /**
       
   266  * xmlSecSymbianCryptoGenerateRandom:
       
   267  * @buffer:		the destination buffer.
       
   268  * @size:		the numer of bytes to generate.
       
   269  *
       
   270  * Generates @size random bytes and puts result in @buffer.
       
   271  *
       
   272  * Returns 0 on success or a negative value otherwise.
       
   273  */
       
   274 EXPORT_C
       
   275 int
       
   276 xmlSecSymbianCryptoGenerateRandom(xmlSecBufferPtr buffer, xmlSecSize size) {	
       
   277     int ret;
       
   278     
       
   279     xmlSecAssert2(buffer, -1);
       
   280     xmlSecAssert2(size > 0, -1);
       
   281 
       
   282     ret = xmlSecBufferSetSize(buffer, size);
       
   283     if(ret < 0) {
       
   284 	xmlSecError(XMLSEC_ERRORS_HERE, 
       
   285 		    NULL,
       
   286 		    "xmlSecBufferSetSize",
       
   287 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   288 		    "size=%d", size);
       
   289 	return(-1);
       
   290     }
       
   291         
       
   292     /* get random data */
       
   293     sc_randomize(xmlSecBufferGetData(buffer), size, SC_STRONG_RANDOM); 
       
   294     /*
       
   295     CSystemRandom* rand=CSystemRandom::NewLC();
       
   296     unsigned char* bytes=xmlSecBufferGetData(buffer);
       
   297     TPtr8 ptr(bytes, xmlSecBufferGetMaxSize(buffer));
       
   298     rand->GenerateBytesL(ptr);
       
   299     CleanupStack::PopAndDestroy();
       
   300     */
       
   301     return(0);
       
   302 }