1 /*
2 * XML Security Library (http://www.aleksey.com/xmlsec).
3 *
4 *
5 * This is free software; see Copyright file in the source
6 * distribution for preciese wording.
7 *
8 * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
9 */
10 /**
11 * SECTION:app
12 * @Short_description: Application support functions for Skeleton.
13 * @Stability: Stable
14 *
15 */
16
17 #include "globals.h"
18
19 #include <string.h>
20
21 /* TODO: add Skeleton include files */
22
23 #include <xmlsec/xmlsec.h>
24 #include <xmlsec/keys.h>
25 #include <xmlsec/transforms.h>
26 #include <xmlsec/errors.h>
27
28 #include <xmlsec/skeleton/app.h>
29 #include <xmlsec/skeleton/crypto.h>
30
31 /**
32 * xmlSecSkeletonAppInit:
33 * @config: the path to Skeleton configuration (unused).
34 *
35 * General crypto engine initialization. This function is used
36 * by XMLSec command line utility and called before
37 * @xmlSecInit function.
38 *
39 * Returns: 0 on success or a negative value otherwise.
40 */
41 int
xmlSecSkeletonAppInit(const char * config ATTRIBUTE_UNUSED)42 xmlSecSkeletonAppInit(const char* config ATTRIBUTE_UNUSED) {
43 /* TODO: initialize Skeleton crypto engine */
44 return(0);
45 }
46
47 /**
48 * xmlSecSkeletonAppShutdown:
49 *
50 * General crypto engine shutdown. This function is used
51 * by XMLSec command line utility and called after
52 * @xmlSecShutdown function.
53 *
54 * Returns: 0 on success or a negative value otherwise.
55 */
56 int
xmlSecSkeletonAppShutdown(void)57 xmlSecSkeletonAppShutdown(void) {
58 /* TODO: shutdown Skeleton crypto engine */
59
60 return(0);
61 }
62
63 /**
64 * xmlSecSkeletonAppKeyLoad:
65 * @filename: the key filename.
66 * @format: the key file format.
67 * @pwd: the key file password.
68 * @pwdCallback: the key password callback.
69 * @pwdCallbackCtx: the user context for password callback.
70 *
71 * Reads key from the a file (not implemented yet).
72 *
73 * Returns: pointer to the key or NULL if an error occurs.
74 */
75 xmlSecKeyPtr
xmlSecSkeletonAppKeyLoad(const char * filename,xmlSecKeyDataFormat format,const char * pwd,void * pwdCallback,void * pwdCallbackCtx)76 xmlSecSkeletonAppKeyLoad(const char *filename, xmlSecKeyDataFormat format,
77 const char *pwd,
78 void* pwdCallback,
79 void* pwdCallbackCtx) {
80 xmlSecAssert2(filename != NULL, NULL);
81 xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
82
83 /* TODO: load key */
84 xmlSecNotImplementedError(NULL);
85 return(NULL);
86 }
87
88 /**
89 * xmlSecSkeletonAppKeyLoadMemory:
90 * @data: the key binary data.
91 * @dataSize: the key binary data size.
92 * @format: the key data format.
93 * @pwd: the key data2 password.
94 * @pwdCallback: the key password callback.
95 * @pwdCallbackCtx: the user context for password callback.
96 *
97 * Reads key from a binary @data.
98 *
99 * Returns: pointer to the key or NULL if an error occurs.
100 */
101 xmlSecKeyPtr
xmlSecSkeletonAppKeyLoadMemory(const xmlSecByte * data,xmlSecSize dataSize,xmlSecKeyDataFormat format,const char * pwd,void * pwdCallback,void * pwdCallbackCtx)102 xmlSecSkeletonAppKeyLoadMemory(const xmlSecByte* data, xmlSecSize dataSize, xmlSecKeyDataFormat format,
103 const char *pwd, void* pwdCallback, void* pwdCallbackCtx) {
104 xmlSecAssert2(data != NULL, NULL);
105 xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, NULL);
106
107 /* TODO: load key */
108 xmlSecNotImplementedError(NULL);
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
xmlSecSkeletonAppKeyCertLoad(xmlSecKeyPtr key,const char * filename,xmlSecKeyDataFormat format)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 xmlSecNotImplementedError(NULL);
134 return(-1);
135 }
136
137 /**
138 * xmlSecSkeletonAppKeyCertLoadMemory:
139 * @key: the pointer to key.
140 * @data: the certificate binary data.
141 * @dataSize: the certificate binary data size.
142 * @format: the certificate file format.
143 *
144 * Reads the certificate from memory buffer and adds it to key.
145 *
146 * Returns: 0 on success or a negative value otherwise.
147 */
148 int
xmlSecSkeletonAppKeyCertLoadMemory(xmlSecKeyPtr key,const xmlSecByte * data,xmlSecSize dataSize,xmlSecKeyDataFormat format)149 xmlSecSkeletonAppKeyCertLoadMemory(xmlSecKeyPtr key, const xmlSecByte* data, xmlSecSize dataSize,
150 xmlSecKeyDataFormat format) {
151 xmlSecAssert2(key != NULL, -1);
152 xmlSecAssert2(data != NULL, -1);
153 xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
154
155 /* TODO */
156 xmlSecNotImplementedError(NULL);
157 return(-1);
158 }
159
160 /**
161 * xmlSecSkeletonAppPkcs12Load:
162 * @filename: the PKCS12 key filename.
163 * @pwd: the PKCS12 file password.
164 * @pwdCallback: the password callback.
165 * @pwdCallbackCtx: the user context for password callback.
166 *
167 * Reads key and all associated certificates from the PKCS12 file
168 * (not implemented yet).
169 * For uniformity, call xmlSecSkeletonAppKeyLoad instead of this function. Pass
170 * in format=xmlSecKeyDataFormatPkcs12.
171 *
172 *
173 * Returns: pointer to the key or NULL if an error occurs.
174 */
175 xmlSecKeyPtr
xmlSecSkeletonAppPkcs12Load(const char * filename,const char * pwd ATTRIBUTE_UNUSED,void * pwdCallback ATTRIBUTE_UNUSED,void * pwdCallbackCtx ATTRIBUTE_UNUSED)176 xmlSecSkeletonAppPkcs12Load(const char *filename,
177 const char *pwd ATTRIBUTE_UNUSED,
178 void* pwdCallback ATTRIBUTE_UNUSED,
179 void* pwdCallbackCtx ATTRIBUTE_UNUSED) {
180 xmlSecAssert2(filename != NULL, NULL);
181
182 /* TODO: load pkcs12 file */
183 xmlSecNotImplementedError(NULL);
184 return(NULL);
185 }
186
187 /**
188 * xmlSecSkeletonAppPkcs12LoadMemory:
189 * @data: the key binary data.
190 * @dataSize: the key binary data size.
191 * @pwd: the PKCS12 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 binary data.
196 * For uniformity, call xmlSecSkeletonAppKeyLoad instead of this function. Pass
197 * in format=xmlSecKeyDataFormatPkcs12.
198 *
199 * Returns: pointer to the key or NULL if an error occurs.
200 */
201 xmlSecKeyPtr
xmlSecSkeletonAppPkcs12LoadMemory(const xmlSecByte * data,xmlSecSize dataSize,const char * pwd,void * pwdCallback ATTRIBUTE_UNUSED,void * pwdCallbackCtx ATTRIBUTE_UNUSED)202 xmlSecSkeletonAppPkcs12LoadMemory(const xmlSecByte* data, xmlSecSize dataSize, const char *pwd,
203 void *pwdCallback ATTRIBUTE_UNUSED,
204 void* pwdCallbackCtx ATTRIBUTE_UNUSED) {
205 xmlSecAssert2(data != NULL, NULL);
206
207 /* TODO: load pkcs12 file */
208 xmlSecNotImplementedError(NULL);
209 return(NULL);
210 }
211
212
213
214 /**
215 * xmlSecSkeletonAppKeysMngrCertLoad:
216 * @mngr: the keys manager.
217 * @filename: the certificate file.
218 * @format: the certificate file format.
219 * @type: the flag that indicates is the certificate in @filename
220 * trusted or not.
221 *
222 * Reads cert from @filename and adds to the list of trusted or known
223 * untrusted certs in @store (not implemented yet).
224 *
225 * Returns: 0 on success or a negative value otherwise.
226 */
227 int
xmlSecSkeletonAppKeysMngrCertLoad(xmlSecKeysMngrPtr mngr,const char * filename,xmlSecKeyDataFormat format,xmlSecKeyDataType type ATTRIBUTE_UNUSED)228 xmlSecSkeletonAppKeysMngrCertLoad(xmlSecKeysMngrPtr mngr, const char *filename,
229 xmlSecKeyDataFormat format,
230 xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
231 xmlSecAssert2(mngr != NULL, -1);
232 xmlSecAssert2(filename != NULL, -1);
233 xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
234
235 /* TODO: load cert and add to keys manager */
236 xmlSecNotImplementedError(NULL);
237 return(-1);
238 }
239
240 /**
241 * xmlSecSkeletonAppKeysMngrCertLoadMemory:
242 * @mngr: the pointer to keys manager.
243 * @data: the key binary data.
244 * @dataSize: the key binary data size.
245 * @format: the certificate format (PEM or DER).
246 * @type: the certificate type (trusted/untrusted).
247 *
248 * Reads cert from @data and adds to the list of trusted or known
249 * untrusted certs in @store
250 *
251 * Returns: 0 on success or a negative value otherwise.
252 */
253 int
xmlSecSkeletonAppKeysMngrCertLoadMemory(xmlSecKeysMngrPtr mngr,const xmlSecByte * data,xmlSecSize dataSize,xmlSecKeyDataFormat format,xmlSecKeyDataType type)254 xmlSecSkeletonAppKeysMngrCertLoadMemory(xmlSecKeysMngrPtr mngr, const xmlSecByte* data,
255 xmlSecSize dataSize, xmlSecKeyDataFormat format,
256 xmlSecKeyDataType type) {
257 xmlSecAssert2(mngr != NULL, -1);
258 xmlSecAssert2(data != NULL, -1);
259 xmlSecAssert2(format != xmlSecKeyDataFormatUnknown, -1);
260
261 /* TODO: load cert and add to keys manager */
262 xmlSecNotImplementedError(NULL);
263 return(-1);
264 }
265
266 #endif /* XMLSEC_NO_X509 */
267
268 /**
269 * xmlSecSkeletonAppDefaultKeysMngrInit:
270 * @mngr: the pointer to keys manager.
271 *
272 * Initializes @mngr with simple keys store #xmlSecSimpleKeysStoreId
273 * and a default Skeleton crypto key data stores.
274 *
275 * Returns: 0 on success or a negative value otherwise.
276 */
277 int
xmlSecSkeletonAppDefaultKeysMngrInit(xmlSecKeysMngrPtr mngr)278 xmlSecSkeletonAppDefaultKeysMngrInit(xmlSecKeysMngrPtr mngr) {
279 int ret;
280
281 xmlSecAssert2(mngr != NULL, -1);
282
283 /* TODO: if Skeleton crypto engine has another default
284 * keys storage then use it!
285 */
286
287 /* create simple keys store if needed */
288 if(xmlSecKeysMngrGetKeysStore(mngr) == NULL) {
289 xmlSecKeyStorePtr keysStore;
290
291 keysStore = xmlSecKeyStoreCreate(xmlSecSimpleKeysStoreId);
292 if(keysStore == NULL) {
293 xmlSecInternalError("xmlSecKeyStoreCreate(xmlSecSimpleKeysStoreId)", NULL);
294 return(-1);
295 }
296
297 ret = xmlSecKeysMngrAdoptKeysStore(mngr, keysStore);
298 if(ret < 0) {
299 xmlSecInternalError("xmlSecKeysMngrAdoptKeysStore", NULL);
300 xmlSecKeyStoreDestroy(keysStore);
301 return(-1);
302 }
303 }
304
305 ret = xmlSecSkeletonKeysMngrInit(mngr);
306 if(ret < 0) {
307 xmlSecInternalError("xmlSecSkeletonKeysMngrInit", NULL);
308 return(-1);
309 }
310
311 mngr->getKey = xmlSecKeysMngrGetKey;
312 return(0);
313 }
314
315 /**
316 * xmlSecSkeletonAppDefaultKeysMngrAdoptKey:
317 * @mngr: the pointer to keys manager.
318 * @key: the pointer to key.
319 *
320 * Adds @key to the keys manager @mngr created with #xmlSecSkeletonAppDefaultKeysMngrInit
321 * function.
322 *
323 * Returns: 0 on success or a negative value otherwise.
324 */
325 int
xmlSecSkeletonAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr mngr,xmlSecKeyPtr key)326 xmlSecSkeletonAppDefaultKeysMngrAdoptKey(xmlSecKeysMngrPtr mngr, xmlSecKeyPtr key) {
327 xmlSecKeyStorePtr store;
328 int ret;
329
330 xmlSecAssert2(mngr != NULL, -1);
331 xmlSecAssert2(key != NULL, -1);
332
333 /* TODO: if Skeleton crypto engine has another default
334 * keys storage then use it!
335 */
336
337 store = xmlSecKeysMngrGetKeysStore(mngr);
338 if(store == NULL) {
339 xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
340 return(-1);
341 }
342
343 ret = xmlSecSimpleKeysStoreAdoptKey(store, key);
344 if(ret < 0) {
345 xmlSecInternalError("xmlSecSimpleKeysStoreAdoptKey", NULL);
346 return(-1);
347 }
348
349 return(0);
350 }
351
352 /**
353 * xmlSecSkeletonAppDefaultKeysMngrLoad:
354 * @mngr: the pointer to keys manager.
355 * @uri: the uri.
356 *
357 * Loads XML keys file from @uri to the keys manager @mngr created
358 * with #xmlSecSkeletonAppDefaultKeysMngrInit function.
359 *
360 * Returns: 0 on success or a negative value otherwise.
361 */
362 int
xmlSecSkeletonAppDefaultKeysMngrLoad(xmlSecKeysMngrPtr mngr,const char * uri)363 xmlSecSkeletonAppDefaultKeysMngrLoad(xmlSecKeysMngrPtr mngr, const char* uri) {
364 xmlSecKeyStorePtr store;
365 int ret;
366
367 xmlSecAssert2(mngr != NULL, -1);
368 xmlSecAssert2(uri != NULL, -1);
369
370 /* TODO: if Skeleton crypto engine has another default
371 * keys storage then use it!
372 */
373
374 store = xmlSecKeysMngrGetKeysStore(mngr);
375 if(store == NULL) {
376 xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
377 return(-1);
378 }
379
380 ret = xmlSecSimpleKeysStoreLoad(store, uri, mngr);
381 if(ret < 0) {
382 xmlSecInternalError2("xmlSecSimpleKeysStoreLoad", NULL,
383 "uri=%s", xmlSecErrorsSafeString(uri));
384 return(-1);
385 }
386
387 return(0);
388 }
389
390 /**
391 * xmlSecSkeletonAppDefaultKeysMngrSave:
392 * @mngr: the pointer to keys manager.
393 * @filename: the destination filename.
394 * @type: the type of keys to save (public/private/symmetric).
395 *
396 * Saves keys from @mngr to XML keys file.
397 *
398 * Returns: 0 on success or a negative value otherwise.
399 */
400 int
xmlSecSkeletonAppDefaultKeysMngrSave(xmlSecKeysMngrPtr mngr,const char * filename,xmlSecKeyDataType type)401 xmlSecSkeletonAppDefaultKeysMngrSave(xmlSecKeysMngrPtr mngr, const char* filename, xmlSecKeyDataType type) {
402 xmlSecKeyStorePtr store;
403 int ret;
404
405 xmlSecAssert2(mngr != NULL, -1);
406 xmlSecAssert2(filename != NULL, -1);
407
408 /* TODO: if Skeleton crypto engine has another default
409 * keys storage then use it!
410 */
411
412 store = xmlSecKeysMngrGetKeysStore(mngr);
413 if(store == NULL) {
414 xmlSecInternalError("xmlSecKeysMngrGetKeysStore", NULL);
415 return(-1);
416 }
417
418 ret = xmlSecSimpleKeysStoreSave(store, filename, type);
419 if(ret < 0) {
420 xmlSecInternalError2("xmlSecSimpleKeysStoreSave", NULL,
421 "filename=%s",
422 xmlSecErrorsSafeString(filename));
423 return(-1);
424 }
425
426 return(0);
427 }
428
429 /**
430 * xmlSecSkeletonAppGetDefaultPwdCallback:
431 *
432 * Gets default password callback.
433 *
434 * Returns: default password callback.
435 */
436 void*
xmlSecSkeletonAppGetDefaultPwdCallback(void)437 xmlSecSkeletonAppGetDefaultPwdCallback(void) {
438 /* TODO */
439 return(NULL);
440 }
441
442