5 * DES Algorithm support
7 * This is free software; see Copyright file in the source
8 * distribution for preciese wording.
10 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
18 #include <xmlsec/xmlsec.h>
19 #include <xmlsec/xmltree.h>
20 #include <xmlsec/keys.h>
21 #include <xmlsec/keyinfo.h>
22 #include <xmlsec/transforms.h>
23 #include <xmlsec/errors.h>
25 #include <xmlsec/gnutls/crypto.h>
27 /*****************************************************************************
29 * Symmetic (binary) keys - just a wrapper for xmlSecKeyDataBinary
31 ****************************************************************************/
32 static int xmlSecGnuTLSSymKeyDataInitialize (xmlSecKeyDataPtr data);
33 static int xmlSecGnuTLSSymKeyDataDuplicate (xmlSecKeyDataPtr dst,
34 xmlSecKeyDataPtr src);
35 static void xmlSecGnuTLSSymKeyDataFinalize (xmlSecKeyDataPtr data);
36 static int xmlSecGnuTLSSymKeyDataXmlRead (xmlSecKeyDataId id,
39 xmlSecKeyInfoCtxPtr keyInfoCtx);
40 static int xmlSecGnuTLSSymKeyDataXmlWrite (xmlSecKeyDataId id,
43 xmlSecKeyInfoCtxPtr keyInfoCtx);
44 static int xmlSecGnuTLSSymKeyDataBinRead (xmlSecKeyDataId id,
46 const xmlSecByte* buf,
48 xmlSecKeyInfoCtxPtr keyInfoCtx);
49 static int xmlSecGnuTLSSymKeyDataBinWrite (xmlSecKeyDataId id,
53 xmlSecKeyInfoCtxPtr keyInfoCtx);
54 static int xmlSecGnuTLSSymKeyDataGenerate (xmlSecKeyDataPtr data,
56 xmlSecKeyDataType type);
58 static xmlSecKeyDataType xmlSecGnuTLSSymKeyDataGetType (xmlSecKeyDataPtr data);
59 static xmlSecSize xmlSecGnuTLSSymKeyDataGetSize (xmlSecKeyDataPtr data);
60 static void xmlSecGnuTLSSymKeyDataDebugDump (xmlSecKeyDataPtr data,
62 static void xmlSecGnuTLSSymKeyDataDebugXmlDump (xmlSecKeyDataPtr data,
64 static int xmlSecGnuTLSSymKeyDataKlassCheck (xmlSecKeyDataKlass* klass);
66 #define xmlSecGnuTLSSymKeyDataCheckId(data) \
67 (xmlSecKeyDataIsValid((data)) && \
68 xmlSecGnuTLSSymKeyDataKlassCheck((data)->id))
71 xmlSecGnuTLSSymKeyDataInitialize(xmlSecKeyDataPtr data) {
72 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), -1);
74 return(xmlSecKeyDataBinaryValueInitialize(data));
78 xmlSecGnuTLSSymKeyDataDuplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) {
79 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(dst), -1);
80 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(src), -1);
81 xmlSecAssert2(dst->id == src->id, -1);
83 return(xmlSecKeyDataBinaryValueDuplicate(dst, src));
87 xmlSecGnuTLSSymKeyDataFinalize(xmlSecKeyDataPtr data) {
88 xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
90 xmlSecKeyDataBinaryValueFinalize(data);
94 xmlSecGnuTLSSymKeyDataXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
95 xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
96 xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
98 return(xmlSecKeyDataBinaryValueXmlRead(id, key, node, keyInfoCtx));
102 xmlSecGnuTLSSymKeyDataXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
103 xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
104 xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
106 return(xmlSecKeyDataBinaryValueXmlWrite(id, key, node, keyInfoCtx));
110 xmlSecGnuTLSSymKeyDataBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
111 const xmlSecByte* buf, xmlSecSize bufSize,
112 xmlSecKeyInfoCtxPtr keyInfoCtx) {
113 xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
115 return(xmlSecKeyDataBinaryValueBinRead(id, key, buf, bufSize, keyInfoCtx));
119 xmlSecGnuTLSSymKeyDataBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
120 xmlSecByte** buf, xmlSecSize* bufSize,
121 xmlSecKeyInfoCtxPtr keyInfoCtx) {
122 xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
124 return(xmlSecKeyDataBinaryValueBinWrite(id, key, buf, bufSize, keyInfoCtx));
128 xmlSecGnuTLSSymKeyDataGenerate(xmlSecKeyDataPtr data, xmlSecSize sizeBits, xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
129 xmlSecBufferPtr buffer;
131 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), -1);
132 xmlSecAssert2(sizeBits > 0, -1);
134 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
135 xmlSecAssert2(buffer != NULL, -1);
137 return(xmlSecGnuTLSGenerateRandom(buffer, (sizeBits + 7) / 8));
140 static xmlSecKeyDataType
141 xmlSecGnuTLSSymKeyDataGetType(xmlSecKeyDataPtr data) {
142 xmlSecBufferPtr buffer;
144 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), xmlSecKeyDataTypeUnknown);
146 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
147 xmlSecAssert2(buffer != NULL, xmlSecKeyDataTypeUnknown);
149 return((xmlSecBufferGetSize(buffer) > 0) ? xmlSecKeyDataTypeSymmetric : xmlSecKeyDataTypeUnknown);
153 xmlSecGnuTLSSymKeyDataGetSize(xmlSecKeyDataPtr data) {
154 xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), 0);
156 return(xmlSecKeyDataBinaryValueGetSize(data));
160 xmlSecGnuTLSSymKeyDataDebugDump(xmlSecKeyDataPtr data, FILE* output) {
161 xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
163 xmlSecKeyDataBinaryValueDebugDump(data, output);
167 xmlSecGnuTLSSymKeyDataDebugXmlDump(xmlSecKeyDataPtr data, FILE* output) {
168 xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
170 xmlSecKeyDataBinaryValueDebugXmlDump(data, output);
174 xmlSecGnuTLSSymKeyDataKlassCheck(xmlSecKeyDataKlass* klass) {
175 #ifndef XMLSEC_NO_DES
176 if(klass == xmlSecGnuTLSKeyDataDesId) {
179 #endif /* XMLSEC_NO_DES */
181 #ifndef XMLSEC_NO_AES
182 if(klass == xmlSecGnuTLSKeyDataAesId) {
185 #endif /* XMLSEC_NO_AES */
187 #ifndef XMLSEC_NO_HMAC
188 if(klass == xmlSecGnuTLSKeyDataHmacId) {
191 #endif /* XMLSEC_NO_HMAC */
196 #ifndef XMLSEC_NO_AES
197 /**************************************************************************
199 * <xmlsec:AESKeyValue> processing
201 *************************************************************************/
202 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataAesKlass = {
203 sizeof(xmlSecKeyDataKlass),
204 xmlSecKeyDataBinarySize,
207 xmlSecNameAESKeyValue,
208 xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
209 /* xmlSecKeyDataUsage usage; */
210 xmlSecHrefAESKeyValue, /* const xmlChar* href; */
211 xmlSecNodeAESKeyValue, /* const xmlChar* dataNodeName; */
212 xmlSecNs, /* const xmlChar* dataNodeNs; */
214 /* constructors/destructor */
215 xmlSecGnuTLSSymKeyDataInitialize, /* xmlSecKeyDataInitializeMethod initialize; */
216 xmlSecGnuTLSSymKeyDataDuplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
217 xmlSecGnuTLSSymKeyDataFinalize, /* xmlSecKeyDataFinalizeMethod finalize; */
218 xmlSecGnuTLSSymKeyDataGenerate, /* xmlSecKeyDataGenerateMethod generate; */
221 xmlSecGnuTLSSymKeyDataGetType, /* xmlSecKeyDataGetTypeMethod getType; */
222 xmlSecGnuTLSSymKeyDataGetSize, /* xmlSecKeyDataGetSizeMethod getSize; */
223 NULL, /* xmlSecKeyDataGetIdentifier getIdentifier; */
226 xmlSecGnuTLSSymKeyDataXmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
227 xmlSecGnuTLSSymKeyDataXmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
228 xmlSecGnuTLSSymKeyDataBinRead, /* xmlSecKeyDataBinReadMethod binRead; */
229 xmlSecGnuTLSSymKeyDataBinWrite, /* xmlSecKeyDataBinWriteMethod binWrite; */
232 xmlSecGnuTLSSymKeyDataDebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
233 xmlSecGnuTLSSymKeyDataDebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
235 /* reserved for the future */
236 NULL, /* void* reserved0; */
237 NULL, /* void* reserved1; */
241 * xmlSecGnuTLSKeyDataAesGetKlass:
243 * The AES key data klass.
245 * Returns: AES key data klass.
248 xmlSecGnuTLSKeyDataAesGetKlass(void) {
249 return(&xmlSecGnuTLSKeyDataAesKlass);
253 * xmlSecGnuTLSKeyDataAesSet:
254 * @data: the pointer to AES key data.
255 * @buf: the pointer to key value.
256 * @bufSize: the key value size (in bytes).
258 * Sets the value of AES key data.
260 * Returns: 0 on success or a negative value if an error occurs.
263 xmlSecGnuTLSKeyDataAesSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
264 xmlSecBufferPtr buffer;
266 xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataAesId), -1);
267 xmlSecAssert2(buf != NULL, -1);
268 xmlSecAssert2(bufSize > 0, -1);
270 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
271 xmlSecAssert2(buffer != NULL, -1);
273 return(xmlSecBufferSetData(buffer, buf, bufSize));
275 #endif /* XMLSEC_NO_AES */
277 #ifndef XMLSEC_NO_DES
278 /**************************************************************************
280 * <xmlsec:DESKeyValue> processing
282 *************************************************************************/
283 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataDesKlass = {
284 sizeof(xmlSecKeyDataKlass),
285 xmlSecKeyDataBinarySize,
288 xmlSecNameDESKeyValue,
289 xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
290 /* xmlSecKeyDataUsage usage; */
291 xmlSecHrefDESKeyValue, /* const xmlChar* href; */
292 xmlSecNodeDESKeyValue, /* const xmlChar* dataNodeName; */
293 xmlSecNs, /* const xmlChar* dataNodeNs; */
295 /* constructors/destructor */
296 xmlSecGnuTLSSymKeyDataInitialize, /* xmlSecKeyDataInitializeMethod initialize; */
297 xmlSecGnuTLSSymKeyDataDuplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
298 xmlSecGnuTLSSymKeyDataFinalize, /* xmlSecKeyDataFinalizeMethod finalize; */
299 xmlSecGnuTLSSymKeyDataGenerate, /* xmlSecKeyDataGenerateMethod generate; */
302 xmlSecGnuTLSSymKeyDataGetType, /* xmlSecKeyDataGetTypeMethod getType; */
303 xmlSecGnuTLSSymKeyDataGetSize, /* xmlSecKeyDataGetSizeMethod getSize; */
304 NULL, /* xmlSecKeyDataGetIdentifier getIdentifier; */
307 xmlSecGnuTLSSymKeyDataXmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
308 xmlSecGnuTLSSymKeyDataXmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
309 xmlSecGnuTLSSymKeyDataBinRead, /* xmlSecKeyDataBinReadMethod binRead; */
310 xmlSecGnuTLSSymKeyDataBinWrite, /* xmlSecKeyDataBinWriteMethod binWrite; */
313 xmlSecGnuTLSSymKeyDataDebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
314 xmlSecGnuTLSSymKeyDataDebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
316 /* reserved for the future */
317 NULL, /* void* reserved0; */
318 NULL, /* void* reserved1; */
322 * xmlSecGnuTLSKeyDataDesGetKlass:
324 * The DES key data klass.
326 * Returns: DES key data klass.
329 xmlSecGnuTLSKeyDataDesGetKlass(void) {
330 return(&xmlSecGnuTLSKeyDataDesKlass);
334 * xmlSecGnuTLSKeyDataDesSet:
335 * @data: the pointer to DES key data.
336 * @buf: the pointer to key value.
337 * @bufSize: the key value size (in bytes).
339 * Sets the value of DES key data.
341 * Returns: 0 on success or a negative value if an error occurs.
344 xmlSecGnuTLSKeyDataDesSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
345 xmlSecBufferPtr buffer;
347 xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataDesId), -1);
348 xmlSecAssert2(buf != NULL, -1);
349 xmlSecAssert2(bufSize > 0, -1);
351 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
352 xmlSecAssert2(buffer != NULL, -1);
354 return(xmlSecBufferSetData(buffer, buf, bufSize));
357 #endif /* XMLSEC_NO_DES */
359 #ifndef XMLSEC_NO_HMAC
360 /**************************************************************************
362 * <xmlsec:HMACKeyValue> processing
364 *************************************************************************/
365 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataHmacKlass = {
366 sizeof(xmlSecKeyDataKlass),
367 xmlSecKeyDataBinarySize,
370 xmlSecNameHMACKeyValue,
371 xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml,
372 /* xmlSecKeyDataUsage usage; */
373 xmlSecHrefHMACKeyValue, /* const xmlChar* href; */
374 xmlSecNodeHMACKeyValue, /* const xmlChar* dataNodeName; */
375 xmlSecNs, /* const xmlChar* dataNodeNs; */
377 /* constructors/destructor */
378 xmlSecGnuTLSSymKeyDataInitialize, /* xmlSecKeyDataInitializeMethod initialize; */
379 xmlSecGnuTLSSymKeyDataDuplicate, /* xmlSecKeyDataDuplicateMethod duplicate; */
380 xmlSecGnuTLSSymKeyDataFinalize, /* xmlSecKeyDataFinalizeMethod finalize; */
381 xmlSecGnuTLSSymKeyDataGenerate, /* xmlSecKeyDataGenerateMethod generate; */
384 xmlSecGnuTLSSymKeyDataGetType, /* xmlSecKeyDataGetTypeMethod getType; */
385 xmlSecGnuTLSSymKeyDataGetSize, /* xmlSecKeyDataGetSizeMethod getSize; */
386 NULL, /* xmlSecKeyDataGetIdentifier getIdentifier; */
389 xmlSecGnuTLSSymKeyDataXmlRead, /* xmlSecKeyDataXmlReadMethod xmlRead; */
390 xmlSecGnuTLSSymKeyDataXmlWrite, /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
391 xmlSecGnuTLSSymKeyDataBinRead, /* xmlSecKeyDataBinReadMethod binRead; */
392 xmlSecGnuTLSSymKeyDataBinWrite, /* xmlSecKeyDataBinWriteMethod binWrite; */
395 xmlSecGnuTLSSymKeyDataDebugDump, /* xmlSecKeyDataDebugDumpMethod debugDump; */
396 xmlSecGnuTLSSymKeyDataDebugXmlDump, /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
398 /* reserved for the future */
399 NULL, /* void* reserved0; */
400 NULL, /* void* reserved1; */
404 * xmlSecGnuTLSKeyDataHmacGetKlass:
406 * The HMAC key data klass.
408 * Returns: HMAC key data klass.
411 xmlSecGnuTLSKeyDataHmacGetKlass(void) {
412 return(&xmlSecGnuTLSKeyDataHmacKlass);
416 * xmlSecGnuTLSKeyDataHmacSet:
417 * @data: the pointer to HMAC key data.
418 * @buf: the pointer to key value.
419 * @bufSize: the key value size (in bytes).
421 * Sets the value of HMAC key data.
423 * Returns: 0 on success or a negative value if an error occurs.
426 xmlSecGnuTLSKeyDataHmacSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
427 xmlSecBufferPtr buffer;
429 xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataHmacId), -1);
430 xmlSecAssert2(buf != NULL, -1);
431 xmlSecAssert2(bufSize > 0, -1);
433 buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
434 xmlSecAssert2(buffer != NULL, -1);
436 return(xmlSecBufferSetData(buffer, buf, bufSize));
439 #endif /* XMLSEC_NO_HMAC */