4 * This is free software; see Copyright file in the source
5 * distribution for preciese wording.
7 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
13 #include <gnutls/gnutls.h>
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>
23 #include <xmlsec/gnutls/app.h>
24 #include <xmlsec/gnutls/crypto.h>
26 static xmlSecCryptoDLFunctionsPtr gXmlSecGnuTLSFunctions = NULL;
29 * xmlSecCryptoGetFunctions_gnutls:
31 * Gets the pointer to xmlsec-gnutls functions table.
33 * Returns: the xmlsec-gnutls functions table or NULL if an error occurs.
35 xmlSecCryptoDLFunctionsPtr
36 xmlSecCryptoGetFunctions_gnutls(void) {
37 static xmlSecCryptoDLFunctions functions;
39 if(gXmlSecGnuTLSFunctions != NULL) {
40 return(gXmlSecGnuTLSFunctions);
43 memset(&functions, 0, sizeof(functions));
44 gXmlSecGnuTLSFunctions = &functions;
47 * Crypto Init/shutdown
49 gXmlSecGnuTLSFunctions->cryptoInit = xmlSecGnuTLSInit;
50 gXmlSecGnuTLSFunctions->cryptoShutdown = xmlSecGnuTLSShutdown;
51 gXmlSecGnuTLSFunctions->cryptoKeysMngrInit = xmlSecGnuTLSKeysMngrInit;
57 gXmlSecGnuTLSFunctions->keyDataAesGetKlass = xmlSecGnuTLSKeyDataAesGetKlass;
58 #endif /* XMLSEC_NO_AES */
61 gXmlSecGnuTLSFunctions->keyDataDesGetKlass = xmlSecGnuTLSKeyDataDesGetKlass;
62 #endif /* XMLSEC_NO_DES */
64 #ifndef XMLSEC_NO_HMAC
65 gXmlSecGnuTLSFunctions->keyDataHmacGetKlass = xmlSecGnuTLSKeyDataHmacGetKlass;
66 #endif /* XMLSEC_NO_HMAC */
73 * Crypto transforms ids
76 gXmlSecGnuTLSFunctions->transformAes128CbcGetKlass = xmlSecGnuTLSTransformAes128CbcGetKlass;
77 gXmlSecGnuTLSFunctions->transformAes192CbcGetKlass = xmlSecGnuTLSTransformAes192CbcGetKlass;
78 gXmlSecGnuTLSFunctions->transformAes256CbcGetKlass = xmlSecGnuTLSTransformAes256CbcGetKlass;
79 #endif /* XMLSEC_NO_AES */
82 gXmlSecGnuTLSFunctions->transformDes3CbcGetKlass = xmlSecGnuTLSTransformDes3CbcGetKlass;
83 #endif /* XMLSEC_NO_DES */
85 #ifndef XMLSEC_NO_HMAC
86 gXmlSecGnuTLSFunctions->transformHmacSha1GetKlass = xmlSecGnuTLSTransformHmacSha1GetKlass;
87 gXmlSecGnuTLSFunctions->transformHmacRipemd160GetKlass = xmlSecGnuTLSTransformHmacRipemd160GetKlass;
88 gXmlSecGnuTLSFunctions->transformHmacMd5GetKlass = xmlSecGnuTLSTransformHmacMd5GetKlass;
89 #endif /* XMLSEC_NO_HMAC */
91 #ifndef XMLSEC_NO_SHA1
92 gXmlSecGnuTLSFunctions->transformSha1GetKlass = xmlSecGnuTLSTransformSha1GetKlass;
93 #endif /* XMLSEC_NO_SHA1 */
96 * High level routines form xmlsec command line utility
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();
112 return(gXmlSecGnuTLSFunctions);
119 * XMLSec library specific crypto engine initialization.
121 * Returns: 0 on success or a negative value otherwise.
124 xmlSecGnuTLSInit (void) {
125 /* Check loaded xmlsec library version */
126 if(xmlSecCheckVersionExact() != 1) {
127 xmlSecError(XMLSEC_ERRORS_HERE,
129 "xmlSecCheckVersionExact",
130 XMLSEC_ERRORS_R_XMLSEC_FAILED,
131 XMLSEC_ERRORS_NO_MESSAGE);
135 /* register our klasses */
136 if(xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms(xmlSecCryptoGetFunctions_gnutls()) < 0) {
137 xmlSecError(XMLSEC_ERRORS_HERE,
139 "xmlSecCryptoDLFunctionsRegisterKeyDataAndTransforms",
140 XMLSEC_ERRORS_R_XMLSEC_FAILED,
141 XMLSEC_ERRORS_NO_MESSAGE);
149 * xmlSecGnuTLSShutdown:
151 * XMLSec library specific crypto engine shutdown.
153 * Returns: 0 on success or a negative value otherwise.
156 xmlSecGnuTLSShutdown(void) {
161 * xmlSecGnuTLSKeysMngrInit:
162 * @mngr: the pointer to keys manager.
164 * Adds GnuTLS specific key data stores in keys manager.
166 * Returns: 0 on success or a negative value otherwise.
169 xmlSecGnuTLSKeysMngrInit(xmlSecKeysMngrPtr mngr) {
170 xmlSecAssert2(mngr != NULL, -1);
172 /* TODO: add key data stores */
177 * xmlSecGnuTLSGenerateRandom:
178 * @buffer: the destination buffer.
179 * @size: the numer of bytes to generate.
181 * Generates @size random bytes and puts result in @buffer.
183 * Returns: 0 on success or a negative value otherwise.
186 xmlSecGnuTLSGenerateRandom(xmlSecBufferPtr buffer, xmlSecSize size) {
189 xmlSecAssert2(buffer != NULL, -1);
190 xmlSecAssert2(size > 0, -1);
192 ret = xmlSecBufferSetSize(buffer, size);
194 xmlSecError(XMLSEC_ERRORS_HERE,
196 "xmlSecBufferSetSize",
197 XMLSEC_ERRORS_R_XMLSEC_FAILED,
202 /* get random data */
203 gcry_randomize(xmlSecBufferGetData(buffer), size, GCRY_STRONG_RANDOM);