On Tue, Jan 04, 2011, Andrey Kulikov wrote:

> 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?
> 

I can't see how that would happen:

ENGINE_by_id() should up the structural reference.
ENGINE_init() should up the functional and structural reference.
ENGINE_free() should decrement the structural reference.

ENGINE_set_default() should increment both structural and functional
references by an amount which depends on the number of default algorithms
added (which might be zero for some ENGINEs).

ENGINE_load_private_key() should be passed a valid functional reference.

A correct implementation of an ENGINE private key function should call
EVP_PKEY_assign which will also up functional and structural reference counts.

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to