Link to ENGINE_load_private_key in OpenSSL function

1.7k views Asked by At

I am developing a sample OpenSSL Engine for my application.

#include <openssl/engine.h>

static const char *engine_id = "sample";
static const char *engine_name = "developed by Devang";

static int engine_init(ENGINE *e);  
static EVP_PKEY *load_key(ENGINE *e, const char *id, UI_METHOD *ui, void *cb);

int bind_helper(ENGINE*e, const char *id)  {
      if(!ENGINE_set_id(e, engine_id) ||
         !ENGINE_set_init_function(e, engine_init) ||
         !ENGINE_set_load_privkey_function(e, load_key))
         return 0;

  return 1;  

}

IMPLEMENT_DYNAMIC_CHECK_FN(); IMPLEMENT_DYANMIC_BIND_FN(bind_helper);

static int engine_init(ENGINE *e) 
{
    printf("In engine_init \n"); 
} 
static EVP_PKEY *load_key(ENGINE *e, const char *id, UI_METHOD *ui, void *cb) {    
    printf(" In load_key function\n"); 
}

I built this openssl engine (sample.so) and shared lib I put into /usr/lib/x86_64-linu-gnu/openssl-1.0.0/engines/

I created a sample application sampleTest.c

#include <openssl/engine.h>

int main(void)
{
    ENGINE_load_dynamic();
    ENGINE *en = ENGINE_by_id("sample");
    ENGINE_init(en);
    ENGINE_load_private_key(en, NULL, UI_OpenSSL(), NULL);
}

I run this sample application, Output:

In engine_init

Why load_key function is not called ? Can anyone help me link to load_key function from my application ?

1

There are 1 answers

1
joshb On

Try returning 1 from your engine_init() function. Openssl will think your engine failed to initialize and not use further function references otherwise.

ie: It thinks your "hardware accelerator" isn't plugged in or some other kind of failure.

static int engine_init(ENGINE *e) 
{
    printf("In engine_init \n"); 
    return 1;
} 

I added this to your example and got the output from load_key() you're looking for.