xmlsecurityengine/xmlsec/src/xmlsec_keysdata.c
changeset 0 e35f40988205
child 20 889504eac4fb
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /** 
       
     2  * XML Security Library (http://www.aleksey.com/xmlsec).
       
     3  *
       
     4  * Key data.
       
     5  *
       
     6  * This is free software; see Copyright file in the source
       
     7  * distribution for preciese wording.
       
     8  * 
       
     9  * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
       
    10  * Portion Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
       
    11  */
       
    12 
       
    13 #include "xmlsec_globals.h"
       
    14 
       
    15 #include <stdlib.h>
       
    16 #include <string.h>
       
    17  
       
    18 #include <libxml2_tree.h>
       
    19 #include <libxml2_globals.h>
       
    20 
       
    21 #include "xmlsec_xmlsec.h"
       
    22 #include "xmlsec_xmltree.h"
       
    23 #include "xmlsec_keys.h"
       
    24 #include "xmlsec_keyinfo.h"
       
    25 #include "xmlsec_transforms.h"
       
    26 #include "xmlsec_base64.h"
       
    27 #include "xmlsec_keyinfo.h"
       
    28 #include "xmlsec_errors.h"
       
    29 
       
    30 
       
    31 /**************************************************************************
       
    32  *
       
    33  * Global xmlSecKeyDataIds list functions
       
    34  *
       
    35  *************************************************************************/
       
    36 static xmlSecPtrList xmlSecAllKeyDataIds;
       
    37 
       
    38 /** 
       
    39  * xmlSecKeyDataIdsGet:
       
    40  *
       
    41  * Gets global registered key data klasses list.
       
    42  * 
       
    43  * Returns the pointer to list of all registered key data klasses.
       
    44  */
       
    45 EXPORT_C
       
    46 xmlSecPtrListPtr
       
    47 xmlSecKeyDataIdsGet(void) {
       
    48     return(&xmlSecAllKeyDataIds);
       
    49 }
       
    50 
       
    51 /** 
       
    52  * xmlSecKeyDataIdsInit:
       
    53  *
       
    54  * Initializes the key data klasses. This function is called from the 
       
    55  * #xmlSecInit function and the application should not call it directly.
       
    56  *
       
    57  * Returns 0 on success or a negative value if an error occurs.
       
    58  */
       
    59 EXPORT_C
       
    60 int 
       
    61 xmlSecKeyDataIdsInit(void) {
       
    62     int ret;
       
    63     
       
    64     ret = xmlSecPtrListInitialize(xmlSecKeyDataIdsGet(), xmlSecKeyDataIdListId);
       
    65     if(ret < 0) {
       
    66 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    67 		    NULL,
       
    68 		    "xmlSecPtrListPtrInitialize",
       
    69 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    70 		    "xmlSecKeyDataIdListId");
       
    71         return(-1);
       
    72     }
       
    73     
       
    74     ret = xmlSecKeyDataIdsRegisterDefault();
       
    75     if(ret < 0) {
       
    76 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    77 		    NULL,
       
    78 		    "xmlSecKeyDataIdsRegisterDefault",
       
    79 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    80 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    81         return(-1);
       
    82     }
       
    83     
       
    84     return(0);
       
    85 }
       
    86 
       
    87 /**
       
    88  * xmlSecKeyDataIdsShutdown:
       
    89  * 
       
    90  * Shuts down the keys data klasses. This function is called from the 
       
    91  * #xmlSecShutdown function and the application should not call it directly.
       
    92  */
       
    93 EXPORT_C
       
    94 void
       
    95 xmlSecKeyDataIdsShutdown(void) {
       
    96     xmlSecPtrListFinalize(xmlSecKeyDataIdsGet());
       
    97 }
       
    98 
       
    99 /** 
       
   100  * xmlSecKeyDataIdsRegister:
       
   101  * @id:			the key data klass.
       
   102  *
       
   103  * Registers @id in the global list of key data klasses.
       
   104  *
       
   105  * Returns 0 on success or a negative value if an error occurs.
       
   106  */
       
   107 EXPORT_C
       
   108 int 
       
   109 xmlSecKeyDataIdsRegister(xmlSecKeyDataId id) {
       
   110     int ret;
       
   111         
       
   112     xmlSecAssert2(id != xmlSecKeyDataIdUnknown, -1);
       
   113     
       
   114     ret = xmlSecPtrListAdd(xmlSecKeyDataIdsGet(), (xmlSecPtr)id);
       
   115     if(ret < 0) {
       
   116 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   117 		    NULL,
       
   118 		    "xmlSecPtrListAdd",
       
   119 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   120 		    "dataId=%s",
       
   121 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)));
       
   122         return(-1);
       
   123     }
       
   124     
       
   125     return(0);    
       
   126 }
       
   127 
       
   128 /**
       
   129  * xmlSecKeyDataIdsRegisterDefault:
       
   130  *
       
   131  * Registers default (implemented by XML Security Library)
       
   132  * key data klasses: <dsig:KeyName/> element processing klass, 
       
   133  * <dsig:KeyValue/> element processing klass, ...
       
   134  *
       
   135  * Returns 0 on success or a negative value if an error occurs.
       
   136  */
       
   137 EXPORT_C
       
   138 int 
       
   139 xmlSecKeyDataIdsRegisterDefault(void) {
       
   140     if(xmlSecKeyDataIdsRegister(xmlSecKeyDataNameId) < 0) {
       
   141 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   142 		    NULL,
       
   143 		    "xmlSecKeyDataIdsRegister",
       
   144 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   145 		    "xmlSecKeyDataNameId");
       
   146         return(-1);	
       
   147     }
       
   148 
       
   149     if(xmlSecKeyDataIdsRegister(xmlSecKeyDataValueId) < 0) {
       
   150 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   151 		    NULL,
       
   152 		    "xmlSecKeyDataIdsRegister",
       
   153 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   154 		    "xmlSecKeyDataValueId");
       
   155         return(-1);	
       
   156     }
       
   157 
       
   158     if(xmlSecKeyDataIdsRegister(xmlSecKeyDataRetrievalMethodId) < 0) {
       
   159 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   160 		    NULL,
       
   161 		    "xmlSecKeyDataIdsRegister",
       
   162 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   163 		    "xmlSecKeyDataRetrievalMethodId");
       
   164         return(-1);	
       
   165     }
       
   166 
       
   167 #ifndef XMLSEC_NO_XMLENC
       
   168     if(xmlSecKeyDataIdsRegister(xmlSecKeyDataEncryptedKeyId) < 0) {
       
   169 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   170 		    NULL,
       
   171 		    "xmlSecKeyDataIdsRegister",
       
   172 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   173 		    "xmlSecKeyDataEncryptedKeyId");
       
   174         return(-1);	
       
   175     }
       
   176 #endif /* XMLSEC_NO_XMLENC */
       
   177     
       
   178     return(0);
       
   179 }
       
   180 
       
   181 /**************************************************************************
       
   182  *
       
   183  * xmlSecKeyData functions
       
   184  *
       
   185  *************************************************************************/
       
   186 /**
       
   187  * xmlSecKeyDataCreate:
       
   188  * @id: 		the data id.
       
   189  *
       
   190  * Allocates and initializes new key data of the specified type @id.
       
   191  * Caller is responsible for destroing returend object with 
       
   192  * #xmlSecKeyDataDestroy function.
       
   193  *
       
   194  * Returns the pointer to newly allocated key data structure
       
   195  * or NULL if an error occurs.
       
   196  */
       
   197 EXPORT_C
       
   198 xmlSecKeyDataPtr	
       
   199 xmlSecKeyDataCreate(xmlSecKeyDataId id)  {
       
   200     xmlSecKeyDataPtr data;
       
   201     int ret;
       
   202         
       
   203     xmlSecAssert2(id != NULL, NULL);
       
   204     xmlSecAssert2(id->klassSize >= sizeof(xmlSecKeyDataKlass), NULL);
       
   205     xmlSecAssert2(id->objSize >= sizeof(xmlSecKeyData), NULL);
       
   206     xmlSecAssert2(id->name != NULL, NULL);
       
   207         
       
   208     /* Allocate a new xmlSecKeyData and fill the fields. */
       
   209     data = (xmlSecKeyDataPtr)xmlMalloc(id->objSize);
       
   210     if(data == NULL) {
       
   211 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   212 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   213 		    NULL,
       
   214 		    XMLSEC_ERRORS_R_MALLOC_FAILED,
       
   215 		    "size=%d", id->objSize); 
       
   216 	return(NULL);
       
   217     }
       
   218     memset(data, 0, id->objSize);    
       
   219     data->id = id;
       
   220 
       
   221     if(id->initialize != NULL) {
       
   222 	ret = (id->initialize)(data);
       
   223         if(ret < 0) {
       
   224 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   225 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   226 			"id->initialize",
       
   227 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   228 			XMLSEC_ERRORS_NO_MESSAGE);
       
   229 	    xmlSecKeyDataDestroy(data);
       
   230 	    return(NULL);
       
   231 	}
       
   232     }
       
   233     
       
   234     return(data);
       
   235 }
       
   236 
       
   237 /**
       
   238  * xmlSecKeyDataDuplicate:
       
   239  * @data: 		the pointer to the key data.
       
   240  *
       
   241  * Creates a duplicate of the given @data. Caller is responsible for 
       
   242  * destroing returend object with #xmlSecKeyDataDestroy function.
       
   243  *
       
   244  * Returns the pointer to newly allocated key data structure
       
   245  * or NULL if an error occurs.
       
   246  */
       
   247 EXPORT_C
       
   248 xmlSecKeyDataPtr	
       
   249 xmlSecKeyDataDuplicate(xmlSecKeyDataPtr data) {
       
   250     xmlSecKeyDataPtr newData;
       
   251     int ret;
       
   252 
       
   253     xmlSecAssert2(xmlSecKeyDataIsValid(data), NULL);
       
   254     xmlSecAssert2(data->id->duplicate != NULL, NULL);
       
   255 
       
   256     newData = xmlSecKeyDataCreate(data->id);
       
   257     if(newData == NULL) {
       
   258 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   259 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   260 		    "xmlSecKeyDataCreate",
       
   261 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   262 		    XMLSEC_ERRORS_NO_MESSAGE); 
       
   263 	return(NULL);
       
   264     }
       
   265 
       
   266     ret = (data->id->duplicate)(newData, data);
       
   267     
       
   268     if(newData == NULL || ret<0) {
       
   269 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   270 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   271 		    "id->duplicate",
       
   272 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   273 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   274 	xmlSecKeyDataDestroy(newData);
       
   275 	return(NULL);	
       
   276     }
       
   277     xmlSecKeyDataDestroy(data);
       
   278     return(newData);
       
   279 }
       
   280 
       
   281 /**
       
   282  * xmlSecKeyDataDestroy:
       
   283  * @data: 		the pointer to the key data.
       
   284  *
       
   285  * Destroys the data and frees all allocated memory. 
       
   286  */
       
   287 EXPORT_C
       
   288 void
       
   289 xmlSecKeyDataDestroy(xmlSecKeyDataPtr data) {
       
   290     xmlSecAssert(xmlSecKeyDataIsValid(data));    
       
   291     xmlSecAssert(data->id->objSize > 0);
       
   292     
       
   293     if(data->id->finalize != NULL) { 
       
   294 	(data->id->finalize)(data);
       
   295     }
       
   296     memset(data, 0, data->id->objSize);
       
   297     xmlFree(data);
       
   298 }
       
   299 
       
   300 
       
   301 /**
       
   302  * xmlSecKeyDataXmlRead:
       
   303  * @id: 		the data klass.
       
   304  * @key: 		the destination key.
       
   305  * @node: 		the pointer to an XML node.
       
   306  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   307  * 
       
   308  * Reads the key data of klass @id from XML @node and adds them to @key.
       
   309  *
       
   310  * Returns 0 on success or a negative value otherwise.
       
   311  */
       
   312 EXPORT_C
       
   313 int
       
   314 xmlSecKeyDataXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   315     xmlSecAssert2(id != NULL, -1);
       
   316     xmlSecAssert2(id->xmlRead != NULL, -1);
       
   317     xmlSecAssert2(key != NULL, -1);
       
   318     xmlSecAssert2(node != NULL, -1);
       
   319 
       
   320     return((id->xmlRead)(id, key, node, keyInfoCtx));
       
   321 }
       
   322 
       
   323 /**
       
   324  * xmlSecKeyDataXmlWrite:
       
   325  * @id: 		the data klass.
       
   326  * @key: 		the source key.
       
   327  * @node: 		the pointer to an XML node.
       
   328  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   329  * 
       
   330  * Writes the key data of klass @id from @key to an XML @node.
       
   331  *
       
   332  * Returns 0 on success or a negative value otherwise.
       
   333  */
       
   334 EXPORT_C
       
   335 int
       
   336 xmlSecKeyDataXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   337     xmlSecAssert2(id != NULL, -1);
       
   338     xmlSecAssert2(id->xmlWrite != NULL, -1);
       
   339     xmlSecAssert2(key != NULL, -1);
       
   340     xmlSecAssert2(node != NULL, -1);
       
   341 
       
   342     return((id->xmlWrite)(id, key, node, keyInfoCtx));
       
   343 }
       
   344 
       
   345 /** 
       
   346  * xmlSecKeyDataBinRead:
       
   347  * @id: 		the data klass.
       
   348  * @key: 		the destination key.
       
   349  * @buf: 		the input binary buffer.
       
   350  * @bufSize: 		the input buffer size.
       
   351  * @keyInfoCtx: 	the <dsig:KeyInfo/> node processing context.
       
   352  *
       
   353  * Reads the key data of klass @id from binary buffer @buf to @key.
       
   354  * 
       
   355  * Returns 0 on success or a negative value if an error occurs.
       
   356  */
       
   357 EXPORT_C
       
   358 int
       
   359 xmlSecKeyDataBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   360 		    const xmlSecByte* buf, xmlSecSize bufSize, 
       
   361 		    xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   362     xmlSecAssert2(id != NULL, -1);
       
   363     xmlSecAssert2(id->binRead != NULL, -1);
       
   364     xmlSecAssert2(key != NULL, -1);
       
   365     xmlSecAssert2(buf != NULL, -1);
       
   366 
       
   367     return((id->binRead)(id, key, buf, bufSize, keyInfoCtx));
       
   368 }
       
   369 
       
   370 /** 
       
   371  * xmlSecKeyDataBinWrite:
       
   372  * @id: 		the data klass.
       
   373  * @key: 		the source key.
       
   374  * @buf: 		the output binary buffer.
       
   375  * @bufSize: 		the output buffer size.
       
   376  * @keyInfoCtx: 	the <dsig:KeyInfo/> node processing context.
       
   377  *
       
   378  * Writes the key data of klass @id from the @key to a binary buffer @buf. 
       
   379  * 
       
   380  * Returns 0 on success or a negative value if an error occurs.
       
   381  */
       
   382 EXPORT_C
       
   383 int
       
   384 xmlSecKeyDataBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   385 		    xmlSecByte** buf, xmlSecSize* bufSize, 
       
   386 		    xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   387     xmlSecAssert2(id != NULL, -1);
       
   388     xmlSecAssert2(id->binWrite != NULL, -1);
       
   389     xmlSecAssert2(key != NULL, -1);
       
   390     xmlSecAssert2(buf != NULL, -1);
       
   391 
       
   392     return((id->binWrite)(id, key, buf, bufSize, keyInfoCtx));
       
   393 }
       
   394 
       
   395 /** 
       
   396  * xmlSecKeyDataGenerate:
       
   397  * @data: 		the pointer to key data.
       
   398  * @sizeBits: 		the desired key data size (in bits).
       
   399  * @type:		the desired key data type.
       
   400  *
       
   401  * Generates new key data of given size and type.
       
   402  *
       
   403  * Returns 0 on success or a negative value otherwise.
       
   404  */
       
   405 EXPORT_C
       
   406 int
       
   407 xmlSecKeyDataGenerate(xmlSecKeyDataPtr data, xmlSecSize sizeBits, 
       
   408 		      xmlSecKeyDataType type) {
       
   409     int ret;
       
   410 
       
   411     xmlSecAssert2(xmlSecKeyDataIsValid(data), -1);
       
   412     xmlSecAssert2(data->id->generate != NULL, -1);
       
   413     
       
   414     /* write data */
       
   415     ret = data->id->generate(data, sizeBits, type);
       
   416     if(ret < 0) {
       
   417 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   418 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   419 		    "id->generate",
       
   420 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   421 		    "size=%d", sizeBits);
       
   422 	return(-1);	    
       
   423     }
       
   424     return(0);    
       
   425 }
       
   426 
       
   427 /** 
       
   428  * xmlSecKeyDataGetType:
       
   429  * @data: 		the pointer to key data.
       
   430  *
       
   431  * Gets key data type.
       
   432  *
       
   433  * Returns key data type.
       
   434  */
       
   435 EXPORT_C  
       
   436 xmlSecKeyDataType	
       
   437 xmlSecKeyDataGetType(xmlSecKeyDataPtr data) {
       
   438     xmlSecAssert2(xmlSecKeyDataIsValid(data), xmlSecKeyDataTypeUnknown);
       
   439     xmlSecAssert2(data->id->getType != NULL, xmlSecKeyDataTypeUnknown);
       
   440     
       
   441     return(data->id->getType(data));
       
   442 }
       
   443 
       
   444 /** 
       
   445  * xmlSecKeyDataGetSize:
       
   446  * @data: 		the pointer to key data.
       
   447  *
       
   448  * Gets key data size.
       
   449  *
       
   450  * Returns key data size (in bits).
       
   451  */
       
   452 EXPORT_C
       
   453 xmlSecSize
       
   454 xmlSecKeyDataGetSize(xmlSecKeyDataPtr data) {
       
   455     xmlSecAssert2(xmlSecKeyDataIsValid(data), 0);
       
   456     xmlSecAssert2(data->id->getSize != NULL, 0);
       
   457     
       
   458     return(data->id->getSize(data));
       
   459 }
       
   460 
       
   461 /**
       
   462  * xmlSecKeyDataGetIdentifier:
       
   463  * @data: 		the pointer to key data.
       
   464  * 
       
   465  * Gets key data identifier string.
       
   466  *
       
   467  * Returns key data id string.
       
   468  */
       
   469 EXPORT_C
       
   470 const xmlChar*
       
   471 xmlSecKeyDataGetIdentifier(xmlSecKeyDataPtr data) {
       
   472     xmlSecAssert2(xmlSecKeyDataIsValid(data), NULL);
       
   473     xmlSecAssert2(data->id->getIdentifier != NULL, NULL);
       
   474     
       
   475     return(data->id->getIdentifier(data));
       
   476 }
       
   477 
       
   478 /** 
       
   479  * xmlSecKeyDataDebugDump:
       
   480  * @data: 		the pointer to key data.
       
   481  * @output: 		the pointer to output FILE.
       
   482  *
       
   483  * Prints key data debug info.
       
   484  */
       
   485 EXPORT_C
       
   486 void
       
   487 xmlSecKeyDataDebugDump(xmlSecKeyDataPtr data, FILE *output) {
       
   488     xmlSecAssert(xmlSecKeyDataIsValid(data));
       
   489     xmlSecAssert(data->id->debugDump != NULL);
       
   490     xmlSecAssert(output != NULL);
       
   491     
       
   492     data->id->debugDump(data, output);
       
   493 }
       
   494 
       
   495 /** 
       
   496  * xmlSecKeyDataDebugXmlDump:
       
   497  * @data: 		the pointer to key data.
       
   498  * @output: 		the pointer to output FILE.
       
   499  *
       
   500  * Prints key data debug info in XML format.
       
   501  */
       
   502 EXPORT_C 
       
   503 void 
       
   504 xmlSecKeyDataDebugXmlDump(xmlSecKeyDataPtr data, FILE *output) {
       
   505     xmlSecAssert(xmlSecKeyDataIsValid(data));
       
   506     xmlSecAssert(data->id->debugXmlDump != NULL);
       
   507     xmlSecAssert(output != NULL);
       
   508     
       
   509     data->id->debugXmlDump(data, output);
       
   510 }
       
   511 
       
   512 /**************************************************************************
       
   513  *
       
   514  * xmlSecKeyDataBinary methods
       
   515  *
       
   516  * key (xmlSecBuffer) is located after xmlSecKeyData structure
       
   517  *
       
   518  *************************************************************************/
       
   519 /** 
       
   520  * xmlSecKeyDataBinaryValueInitialize:
       
   521  * @data: 		the pointer to binary key data.
       
   522  *
       
   523  * Initializes key data.
       
   524  * 
       
   525  * Returns 0 on success or a negative value otherwise.
       
   526  */
       
   527 EXPORT_C
       
   528 int
       
   529 xmlSecKeyDataBinaryValueInitialize(xmlSecKeyDataPtr data) {
       
   530     xmlSecBufferPtr buffer;
       
   531     int ret;
       
   532     
       
   533     xmlSecAssert2(xmlSecKeyDataIsValid(data), -1);
       
   534     xmlSecAssert2(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize), -1);
       
   535         
       
   536     /* initialize buffer */
       
   537     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
   538     xmlSecAssert2(buffer != NULL, -1);
       
   539     
       
   540     ret = xmlSecBufferInitialize(buffer, 0);
       
   541     if(ret < 0) {
       
   542 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   543 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   544 		    "xmlSecBufferInitialize",
       
   545 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   546 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   547 	return(-1);
       
   548     }
       
   549     
       
   550     return(0);    
       
   551 }
       
   552 
       
   553 /** 
       
   554  * xmlSecKeyDataBinaryValueDuplicate:
       
   555  * @dst: 		the pointer to destination binary key data.
       
   556  * @src: 		the pointer to source binary key data.
       
   557  *
       
   558  * Copies binary key data from @src to @dst.
       
   559  * 
       
   560  * Returns 0 on success or a negative value otherwise.
       
   561  */
       
   562 EXPORT_C
       
   563 int
       
   564 xmlSecKeyDataBinaryValueDuplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) {
       
   565     xmlSecBufferPtr buffer;
       
   566     int ret;
       
   567     
       
   568     xmlSecAssert2(xmlSecKeyDataIsValid(dst), -1);
       
   569     xmlSecAssert2(xmlSecKeyDataCheckSize(dst, xmlSecKeyDataBinarySize), -1);
       
   570     xmlSecAssert2(xmlSecKeyDataIsValid(src), -1);
       
   571     xmlSecAssert2(xmlSecKeyDataCheckSize(src, xmlSecKeyDataBinarySize), -1);
       
   572 
       
   573     buffer = xmlSecKeyDataBinaryValueGetBuffer(src);
       
   574     xmlSecAssert2(buffer != NULL, -1);
       
   575     
       
   576     /* copy data */
       
   577     ret = xmlSecKeyDataBinaryValueSetBuffer(dst,
       
   578 		    xmlSecBufferGetData(buffer),
       
   579 		    xmlSecBufferGetSize(buffer));
       
   580     if(ret < 0) {
       
   581 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   582 		    xmlSecErrorsSafeString(xmlSecKeyDataGetName(dst)),
       
   583 		    "xmlSecKeyDataBinaryValueSetBuffer",
       
   584 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   585 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   586 	return(-1);
       
   587     }
       
   588 
       
   589     return(0);
       
   590 }
       
   591 
       
   592 /** 
       
   593  * xmlSecKeyDataBinaryValueFinalize:
       
   594  * @data: 		the pointer to binary key data.
       
   595  *
       
   596  * Cleans up binary key data.
       
   597  */
       
   598 EXPORT_C
       
   599 void 
       
   600 xmlSecKeyDataBinaryValueFinalize(xmlSecKeyDataPtr data) {
       
   601     xmlSecBufferPtr buffer;
       
   602     
       
   603     xmlSecAssert(xmlSecKeyDataIsValid(data));
       
   604     xmlSecAssert(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize));
       
   605     
       
   606     /* initialize buffer */
       
   607     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
   608     xmlSecAssert(buffer != NULL);
       
   609     
       
   610     xmlSecBufferFinalize(buffer);    
       
   611 }
       
   612 
       
   613 /** 
       
   614  * xmlSecKeyDataBinaryValueXmlRead:
       
   615  * @id: 		the data klass.
       
   616  * @key:		the pointer to destination key.
       
   617  * @node: 		the pointer to an XML node.
       
   618  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   619  *
       
   620  * Reads binary key data from @node to the key by base64 decoding the @node content.
       
   621  * 
       
   622  * Returns 0 on success or a negative value otherwise.
       
   623  */
       
   624 EXPORT_C
       
   625 int 
       
   626 xmlSecKeyDataBinaryValueXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   627 				xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   628     xmlChar* str;
       
   629     xmlSecSize len;
       
   630     xmlSecKeyDataPtr data;
       
   631     int ret;
       
   632     
       
   633     xmlSecAssert2(id != xmlSecKeyDataIdUnknown, -1);
       
   634     xmlSecAssert2(key != NULL, -1);
       
   635     xmlSecAssert2(node != NULL, -1);
       
   636     xmlSecAssert2(keyInfoCtx != NULL, -1);
       
   637 
       
   638     str = xmlNodeGetContent(node);
       
   639     if(str == NULL) {
       
   640 	xmlSecError(XMLSEC_ERRORS_HERE, 
       
   641 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   642 		    xmlSecErrorsSafeString(xmlSecNodeGetName(node)),
       
   643 		    XMLSEC_ERRORS_R_INVALID_NODE_CONTENT,
       
   644 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   645 	return(-1);
       
   646     }
       
   647 
       
   648     /* usual trick: decode into the same buffer */
       
   649     ret = xmlSecBase64Decode(str, (xmlSecByte*)str, xmlStrlen(str));
       
   650     if(ret < 0) {
       
   651 	xmlSecError(XMLSEC_ERRORS_HERE, 
       
   652 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   653 		    "xmlSecBase64Decode",
       
   654 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   655 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   656 	xmlFree(str);
       
   657 	return(-1);
       
   658     }
       
   659     len = ret;
       
   660 
       
   661     /* check do we have a key already */
       
   662     data = xmlSecKeyGetValue(key);
       
   663     if(data != NULL) {
       
   664 	xmlSecBufferPtr buffer;
       
   665 	
       
   666 	if(!xmlSecKeyDataCheckId(data, id)) {
       
   667 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   668 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   669 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   670 			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   671 			XMLSEC_ERRORS_NO_MESSAGE);
       
   672 	    xmlFree(str);
       
   673 	    return(-1);	
       
   674 	}
       
   675 	
       
   676 	buffer = xmlSecKeyDataBinaryValueGetBuffer(data);	
       
   677 	if((buffer != NULL) && ((xmlSecSize)xmlSecBufferGetSize(buffer) != len)) {
       
   678 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   679 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   680 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   681 			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   682 			"cur-data-size=%d;new-data-size=%d",
       
   683 			xmlSecBufferGetSize(buffer), len);
       
   684 	    xmlFree(str);
       
   685 	    return(-1);		
       
   686 	}
       
   687 	if((buffer != NULL) && (len > 0) && (memcmp(xmlSecBufferGetData(buffer), str, len) != 0)) {
       
   688 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   689 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   690 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   691 			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   692 			"key already has a different value");
       
   693 	    xmlFree(str);
       
   694 	    return(-1);		
       
   695 	}
       
   696 	if(buffer != NULL) {
       
   697 	    /* we already have exactly the same key */
       
   698     	    xmlFree(str);
       
   699 	    return(0);
       
   700 	}
       
   701 	
       
   702 	/* we have binary key value with empty buffer */
       
   703     }
       
   704 
       
   705     
       
   706     data = xmlSecKeyDataCreate(id);
       
   707     if(data == NULL ) {
       
   708 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   709 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   710 		    "xmlSecKeyDataCreate",
       
   711 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   712 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   713 	xmlFree(str);
       
   714 	return(-1);
       
   715     }
       
   716         
       
   717     ret = xmlSecKeyDataBinaryValueSetBuffer(data, (xmlSecByte*)str, len);
       
   718     if(ret < 0) {
       
   719 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   720 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   721 		    "xmlSecKeyDataBinaryValueSetBuffer",
       
   722 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   723 		    "size=%d", len);
       
   724 	xmlSecKeyDataDestroy(data);
       
   725 	xmlFree(str);
       
   726 	return(-1);
       
   727     }
       
   728     xmlFree(str);
       
   729 
       
   730     if(xmlSecKeyReqMatchKeyValue(&(keyInfoCtx->keyReq), data) != 1) {
       
   731 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   732 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   733 		    "xmlSecKeyReqMatchKeyValue",
       
   734 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   735 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   736 	xmlSecKeyDataDestroy(data);
       
   737 	return(0);
       
   738     }
       
   739     
       
   740     ret = xmlSecKeySetValue(key, data);
       
   741     if(ret < 0) {
       
   742 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   743 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   744 		    "xmlSecKeySetValue",
       
   745 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   746 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   747 	xmlSecKeyDataDestroy(data);
       
   748 	return(-1);
       
   749     }
       
   750 
       
   751     return(0);
       
   752 }
       
   753 
       
   754 /** 
       
   755  * xmlSecKeyDataBinaryValueXmlWrite:
       
   756  * @id: 		the data klass.
       
   757  * @key:		the pointer to source key.
       
   758  * @node: 		the pointer to an XML node.
       
   759  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   760  *
       
   761  * Base64 encodes binary key data of klass @id from the @key and 
       
   762  * sets to the @node content. 
       
   763  * 
       
   764  * Returns 0 on success or a negative value otherwise.
       
   765  */
       
   766 EXPORT_C
       
   767 int 
       
   768 xmlSecKeyDataBinaryValueXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   769 			    xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   770     xmlSecBufferPtr buffer;
       
   771     xmlSecKeyDataPtr value;
       
   772     xmlChar* str;
       
   773     
       
   774     xmlSecAssert2(id != xmlSecKeyDataIdUnknown, -1);
       
   775     xmlSecAssert2(key != NULL, -1);
       
   776     xmlSecAssert2(node != NULL, -1);
       
   777     xmlSecAssert2(keyInfoCtx != NULL, -1);
       
   778 
       
   779     if((xmlSecKeyDataTypeSymmetric & keyInfoCtx->keyReq.keyType) == 0) {
       
   780 	/* we can have only symmetric key */
       
   781 	return(0);
       
   782     }    
       
   783 
       
   784     value = xmlSecKeyGetValue(key);
       
   785     xmlSecAssert2(xmlSecKeyDataIsValid(value), -1);
       
   786 
       
   787     buffer = xmlSecKeyDataBinaryValueGetBuffer(value);
       
   788     xmlSecAssert2(buffer != NULL, -1);
       
   789 
       
   790     str = xmlSecBase64Encode(xmlSecBufferGetData(buffer),
       
   791 			     xmlSecBufferGetSize(buffer),
       
   792 			     keyInfoCtx->base64LineSize);
       
   793     if(str == NULL) {
       
   794 	xmlSecError(XMLSEC_ERRORS_HERE, 
       
   795 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   796 		    "xmlSecBase64Encode",
       
   797 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   798 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   799 	return(-1);
       
   800     }    
       
   801     xmlNodeSetContent(node, str);
       
   802     xmlFree(str);
       
   803     return(0);
       
   804 }
       
   805 
       
   806 /** 
       
   807  * xmlSecKeyDataBinaryValueBinRead:
       
   808  * @id: 		the data klass.
       
   809  * @key:		the pointer to destination key.
       
   810  * @buf:		the source binary buffer.
       
   811  * @bufSize:		the source binary buffer size.
       
   812  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   813  *
       
   814  * Reads binary key data of the klass @id from @buf to the @key.
       
   815  * 
       
   816  * Returns 0 on success or a negative value otherwise.
       
   817  */
       
   818 EXPORT_C
       
   819 int 
       
   820 xmlSecKeyDataBinaryValueBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   821 				const xmlSecByte* buf, xmlSecSize bufSize, 
       
   822 				xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   823     xmlSecKeyDataPtr data;
       
   824     int ret;
       
   825     
       
   826     xmlSecAssert2(id != xmlSecKeyDataIdUnknown, -1);
       
   827     xmlSecAssert2(key != NULL, -1);
       
   828     xmlSecAssert2(buf != NULL, -1);
       
   829     xmlSecAssert2(bufSize > 0, -1);
       
   830     xmlSecAssert2(keyInfoCtx != NULL, -1);
       
   831 
       
   832     /* check do we have a key already */
       
   833     data = xmlSecKeyGetValue(key);
       
   834     if(data != NULL) {
       
   835 	xmlSecBufferPtr buffer;
       
   836 	
       
   837 	if(!xmlSecKeyDataCheckId(data, id)) {
       
   838 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   839 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   840 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   841     			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   842 			XMLSEC_ERRORS_NO_MESSAGE);
       
   843 	    return(-1);	
       
   844 	}
       
   845 	
       
   846 	buffer = xmlSecKeyDataBinaryValueGetBuffer(data);	
       
   847 	if((buffer != NULL) && ((xmlSecSize)xmlSecBufferGetSize(buffer) != bufSize)) {
       
   848 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   849 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   850 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   851 			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   852 			"cur-data-size=%d;new-data-size=%d",
       
   853 			xmlSecBufferGetSize(buffer), bufSize);
       
   854 	    return(-1);		
       
   855 	}
       
   856 	if((buffer != NULL) && (bufSize > 0) && (memcmp(xmlSecBufferGetData(buffer), buf, bufSize) != 0)) {
       
   857 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
   858 			xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   859 			xmlSecErrorsSafeString(xmlSecKeyDataGetName(data)),
       
   860 			XMLSEC_ERRORS_R_KEY_DATA_ALREADY_EXIST,
       
   861 			"key already has a different value");
       
   862 	    return(-1);		
       
   863 	}
       
   864 	if(buffer != NULL) {
       
   865 	    /* we already have exactly the same key */
       
   866 	    return(0);
       
   867 	}
       
   868 	
       
   869 	/* we have binary key value with empty buffer */
       
   870     }
       
   871     
       
   872     data = xmlSecKeyDataCreate(id);
       
   873     if(data == NULL ) {
       
   874 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   875 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   876 		    "xmlSecKeyDataCreate",
       
   877 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   878 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   879 	return(-1);
       
   880     }
       
   881         
       
   882     ret = xmlSecKeyDataBinaryValueSetBuffer(data, buf, bufSize);
       
   883     if(ret < 0) {
       
   884 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   885 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   886 		    "xmlSecKeyDataBinaryValueSetBuffer",
       
   887 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   888 		    "size=%d", bufSize);
       
   889 	xmlSecKeyDataDestroy(data);
       
   890 	return(-1);
       
   891     }
       
   892 
       
   893     if(xmlSecKeyReqMatchKeyValue(&(keyInfoCtx->keyReq), data) != 1) {
       
   894 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   895 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   896 		    "xmlSecKeyReqMatchKeyValue",
       
   897 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   898 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   899 	xmlSecKeyDataDestroy(data);
       
   900 	return(0);
       
   901     }
       
   902     
       
   903     ret = xmlSecKeySetValue(key, data);
       
   904     if(ret < 0) {
       
   905 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   906 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   907 		    "xmlSecKeySetValue",
       
   908 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   909 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   910 	xmlSecKeyDataDestroy(data);
       
   911 	return(-1);
       
   912     }
       
   913 
       
   914     return(0);
       
   915 }
       
   916 
       
   917 /** 
       
   918  * xmlSecKeyDataBinaryValueBinWrite:
       
   919  * @id: 		the data klass.
       
   920  * @key:		the pointer to source key.
       
   921  * @buf:		the destination binary buffer.
       
   922  * @bufSize:		the destination binary buffer size.
       
   923  * @keyInfoCtx: 	the pointer to <dsig:KeyInfo/> element processing context.
       
   924  *
       
   925  * Writes binary key data of klass @id from the @key to @buf.
       
   926  * 
       
   927  * Returns 0 on success or a negative value otherwise.
       
   928  */
       
   929 EXPORT_C
       
   930 int 
       
   931 xmlSecKeyDataBinaryValueBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key, 
       
   932 				xmlSecByte** buf, xmlSecSize* bufSize, 
       
   933 				xmlSecKeyInfoCtxPtr keyInfoCtx) {
       
   934     xmlSecKeyDataPtr value;
       
   935     xmlSecBufferPtr buffer;
       
   936 
       
   937     xmlSecAssert2(id != xmlSecKeyDataIdUnknown, -1);
       
   938     xmlSecAssert2(key != NULL, -1);
       
   939     xmlSecAssert2(buf != NULL, -1);
       
   940     xmlSecAssert2(bufSize != NULL, -1);
       
   941     xmlSecAssert2(keyInfoCtx != NULL, -1);
       
   942 
       
   943     if((xmlSecKeyDataTypeSymmetric & keyInfoCtx->keyReq.keyType) == 0) {
       
   944 	/* we can have only symmetric key */
       
   945 	return(0);
       
   946     }    
       
   947 
       
   948     value = xmlSecKeyGetValue(key);
       
   949     xmlSecAssert2(xmlSecKeyDataIsValid(value), -1);
       
   950 
       
   951     buffer = xmlSecKeyDataBinaryValueGetBuffer(key->value);
       
   952     xmlSecAssert2(buffer != NULL, -1);
       
   953 
       
   954     (*bufSize) = xmlSecBufferGetSize(buffer);
       
   955     (*buf) = (xmlSecByte*) xmlMalloc((*bufSize));
       
   956     if((*buf) == NULL) {
       
   957 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   958 		    xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(id)),
       
   959 		    NULL,
       
   960 		    XMLSEC_ERRORS_R_MALLOC_FAILED,
       
   961 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   962 	return(-1);
       
   963     }
       
   964     memcpy((*buf), xmlSecBufferGetData(buffer), (*bufSize));    
       
   965     return(0);
       
   966 }
       
   967 
       
   968 /** 
       
   969  * xmlSecKeyDataBinaryValueDebugDump:
       
   970  * @data: 		the pointer to binary key data.
       
   971  * @output:		the pointer to output FILE.
       
   972  * 
       
   973  * Prints binary key data debug information to @output.
       
   974  */
       
   975 EXPORT_C
       
   976 void 
       
   977 xmlSecKeyDataBinaryValueDebugDump(xmlSecKeyDataPtr data, FILE* output) {
       
   978     xmlSecBufferPtr buffer;
       
   979     
       
   980     xmlSecAssert(xmlSecKeyDataIsValid(data));
       
   981     xmlSecAssert(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize));
       
   982     xmlSecAssert(data->id->dataNodeName != NULL);
       
   983     xmlSecAssert(output != NULL);
       
   984 
       
   985     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
   986     xmlSecAssert(buffer != NULL);
       
   987 
       
   988     /* print only size, everything else is sensitive */    
       
   989     fprintf(output, "=== %s: size=%d\n", data->id->dataNodeName, 
       
   990 					 xmlSecKeyDataGetSize(data));
       
   991 }
       
   992 
       
   993 /** 
       
   994  * xmlSecKeyDataBinaryValueDebugXmlDump:
       
   995  * @data: 		the pointer to binary key data.
       
   996  * @output:		the pointer to output FILE.
       
   997  * 
       
   998  * Prints binary key data debug information to @output in XML format.
       
   999  */
       
  1000 EXPORT_C
       
  1001 void 
       
  1002 xmlSecKeyDataBinaryValueDebugXmlDump(xmlSecKeyDataPtr data, FILE* output) {
       
  1003     xmlSecBufferPtr buffer;
       
  1004 
       
  1005     xmlSecAssert(xmlSecKeyDataIsValid(data));
       
  1006     xmlSecAssert(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize));
       
  1007     xmlSecAssert(data->id->dataNodeName != NULL);
       
  1008     xmlSecAssert(output != NULL);
       
  1009 
       
  1010     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
  1011     xmlSecAssert(buffer != NULL);
       
  1012     
       
  1013     /* print only size, everything else is sensitive */    
       
  1014     fprintf(output, "<%s size=\"%d\" />\n", data->id->dataNodeName, 
       
  1015 					    xmlSecKeyDataGetSize(data));
       
  1016 }
       
  1017 
       
  1018 /** 
       
  1019  * xmlSecKeyDataBinaryValueGetSize:
       
  1020  * @data: 		the pointer to binary key data.
       
  1021  *
       
  1022  * Gets the binary key data size.
       
  1023  *
       
  1024  * Returns binary key data size in bits.
       
  1025  */
       
  1026 EXPORT_C
       
  1027 xmlSecSize
       
  1028 xmlSecKeyDataBinaryValueGetSize(xmlSecKeyDataPtr data) {
       
  1029     xmlSecBufferPtr buffer;
       
  1030 
       
  1031     xmlSecAssert2(xmlSecKeyDataIsValid(data), 0);
       
  1032     xmlSecAssert2(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize), 0);
       
  1033 
       
  1034     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
  1035     xmlSecAssert2(buffer != NULL, 0);
       
  1036 
       
  1037     /* return size in bits */    
       
  1038     return(8 * xmlSecBufferGetSize(buffer));    
       
  1039 }
       
  1040 
       
  1041 /** 
       
  1042  * xmlSecKeyDataBinaryValueGetBuffer:
       
  1043  * @data: 		the pointer to binary key data.
       
  1044  *
       
  1045  * Gets the binary key data buffer.
       
  1046  *
       
  1047  * Returns pointer to binary key data buffer.
       
  1048  */
       
  1049 EXPORT_C
       
  1050 xmlSecBufferPtr 
       
  1051 xmlSecKeyDataBinaryValueGetBuffer(xmlSecKeyDataPtr data) {
       
  1052     xmlSecAssert2(xmlSecKeyDataIsValid(data), NULL);
       
  1053     xmlSecAssert2(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize), NULL);
       
  1054 
       
  1055     /* key (xmlSecBuffer) is located after xmlSecKeyData structure */
       
  1056     return((xmlSecBufferPtr)(((xmlSecByte*)data) + sizeof(xmlSecKeyData)));
       
  1057 }
       
  1058 
       
  1059 /** 
       
  1060  * xmlSecKeyDataBinaryValueSetBuffer:
       
  1061  * @data: 		the pointer to binary key data.
       
  1062  * @buf: 		the pointer to binary buffer.
       
  1063  * @bufSize:		the binary buffer size.
       
  1064  *
       
  1065  * Sets the value of @data to @buf.
       
  1066  * 
       
  1067  * Returns 0 on success or a negative value otherwise.
       
  1068  */
       
  1069 EXPORT_C
       
  1070 int
       
  1071 xmlSecKeyDataBinaryValueSetBuffer(xmlSecKeyDataPtr data, 
       
  1072 			const xmlSecByte* buf, xmlSecSize bufSize) {
       
  1073     xmlSecBufferPtr buffer;
       
  1074 
       
  1075     xmlSecAssert2(xmlSecKeyDataIsValid(data), -1);
       
  1076     xmlSecAssert2(xmlSecKeyDataCheckSize(data, xmlSecKeyDataBinarySize), -1);
       
  1077     xmlSecAssert2(buf != NULL, -1);
       
  1078     xmlSecAssert2(bufSize > 0, -1);
       
  1079 
       
  1080     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
       
  1081     xmlSecAssert2(buffer != NULL, -1);
       
  1082 
       
  1083     return(xmlSecBufferSetData(buffer, buf, bufSize));
       
  1084 }
       
  1085 
       
  1086 /***********************************************************************
       
  1087  *
       
  1088  * Keys Data list
       
  1089  *
       
  1090  **********************************************************************/
       
  1091 static xmlSecPtrListKlass xmlSecKeyDataListKlass = {
       
  1092     BAD_CAST "key-data-list",
       
  1093     (xmlSecPtrDuplicateItemMethod)xmlSecKeyDataDuplicate, 	/* xmlSecPtrDuplicateItemMethod duplicateItem; */
       
  1094     (xmlSecPtrDestroyItemMethod)xmlSecKeyDataDestroy,		/* xmlSecPtrDestroyItemMethod destroyItem; */
       
  1095     (xmlSecPtrDebugDumpItemMethod)xmlSecKeyDataDebugDump,	/* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
       
  1096     (xmlSecPtrDebugDumpItemMethod)xmlSecKeyDataDebugXmlDump,	/* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
       
  1097 };
       
  1098 
       
  1099 /**
       
  1100  * xmlSecKeyDataListGetKlass:
       
  1101  * 
       
  1102  * The key data list klass.
       
  1103  *
       
  1104  * Returns pointer to the key data list klass.
       
  1105  */
       
  1106 EXPORT_C
       
  1107 xmlSecPtrListId 
       
  1108 xmlSecKeyDataListGetKlass(void) {
       
  1109     return(&xmlSecKeyDataListKlass);
       
  1110 }
       
  1111 
       
  1112 
       
  1113 /***********************************************************************
       
  1114  *
       
  1115  * Keys Data Ids list
       
  1116  *
       
  1117  **********************************************************************/
       
  1118 static xmlSecPtrListKlass xmlSecKeyDataIdListKlass = {
       
  1119     BAD_CAST "key-data-ids-list",
       
  1120     NULL, 							/* xmlSecPtrDuplicateItemMethod duplicateItem; */
       
  1121     NULL,							/* xmlSecPtrDestroyItemMethod destroyItem; */
       
  1122     NULL,							/* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
       
  1123     NULL,							/* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
       
  1124 };
       
  1125 
       
  1126 /**
       
  1127  * xmlSecKeyDataIdListGetKlass:
       
  1128  * 
       
  1129  * The key data id list klass.
       
  1130  *
       
  1131  * Returns pointer to the key data id list klass.
       
  1132  */
       
  1133 EXPORT_C
       
  1134 xmlSecPtrListId 
       
  1135 xmlSecKeyDataIdListGetKlass(void) {
       
  1136     return(&xmlSecKeyDataIdListKlass);
       
  1137 }
       
  1138 
       
  1139 /**
       
  1140  * xmlSecKeyDataIdListFind:
       
  1141  * @list:		the pointer to key data ids list.
       
  1142  * @dataId:		the key data klass.
       
  1143  *
       
  1144  * Lookups @dataId in @list.
       
  1145  *
       
  1146  * Returns 1 if @dataId is found in the @list, 0 if not and a negative
       
  1147  * value if an error occurs.
       
  1148  */
       
  1149 EXPORT_C
       
  1150 int 
       
  1151 xmlSecKeyDataIdListFind(xmlSecPtrListPtr list, xmlSecKeyDataId dataId) {
       
  1152     xmlSecSize i, size;
       
  1153     
       
  1154     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId), 0);
       
  1155     xmlSecAssert2(dataId != NULL, 0);
       
  1156     
       
  1157     size = xmlSecPtrListGetSize(list);
       
  1158     for(i = 0; i < size; ++i) {
       
  1159 	if((xmlSecKeyDataId)xmlSecPtrListGetItem(list, i) == dataId) {
       
  1160 	    return(1);
       
  1161 	}
       
  1162     }
       
  1163     return(0);
       
  1164 }
       
  1165 
       
  1166 /** 
       
  1167  * xmlSecKeyDataIdListFindByNode:
       
  1168  * @list:		the pointer to key data ids list.
       
  1169  * @nodeName:		the desired key data klass XML node name.
       
  1170  * @nodeNs:		the desired key data klass XML node namespace.
       
  1171  * @usage:		the desired key data usage.
       
  1172  *
       
  1173  * Lookups data klass in the list with given @nodeName, @nodeNs and 
       
  1174  * @usage in the @list.
       
  1175  *
       
  1176  * Returns key data klass is found and NULL otherwise.
       
  1177  */
       
  1178 EXPORT_C 
       
  1179 xmlSecKeyDataId	
       
  1180 xmlSecKeyDataIdListFindByNode(xmlSecPtrListPtr list, const xmlChar* nodeName,
       
  1181 			    const xmlChar* nodeNs, xmlSecKeyDataUsage usage) {
       
  1182     xmlSecKeyDataId dataId;
       
  1183     xmlSecSize i, size;
       
  1184     
       
  1185     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId), xmlSecKeyDataIdUnknown);
       
  1186     xmlSecAssert2(nodeName != NULL, xmlSecKeyDataIdUnknown);
       
  1187     
       
  1188     size = xmlSecPtrListGetSize(list);
       
  1189     for(i = 0; i < size; ++i) {
       
  1190 	dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i);
       
  1191 	xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, xmlSecKeyDataIdUnknown);
       
  1192 
       
  1193 	if(((usage & dataId->usage) != 0) &&
       
  1194 	   xmlStrEqual(nodeName, dataId->dataNodeName) &&
       
  1195 	   xmlStrEqual(nodeNs, dataId->dataNodeNs)) {
       
  1196 	    
       
  1197 	   return(dataId);	   
       
  1198 	}
       
  1199     }
       
  1200     return(xmlSecKeyDataIdUnknown);
       
  1201 }
       
  1202 
       
  1203 /** 
       
  1204  * xmlSecKeyDataIdListFindByHref:
       
  1205  * @list:		the pointer to key data ids list.
       
  1206  * @href:		the desired key data klass href.
       
  1207  * @usage:		the desired key data usage.
       
  1208  *
       
  1209  * Lookups data klass in the list with given @href and @usage in @list.
       
  1210  *
       
  1211  * Returns key data klass is found and NULL otherwise.
       
  1212  */
       
  1213 EXPORT_C 
       
  1214 xmlSecKeyDataId	
       
  1215 xmlSecKeyDataIdListFindByHref(xmlSecPtrListPtr list, const xmlChar* href,
       
  1216 			    xmlSecKeyDataUsage usage) {
       
  1217     xmlSecKeyDataId dataId;
       
  1218     xmlSecSize i, size;
       
  1219     
       
  1220     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId), xmlSecKeyDataIdUnknown);
       
  1221     xmlSecAssert2(href != NULL, xmlSecKeyDataIdUnknown);
       
  1222     
       
  1223     size = xmlSecPtrListGetSize(list);
       
  1224     for(i = 0; i < size; ++i) {
       
  1225 	dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i);
       
  1226 	xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, xmlSecKeyDataIdUnknown);
       
  1227 
       
  1228 	if(((usage & dataId->usage) != 0) && (dataId->href != NULL) &&
       
  1229 	   xmlStrEqual(href, dataId->href)) {
       
  1230 	   
       
  1231 	   return(dataId);	   
       
  1232 	}
       
  1233     }
       
  1234     return(xmlSecKeyDataIdUnknown);
       
  1235 }
       
  1236 
       
  1237 /** 
       
  1238  * xmlSecKeyDataIdListFindByName:
       
  1239  * @list:		the pointer to key data ids list.
       
  1240  * @name:		the desired key data klass name.
       
  1241  * @usage:		the desired key data usage.
       
  1242  *
       
  1243  * Lookups data klass in the list with given @name and @usage in @list.
       
  1244  *
       
  1245  * Returns key data klass is found and NULL otherwise.
       
  1246  */
       
  1247 EXPORT_C 
       
  1248 xmlSecKeyDataId	
       
  1249 xmlSecKeyDataIdListFindByName(xmlSecPtrListPtr list, const xmlChar* name, 
       
  1250 			    xmlSecKeyDataUsage usage) {
       
  1251     xmlSecKeyDataId dataId;
       
  1252     xmlSecSize i, size;
       
  1253     
       
  1254     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId), xmlSecKeyDataIdUnknown);
       
  1255     xmlSecAssert2(name != NULL, xmlSecKeyDataIdUnknown);
       
  1256     
       
  1257     size = xmlSecPtrListGetSize(list);
       
  1258     for(i = 0; i < size; ++i) {
       
  1259 	dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i);
       
  1260 	xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, xmlSecKeyDataIdUnknown);
       
  1261 
       
  1262 	if(((usage & dataId->usage) != 0) && (dataId->name != NULL) && 
       
  1263 	   xmlStrEqual(name, BAD_CAST dataId->name)) {
       
  1264 	   
       
  1265 	   return(dataId);	   
       
  1266 	}
       
  1267     }
       
  1268     return(xmlSecKeyDataIdUnknown);
       
  1269 }
       
  1270 
       
  1271 /** 
       
  1272  * xmlSecKeyDataIdListDebugDump:
       
  1273  * @list:		the pointer to key data ids list.
       
  1274  * @output:		the pointer to output FILE.
       
  1275  * 
       
  1276  * Prints binary key data debug information to @output.
       
  1277  */
       
  1278 EXPORT_C
       
  1279 void 
       
  1280 xmlSecKeyDataIdListDebugDump(xmlSecPtrListPtr list, FILE* output) {
       
  1281     xmlSecKeyDataId dataId;
       
  1282     xmlSecSize i, size;
       
  1283     
       
  1284     xmlSecAssert(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId));
       
  1285     xmlSecAssert(output != NULL);
       
  1286 
       
  1287     size = xmlSecPtrListGetSize(list);
       
  1288     for(i = 0; i < size; ++i) {
       
  1289 	dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i);
       
  1290 	xmlSecAssert(dataId != NULL);
       
  1291 	xmlSecAssert(dataId->name != NULL);
       
  1292 	    
       
  1293 	if(i > 0) {
       
  1294 	    fprintf(output, ",\"%s\"", dataId->name);
       
  1295 	} else {
       
  1296 	    fprintf(output, "\"%s\"", dataId->name);
       
  1297 	}	    
       
  1298     }
       
  1299     fprintf(output, "\n");
       
  1300 }
       
  1301 
       
  1302 /** 
       
  1303  * xmlSecKeyDataIdListDebugXmlDump:
       
  1304  * @list:		the pointer to key data ids list.
       
  1305  * @output:		the pointer to output FILE.
       
  1306  * 
       
  1307  * Prints binary key data debug information to @output in XML format.
       
  1308  */
       
  1309 EXPORT_C
       
  1310 void 
       
  1311 xmlSecKeyDataIdListDebugXmlDump(xmlSecPtrListPtr list, FILE* output) {
       
  1312     xmlSecKeyDataId dataId;
       
  1313     xmlSecSize i, size;
       
  1314 
       
  1315     xmlSecAssert(xmlSecPtrListCheckId(list, xmlSecKeyDataIdListId));
       
  1316     xmlSecAssert(output != NULL);
       
  1317 
       
  1318     fprintf(output, "<KeyDataIdsList>\n");
       
  1319     size = xmlSecPtrListGetSize(list);
       
  1320     for(i = 0; i < size; ++i) {
       
  1321 	dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(list, i);
       
  1322 	xmlSecAssert(dataId != NULL);
       
  1323 	xmlSecAssert(dataId->name != NULL);
       
  1324 	    
       
  1325 	fprintf(output, "<DataId name=\"%s\" />", dataId->name);
       
  1326     }
       
  1327     fprintf(output, "</KeyDataIdsList>\n");
       
  1328 }
       
  1329 
       
  1330 /**************************************************************************
       
  1331  *
       
  1332  * xmlSecKeyDataStore functions
       
  1333  *
       
  1334  *************************************************************************/
       
  1335 /**
       
  1336  * xmlSecKeyDataStoreCreate:
       
  1337  * @id: 		the store id.
       
  1338  *
       
  1339  * Creates new key data store of the specified klass @id. Caller is responsible
       
  1340  * for freeng returned object with #xmlSecKeyDataStoreDestroy function.
       
  1341  *
       
  1342  * Returns the pointer to newly allocated key data store structure
       
  1343  * or NULL if an error occurs.
       
  1344  */
       
  1345 EXPORT_C
       
  1346 xmlSecKeyDataStorePtr	
       
  1347 xmlSecKeyDataStoreCreate(xmlSecKeyDataStoreId id)  {
       
  1348     xmlSecKeyDataStorePtr store;
       
  1349     int ret;
       
  1350         
       
  1351     xmlSecAssert2(id != NULL, NULL);
       
  1352     xmlSecAssert2(id->objSize > 0, NULL);
       
  1353         
       
  1354     /* Allocate a new xmlSecKeyDataStore and fill the fields. */
       
  1355     store = (xmlSecKeyDataStorePtr)xmlMalloc(id->objSize);
       
  1356     if(store == NULL) {
       
  1357 	xmlSecError(XMLSEC_ERRORS_HERE,
       
  1358 		    xmlSecErrorsSafeString(xmlSecKeyDataStoreKlassGetName(id)),
       
  1359 		    NULL,
       
  1360 		    XMLSEC_ERRORS_R_MALLOC_FAILED,
       
  1361 		    "size=%d", id->objSize); 
       
  1362 	return(NULL);
       
  1363     }
       
  1364     memset(store, 0, id->objSize);    
       
  1365     store->id = id;
       
  1366 
       
  1367     if(id->initialize != NULL) {
       
  1368 	ret = (id->initialize)(store);
       
  1369         if(ret < 0) {
       
  1370 	    xmlSecError(XMLSEC_ERRORS_HERE,
       
  1371 			xmlSecErrorsSafeString(xmlSecKeyDataStoreKlassGetName(id)),
       
  1372 			"id->initialize",
       
  1373 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
  1374 			XMLSEC_ERRORS_NO_MESSAGE);
       
  1375 	    xmlSecKeyDataStoreDestroy(store);
       
  1376 	    return(NULL);
       
  1377 	}
       
  1378     }
       
  1379     
       
  1380     return(store);
       
  1381 }
       
  1382 
       
  1383 /**
       
  1384  * xmlSecKeyDataStoreDestroy:
       
  1385  * @store: 		the pointer to the key data store..
       
  1386  *
       
  1387  * Destroys the key data store created with #xmlSecKeyDataStoreCreate
       
  1388  * function.
       
  1389  */
       
  1390 EXPORT_C
       
  1391 void
       
  1392 xmlSecKeyDataStoreDestroy(xmlSecKeyDataStorePtr store) {
       
  1393     xmlSecAssert(xmlSecKeyDataStoreIsValid(store));    
       
  1394     xmlSecAssert(store->id->objSize > 0);
       
  1395     
       
  1396     if(store->id->finalize != NULL) {  
       
  1397         (store->id->finalize)(store);
       
  1398     }
       
  1399     memset(store, 0, store->id->objSize);
       
  1400     xmlFree(store);
       
  1401 }
       
  1402 
       
  1403 /***********************************************************************
       
  1404  *
       
  1405  * Keys Data Store list
       
  1406  *
       
  1407  **********************************************************************/
       
  1408 static xmlSecPtrListKlass xmlSecKeyDataStorePtrListKlass = {
       
  1409     BAD_CAST "keys-data-store-list",
       
  1410     NULL, 							/* xmlSecPtrDuplicateItemMethod duplicateItem; */
       
  1411     (xmlSecPtrDestroyItemMethod)xmlSecKeyDataStoreDestroy,	/* xmlSecPtrDestroyItemMethod destroyItem; */
       
  1412     NULL,							/* xmlSecPtrDebugDumpItemMethod debugDumpItem; */
       
  1413     NULL,							/* xmlSecPtrDebugDumpItemMethod debugXmlDumpItem; */
       
  1414 };
       
  1415 
       
  1416 /**
       
  1417  * xmlSecKeyDataStorePtrListGetKlass:
       
  1418  * 
       
  1419  * Key data stores list.
       
  1420  *
       
  1421  * Returns key data stores list klass.
       
  1422  */
       
  1423 EXPORT_C
       
  1424 xmlSecPtrListId 
       
  1425 xmlSecKeyDataStorePtrListGetKlass(void) {
       
  1426     return(&xmlSecKeyDataStorePtrListKlass);
       
  1427 }
       
  1428 
       
  1429