libssh2

Форк
0
/
userauth_kbd_packet.c 
166 строк · 6.1 Кб
1
/* Copyright (C) Xaver Loppenstedt <xaver@loppenstedt.de>
2
 * All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms,
5
 * with or without modification, are permitted provided
6
 * that the following conditions are met:
7
 *
8
 *   Redistributions of source code must retain the above
9
 *   copyright notice, this list of conditions and the
10
 *   following disclaimer.
11
 *
12
 *   Redistributions in binary form must reproduce the above
13
 *   copyright notice, this list of conditions and the following
14
 *   disclaimer in the documentation and/or other materials
15
 *   provided with the distribution.
16
 *
17
 *   Neither the name of the copyright holder nor the names
18
 *   of any other contributors may be used to endorse or
19
 *   promote products derived from this software without
20
 *   specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35
 * OF SUCH DAMAGE.
36
 *
37
 * SPDX-License-Identifier: BSD-3-Clause
38
 */
39

40
#include "libssh2_priv.h"
41
#include "userauth_kbd_packet.h"
42

43
int userauth_keyboard_interactive_decode_info_request(LIBSSH2_SESSION *session)
44
{
45
    unsigned char *language_tag;
46
    size_t language_tag_len;
47
    unsigned int i;
48
    unsigned char packet_type;
49
    uint32_t tmp_u32;
50

51
    struct string_buf decoded;
52

53
    decoded.data = session->userauth_kybd_data;
54
    decoded.dataptr = session->userauth_kybd_data;
55
    decoded.len = session->userauth_kybd_data_len;
56

57
    if(session->userauth_kybd_data_len < 17) {
58
        _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
59
                       "userauth keyboard data buffer too small "
60
                       "to get length");
61
        return -1;
62
    }
63

64
    /* byte      SSH_MSG_USERAUTH_INFO_REQUEST */
65
    _libssh2_get_byte(&decoded, &packet_type);
66

67
    /* string    name (ISO-10646 UTF-8) */
68
    if(_libssh2_copy_string(session, &decoded,
69
                            &session->userauth_kybd_auth_name,
70
                            &session->userauth_kybd_auth_name_len) == -1) {
71
        _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
72
                       "Unable to decode "
73
                       "keyboard-interactive 'name' "
74
                       "request field");
75
        return -1;
76
    }
77

78
    /* string    instruction (ISO-10646 UTF-8) */
79
    if(_libssh2_copy_string(session, &decoded,
80
                            &session->userauth_kybd_auth_instruction,
81
                            &session->userauth_kybd_auth_instruction_len)
82
       == -1) {
83
        _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
84
                       "Unable to decode "
85
                       "keyboard-interactive 'instruction' "
86
                       "request field");
87
        return -1;
88
    }
89

90
    /* string    language tag (as defined in [RFC-3066]) */
91
    if(_libssh2_get_string(&decoded, &language_tag,
92
                           &language_tag_len) == -1) {
93
        _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
94
                       "Unable to decode "
95
                       "keyboard-interactive 'language tag' "
96
                       "request field");
97
        return -1;
98
    }
99

100
    /* int       num-prompts */
101
    if(_libssh2_get_u32(&decoded, &tmp_u32) == -1 ||
102
       (session->userauth_kybd_num_prompts = tmp_u32) != tmp_u32) {
103
        _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
104
                       "Unable to decode "
105
                       "keyboard-interactive number of keyboard prompts");
106
        return -1;
107
    }
108

109
    if(session->userauth_kybd_num_prompts > 100) {
110
        _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY,
111
                       "Too many replies for "
112
                       "keyboard-interactive prompts");
113
        return -1;
114
    }
115

116
    if(session->userauth_kybd_num_prompts == 0) {
117
        return 0;
118
    }
119

120
    session->userauth_kybd_prompts =
121
        LIBSSH2_CALLOC(session,
122
                       sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) *
123
                       session->userauth_kybd_num_prompts);
124
    if(!session->userauth_kybd_prompts) {
125
        _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
126
                       "Unable to allocate memory for "
127
                       "keyboard-interactive prompts array");
128
        return -1;
129
    }
130

131
    session->userauth_kybd_responses =
132
        LIBSSH2_CALLOC(session,
133
                       sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) *
134
                       session->userauth_kybd_num_prompts);
135
    if(!session->userauth_kybd_responses) {
136
        _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
137
                       "Unable to allocate memory for "
138
                       "keyboard-interactive responses array");
139
        return -1;
140
    }
141

142
    for(i = 0; i < session->userauth_kybd_num_prompts; i++) {
143
        /* string    prompt[1] (ISO-10646 UTF-8) */
144
        if(_libssh2_copy_string(session, &decoded,
145
                                &session->userauth_kybd_prompts[i].text,
146
                                &session->userauth_kybd_prompts[i].length)
147
           == -1) {
148
            _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
149
                           "Unable to decode "
150
                           "keyboard-interactive prompt message");
151
            return -1;
152
        }
153

154
        /* boolean   echo[1] */
155
        if(_libssh2_get_boolean(&decoded,
156
                                &session->userauth_kybd_prompts[i].echo)
157
           == -1) {
158
            _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL,
159
                           "Unable to decode "
160
                           "user auth keyboard prompt echo");
161
            return -1;
162
        }
163
    }
164

165
    return 0;
166
}
167

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.