Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/ui/vnc-auth-vencrypt.c
Line
Count
Source
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 "qemu/osdep.h"
28
#include "vnc.h"
29
#include "qapi/error.h"
30
#include "qemu/main-loop.h"
31
#include "trace.h"
32
33
static void start_auth_vencrypt_subauth(VncState *vs)
34
0
{
35
0
    switch (vs->subauth) {
36
0
    case VNC_AUTH_VENCRYPT_TLSNONE:
37
0
    case VNC_AUTH_VENCRYPT_X509NONE:
38
0
       vnc_write_u32(vs, 0); /* Accept auth completion */
39
0
       start_client_init(vs);
40
0
       break;
41
42
0
    case VNC_AUTH_VENCRYPT_TLSVNC:
43
0
    case VNC_AUTH_VENCRYPT_X509VNC:
44
0
       start_auth_vnc(vs);
45
0
       break;
46
47
#ifdef CONFIG_VNC_SASL
48
    case VNC_AUTH_VENCRYPT_TLSSASL:
49
    case VNC_AUTH_VENCRYPT_X509SASL:
50
      start_auth_sasl(vs);
51
      break;
52
#endif /* CONFIG_VNC_SASL */
53
54
0
    default: /* Should not be possible, but just in case */
55
0
       trace_vnc_auth_fail(vs, vs->auth, "Unhandled VeNCrypt subauth", "");
56
0
       vnc_write_u8(vs, 1);
57
0
       if (vs->minor >= 8) {
58
0
           static const char err[] = "Unsupported authentication type";
59
0
           vnc_write_u32(vs, sizeof(err));
60
0
           vnc_write(vs, err, sizeof(err));
61
0
       }
62
0
       vnc_client_error(vs);
63
0
    }
64
0
}
65
66
static void vnc_tls_handshake_done(QIOTask *task,
67
                                   gpointer user_data)
68
0
{
69
0
    VncState *vs = user_data;
70
0
    Error *err = NULL;
71
72
0
    if (qio_task_propagate_error(task, &err)) {
73
0
        trace_vnc_auth_fail(vs, vs->auth, "TLS handshake failed",
74
0
                            error_get_pretty(err));
75
0
        vnc_client_error(vs);
76
0
        error_free(err);
77
0
    } else {
78
0
        if (vs->ioc_tag) {
79
0
            g_source_remove(vs->ioc_tag);
80
0
        }
81
0
        vs->ioc_tag = qio_channel_add_watch(
82
0
            vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT,
83
0
            vnc_client_io, vs, NULL);
84
0
        start_auth_vencrypt_subauth(vs);
85
0
    }
86
0
}
87
88
89
static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
90
0
{
91
0
    int auth = read_u32(data, 0);
92
93
0
    trace_vnc_auth_vencrypt_subauth(vs, auth);
94
0
    if (auth != vs->subauth) {
95
0
        trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
96
0
        vnc_write_u8(vs, 0); /* Reject auth */
97
0
        vnc_flush(vs);
98
0
        vnc_client_error(vs);
99
0
    } else {
100
0
        Error *err = NULL;
101
0
        QIOChannelTLS *tls;
102
0
        vnc_write_u8(vs, 1); /* Accept auth */
103
0
        vnc_flush(vs);
104
0
        g_clear_handle_id(&vs->ioc_tag, g_source_remove);
105
106
0
        tls = qio_channel_tls_new_server(
107
0
            vs->ioc,
108
0
            vs->vd->tlscreds,
109
0
            vs->vd->tlsauthzid,
110
0
            &err);
111
0
        if (!tls) {
112
0
            trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
113
0
                                error_get_pretty(err));
114
0
            error_free(err);
115
0
            vnc_client_error(vs);
116
0
            return 0;
117
0
        }
118
119
0
        qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
120
0
        object_unref(OBJECT(vs->ioc));
121
0
        vs->ioc = QIO_CHANNEL(tls);
122
0
        trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
123
0
        vs->tls = qio_channel_tls_get_session(tls);
124
125
0
        qio_channel_tls_handshake(tls,
126
0
                                  vnc_tls_handshake_done,
127
0
                                  vs,
128
0
                                  NULL,
129
0
                                  NULL);
130
0
    }
131
0
    return 0;
132
0
}
133
134
static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
135
0
{
136
0
    trace_vnc_auth_vencrypt_version(vs, (int)data[0], (int)data[1]);
137
0
    if (data[0] != 0 ||
138
0
        data[1] != 2) {
139
0
        trace_vnc_auth_fail(vs, vs->auth, "Unsupported version", "");
140
0
        vnc_write_u8(vs, 1); /* Reject version */
141
0
        vnc_flush(vs);
142
0
        vnc_client_error(vs);
143
0
    } else {
144
0
        vnc_write_u8(vs, 0); /* Accept version */
145
0
        vnc_write_u8(vs, 1); /* Number of sub-auths */
146
0
        vnc_write_u32(vs, vs->subauth); /* The supported auth */
147
0
        vnc_flush(vs);
148
0
        vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
149
0
    }
150
0
    return 0;
151
0
}
152
153
154
void start_auth_vencrypt(VncState *vs)
155
0
{
156
    /* Send VeNCrypt version 0.2 */
157
0
    vnc_write_u8(vs, 0);
158
0
    vnc_write_u8(vs, 2);
159
160
0
    vnc_read_when(vs, protocol_client_vencrypt_init, 2);
161
0
}
162