Git init
[external/xmlsec1.git] / src / gnutls / crypto.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 #include <gnutls/gnutls.h>
14 #include <gcrypt.h>
15
16 #include <xmlsec/xmlsec.h>
17 #include <xmlsec/keys.h>
18 #include <xmlsec/transforms.h>
19 #include <xmlsec/errors.h>
20 #include <xmlsec/dl.h>
21 #include <xmlsec/private.h>
22
23 #include <xmlsec/gnutls/app.h>
24 #include <xmlsec/gnutls/crypto.h>
25
26 static xmlSecCryptoDLFunctionsPtr gXmlSecGnuTLSFunctions = NULL;
27
28 /**
29  * xmlSecCryptoGetFunctions_gnutls:
30  *
31  * Gets the pointer to xmlsec-gnutls functions table.
32  *
33  * Returns: the xmlsec-gnutls functions table or NULL if an error occurs.
34  */
35 xmlSecCryptoDLFunctionsPtr
36 xmlSecCryptoGetFunctions_gnutls(void) {
37     static xmlSecCryptoDLFunctions functions;
38     
39     if(gXmlSecGnuTLSFunctions != NULL) {
40         return(gXmlSecGnuTLSFunctions);
41     }
42
43     memset(&functions, 0, sizeof(functions));
44     gXmlSecGnuTLSFunctions = &functions;
45
46     /**  
47      * Crypto Init/shutdown
48      */
49     gXmlSecGnuTLSFunctions->cryptoInit                  = xmlSecGnuTLSInit;
50     gXmlSecGnuTLSFunctions->cryptoShutdown              = xmlSecGnuTLSShutdown;
51     gXmlSecGnuTLSFunctions->cryptoKeysMngrInit          = xmlSecGnuTLSKeysMngrInit;
52
53     /**
54      * Key data ids
55      */
56 #ifndef XMLSEC_NO_AES    
57     gXmlSecGnuTLSFunctions->keyDataAesGetKlass          = xmlSecGnuTLSKeyDataAesGetKlass;
58 #endif /* XMLSEC_NO_AES */
59
60 #ifndef XMLSEC_NO_DES    
61     gXmlSecGnuTLSFunctions->keyDataDesGetKlass          = xmlSecGnuTLSKeyDataDesGetKlass;
62 #endif /* XMLSEC_NO_DES */
63
64 #ifndef XMLSEC_NO_HMAC  
65     gXmlSecGnuTLSFunctions->keyDataHmacGetKlass         = xmlSecGnuTLSKeyDataHmacGetKlass;
66 #endif /* XMLSEC_NO_HMAC */    
67
68     /**
69      * Key data store ids
70      */
71
72     /**
73      * Crypto transforms ids
74      */
75 #ifndef XMLSEC_NO_AES    
76     gXmlSecGnuTLSFunctions->transformAes128CbcGetKlass          = xmlSecGnuTLSTransformAes128CbcGetKlass;
77     gXmlSecGnuTLSFunctions->transformAes192CbcGetKlass          = xmlSecGnuTLSTransformAes192CbcGetKlass;
78     gXmlSecGnuTLSFunctions->transformAes256CbcGetKlass          = xmlSecGnuTLSTransformAes256CbcGetKlass;
79 #endif /* XMLSEC_NO_AES */
80
81 #ifndef XMLSEC_NO_DES    
82     gXmlSecGnuTLSFunctions->transformDes3CbcGetKlass            = xmlSecGnuTLSTransformDes3CbcGetKlass;
83 #endif /* XMLSEC_NO_DES */
84
85 #ifndef XMLSEC_NO_HMAC
86     gXmlSecGnuTLSFunctions->transformHmacSha1GetKlass           = xmlSecGnuTLSTransformHmacSha1GetKlass;
87     gXmlSecGnuTLSFunctions->transformHmacRipemd160GetKlass      = xmlSecGnuTLSTransformHmacRipemd160GetKlass;
88     gXmlSecGnuTLSFunctions->transformHmacMd5GetKlass            = xmlSecGnuTLSTransformHmacMd5GetKlass;
89 #endif /* XMLSEC_NO_HMAC */
90
91 #ifndef XMLSEC_NO_SHA1    
92     gXmlSecGnuTLSFunctions->transformSha1GetKlass               = xmlSecGnuTLSTransformSha1GetKlass;
93 #endif /* XMLSEC_NO_SHA1 */
94
95     /**
96      * High level routines form xmlsec command line utility
97      */ 
98     gXmlSecGnuTLSFunctions->cryptoAppInit                       = xmlSecGnuTLSAppInit;
99     gXmlSecGnuTLSFunctions->cryptoAppShutdown                   = xmlSecGnuTLSAppShutdown;
100     gXmlSecGnuTLSFunctions->cryptoAppDefaultKeysMngrInit        = xmlSecGnuTLSAppDefaultKeysMngrInit;
101     gXmlSecGnuTLSFunctions->cryptoAppDefaultKeysMngrAdoptKey    = xmlSecGnuTLSAppDefaultKeysMngrAdoptKey;
102     gXmlSecGnuTLSFunctions->cryptoAppDefaultKeysMngrLoad        = xmlSecGnuTLSAppDefaultKeysMngrLoad;
103     gXmlSecGnuTLSFunctions->cryptoAppDefaultKeysMngrSave        = xmlSecGnuTLSAppDefaultKeysMngrSave;
104 #ifndef XMLSEC_NO_X509
105     gXmlSecGnuTLSFunctions->cryptoAppKeysMngrCertLoad           = xmlSecGnuTLSAppKeysMngrCertLoad;
106     gXmlSecGnuTLSFunctions->cryptoAppPkcs12Load                 = xmlSecGnuTLSAppPkcs12Load; 
107     gXmlSecGnuTLSFunctions->cryptoAppKeyCertLoad                = xmlSecGnuTLSAppKeyCertLoad;
108 #endif /* XMLSEC_NO_X509 */
109     gXmlSecGnuTLSFunctions->cryptoAppKeyLoad                    = xmlSecGnuTLSAppKeyLoad; 
110     gXmlSecGnuTLSFunctions->cryptoAppDefaultPwdCallback         = (void*)xmlSecGnuTLSAppGetDefaultPwdCallback();
111
112     return(gXmlSecGnuTLSFunctions);
113 }
114
115
116 /**
117  * xmlSecGnuTLSInit:
118  * 
119  * XMLSec library specific crypto engine initialization. 
120  *
121  * Returns: 0 on success or a negative value otherwise.
122  */
123 int 
124 xmlSecGnuTLSInit (void)  {
125     /* Check loaded xmlsec library version */
126     if(xmlSecCheckVersionExact() != 1) {
127         xmlSecError(XMLSEC_ERRORS_HERE,
128                     NULL,
129                     "xmlSecCheckVersionExact",
130                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
131                     XMLSEC_ERRORS_NO_MESSAGE);
132         return(-1);
133     }
134
135     /* register our klasses */
136     if(xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms(xmlSecCryptoGetFunctions_gnutls()) < 0) {
137         xmlSecError(XMLSEC_ERRORS_HERE,
138                     NULL,
139                     "xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms",
140                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
141                     XMLSEC_ERRORS_NO_MESSAGE);
142         return(-1);
143     }
144     
145     return(0);
146 }
147
148 /**
149  * xmlSecGnuTLSShutdown:
150  * 
151  * XMLSec library specific crypto engine shutdown. 
152  *
153  * Returns: 0 on success or a negative value otherwise.
154  */
155 int 
156 xmlSecGnuTLSShutdown(void) {
157     return(0);
158 }
159
160 /**
161  * xmlSecGnuTLSKeysMngrInit:
162  * @mngr:               the pointer to keys manager.
163  *
164  * Adds GnuTLS specific key data stores in keys manager.
165  *
166  * Returns: 0 on success or a negative value otherwise.
167  */
168 int
169 xmlSecGnuTLSKeysMngrInit(xmlSecKeysMngrPtr mngr) {
170     xmlSecAssert2(mngr != NULL, -1);
171
172     /* TODO: add key data stores */
173     return(0);
174 }
175
176 /**
177  * xmlSecGnuTLSGenerateRandom:
178  * @buffer:             the destination buffer.
179  * @size:               the numer of bytes to generate.
180  *
181  * Generates @size random bytes and puts result in @buffer.
182  *
183  * Returns: 0 on success or a negative value otherwise.
184  */
185 int
186 xmlSecGnuTLSGenerateRandom(xmlSecBufferPtr buffer, xmlSecSize size) {   
187     int ret;
188     
189     xmlSecAssert2(buffer != NULL, -1);
190     xmlSecAssert2(size > 0, -1);
191
192     ret = xmlSecBufferSetSize(buffer, size);
193     if(ret < 0) {
194         xmlSecError(XMLSEC_ERRORS_HERE, 
195                     NULL,
196                     "xmlSecBufferSetSize",
197                     XMLSEC_ERRORS_R_XMLSEC_FAILED,
198                     "size=%d", size);
199         return(-1);
200     }
201         
202     /* get random data */
203     gcry_randomize(xmlSecBufferGetData(buffer), size, GCRY_STRONG_RANDOM);
204     return(0);
205 }