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