Git init
[external/xmlsec1.git] / src / gnutls / app.c
1 /** 
2  * XMLSec library
3  *
4  * This is free software; see Copyright file in the source
5  * distribution for preciese wording.
6  * 
7  * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
8  */
9 #include "globals.h"
10
11 #include <string.h>
12
13 #include <gnutls/gnutls.h>
14
15 #include <xmlsec/xmlsec.h>
16 #include <xmlsec/keys.h>
17 #include <xmlsec/transforms.h>
18 #include <xmlsec/errors.h>
19
20 #include <xmlsec/gnutls/app.h>
21 #include <xmlsec/gnutls/crypto.h>
22
23 /**
24  * xmlSecGnuTLSAppInit:
25  * @config:             the path to GnuTLS configuration (unused).
26  * 
27  * General crypto engine initialization. This function is used
28  * by XMLSec command line utility and called before 
29  * @xmlSecInit function.
30  *
31  * Returns: 0 on success or a negative value otherwise.
32  */
33 int
34 xmlSecGnuTLSAppInit(const char* config ATTRIBUTE_UNUSED) {
35     int ret;
36
37     ret = gnutls_global_init();
38     if(ret != 0) {
39         xmlSecError(XMLSEC_ERRORS_HERE,
40                     NULL,
41                     "gnutls_global_init",
42                     XMLSEC_ERRORS_R_CRYPTO_FAILED,
43                     "ret=%d", ret);
44         return(-1);
45     }
46     return(0);
47 }
48
49 /**
50  * xmlSecGnuTLSAppShutdown:
51  * 
52  * General crypto engine shutdown. This function is used
53  * by XMLSec command line utility and called after 
54  * @xmlSecShutdown function.
55  *
56  * Returns: 0 on success or a negative value otherwise.
57  */
58 int
59 xmlSecGnuTLSAppShutdown(void) {
60     gnutls_global_deinit();
61     return(0);
62 }
63
64 /**
65  * xmlSecGnuTLSAppKeyLoad:
66  * @filename:           the key filename.
67  * @format:             the key file format.
68  * @pwd:                the key file password.
69  * @pwdCallback:        the key password callback.
70  * @pwdCallbackCtx:     the user context for password callback.
71  *
72  * Reads key from the a file (not implemented yet).
73  *
74  * Returns: pointer to the key or NULL if an error occurs.
75  */
76 xmlSecKeyPtr
77 xmlSecGnuTLSAppKeyLoad(const char *filename, xmlSecKeyDataFormat format,
78                         const char *pwd,
79                         void* pwdCallback,
80                         void* pwdCallbackCtx) {
81     xmlSecAssert2(filename != NULL, NULL);
82     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
83     
84
85     if (format == xmlSecKeyDataFormatPkcs12) {
86         return (xmlSecGnuTLSAppPkcs12Load(filename, pwd, pwdCallback,
87                                           pwdCallbackCtx));
88     }
89
90     /* TODO */
91     xmlSecError(XMLSEC_ERRORS_HERE,
92                 NULL,
93                 "xmlSecGnuTLSAppKeyLoad",
94                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
95                 XMLSEC_ERRORS_NO_MESSAGE);
96     return(NULL);
97 }
98
99 /**
100  * xmlSecGnuTLSAppKeyLoadMemory:
101  * @data:               the binary key data.
102  * @dataSize:           the size of binary key.
103  * @format:             the key file format.
104  * @pwd:                the key file password.
105  * @pwdCallback:        the key password callback.
106  * @pwdCallbackCtx:     the user context for password callback.
107  *
108  * Reads key from the memory buffer (not implemented yet).
109  *
110  * Returns: pointer to the key or NULL if an error occurs.
111  */
112 xmlSecKeyPtr
113 xmlSecGnuTLSAppKeyLoadMemory(const xmlSecByte* data, xmlSecSize dataSize, 
114                         xmlSecKeyDataFormat format, const char *pwd, 
115                         void* pwdCallback, void* pwdCallbackCtx) {
116     xmlSecAssert2(data != NULL, NULL);
117     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
118
119     if (format == xmlSecKeyDataFormatPkcs12) {
120         return (xmlSecGnuTLSAppPkcs12LoadMemory(data, dataSize, pwd, 
121                                         pwdCallback, pwdCallbackCtx));
122     }
123
124     /* TODO */
125     xmlSecError(XMLSEC_ERRORS_HERE,
126                 NULL,
127                 "xmlSecGnuTLSAppKeyLoadMemory",
128                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
129                 XMLSEC_ERRORS_NO_MESSAGE);
130     return(NULL);
131 }
132
133 #ifndef XMLSEC_NO_X509
134 /**
135  * xmlSecGnuTLSAppKeyCertLoad:
136  * @key:                the pointer to key.
137  * @filename:           the certificate filename.
138  * @format:             the certificate file format.
139  *
140  * Reads the certificate from $@filename and adds it to key
141  * (not implemented yet).
142  * 
143  * Returns: 0 on success or a negative value otherwise.
144  */
145 int             
146 xmlSecGnuTLSAppKeyCertLoad(xmlSecKeyPtr key, const char* filename, 
147                           xmlSecKeyDataFormat format) {
148     xmlSecAssert2(key != NULL, -1);
149     xmlSecAssert2(filename != NULL, -1);
150     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
151     
152     /* TODO */
153     xmlSecError(XMLSEC_ERRORS_HERE,
154                 NULL,
155                 "xmlSecGnuTLSAppKeyCertLoad",
156                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
157                 XMLSEC_ERRORS_NO_MESSAGE);
158     return(-1);
159 }
160
161 /**
162  * xmlSecGnuTLSAppKeyCertLoadMemory:
163  * @key:                the pointer to key.
164  * @data:               the certificate binary data.
165  * @dataSize:           the certificate binary data size.
166  * @format:             the certificate file format.
167  *
168  * Reads the certificate from memory buffer and adds it to key (not implemented yet).
169  * 
170  * Returns: 0 on success or a negative value otherwise.
171  */
172 int             
173 xmlSecGnuTLSAppKeyCertLoadMemory(xmlSecKeyPtr key, const xmlSecByte* data, xmlSecSize dataSize, 
174                                 xmlSecKeyDataFormat format) {
175     xmlSecAssert2(key != NULL, -1);
176     xmlSecAssert2(data != NULL, -1);
177     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
178
179     /* TODO */
180     xmlSecError(XMLSEC_ERRORS_HERE,
181                 NULL,
182                 "xmlSecGnuTLSAppKeyCertLoadMemory",
183                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
184                 XMLSEC_ERRORS_NO_MESSAGE);
185     return(-1);
186 }
187
188 /**
189  * xmlSecGnuTLSAppPkcs12Load:
190  * @filename:           the PKCS12 key filename.
191  * @pwd:                the PKCS12 file password.
192  * @pwdCallback:        the password callback.
193  * @pwdCallbackCtx:     the user context for password callback.
194  *
195  * Reads key and all associated certificates from the PKCS12 file
196  * (not implemented yet).
197  * For uniformity, call xmlSecGnuTLSAppKeyLoad instead of this function. Pass
198  * in format=xmlSecKeyDataFormatPkcs12.
199  *
200  * Returns: pointer to the key or NULL if an error occurs.
201  */
202 xmlSecKeyPtr    
203 xmlSecGnuTLSAppPkcs12Load(const char *filename, 
204                           const char *pwd ATTRIBUTE_UNUSED,
205                           void* pwdCallback ATTRIBUTE_UNUSED, 
206                           void* pwdCallbackCtx ATTRIBUTE_UNUSED) {
207     xmlSecAssert2(filename != NULL, NULL);
208
209     /* TODO */
210     xmlSecError(XMLSEC_ERRORS_HERE,
211                 NULL,
212                 "xmlSecGnuTLSAppPkcs12Load",
213                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
214                 XMLSEC_ERRORS_NO_MESSAGE);
215     return(NULL); 
216 }
217
218 /**
219  * xmlSecGnuTLSAppPkcs12LoadMemory:
220  * @data:               the PKCS12 binary data.
221  * @dataSize:           the PKCS12 binary data size.
222  * @pwd:                the PKCS12 file password.
223  * @pwdCallback:        the password callback.
224  * @pwdCallbackCtx:     the user context for password callback.
225  *
226  * Reads key and all associated certificates from the PKCS12 data in memory buffer.
227  * For uniformity, call xmlSecGnuTLSAppKeyLoadMemory instead of this function. Pass
228  * in format=xmlSecKeyDataFormatPkcs12 (not implemented yet).
229  *
230  * Returns: pointer to the key or NULL if an error occurs.
231  */
232 xmlSecKeyPtr    
233 xmlSecGnuTLSAppPkcs12LoadMemory(const xmlSecByte* data, xmlSecSize dataSize, 
234                            const char *pwd, void* pwdCallback, 
235                            void* pwdCallbackCtx) {
236     xmlSecAssert2(data != NULL, NULL);
237     /* TODO */
238     xmlSecError(XMLSEC_ERRORS_HERE,
239                 NULL,
240                 "xmlSecGnuTLSAppPkcs12LoadMemory",
241                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
242                 XMLSEC_ERRORS_NO_MESSAGE);
243     return(NULL); 
244 }
245
246 /**
247  * xmlSecGnuTLSAppKeysMngrCertLoad:
248  * @mngr:               the keys manager.
249  * @filename:           the certificate file.
250  * @format:             the certificate file format.
251  * @type:               the flag that indicates is the certificate in @filename
252  *                      trusted or not.
253  * 
254  * Reads cert from @filename and adds to the list of trusted or known
255  * untrusted certs in @store (not implemented yet).
256  *
257  * Returns: 0 on success or a negative value otherwise.
258  */
259 int
260 xmlSecGnuTLSAppKeysMngrCertLoad(xmlSecKeysMngrPtr mngr, const char *filename, 
261                                 xmlSecKeyDataFormat format, 
262                                 xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
263     xmlSecAssert2(mngr != NULL, -1);
264     xmlSecAssert2(filename != NULL, -1);
265     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
266
267     /* TODO */
268     xmlSecError(XMLSEC_ERRORS_HERE,
269                 NULL,
270                 "xmlSecGnuTLSAppKeysMngrCertLoad",
271                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
272                 XMLSEC_ERRORS_NO_MESSAGE);
273     return(-1);
274 }
275
276 /**
277  * xmlSecGnuTLSAppKeysMngrCertLoadMemory:
278  * @mngr:               the keys manager.
279  * @data:               the certificate binary data.
280  * @dataSize:           the certificate binary data size.
281  * @format:             the certificate file format.
282  * @type:               the flag that indicates is the certificate trusted or not.
283  * 
284  * Reads cert from binary buffer @data and adds to the list of trusted or known
285  * untrusted certs in @store (not implemented yet).
286  *
287  * Returns: 0 on success or a negative value otherwise.
288  */
289 int
290 xmlSecGnuTLSAppKeysMngrCertLoadMemory(xmlSecKeysMngrPtr mngr, const xmlSecByte* data,
291                                     xmlSecSize dataSize, xmlSecKeyDataFormat format, 
292                                     xmlSecKeyDataType type) {
293     xmlSecAssert2(mngr != NULL, -1);
294     xmlSecAssert2(data != NULL, -1);
295     xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
296
297     /* TODO */
298     xmlSecError(XMLSEC_ERRORS_HERE,
299                 NULL,
300                 "xmlSecGnuTLSAppKeysMngrCertLoadMemory",
301                 XMLSEC_ERRORS_R_NOT_IMPLEMENTED,
302                 XMLSEC_ERRORS_NO_MESSAGE);
303     return(-1);
304 }
305
306 #endif /* XMLSEC_NO_X509 */
307
308 /**
309  * xmlSecGnuTLSAppDefaultKeysMngrInit:
310  * @mngr:               the pointer to keys manager.
311  *
312  * Initializes @mngr with simple keys store #xmlSecSimpleKeysStoreId
313  * and a default GnuTLS crypto key data stores.
314  *
315  * Returns: 0 on success or a negative value otherwise.
316  */ 
317 int
318 xmlSecGnuTLSAppDefaultKeysMngrInit(xmlSecKeysMngrPtr mngr) {
319     int ret;
320     
321     xmlSecAssert2(mngr != NULL, -1);
322
323     /* create simple keys store if needed */        
324     if(xmlSecKeysMngrGetKeysStore(mngr) == NULL) {
325         xmlSecKeyStorePtr keysStore;
326
327         keysStore = xmlSecKeyStoreCreate(xmlSecSimpleKeysStoreId);
328         if(keysStore == NULL) {
329             xmlSecError(XMLSEC_ERRORS_HERE,
330                         NULL,
331                         "xmlSecKeyStoreCreate",
332                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
333                         "xmlSecSimpleKeysStoreId");
334             return(-1);
335         }
336         
337         ret = xmlSecKeysMngrAdoptKeysStore(mngr, keysStore);
338         if(ret < 0) {
339             xmlSecError(XMLSEC_ERRORS_HERE,
340                         NULL,
341                         "xmlSecKeysMngrAdoptKeysStore",
342                         XMLSEC_ERRORS_R_XMLSEC_FAILED,
343                         XMLSEC_ERRORS_NO_MESSAGE);
344             xmlSecKeyStoreDestroy(keysStore);
345             return(-1);        
346         }
347     }
348
349     ret = xmlSecGnuTLSKeysMngrInit(mngr);    
350     if(ret < 0) {
351         xmlSecError(XMLSEC_ERRORS_HERE,
352                     NULL,
353                     "xmlSecGnuTLSKeysMngrInit",
354                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
355                     XMLSEC_ERRORS_NO_MESSAGE);
356         return(-1); 
357     }
358     
359     /* TODO */
360     mngr->getKey = xmlSecKeysMngrGetKey;
361     return(0);
362 }
363
364 /**
365  * xmlSecGnuTLSAppDefaultKeysMngrAdoptKey:
366  * @mngr:               the pointer to keys manager.
367  * @key:                the pointer to key.
368  *
369  * Adds @key to the keys manager @mngr created with #xmlSecGnuTLSAppDefaultKeysMngrInit
370  * function.
371  *  
372  * Returns: 0 on success or a negative value otherwise.
373  */ 
374 int 
375 xmlSecGnuTLSAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr mngr, xmlSecKeyPtr key) {
376     xmlSecKeyStorePtr store;
377     int ret;
378     
379     xmlSecAssert2(mngr != NULL, -1);
380     xmlSecAssert2(key != NULL, -1);
381     
382     store = xmlSecKeysMngrGetKeysStore(mngr);
383     if(store == NULL) {
384         xmlSecError(XMLSEC_ERRORS_HERE,
385                     NULL,
386                     "xmlSecKeysMngrGetKeysStore",
387                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
388                     XMLSEC_ERRORS_NO_MESSAGE);
389         return(-1);
390     }
391     
392     ret = xmlSecSimpleKeysStoreAdoptKey(store, key);
393     if(ret < 0) {
394         xmlSecError(XMLSEC_ERRORS_HERE,
395                     NULL,
396                     "xmlSecSimpleKeysStoreAdoptKey",
397                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
398                     XMLSEC_ERRORS_NO_MESSAGE);
399         return(-1);
400     }
401     
402     return(0);
403 }
404
405 /**
406  * xmlSecGnuTLSAppDefaultKeysMngrLoad:
407  * @mngr:               the pointer to keys manager.
408  * @uri:                the uri.
409  *
410  * Loads XML keys file from @uri to the keys manager @mngr created 
411  * with #xmlSecGnuTLSAppDefaultKeysMngrInit function.
412  *  
413  * Returns: 0 on success or a negative value otherwise.
414  */ 
415 int 
416 xmlSecGnuTLSAppDefaultKeysMngrLoad(xmlSecKeysMngrPtr mngr, const char* uri) {
417     xmlSecKeyStorePtr store;
418     int ret;
419     
420     xmlSecAssert2(mngr != NULL, -1);
421     xmlSecAssert2(uri != NULL, -1);
422     
423     store = xmlSecKeysMngrGetKeysStore(mngr);
424     if(store == NULL) {
425         xmlSecError(XMLSEC_ERRORS_HERE,
426                     NULL,
427                     "xmlSecKeysMngrGetKeysStore",
428                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
429                     XMLSEC_ERRORS_NO_MESSAGE);
430         return(-1);
431     }
432     
433     ret = xmlSecSimpleKeysStoreLoad(store, uri, mngr);
434     if(ret < 0) {
435         xmlSecError(XMLSEC_ERRORS_HERE,
436                     NULL,
437                     "xmlSecSimpleKeysStoreLoad",
438                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
439                     "uri=%s", xmlSecErrorsSafeString(uri));
440         return(-1);
441     }
442     
443     return(0);
444 }
445
446 /**
447  * xmlSecGnuTLSAppDefaultKeysMngrSave:
448  * @mngr:               the pointer to keys manager.
449  * @filename:           the destination filename.
450  * @type:               the type of keys to save (public/private/symmetric).
451  *
452  * Saves keys from @mngr to  XML keys file.
453  *  
454  * Returns: 0 on success or a negative value otherwise.
455  */ 
456 int 
457 xmlSecGnuTLSAppDefaultKeysMngrSave(xmlSecKeysMngrPtr mngr, const char* filename, xmlSecKeyDataType type) {
458     xmlSecKeyStorePtr store;
459     int ret;
460     
461     xmlSecAssert2(mngr != NULL, -1);
462     xmlSecAssert2(filename != NULL, -1);
463     
464     store = xmlSecKeysMngrGetKeysStore(mngr);
465     if(store == NULL) {
466         xmlSecError(XMLSEC_ERRORS_HERE,
467                     NULL,
468                     "xmlSecKeysMngrGetKeysStore",
469                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
470                     XMLSEC_ERRORS_NO_MESSAGE);
471         return(-1);
472     }
473     
474     ret = xmlSecSimpleKeysStoreSave(store, filename, type);
475     if(ret < 0) {
476         xmlSecError(XMLSEC_ERRORS_HERE,
477                     NULL,
478                     "xmlSecSimpleKeysStoreSave",
479                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
480                     "filename=%s", 
481                     xmlSecErrorsSafeString(filename));
482         return(-1);
483     }
484     
485     return(0);
486 }
487
488 /**
489  * xmlSecGnuTLSAppGetDefaultPwdCallback:
490  *
491  * Gets default password callback.
492  *
493  * Returns: default password callback.
494  */
495 void*
496 xmlSecGnuTLSAppGetDefaultPwdCallback(void) {
497     return(NULL);
498 }
499