Previous: Authentication and credentials, Up: Authentication methods


4.6 Parameters stored in credentials

Several parameters such as the ones used for Diffie-Hellman authentication are stored within the credentials structures, so all sessions can access them. Those parameters are stored in structures such as gnutls_dh_params_t and gnutls_rsa_params_t, and functions like gnutls_certificate_set_dh_params and gnutls_certificate_set_rsa_export_params can be used to associate those parameters with the given credentials structure.

Since those parameters need to be renewed from time to time and a global structure such as the credentials, may not be easy to modify since it is accessible by all sessions, an alternative interface is available using a callback function. This can be set using the gnutls_certificate_set_params_function. An example is shown below.

     #include <gnutls.h>
     
     gnutls_rsa_params_t rsa_params;
     gnutls_dh_params_t dh_params;
     
     /* This function will be called once a session requests DH
      * or RSA parameters. The parameters returned (if any) will
      * be used for the first handshake only.
      */
     static int get_params( gnutls_session_t session,
             gnutls_params_type_t type,
             gnutls_params_st *st)
     {
        if (type == GNUTLS_PARAMS_RSA_EXPORT)
           st->params.rsa_export = rsa_params;
        else if (type == GNUTLS_PARAMS_DH)
           st->params.dh = dh_params;
        else return -1;
     
        st->type = type;
        /* do not deinitialize those parameters.
         */
        st->deinit = 0;
     
        return 0;
     }
     
     int main()
     {
        gnutls_certificate_credentials_t cert_cred;
     
        initialize_params();
     
        /* ...
         */
     
        gnutls_certificate_set_params_function( cert_cred, get_params);
     }