0

我已经安装了 OpenSSL 1.1.1c。我正在尝试为 RSA 制作自定义 OpenSSL 引擎。以下示例代码是从引擎e_dasync.c复制而来的。

以下是我的 RSA 引擎 ( rsa-engine.c) 的示例代码,

/* Engine Id and Name */
static const char *engine_rsa_id = "rsa-engine-new";
static const char *engine_rsa_name = "RSA engine for testing";

// data encryption function
static int eng_rsa_pub_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) {
    printf("RSA Engine is encrypting using public key\n");
    return RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
        (flen,from,to,rsa,padding);
}

// signature verify
static int eng_rsa_pub_dec(int flen, const unsigned char *from,
                          unsigned char *to, RSA *rsa, int padding) {
    printf("Signature verification\n");
    return 0;
}


// signature
static int eng_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding){
    printf("Signature method:\n");
    return 0;
}


// data decryption
static int eng_rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding){
   printf("decryption method:\n");
   return 0;
}


static RSA_METHOD *test_rsa_method = NULL;


static int bind_dasync(ENGINE *e){
    /* Setup RSA_METHOD */
    if ((test_rsa_method = RSA_meth_new("Test RSA Engine", 0)) == NULL
        || RSA_meth_set_pub_enc(test_rsa_method, eng_rsa_pub_enc) == 0
        || RSA_meth_set_pub_dec(test_rsa_method, eng_rsa_pub_dec) == 0
        || RSA_meth_set_priv_enc(test_rsa_method, eng_rsa_priv_enc) == 0
        || RSA_meth_set_priv_dec(test_rsa_method, eng_rsa_priv_dec) == 0
            ) {

        return 0;
    }

    /* Ensure the dasync error handling is set up */

    if (!ENGINE_set_id(e, engine_rsa_id)
        || !ENGINE_set_name(e, engine_rsa_name)
        || !ENGINE_set_RSA(e, test_rsa_method)
            ) {
        return 0;
    }
    return 1;
}

static int bind_helper(ENGINE *e, const char *id){
    if (!bind_dasync(e)){
        printf("2_Error: Inside Bind helper\n");
        return 0;
    }
    return 1;
}

IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
IMPLEMENT_DYNAMIC_CHECK_FN()

我的Makefile样子如下,

rsa-engine:
gcc -g -fPIC -c rsa-engine.c
gcc -g -shared -o librsa_engine.so -L./libdune rsa-engine.o -Bdynamic -lcrypto -lpthread
mv librsa_engine.so rsa-engine-new.so
sudo cp rsa-engine-new.so /opt/openssl/lib/engines-1.1/

clean:
rm -f *.o *.d *.so rsa-engine

我的代码编译。当我尝试使用以下命令进行加密时,openssl rsautl -encrypt -inkey public.pem -pubin -in msg.txt -out msg.enc -engine rsa-engine-new

我得到一个分段错误,

engine "rsa-engine-new" set.
RSA Engine is encrypting using public key
Segmentation fault (core dumped)

我是否需要使用 OpenSSL 编译此示例引擎才能使其工作?

4

0 回答 0