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      "testengine"
171cb0ef41Sopenharmony_ci#define TEST_ENGINE_NAME    "dummy test engine"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#define AGENT_KEY           "test/fixtures/keys/agent1-key.pem"
201cb0ef41Sopenharmony_ci#define AGENT_CERT          "test/fixtures/keys/agent1-cert.pem"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifdef _WIN32
231cb0ef41Sopenharmony_ci# define DEFAULT_VISIBILITY __declspec(dllexport)
241cb0ef41Sopenharmony_ci#else
251cb0ef41Sopenharmony_ci# define DEFAULT_VISIBILITY __attribute__((visibility("default")))
261cb0ef41Sopenharmony_ci#endif
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cinamespace {
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciint EngineInit(ENGINE* engine) {
311cb0ef41Sopenharmony_ci  return 1;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciint EngineFinish(ENGINE* engine) {
351cb0ef41Sopenharmony_ci  return 1;
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciint EngineDestroy(ENGINE* engine) {
391cb0ef41Sopenharmony_ci  return 1;
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cistd::string LoadFile(const char* filename) {
431cb0ef41Sopenharmony_ci  std::ifstream file(filename);
441cb0ef41Sopenharmony_ci  return std::string(std::istreambuf_iterator<char>(file),
451cb0ef41Sopenharmony_ci                     std::istreambuf_iterator<char>());
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciint EngineLoadSSLClientCert(ENGINE* engine,
501cb0ef41Sopenharmony_ci                            SSL* ssl,
511cb0ef41Sopenharmony_ci                            STACK_OF(X509_NAME)* ca_dn,
521cb0ef41Sopenharmony_ci                            X509** ppcert,
531cb0ef41Sopenharmony_ci                            EVP_PKEY** ppkey,
541cb0ef41Sopenharmony_ci                            STACK_OF(X509)** pother,
551cb0ef41Sopenharmony_ci                            UI_METHOD* ui_method,
561cb0ef41Sopenharmony_ci                            void* callback_data) {
571cb0ef41Sopenharmony_ci  if (ppcert != nullptr) {
581cb0ef41Sopenharmony_ci    std::string cert = LoadFile(AGENT_CERT);
591cb0ef41Sopenharmony_ci    if (cert.empty()) {
601cb0ef41Sopenharmony_ci      return 0;
611cb0ef41Sopenharmony_ci    }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    BIO* bio = BIO_new_mem_buf(cert.data(), cert.size());
641cb0ef41Sopenharmony_ci    *ppcert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
651cb0ef41Sopenharmony_ci     BIO_vfree(bio);
661cb0ef41Sopenharmony_ci     if (*ppcert == nullptr) {
671cb0ef41Sopenharmony_ci       printf("Could not read certificate\n");
681cb0ef41Sopenharmony_ci       return 0;
691cb0ef41Sopenharmony_ci     }
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  if (ppkey != nullptr) {
731cb0ef41Sopenharmony_ci    std::string key = LoadFile(AGENT_KEY);
741cb0ef41Sopenharmony_ci    if (key.empty()) {
751cb0ef41Sopenharmony_ci      return 0;
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci    BIO* bio = BIO_new_mem_buf(key.data(), key.size());
791cb0ef41Sopenharmony_ci    *ppkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
801cb0ef41Sopenharmony_ci    BIO_vfree(bio);
811cb0ef41Sopenharmony_ci    if (*ppkey == nullptr) {
821cb0ef41Sopenharmony_ci      printf("Could not read private key\n");
831cb0ef41Sopenharmony_ci      return 0;
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  return 1;
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciint bind_fn(ENGINE* engine, const char* id) {
911cb0ef41Sopenharmony_ci  ENGINE_set_id(engine, TEST_ENGINE_ID);
921cb0ef41Sopenharmony_ci  ENGINE_set_name(engine, TEST_ENGINE_NAME);
931cb0ef41Sopenharmony_ci  ENGINE_set_init_function(engine, EngineInit);
941cb0ef41Sopenharmony_ci  ENGINE_set_finish_function(engine, EngineFinish);
951cb0ef41Sopenharmony_ci  ENGINE_set_destroy_function(engine, EngineDestroy);
961cb0ef41Sopenharmony_ci  ENGINE_set_load_ssl_client_cert_function(engine, EngineLoadSSLClientCert);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  return 1;
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciextern "C" {
1021cb0ef41Sopenharmony_ci  DEFAULT_VISIBILITY IMPLEMENT_DYNAMIC_CHECK_FN();
1031cb0ef41Sopenharmony_ci  DEFAULT_VISIBILITY IMPLEMENT_DYNAMIC_BIND_FN(bind_fn);
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci}  // anonymous namespace
107