OpenVPN
libtestengine.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <openssl/engine.h>
3 #include <openssl/evp.h>
4 #include <openssl/pem.h>
5 
6 static char *engine_id = "testengine";
7 static char *engine_name = "Engine for testing openvpn engine key support";
8 
9 static int is_initialized = 0;
10 
11 static int
12 engine_init(ENGINE *e)
13 {
14  is_initialized = 1;
15  fprintf(stderr, "ENGINE: engine_init called\n");
16  return 1;
17 }
18 
19 static int
20 engine_finish(ENGINE *e)
21 {
22  fprintf(stderr, "ENGINE: engine_finsh called\n");
23  is_initialized = 0;
24  return 1;
25 }
26 
27 static EVP_PKEY *
28 engine_load_key(ENGINE *e, const char *key_id,
29  UI_METHOD *ui_method, void *cb_data)
30 {
31  BIO *b;
32  EVP_PKEY *pkey;
33  PKCS8_PRIV_KEY_INFO *p8inf;
34  UI *ui;
35  char auth[256];
36 
37  fprintf(stderr, "ENGINE: engine_load_key called\n");
38 
39  if (!is_initialized)
40  {
41  fprintf(stderr, "Load Key called without correct initialization\n");
42  return NULL;
43  }
44  b = BIO_new_file(key_id, "r");
45  if (!b)
46  {
47  fprintf(stderr, "File %s does not exist or cannot be read\n", key_id);
48  return 0;
49  }
50  /* Basically read an EVP_PKEY private key file with different
51  * PEM guards --- we are a test engine */
52  p8inf = PEM_ASN1_read_bio((d2i_of_void *)d2i_PKCS8_PRIV_KEY_INFO,
53  "TEST ENGINE KEY", b,
54  NULL, NULL, NULL);
55  BIO_free(b);
56  if (!p8inf)
57  {
58  fprintf(stderr, "Failed to read engine private key\n");
59  return NULL;
60  }
61  pkey = EVP_PKCS82PKEY(p8inf);
62 
63  /* now we have a private key, pretend it had a password
64  * this verifies the password makes it through openvpn OK */
65  ui = UI_new();
66 
67  if (ui_method)
68  {
69  UI_set_method(ui, ui_method);
70  }
71 
72  UI_add_user_data(ui, cb_data);
73 
74  if (UI_add_input_string(ui, "enter test engine key",
75  UI_INPUT_FLAG_DEFAULT_PWD,
76  auth, 0, sizeof(auth)) == 0)
77  {
78  fprintf(stderr, "UI_add_input_string failed\n");
79  goto out;
80  }
81 
82  if (UI_process(ui))
83  {
84  fprintf(stderr, "UI_process failed\n");
85  goto out;
86  }
87 
88  fprintf(stderr, "ENGINE: engine_load_key got password %s\n", auth);
89 
90 out:
91  UI_free(ui);
92 
93  return pkey;
94 }
95 
96 
97 static int
98 engine_bind_fn(ENGINE *e, const char *id)
99 {
100  if (id && strcmp(id, engine_id) != 0)
101  {
102  return 0;
103  }
104  if (!ENGINE_set_id(e, engine_id)
105  || !ENGINE_set_name(e, engine_name)
106  || !ENGINE_set_init_function(e, engine_init)
107  || !ENGINE_set_finish_function(e, engine_finish)
108  || !ENGINE_set_load_privkey_function(e, engine_load_key))
109  {
110  return 0;
111  }
112  return 1;
113 }
114 
115 IMPLEMENT_DYNAMIC_CHECK_FN()
116 IMPLEMENT_DYNAMIC_BIND_FN(engine_bind_fn)
static char * engine_name
Definition: libtestengine.c:7
static int is_initialized
Definition: libtestengine.c:9
static int engine_finish(ENGINE *e)
Definition: libtestengine.c:20
static char * engine_id
Definition: libtestengine.c:6
static EVP_PKEY * engine_load_key(ENGINE *e, const char *key_id, UI_METHOD *ui_method, void *cb_data)
Definition: libtestengine.c:28
static int engine_bind_fn(ENGINE *e, const char *id)
Definition: libtestengine.c:98
static int engine_init(ENGINE *e)
Definition: libtestengine.c:12