11cb0ef41Sopenharmony_ci#include <openssl/engine.h>
21cb0ef41Sopenharmony_ci#include <openssl/pem.h>
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#include <assert.h>
51cb0ef41Sopenharmony_ci#include <string.h>
61cb0ef41Sopenharmony_ci#include <stdlib.h>
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <fstream>
91cb0ef41Sopenharmony_ci#include <iterator>
101cb0ef41Sopenharmony_ci#include <string>
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#ifndef ENGINE_CMD_BASE
131cb0ef41Sopenharmony_ci# error did not get engine.h
141cb0ef41Sopenharmony_ci#endif
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#define TEST_ENGINE_ID      "testkeyengine"
171cb0ef41Sopenharmony_ci#define TEST_ENGINE_NAME    "dummy test key engine"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#define PRIVATE_KEY           "test/fixtures/keys/agent1-key.pem"
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#ifdef _WIN32
221cb0ef41Sopenharmony_ci# define DEFAULT_VISIBILITY __declspec(dllexport)
231cb0ef41Sopenharmony_ci#else
241cb0ef41Sopenharmony_ci# define DEFAULT_VISIBILITY __attribute__((visibility("default")))
251cb0ef41Sopenharmony_ci#endif
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cinamespace {
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciint EngineInit(ENGINE* engine) {
301cb0ef41Sopenharmony_ci  return 1;
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciint EngineFinish(ENGINE* engine) {
341cb0ef41Sopenharmony_ci  return 1;
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciint EngineDestroy(ENGINE* engine) {
381cb0ef41Sopenharmony_ci  return 1;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cistd::string LoadFile(const char* filename) {
421cb0ef41Sopenharmony_ci  std::ifstream file(filename);
431cb0ef41Sopenharmony_ci  return std::string(std::istreambuf_iterator<char>(file),
441cb0ef41Sopenharmony_ci                     std::istreambuf_iterator<char>());
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cistatic EVP_PKEY* EngineLoadPrivkey(ENGINE* engine, const char* name,
481cb0ef41Sopenharmony_ci                                   UI_METHOD* ui_method, void* callback_data) {
491cb0ef41Sopenharmony_ci  if (strcmp(name, "dummykey") == 0) {
501cb0ef41Sopenharmony_ci    std::string key = LoadFile(PRIVATE_KEY);
511cb0ef41Sopenharmony_ci    BIO* bio = BIO_new_mem_buf(key.data(), key.size());
521cb0ef41Sopenharmony_ci    EVP_PKEY* ret = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    BIO_vfree(bio);
551cb0ef41Sopenharmony_ci    if (ret != nullptr) {
561cb0ef41Sopenharmony_ci      return ret;
571cb0ef41Sopenharmony_ci    }
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  return nullptr;
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciint bind_fn(ENGINE* engine, const char* id) {
641cb0ef41Sopenharmony_ci  ENGINE_set_id(engine, TEST_ENGINE_ID);
651cb0ef41Sopenharmony_ci  ENGINE_set_name(engine, TEST_ENGINE_NAME);
661cb0ef41Sopenharmony_ci  ENGINE_set_init_function(engine, EngineInit);
671cb0ef41Sopenharmony_ci  ENGINE_set_finish_function(engine, EngineFinish);
681cb0ef41Sopenharmony_ci  ENGINE_set_destroy_function(engine, EngineDestroy);
691cb0ef41Sopenharmony_ci  ENGINE_set_load_privkey_function(engine, EngineLoadPrivkey);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  return 1;
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciextern "C" {
751cb0ef41Sopenharmony_ci  DEFAULT_VISIBILITY IMPLEMENT_DYNAMIC_CHECK_FN();
761cb0ef41Sopenharmony_ci  DEFAULT_VISIBILITY IMPLEMENT_DYNAMIC_BIND_FN(bind_fn);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci}  // anonymous namespace
80