1 /**
2 *
3 * XMLSec library
4 *
5 *
6 * See Copyright for the status of this software.
7 *
8 * Copyright (C) 2002-2016 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
9 */
10 #if defined(_MSC_VER) && _MSC_VER < 1900
11 #define snprintf _snprintf
12 #endif
13
14 #include <string.h>
15
16 #include <xmlsec/xmlsec.h>
17 #include <xmlsec/keys.h>
18 #include <xmlsec/transforms.h>
19 #include <xmlsec/errors.h>
20
21 #include "crypto.h"
22
23 int
xmlSecAppCryptoInit(const char * config)24 xmlSecAppCryptoInit(const char* config) {
25 if(xmlSecCryptoAppInit(config) < 0) {
26 fprintf(stderr, "Error: xmlSecCryptoAppInit failed\n");
27 return(-1);
28 }
29 if(xmlSecCryptoInit() < 0) {
30 fprintf(stderr, "Error: xmlSecCryptoInit failed\n");
31 return(-1);
32 }
33
34 return(0);
35 }
36
37 int
xmlSecAppCryptoShutdown(void)38 xmlSecAppCryptoShutdown(void) {
39 if(xmlSecCryptoShutdown() < 0) {
40 fprintf(stderr, "Error: xmlSecCryptoShutdown failed\n");
41 return(-1);
42 }
43
44 if(xmlSecCryptoAppShutdown() < 0) {
45 fprintf(stderr, "Error: xmlSecCryptoAppShutdown failed\n");
46 return(-1);
47 }
48 return(0);
49 }
50
51 int
xmlSecAppCryptoSimpleKeysMngrInit(xmlSecKeysMngrPtr mngr)52 xmlSecAppCryptoSimpleKeysMngrInit(xmlSecKeysMngrPtr mngr) {
53 xmlSecAssert2(mngr != NULL, -1);
54
55 return(xmlSecCryptoAppDefaultKeysMngrInit(mngr));
56 }
57
58 int
xmlSecAppCryptoSimpleKeysMngrLoad(xmlSecKeysMngrPtr mngr,const char * filename)59 xmlSecAppCryptoSimpleKeysMngrLoad(xmlSecKeysMngrPtr mngr, const char *filename) {
60 xmlSecAssert2(mngr != NULL, -1);
61 xmlSecAssert2(filename != NULL, -1);
62
63 return(xmlSecCryptoAppDefaultKeysMngrLoad(mngr, filename));
64 }
65
66 int
xmlSecAppCryptoSimpleKeysMngrSave(xmlSecKeysMngrPtr mngr,const char * filename,xmlSecKeyDataType type)67 xmlSecAppCryptoSimpleKeysMngrSave(xmlSecKeysMngrPtr mngr, const char *filename, xmlSecKeyDataType type) {
68 xmlSecAssert2(mngr != NULL, -1);
69 xmlSecAssert2(filename != NULL, -1);
70
71 return(xmlSecCryptoAppDefaultKeysMngrSave(mngr, filename, type));
72 }
73
74 int
xmlSecAppCryptoSimpleKeysMngrCertLoad(xmlSecKeysMngrPtr mngr,const char * filename,xmlSecKeyDataFormat format,xmlSecKeyDataType type)75 xmlSecAppCryptoSimpleKeysMngrCertLoad(xmlSecKeysMngrPtr mngr, const char *filename,
76 xmlSecKeyDataFormat format, xmlSecKeyDataType type) {
77 xmlSecAssert2(mngr != NULL, -1);
78 xmlSecAssert2(filename != NULL, -1);
79
80 #ifndef XMLSEC_NO_X509
81 return(xmlSecCryptoAppKeysMngrCertLoad(mngr, filename, format, type));
82 #else /* XMLSEC_NO_X509 */
83 return(-1);
84 #endif /* XMLSEC_NO_X509 */
85 }
86
87 int
xmlSecAppCryptoSimpleKeysMngrKeyAndCertsLoad(xmlSecKeysMngrPtr mngr,const char * files,const char * pwd,const char * name,xmlSecKeyDataFormat format)88 xmlSecAppCryptoSimpleKeysMngrKeyAndCertsLoad(xmlSecKeysMngrPtr mngr,
89 const char* files, const char* pwd,
90 const char* name,
91 xmlSecKeyDataFormat format) {
92 xmlSecKeyPtr key;
93 int ret;
94
95 xmlSecAssert2(mngr != NULL, -1);
96 xmlSecAssert2(files != NULL, -1);
97
98 /* first is the key file */
99 key = xmlSecCryptoAppKeyLoad(files, format, pwd,
100 xmlSecCryptoAppGetDefaultPwdCallback(), (void*)files);
101 if(key == NULL) {
102 fprintf(stderr, "Error: xmlSecCryptoAppKeyLoad failed: file=%s\n",
103 xmlSecErrorsSafeString(files));
104 return(-1);
105 }
106
107 if(name != NULL) {
108 ret = xmlSecKeySetName(key, BAD_CAST name);
109 if(ret < 0) {
110 fprintf(stderr, "Error: xmlSecKeySetName failed: name=%s\n",
111 xmlSecErrorsSafeString(name));
112 xmlSecKeyDestroy(key);
113 return(-1);
114 }
115 }
116
117 #ifndef XMLSEC_NO_X509
118 for(files += strlen(files) + 1; (files[0] != '\0'); files += strlen(files) + 1) {
119 ret = xmlSecCryptoAppKeyCertLoad(key, files, format);
120 if(ret < 0) {
121 fprintf(stderr, "Error: xmlSecCryptoAppKeyCertLoad failed: file=%s\n",
122 xmlSecErrorsSafeString(files));
123 xmlSecKeyDestroy(key);
124 return(-1);
125 }
126 }
127 #else /* XMLSEC_NO_X509 */
128 files += strlen(files) + 1;
129 if(files[0] != '\0') {
130 fprintf(stderr, "Error: X509 support is disabled\n");
131 return(-1);
132 }
133 #endif /* XMLSEC_NO_X509 */
134
135 ret = xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key);
136 if(ret < 0) {
137 fprintf(stderr, "Error: xmlSecCryptoAppDefaultKeysMngrAdoptKey failed\n");
138 xmlSecKeyDestroy(key);
139 return(-1);
140 }
141
142 return(0);
143 }
144
145
146 int
xmlSecAppCryptoSimpleKeysMngrPkcs12KeyLoad(xmlSecKeysMngrPtr mngr,const char * filename,const char * pwd,const char * name)147 xmlSecAppCryptoSimpleKeysMngrPkcs12KeyLoad(xmlSecKeysMngrPtr mngr, const char *filename, const char* pwd, const char *name) {
148 xmlSecKeyPtr key;
149 int ret;
150
151 xmlSecAssert2(mngr != NULL, -1);
152 xmlSecAssert2(filename != NULL, -1);
153
154 #ifndef XMLSEC_NO_X509
155 key = xmlSecCryptoAppKeyLoad(filename, xmlSecKeyDataFormatPkcs12, pwd,
156 xmlSecCryptoAppGetDefaultPwdCallback(), (void*)filename);
157 if(key == NULL) {
158 fprintf(stderr, "Error: xmlSecCryptoAppKeyLoad failed: filename=%s\n",
159 xmlSecErrorsSafeString(filename));
160 return(-1);
161 }
162
163 if(name != NULL) {
164 ret = xmlSecKeySetName(key, BAD_CAST name);
165 if(ret < 0) {
166 fprintf(stderr, "Error: xmlSecKeySetName failed: name=%s\n",
167 xmlSecErrorsSafeString(name));
168 xmlSecKeyDestroy(key);
169 return(-1);
170 }
171 }
172
173 ret = xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key);
174 if(ret < 0) {
175 fprintf(stderr, "Error: xmlSecCryptoAppDefaultKeysMngrAdoptKey failed\n");
176 xmlSecKeyDestroy(key);
177 return(-1);
178 }
179
180 return(0);
181 #else /* XMLSEC_NO_X509 */
182 fprintf(stderr, "Error: X509 support is disabled\n");
183 return(-1);
184 #endif /* XMLSEC_NO_X509 */
185 }
186
187 int
xmlSecAppCryptoSimpleKeysMngrBinaryKeyLoad(xmlSecKeysMngrPtr mngr,const char * keyKlass,const char * filename,const char * name)188 xmlSecAppCryptoSimpleKeysMngrBinaryKeyLoad(xmlSecKeysMngrPtr mngr, const char* keyKlass, const char *filename, const char *name) {
189 xmlSecKeyPtr key;
190 xmlSecKeyDataId dataId;
191 int ret;
192
193 xmlSecAssert2(mngr != NULL, -1);
194 xmlSecAssert2(keyKlass != NULL, -1);
195 xmlSecAssert2(filename != NULL, -1);
196
197 /* find requested data */
198 dataId = xmlSecKeyDataIdListFindByName(xmlSecKeyDataIdsGet(), BAD_CAST keyKlass,
199 xmlSecKeyDataUsageAny);
200 if(dataId == xmlSecKeyDataIdUnknown) {
201 fprintf(stderr, "Error: xmlSecKeyDataIdListFindByName failed keyKlass=%s\n",
202 xmlSecErrorsSafeString(keyKlass));
203 return(-1);
204 }
205
206 key = xmlSecKeyReadBinaryFile(dataId, filename);
207 if(key == NULL) {
208 fprintf(stderr, "Error: xmlSecKeyReadBinaryFile failed filename=%s\n",
209 xmlSecErrorsSafeString(filename));
210 return(-1);
211 }
212
213 ret = xmlSecKeySetName(key, BAD_CAST name);
214 if(ret < 0) {
215 fprintf(stderr, "Error: xmlSecKeySetName failed: name=%s\n",
216 xmlSecErrorsSafeString(name));
217 xmlSecKeyDestroy(key);
218 return(-1);
219 }
220
221 /* finally add it to keys manager */
222 ret = xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key);
223 if(ret < 0) {
224 fprintf(stderr, "Error: xmlSecCryptoAppDefaultKeysMngrAdoptKey failed\n");
225 xmlSecKeyDestroy(key);
226 return(-1);
227 }
228
229 return(0);
230 }
231
232
233 int
xmlSecAppCryptoSimpleKeysMngrKeyGenerate(xmlSecKeysMngrPtr mngr,const char * keyKlassAndSize,const char * name)234 xmlSecAppCryptoSimpleKeysMngrKeyGenerate(xmlSecKeysMngrPtr mngr, const char* keyKlassAndSize, const char* name) {
235 xmlSecKeyPtr key;
236 int ret;
237
238 xmlSecAssert2(mngr != NULL, -1);
239 xmlSecAssert2(keyKlassAndSize != NULL, -1);
240
241 key = xmlSecAppCryptoKeyGenerate(keyKlassAndSize, name, xmlSecKeyDataTypePermanent);
242 if(key == NULL) {
243 fprintf(stderr, "Error: xmlSecAppCryptoSimpleKeysMngrKeyGenerate failed: name=%s\n",
244 xmlSecErrorsSafeString(name));
245 return(-1);
246 }
247
248 ret = xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key);
249 if(ret < 0) {
250 fprintf(stderr, "Error: xmlSecCryptoAppDefaultKeysMngrAdoptKey failed\n");
251 xmlSecKeyDestroy(key);
252 return(-1);
253 }
254 return(0);
255 }
256
257 xmlSecKeyPtr
xmlSecAppCryptoKeyGenerate(const char * keyKlassAndSize,const char * name,xmlSecKeyDataType type)258 xmlSecAppCryptoKeyGenerate(const char* keyKlassAndSize, const char* name, xmlSecKeyDataType type) {
259 xmlSecKeyPtr key;
260 char* buf;
261 char* p;
262 int size;
263 int ret;
264
265 xmlSecAssert2(keyKlassAndSize != NULL, NULL);
266
267 buf = (char*) xmlStrdup(BAD_CAST keyKlassAndSize);
268 if(buf == NULL) {
269 fprintf(stderr, "Error: xmlSecStrdupError(keyKlassAndSize) failed\n");
270 return(NULL);
271 }
272
273 /* separate key klass and size */
274 p = strchr(buf, '-');
275 if(p == NULL) {
276 fprintf(stderr, "Error: key size is not specified in the key definition \"%s\"\n",
277 xmlSecErrorsSafeString(buf));
278 xmlFree(buf);
279 return(NULL);
280 }
281 *(p++) = '\0';
282 size = atoi(p);
283
284 key = xmlSecKeyGenerateByName(BAD_CAST buf, size, type);
285 if(key == NULL) {
286 fprintf(stderr, "Error: xmlSecKeyGenerateByName() failed: name=%s;size=%d;type=%d\n",
287 xmlSecErrorsSafeString(buf), size, (int)type);
288 xmlFree(buf);
289 return(NULL);
290 }
291
292 ret = xmlSecKeySetName(key, BAD_CAST name);
293 if(ret < 0) {
294 fprintf(stderr, "Error: xmlSecKeySetName failed: name=%s\n",
295 xmlSecErrorsSafeString(name));
296 xmlSecKeyDestroy(key);
297 xmlFree(buf);
298 return(NULL);
299 }
300
301 xmlFree(buf);
302 return(key);
303 }
304