xref: /openbmc/qemu/ui/vnc-auth-vencrypt.c (revision 585ec727)
1 /*
2  * QEMU VNC display driver: VeNCrypt authentication setup
3  *
4  * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5  * Copyright (C) 2006 Fabrice Bellard
6  * Copyright (C) 2009 Red Hat, Inc
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include "vnc.h"
28 #include "qemu/main-loop.h"
29 
30 static void start_auth_vencrypt_subauth(VncState *vs)
31 {
32     switch (vs->subauth) {
33     case VNC_AUTH_VENCRYPT_TLSNONE:
34     case VNC_AUTH_VENCRYPT_X509NONE:
35        VNC_DEBUG("Accept TLS auth none\n");
36        vnc_write_u32(vs, 0); /* Accept auth completion */
37        start_client_init(vs);
38        break;
39 
40     case VNC_AUTH_VENCRYPT_TLSVNC:
41     case VNC_AUTH_VENCRYPT_X509VNC:
42        VNC_DEBUG("Start TLS auth VNC\n");
43        start_auth_vnc(vs);
44        break;
45 
46 #ifdef CONFIG_VNC_SASL
47     case VNC_AUTH_VENCRYPT_TLSSASL:
48     case VNC_AUTH_VENCRYPT_X509SASL:
49       VNC_DEBUG("Start TLS auth SASL\n");
50       start_auth_sasl(vs);
51       break;
52 #endif /* CONFIG_VNC_SASL */
53 
54     default: /* Should not be possible, but just in case */
55        VNC_DEBUG("Reject subauth %d server bug\n", vs->auth);
56        vnc_write_u8(vs, 1);
57        if (vs->minor >= 8) {
58            static const char err[] = "Unsupported authentication type";
59            vnc_write_u32(vs, sizeof(err));
60            vnc_write(vs, err, sizeof(err));
61        }
62        vnc_client_error(vs);
63     }
64 }
65 
66 static void vnc_tls_handshake_done(Object *source,
67                                    Error *err,
68                                    gpointer user_data)
69 {
70     VncState *vs = user_data;
71 
72     if (err) {
73         VNC_DEBUG("Handshake failed %s\n",
74                   error_get_pretty(err));
75         vnc_client_error(vs);
76     } else {
77         vs->ioc_tag = qio_channel_add_watch(
78             vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
79         start_auth_vencrypt_subauth(vs);
80     }
81 }
82 
83 
84 static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
85 {
86     int auth = read_u32(data, 0);
87 
88     if (auth != vs->subauth) {
89         VNC_DEBUG("Rejecting auth %d\n", auth);
90         vnc_write_u8(vs, 0); /* Reject auth */
91         vnc_flush(vs);
92         vnc_client_error(vs);
93     } else {
94         Error *err = NULL;
95         QIOChannelTLS *tls;
96         VNC_DEBUG("Accepting auth %d, setting up TLS for handshake\n", auth);
97         vnc_write_u8(vs, 1); /* Accept auth */
98         vnc_flush(vs);
99 
100         if (vs->ioc_tag) {
101             g_source_remove(vs->ioc_tag);
102             vs->ioc_tag = 0;
103         }
104 
105         tls = qio_channel_tls_new_server(
106             vs->ioc,
107             vs->vd->tlscreds,
108             vs->vd->tlsaclname,
109             &err);
110         if (!tls) {
111             VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
112             error_free(err);
113             vnc_client_error(vs);
114             return 0;
115         }
116 
117         VNC_DEBUG("Start TLS VeNCrypt handshake process\n");
118         object_unref(OBJECT(vs->ioc));
119         vs->ioc = QIO_CHANNEL(tls);
120         vs->tls = qio_channel_tls_get_session(tls);
121 
122         qio_channel_tls_handshake(tls,
123                                   vnc_tls_handshake_done,
124                                   vs,
125                                   NULL);
126     }
127     return 0;
128 }
129 
130 static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
131 {
132     if (data[0] != 0 ||
133         data[1] != 2) {
134         VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
135         vnc_write_u8(vs, 1); /* Reject version */
136         vnc_flush(vs);
137         vnc_client_error(vs);
138     } else {
139         VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
140         vnc_write_u8(vs, 0); /* Accept version */
141         vnc_write_u8(vs, 1); /* Number of sub-auths */
142         vnc_write_u32(vs, vs->subauth); /* The supported auth */
143         vnc_flush(vs);
144         vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
145     }
146     return 0;
147 }
148 
149 
150 void start_auth_vencrypt(VncState *vs)
151 {
152     /* Send VeNCrypt version 0.2 */
153     vnc_write_u8(vs, 0);
154     vnc_write_u8(vs, 2);
155 
156     vnc_read_when(vs, protocol_client_vencrypt_init, 2);
157 }
158 
159