tls: Add C API for TLS openssl to set engine
[vpp.git] / src / plugins / tlsopenssl / tls_openssl_test.c
1 /*
2  * tls_openssl_test.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) 2019 Intel Corporation
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <ctype.h>
22
23 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
24
25 /* Declare message IDs */
26 #include <tlsopenssl/tls_openssl_msg_enum.h>
27
28 /* define message structures */
29 #define vl_typedefs
30 #include <tlsopenssl/tls_openssl_all_api_h.h>
31 #undef vl_typedefs
32
33 /* declare message handlers for each api */
34
35 #define vl_endianfun            /* define message structures */
36 #include <tlsopenssl/tls_openssl_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...)
41 #define vl_printfun
42 #include <tlsopenssl/tls_openssl_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number. */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <tlsopenssl/tls_openssl_all_api_h.h>
48 #undef vl_api_version
49
50 typedef struct
51 {
52   /* API message ID base */
53   u16 msg_id_base;
54   vat_main_t *vat_main;
55 } tls_openssl_test_main_t;
56
57 tls_openssl_test_main_t tls_openssl_test_main;
58
59 #define __plugin_msg_base tls_openssl_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
61
62 #define foreach_standard_reply_retval_handler   \
63 _(tls_openssl_set_engine_reply)
64
65 #define _(n)                                            \
66     static void vl_api_##n##_t_handler                  \
67     (vl_api_##n##_t * mp)                               \
68     {                                                   \
69         vat_main_t * vam = tls_openssl_test_main.vat_main;   \
70         i32 retval = ntohl(mp->retval);                 \
71         if (vam->async_mode) {                          \
72             vam->async_errors += (retval < 0);          \
73         } else {                                        \
74             vam->retval = retval;                       \
75             vam->result_ready = 1;                      \
76         }                                               \
77     }
78 foreach_standard_reply_retval_handler;
79 #undef _
80
81 /*
82  * Table of message reply handlers, must include boilerplate handlers
83  * we just generated
84  */
85 #define foreach_vpe_api_reply_msg                       \
86 _(TLS_OPENSSL_SET_ENGINE_REPLY, tls_openssl_set_engine_reply)
87
88
89 static int
90 api_tls_openssl_set_engine (vat_main_t * vam)
91 {
92   unformat_input_t *line_input = vam->input;
93   vl_api_tls_openssl_set_engine_t *mp;
94   u8 *engine_name = 0;
95   u8 *engine_alg = 0;
96   u8 *ciphers = 0;
97   u32 async = 0;
98   int ret;
99
100   /* Parse args required to build the message */
101   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102     {
103       if (unformat (line_input, "engine %s", &engine_name))
104         ;
105       else if (unformat (line_input, "async"))
106         {
107           async = 1;
108         }
109       else if (unformat (line_input, "alg %s", &engine_alg))
110         ;
111       else if (unformat (line_input, "ciphers %s", &ciphers))
112         ;
113       else
114         {
115           errmsg ("unknown input `%U'", format_unformat_error, line_input);
116           return -99;
117         }
118     }
119
120   if (engine_name == 0)
121     {
122       errmsg ("Must specify engine name");
123       return -99;
124     }
125
126   if (engine_alg == 0)
127     engine_alg = format (0, "ALL");
128   else
129     {
130       for (int i = 0; i < strnlen ((char *) engine_alg, 64); i++)
131         engine_alg[i] = toupper (engine_alg[i]);
132     }
133
134
135   /* Construct the API message */
136   M (TLS_OPENSSL_SET_ENGINE, mp);
137   mp->async = async;
138
139   clib_memcpy_fast (mp->engine, engine_name,
140                     strnlen ((const char *) engine_name, 64));
141
142   clib_memcpy_fast (mp->algorithm, engine_alg,
143                     strnlen ((const char *) engine_alg, 64));
144
145   if (ciphers)
146     clib_memcpy_fast (mp->ciphers, ciphers,
147                       strnlen ((const char *) ciphers, 64));
148
149   /* send it... */
150   S (mp);
151
152   /* Wait for a reply... */
153   W (ret);
154   return ret;
155 }
156
157 /*
158  * List of messages that the api test plugin sends,
159  * and that the data plane plugin processes
160  */
161 #define foreach_vpe_api_msg                                      \
162 _(tls_openssl_set_engine, "tls openssl set [engine <engine name>]" \
163 "[alg [algorithm] [async]\n")
164
165 static void
166 tls_openssl_api_hookup (vat_main_t * vam)
167 {
168   tls_openssl_test_main_t *htmp = &tls_openssl_test_main;
169   /* Hook up handlers for replies from the data plane plug-in */
170 #define _(N,n)                                                  \
171     vl_msg_api_set_handlers((VL_API_##N + htmp->msg_id_base),     \
172                            #n,                                  \
173                            vl_api_##n##_t_handler,              \
174                            vl_noop_handler,                     \
175                            vl_api_##n##_t_endian,               \
176                            vl_api_##n##_t_print,                \
177                            sizeof(vl_api_##n##_t), 1);
178   foreach_vpe_api_reply_msg;
179 #undef _
180
181   /* API messages we can send */
182 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
183   foreach_vpe_api_msg;
184 #undef _
185
186   /* Help strings */
187 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
188   foreach_vpe_api_msg;
189 #undef _
190 }
191
192 VAT_PLUGIN_REGISTER (tls_openssl);
193
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "gnu")
199  * End:
200  */