]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Added the new functions gnutls_get_malloc_function(), gnutls_get_free_function()...
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 6 Dec 2002 17:14:22 +0000 (17:14 +0000)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 6 Dec 2002 17:14:22 +0000 (17:14 +0000)
20 files changed:
NEWS
doc/TODO
doc/tex/Makefile.am
doc/tex/callbacks.tex [new file with mode: 0644]
doc/tex/library.tex
lib/gnutls.h.in.in
lib/gnutls_datum.c
lib/gnutls_datum.h
lib/gnutls_db.c
lib/gnutls_global.c
lib/gnutls_mem.c
lib/gnutls_mem.h
lib/gnutls_state.c
lib/gnutls_str.c
lib/gnutls_str.h
libextra/auth_srp_passwd.c
libextra/auth_srp_passwd.h
libextra/gnutls_srp.c
src/cli.c
src/serv.c

diff --git a/NEWS b/NEWS
index 4afefe9cbdb74427cf6afd4a960918da909c7b68..ef4b7dfdf199231db8c48f2c1919c426333549c9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,10 @@ Version 0.5.12
   than password files.
 - Added the function gnutls_openpgp_set_recv_key_function()
   which can be used to set a callback, to get OpenPGP keys.
+- Added the new functions:
+   gnutls_get_malloc_function()
+   gnutls_get_free_function()
+  to be used in callbacks.
 
 Version 0.5.11 (5/11/2002)
 - Some fixes in 'gnutls-cli' client program to prevent some segmentation 
index 1f2ffca5b07d256320e006603d3e55908a7c9c7e..4fce3a69b060f97f8c516bffe8b73dfdaa34997b 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
@@ -5,6 +5,7 @@ in order to avoid having people working on the same thing.
 Current list:
 + Add ability to read PKCS-12 structures (certificate and private key)
 * Convert documentation to texinfo format
+* Provide the callbacks with a malloc() and a free() like functions.
 * Add support for certificate CRLs in certificate verification
 * Audit the code
 * Add GPGSM certificate manager support
index 4cf7322bee0973d42986371206a4b35e11d9e072..8023be5b541c4b251eee8f82330423a9d7dc3c27 100644 (file)
@@ -17,7 +17,7 @@ TEX_OBJECTS = gnutls.tex ../../lib/gnutls-api.tex fdl.tex \
        appendix.tex x509cert.xml.tex pgpcert.xml.tex \
        programs.tex library.tex certificate.tex record_weaknesses.tex \
        tlsintro.tex compression.tex $(EXAMPLE_OBJECTS) \
-       tls_extensions.tex srp.tex preparation.tex
+       tls_extensions.tex srp.tex preparation.tex callbacks.tex
 
 gnutls.html: $(TEX_OBJECTS)            
        -latex2html gnutls.tex -no_navigation -split 0 \
diff --git a/doc/tex/callbacks.tex b/doc/tex/callbacks.tex
new file mode 100644 (file)
index 0000000..59f3fff
--- /dev/null
@@ -0,0 +1,24 @@
+\section{Callback functions}
+\index{Callback functions}
+
+There are several cases where \gnutls{} may need some out of band input from
+your program. This is now implemented using some callback functions,
+which your program is expected to register.
+
+An example of this type of functions are the push and pull callbacks
+which are used to specify the functions that will retrieve and send
+data to the transport layer.
+\begin{itemize}
+\item \printfunc{gnutls_transport_set_push_function}{gnutls\_transport\_set\_push\_function}
+\item \printfunc{gnutls_transport_set_pull_function}{gnutls\_transport\_set\_pull\_function}
+\end{itemize}
+
+Other callback functions such as the one set by
+\printfunc{gnutls_srp_set_server_credentials_function}{gnutls\_srp\_set\_server\_credentials\_function},
+may require more complicated input, including data to be allocated.
+These callbacks should use the following function to get a malloc()
+and a free() like functions, to allocate data.
+\begin{itemize}
+\item \printfunc{gnutls_global_get_malloc_function}{gnutls\_global\_get\_malloc\_function}
+\item \printfunc{gnutls_global_get_free_function}{gnutls\_global\_get\_free\_function}
+\end{itemize}
index 62cbdc4596325beb0c5208fcc0a0f750b500fae9..5ef9b58f29d57ba54b59040d6b3eb46260cf6547 100644 (file)
@@ -107,3 +107,5 @@ with the previous one.
 \input{errors}
 
 \input{memory}
+
+\input{callbacks}
index f215e9dd2203efeb908fa4fc80d9762a7fae7916..b65e00c46818f115669e3e55d1565d98d3d2e19b 100644 (file)
@@ -312,10 +312,18 @@ int gnutls_certificate_set_x509_key_mem(gnutls_certificate_credentials res,
 int gnutls_global_init(void);
 void gnutls_global_deinit(void);
 
+typedef void* (*gnutls_alloc_function)(size_t);
+typedef void (*gnutls_free_function)(void*);
+typedef void* (*gnutls_realloc_function)(void*, size_t);
+
 void gnutls_global_set_mem_functions( 
-       void *(*gnutls_alloc_func)(size_t), void* (*gnutls_secure_alloc_func)(size_t),
-       int (*gnutls_is_secure_func)(const void*), void *(*gnutls_realloc_func)(void *, size_t),
-       void (*gnutls_free_func)(void*));
+       gnutls_alloc_function, gnutls_alloc_function,
+       int (*gnutls_is_secure_func)(const void*), gnutls_realloc_function,
+       gnutls_free_function);
+
+/* For use in callbacks */
+gnutls_alloc_function* gnutls_global_get_malloc_function(void);
+gnutls_free_function* gnutls_global_get_free_function(void);
 
 typedef void (*gnutls_log_func)( const char*);
 void gnutls_global_set_log_function( gnutls_log_func log_func);
index 30b2cff11f660e4a69e44959ab6d4314bd90f4ba..5d77cd0b010bbb460b9f636a79de4a9b1d217b39 100644 (file)
@@ -49,7 +49,7 @@ void _gnutls_write_datum8( opaque* dest, gnutls_datum dat) {
 
 
 int _gnutls_set_datum_m( gnutls_datum* dat, const void* data, int data_size, 
-       ALLOC_FUNC galloc_func) {
+       gnutls_alloc_function galloc_func) {
        dat->data = galloc_func(data_size);
        if (dat->data==NULL) return GNUTLS_E_MEMORY_ERROR;
 
@@ -60,7 +60,7 @@ int _gnutls_set_datum_m( gnutls_datum* dat, const void* data, int data_size,
 }
 
 int _gnutls_datum_append_m( gnutls_datum* dst, const void* data, int data_size,
-       REALLOC_FUNC grealloc_func) {
+       gnutls_realloc_function grealloc_func) {
 
        dst->data = grealloc_func(dst->data, data_size+dst->size);
        if (dst->data==NULL) return GNUTLS_E_MEMORY_ERROR;
@@ -71,7 +71,7 @@ int _gnutls_datum_append_m( gnutls_datum* dst, const void* data, int data_size,
        return 0;
 }
 
-void _gnutls_free_datum_m( gnutls_datum* dat, FREE_FUNC gfree_func) {
+void _gnutls_free_datum_m( gnutls_datum* dat, gnutls_free_function gfree_func) {
        if (dat->data!=NULL && dat->size!=0)
                gfree_func( dat->data);
 
index b1b23d6571d0f8a065fca290817361808536ebb4..f10026ffd7a4b3ca2e3f05916b93584268855a9e 100644 (file)
@@ -4,14 +4,14 @@ void _gnutls_write_datum32( opaque* dest, gnutls_datum dat);
 void _gnutls_write_datum8( opaque* dest, gnutls_datum dat);
 
 int _gnutls_set_datum_m( gnutls_datum* dat, const void* data, int data_size, 
-       ALLOC_FUNC);
+       gnutls_alloc_function);
 #define _gnutls_set_datum( x, y, z) _gnutls_set_datum_m(x,y,z, gnutls_malloc)
 #define _gnutls_sset_datum( x, y, z) _gnutls_set_datum_m(x,y,z, gnutls_secure_malloc)
 
 int _gnutls_datum_append_m( gnutls_datum* dat, const void* data, int data_size,
-       REALLOC_FUNC);
+       gnutls_realloc_function);
 #define _gnutls_datum_append(x,y,z) _gnutls_datum_append_m(x,y,z, gnutls_realloc)
 
 void _gnutls_free_datum_m( gnutls_datum* dat, 
-       FREE_FUNC);
+       gnutls_free_function);
 #define _gnutls_free_datum(x) _gnutls_free_datum_m(x, gnutls_free)
index 1c2ab4fdcebdb45ee54f544624108df8d0dd4b9c..601f6bad2fa0c7cee83e67cf644d8f801ff18ce4 100644 (file)
@@ -39,6 +39,9 @@
   * sessions database. This function must return a gnutls_datum containing the
   * data on success, or a gnutls_datum containing null and 0 on failure.
   *
+  * The datum's data must be allocated using the function returned by
+  * gnutls_get_malloc_function().
+  *
   * The first argument to store_function() will be null unless gnutls_db_set_ptr() 
   * has been called.
   *
@@ -222,10 +225,8 @@ int ret;
                gnutls_assert();
                return ret;
        }
-       
-       /* Note: Data is not allocated with gnutls_malloc
-        */
-       free(data.data);
+
+       gnutls_free(data.data);
 
        return 0;
 }
@@ -306,7 +307,6 @@ int ret = 0;
        if (session->internals.db_remove_func!=NULL)
                ret = session->internals.db_remove_func( session->internals.db_ptr, session_id);
 
-
        return (ret == 0 ? ret : GNUTLS_E_DB_ERROR);
 
 }
index db106b36af81776a0ec4b494fc9e44ff7f52dd64..2494f1aa0df346ae9d328569477d9ea8417719bb 100644 (file)
@@ -68,11 +68,11 @@ static void dlog( const char* str) {
 #endif
 }
 
-extern ALLOC_FUNC gnutls_secure_malloc;
-extern ALLOC_FUNC gnutls_malloc;
-extern FREE_FUNC gnutls_free;
+extern gnutls_alloc_function gnutls_secure_malloc;
+extern gnutls_alloc_function gnutls_malloc;
+extern gnutls_free_function gnutls_free;
 extern int (*_gnutls_is_secure_memory)(const void*);
-extern REALLOC_FUNC gnutls_realloc;
+extern gnutls_realloc_function gnutls_realloc;
 extern char* (*gnutls_strdup)(const char*);
 extern void* (*gnutls_calloc)(size_t, size_t);
 
@@ -344,3 +344,29 @@ gnutls_check_version( const char *req_version )
     return NULL;
 }
 
+/**
+  * gnutls_global_get_malloc_function - Returns a malloc() like function
+  * @session: is a &gnutls_session structure.
+  *
+  * This function will return a malloc() compatible function to be
+  * used by callbacks.
+  *
+  **/
+gnutls_alloc_function gnutls_global_get_malloc_function()
+{
+       return gnutls_malloc;
+}
+
+/**
+  * gnutls_global_get_free_function - Returns a free() like function
+  * @session: is a &gnutls_session structure.
+  *
+  * This function will return a free() compatible function to be
+  * used by callbacks.
+  *
+  **/
+gnutls_alloc_function gnutls_global_get_free_function()
+{
+       return gnutls_free;
+}
+
index 88a0787d0eaa960ba07cf99f5a56a435748eb030..9dea5e3451ccd94eb13593bb8c3cba8f6111f5d4 100644 (file)
 #include <gnutls_errors.h>
 #include <gnutls_num.h>
 
-ALLOC_FUNC gnutls_secure_malloc = malloc;
-ALLOC_FUNC gnutls_malloc = malloc;
-FREE_FUNC gnutls_free = free;
-REALLOC_FUNC gnutls_realloc = realloc;
+gnutls_alloc_function gnutls_secure_malloc = malloc;
+gnutls_alloc_function gnutls_malloc = malloc;
+gnutls_free_function gnutls_free = free;
+gnutls_realloc_function gnutls_realloc = realloc;
 
 void* (*gnutls_calloc)(size_t, size_t) = calloc;
 char* (*gnutls_strdup)(const char*) = strdup;
index aedf27efb05df1d36c99a5e7bdccbc3e387bf495..5a95d1da594de9f0355c2802eb7b2bf22a809ffa 100644 (file)
@@ -19,16 +19,16 @@ typedef void svoid; /* for functions that allocate using gnutls_secure_malloc */
 # define gnutls_afree gnutls_free
 #endif /* HAVE_ALLOCA */
 
-typedef void* (*ALLOC_FUNC)(size_t);
-typedef void (*FREE_FUNC)(void*);
-typedef void* (*REALLOC_FUNC)(void*, size_t);
+typedef void* (*gnutls_alloc_function)(size_t);
+typedef void (*gnutls_free_function)(void*);
+typedef void* (*gnutls_realloc_function)(void*, size_t);
 
-extern ALLOC_FUNC gnutls_secure_malloc;
-extern ALLOC_FUNC gnutls_malloc;
-extern FREE_FUNC gnutls_free;
+extern gnutls_alloc_function gnutls_secure_malloc;
+extern gnutls_alloc_function gnutls_malloc;
+extern gnutls_free_function gnutls_free;
 
 extern int (*_gnutls_is_secure_memory)(const void*);
-extern REALLOC_FUNC gnutls_realloc;
+extern gnutls_realloc_function gnutls_realloc;
 
 extern void* (*gnutls_calloc)(size_t, size_t);
 extern char* (*gnutls_strdup)( const char*);
index 689d12f6ab826391486102a67ca3bf40a58b91ac..6404e1e7453234e294a51cd2e1076b1c67f9ce24 100644 (file)
@@ -764,3 +764,4 @@ void gnutls_session_set_ptr(gnutls_session session, void* ptr)
 int gnutls_record_get_direction(gnutls_session session) {
        return session->internals.direction;
 }
+
index 2cb96c947896d53899e93cf7d0604eccfd66615c..00a344793222b64889b2218560b224d9a61036c5 100644 (file)
@@ -69,9 +69,9 @@ void _gnutls_mem_cpy( char* dest, size_t dest_tot_size, const char* src, size_t
        }
 }
 
-void _gnutls_string_init( gnutls_string* str, ALLOC_FUNC alloc_func, 
-       REALLOC_FUNC realloc_func,
-       FREE_FUNC free_func) 
+void _gnutls_string_init( gnutls_string* str, gnutls_alloc_function alloc_func, 
+       gnutls_realloc_function realloc_func,
+       gnutls_free_function free_func) 
 {
        str->data = NULL;
        str->max_length = 0;
index de2f31c6145b376190c827d05ce5344d92cc3810..5b5c8f10399cade16da8bb430aa952f956d56958 100644 (file)
@@ -11,12 +11,12 @@ typedef struct {
        opaque * data;
        size_t max_length;
        size_t length;
-       REALLOC_FUNC realloc_func;
-       ALLOC_FUNC alloc_func;
-       FREE_FUNC free_func;
+       gnutls_realloc_function realloc_func;
+       gnutls_alloc_function alloc_func;
+       gnutls_free_function free_func;
 } gnutls_string;
 
-void _gnutls_string_init( gnutls_string*, ALLOC_FUNC, REALLOC_FUNC, FREE_FUNC);
+void _gnutls_string_init( gnutls_string*, gnutls_alloc_function, gnutls_realloc_function, gnutls_free_function);
 void _gnutls_string_clear( gnutls_string*);
 
 /* Beware, do not clear the string, after calling this
index a032b176fa172a7b4bbc23097edcdd7e1410ac9a..fd5a6d3a1ca1c3b1314ebbaeec0979e8aae935d4 100644 (file)
@@ -243,7 +243,6 @@ SRP_PWD_ENTRY *_gnutls_srp_pwd_read_entry( gnutls_session state, char* username,
        if (cred->pwd_callback != NULL) {
                ret = cred->pwd_callback( state, username, &entry->salt,
                        &entry->v, &entry->g, &entry->n);
-               entry->malloced = 1;
                
                if (ret < 0) {
                        gnutls_assert();
@@ -359,17 +358,10 @@ SRP_PWD_ENTRY* _gnutls_randomize_pwd_entry() {
 }
 
 void _gnutls_srp_entry_free( SRP_PWD_ENTRY * entry) {
-       if (entry->malloced) {
-               free( entry->v.data); entry->v.data = NULL;
-               free( entry->g.data); entry->g.data = NULL;
-               free( entry->n.data); entry->n.data = NULL;
-               free( entry->salt.data); entry->salt.data = NULL;
-       } else {
-               _gnutls_free_datum(&entry->v);
-               _gnutls_free_datum(&entry->g);
-               _gnutls_free_datum(&entry->n);
-               _gnutls_free_datum(&entry->salt);
-       }
+       _gnutls_free_datum(&entry->v);
+       _gnutls_free_datum(&entry->g);
+       _gnutls_free_datum(&entry->n);
+       _gnutls_free_datum(&entry->salt);
 
        gnutls_free(entry->username);
        gnutls_free(entry);
index 2171adb782cc19a52b560a726fae8e82f2d0b48c..14b17c36f99d244e1a4e3b7fd6ca5bac7fdce8d4 100644 (file)
@@ -7,10 +7,6 @@ typedef struct {
        gnutls_datum v;
        gnutls_datum g;
        gnutls_datum n;
-       
-       int malloced; /* if non zero, use free() instead of gnutls_free()
-                      */
-
 } SRP_PWD_ENTRY;
 
 /* this is localy alocated. It should be freed using the provided function */
index 2279a28f7a98d126563518c5c948432f2f220098..f58733a6988ee08d51c15d405dc3fe19a6963577 100644 (file)
@@ -514,7 +514,7 @@ void gnutls_srp_server_set_select_function(gnutls_session session,
   * 'username' contains the actual username.
   *
   * The 'salt', 'verifier', 'generator' and 'prime' must be filled
-  * in (using malloc).
+  * in using the malloc returned by gnutls_get_malloc_function().
   *
   * In case the callback returned a negative number then gnutls will
   * assume that the username does not exist.
index ddf45b6defd53a192313688ddccbb27d49b7fa9f..98a9fd6f12be6da6ca82c61cce9da3f2437cd53c 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -119,9 +119,6 @@ void init_global_tls_stuff(void);
 #define DEFAULT_PGP_CERTFILE "openpgp/cli_pub.asc"
 #define DEFAULT_PGP_KEYRING "openpgp/cli_ring.gpg"
 
-#define DEFAULT_SRP_USERNAME "test"
-#define DEFAULT_SRP_PASSWD "test"
-
 /* initializes a gnutls_session with some defaults.
  */
 static gnutls_session init_tls_session( const char* hostname)
@@ -428,13 +425,9 @@ void gaa_parser(int argc, char **argv)
 
    if (info.srp_passwd != NULL)
       srp_passwd = info.srp_passwd;
-   else
-      srp_passwd = DEFAULT_SRP_PASSWD;
 
    if (info.srp_username != NULL)
       srp_username = info.srp_username;
-   else
-      srp_username = DEFAULT_SRP_USERNAME;
 #else
    srp_username = info.srp_username;
    srp_passwd = info.srp_passwd;
@@ -653,7 +646,10 @@ int ret;
       if (gnutls_srp_allocate_client_credentials(&cred) < 0) {
         fprintf(stderr, "SRP authentication error\n");
       }
-      gnutls_srp_set_client_credentials(cred, srp_username, srp_passwd);
+
+      if ((ret=gnutls_srp_set_client_credentials(cred, srp_username, srp_passwd)) < 0) {
+        fprintf(stderr, "SRP credentials set error [%d]\n", ret);
+      }
    }
 
    /* ANON stuff */
index 05907772398546eeaf9f750f834c59972295fdcc..161c620bd43aa24bd40ec511aa559f7973c37105 100644 (file)
 #include <config.h>
 #include <list.h>
 
+#define ENA 1
+#if ENA
+
+#include <opencdk.h>
+
+
+int
+recv_openpgp_key(gnutls_session session, const unsigned char *keyfpr, 
+       unsigned int keyfpr_length, gnutls_datum * key)
+{
+static const char *hostname = "hkp://wwwkeys.pgp.net";
+static const short port = 11371;
+   int rc;
+   CDK_KBNODE knode = NULL;
+
+   /* The key fingerprint should be 20 bytes
+    * in v4 keys.
+    */
+   if (keyfpr_length != 20)
+      return -1;
+
+   rc = cdk_keyserver_recv_key( hostname, port, keyfpr, 
+      CDK_DBSEARCH_FPR, &knode );
+
+   if( !rc ) {
+       size_t len;
+
+       cdk_kbnode_write_to_mem( knode, NULL, &len);
+
+       key->data = malloc( len);
+       if (key->data==NULL) {
+          rc = -1;
+          goto finish;
+       }
+
+       cdk_kbnode_write_to_mem( knode, key->data, &len);
+
+       rc = 0; /* success */
+
+   } else {
+       rc = -1;
+   }
+
+   finish:
+
+   cdk_kbnode_release( knode );
+   return rc;
+
+}
+
+
+#endif
+
 /* konqueror cannot handle sending the page in multiple
  * pieces.
  */
@@ -255,6 +308,10 @@ gnutls_session initialize_session (void)
     */
    gnutls_handshake_set_private_extensions( session, 1);
 
+#if ENA
+gnutls_openpgp_set_recv_key_function( session, recv_openpgp_key);
+#endif
+
    if (nodb==0) {
     gnutls_db_set_retrieve_function( session, wrap_db_fetch);
     gnutls_db_set_remove_function( session, wrap_db_delete);
@@ -591,7 +648,6 @@ int main(int argc, char **argv)
        fprintf(stderr, "Error while setting SRP parameters\n");
    }
 
-
    gnutls_anon_allocate_server_credentials(&dh_cred);
    if (generate != 0)
       gnutls_anon_set_server_dh_params(dh_cred, dh_params);