Git init
[external/xmlsec1.git] / src / gnutls / symkeys.c
1 /** 
2  *
3  * XMLSec library
4  * 
5  * DES Algorithm support
6  * 
7  * This is free software; see Copyright file in the source
8  * distribution for preciese wording.
9  * 
10  * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
11  */
12 #include "globals.h"
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17
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>
24
25 #include <xmlsec/gnutls/crypto.h>
26
27 /*****************************************************************************
28  * 
29  * Symmetic (binary) keys - just a wrapper for xmlSecKeyDataBinary
30  *
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,
37                                                          xmlSecKeyPtr key,
38                                                          xmlNodePtr node,
39                                                          xmlSecKeyInfoCtxPtr keyInfoCtx);
40 static int      xmlSecGnuTLSSymKeyDataXmlWrite          (xmlSecKeyDataId id,
41                                                          xmlSecKeyPtr key,
42                                                          xmlNodePtr node,
43                                                          xmlSecKeyInfoCtxPtr keyInfoCtx);
44 static int      xmlSecGnuTLSSymKeyDataBinRead           (xmlSecKeyDataId id,
45                                                          xmlSecKeyPtr key,
46                                                          const xmlSecByte* buf,
47                                                          xmlSecSize bufSize,
48                                                          xmlSecKeyInfoCtxPtr keyInfoCtx);
49 static int      xmlSecGnuTLSSymKeyDataBinWrite          (xmlSecKeyDataId id,
50                                                          xmlSecKeyPtr key,
51                                                          xmlSecByte** buf,
52                                                          xmlSecSize* bufSize,
53                                                          xmlSecKeyInfoCtxPtr keyInfoCtx);
54 static int      xmlSecGnuTLSSymKeyDataGenerate          (xmlSecKeyDataPtr data,
55                                                          xmlSecSize sizeBits,
56                                                          xmlSecKeyDataType type);
57
58 static xmlSecKeyDataType xmlSecGnuTLSSymKeyDataGetType  (xmlSecKeyDataPtr data);
59 static xmlSecSize        xmlSecGnuTLSSymKeyDataGetSize  (xmlSecKeyDataPtr data);
60 static void     xmlSecGnuTLSSymKeyDataDebugDump         (xmlSecKeyDataPtr data,
61                                                          FILE* output);
62 static void     xmlSecGnuTLSSymKeyDataDebugXmlDump      (xmlSecKeyDataPtr data,
63                                                          FILE* output);
64 static int      xmlSecGnuTLSSymKeyDataKlassCheck        (xmlSecKeyDataKlass* klass);
65
66 #define xmlSecGnuTLSSymKeyDataCheckId(data) \
67     (xmlSecKeyDataIsValid((data)) && \
68      xmlSecGnuTLSSymKeyDataKlassCheck((data)->id))
69
70 static int
71 xmlSecGnuTLSSymKeyDataInitialize(xmlSecKeyDataPtr data) {
72     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), -1);
73     
74     return(xmlSecKeyDataBinaryValueInitialize(data));
75 }
76
77 static int
78 xmlSecGnuTLSSymKeyDataDuplicate(xmlSecKeyDataPtr dst, xmlSecKeyDataPtr src) {
79     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(dst), -1);
80     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(src), -1);
81     xmlSecAssert2(dst->id == src->id, -1);
82         
83     return(xmlSecKeyDataBinaryValueDuplicate(dst, src));
84 }
85
86 static void
87 xmlSecGnuTLSSymKeyDataFinalize(xmlSecKeyDataPtr data) {
88     xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
89     
90     xmlSecKeyDataBinaryValueFinalize(data);
91 }
92
93 static int
94 xmlSecGnuTLSSymKeyDataXmlRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
95                                xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
96     xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
97     
98     return(xmlSecKeyDataBinaryValueXmlRead(id, key, node, keyInfoCtx));
99 }
100
101 static int 
102 xmlSecGnuTLSSymKeyDataXmlWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
103                                     xmlNodePtr node, xmlSecKeyInfoCtxPtr keyInfoCtx) {
104     xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
105     
106     return(xmlSecKeyDataBinaryValueXmlWrite(id, key, node, keyInfoCtx));
107 }
108
109 static int
110 xmlSecGnuTLSSymKeyDataBinRead(xmlSecKeyDataId id, xmlSecKeyPtr key,
111                                     const xmlSecByte* buf, xmlSecSize bufSize,
112                                     xmlSecKeyInfoCtxPtr keyInfoCtx) {
113     xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
114     
115     return(xmlSecKeyDataBinaryValueBinRead(id, key, buf, bufSize, keyInfoCtx));
116 }
117
118 static int
119 xmlSecGnuTLSSymKeyDataBinWrite(xmlSecKeyDataId id, xmlSecKeyPtr key,
120                                     xmlSecByte** buf, xmlSecSize* bufSize,
121                                     xmlSecKeyInfoCtxPtr keyInfoCtx) {
122     xmlSecAssert2(xmlSecGnuTLSSymKeyDataKlassCheck(id), -1);
123     
124     return(xmlSecKeyDataBinaryValueBinWrite(id, key, buf, bufSize, keyInfoCtx));
125 }
126
127 static int
128 xmlSecGnuTLSSymKeyDataGenerate(xmlSecKeyDataPtr data, xmlSecSize sizeBits, xmlSecKeyDataType type ATTRIBUTE_UNUSED) {
129     xmlSecBufferPtr buffer;
130
131     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), -1);
132     xmlSecAssert2(sizeBits > 0, -1);
133
134     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
135     xmlSecAssert2(buffer != NULL, -1);
136     
137     return(xmlSecGnuTLSGenerateRandom(buffer, (sizeBits + 7) / 8));
138 }
139
140 static xmlSecKeyDataType
141 xmlSecGnuTLSSymKeyDataGetType(xmlSecKeyDataPtr data) {
142     xmlSecBufferPtr buffer;
143
144     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), xmlSecKeyDataTypeUnknown);
145
146     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
147     xmlSecAssert2(buffer != NULL, xmlSecKeyDataTypeUnknown);
148
149     return((xmlSecBufferGetSize(buffer) > 0) ? xmlSecKeyDataTypeSymmetric : xmlSecKeyDataTypeUnknown);
150 }
151
152 static xmlSecSize 
153 xmlSecGnuTLSSymKeyDataGetSize(xmlSecKeyDataPtr data) {
154     xmlSecAssert2(xmlSecGnuTLSSymKeyDataCheckId(data), 0);
155     
156     return(xmlSecKeyDataBinaryValueGetSize(data));
157 }
158
159 static void 
160 xmlSecGnuTLSSymKeyDataDebugDump(xmlSecKeyDataPtr data, FILE* output) {
161     xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
162     
163     xmlSecKeyDataBinaryValueDebugDump(data, output);    
164 }
165
166 static void
167 xmlSecGnuTLSSymKeyDataDebugXmlDump(xmlSecKeyDataPtr data, FILE* output) {
168     xmlSecAssert(xmlSecGnuTLSSymKeyDataCheckId(data));
169     
170     xmlSecKeyDataBinaryValueDebugXmlDump(data, output);    
171 }
172
173 static int 
174 xmlSecGnuTLSSymKeyDataKlassCheck(xmlSecKeyDataKlass* klass) {    
175 #ifndef XMLSEC_NO_DES
176     if(klass == xmlSecGnuTLSKeyDataDesId) {
177         return(1);
178     }
179 #endif /* XMLSEC_NO_DES */
180
181 #ifndef XMLSEC_NO_AES
182     if(klass == xmlSecGnuTLSKeyDataAesId) {
183         return(1);
184     }
185 #endif /* XMLSEC_NO_AES */
186
187 #ifndef XMLSEC_NO_HMAC
188     if(klass == xmlSecGnuTLSKeyDataHmacId) {
189         return(1);
190     }
191 #endif /* XMLSEC_NO_HMAC */
192
193     return(0);
194 }
195
196 #ifndef XMLSEC_NO_AES
197 /**************************************************************************
198  *
199  * <xmlsec:AESKeyValue> processing
200  *
201  *************************************************************************/
202 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataAesKlass = {
203     sizeof(xmlSecKeyDataKlass),
204     xmlSecKeyDataBinarySize,
205
206     /* data */
207     xmlSecNameAESKeyValue,
208     xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml, 
209                                                 /* xmlSecKeyDataUsage usage; */
210     xmlSecHrefAESKeyValue,                      /* const xmlChar* href; */
211     xmlSecNodeAESKeyValue,                      /* const xmlChar* dataNodeName; */
212     xmlSecNs,                                   /* const xmlChar* dataNodeNs; */
213     
214     /* constructors/destructor */
215     xmlSecGnuTLSSymKeyDataInitialize,           /* xmlSecKeyDataInitializeMethod initialize; */
216     xmlSecGnuTLSSymKeyDataDuplicate,            /* xmlSecKeyDataDuplicateMethod duplicate; */
217     xmlSecGnuTLSSymKeyDataFinalize,             /* xmlSecKeyDataFinalizeMethod finalize; */
218     xmlSecGnuTLSSymKeyDataGenerate,             /* xmlSecKeyDataGenerateMethod generate; */
219     
220     /* get info */
221     xmlSecGnuTLSSymKeyDataGetType,              /* xmlSecKeyDataGetTypeMethod getType; */
222     xmlSecGnuTLSSymKeyDataGetSize,              /* xmlSecKeyDataGetSizeMethod getSize; */
223     NULL,                                       /* xmlSecKeyDataGetIdentifier getIdentifier; */
224
225     /* read/write */
226     xmlSecGnuTLSSymKeyDataXmlRead,              /* xmlSecKeyDataXmlReadMethod xmlRead; */
227     xmlSecGnuTLSSymKeyDataXmlWrite,             /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
228     xmlSecGnuTLSSymKeyDataBinRead,              /* xmlSecKeyDataBinReadMethod binRead; */
229     xmlSecGnuTLSSymKeyDataBinWrite,             /* xmlSecKeyDataBinWriteMethod binWrite; */
230
231     /* debug */
232     xmlSecGnuTLSSymKeyDataDebugDump,            /* xmlSecKeyDataDebugDumpMethod debugDump; */
233     xmlSecGnuTLSSymKeyDataDebugXmlDump,         /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
234
235     /* reserved for the future */
236     NULL,                                       /* void* reserved0; */
237     NULL,                                       /* void* reserved1; */
238 };
239
240 /** 
241  * xmlSecGnuTLSKeyDataAesGetKlass:
242  * 
243  * The AES key data klass.
244  *
245  * Returns: AES key data klass.
246  */
247 xmlSecKeyDataId 
248 xmlSecGnuTLSKeyDataAesGetKlass(void) {
249     return(&xmlSecGnuTLSKeyDataAesKlass);
250 }
251
252 /**
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).
257  *
258  * Sets the value of AES key data.
259  *
260  * Returns: 0 on success or a negative value if an error occurs.
261  */
262 int
263 xmlSecGnuTLSKeyDataAesSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
264     xmlSecBufferPtr buffer;
265     
266     xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataAesId), -1);
267     xmlSecAssert2(buf != NULL, -1);
268     xmlSecAssert2(bufSize > 0, -1);
269     
270     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
271     xmlSecAssert2(buffer != NULL, -1);
272     
273     return(xmlSecBufferSetData(buffer, buf, bufSize));
274 }
275 #endif /* XMLSEC_NO_AES */
276
277 #ifndef XMLSEC_NO_DES
278 /**************************************************************************
279  *
280  * <xmlsec:DESKeyValue> processing
281  *
282  *************************************************************************/
283 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataDesKlass = {
284     sizeof(xmlSecKeyDataKlass),
285     xmlSecKeyDataBinarySize,
286
287     /* data */
288     xmlSecNameDESKeyValue,
289     xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml, 
290                                                 /* xmlSecKeyDataUsage usage; */
291     xmlSecHrefDESKeyValue,                      /* const xmlChar* href; */
292     xmlSecNodeDESKeyValue,                      /* const xmlChar* dataNodeName; */
293     xmlSecNs,                                   /* const xmlChar* dataNodeNs; */
294     
295     /* constructors/destructor */
296     xmlSecGnuTLSSymKeyDataInitialize,           /* xmlSecKeyDataInitializeMethod initialize; */
297     xmlSecGnuTLSSymKeyDataDuplicate,            /* xmlSecKeyDataDuplicateMethod duplicate; */
298     xmlSecGnuTLSSymKeyDataFinalize,             /* xmlSecKeyDataFinalizeMethod finalize; */
299     xmlSecGnuTLSSymKeyDataGenerate,             /* xmlSecKeyDataGenerateMethod generate; */
300     
301     /* get info */
302     xmlSecGnuTLSSymKeyDataGetType,              /* xmlSecKeyDataGetTypeMethod getType; */
303     xmlSecGnuTLSSymKeyDataGetSize,              /* xmlSecKeyDataGetSizeMethod getSize; */
304     NULL,                                       /* xmlSecKeyDataGetIdentifier getIdentifier; */
305
306     /* read/write */
307     xmlSecGnuTLSSymKeyDataXmlRead,              /* xmlSecKeyDataXmlReadMethod xmlRead; */
308     xmlSecGnuTLSSymKeyDataXmlWrite,             /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
309     xmlSecGnuTLSSymKeyDataBinRead,              /* xmlSecKeyDataBinReadMethod binRead; */
310     xmlSecGnuTLSSymKeyDataBinWrite,             /* xmlSecKeyDataBinWriteMethod binWrite; */
311
312     /* debug */
313     xmlSecGnuTLSSymKeyDataDebugDump,            /* xmlSecKeyDataDebugDumpMethod debugDump; */
314     xmlSecGnuTLSSymKeyDataDebugXmlDump,         /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
315
316     /* reserved for the future */
317     NULL,                                       /* void* reserved0; */
318     NULL,                                       /* void* reserved1; */
319 };
320
321 /** 
322  * xmlSecGnuTLSKeyDataDesGetKlass:
323  * 
324  * The DES key data klass.
325  *
326  * Returns: DES key data klass.
327  */
328 xmlSecKeyDataId 
329 xmlSecGnuTLSKeyDataDesGetKlass(void) {
330     return(&xmlSecGnuTLSKeyDataDesKlass);
331 }
332
333 /**
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).
338  *
339  * Sets the value of DES key data.
340  *
341  * Returns: 0 on success or a negative value if an error occurs.
342  */
343 int
344 xmlSecGnuTLSKeyDataDesSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
345     xmlSecBufferPtr buffer;
346     
347     xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataDesId), -1);
348     xmlSecAssert2(buf != NULL, -1);
349     xmlSecAssert2(bufSize > 0, -1);
350     
351     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
352     xmlSecAssert2(buffer != NULL, -1);
353     
354     return(xmlSecBufferSetData(buffer, buf, bufSize));
355 }
356
357 #endif /* XMLSEC_NO_DES */
358
359 #ifndef XMLSEC_NO_HMAC
360 /**************************************************************************
361  *
362  * <xmlsec:HMACKeyValue> processing
363  *
364  *************************************************************************/
365 static xmlSecKeyDataKlass xmlSecGnuTLSKeyDataHmacKlass = {
366     sizeof(xmlSecKeyDataKlass),
367     xmlSecKeyDataBinarySize,
368
369     /* data */
370     xmlSecNameHMACKeyValue,
371     xmlSecKeyDataUsageKeyValueNode | xmlSecKeyDataUsageRetrievalMethodNodeXml, 
372                                                 /* xmlSecKeyDataUsage usage; */
373     xmlSecHrefHMACKeyValue,                     /* const xmlChar* href; */
374     xmlSecNodeHMACKeyValue,                     /* const xmlChar* dataNodeName; */
375     xmlSecNs,                                   /* const xmlChar* dataNodeNs; */
376     
377     /* constructors/destructor */
378     xmlSecGnuTLSSymKeyDataInitialize,           /* xmlSecKeyDataInitializeMethod initialize; */
379     xmlSecGnuTLSSymKeyDataDuplicate,            /* xmlSecKeyDataDuplicateMethod duplicate; */
380     xmlSecGnuTLSSymKeyDataFinalize,             /* xmlSecKeyDataFinalizeMethod finalize; */
381     xmlSecGnuTLSSymKeyDataGenerate,             /* xmlSecKeyDataGenerateMethod generate; */
382     
383     /* get info */
384     xmlSecGnuTLSSymKeyDataGetType,              /* xmlSecKeyDataGetTypeMethod getType; */
385     xmlSecGnuTLSSymKeyDataGetSize,              /* xmlSecKeyDataGetSizeMethod getSize; */
386     NULL,                                       /* xmlSecKeyDataGetIdentifier getIdentifier; */
387
388     /* read/write */
389     xmlSecGnuTLSSymKeyDataXmlRead,              /* xmlSecKeyDataXmlReadMethod xmlRead; */
390     xmlSecGnuTLSSymKeyDataXmlWrite,             /* xmlSecKeyDataXmlWriteMethod xmlWrite; */
391     xmlSecGnuTLSSymKeyDataBinRead,              /* xmlSecKeyDataBinReadMethod binRead; */
392     xmlSecGnuTLSSymKeyDataBinWrite,             /* xmlSecKeyDataBinWriteMethod binWrite; */
393
394     /* debug */
395     xmlSecGnuTLSSymKeyDataDebugDump,            /* xmlSecKeyDataDebugDumpMethod debugDump; */
396     xmlSecGnuTLSSymKeyDataDebugXmlDump,         /* xmlSecKeyDataDebugDumpMethod debugXmlDump; */
397
398     /* reserved for the future */
399     NULL,                                       /* void* reserved0; */
400     NULL,                                       /* void* reserved1; */
401 };
402
403 /** 
404  * xmlSecGnuTLSKeyDataHmacGetKlass:
405  * 
406  * The HMAC key data klass.
407  *
408  * Returns: HMAC key data klass.
409  */
410 xmlSecKeyDataId 
411 xmlSecGnuTLSKeyDataHmacGetKlass(void) {
412     return(&xmlSecGnuTLSKeyDataHmacKlass);
413 }
414
415 /**
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).
420  *
421  * Sets the value of HMAC key data.
422  *
423  * Returns: 0 on success or a negative value if an error occurs.
424  */
425 int
426 xmlSecGnuTLSKeyDataHmacSet(xmlSecKeyDataPtr data, const xmlSecByte* buf, xmlSecSize bufSize) {
427     xmlSecBufferPtr buffer;
428     
429     xmlSecAssert2(xmlSecKeyDataCheckId(data, xmlSecGnuTLSKeyDataHmacId), -1);
430     xmlSecAssert2(buf != NULL, -1);
431     xmlSecAssert2(bufSize > 0, -1);
432     
433     buffer = xmlSecKeyDataBinaryValueGetBuffer(data);
434     xmlSecAssert2(buffer != NULL, -1);
435     
436     return(xmlSecBufferSetData(buffer, buf, bufSize));
437 }
438
439 #endif /* XMLSEC_NO_HMAC */
440