In some cases one might want to set a string in an OpenSSL CONFIG object.
The following code does just that and it works for 0.9.5a but did not check 
if it works for 0.9.6 release. 

I used it extensively and thought that it might be useful for others but
presently 
have no time to test and refit it for 0.9.6 (may be later).

If any of you find it useful, it might be a good idea to include it in the
next release.

Thanks
--
Alex


/* CONF_set_string() - function that OpenSSL lacks in a pure form
 * so I had to get a snippet here and there - mainly forom the 
 * OpenSSL CONF_new() function and wrap them as a single piece
 */
int CONF_set_string( LHASH* cfg,
                     char*  szSection, 
                     char*  szValueName,
                     char*  szValueData)
{
    CONF_VALUE *v  = 0,
               *vv = 0,
               *tv = 0;
    
    STACK_OF(CONF_VALUE) *ts;

    int res = 0;

    if( 0 != cfg && 0 != szSection && 0 != szValueName && 0 != szValueData)
    {
        if (!(v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))))
                {
                                /* ERR_R_MALLOC_FAILURE */
                                goto err;
                }
                
        v->name  = (char*)Malloc( strlen( szValueName) + 1);
                v->value = (char*)Malloc( strlen( szValueData) + 1);

                if( 0 == v->name || 0 == v->value)
                {
                        /* ERR_R_MALLOC_FAILURE */
                        goto err;
                }
                
        strcpy( v->name,  szValueName);
        strcpy( v->value, szValueData);

                if( 0 == ( tv = get_section( cfg, szSection)))
                                tv=new_section( cfg, szSection);

                if( 0 == tv )
            {
                   /* CONF_R_UNABLE_TO_CREATE_NEW_SECTION */
                    goto err;
                }

                ts = ( STACK_OF( CONF_VALUE) *)tv->value;

                v->section = tv->section;       

                if( !sk_CONF_VALUE_push( ts,v))
                {
                        /* ERR_R_MALLOC_FAILURE */
                        goto err;
                }

                vv = ( CONF_VALUE *)lh_insert( cfg, v);

        res = 1;

                if( vv != NULL)
                {
                        sk_CONF_VALUE_delete_ptr( ts, vv);

                        Free( vv->name);
                        Free( vv->value);
                        Free( vv);
                }
                v = NULL;
    }
err:
    if( 0 != v )
        {
                if( 0 != v->name  ) 
            Free(v->name);
                
        if( 0 != v->value ) 
            Free(v->value);
                
        if( 0 != v) 
            Free(v);
        }
        return res;
}


/* - very very OpenSSL config implementation dependent functions
 * get_section() and new_section() were taken from the original 
 * OpenSSL source - they were not exported so I could not just 
 * link to them
 */

static CONF_VALUE *get_section(LHASH *conf, char *section)
{
        CONF_VALUE *v,vv;

        if ((conf == NULL) || (section == NULL)) return(NULL);
        vv.name=NULL;
        vv.section=section;
        v=(CONF_VALUE *)lh_retrieve(conf,&vv);
        return(v);
}

static CONF_VALUE *new_section(LHASH *conf, char *section)
        {
        STACK *sk=NULL;
        int ok=0,i;
        CONF_VALUE *v=NULL,*vv;

        if ((sk=sk_new_null()) == NULL)
                goto err;
        if ((v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))) == NULL)
                goto err;
        i=strlen(section)+1;
        if ((v->section=(char *)Malloc(i)) == NULL)
                goto err;

        memcpy(v->section,section,i);
        v->name=NULL;
        v->value=(char *)sk;
        
        vv=(CONF_VALUE *)lh_insert(conf,v);
        if (vv != NULL)
                {
#if !defined(NO_STDIO) && !defined(WIN16)
                fprintf(stderr,"internal fault\n");
#endif
                abort();
                }
        ok=1;
err:
        if (!ok)
                {
                if (sk != NULL) sk_free(sk);
                if (v != NULL) Free(v);
                v=NULL;
                }
        return(v);
        }


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to