xmlsecurityengine/xmlsec/src/xmlsec_xmlsec.c
changeset 0 e35f40988205
child 24 74f0b3eb154c
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 /** 
       
     2  * XML Security Library (http://www.aleksey.com/xmlsec).
       
     3  *
       
     4  * General functions.
       
     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 #include "xmlsec_globals.h"
       
    13 
       
    14 #include <stdlib.h>
       
    15 #include <stdio.h>
       
    16 
       
    17 #include <libxml2_tree.h>
       
    18 
       
    19 #include "xmlsec_xmlsec.h"
       
    20 #include "xmlsec_xmltree.h"
       
    21 #include "xmlsec_keys.h"
       
    22 #include "xmlsec_transforms.h"
       
    23 #include "xmlsec_app.h"
       
    24 #include "xmlsec_io.h"
       
    25 #include "xmlsec_xkms.h"
       
    26 #include "xmlsec_errors.h"
       
    27 
       
    28 /**
       
    29  * xmlSecInit:
       
    30  *
       
    31  * Initializes XML Security Library. The depended libraries
       
    32  * (LibXML and LibXSLT) must be initialized before.
       
    33  *
       
    34  * Returns 0 on success or a negative value otherwise.
       
    35  */
       
    36 EXPORT_C
       
    37 int
       
    38 xmlSecInit(void) {
       
    39 
       
    40 	xmlSecErrorsInit();
       
    41     xmlSecIOInit();
       
    42     
       
    43 #ifndef XMLSEC_NO_CRYPTO_DYNAMIC_LOADING
       
    44     if(xmlSecCryptoDLInit() < 0) {
       
    45 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    46 		    NULL,
       
    47 		    "xmlSecCryptoDLInit",
       
    48 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    49 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    50 	return(-1);
       
    51     }
       
    52 #endif /* XMLSEC_NO_CRYPTO_DYNAMIC_LOADING */
       
    53     
       
    54     if(xmlSecKeyDataIdsInit() < 0) {
       
    55 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    56 		    NULL,
       
    57 		    "xmlSecKeyDataIdsInit",
       
    58 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    59 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    60 	return(-1);
       
    61     }
       
    62     
       
    63     if(xmlSecTransformIdsInit() < 0) {
       
    64 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    65 		    NULL,
       
    66 		    "xmlSecTransformIdsInit",
       
    67 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    68 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    69 	return(-1);
       
    70     }
       
    71     
       
    72 #ifndef XMLSEC_NO_XKMS    
       
    73     if(xmlSecXkmsRespondWithIdsInit() < 0) {
       
    74 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    75 		    NULL,
       
    76 		    "xmlSecXkmsRespondWithIdsInit",
       
    77 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    78 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    79 	return(-1);
       
    80     }
       
    81     if(xmlSecXkmsServerRequestIdsInit() < 0) {
       
    82 	xmlSecError(XMLSEC_ERRORS_HERE,
       
    83 		    NULL,
       
    84 		    "xmlSecXkmsServerRequestIdsInit",
       
    85 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
    86 		    XMLSEC_ERRORS_NO_MESSAGE);
       
    87 	return(-1);
       
    88     }
       
    89 #endif /* XMLSEC_NO_XKMS */
       
    90 
       
    91     /* we use rand() function to generate id attributes */
       
    92     srand(time(NULL));
       
    93     return(0);
       
    94 }
       
    95 
       
    96 /**
       
    97  * xmlSecShutdown:
       
    98  *
       
    99  * Clean ups the XML Security Library.
       
   100  *
       
   101  * Returns 0 on success or a negative value otherwise.
       
   102  */
       
   103 EXPORT_C
       
   104 int
       
   105 xmlSecShutdown(void) {
       
   106     int res = 0;    
       
   107 
       
   108 #ifndef XMLSEC_NO_XKMS    
       
   109     xmlSecXkmsServerRequestIdsShutdown();
       
   110     xmlSecXkmsRespondWithIdsShutdown();
       
   111 #endif /* XMLSEC_NO_XKMS */
       
   112 
       
   113     xmlSecTransformIdsShutdown();
       
   114     xmlSecKeyDataIdsShutdown();
       
   115 
       
   116 #ifndef XMLSEC_NO_CRYPTO_DYNAMIC_LOADING
       
   117     if(xmlSecCryptoDLShutdown() < 0) {
       
   118 	xmlSecError(XMLSEC_ERRORS_HERE,
       
   119 		    NULL,
       
   120 		    "xmlSecCryptoDLShutdown",
       
   121 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   122 		    XMLSEC_ERRORS_NO_MESSAGE);
       
   123 	res = -1;
       
   124     }
       
   125 #endif /* XMLSEC_NO_CRYPTO_DYNAMIC_LOADING */
       
   126 
       
   127     xmlSecIOShutdown();
       
   128     xmlSecErrorsShutdown();  
       
   129 
       
   130     return(res);
       
   131 }
       
   132 
       
   133 /** 
       
   134  * xmlSecCheckVersionExt:
       
   135  * @major:		the major version number.
       
   136  * @minor:		the minor version number.
       
   137  * @subminor:		the subminor version number.
       
   138  * @mode:		the version check mode.
       
   139  *
       
   140  * Checks if the loaded version of xmlsec library could be used.
       
   141  *
       
   142  * Returns 1 if the loaded xmlsec library version is OK to use
       
   143  * 0 if it is not or a negative value if an error occurs.
       
   144  */
       
   145 EXPORT_C
       
   146 int 
       
   147 xmlSecCheckVersionExt(int major, int minor, int subminor, xmlSecCheckVersionMode mode) {
       
   148     /* we always want to have a match for major version number */
       
   149     if(major != XMLSEC_VERSION_MAJOR) {
       
   150 	xmlSecError(XMLSEC_ERRORS_HERE, 
       
   151 		    NULL,
       
   152 		    NULL,
       
   153 		    XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   154 		    "expected major version=%d;real major version=%d",
       
   155 		    XMLSEC_VERSION_MAJOR, major);
       
   156 	return(0);
       
   157     }
       
   158     
       
   159     switch(mode) {
       
   160     case xmlSecCheckVersionExact:
       
   161 	if((minor != XMLSEC_VERSION_MINOR) || (subminor != XMLSEC_VERSION_SUBMINOR)) {
       
   162 	    xmlSecError(XMLSEC_ERRORS_HERE, 
       
   163 			NULL,
       
   164 			NULL,
       
   165 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   166 			"mode=exact;expected minor version=%d;real minor version=%d;expected subminor version=%d;real subminor version=%d",
       
   167 			XMLSEC_VERSION_MINOR, minor,
       
   168 			XMLSEC_VERSION_SUBMINOR, subminor);
       
   169 	    return(0);
       
   170 	}
       
   171 	break;
       
   172     case xmlSecCheckVersionABICompatible:
       
   173 	if((minor < XMLSEC_VERSION_MINOR) ||
       
   174 	   ((minor == XMLSEC_VERSION_MINOR) && 
       
   175 	    (subminor < XMLSEC_VERSION_SUBMINOR))) {
       
   176 	    xmlSecError(XMLSEC_ERRORS_HERE, 
       
   177 			NULL,
       
   178 			NULL,
       
   179 			XMLSEC_ERRORS_R_XMLSEC_FAILED,
       
   180 			"mode=abi compatible;expected minor version=%d;real minor version=%d;expected subminor version=%d;real subminor version=%d",
       
   181 			XMLSEC_VERSION_MINOR, minor,
       
   182 			XMLSEC_VERSION_SUBMINOR, subminor);
       
   183 	    return(0);
       
   184 	}
       
   185 	break;
       
   186     }
       
   187     
       
   188     return(1);
       
   189 }
       
   190 
       
   191