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