Thanks for a explanations.

Let's consider following main, using ccgost engine:

main(){

    OPENSSL_config(NULL);
    ENGINE *e = ENGINE_by_id("gost");
    ENGINE_init(e);
    ENGINE_free(e);
    ENGINE_set_default(e, ENGINE_METHOD_ALL);
    OpenSSL_add_all_algorithms();

    // emulating ENGINE_load_private_key()
    EVP_PKEY *pkey = EVP_PKEY_new();
    pkey->ameth = ENGINE_get_pkey_asn1_meth(e, NID_id_GostR3410_2001);
bla-bla-bla...
    //end emulating


    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, e);
}

Result ctx is always NULL.
It happens because ENGINE_get_pkey_asn1_meth() returns pointer to corrupted
internall ccgost structure "ameth_GostR3410_2001".
It IS initialized, then being freed. And contains garbage. Specifically
pkey->ameth->pkey_id contains some random value instead of 811
(NID_id_GostR3410_2001).

Is this code contain some error or invalid engine API usage?

On 4 January 2011 06:23, Dr. Stephen Henson <st...@openssl.org> wrote:

> On Tue, Jan 04, 2011, Andrey Kulikov wrote:
>
> > If we take a look at any ENGINE_load_XXX function, we find that they all
> has
> > similar structure:
> >
> >     ENGINE *toadd = engine_XXX();
> >     if(!toadd) return;
> >     ENGINE_add(toadd);
> >     ENGINE_free(toadd);
> >     ERR_clear_error();
> >
> > My question is: why we need call ENGINE_free(toadd) ??
>
> To avoid a memory leak.
>
> When you call engine_XXX() you get a reference to the ENGINE. When it is
> added
> to the ENGINE list the reference count is incremented. When you call
> ENGINE_free() the count is decremented and the ENGINE is freed only if the
> reference count is zero.
>
> Steve.
>
>

Reply via email to