LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/xmlsec/src - keysmngr.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 260 0.0 %
Date: 2012-12-17 Functions: 0 18 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /** 
       2             :  * XML Security Library (http://www.aleksey.com/xmlsec).
       3             :  *
       4             :  * Keys Manager
       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             :  */
      11             : #include "globals.h"
      12             : 
      13             : #include <stdlib.h>
      14             : #include <stdio.h>
      15             : #include <string.h>
      16             : #include <errno.h>
      17             : 
      18             : #include <libxml/tree.h>
      19             : #include <libxml/parser.h>
      20             : 
      21             : #include <xmlsec/xmlsec.h>
      22             : #include <xmlsec/xmltree.h>
      23             : #include <xmlsec/list.h>
      24             : #include <xmlsec/keys.h>
      25             : #include <xmlsec/transforms.h>
      26             : #include <xmlsec/keysmngr.h>
      27             : #include <xmlsec/errors.h>
      28             : 
      29             : /****************************************************************************
      30             :  *
      31             :  * Keys Manager
      32             :  *
      33             :  ***************************************************************************/
      34             : /** 
      35             :  * xmlSecKeysMngrCreate:
      36             :  * 
      37             :  * Creates new keys manager. Caller is responsible for freeing it with 
      38             :  * #xmlSecKeysMngrDestroy function.
      39             :  * 
      40             :  * Returns: the pointer to newly allocated keys manager or NULL if 
      41             :  * an error occurs.
      42             :  */
      43             : xmlSecKeysMngrPtr 
      44           0 : xmlSecKeysMngrCreate(void) {
      45             :     xmlSecKeysMngrPtr mngr;
      46             :     int ret;
      47             :     
      48             :     /* Allocate a new xmlSecKeysMngr and fill the fields. */
      49           0 :     mngr = (xmlSecKeysMngrPtr)xmlMalloc(sizeof(xmlSecKeysMngr));
      50           0 :     if(mngr == NULL) {
      51           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
      52             :                     NULL,
      53             :                     NULL,
      54             :                     XMLSEC_ERRORS_R_MALLOC_FAILED,
      55             :                     "sizeof(xmlSecKeysMngr)=%d", 
      56             :                     sizeof(xmlSecKeysMngr));
      57           0 :         return(NULL);
      58             :     }
      59           0 :     memset(mngr, 0, sizeof(xmlSecKeysMngr));    
      60             : 
      61           0 :     ret = xmlSecPtrListInitialize(&(mngr->storesList), xmlSecKeyDataStorePtrListId);
      62           0 :     if(ret < 0) {
      63           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
      64             :                     NULL,
      65             :                     "xmlSecPtrListInitialize",
      66             :                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
      67             :                     "xmlSecKeyDataStorePtrListId");
      68           0 :         return(NULL);
      69             :     }
      70             : 
      71           0 :     return(mngr);    
      72             : }
      73             : 
      74             : /** 
      75             :  * xmlSecKeysMngrDestroy:
      76             :  * @mngr:               the pointer to keys manager.
      77             :  *
      78             :  * Destroys keys manager created with #xmlSecKeysMngrCreate function.
      79             :  */
      80             : void
      81           0 : xmlSecKeysMngrDestroy(xmlSecKeysMngrPtr mngr) {
      82           0 :     xmlSecAssert(mngr != NULL);
      83             : 
      84             :     /* destroy keys store */
      85           0 :     if(mngr->keysStore != NULL) {
      86           0 :         xmlSecKeyStoreDestroy(mngr->keysStore);
      87             :     }
      88             :     
      89             :     /* destroy other data stores */
      90           0 :     xmlSecPtrListFinalize(&(mngr->storesList));
      91             : 
      92           0 :     memset(mngr, 0, sizeof(xmlSecKeysMngr));    
      93           0 :     xmlFree(mngr);    
      94             : }
      95             : 
      96             : /**
      97             :  * xmlSecKeysMngrFindKey:
      98             :  * @mngr:               the pointer to keys manager.
      99             :  * @name:               the desired key name.
     100             :  * @keyInfoCtx:         the pointer to <dsig:KeyInfo/> node processing context.
     101             :  *
     102             :  * Lookups key in the keys manager keys store. The caller is responsible 
     103             :  * for destroying the returned key using #xmlSecKeyDestroy method.
     104             :  *
     105             :  * Returns: the pointer to a key or NULL if key is not found or an error occurs.
     106             :  */
     107             : xmlSecKeyPtr
     108           0 : xmlSecKeysMngrFindKey(xmlSecKeysMngrPtr mngr, const xmlChar* name, xmlSecKeyInfoCtxPtr keyInfoCtx) {
     109             :     xmlSecKeyStorePtr store;
     110             :     
     111           0 :     xmlSecAssert2(mngr != NULL, NULL);
     112           0 :     xmlSecAssert2(keyInfoCtx != NULL, NULL);
     113             :     
     114           0 :     store = xmlSecKeysMngrGetKeysStore(mngr);
     115           0 :     if(store == NULL) {
     116             :         /* no store. is it an error? */
     117           0 :         return(NULL);
     118             :     }
     119             :     
     120           0 :     return(xmlSecKeyStoreFindKey(store, name, keyInfoCtx));
     121             : }
     122             : 
     123             : /**
     124             :  * xmlSecKeysMngrAdoptKeysStore:
     125             :  * @mngr:               the pointer to keys manager.
     126             :  * @store:              the pointer to keys store.
     127             :  *
     128             :  * Adopts keys store in the keys manager @mngr.
     129             :  *
     130             :  * Returns: 0 on success or a negative value if an error occurs.
     131             :  */
     132             : int
     133           0 : xmlSecKeysMngrAdoptKeysStore(xmlSecKeysMngrPtr mngr, xmlSecKeyStorePtr store) {
     134           0 :     xmlSecAssert2(mngr != NULL, -1);
     135           0 :     xmlSecAssert2(xmlSecKeyStoreIsValid(store), -1);
     136             :     
     137           0 :     if(mngr->keysStore != NULL) {
     138           0 :         xmlSecKeyStoreDestroy(mngr->keysStore);
     139             :     }
     140           0 :     mngr->keysStore = store;
     141             :     
     142           0 :     return(0);
     143             : }
     144             : 
     145             : /**
     146             :  * xmlSecKeysMngrGetKeysStore:
     147             :  * @mngr:               the pointer to keys manager.
     148             :  *
     149             :  * Gets the keys store.
     150             :  *
     151             :  * Returns: the keys store in the keys manager @mngr or NULL if 
     152             :  * there is no store or an error occurs.
     153             :  */
     154             : xmlSecKeyStorePtr
     155           0 : xmlSecKeysMngrGetKeysStore(xmlSecKeysMngrPtr mngr) {
     156           0 :     xmlSecAssert2(mngr != NULL, NULL);
     157             :     
     158           0 :     return(mngr->keysStore);
     159             : }
     160             : 
     161             : /**
     162             :  * xmlSecKeysMngrAdoptDataStore:
     163             :  * @mngr:               the pointer to keys manager.
     164             :  * @store:              the pointer to data store.
     165             :  *
     166             :  * Adopts data store in the keys manager.
     167             :  *
     168             :  * Returns: 0 on success or a negative value if an error occurs.
     169             :  */
     170             : int
     171           0 : xmlSecKeysMngrAdoptDataStore(xmlSecKeysMngrPtr mngr, xmlSecKeyDataStorePtr store) {
     172             :     xmlSecKeyDataStorePtr tmp;
     173             :     xmlSecSize pos, size;
     174             :     
     175           0 :     xmlSecAssert2(mngr != NULL, -1);
     176           0 :     xmlSecAssert2(xmlSecKeyDataStoreIsValid(store), -1);
     177             : 
     178           0 :     size = xmlSecPtrListGetSize(&(mngr->storesList));
     179           0 :     for(pos = 0; pos < size; ++pos) {
     180           0 :         tmp = (xmlSecKeyDataStorePtr)xmlSecPtrListGetItem(&(mngr->storesList), pos);
     181           0 :         if((tmp != NULL) && (tmp->id == store->id)) {     
     182           0 :             return(xmlSecPtrListSet(&(mngr->storesList), store, pos));
     183             :         }
     184             :     }
     185             :     
     186           0 :     return(xmlSecPtrListAdd(&(mngr->storesList), store));
     187             : }
     188             : 
     189             : 
     190             : /**
     191             :  * xmlSecKeysMngrGetDataStore:
     192             :  * @mngr:               the pointer to keys manager.
     193             :  * @id:                 the desired data store klass.
     194             :  *
     195             :  * Lookups the data store of given klass @id in the keys manager.
     196             :  *
     197             :  * Returns: pointer to data store or NULL if it is not found or an error
     198             :  * occurs.
     199             :  */
     200             : xmlSecKeyDataStorePtr 
     201           0 : xmlSecKeysMngrGetDataStore(xmlSecKeysMngrPtr mngr, xmlSecKeyDataStoreId id) {
     202             :     xmlSecKeyDataStorePtr tmp;
     203             :     xmlSecSize pos, size;
     204             :     
     205           0 :     xmlSecAssert2(mngr != NULL, NULL);
     206           0 :     xmlSecAssert2(id != xmlSecKeyDataStoreIdUnknown, NULL);
     207             : 
     208           0 :     size = xmlSecPtrListGetSize(&(mngr->storesList));
     209           0 :     for(pos = 0; pos < size; ++pos) {
     210           0 :         tmp = (xmlSecKeyDataStorePtr)xmlSecPtrListGetItem(&(mngr->storesList), pos);
     211           0 :         if((tmp != NULL) && (tmp->id == id)) {       
     212           0 :             return(tmp);
     213             :         }
     214             :     }
     215             :     
     216           0 :     return(NULL);
     217             : }
     218             : 
     219             : /**************************************************************************
     220             :  *
     221             :  * xmlSecKeyStore functions
     222             :  *
     223             :  *************************************************************************/
     224             : /**
     225             :  * xmlSecKeyStoreCreate:
     226             :  * @id:                 the key store klass.
     227             :  *
     228             :  * Creates new store of the specified klass @klass. Caller is responsible
     229             :  * for freeing the returned store by calling #xmlSecKeyStoreDestroy function.
     230             :  *
     231             :  * Returns: the pointer to newly allocated keys store or NULL if an error occurs.
     232             :  */
     233             : xmlSecKeyStorePtr       
     234           0 : xmlSecKeyStoreCreate(xmlSecKeyStoreId id)  {
     235             :     xmlSecKeyStorePtr store;
     236             :     int ret;
     237             :         
     238           0 :     xmlSecAssert2(id != NULL, NULL);
     239           0 :     xmlSecAssert2(id->objSize > 0, NULL);
     240             :         
     241             :     /* Allocate a new xmlSecKeyStore and fill the fields. */
     242           0 :     store = (xmlSecKeyStorePtr)xmlMalloc(id->objSize);
     243           0 :     if(store == NULL) {
     244           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     245           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreKlassGetName(id)),
     246             :                     NULL,
     247             :                     XMLSEC_ERRORS_R_MALLOC_FAILED,
     248             :                     "size=%d", id->objSize); 
     249           0 :         return(NULL);
     250             :     }
     251           0 :     memset(store, 0, id->objSize);    
     252           0 :     store->id = id;
     253             : 
     254           0 :     if(id->initialize != NULL) {
     255           0 :         ret = (id->initialize)(store);
     256           0 :         if(ret < 0) {
     257           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     258           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreKlassGetName(id)),
     259             :                         "id->initialize",
     260             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     261             :                         XMLSEC_ERRORS_NO_MESSAGE);
     262           0 :             xmlSecKeyStoreDestroy(store);
     263           0 :             return(NULL);
     264             :         }
     265             :     }
     266             :     
     267           0 :     return(store);
     268             : }
     269             : 
     270             : /**
     271             :  * xmlSecKeyStoreDestroy:
     272             :  * @store:              the pointer to keys store. 
     273             :  *
     274             :  * Destroys the store created with #xmlSecKeyStoreCreate function.
     275             :  */
     276             : void
     277           0 : xmlSecKeyStoreDestroy(xmlSecKeyStorePtr store) {
     278           0 :     xmlSecAssert(xmlSecKeyStoreIsValid(store));    
     279           0 :     xmlSecAssert(store->id->objSize > 0);
     280             :     
     281           0 :     if(store->id->finalize != NULL) {  
     282           0 :         (store->id->finalize)(store);
     283             :     }
     284           0 :     memset(store, 0, store->id->objSize);
     285           0 :     xmlFree(store);
     286             : }
     287             : 
     288             : /**
     289             :  * xmlSecKeyStoreFindKey:
     290             :  * @store:              the pointer to keys store.
     291             :  * @name:               the desired key name.
     292             :  * @keyInfoCtx:         the pointer to <dsig:KeyInfo/> node processing context.
     293             :  *
     294             :  * Lookups key in the store. The caller is responsible for destroying 
     295             :  * the returned key using #xmlSecKeyDestroy method.
     296             :  *
     297             :  * Returns: the pointer to a key or NULL if key is not found or an error occurs.
     298             :  */
     299             : xmlSecKeyPtr
     300           0 : xmlSecKeyStoreFindKey(xmlSecKeyStorePtr store, const xmlChar* name, xmlSecKeyInfoCtxPtr keyInfoCtx) {
     301           0 :     xmlSecAssert2(xmlSecKeyStoreIsValid(store), NULL);    
     302           0 :     xmlSecAssert2(store->id->findKey != NULL, NULL);
     303           0 :     xmlSecAssert2(keyInfoCtx != NULL, NULL);
     304             : 
     305           0 :     return(store->id->findKey(store, name, keyInfoCtx));
     306             : }
     307             : 
     308             : /****************************************************************************
     309             :  *
     310             :  * Simple Keys Store
     311             :  * 
     312             :  * keys list (xmlSecPtrList) is located after xmlSecKeyStore
     313             :  *
     314             :  ***************************************************************************/
     315             : #define xmlSecSimpleKeysStoreSize \
     316             :         (sizeof(xmlSecKeyStore) + sizeof(xmlSecPtrList))
     317             : #define xmlSecSimpleKeysStoreGetList(store) \
     318             :     ((xmlSecKeyStoreCheckSize((store), xmlSecSimpleKeysStoreSize)) ? \
     319             :         (xmlSecPtrListPtr)(((xmlSecByte*)(store)) + sizeof(xmlSecKeyStore)) : \
     320             :         (xmlSecPtrListPtr)NULL)
     321             : 
     322             : static int                      xmlSecSimpleKeysStoreInitialize (xmlSecKeyStorePtr store);
     323             : static void                     xmlSecSimpleKeysStoreFinalize   (xmlSecKeyStorePtr store);
     324             : static xmlSecKeyPtr             xmlSecSimpleKeysStoreFindKey    (xmlSecKeyStorePtr store, 
     325             :                                                                  const xmlChar* name, 
     326             :                                                                  xmlSecKeyInfoCtxPtr keyInfoCtx);
     327             : 
     328             : static xmlSecKeyStoreKlass xmlSecSimpleKeysStoreKlass = {
     329             :     sizeof(xmlSecKeyStoreKlass),
     330             :     xmlSecSimpleKeysStoreSize,
     331             : 
     332             :     /* data */
     333             :     BAD_CAST "simple-keys-store",             /* const xmlChar* name; */ 
     334             :         
     335             :     /* constructors/destructor */
     336             :     xmlSecSimpleKeysStoreInitialize,            /* xmlSecKeyStoreInitializeMethod initialize; */
     337             :     xmlSecSimpleKeysStoreFinalize,              /* xmlSecKeyStoreFinalizeMethod finalize; */
     338             :     xmlSecSimpleKeysStoreFindKey,               /* xmlSecKeyStoreFindKeyMethod findKey; */
     339             : 
     340             :     /* reserved for the future */
     341             :     NULL,                                       /* void* reserved0; */
     342             :     NULL,                                       /* void* reserved1; */
     343             : };
     344             : 
     345             : /**
     346             :  * xmlSecSimpleKeysStoreGetKlass:
     347             :  * 
     348             :  * The simple list based keys store klass.
     349             :  *
     350             :  * Returns: simple list based keys store klass.
     351             :  */
     352             : xmlSecKeyStoreId 
     353           0 : xmlSecSimpleKeysStoreGetKlass(void) {
     354           0 :     return(&xmlSecSimpleKeysStoreKlass);
     355             : }
     356             : 
     357             : /**
     358             :  * xmlSecSimpleKeysStoreAdoptKey:
     359             :  * @store:              the pointer to simple keys store.
     360             :  * @key:                the pointer to key.
     361             :  * 
     362             :  * Adds @key to the @store. 
     363             :  *
     364             :  * Returns: 0 on success or a negative value if an error occurs.
     365             :  */
     366             : int 
     367           0 : xmlSecSimpleKeysStoreAdoptKey(xmlSecKeyStorePtr store, xmlSecKeyPtr key) {
     368             :     xmlSecPtrListPtr list;
     369             :     int ret;
     370             :     
     371           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), -1);
     372           0 :     xmlSecAssert2(key != NULL, -1);
     373             : 
     374           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     375           0 :     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), -1);
     376             : 
     377           0 :     ret = xmlSecPtrListAdd(list, key);
     378           0 :     if(ret < 0) {
     379           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     380           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     381             :                     "xmlSecPtrListAdd",
     382             :                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
     383             :                     XMLSEC_ERRORS_NO_MESSAGE);
     384           0 :         return(-1);
     385             :     }
     386             : 
     387           0 :     return(0);
     388             : }
     389             : 
     390             : /** 
     391             :  * xmlSecSimpleKeysStoreLoad:
     392             :  * @store:              the pointer to simple keys store.
     393             :  * @uri:                the filename.
     394             :  * @keysMngr:           the pointer to associated keys manager. 
     395             :  * 
     396             :  * Reads keys from an XML file.
     397             :  *
     398             :  * Returns: 0 on success or a negative value if an error occurs.
     399             :  */
     400             : int
     401           0 : xmlSecSimpleKeysStoreLoad(xmlSecKeyStorePtr store, const char *uri, 
     402             :                             xmlSecKeysMngrPtr keysMngr) {
     403             :     xmlDocPtr doc;
     404             :     xmlNodePtr root;
     405             :     xmlNodePtr cur;
     406             :     xmlSecKeyPtr key;
     407             :     xmlSecKeyInfoCtx keyInfoCtx;
     408             :     int ret;
     409             : 
     410           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), -1);
     411           0 :     xmlSecAssert2(uri != NULL, -1);    
     412             : 
     413           0 :     doc = xmlParseFile(uri);
     414           0 :     if(doc == NULL) {
     415           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     416           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     417             :                     "xmlParseFile",
     418             :                     XMLSEC_ERRORS_R_XML_FAILED,
     419             :                     "uri=%s", 
     420             :                     xmlSecErrorsSafeString(uri));
     421           0 :         return(-1);
     422             :     }
     423             :     
     424           0 :     root = xmlDocGetRootElement(doc);
     425           0 :     if(!xmlSecCheckNodeName(root, BAD_CAST "Keys", xmlSecNs)) {
     426           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     427           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     428           0 :                     xmlSecErrorsSafeString(xmlSecNodeGetName(root)),
     429             :                     XMLSEC_ERRORS_R_INVALID_NODE,
     430             :                     "expected-node=<xmlsec:Keys>");
     431           0 :         xmlFreeDoc(doc);
     432           0 :         return(-1);
     433             :     }
     434             :         
     435           0 :     cur = xmlSecGetNextElementNode(root->children);
     436           0 :     while((cur != NULL) && xmlSecCheckNodeName(cur, xmlSecNodeKeyInfo, xmlSecDSigNs)) {  
     437           0 :         key = xmlSecKeyCreate();
     438           0 :         if(key == NULL) {
     439           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     440           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     441           0 :                         xmlSecErrorsSafeString(xmlSecNodeGetName(cur)),
     442             :                         XMLSEC_ERRORS_R_INVALID_NODE,
     443             :                         "expected-node=%s",
     444             :                         xmlSecErrorsSafeString(xmlSecNodeKeyInfo));
     445           0 :             xmlFreeDoc(doc);
     446           0 :             return(-1);
     447             :         }
     448             : 
     449           0 :         ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL);
     450           0 :         if(ret < 0) {
     451           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     452           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     453             :                         "xmlSecKeyInfoCtxInitialize",
     454             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     455             :                         XMLSEC_ERRORS_NO_MESSAGE);
     456           0 :             xmlSecKeyDestroy(key);
     457           0 :             xmlFreeDoc(doc);
     458           0 :             return(-1);
     459             :         }
     460             :         
     461           0 :         keyInfoCtx.mode           = xmlSecKeyInfoModeRead;
     462           0 :         keyInfoCtx.keysMngr       = keysMngr;
     463           0 :         keyInfoCtx.flags          = XMLSEC_KEYINFO_FLAGS_DONT_STOP_ON_KEY_FOUND |
     464             :                                     XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;
     465           0 :         keyInfoCtx.keyReq.keyId   = xmlSecKeyDataIdUnknown;
     466           0 :         keyInfoCtx.keyReq.keyType = xmlSecKeyDataTypeAny;
     467           0 :         keyInfoCtx.keyReq.keyUsage= xmlSecKeyDataUsageAny;
     468             : 
     469           0 :         ret = xmlSecKeyInfoNodeRead(cur, key, &keyInfoCtx);
     470           0 :         if(ret < 0) {
     471           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     472           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     473             :                         "xmlSecKeyInfoNodeRead",
     474             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     475             :                         XMLSEC_ERRORS_NO_MESSAGE);
     476           0 :             xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
     477           0 :             xmlSecKeyDestroy(key);
     478           0 :             xmlFreeDoc(doc);
     479           0 :             return(-1);
     480             :         }
     481           0 :         xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
     482             :         
     483           0 :         if(xmlSecKeyIsValid(key)) {
     484           0 :             ret = xmlSecSimpleKeysStoreAdoptKey(store, key);
     485           0 :             if(ret < 0) {
     486           0 :                 xmlSecError(XMLSEC_ERRORS_HERE,
     487           0 :                             xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     488             :                             "xmlSecSimpleKeysStoreAdoptKey",
     489             :                             XMLSEC_ERRORS_R_XMLSEC_FAILED,
     490             :                             XMLSEC_ERRORS_NO_MESSAGE);
     491           0 :                 xmlSecKeyDestroy(key);
     492           0 :                 xmlFreeDoc(doc);
     493           0 :                 return(-1);
     494             :             }
     495             :         } else {
     496             :             /* we have an unknown key in our file, just ignore it */
     497           0 :             xmlSecKeyDestroy(key);
     498             :         }
     499           0 :         cur = xmlSecGetNextElementNode(cur->next);
     500             :     }
     501             :     
     502           0 :     if(cur != NULL) {
     503           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     504           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     505           0 :                     xmlSecErrorsSafeString(xmlSecNodeGetName(cur)),
     506             :                     XMLSEC_ERRORS_R_UNEXPECTED_NODE,
     507             :                     XMLSEC_ERRORS_NO_MESSAGE);
     508           0 :         xmlFreeDoc(doc);
     509           0 :         return(-1);         
     510             :     }
     511             :     
     512           0 :     xmlFreeDoc(doc);
     513           0 :     return(0);
     514             : 
     515             : }
     516             : 
     517             : /** 
     518             :  * xmlSecSimpleKeysStoreSave:
     519             :  * @store:              the pointer to simple keys store.
     520             :  * @filename:           the filename.
     521             :  * @type:               the saved keys type (public, private, ...).
     522             :  * 
     523             :  * Writes keys from @store to an XML file.
     524             :  *
     525             :  * Returns: 0 on success or a negative value if an error occurs.
     526             :  */
     527             : int
     528           0 : xmlSecSimpleKeysStoreSave(xmlSecKeyStorePtr store, const char *filename, xmlSecKeyDataType type) {
     529             :     xmlSecKeyInfoCtx keyInfoCtx;
     530             :     xmlSecPtrListPtr list;
     531             :     xmlSecKeyPtr key;
     532             :     xmlSecSize i, keysSize;    
     533             :     xmlDocPtr doc;
     534             :     xmlNodePtr cur;
     535             :     xmlSecKeyDataPtr data;
     536             :     xmlSecPtrListPtr idsList;
     537             :     xmlSecKeyDataId dataId;
     538             :     xmlSecSize idsSize, j;
     539             :     int ret;
     540             : 
     541           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), -1);
     542           0 :     xmlSecAssert2(filename != NULL, -1);    
     543             : 
     544           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     545           0 :     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), -1);
     546             : 
     547             :     /* create doc */
     548           0 :     doc = xmlSecCreateTree(BAD_CAST "Keys", xmlSecNs);
     549           0 :     if(doc == NULL) {
     550           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     551           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     552             :                     "xmlSecCreateTree",
     553             :                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
     554             :                     XMLSEC_ERRORS_NO_MESSAGE);
     555           0 :         return(-1);
     556             :     }
     557             :     
     558           0 :     idsList = xmlSecKeyDataIdsGet();    
     559           0 :     xmlSecAssert2(idsList != NULL, -1);
     560             :         
     561           0 :     keysSize = xmlSecPtrListGetSize(list);
     562           0 :     idsSize = xmlSecPtrListGetSize(idsList);
     563           0 :     for(i = 0; i < keysSize; ++i) {
     564           0 :         key = (xmlSecKeyPtr)xmlSecPtrListGetItem(list, i);
     565           0 :         xmlSecAssert2(key != NULL, -1);
     566             :             
     567           0 :         cur = xmlSecAddChild(xmlDocGetRootElement(doc), xmlSecNodeKeyInfo, xmlSecDSigNs);
     568           0 :         if(cur == NULL) {
     569           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     570           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     571             :                         "xmlSecAddChild",
     572             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     573             :                         "node=%s",
     574             :                         xmlSecErrorsSafeString(xmlSecNodeKeyInfo));
     575           0 :             xmlFreeDoc(doc); 
     576           0 :             return(-1);
     577             :         }
     578             : 
     579             :         /* special data key name */
     580           0 :         if(xmlSecKeyGetName(key) != NULL) {
     581           0 :             if(xmlSecAddChild(cur, xmlSecNodeKeyName, xmlSecDSigNs) == NULL) {
     582           0 :                 xmlSecError(XMLSEC_ERRORS_HERE,
     583           0 :                             xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     584             :                             "xmlSecAddChild",
     585             :                             XMLSEC_ERRORS_R_XMLSEC_FAILED,
     586             :                             "node=%s",
     587             :                             xmlSecErrorsSafeString(xmlSecNodeKeyName));
     588           0 :                 xmlFreeDoc(doc); 
     589           0 :                 return(-1);
     590             :             }
     591             :         }
     592             :     
     593             :         /* create nodes for other keys data */
     594           0 :         for(j = 0; j < idsSize; ++j) {
     595           0 :             dataId = (xmlSecKeyDataId)xmlSecPtrListGetItem(idsList, j);
     596           0 :             xmlSecAssert2(dataId != xmlSecKeyDataIdUnknown, -1);
     597             : 
     598           0 :             if(dataId->dataNodeName == NULL) {
     599           0 :                 continue;
     600             :             }
     601             :             
     602           0 :             data = xmlSecKeyGetData(key, dataId);
     603           0 :             if(data == NULL) {
     604           0 :                 continue;
     605             :             }
     606             : 
     607           0 :             if(xmlSecAddChild(cur, dataId->dataNodeName, dataId->dataNodeNs) == NULL) {
     608           0 :                 xmlSecError(XMLSEC_ERRORS_HERE,
     609           0 :                             xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     610             :                             "xmlSecAddChild",
     611             :                             XMLSEC_ERRORS_R_XMLSEC_FAILED,
     612             :                             "node=%s", 
     613           0 :                             xmlSecErrorsSafeString(dataId->dataNodeName));
     614           0 :                 xmlFreeDoc(doc); 
     615           0 :                 return(-1);
     616             :             }
     617             :         }
     618             : 
     619           0 :         ret = xmlSecKeyInfoCtxInitialize(&keyInfoCtx, NULL);
     620           0 :         if(ret < 0) {
     621           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     622           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     623             :                         "xmlSecKeyInfoCtxInitialize",
     624             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     625             :                         XMLSEC_ERRORS_NO_MESSAGE);
     626           0 :             xmlFreeDoc(doc);
     627           0 :             return(-1);
     628             :         }
     629             : 
     630           0 :         keyInfoCtx.mode                 = xmlSecKeyInfoModeWrite;
     631           0 :         keyInfoCtx.keyReq.keyId         = xmlSecKeyDataIdUnknown;
     632           0 :         keyInfoCtx.keyReq.keyType       = type;
     633           0 :         keyInfoCtx.keyReq.keyUsage      = xmlSecKeyDataUsageAny;
     634             : 
     635             :         /* finally write key in the node */
     636           0 :         ret = xmlSecKeyInfoNodeWrite(cur, key, &keyInfoCtx);
     637           0 :         if(ret < 0) {
     638           0 :             xmlSecError(XMLSEC_ERRORS_HERE,
     639           0 :                         xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     640             :                         "xmlSecKeyInfoNodeWrite",
     641             :                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
     642             :                         XMLSEC_ERRORS_NO_MESSAGE);
     643           0 :             xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
     644           0 :             xmlFreeDoc(doc); 
     645           0 :             return(-1);
     646             :         }               
     647           0 :         xmlSecKeyInfoCtxFinalize(&keyInfoCtx);
     648             :     }
     649             :     
     650             :     /* now write result */
     651           0 :     ret = xmlSaveFormatFile(filename, doc, 1);
     652           0 :     if(ret < 0) {
     653           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     654           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     655             :                     "xmlSaveFormatFile",
     656             :                     XMLSEC_ERRORS_R_XML_FAILED,
     657             :                     "filename=%s", 
     658             :                     xmlSecErrorsSafeString(filename));
     659           0 :         xmlFreeDoc(doc); 
     660           0 :         return(-1);
     661             :     }      
     662             :     
     663           0 :     xmlFreeDoc(doc);
     664           0 :     return(0);
     665             : }
     666             : 
     667             : /** 
     668             :  * xmlSecSimpleKeysStoreGetKeys:
     669             :  * @store:              the pointer to simple keys store.
     670             :  * 
     671             :  * Gets list of keys from simple keys store.
     672             :  * 
     673             :  * Returns: pointer to the list of keys stored in the keys store or NULL
     674             :  * if an error occurs.
     675             :  */
     676             : xmlSecPtrListPtr 
     677           0 : xmlSecSimpleKeysStoreGetKeys(xmlSecKeyStorePtr store) {
     678             :     xmlSecPtrListPtr list;
     679             : 
     680           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), NULL);
     681             : 
     682           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     683           0 :     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), NULL);
     684             : 
     685           0 :     return list;
     686             : }
     687             : 
     688             : static int
     689           0 : xmlSecSimpleKeysStoreInitialize(xmlSecKeyStorePtr store) {
     690             :     xmlSecPtrListPtr list;
     691             :     int ret;
     692             : 
     693           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), -1);
     694             : 
     695           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     696           0 :     xmlSecAssert2(list != NULL, -1);
     697             :     
     698           0 :     ret = xmlSecPtrListInitialize(list, xmlSecKeyPtrListId);
     699           0 :     if(ret < 0) {
     700           0 :         xmlSecError(XMLSEC_ERRORS_HERE,
     701           0 :                     xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
     702             :                     "xmlSecPtrListInitialize",
     703             :                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
     704             :                     "xmlSecKeyPtrListId");
     705           0 :         return(-1);
     706             :     }
     707             : 
     708           0 :     return(0);    
     709             : }
     710             : 
     711             : static void
     712           0 : xmlSecSimpleKeysStoreFinalize(xmlSecKeyStorePtr store) {
     713             :     xmlSecPtrListPtr list;
     714             :     
     715           0 :     xmlSecAssert(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId));
     716             :     
     717           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     718           0 :     xmlSecAssert(list != NULL);
     719             :     
     720           0 :     xmlSecPtrListFinalize(list);
     721             : }
     722             : 
     723             : static xmlSecKeyPtr 
     724           0 : xmlSecSimpleKeysStoreFindKey(xmlSecKeyStorePtr store, const xmlChar* name, 
     725             :                             xmlSecKeyInfoCtxPtr keyInfoCtx) {
     726             :     xmlSecPtrListPtr list;
     727             :     xmlSecKeyPtr key;
     728             :     xmlSecSize pos, size;
     729             : 
     730           0 :     xmlSecAssert2(xmlSecKeyStoreCheckId(store, xmlSecSimpleKeysStoreId), NULL);
     731           0 :     xmlSecAssert2(keyInfoCtx != NULL, NULL);
     732             : 
     733           0 :     list = xmlSecSimpleKeysStoreGetList(store);
     734           0 :     xmlSecAssert2(xmlSecPtrListCheckId(list, xmlSecKeyPtrListId), NULL);
     735             : 
     736           0 :     size = xmlSecPtrListGetSize(list);
     737           0 :     for(pos = 0; pos < size; ++pos) {
     738           0 :         key = (xmlSecKeyPtr)xmlSecPtrListGetItem(list, pos);
     739           0 :         if((key != NULL) && (xmlSecKeyMatch(key, name, &(keyInfoCtx->keyReq)) == 1)) {
     740           0 :             return(xmlSecKeyDuplicate(key));
     741             :         }
     742             :     }
     743           0 :     return(NULL);
     744             : }
     745             : 

Generated by: LCOV version 1.10