OpenSSL Engine的三种加载方式

打印 上一主题 下一主题

主题 552|帖子 552|积分 1656

本文测试代码基于Openssl版本:1.1.1f
创建一个Engine lib
  1. #include <openssl/evp.h>
  2. #include <openssl/engine.h>
  3. #include <iostream>
  4. static int encryptfn(EVP_PKEY_CTX *ctx,unsigned char *out,size_t *outlen,const unsigned char *in,size_t inlen){
  5.   *outlen = 1;
  6.   std::cout << "encryptfn call" << std::endl;
  7.   return 1;
  8. }
  9. static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  10.                            const int **pnids, int nid)
  11. {
  12.     static const int rnid = EVP_PKEY_RSA;
  13.     if (pmeth == NULL) {
  14.         *pnids = &rnid;
  15.         return 1;
  16.     }
  17.     if (nid == EVP_PKEY_RSA) {
  18.         EVP_PKEY_METHOD* method{EVP_PKEY_meth_new(0,0)};
  19.         EVP_PKEY_meth_set_encrypt(method, nullptr, encryptfn);//设置自定义方法
  20.         *pmeth = method;
  21.         return 1;
  22.     }
  23.     *pmeth = NULL;
  24.     return 0;
  25. }
  26. static std::int32_t bind(ENGINE* const e, const char* const id) {
  27.   std::int32_t ret{0};
  28.   ENGINE_set_id(e,"test_ID");
  29.   ENGINE_set_name(e,"test_name");
  30.   ENGINE_set_pkey_meths(e,test_pkey_meths);
  31.   return 1;
  32. }
  33. extern "C" std::int32_t bind_engine(ENGINE* const e, const char* const id,
  34.                                     const dynamic_fns* const fns);
  35. extern "C" std::int32_t bind_engine(ENGINE* const e, const char* const id,
  36.                                     const dynamic_fns* const fns) {
  37.   if (ENGINE_get_static_state() == fns->static_state) {
  38.     if (0 == bind(e, id)) {
  39.       return 0;
  40.     }
  41.     return 1;
  42.   }
  43.   static_cast<void>(CRYPTO_set_mem_functions(
  44.       fns->mem_fns.malloc_fn, fns->mem_fns.realloc_fn, fns->mem_fns.free_fn));
  45.   if (0 == bind(e, id)) {
  46.     return 0;
  47.   }
  48.   return 1;
  49. }
  50. extern "C" uint64_t v_check(const uint64_t v) noexcept;
  51. extern "C" uint64_t v_check(const uint64_t v) noexcept {
  52.   if (v >= static_cast<uint64_t>(0x00030000U)) {
  53.     return static_cast<uint64_t>(0x00030000U);
  54.   }
  55.   return 0U;
  56. }
复制代码
编译动态库:g++ -fPIC -shared engine_evp_so.cpp -o libengine_evp.so

Engine 加载方式1:cmd加载

方法1:openssl cmd加载
  1. > engine dynamic -pre SO_PATH:/yourpath/libengine_evp.so -pre LOAD
  2. (dynamic) Dynamic engine loading support
  3. [Success]: SO_PATH:/yourpath/libengine_evp.so
  4. [Success]: LOAD
  5. Loaded: (test_ID) test_name
复制代码
方法2:进程中调用cmd函数加载

在代码中使用ENGINE_ctrl_cmd_string()调用cmd能力来加载engine
[code]#include #include #include void dump_hex(const uint8_t *hex, uint32_t size) {  uint32_t i = 0;  for (i = 0; i < size; ++i) {    if ((i % 8) == 0) {      printf("\n");    }    printf("0x%02x ", hex);  }  printf("\n");}RSA* rsa_create(){    RSA* rsa = RSA_new();//分配空间    BIGNUM* pBNe = BN_new();//分配空间    BN_set_word(pBNe, RSA_F4);    int ret = RSA_generate_key_ex(rsa, 1024, pBNe, NULL);    if(ret < 0 ){        printf("encrypt failed, ret:%d \n", ret);        return nullptr;    }    BN_free(pBNe);    return rsa;}int main(){  ENGINE_load_dynamic();//加载dynamic engine  ENGINE* engine = ENGINE_by_id("dynamic");  if(engine == nullptr){    std::cout
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊雷无声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表