vnc-auth-vencrypt.c 5.1 KB
Newer Older
A
aliguori 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * QEMU VNC display driver: VeNCrypt authentication setup
 *
 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
 * Copyright (C) 2006 Fabrice Bellard
 * Copyright (C) 2009 Red Hat, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

P
Peter Maydell 已提交
27
#include "qemu/osdep.h"
A
aliguori 已提交
28
#include "vnc.h"
29
#include "qapi/error.h"
30
#include "qemu/main-loop.h"
31
#include "trace.h"
A
aliguori 已提交
32 33 34

static void start_auth_vencrypt_subauth(VncState *vs)
{
35
    switch (vs->subauth) {
A
aliguori 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
    case VNC_AUTH_VENCRYPT_TLSNONE:
    case VNC_AUTH_VENCRYPT_X509NONE:
       vnc_write_u32(vs, 0); /* Accept auth completion */
       start_client_init(vs);
       break;

    case VNC_AUTH_VENCRYPT_TLSVNC:
    case VNC_AUTH_VENCRYPT_X509VNC:
       start_auth_vnc(vs);
       break;

#ifdef CONFIG_VNC_SASL
    case VNC_AUTH_VENCRYPT_TLSSASL:
    case VNC_AUTH_VENCRYPT_X509SASL:
B
Blue Swirl 已提交
50 51
      start_auth_sasl(vs);
      break;
A
aliguori 已提交
52 53 54
#endif /* CONFIG_VNC_SASL */

    default: /* Should not be possible, but just in case */
55
       trace_vnc_auth_fail(vs, vs->auth, "Unhandled VeNCrypt subauth", "");
A
aliguori 已提交
56 57 58 59 60 61 62 63 64 65
       vnc_write_u8(vs, 1);
       if (vs->minor >= 8) {
           static const char err[] = "Unsupported authentication type";
           vnc_write_u32(vs, sizeof(err));
           vnc_write(vs, err, sizeof(err));
       }
       vnc_client_error(vs);
    }
}

66
static void vnc_tls_handshake_done(QIOTask *task,
67
                                   gpointer user_data)
C
Chih-Min Chao 已提交
68
{
69
    VncState *vs = user_data;
70
    Error *err = NULL;
A
aliguori 已提交
71

72
    if (qio_task_propagate_error(task, &err)) {
73 74
        trace_vnc_auth_fail(vs, vs->auth, "TLS handshake failed",
                            error_get_pretty(err));
75
        vnc_client_error(vs);
76
        error_free(err);
77
    } else {
78 79 80
        if (vs->ioc_tag) {
            g_source_remove(vs->ioc_tag);
        }
81 82
        vs->ioc_tag = qio_channel_add_watch(
            vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
83 84
        start_auth_vencrypt_subauth(vs);
    }
A
aliguori 已提交
85 86 87 88 89 90 91
}


static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
{
    int auth = read_u32(data, 0);

92
    trace_vnc_auth_vencrypt_subauth(vs, auth);
93
    if (auth != vs->subauth) {
94
        trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
A
aliguori 已提交
95 96 97 98
        vnc_write_u8(vs, 0); /* Reject auth */
        vnc_flush(vs);
        vnc_client_error(vs);
    } else {
99
        Error *err = NULL;
100
        QIOChannelTLS *tls;
A
aliguori 已提交
101 102 103
        vnc_write_u8(vs, 1); /* Accept auth */
        vnc_flush(vs);

104 105 106 107 108 109 110 111 112 113 114
        if (vs->ioc_tag) {
            g_source_remove(vs->ioc_tag);
            vs->ioc_tag = 0;
        }

        tls = qio_channel_tls_new_server(
            vs->ioc,
            vs->vd->tlscreds,
            vs->vd->tlsaclname,
            &err);
        if (!tls) {
115 116
            trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
                                error_get_pretty(err));
117 118
            error_free(err);
            vnc_client_error(vs);
A
aliguori 已提交
119 120 121
            return 0;
        }

122
        qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
123 124
        object_unref(OBJECT(vs->ioc));
        vs->ioc = QIO_CHANNEL(tls);
125
        trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
126 127 128 129 130 131
        vs->tls = qio_channel_tls_get_session(tls);

        qio_channel_tls_handshake(tls,
                                  vnc_tls_handshake_done,
                                  vs,
                                  NULL);
A
aliguori 已提交
132 133 134 135 136 137
    }
    return 0;
}

static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
{
138
    trace_vnc_auth_vencrypt_version(vs, (int)data[0], (int)data[1]);
A
aliguori 已提交
139 140
    if (data[0] != 0 ||
        data[1] != 2) {
141
        trace_vnc_auth_fail(vs, vs->auth, "Unsupported version", "");
A
aliguori 已提交
142 143 144 145 146 147
        vnc_write_u8(vs, 1); /* Reject version */
        vnc_flush(vs);
        vnc_client_error(vs);
    } else {
        vnc_write_u8(vs, 0); /* Accept version */
        vnc_write_u8(vs, 1); /* Number of sub-auths */
148
        vnc_write_u32(vs, vs->subauth); /* The supported auth */
A
aliguori 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        vnc_flush(vs);
        vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
    }
    return 0;
}


void start_auth_vencrypt(VncState *vs)
{
    /* Send VeNCrypt version 0.2 */
    vnc_write_u8(vs, 0);
    vnc_write_u8(vs, 2);

    vnc_read_when(vs, protocol_client_vencrypt_init, 2);
}