tls: some rework based on TLS openssl C API
[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;
63   char *ciphers;
64   int rv;
65
66   if (mp->async_enable)
67     {
68       om->async = 1;
69       openssl_async_node_enable_disable (1);
70     }
71
72   ciphers = (char *) &mp->ciphers;
73   ciphers[63] = '\0';
74   if (ciphers[0])
75     tls_openssl_set_ciphers (ciphers);
76
77   engine = (char *) mp->engine;
78   engine[63] = '\0';
79   alg = (char *) mp->algorithm;
80   alg[63] = '\0';
81   rv = openssl_engine_register (engine, alg);
82
83   REPLY_MACRO (VL_API_TLS_OPENSSL_SET_ENGINE_REPLY);
84 }
85
86 /* Set up the API message handling tables */
87 static clib_error_t *
88 tls_openssl_plugin_api_hookup (vlib_main_t * vm)
89 {
90   openssl_main_t *om = &openssl_main;
91 #define _(N, n)                                                         \
92   vl_msg_api_set_handlers ((VL_API_##N + om->msg_id_base), #n,          \
93                            vl_api_##n##_t_handler, vl_noop_handler,     \
94                            vl_api_##n##_t_endian, vl_api_##n##_t_print, \
95                            sizeof (vl_api_##n##_t), 1);
96   foreach_tls_openssl_plugin_api_msg;
97 #undef _
98
99   return 0;
100 }
101
102 #define vl_msg_name_crc_list
103 #include <tlsopenssl/tls_openssl_all_api_h.h>
104 #undef vl_msg_name_crc_list
105
106 static void
107 setup_message_id_table (openssl_main_t * om, api_main_t * am)
108 {
109 #define _(id, n, crc) \
110   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + om->msg_id_base);
111   foreach_vl_msg_name_crc_tls_openssl;
112 #undef _
113 }
114
115 clib_error_t *
116 tls_openssl_api_init (vlib_main_t * vm)
117 {
118   openssl_main_t *om = &openssl_main;
119   clib_error_t *error = 0;
120   u8 *name;
121
122   name = format (0, "tls_openssl_%08x%c", api_version, 0);
123
124   /* Ask for a correctly-sized block of API message decode slots */
125   om->msg_id_base =
126     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
127
128   error = tls_openssl_plugin_api_hookup (vm);
129
130   /* Add our API messages to the global name_crc hash table */
131   setup_message_id_table (om, &api_main);
132   vec_free (name);
133
134   return error;
135 }
136
137 /*
138  * fd.io coding-style-patch-verification: ON
139  *
140  * Local Variables:
141  * eval: (c-set-style "gnu")
142  * End:
143  */