tls: Add C API for TLS openssl to set engine
[vpp.git] / src / plugins / tlsopenssl / tls_openssl_api.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vpp/app/version.h>
20 #include <tlsopenssl/tls_openssl.h>
21
22 /* define message IDs */
23 #include <tlsopenssl/tls_openssl_msg_enum.h>
24
25 /* define message structures */
26 #define vl_typedefs
27 #include <tlsopenssl/tls_openssl_all_api_h.h>
28 #undef vl_typedefs
29
30 /* define generated endian-swappers */
31 #define vl_endianfun
32 #include <tlsopenssl/tls_openssl_all_api_h.h>
33 #undef vl_endianfun
34
35 /* instantiate all the print functions we know about */
36 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #define vl_printfun
38 #include <tlsopenssl/tls_openssl_all_api_h.h>
39 #undef vl_printfun
40
41 /* Get the API version number */
42 #define vl_api_version(n, v) static u32 api_version = (v);
43 #include <tlsopenssl/tls_openssl_all_api_h.h>
44 #undef vl_api_version
45
46 #define REPLY_MSG_ID_BASE om->msg_id_base
47 #include <vlibapi/api_helper_macros.h>
48
49 /* List of message types that this plugin understands */
50
51 #define foreach_tls_openssl_plugin_api_msg \
52   _ (TLS_OPENSSL_SET_ENGINE, tls_openssl_set_engine)
53
54 extern openssl_main_t openssl_main;
55
56 /* API message handler */
57 static void
58 vl_api_tls_openssl_set_engine_t_handler (vl_api_tls_openssl_set_engine_t *mp)
59 {
60   vl_api_tls_openssl_set_engine_reply_t *rmp;
61   openssl_main_t *om = &openssl_main;
62   char *engine, *alg, *ciphers;
63   int rv;
64
65   engine = (char *)&mp->engine;
66   alg = (char *)&mp->algorithm;
67   ciphers = (char *)&mp->ciphers;
68
69   if (mp->async)
70     {
71       om->async = 1;
72       openssl_async_node_enable_disable (1);
73     }
74
75   if (ciphers[0])
76     tls_openssl_set_ciphers (ciphers);
77   rv = openssl_engine_register (engine, alg);
78
79   REPLY_MACRO (VL_API_TLS_OPENSSL_SET_ENGINE_REPLY);
80 }
81
82 /* Set up the API message handling tables */
83 static clib_error_t *tls_openssl_plugin_api_hookup (vlib_main_t *vm)
84 {
85   openssl_main_t *om = &openssl_main;
86 #define _(N, n)                                                         \
87   vl_msg_api_set_handlers ((VL_API_##N + om->msg_id_base), #n,          \
88                            vl_api_##n##_t_handler, vl_noop_handler,     \
89                            vl_api_##n##_t_endian, vl_api_##n##_t_print, \
90                            sizeof (vl_api_##n##_t), 1);
91   foreach_tls_openssl_plugin_api_msg;
92 #undef _
93
94   return 0;
95 }
96
97 #define vl_msg_name_crc_list
98 #include <tlsopenssl/tls_openssl_all_api_h.h>
99 #undef vl_msg_name_crc_list
100
101 static void setup_message_id_table (openssl_main_t *om, api_main_t *am)
102 {
103 #define _(id, n, crc) \
104   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + om->msg_id_base);
105   foreach_vl_msg_name_crc_tls_openssl;
106 #undef _
107 }
108
109 clib_error_t *tls_openssl_api_init (vlib_main_t *vm)
110 {
111   openssl_main_t *om = &openssl_main;
112   clib_error_t *error = 0;
113   u8 *name;
114
115   name = format (0, "tls_openssl_%08x%c", api_version, 0);
116
117   /* Ask for a correctly-sized block of API message decode slots */
118   om->msg_id_base =
119       vl_msg_api_get_msg_ids ((char *)name, VL_MSG_FIRST_AVAILABLE);
120
121   error = tls_openssl_plugin_api_hookup (vm);
122
123   /* Add our API messages to the global name_crc hash table */
124   setup_message_id_table (om, &api_main);
125   vec_free (name);
126
127   return error;
128 }