xmlsecurityengine/xmlseccrypto/src/xmlsecc_app.cpp
changeset 0 e35f40988205
child 8 e65204f75c47
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 <string.h>
       
    11 
       
    12 #include "xmlsecc_globals.h"
       
    13 
       
    14 #include "xmlsecc_config.h"
       
    15 #include "xmlsec_xmlsec.h"
       
    16 #include "xmlsec_keys.h"
       
    17 #include "xmlsec_transforms.h"
       
    18 #include "xmlsec_errors.h"
       
    19 
       
    20 #include "xmlsecc_app.h"
       
    21 #include "xmlsecc_crypto.h"
       
    22 #include "xmlsecc_evp.h"
       
    23 
       
    24 #ifndef XMLSEC_NO_X509   
       
    25 #include "xmlsecc_x509.h"
       
    26 #include "xmlsecc_x509wrapper.h"
       
    27 #include "xmlsecc_bio.h"
       
    28 #endif /* XMLSEC_NO_X509 */ 
       
    29 
       
    30 /**
       
    31  * xmlSecSymbianCryptoAppInit:
       
    32  * @config:		the path to SymbianCrypto configuration (unused).
       
    33  * 
       
    34  * General crypto engine initialization. This function is used
       
    35  * by XMLSec command line utility and called before 
       
    36  * @xmlSecInit function.
       
    37  *
       
    38  * Returns 0 on success or a negative value otherwise.
       
    39  */
       
    40 EXPORT_C
       
    41 int
       
    42 xmlSecSymbianCryptoAppInit(const char* config ATTRIBUTE_UNUSED) {
       
    43     int ret(0);
       
    44     if( ret ) {
       
    45 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    46 		    NULL,
       
    47 		    "symbiancrypto_init",
       
    48 		    XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
    49 		    "ret=%d", ret);
       
    50 	return(-1);
       
    51     }
       
    52     return(0);
       
    53 }
       
    54 
       
    55 /**
       
    56  * xmlSecSymbianCryptoAppShutdown:
       
    57  * 
       
    58  * General crypto engine shutdown. This function is used
       
    59  * by XMLSec command line utility and called after 
       
    60  * @xmlSecShutdown function.
       
    61  *
       
    62  * Returns 0 on success or a negative value otherwise.
       
    63  */
       
    64 EXPORT_C
       
    65 int
       
    66 xmlSecSymbianCryptoAppShutdown(void) {
       
    67     return(0);
       
    68 }
       
    69 
       
    70 /**
       
    71  * xmlSecSymbianCryptoAppKeyLoadWithName:
       
    72  * @filename:		the key filename.
       
    73  * @format:		the key file format.
       
    74  * @keyname:    the assigned name of key
       
    75  * @pwd:		the key file password.
       
    76  * @pwdCallback:	the key password callback.
       
    77  * @pwdCallbackCtx:	the user context for password callback.
       
    78  *
       
    79  * Reads key from the a file (not implemented yet).
       
    80  *
       
    81  * Returns pointer to the key or NULL if an error occurs.
       
    82  */
       
    83 EXPORT_C
       
    84 xmlSecKeyPtr
       
    85 xmlSecSymbianCryptoAppKeyLoadWithName(const char* filename, xmlSecKeyDataFormat format,
       
    86 			const char* keyname,
       
    87 			const char* pwd,
       
    88 			void* pwdCallback,
       
    89 			void* pwdCallbackCtx) {			
       
    90     xmlSecKeyPtr key = NULL;
       
    91     EVP_PKEY* pKey = NULL; 
       
    92     BIO* bio;
       
    93     xmlSecKeyDataPtr data;  
       
    94     			
       
    95     xmlSecAssert2(filename, NULL);
       
    96     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
       
    97      
       
    98     bio = BIO_new_file(filename, (const char*)"rb", keyname);
       
    99     if(!bio) {
       
   100 	return(NULL);    
       
   101     }	
       
   102     
       
   103     key = xmlSecSymbianCryptoAppKeyLoadBIO (bio, format, pwd, pwdCallback, pwdCallbackCtx);
       
   104     BIO_free(bio);
       
   105     if(!key) {
       
   106 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   107 		    NULL,
       
   108 		    "xmlSecOpenSSLAppKeyLoadBIO",
       
   109 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   110 		    "filename=%s", 
       
   111 		    xmlSecErrorsSafeString(filename));
       
   112 	return(NULL);
       
   113     }
       
   114     
       
   115     if(xmlSecKeySetName(key, (const unsigned char*)keyname) < 0) {
       
   116 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   117 		    NULL,
       
   118 		    "xmlSecKeySetName",
       
   119 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   120 		    "filename=%s", 
       
   121 		    xmlSecErrorsSafeString(filename));
       
   122 	xmlSecKeyDestroy(key);
       
   123 	return(NULL);
       
   124     }
       
   125         
       
   126     return(key);
       
   127 }
       
   128 
       
   129 /**
       
   130  * xmlSecSymbianCryptoAppKeyLoadMemory:
       
   131  * @data:		the binary key data.
       
   132  * @dataSize:		the size of binary key.
       
   133  * @format:		the key file format.
       
   134  * @pwd:		the key file password.
       
   135  * @pwdCallback:	the key password callback.
       
   136  * @pwdCallbackCtx:	the user context for password callback.
       
   137  *
       
   138  * Reads key from the memory buffer (not implemented yet).
       
   139  *
       
   140  * Returns pointer to the key or NULL if an error occurs.
       
   141  */
       
   142 EXPORT_C
       
   143 xmlSecKeyPtr
       
   144 xmlSecSymbianCryptoAppKeyLoadMemoryWithName(const xmlSecByte* data, xmlSecSize dataSize, 
       
   145 			xmlSecKeyDataFormat format, const char* keyname, const char* pwd, 
       
   146 			void* pwdCallback, void* pwdCallbackCtx) {
       
   147     
       
   148     xmlSecKeyPtr key = NULL;
       
   149     EVP_PKEY* pKey = NULL; 
       
   150     BIO* bio;
       
   151     
       
   152     bio = BIO_new_buffer((const char*)data,dataSize,keyname);
       
   153     if(!bio) {
       
   154 	return(NULL);    
       
   155     }
       
   156     		
       
   157     key = xmlSecSymbianCryptoAppKeyLoadBIO(bio, format, pwd, pwdCallback, pwdCallbackCtx);
       
   158     BIO_free(bio);
       
   159     if(!key) {
       
   160 	    return(NULL);
       
   161     }    
       
   162             
       
   163     if(xmlSecKeySetName(key, (const unsigned char*)keyname) < 0) {
       
   164         xmlSecKeyDestroy(key);
       
   165 	    return(NULL);
       
   166     }
       
   167     return(key);
       
   168 }
       
   169 
       
   170 /**
       
   171  * xmlSecOpenSSLAppKeyLoadBIO:
       
   172  * @bio:		the key BIO.
       
   173  * @format:		the key file format.
       
   174  * @pwd:		the key file password.
       
   175  * @pwdCallback:	the key password callback.
       
   176  * @pwdCallbackCtx:	the user context for password callback.
       
   177  *
       
   178  * Reads key from the an OpenSSL BIO object.
       
   179  *
       
   180  * Returns pointer to the key or NULL if an error occurs.
       
   181  */
       
   182 EXPORT_C
       
   183 xmlSecKeyPtr
       
   184 xmlSecSymbianCryptoAppKeyLoadBIO(BIO* bio, xmlSecKeyDataFormat format,
       
   185 			const char* pwd, void* pwdCallback, 
       
   186 			void* pwdCallbackCtx) {
       
   187 
       
   188     xmlSecKeyPtr key = NULL;
       
   189     xmlSecKeyDataPtr data;
       
   190     EVP_PKEY* pKey = NULL;    
       
   191     int ret(-1);
       
   192 
       
   193     xmlSecAssert2(bio, NULL);
       
   194     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
       
   195 
       
   196     switch (format)
       
   197     {
       
   198 		case xmlSecKeyDataFormatDer:	
       
   199         /* try to read private key first */           
       
   200 	    if(!pKey) {
       
   201        	    // go to start of the file and try to read public key 
       
   202 		    pKey = d2i_PUBKEY_bio(bio);
       
   203 		    if(!pKey) {
       
   204 			xmlSecError(XMLSEC_ERRORS_HERE,
       
   205 				    NULL,
       
   206 				    "d2i_PrivateKey_bio and d2i_PUBKEY_bio",
       
   207 				    XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
   208 				    XMLSEC_ERRORS_NO_MESSAGE);
       
   209 			return(NULL);
       
   210 		    }
       
   211 		}
       
   212 				
       
   213 		break;
       
   214     	case xmlSecKeyDataFormatPkcs8Der:
       
   215         /* try to read private key first */    
       
   216 		pKey = d2i_PKCS8PrivateKey_bio(bio, pwdCallback, pwdCallbackCtx);
       
   217 	    if(!pKey) {
       
   218 		    xmlSecError(XMLSEC_ERRORS_HERE,
       
   219 				NULL,
       
   220 				"sc_PrivateKey_read",
       
   221 				XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
   222 				XMLSEC_ERRORS_NO_MESSAGE);
       
   223 		    return(NULL);
       
   224 		}    					    		  
       
   225     	break;
       
   226     	default:
       
   227 			xmlSecError(XMLSEC_ERRORS_HERE,
       
   228 		    NULL,
       
   229 		    NULL,
       
   230 		    XMLSEC_ERRORS_R_INVALID_FORMAT,
       
   231 		    "format=%d", format); 
       
   232 			return(NULL);    	
       
   233     } 
       
   234 
       
   235 	data = xmlSecSymbianCryptoEvpKeyAdopt(pKey);
       
   236     if(!data) {
       
   237 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   238 		    NULL,
       
   239 		    "xmlSecSymbianCryptoEvpKeyAdopt",
       
   240 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   241 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   242 	sc_pkey_free(pKey);
       
   243 	return(NULL);	    
       
   244     }    
       
   245 
       
   246     key = xmlSecKeyCreate();
       
   247     if(!key) {
       
   248 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   249 		    NULL,
       
   250 		    "xmlSecKeyCreate",
       
   251 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   252 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   253 	xmlSecKeyDataDestroy(data);
       
   254 	return(NULL);
       
   255     }
       
   256     
       
   257     ret = xmlSecKeySetValue(key, data);
       
   258     if(ret < 0) {
       
   259 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   260 		    NULL,
       
   261 		    "xmlSecKeySetValue",
       
   262 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   263 		    "data=%s",
       
   264 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)));
       
   265 	xmlSecKeyDestroy(key);
       
   266 	xmlSecKeyDataDestroy(data);
       
   267 	return(NULL);
       
   268     } 
       
   269     
       
   270     return(key);
       
   271 }
       
   272 
       
   273 
       
   274 #ifndef XMLSEC_NO_X509
       
   275 static X509*		xmlSecSymbianCryptoAppCertLoadBIO		(BIO* bio,
       
   276 								 xmlSecKeyDataFormat format);
       
   277 
       
   278 /**
       
   279  * xmlSecSymbianCryptoAppKeyCertLoad:
       
   280  * @key:		the pointer to key.
       
   281  * @filename:		the certificate filename.
       
   282  * @format:		the certificate file format.
       
   283  *
       
   284  * Reads the certificate from $@filename and adds it to key
       
   285  * (not implemented yet).
       
   286  * 
       
   287  * Returns 0 on success or a negative value otherwise.
       
   288  */
       
   289 EXPORT_C
       
   290 int		
       
   291 xmlSecSymbianCryptoAppKeyCertLoad(xmlSecKeyPtr key, const char* filename, 
       
   292 			  xmlSecKeyDataFormat format) {
       
   293 	BIO* bio(NULL);
       
   294 	int ret(-1);
       
   295 	
       
   296     xmlSecAssert2(key, -1);
       
   297     xmlSecAssert2(filename, -1);
       
   298     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   299     
       
   300     /* Implementation from OpenSSL */
       
   301     bio = BIO_new_file(filename, "rb", NULL);
       
   302     if(!bio) {
       
   303 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   304 		    NULL,
       
   305 		    "BIO_new_file",
       
   306 		    XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
   307 		    "filename=%s", 
       
   308 		    xmlSecErrorsSafeString(filename));
       
   309 	return(-1);    
       
   310     }
       
   311 
       
   312     ret = xmlSecSymbianCryptoAppKeyCertLoadBIO (key, bio, format);
       
   313     if(ret < 0) {
       
   314 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   315 		    NULL,
       
   316 		    "xmlSecSymbianCryptoAppKeyCertLoadBIO",
       
   317 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   318 		    "filename=%s", 
       
   319 		    xmlSecErrorsSafeString(filename));
       
   320 	BIO_free(bio);
       
   321 	return(-1);
       
   322     }
       
   323     
       
   324     BIO_free(bio);
       
   325     return(0);
       
   326     
       
   327 }
       
   328 
       
   329 /**
       
   330  * xmlSecSymbianCryptoAppKeyCertLoadBIO:
       
   331  * @key:		the pointer to key.
       
   332  * @bio:		the certificate bio.
       
   333  * @format:		the certificate file format.
       
   334  *
       
   335  * Reads the certificate from memory buffer and adds it to key.
       
   336  * 
       
   337  * Returns 0 on success or a negative value otherwise.
       
   338  */
       
   339 EXPORT_C
       
   340 int		
       
   341 xmlSecSymbianCryptoAppKeyCertLoadBIO(xmlSecKeyPtr key, BIO* bio, xmlSecKeyDataFormat format) {
       
   342 
       
   343     xmlSecKeyDataFormat certFormat;
       
   344     xmlSecKeyDataPtr data;
       
   345     X509* cert(NULL);
       
   346     int ret(-1);
       
   347     
       
   348     xmlSecAssert2(key, -1);
       
   349     xmlSecAssert2(bio, -1);
       
   350     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   351     
       
   352     data = xmlSecKeyEnsureData(key, xmlSecSymbianCryptoKeyDataX509Id);
       
   353     if(!data) {
       
   354 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   355 		    NULL,
       
   356 		    "xmlSecKeyEnsureData",		    
       
   357 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   358 		    "transform=%s",
       
   359 		    xmlSecErrorsSafeString(xmlSecTransformKlassGetName(xmlSecSymbianCryptoKeyDataX509Id)));
       
   360 	return(-1);
       
   361     }
       
   362 
       
   363     /* adjust cert format */
       
   364     switch(format) {
       
   365     case xmlSecKeyDataFormatPkcs8Pem:
       
   366 	certFormat = xmlSecKeyDataFormatPem;
       
   367 	break;
       
   368     case xmlSecKeyDataFormatPkcs8Der:
       
   369 	certFormat = xmlSecKeyDataFormatDer;
       
   370 	break;
       
   371     default:
       
   372 	certFormat = format;
       
   373     }
       
   374 
       
   375     cert = xmlSecSymbianCryptoAppCertLoadBIO(bio, certFormat);
       
   376     if(!cert) {
       
   377 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   378 		    NULL,
       
   379 		    "xmlSecSymbianCryptoAppCertLoad", 
       
   380 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   381 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   382 	return(-1);    
       
   383     }    	
       
   384     
       
   385     ret = xmlSecSymbianCryptoKeyDataX509AdoptKeyCert(data, cert);
       
   386     if(ret < 0) {
       
   387 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   388 		    NULL,
       
   389 		    "xmlSecSymbianCryptoKeyDataX509AdoptCert",
       
   390 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   391 		    "data=%s",
       
   392 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)));
       
   393 	X509_free(cert);
       
   394 	return(-1);    
       
   395     }
       
   396     
       
   397     return(0);        
       
   398 }
       
   399 
       
   400 
       
   401 /**
       
   402  * xmlSecSymbianCryptoAppKeyCertLoadMemory:
       
   403  * @key:		the pointer to key.
       
   404  * @data:		the certificate binary data.
       
   405  * @dataSize:		the certificate binary data size.
       
   406  * @format:		the certificate file format.
       
   407  *
       
   408  * Reads the certificate from memory buffer and adds it to key (not implemented yet).
       
   409  * 
       
   410  * Returns 0 on success or a negative value otherwise.
       
   411  */
       
   412 EXPORT_C
       
   413 int		
       
   414 xmlSecSymbianCryptoAppKeyCertLoadMemory(xmlSecKeyPtr key, 
       
   415                                     const xmlSecByte* data, 
       
   416                                     xmlSecSize dataSize, 
       
   417 			        xmlSecKeyDataFormat format) {
       
   418     xmlSecAssert2(key, -1);
       
   419     xmlSecAssert2(data, -1);
       
   420     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   421     int ret(-1);
       
   422     BIO* bio(NULL);
       
   423     
       
   424     bio = BIO_new_buffer((const char*)data,dataSize,NULL);
       
   425     if(!bio) {
       
   426 	    return(-1);    
       
   427     }
       
   428     
       
   429     ret = xmlSecSymbianCryptoAppKeyCertLoadBIO (key, bio, format);
       
   430     BIO_free(bio);
       
   431     if(ret < 0) {
       
   432 	    return(-1);
       
   433     }
       
   434     return(0);
       
   435 }
       
   436 
       
   437 /**
       
   438  * xmlSecSymbianCryptoAppPEMReadPrivateKey:
       
   439  * @filename:		the PEM key filename.
       
   440  * @pwd:		the PEM file password.
       
   441  * @pwdCallback:	the password callback.
       
   442  * @pwdCallbackCtx:	the user context for password callback.
       
   443  *
       
   444  * Reads key and all associated certificates from the PKCS12 file
       
   445  * (not implemented yet).
       
   446  * For uniformity, call xmlSecSymbianCryptoAppKeyLoad instead of this function. Pass
       
   447  * in format=xmlSecKeyDataFormatPkcs12.
       
   448  *
       
   449  * Returns pointer to the key or NULL if an error occurs.
       
   450  */
       
   451 xmlSecKeyPtr	
       
   452 xmlSecSymbianCryptoAppPEMReadPrivateKey(const char* filename, 
       
   453 			  const char* pwd ATTRIBUTE_UNUSED,
       
   454 		          void* pwdCallback ATTRIBUTE_UNUSED, 
       
   455 			  void* pwdCallbackCtx ATTRIBUTE_UNUSED) {
       
   456     xmlSecAssert2(filename, NULL);
       
   457 
       
   458     /* Unimplemented - for futher use */
       
   459     xmlSecError(XMLSEC_ERRORS_HERE,
       
   460 		NULL,
       
   461 		"xmlSecSymbianCryptoAppPEMReadPrivateKey",
       
   462 		XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
       
   463 		XMLSEC_ERRORS_NO_MESSAGE);
       
   464     return(NULL); 
       
   465 }
       
   466 
       
   467 
       
   468 /**
       
   469  * xmlSecSymbianCryptoAppPkcs12Load:
       
   470  * @filename:		the PKCS12 key filename.
       
   471  * @pwd:		the PKCS12 file password.
       
   472  * @pwdCallback:	the password callback.
       
   473  * @pwdCallbackCtx:	the user context for password callback.
       
   474  *
       
   475  * Reads key and all associated certificates from the PKCS12 file
       
   476  * (not implemented yet).
       
   477  * For uniformity, call xmlSecSymbianCryptoAppKeyLoad instead of this function. Pass
       
   478  * in format=xmlSecKeyDataFormatPkcs12.
       
   479  *
       
   480  * Returns pointer to the key or NULL if an error occurs.
       
   481  */
       
   482 EXPORT_C
       
   483 xmlSecKeyPtr	
       
   484 xmlSecSymbianCryptoAppPkcs12Load(const char* filename, 
       
   485 			  const char* pwd ATTRIBUTE_UNUSED,
       
   486 		          void* pwdCallback ATTRIBUTE_UNUSED, 
       
   487 			  void* pwdCallbackCtx ATTRIBUTE_UNUSED) {
       
   488     xmlSecAssert2(filename, NULL);
       
   489 
       
   490     /* Unimplemented - for futher use */
       
   491     xmlSecError(XMLSEC_ERRORS_HERE,
       
   492 		NULL,
       
   493 		"xmlSecSymbianCryptoAppPkcs12Load",
       
   494 		XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
       
   495 		XMLSEC_ERRORS_NO_MESSAGE);
       
   496     return(NULL); 
       
   497 }
       
   498 
       
   499 /**
       
   500  * xmlSecSymbianCryptoAppPkcs12LoadMemory:
       
   501  * @data:		the PKCS12 binary data.
       
   502  * @dataSize:		the PKCS12 binary data size.
       
   503  * @pwd:		the PKCS12 file password.
       
   504  * @pwdCallback:	the password callback.
       
   505  * @pwdCallbackCtx:	the user context for password callback.
       
   506  *
       
   507  * Reads key and all associated certificates from the PKCS12 data in memory buffer.
       
   508  * For uniformity, call xmlSecSymbianCryptoAppKeyLoadMemory instead of this function. Pass
       
   509  * in format=xmlSecKeyDataFormatPkcs12 (not implemented yet).
       
   510  *
       
   511  * Returns pointer to the key or NULL if an error occurs.
       
   512  */
       
   513 EXPORT_C
       
   514 xmlSecKeyPtr	
       
   515 xmlSecSymbianCryptoAppPkcs12LoadMemory(const xmlSecByte* data, xmlSecSize dataSize, 
       
   516 			   const char* pwd, void* pwdCallback, 
       
   517 			   void* pwdCallbackCtx) {
       
   518     xmlSecAssert2(data, NULL);
       
   519     /* Unimplemented - for futher use */
       
   520     xmlSecError(XMLSEC_ERRORS_HERE,
       
   521 		NULL,
       
   522 		"xmlSecSymbianCryptoAppPkcs12LoadMemory",
       
   523 		XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
       
   524 		XMLSEC_ERRORS_NO_MESSAGE);
       
   525     return(NULL); 
       
   526 }
       
   527 
       
   528 /**
       
   529  * xmlSecSymbianCryptoAppKeysMngrCertLoad:
       
   530  * @mngr: 		the keys manager.
       
   531  * @filename: 		the certificate file.
       
   532  * @format:		the certificate file format.
       
   533  * @type: 		the flag that indicates is the certificate in @filename
       
   534  *    			trusted or not.
       
   535  * 
       
   536  * Reads cert from @filename and adds to the list of trusted or known
       
   537  * untrusted certs in @store (not implemented yet).
       
   538  *
       
   539  * Returns 0 on success or a negative value otherwise.
       
   540  */
       
   541 EXPORT_C
       
   542 int
       
   543 xmlSecSymbianCryptoAppKeysMngrCertLoad(xmlSecKeysMngrPtr mngr, const char *filename, 
       
   544 				xmlSecKeyDataFormat format, 
       
   545 				xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
       
   546 	BIO* bio(NULL);
       
   547 	int ret(-1);
       
   548 					
       
   549     xmlSecAssert2(mngr, -1);
       
   550     xmlSecAssert2(filename, -1);
       
   551     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   552 
       
   553     /* Implementation from OpenSSL */
       
   554     bio = BIO_new_file(filename, "rb", NULL);
       
   555     if(!bio) {
       
   556 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   557 		    NULL,
       
   558 		    "BIO_new_file",
       
   559 		    XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
   560 		    "filename=%s", 
       
   561 		    xmlSecErrorsSafeString(filename));
       
   562 	return(-1);    
       
   563     }
       
   564 
       
   565     ret = xmlSecSymbianCryptoAppKeysMngrCertLoadBIO(mngr, bio, format, type);
       
   566     if(ret < 0) {
       
   567 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   568 		    NULL,
       
   569 		    "xmlSecSymbianCryptoAppKeysMngrCertLoadBIO",
       
   570 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   571 		    "filename=%s", 
       
   572 		    xmlSecErrorsSafeString(filename));
       
   573 	BIO_free(bio);
       
   574 	return(-1);
       
   575     }
       
   576     
       
   577     BIO_free(bio);    
       
   578     return(0);
       
   579 }
       
   580 
       
   581 /**
       
   582  * xmlSecSymbianCryptoAppKeysMngrCertLoadMemory:
       
   583  * @mngr: 		the keys manager.
       
   584  * @data:		the certificate binary data.
       
   585  * @dataSize:		the certificate binary data size.
       
   586  * @format:		the certificate file format.
       
   587  * @type: 		the flag that indicates is the certificate trusted or not.
       
   588  * 
       
   589  * Reads cert from binary buffer @data and adds to the list of trusted or known
       
   590  * untrusted certs in @store (not implemented yet).
       
   591  *
       
   592  * Returns 0 on success or a negative value otherwise.
       
   593  */
       
   594 EXPORT_C
       
   595 int
       
   596 xmlSecSymbianCryptoAppKeysMngrCertLoadMemory(xmlSecKeysMngrPtr mngr, const xmlSecByte* data,
       
   597 				    xmlSecSize dataSize, xmlSecKeyDataFormat format, 
       
   598 				    xmlSecKeyDataType type) {
       
   599     xmlSecAssert2(mngr, -1);
       
   600     xmlSecAssert2(data, -1);
       
   601     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   602 
       
   603 	BIO* bio(NULL);
       
   604 	int ret(-1);
       
   605 
       
   606     bio = BIO_new_buffer((const char*)data,dataSize, NULL);
       
   607     if(!bio) {
       
   608     	return(-1);    
       
   609     }
       
   610 
       
   611     ret = xmlSecSymbianCryptoAppKeysMngrCertLoadBIO(mngr, bio, format, type);
       
   612     BIO_free(bio);
       
   613     if(ret < 0) {
       
   614     	return(-1);
       
   615     }
       
   616     return(0);
       
   617 }
       
   618 
       
   619 static X509*	
       
   620 xmlSecSymbianCryptoAppCertLoadBIO(BIO* bio, xmlSecKeyDataFormat format) {
       
   621     X509 *cert;
       
   622     
       
   623     xmlSecAssert2(bio, NULL);
       
   624     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
       
   625 
       
   626     switch(format) {
       
   627     case xmlSecKeyDataFormatDer:
       
   628     case xmlSecKeyDataFormatCertDer:
       
   629 	cert = d2i_X509_bio(bio);
       
   630 	if(!cert) {
       
   631 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   632 			NULL,
       
   633 			"d2i_X509_bio",
       
   634 			XMLSEC_ERRORS_R_CRYPTO_FAILED,
       
   635 			XMLSEC_ERRORS_NO_MESSAGE);
       
   636 	    return(NULL);    
       
   637 	}
       
   638 	break;
       
   639     default:
       
   640 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   641 		    NULL,
       
   642 		    NULL,
       
   643 		    XMLSEC_ERRORS_R_INVALID_FORMAT,
       
   644 		    "format=%d", format); 
       
   645 	return(NULL);
       
   646     }
       
   647         	
       
   648     return(cert);
       
   649 }
       
   650 
       
   651 
       
   652 /**
       
   653  * xmlSecSymbianCryptoAppKeysMngrCertLoadBIO:
       
   654  * @mngr: 		the keys manager.
       
   655  * @bio: 		the certificate BIO.
       
   656  * @format:		the certificate file format.
       
   657  * @type: 		the flag that indicates is the certificate trusted or not.
       
   658  * 
       
   659  * Reads cert from an SymbianCrypto BIO object and adds to the list of trusted or known
       
   660  * untrusted certs in @store.
       
   661  *
       
   662  * Returns 0 on success or a negative value otherwise.
       
   663  */
       
   664 EXPORT_C
       
   665 int
       
   666 xmlSecSymbianCryptoAppKeysMngrCertLoadBIO(xmlSecKeysMngrPtr mngr, BIO* bio, 
       
   667 				    xmlSecKeyDataFormat format, xmlSecKeyDataType type) {
       
   668     xmlSecKeyDataStorePtr x509Store;
       
   669     X509* cert(NULL);
       
   670     int ret(-1);
       
   671 
       
   672     xmlSecAssert2(mngr, -1);
       
   673     xmlSecAssert2(bio, -1);
       
   674     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
       
   675     
       
   676     x509Store = xmlSecKeysMngrGetDataStore(mngr, xmlSecSymbianCryptoX509StoreId);
       
   677     if(!x509Store) {
       
   678 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   679 		    NULL,
       
   680 		    "xmlSecKeysMngrGetDataStore",
       
   681 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   682 		    "xmlSecSymbianCryptoX509StoreId");
       
   683 	return(-1);
       
   684     }
       
   685 
       
   686     cert = xmlSecSymbianCryptoAppCertLoadBIO(bio, format);
       
   687     if(!cert) {
       
   688 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   689 		    NULL,
       
   690 		    "xmlSecSymbianCryptoAppCertLoadBIO",
       
   691 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   692 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   693 	return(-1);    
       
   694     }    	
       
   695     
       
   696     ret = xmlSecSymbianCryptoX509StoreAdoptCert(x509Store, cert, type);
       
   697     if(ret < 0) {
       
   698 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   699 		    NULL,
       
   700 		    "xmlSecSymbianCryptoX509StoreAdoptCert",		    
       
   701 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   702 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   703 	X509_free(cert);
       
   704 	return(-1);    
       
   705     }
       
   706     
       
   707     return(0);
       
   708 }
       
   709 
       
   710 #endif /* XMLSEC_NO_X509 */
       
   711 
       
   712 /**
       
   713  * xmlSecSymbianCryptoAppDefaultKeysMngrInit:
       
   714  * @mngr: 		the pointer to keys manager.
       
   715  *
       
   716  * Initializes @mngr with simple keys store #xmlSecSimpleKeysStoreId
       
   717  * and a default SymbianCrypto crypto key data stores.
       
   718  *
       
   719  * Returns 0 on success or a negative value otherwise.
       
   720  */
       
   721 EXPORT_C 
       
   722 int
       
   723 xmlSecSymbianCryptoAppDefaultKeysMngrInit(xmlSecKeysMngrPtr mngr) {
       
   724     int ret(-1);
       
   725     
       
   726     xmlSecAssert2(mngr, -1);
       
   727 
       
   728     /* create simple keys store if needed */        
       
   729     if(!xmlSecKeysMngrGetKeysStore(mngr)) {
       
   730 	xmlSecKeyStorePtr keysStore;
       
   731 
       
   732         keysStore = xmlSecKeyStoreCreate(xmlSecSymbianKeysStoreId);
       
   733 	if(!keysStore) {
       
   734 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   735 			NULL,
       
   736 			"xmlSecKeyStoreCreate",
       
   737 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   738 			"xmlSecSimpleKeysStoreId");
       
   739 	    return(-1);
       
   740 	}
       
   741 	
       
   742 	ret = xmlSecKeysMngrAdoptKeysStore(mngr, keysStore);
       
   743 	if(ret < 0) {
       
   744 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   745 			NULL,
       
   746 			"xmlSecKeysMngrAdoptKeysStore",
       
   747 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   748 			XMLSEC_ERRORS_NO_MESSAGE);
       
   749 	    xmlSecKeyStoreDestroy(keysStore);
       
   750 	    return(-1);        
       
   751 	}
       
   752     }
       
   753 
       
   754     ret = xmlSecSymbianCryptoKeysMngrInit(mngr);    
       
   755     if(ret < 0) {
       
   756 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   757 		    NULL,
       
   758 		    "xmlSecSymbianCryptoKeysMngrInit",
       
   759 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   760 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   761 	return(-1); 
       
   762     }
       
   763     
       
   764     mngr->getKey = xmlSecKeysMngrGetKey;
       
   765     return(0);
       
   766 }
       
   767 
       
   768 /**
       
   769  * xmlSecSymbianCryptoAppDefaultKeysMngrAdoptKey:
       
   770  * @mngr: 		the pointer to keys manager.
       
   771  * @key:		the pointer to key.
       
   772  *
       
   773  * Adds @key to the keys manager @mngr created with #xmlSecSymbianCryptoAppDefaultKeysMngrInit
       
   774  * function.
       
   775  *  
       
   776  * Returns 0 on success or a negative value otherwise.
       
   777  */
       
   778 EXPORT_C 
       
   779 int 
       
   780 xmlSecSymbianCryptoAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr mngr, xmlSecKeyPtr key) {
       
   781     xmlSecKeyStorePtr store;
       
   782     int ret(-1);
       
   783     
       
   784     xmlSecAssert2(mngr, -1);
       
   785     xmlSecAssert2(key, -1);
       
   786     
       
   787     store = xmlSecKeysMngrGetKeysStore(mngr);
       
   788     if(!store) {
       
   789 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   790 		    NULL,
       
   791 		    "xmlSecKeysMngrGetKeysStore",
       
   792 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   793 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   794 	return(-1);
       
   795     }
       
   796     
       
   797     ret = xmlSecSimpleKeysStoreAdoptKey(store, key);
       
   798     if(ret < 0) {
       
   799 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   800 		    NULL,
       
   801 		    "xmlSecSimpleKeysStoreAdoptKey",
       
   802 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   803 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   804 	return(-1);
       
   805     }
       
   806     
       
   807     return(0);
       
   808 }
       
   809 
       
   810 /**
       
   811  * xmlSecSymbianCryptoAppDefaultKeysMngrLoad:
       
   812  * @mngr: 		the pointer to keys manager.
       
   813  * @uri:		the uri.
       
   814  *
       
   815  * Loads XML keys file from @uri to the keys manager @mngr created 
       
   816  * with #xmlSecSymbianCryptoAppDefaultKeysMngrInit function.
       
   817  *  
       
   818  * Returns 0 on success or a negative value otherwise.
       
   819  */
       
   820 EXPORT_C 
       
   821 int 
       
   822 xmlSecSymbianCryptoAppDefaultKeysMngrLoad(xmlSecKeysMngrPtr mngr, const char* uri) {
       
   823     xmlSecKeyStorePtr store;
       
   824     int ret(-1);
       
   825     
       
   826     xmlSecAssert2(mngr, -1);
       
   827     xmlSecAssert2(uri, -1);
       
   828     
       
   829     store = xmlSecKeysMngrGetKeysStore(mngr);
       
   830     if(!store) {
       
   831 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   832 		    NULL,
       
   833 		    "xmlSecKeysMngrGetKeysStore",
       
   834 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   835 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   836 	return(-1);
       
   837     }
       
   838     
       
   839     ret = xmlSecSimpleKeysStoreLoad(store, uri, mngr);
       
   840     if(ret < 0) {
       
   841 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   842 		    NULL,
       
   843 		    "xmlSecSimpleKeysStoreLoad",
       
   844 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   845 		    "uri=%s", xmlSecErrorsSafeString(uri));
       
   846 	return(-1);
       
   847     }
       
   848     
       
   849     return(0);
       
   850 }
       
   851 
       
   852 /**
       
   853  * xmlSecSymbianCryptoAppDefaultKeysMngrSave:
       
   854  * @mngr: 		the pointer to keys manager.
       
   855  * @filename:		the destination filename.
       
   856  * @type:		the type of keys to save (public/private/symmetric).
       
   857  *
       
   858  * Saves keys from @mngr to  XML keys file.
       
   859  *  
       
   860  * Returns 0 on success or a negative value otherwise.
       
   861  */
       
   862 EXPORT_C 
       
   863 int 
       
   864 xmlSecSymbianCryptoAppDefaultKeysMngrSave(xmlSecKeysMngrPtr mngr, 
       
   865                                                     const char* filename, 
       
   866                                                     xmlSecKeyDataType type) {
       
   867     xmlSecKeyStorePtr store;
       
   868     int ret(-1);
       
   869     
       
   870     xmlSecAssert2(mngr, -1);
       
   871     xmlSecAssert2(filename, -1);
       
   872     
       
   873     store = xmlSecKeysMngrGetKeysStore(mngr);
       
   874     if(!store) {
       
   875 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   876 		    NULL,
       
   877 		    "xmlSecKeysMngrGetKeysStore",
       
   878 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   879 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   880 	return(-1);
       
   881     }
       
   882     
       
   883     ret = xmlSecSimpleKeysStoreSave(store, filename, type);
       
   884     if(ret < 0) {
       
   885 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   886 		    NULL,
       
   887 		    "xmlSecSimpleKeysStoreSave",
       
   888 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   889 		    "filename=%s", 
       
   890 		    xmlSecErrorsSafeString(filename));
       
   891 	return(-1);
       
   892     }
       
   893     
       
   894     return(0);
       
   895 }
       
   896 
       
   897 /**
       
   898  * xmlSecSymbianCryptoAppGetDefaultPwdCallback:
       
   899  *
       
   900  * Gets default password callback.
       
   901  *
       
   902  * Returns default password callback.
       
   903  */
       
   904 EXPORT_C
       
   905 void*
       
   906 xmlSecSymbianCryptoAppGetDefaultPwdCallback(void) {
       
   907     return(NULL);
       
   908 }
       
   909