tls: add custom openssl bio
[vpp.git] / src / plugins / tlsopenssl / tls_bio.c
1
2 /*
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <openssl/bio.h>
17 #include <openssl/err.h>
18 #include <vnet/session/session.h>
19 #include <vnet/session/application_interface.h>
20
21 static inline session_t *
22 bio_session (BIO * bio)
23 {
24   return session_get_from_handle (pointer_to_uword (BIO_get_data (bio)));
25 }
26
27 static int
28 bio_tls_alloc (BIO * bio)
29 {
30   BIO_set_init (bio, 0);
31   BIO_set_data (bio, 0);
32   BIO_set_flags (bio, 0);
33   BIO_set_shutdown (bio, 0);
34   return 1;
35 }
36
37 static int
38 bio_tls_free (BIO * bio)
39 {
40   if (!bio)
41     return 0;
42
43   if (BIO_get_shutdown (bio))
44     {
45       if (BIO_get_init (bio))
46         session_close (bio_session (bio));
47       BIO_set_init (bio, 0);
48       BIO_set_flags (bio, 0);
49     }
50
51   return 1;
52 }
53
54 static int
55 bio_tls_read (BIO * b, char *out, int outl)
56 {
57   session_t *s;
58   int rv;
59
60   if (PREDICT_FALSE (!out))
61     return 0;
62
63   s = bio_session (b);
64   if (!s)
65     {
66       clib_warning ("no session");
67       errno = EBADFD;
68       return -1;
69     }
70
71   rv = app_recv_stream_raw (s->rx_fifo, (u8 *) out, outl,
72                             0 /* clear evt */ , 0 /* peek */ );
73   if (rv < 0)
74     {
75       BIO_set_retry_read (b);
76       errno = EAGAIN;
77       return -1;
78     }
79
80   if (svm_fifo_is_empty_cons (s->rx_fifo))
81     svm_fifo_unset_event (s->rx_fifo);
82
83   BIO_clear_retry_flags (b);
84
85   return rv;
86 }
87
88 static int
89 bio_tls_write (BIO * b, const char *in, int inl)
90 {
91   svm_msg_q_t *mq;
92   session_t *s;
93   int rv;
94
95   if (PREDICT_FALSE (!in))
96     return 0;
97
98   s = bio_session (b);
99   if (!s)
100     {
101       clib_warning ("no session");
102       errno = EBADFD;
103       return -1;
104     }
105
106   mq = session_main_get_vpp_event_queue (s->thread_index);
107   rv = app_send_stream_raw (s->tx_fifo, mq, (u8 *) in, inl,
108                             SESSION_IO_EVT_TX, 1 /* do_evt */ ,
109                             0 /* noblock */ );
110   if (rv < 0)
111     {
112       BIO_set_retry_write (b);
113       errno = EAGAIN;
114       return -1;
115     }
116
117   BIO_clear_retry_flags (b);
118
119   return rv;
120 }
121
122 long
123 bio_tls_ctrl (BIO * b, int cmd, long larg, void *ptr)
124 {
125   long ret = 1;
126
127   switch (cmd)
128     {
129     case BIO_C_SET_FD:
130       ASSERT (0);
131       break;
132     case BIO_C_GET_FD:
133       ASSERT (0);
134       break;
135     case BIO_CTRL_GET_CLOSE:
136       ret = BIO_get_shutdown (b);
137       break;
138     case BIO_CTRL_SET_CLOSE:
139       BIO_set_shutdown (b, (int) larg);
140       break;
141     case BIO_CTRL_DUP:
142     case BIO_CTRL_FLUSH:
143       ret = 1;
144       break;
145     case BIO_CTRL_PENDING:
146       ret = 0;
147       break;
148     default:
149       ret = 0;
150       break;
151     }
152   return ret;
153 }
154
155 BIO *
156 BIO_new_tls (session_handle_t sh)
157 {
158   static BIO_METHOD *tls_bio_method;
159   BIO *b;
160   if (!tls_bio_method)
161     {
162       tls_bio_method = BIO_meth_new (BIO_TYPE_SOCKET, "tls_bio");
163       BIO_meth_set_write (tls_bio_method, bio_tls_write);
164       BIO_meth_set_read (tls_bio_method, bio_tls_read);
165       BIO_meth_set_create (tls_bio_method, bio_tls_alloc);
166       BIO_meth_set_destroy (tls_bio_method, bio_tls_free);
167       BIO_meth_set_ctrl (tls_bio_method, bio_tls_ctrl);
168     }
169   b = BIO_new (tls_bio_method);
170   /* Initialize the BIO */
171   BIO_set_data (b, uword_to_pointer (sh, void *));
172   BIO_set_init (b, 1);
173   return b;
174 }
175
176 /*
177  * fd.io coding-style-patch-verification: ON
178  *
179  * Local Variables:
180  * eval: (c-set-style "gnu")
181  * End:
182  */