ssh

Dependents:   OS

Committer:
sPymbed
Date:
Mon Nov 25 14:24:05 2019 +0000
Revision:
0:c4152c628df5
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sPymbed 0:c4152c628df5 1 /* ssh.c
sPymbed 0:c4152c628df5 2 *
sPymbed 0:c4152c628df5 3 * Copyright (C) 2014-2016 wolfSSL Inc.
sPymbed 0:c4152c628df5 4 *
sPymbed 0:c4152c628df5 5 * This file is part of wolfSSH.
sPymbed 0:c4152c628df5 6 *
sPymbed 0:c4152c628df5 7 * wolfSSH is free software; you can redistribute it and/or modify
sPymbed 0:c4152c628df5 8 * it under the terms of the GNU General Public License as published by
sPymbed 0:c4152c628df5 9 * the Free Software Foundation; either version 3 of the License, or
sPymbed 0:c4152c628df5 10 * (at your option) any later version.
sPymbed 0:c4152c628df5 11 *
sPymbed 0:c4152c628df5 12 * wolfSSH is distributed in the hope that it will be useful,
sPymbed 0:c4152c628df5 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sPymbed 0:c4152c628df5 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
sPymbed 0:c4152c628df5 15 * GNU General Public License for more details.
sPymbed 0:c4152c628df5 16 *
sPymbed 0:c4152c628df5 17 * You should have received a copy of the GNU General Public License
sPymbed 0:c4152c628df5 18 * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
sPymbed 0:c4152c628df5 19 */
sPymbed 0:c4152c628df5 20
sPymbed 0:c4152c628df5 21
sPymbed 0:c4152c628df5 22 /*
sPymbed 0:c4152c628df5 23 * The ssh module contains the public API for wolfSSH.
sPymbed 0:c4152c628df5 24 */
sPymbed 0:c4152c628df5 25
sPymbed 0:c4152c628df5 26
sPymbed 0:c4152c628df5 27 #ifdef HAVE_CONFIG_H
sPymbed 0:c4152c628df5 28 #include <config.h>
sPymbed 0:c4152c628df5 29 #endif
sPymbed 0:c4152c628df5 30
sPymbed 0:c4152c628df5 31 #include <wolfssh/ssh.h>
sPymbed 0:c4152c628df5 32 #include <wolfssh/internal.h>
sPymbed 0:c4152c628df5 33 #include <wolfssh/log.h>
sPymbed 0:c4152c628df5 34 #include <wolfcrypt/wc_port.h>
sPymbed 0:c4152c628df5 35 //#include "esp_log.h"
sPymbed 0:c4152c628df5 36
sPymbed 0:c4152c628df5 37 #ifdef NO_INLINE
sPymbed 0:c4152c628df5 38 #include <wolfssh/misc.h>
sPymbed 0:c4152c628df5 39 #else
sPymbed 0:c4152c628df5 40 #define WOLFSSH_MISC_INCLUDED
sPymbed 0:c4152c628df5 41 #include "src/misc.c"
sPymbed 0:c4152c628df5 42 #endif
sPymbed 0:c4152c628df5 43
sPymbed 0:c4152c628df5 44
sPymbed 0:c4152c628df5 45 int wolfSSH_Init(void)
sPymbed 0:c4152c628df5 46 {
sPymbed 0:c4152c628df5 47 int ret = WS_SUCCESS;
sPymbed 0:c4152c628df5 48
sPymbed 0:c4152c628df5 49 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_Init()");
sPymbed 0:c4152c628df5 50 if (wolfCrypt_Init() != 0)
sPymbed 0:c4152c628df5 51 ret = WS_CRYPTO_FAILED;
sPymbed 0:c4152c628df5 52
sPymbed 0:c4152c628df5 53 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_Init(), returning %d", ret);
sPymbed 0:c4152c628df5 54 return ret;
sPymbed 0:c4152c628df5 55 }
sPymbed 0:c4152c628df5 56
sPymbed 0:c4152c628df5 57
sPymbed 0:c4152c628df5 58 int wolfSSH_Cleanup(void)
sPymbed 0:c4152c628df5 59 {
sPymbed 0:c4152c628df5 60 int ret = WS_SUCCESS;
sPymbed 0:c4152c628df5 61
sPymbed 0:c4152c628df5 62 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_Cleanup()");
sPymbed 0:c4152c628df5 63
sPymbed 0:c4152c628df5 64 if (wolfCrypt_Cleanup() != 0)
sPymbed 0:c4152c628df5 65 ret = WS_CRYPTO_FAILED;
sPymbed 0:c4152c628df5 66
sPymbed 0:c4152c628df5 67 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_Cleanup(), returning %d", ret);
sPymbed 0:c4152c628df5 68 return ret;
sPymbed 0:c4152c628df5 69 }
sPymbed 0:c4152c628df5 70
sPymbed 0:c4152c628df5 71
sPymbed 0:c4152c628df5 72 WOLFSSH_CTX* wolfSSH_CTX_new(byte side, void* heap)
sPymbed 0:c4152c628df5 73 {
sPymbed 0:c4152c628df5 74 WOLFSSH_CTX* ctx;
sPymbed 0:c4152c628df5 75
sPymbed 0:c4152c628df5 76 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_CTX_new()");
sPymbed 0:c4152c628df5 77
sPymbed 0:c4152c628df5 78 if (side != WOLFSSH_ENDPOINT_SERVER && side != WOLFSSH_ENDPOINT_CLIENT) {
sPymbed 0:c4152c628df5 79 WLOG(WS_LOG_DEBUG, "Invalid endpoint type");
sPymbed 0:c4152c628df5 80 return NULL;
sPymbed 0:c4152c628df5 81 }
sPymbed 0:c4152c628df5 82
sPymbed 0:c4152c628df5 83 ctx = (WOLFSSH_CTX*)WMALLOC(sizeof(WOLFSSH_CTX), heap, DYNTYPE_CTX);
sPymbed 0:c4152c628df5 84 ctx = CtxInit(ctx, side, heap);
sPymbed 0:c4152c628df5 85
sPymbed 0:c4152c628df5 86 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_CTX_new(), ctx = %p", ctx);
sPymbed 0:c4152c628df5 87
sPymbed 0:c4152c628df5 88 return ctx;
sPymbed 0:c4152c628df5 89 }
sPymbed 0:c4152c628df5 90
sPymbed 0:c4152c628df5 91
sPymbed 0:c4152c628df5 92 void wolfSSH_CTX_free(WOLFSSH_CTX* ctx)
sPymbed 0:c4152c628df5 93 {
sPymbed 0:c4152c628df5 94 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_CTX_free()");
sPymbed 0:c4152c628df5 95
sPymbed 0:c4152c628df5 96 if (ctx) {
sPymbed 0:c4152c628df5 97 CtxResourceFree(ctx);
sPymbed 0:c4152c628df5 98 WFREE(ctx, ctx->heap, DYNTYPE_CTX);
sPymbed 0:c4152c628df5 99 }
sPymbed 0:c4152c628df5 100 }
sPymbed 0:c4152c628df5 101
sPymbed 0:c4152c628df5 102
sPymbed 0:c4152c628df5 103 WOLFSSH* wolfSSH_new(WOLFSSH_CTX* ctx)
sPymbed 0:c4152c628df5 104 {
sPymbed 0:c4152c628df5 105 WOLFSSH* ssh;
sPymbed 0:c4152c628df5 106 void* heap = NULL;
sPymbed 0:c4152c628df5 107
sPymbed 0:c4152c628df5 108 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_new()");
sPymbed 0:c4152c628df5 109 //ESP_LOGI("WOLFSSH", "Entering wolfSSH_new()");
sPymbed 0:c4152c628df5 110
sPymbed 0:c4152c628df5 111 if (ctx)
sPymbed 0:c4152c628df5 112 heap = ctx->heap;
sPymbed 0:c4152c628df5 113 else {
sPymbed 0:c4152c628df5 114 WLOG(WS_LOG_ERROR, "Trying to init a wolfSSH w/o wolfSSH_CTX");
sPymbed 0:c4152c628df5 115 return NULL;
sPymbed 0:c4152c628df5 116 }
sPymbed 0:c4152c628df5 117
sPymbed 0:c4152c628df5 118 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_new()");
sPymbed 0:c4152c628df5 119 //ESP_LOGI("WOLFSSH", "ctx check ok, now malloc WOLFSSH");
sPymbed 0:c4152c628df5 120
sPymbed 0:c4152c628df5 121 ssh = (WOLFSSH*)WMALLOC(sizeof(WOLFSSH), heap, DYNTYPE_SSH);
sPymbed 0:c4152c628df5 122 //ESP_LOGI("WOLFSSH", "malloc done, SSH Initialization...");
sPymbed 0:c4152c628df5 123 //if(ssh == NULL) ESP_LOGE("WOLFSSH", "ssh is NULL, passing to SshInit() anyway...");
sPymbed 0:c4152c628df5 124 ssh = SshInit(ssh, ctx);
sPymbed 0:c4152c628df5 125 //ESP_LOGI("WOLFSSH", "SshInit OK");
sPymbed 0:c4152c628df5 126 //if(ssh == NULL) ESP_LOGE("WOLFSSH", "ssh is NULL after SshInit()");
sPymbed 0:c4152c628df5 127
sPymbed 0:c4152c628df5 128 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_new(), ssh = %p", ssh);
sPymbed 0:c4152c628df5 129 //ESP_LOGI("WOLFSSH", "Leaving wolfSSH_new()");
sPymbed 0:c4152c628df5 130
sPymbed 0:c4152c628df5 131 return ssh;
sPymbed 0:c4152c628df5 132 }
sPymbed 0:c4152c628df5 133
sPymbed 0:c4152c628df5 134
sPymbed 0:c4152c628df5 135 void wolfSSH_free(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 136 {
sPymbed 0:c4152c628df5 137 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_free()");
sPymbed 0:c4152c628df5 138
sPymbed 0:c4152c628df5 139 if (ssh) {
sPymbed 0:c4152c628df5 140 void* heap = ssh->ctx ? ssh->ctx->heap : NULL;
sPymbed 0:c4152c628df5 141 SshResourceFree(ssh, heap);
sPymbed 0:c4152c628df5 142 WFREE(ssh, heap, DYNTYPE_SSH);
sPymbed 0:c4152c628df5 143 }
sPymbed 0:c4152c628df5 144 }
sPymbed 0:c4152c628df5 145
sPymbed 0:c4152c628df5 146
sPymbed 0:c4152c628df5 147 int wolfSSH_set_fd(WOLFSSH* ssh, int fd)
sPymbed 0:c4152c628df5 148 {
sPymbed 0:c4152c628df5 149 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_set_fd()");
sPymbed 0:c4152c628df5 150
sPymbed 0:c4152c628df5 151 if (ssh) {
sPymbed 0:c4152c628df5 152 ssh->rfd = fd;
sPymbed 0:c4152c628df5 153 ssh->wfd = fd;
sPymbed 0:c4152c628df5 154
sPymbed 0:c4152c628df5 155 ssh->ioReadCtx = &ssh->rfd;
sPymbed 0:c4152c628df5 156 ssh->ioWriteCtx = &ssh->wfd;
sPymbed 0:c4152c628df5 157
sPymbed 0:c4152c628df5 158 return WS_SUCCESS;
sPymbed 0:c4152c628df5 159 }
sPymbed 0:c4152c628df5 160 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 161 }
sPymbed 0:c4152c628df5 162
sPymbed 0:c4152c628df5 163
sPymbed 0:c4152c628df5 164 int wolfSSH_get_fd(const WOLFSSH* ssh)
sPymbed 0:c4152c628df5 165 {
sPymbed 0:c4152c628df5 166 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_get_fd()");
sPymbed 0:c4152c628df5 167
sPymbed 0:c4152c628df5 168 if (ssh)
sPymbed 0:c4152c628df5 169 return ssh->rfd;
sPymbed 0:c4152c628df5 170
sPymbed 0:c4152c628df5 171 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 172 }
sPymbed 0:c4152c628df5 173
sPymbed 0:c4152c628df5 174
sPymbed 0:c4152c628df5 175 int wolfSSH_SetHighwater(WOLFSSH* ssh, word32 highwater)
sPymbed 0:c4152c628df5 176 {
sPymbed 0:c4152c628df5 177 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_SetHighwater()");
sPymbed 0:c4152c628df5 178
sPymbed 0:c4152c628df5 179 if (ssh) {
sPymbed 0:c4152c628df5 180 ssh->highwaterMark = highwater;
sPymbed 0:c4152c628df5 181
sPymbed 0:c4152c628df5 182 return WS_SUCCESS;
sPymbed 0:c4152c628df5 183 }
sPymbed 0:c4152c628df5 184
sPymbed 0:c4152c628df5 185 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 186 }
sPymbed 0:c4152c628df5 187
sPymbed 0:c4152c628df5 188
sPymbed 0:c4152c628df5 189 word32 wolfSSH_GetHighwater(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 190 {
sPymbed 0:c4152c628df5 191 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_GetHighwater()");
sPymbed 0:c4152c628df5 192
sPymbed 0:c4152c628df5 193 if (ssh)
sPymbed 0:c4152c628df5 194 return ssh->highwaterMark;
sPymbed 0:c4152c628df5 195
sPymbed 0:c4152c628df5 196 return 0;
sPymbed 0:c4152c628df5 197 }
sPymbed 0:c4152c628df5 198
sPymbed 0:c4152c628df5 199
sPymbed 0:c4152c628df5 200 void wolfSSH_SetHighwaterCb(WOLFSSH_CTX* ctx, word32 highwater,
sPymbed 0:c4152c628df5 201 WS_CallbackHighwater cb)
sPymbed 0:c4152c628df5 202 {
sPymbed 0:c4152c628df5 203 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_SetHighwaterCb()");
sPymbed 0:c4152c628df5 204
sPymbed 0:c4152c628df5 205 if (ctx) {
sPymbed 0:c4152c628df5 206 ctx->highwaterMark = highwater;
sPymbed 0:c4152c628df5 207 ctx->highwaterCb = cb;
sPymbed 0:c4152c628df5 208 }
sPymbed 0:c4152c628df5 209 }
sPymbed 0:c4152c628df5 210
sPymbed 0:c4152c628df5 211
sPymbed 0:c4152c628df5 212 void wolfSSH_SetHighwaterCtx(WOLFSSH* ssh, void* ctx)
sPymbed 0:c4152c628df5 213 {
sPymbed 0:c4152c628df5 214 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_SetHighwaterCtx()");
sPymbed 0:c4152c628df5 215
sPymbed 0:c4152c628df5 216 if (ssh)
sPymbed 0:c4152c628df5 217 ssh->highwaterCtx = ctx;
sPymbed 0:c4152c628df5 218 }
sPymbed 0:c4152c628df5 219
sPymbed 0:c4152c628df5 220
sPymbed 0:c4152c628df5 221 void* wolfSSH_GetHighwaterCtx(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 222 {
sPymbed 0:c4152c628df5 223 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_GetHighwaterCtx()");
sPymbed 0:c4152c628df5 224
sPymbed 0:c4152c628df5 225 if (ssh)
sPymbed 0:c4152c628df5 226 return ssh->highwaterCtx;
sPymbed 0:c4152c628df5 227
sPymbed 0:c4152c628df5 228 return NULL;
sPymbed 0:c4152c628df5 229 }
sPymbed 0:c4152c628df5 230
sPymbed 0:c4152c628df5 231
sPymbed 0:c4152c628df5 232 int wolfSSH_get_error(const WOLFSSH* ssh)
sPymbed 0:c4152c628df5 233 {
sPymbed 0:c4152c628df5 234 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_get_error()");
sPymbed 0:c4152c628df5 235
sPymbed 0:c4152c628df5 236 if (ssh)
sPymbed 0:c4152c628df5 237 return ssh->error;
sPymbed 0:c4152c628df5 238
sPymbed 0:c4152c628df5 239 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 240 }
sPymbed 0:c4152c628df5 241
sPymbed 0:c4152c628df5 242
sPymbed 0:c4152c628df5 243 const char* wolfSSH_get_error_name(const WOLFSSH* ssh)
sPymbed 0:c4152c628df5 244 {
sPymbed 0:c4152c628df5 245 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_get_error_name()");
sPymbed 0:c4152c628df5 246
sPymbed 0:c4152c628df5 247 if (ssh)
sPymbed 0:c4152c628df5 248 return GetErrorString(ssh->error);
sPymbed 0:c4152c628df5 249
sPymbed 0:c4152c628df5 250 return NULL;
sPymbed 0:c4152c628df5 251 }
sPymbed 0:c4152c628df5 252
sPymbed 0:c4152c628df5 253
sPymbed 0:c4152c628df5 254 const char acceptError[] = "accept error: %s, %d";
sPymbed 0:c4152c628df5 255 const char acceptState[] = "accept state: %s";
sPymbed 0:c4152c628df5 256
sPymbed 0:c4152c628df5 257
sPymbed 0:c4152c628df5 258 int wolfSSH_accept(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 259 {
sPymbed 0:c4152c628df5 260 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_accept()");
sPymbed 0:c4152c628df5 261 //ESP_LOGI("WOLFSSH", "Entering wolfSSH_accept()");
sPymbed 0:c4152c628df5 262
sPymbed 0:c4152c628df5 263 if (ssh == NULL)
sPymbed 0:c4152c628df5 264 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 265
sPymbed 0:c4152c628df5 266 //ESP_LOGI("WOLFSSH", "ssh is not NULL");
sPymbed 0:c4152c628df5 267
sPymbed 0:c4152c628df5 268 switch (ssh->acceptState) {
sPymbed 0:c4152c628df5 269
sPymbed 0:c4152c628df5 270 case ACCEPT_BEGIN:
sPymbed 0:c4152c628df5 271 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_BEGIN");
sPymbed 0:c4152c628df5 272 if ( (ssh->error = SendProtoId(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 273 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 274 // non-blocking tcp
sPymbed 0:c4152c628df5 275 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 276 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 277 }
sPymbed 0:c4152c628df5 278 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 279 WLOG(WS_LOG_DEBUG, acceptError, "BEGIN", ssh->error);
sPymbed 0:c4152c628df5 280 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 281 }
sPymbed 0:c4152c628df5 282 ssh->acceptState = ACCEPT_SERVER_VERSION_SENT;
sPymbed 0:c4152c628df5 283 WLOG(WS_LOG_DEBUG, acceptState, "SERVER_VERSION_SENT");
sPymbed 0:c4152c628df5 284 FALL_THROUGH;
sPymbed 0:c4152c628df5 285
sPymbed 0:c4152c628df5 286 case ACCEPT_SERVER_VERSION_SENT:
sPymbed 0:c4152c628df5 287 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_SERVER_VERSION_SENT");
sPymbed 0:c4152c628df5 288 while (ssh->clientState < CLIENT_VERSION_DONE) {
sPymbed 0:c4152c628df5 289 if ( (ssh->error = DoProtoId(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 290 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 291 // non-blocking tcp
sPymbed 0:c4152c628df5 292 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 293 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 294 }
sPymbed 0:c4152c628df5 295 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 296 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 297 "SERVER_VERSION_SENT", ssh->error);
sPymbed 0:c4152c628df5 298 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 299 }
sPymbed 0:c4152c628df5 300 }
sPymbed 0:c4152c628df5 301 ssh->acceptState = ACCEPT_CLIENT_VERSION_DONE;
sPymbed 0:c4152c628df5 302 WLOG(WS_LOG_DEBUG, acceptState, "CLIENT_VERSION_DONE");
sPymbed 0:c4152c628df5 303 FALL_THROUGH;
sPymbed 0:c4152c628df5 304
sPymbed 0:c4152c628df5 305 case ACCEPT_CLIENT_VERSION_DONE:
sPymbed 0:c4152c628df5 306 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_CLIENT_VERSION_DONE");
sPymbed 0:c4152c628df5 307 if ( (ssh->error = SendKexInit(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 308 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 309 // non-blocking tcp
sPymbed 0:c4152c628df5 310 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 311 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 312 }
sPymbed 0:c4152c628df5 313 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 314 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 315 "CLIENT_VERSION_DONE", ssh->error);
sPymbed 0:c4152c628df5 316 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 317 }
sPymbed 0:c4152c628df5 318 ssh->acceptState = ACCEPT_SERVER_KEXINIT_SENT;
sPymbed 0:c4152c628df5 319 WLOG(WS_LOG_DEBUG, acceptState, "SERVER_KEXINIT_SENT");
sPymbed 0:c4152c628df5 320 FALL_THROUGH;
sPymbed 0:c4152c628df5 321
sPymbed 0:c4152c628df5 322 case ACCEPT_SERVER_KEXINIT_SENT:
sPymbed 0:c4152c628df5 323 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_SERVER_KEXINIT_SENT");
sPymbed 0:c4152c628df5 324 while (ssh->isKeying) {
sPymbed 0:c4152c628df5 325 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 326 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 327 // non-blocking tcp
sPymbed 0:c4152c628df5 328 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 329 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 330 }
sPymbed 0:c4152c628df5 331 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 332 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 333 "SERVER_KEXINIT_SENT", ssh->error);
sPymbed 0:c4152c628df5 334 //ESP_LOGE("WOLFSSH", "Accept error: SERVER_KEXINIT_SENT, %d", ssh->error);
sPymbed 0:c4152c628df5 335 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 336 }
sPymbed 0:c4152c628df5 337 }
sPymbed 0:c4152c628df5 338 ssh->acceptState = ACCEPT_KEYED;
sPymbed 0:c4152c628df5 339 WLOG(WS_LOG_DEBUG, acceptState, "KEYED");
sPymbed 0:c4152c628df5 340 FALL_THROUGH;
sPymbed 0:c4152c628df5 341
sPymbed 0:c4152c628df5 342 case ACCEPT_KEYED:
sPymbed 0:c4152c628df5 343 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_KEYED");
sPymbed 0:c4152c628df5 344 while (ssh->clientState < CLIENT_USERAUTH_REQUEST_DONE) {
sPymbed 0:c4152c628df5 345 if ( (ssh->error = DoReceive(ssh)) < 0) {
sPymbed 0:c4152c628df5 346 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 347 // non-blocking tcp
sPymbed 0:c4152c628df5 348 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 349 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 350 }
sPymbed 0:c4152c628df5 351 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 352 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 353 "KEYED", ssh->error);
sPymbed 0:c4152c628df5 354 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 355 }
sPymbed 0:c4152c628df5 356 }
sPymbed 0:c4152c628df5 357 ssh->acceptState = ACCEPT_CLIENT_USERAUTH_REQUEST_DONE;
sPymbed 0:c4152c628df5 358 WLOG(WS_LOG_DEBUG, acceptState, "CLIENT_USERAUTH_REQUEST_DONE");
sPymbed 0:c4152c628df5 359 FALL_THROUGH;
sPymbed 0:c4152c628df5 360
sPymbed 0:c4152c628df5 361 case ACCEPT_CLIENT_USERAUTH_REQUEST_DONE:
sPymbed 0:c4152c628df5 362 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_CLIENT_USERAUTH_REQUEST_DONE");
sPymbed 0:c4152c628df5 363 if ( (ssh->error = SendServiceAccept(ssh, ID_SERVICE_USERAUTH)) <
sPymbed 0:c4152c628df5 364 WS_SUCCESS) {
sPymbed 0:c4152c628df5 365 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 366 "CLIENT_USERAUTH_REQUEST_DONE", ssh->error);
sPymbed 0:c4152c628df5 367 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 368 }
sPymbed 0:c4152c628df5 369 ssh->acceptState = ACCEPT_SERVER_USERAUTH_ACCEPT_SENT;
sPymbed 0:c4152c628df5 370 WLOG(WS_LOG_DEBUG, acceptState,
sPymbed 0:c4152c628df5 371 "ACCEPT_SERVER_USERAUTH_ACCEPT_SENT");
sPymbed 0:c4152c628df5 372 FALL_THROUGH;
sPymbed 0:c4152c628df5 373
sPymbed 0:c4152c628df5 374 case ACCEPT_SERVER_USERAUTH_ACCEPT_SENT:
sPymbed 0:c4152c628df5 375 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_SERVER_USERAUTH_ACCEPT_SENT");
sPymbed 0:c4152c628df5 376 while (ssh->clientState < CLIENT_USERAUTH_DONE) {
sPymbed 0:c4152c628df5 377 if ( (ssh->error = DoReceive(ssh)) < 0) {
sPymbed 0:c4152c628df5 378 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 379 // non-blocking tcp
sPymbed 0:c4152c628df5 380 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 381 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 382 }
sPymbed 0:c4152c628df5 383 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 384 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 385 "SERVER_USERAUTH_ACCEPT_SENT", ssh->error);
sPymbed 0:c4152c628df5 386 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 387 }
sPymbed 0:c4152c628df5 388 }
sPymbed 0:c4152c628df5 389 ssh->acceptState = ACCEPT_CLIENT_USERAUTH_DONE;
sPymbed 0:c4152c628df5 390 WLOG(WS_LOG_DEBUG, acceptState, "CLIENT_USERAUTH_DONE");
sPymbed 0:c4152c628df5 391 FALL_THROUGH;
sPymbed 0:c4152c628df5 392
sPymbed 0:c4152c628df5 393 case ACCEPT_CLIENT_USERAUTH_DONE:
sPymbed 0:c4152c628df5 394 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_CLIENT_USERAUTH_DONE");
sPymbed 0:c4152c628df5 395 if ( (ssh->error = SendUserAuthSuccess(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 396 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 397 "CLIENT_USERAUTH_DONE", ssh->error);
sPymbed 0:c4152c628df5 398 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 399 }
sPymbed 0:c4152c628df5 400 ssh->acceptState = ACCEPT_SERVER_USERAUTH_SENT;
sPymbed 0:c4152c628df5 401 WLOG(WS_LOG_DEBUG, acceptState, "SERVER_USERAUTH_SENT");
sPymbed 0:c4152c628df5 402 FALL_THROUGH;
sPymbed 0:c4152c628df5 403
sPymbed 0:c4152c628df5 404 case ACCEPT_SERVER_USERAUTH_SENT:
sPymbed 0:c4152c628df5 405 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_SERVER_USERAUTH_SENT");
sPymbed 0:c4152c628df5 406 while (ssh->clientState < CLIENT_DONE) {
sPymbed 0:c4152c628df5 407 if ( (ssh->error = DoReceive(ssh)) < 0) {
sPymbed 0:c4152c628df5 408 while(ssh->error == WS_WANT_READ) {
sPymbed 0:c4152c628df5 409 // non-blocking tcp
sPymbed 0:c4152c628df5 410 //ESP_LOGW("WOLFSSH", "WANT_READ: looping");
sPymbed 0:c4152c628df5 411 ssh->error = DoReceive(ssh);
sPymbed 0:c4152c628df5 412 }
sPymbed 0:c4152c628df5 413 if(ssh->error == WS_SUCCESS) break;
sPymbed 0:c4152c628df5 414 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 415 "SERVER_USERAUTH_SENT", ssh->error);
sPymbed 0:c4152c628df5 416 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 417 }
sPymbed 0:c4152c628df5 418 }
sPymbed 0:c4152c628df5 419 ssh->acceptState = ACCEPT_CLIENT_CHANNEL_REQUEST_DONE;
sPymbed 0:c4152c628df5 420 WLOG(WS_LOG_DEBUG, acceptState, "CLIENT_CHANNEL_REQUEST_DONE");
sPymbed 0:c4152c628df5 421 FALL_THROUGH;
sPymbed 0:c4152c628df5 422
sPymbed 0:c4152c628df5 423 case ACCEPT_CLIENT_CHANNEL_REQUEST_DONE:
sPymbed 0:c4152c628df5 424 //ESP_LOGI("WOLFSSH", "acceptState = ACCEPT_CLIENT_CHANNEL_REQUEST_DONE");
sPymbed 0:c4152c628df5 425 if ( (ssh->error = SendChannelOpenConf(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 426 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 427 "CLIENT_CHANNEL_REQUEST_DONE", ssh->error);
sPymbed 0:c4152c628df5 428 //ESP_LOGE("WOLFSSH", "SendChannelOpenConf failed: %d", ssh->error);
sPymbed 0:c4152c628df5 429 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 430 }
sPymbed 0:c4152c628df5 431 ssh->acceptState = ACCEPT_SERVER_CHANNEL_ACCEPT_SENT;
sPymbed 0:c4152c628df5 432 WLOG(WS_LOG_DEBUG, acceptState, "SERVER_CHANNEL_ACCEPT_SENT");
sPymbed 0:c4152c628df5 433 }
sPymbed 0:c4152c628df5 434
sPymbed 0:c4152c628df5 435 return WS_SUCCESS;
sPymbed 0:c4152c628df5 436 }
sPymbed 0:c4152c628df5 437
sPymbed 0:c4152c628df5 438
sPymbed 0:c4152c628df5 439 const char connectError[] = "connect error: %s, %d";
sPymbed 0:c4152c628df5 440 const char connectState[] = "connect state: %s";
sPymbed 0:c4152c628df5 441
sPymbed 0:c4152c628df5 442
sPymbed 0:c4152c628df5 443 int wolfSSH_connect(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 444 {
sPymbed 0:c4152c628df5 445 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_connect()");
sPymbed 0:c4152c628df5 446
sPymbed 0:c4152c628df5 447 if (ssh == NULL)
sPymbed 0:c4152c628df5 448 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 449
sPymbed 0:c4152c628df5 450 switch (ssh->connectState) {
sPymbed 0:c4152c628df5 451
sPymbed 0:c4152c628df5 452 case CONNECT_BEGIN:
sPymbed 0:c4152c628df5 453 if ( (ssh->error = SendProtoId(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 454 WLOG(WS_LOG_DEBUG, connectError, "BEGIN", ssh->error);
sPymbed 0:c4152c628df5 455 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 456 }
sPymbed 0:c4152c628df5 457 ssh->connectState = CONNECT_CLIENT_VERSION_SENT;
sPymbed 0:c4152c628df5 458 WLOG(WS_LOG_DEBUG, connectState, "CLIENT_VERSION_SENT");
sPymbed 0:c4152c628df5 459 FALL_THROUGH;
sPymbed 0:c4152c628df5 460
sPymbed 0:c4152c628df5 461 case CONNECT_CLIENT_VERSION_SENT:
sPymbed 0:c4152c628df5 462 while (ssh->serverState < SERVER_VERSION_DONE) {
sPymbed 0:c4152c628df5 463 if ( (ssh->error = DoProtoId(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 464 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 465 "CLIENT_VERSION_SENT", ssh->error);
sPymbed 0:c4152c628df5 466 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 467 }
sPymbed 0:c4152c628df5 468 }
sPymbed 0:c4152c628df5 469 ssh->connectState = CONNECT_SERVER_VERSION_DONE;
sPymbed 0:c4152c628df5 470 WLOG(WS_LOG_DEBUG, connectState, "SERVER_VERSION_DONE");
sPymbed 0:c4152c628df5 471 FALL_THROUGH;
sPymbed 0:c4152c628df5 472
sPymbed 0:c4152c628df5 473 case CONNECT_SERVER_VERSION_DONE:
sPymbed 0:c4152c628df5 474 if ( (ssh->error = SendKexInit(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 475 WLOG(WS_LOG_DEBUG, acceptError,
sPymbed 0:c4152c628df5 476 "SERVER_VERSION_DONE", ssh->error);
sPymbed 0:c4152c628df5 477 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 478 }
sPymbed 0:c4152c628df5 479 ssh->connectState = CONNECT_CLIENT_KEXINIT_SENT;
sPymbed 0:c4152c628df5 480 WLOG(WS_LOG_DEBUG, connectState, "CLIENT_KEXINIT_SENT");
sPymbed 0:c4152c628df5 481 FALL_THROUGH;
sPymbed 0:c4152c628df5 482
sPymbed 0:c4152c628df5 483 case CONNECT_CLIENT_KEXINIT_SENT:
sPymbed 0:c4152c628df5 484 while (ssh->serverState < SERVER_KEXINIT_DONE) {
sPymbed 0:c4152c628df5 485 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 486 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 487 "CLIENT_KEXINIT_SENT", ssh->error);
sPymbed 0:c4152c628df5 488 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 489 }
sPymbed 0:c4152c628df5 490 }
sPymbed 0:c4152c628df5 491 ssh->connectState = CONNECT_SERVER_KEXINIT_DONE;
sPymbed 0:c4152c628df5 492 WLOG(WS_LOG_DEBUG, connectState, "SERVER_KEXINIT_DONE");
sPymbed 0:c4152c628df5 493 FALL_THROUGH;
sPymbed 0:c4152c628df5 494
sPymbed 0:c4152c628df5 495 case CONNECT_SERVER_KEXINIT_DONE:
sPymbed 0:c4152c628df5 496 if (ssh->handshake->kexId == ID_DH_GEX_SHA256)
sPymbed 0:c4152c628df5 497 ssh->error = SendKexDhGexRequest(ssh);
sPymbed 0:c4152c628df5 498 else
sPymbed 0:c4152c628df5 499 ssh->error = SendKexDhInit(ssh);
sPymbed 0:c4152c628df5 500 if (ssh->error < WS_SUCCESS) {
sPymbed 0:c4152c628df5 501 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 502 "SERVER_KEXINIT_DONE", ssh->error);
sPymbed 0:c4152c628df5 503 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 504 }
sPymbed 0:c4152c628df5 505 ssh->connectState = CONNECT_CLIENT_KEXDH_INIT_SENT;
sPymbed 0:c4152c628df5 506 WLOG(WS_LOG_DEBUG, connectState, "CLIENT_KEXDH_INIT_SENT");
sPymbed 0:c4152c628df5 507 FALL_THROUGH;
sPymbed 0:c4152c628df5 508
sPymbed 0:c4152c628df5 509 case CONNECT_CLIENT_KEXDH_INIT_SENT:
sPymbed 0:c4152c628df5 510 while (ssh->isKeying) {
sPymbed 0:c4152c628df5 511 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 512 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 513 "CLIENT_KEXDH_INIT_SENT", ssh->error);
sPymbed 0:c4152c628df5 514 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 515 }
sPymbed 0:c4152c628df5 516 }
sPymbed 0:c4152c628df5 517 ssh->connectState = CONNECT_KEYED;
sPymbed 0:c4152c628df5 518 WLOG(WS_LOG_DEBUG, connectState, "KEYED");
sPymbed 0:c4152c628df5 519 FALL_THROUGH;
sPymbed 0:c4152c628df5 520
sPymbed 0:c4152c628df5 521 case CONNECT_KEYED:
sPymbed 0:c4152c628df5 522 if ( (ssh->error = SendServiceRequest(ssh, ID_SERVICE_USERAUTH)) <
sPymbed 0:c4152c628df5 523 WS_SUCCESS) {
sPymbed 0:c4152c628df5 524 WLOG(WS_LOG_DEBUG, connectError, "KEYED", ssh->error);
sPymbed 0:c4152c628df5 525 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 526 }
sPymbed 0:c4152c628df5 527 ssh->connectState = CONNECT_CLIENT_USERAUTH_REQUEST_SENT;
sPymbed 0:c4152c628df5 528 WLOG(WS_LOG_DEBUG, connectState, "CLIENT_USERAUTH_REQUEST_SENT");
sPymbed 0:c4152c628df5 529 FALL_THROUGH;
sPymbed 0:c4152c628df5 530
sPymbed 0:c4152c628df5 531 case CONNECT_CLIENT_USERAUTH_REQUEST_SENT:
sPymbed 0:c4152c628df5 532 while (ssh->serverState < SERVER_USERAUTH_REQUEST_DONE) {
sPymbed 0:c4152c628df5 533 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 534 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 535 "CLIENT_USERAUTH_REQUEST_SENT", ssh->error);
sPymbed 0:c4152c628df5 536 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 537 }
sPymbed 0:c4152c628df5 538 }
sPymbed 0:c4152c628df5 539 ssh->connectState = CONNECT_SERVER_USERAUTH_REQUEST_DONE;
sPymbed 0:c4152c628df5 540 WLOG(WS_LOG_DEBUG, connectState, "SERVER_USERAUTH_REQUEST_DONE");
sPymbed 0:c4152c628df5 541 FALL_THROUGH;
sPymbed 0:c4152c628df5 542
sPymbed 0:c4152c628df5 543 case CONNECT_SERVER_USERAUTH_REQUEST_DONE:
sPymbed 0:c4152c628df5 544 if ( (ssh->error = SendUserAuthRequest(ssh, ID_NONE)) <
sPymbed 0:c4152c628df5 545 WS_SUCCESS) {
sPymbed 0:c4152c628df5 546 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 547 "SERVER_USERAUTH_REQUEST_DONE", ssh->error);
sPymbed 0:c4152c628df5 548 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 549 }
sPymbed 0:c4152c628df5 550 ssh->connectState = CONNECT_CLIENT_USERAUTH_SENT;
sPymbed 0:c4152c628df5 551 WLOG(WS_LOG_DEBUG, connectState, "CLIENT_USERAUTH_SENT");
sPymbed 0:c4152c628df5 552 FALL_THROUGH;
sPymbed 0:c4152c628df5 553
sPymbed 0:c4152c628df5 554 case CONNECT_CLIENT_USERAUTH_SENT:
sPymbed 0:c4152c628df5 555 while (ssh->serverState < SERVER_USERAUTH_ACCEPT_DONE) {
sPymbed 0:c4152c628df5 556 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 557 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 558 "CLIENT_USERAUTH_SENT", ssh->error);
sPymbed 0:c4152c628df5 559 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 560 }
sPymbed 0:c4152c628df5 561 }
sPymbed 0:c4152c628df5 562 ssh->connectState = CONNECT_SERVER_USERAUTH_ACCEPT_DONE;
sPymbed 0:c4152c628df5 563 WLOG(WS_LOG_DEBUG, connectState, "SERVER_USERAUTH_ACCEPT_DONE");
sPymbed 0:c4152c628df5 564 FALL_THROUGH;
sPymbed 0:c4152c628df5 565
sPymbed 0:c4152c628df5 566 case CONNECT_SERVER_USERAUTH_ACCEPT_DONE:
sPymbed 0:c4152c628df5 567 if ( (ssh->error = SendChannelOpenSession(ssh, DEFAULT_WINDOW_SZ,
sPymbed 0:c4152c628df5 568 DEFAULT_MAX_PACKET_SZ)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 569 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 570 "SERVER_USERAUTH_ACCEPT_DONE", ssh->error);
sPymbed 0:c4152c628df5 571 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 572 }
sPymbed 0:c4152c628df5 573 ssh->connectState = CONNECT_CLIENT_CHANNEL_OPEN_SESSION_SENT;
sPymbed 0:c4152c628df5 574 WLOG(WS_LOG_DEBUG, connectState,
sPymbed 0:c4152c628df5 575 "CLIENT_CHANNEL_OPEN_SESSION_SENT");
sPymbed 0:c4152c628df5 576 FALL_THROUGH;
sPymbed 0:c4152c628df5 577
sPymbed 0:c4152c628df5 578 case CONNECT_CLIENT_CHANNEL_OPEN_SESSION_SENT:
sPymbed 0:c4152c628df5 579 while (ssh->serverState < SERVER_CHANNEL_OPEN_DONE) {
sPymbed 0:c4152c628df5 580 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 581 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 582 "CLIENT_CHANNEL_OPEN_SESSION_SENT", ssh->error);
sPymbed 0:c4152c628df5 583 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 584 }
sPymbed 0:c4152c628df5 585 }
sPymbed 0:c4152c628df5 586 ssh->connectState = CONNECT_SERVER_CHANNEL_OPEN_SESSION_DONE;
sPymbed 0:c4152c628df5 587 WLOG(WS_LOG_DEBUG, connectState,
sPymbed 0:c4152c628df5 588 "SERVER_CHANNEL_OPEN_SESSION_DONE");
sPymbed 0:c4152c628df5 589 FALL_THROUGH;
sPymbed 0:c4152c628df5 590
sPymbed 0:c4152c628df5 591 case CONNECT_SERVER_CHANNEL_OPEN_SESSION_DONE:
sPymbed 0:c4152c628df5 592 if ( (ssh->error = SendChannelRequestShell(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 593 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 594 "SERVER_CHANNEL_OPEN_SESSION_DONE", ssh->error);
sPymbed 0:c4152c628df5 595 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 596 }
sPymbed 0:c4152c628df5 597 ssh->connectState = CONNECT_CLIENT_CHANNEL_REQUEST_SHELL_SENT;
sPymbed 0:c4152c628df5 598 WLOG(WS_LOG_DEBUG, connectState,
sPymbed 0:c4152c628df5 599 "CLIENT_CHANNEL_REQUEST_SHELL_SENT");
sPymbed 0:c4152c628df5 600 FALL_THROUGH;
sPymbed 0:c4152c628df5 601
sPymbed 0:c4152c628df5 602 case CONNECT_CLIENT_CHANNEL_REQUEST_SHELL_SENT:
sPymbed 0:c4152c628df5 603 while (ssh->serverState < SERVER_DONE) {
sPymbed 0:c4152c628df5 604 if ( (ssh->error = DoReceive(ssh)) < WS_SUCCESS) {
sPymbed 0:c4152c628df5 605 WLOG(WS_LOG_DEBUG, connectError,
sPymbed 0:c4152c628df5 606 "CLIENT_CHANNEL_REQUEST_SHELL_SENT", ssh->error);
sPymbed 0:c4152c628df5 607 return WS_FATAL_ERROR;
sPymbed 0:c4152c628df5 608 }
sPymbed 0:c4152c628df5 609 }
sPymbed 0:c4152c628df5 610 ssh->connectState = CONNECT_SERVER_CHANNEL_REQUEST_SHELL_DONE;
sPymbed 0:c4152c628df5 611 WLOG(WS_LOG_DEBUG, connectState,
sPymbed 0:c4152c628df5 612 "SERVER_CHANNEL_REQUEST_SHELL_DONE");
sPymbed 0:c4152c628df5 613 }
sPymbed 0:c4152c628df5 614
sPymbed 0:c4152c628df5 615 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_connect()");
sPymbed 0:c4152c628df5 616 return WS_SUCCESS;
sPymbed 0:c4152c628df5 617 }
sPymbed 0:c4152c628df5 618
sPymbed 0:c4152c628df5 619
sPymbed 0:c4152c628df5 620 int wolfSSH_shutdown(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 621 {
sPymbed 0:c4152c628df5 622 int ret = WS_SUCCESS;
sPymbed 0:c4152c628df5 623
sPymbed 0:c4152c628df5 624 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_shutdown()");
sPymbed 0:c4152c628df5 625
sPymbed 0:c4152c628df5 626 if (ssh == NULL)
sPymbed 0:c4152c628df5 627 ret = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 628
sPymbed 0:c4152c628df5 629 if (ret == WS_SUCCESS)
sPymbed 0:c4152c628df5 630 ret = SendChannelEof(ssh, 0);
sPymbed 0:c4152c628df5 631
sPymbed 0:c4152c628df5 632 if (ret == WS_SUCCESS)
sPymbed 0:c4152c628df5 633 ret = SendChannelClose(ssh, 0);
sPymbed 0:c4152c628df5 634
sPymbed 0:c4152c628df5 635 if (ret == WS_SUCCESS)
sPymbed 0:c4152c628df5 636 ret = SendDisconnect(ssh, WOLFSSH_DISCONNECT_BY_APPLICATION);
sPymbed 0:c4152c628df5 637
sPymbed 0:c4152c628df5 638 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_shutdown(), ret = %d", ret);
sPymbed 0:c4152c628df5 639 return ret;
sPymbed 0:c4152c628df5 640 }
sPymbed 0:c4152c628df5 641
sPymbed 0:c4152c628df5 642
sPymbed 0:c4152c628df5 643 int wolfSSH_TriggerKeyExchange(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 644 {
sPymbed 0:c4152c628df5 645 int ret = WS_SUCCESS;
sPymbed 0:c4152c628df5 646
sPymbed 0:c4152c628df5 647 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_TriggerKeyExchange()");
sPymbed 0:c4152c628df5 648 if (ssh == NULL)
sPymbed 0:c4152c628df5 649 ret = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 650
sPymbed 0:c4152c628df5 651 if (ret == WS_SUCCESS)
sPymbed 0:c4152c628df5 652 ret = SendKexInit(ssh);
sPymbed 0:c4152c628df5 653
sPymbed 0:c4152c628df5 654 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_TriggerKeyExchange(), ret = %d", ret);
sPymbed 0:c4152c628df5 655 return ret;
sPymbed 0:c4152c628df5 656 }
sPymbed 0:c4152c628df5 657
sPymbed 0:c4152c628df5 658
sPymbed 0:c4152c628df5 659 int wolfSSH_stream_read(WOLFSSH* ssh, byte* buf, word32 bufSz)
sPymbed 0:c4152c628df5 660 {
sPymbed 0:c4152c628df5 661 Buffer* inputBuffer;
sPymbed 0:c4152c628df5 662
sPymbed 0:c4152c628df5 663 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_stream_read()");
sPymbed 0:c4152c628df5 664 //ESP_LOGI("WOLFSSH", "wolfSSH_stream_read()");
sPymbed 0:c4152c628df5 665
sPymbed 0:c4152c628df5 666 if (ssh == NULL || buf == NULL || bufSz == 0 || ssh->channelList == NULL)
sPymbed 0:c4152c628df5 667 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 668
sPymbed 0:c4152c628df5 669 inputBuffer = &ssh->channelList->inputBuffer;
sPymbed 0:c4152c628df5 670
sPymbed 0:c4152c628df5 671 while (inputBuffer->length - inputBuffer->idx == 0) {
sPymbed 0:c4152c628df5 672 int ret = DoReceive(ssh);
sPymbed 0:c4152c628df5 673 if (ret < 0) {
sPymbed 0:c4152c628df5 674 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_stream_read(), ret = %d", ret);
sPymbed 0:c4152c628df5 675 return ret;
sPymbed 0:c4152c628df5 676 }
sPymbed 0:c4152c628df5 677 }
sPymbed 0:c4152c628df5 678 //ESP_LOGI("WOLFSSH", "wolfSSH_stream_read: while loop done");
sPymbed 0:c4152c628df5 679
sPymbed 0:c4152c628df5 680 bufSz = min(bufSz, inputBuffer->length - inputBuffer->idx);
sPymbed 0:c4152c628df5 681 WMEMCPY(buf, inputBuffer->buffer + inputBuffer->idx, bufSz);
sPymbed 0:c4152c628df5 682 inputBuffer->idx += bufSz;
sPymbed 0:c4152c628df5 683
sPymbed 0:c4152c628df5 684 if (!ssh->isKeying && (inputBuffer->length > inputBuffer->bufferSz / 2)) {
sPymbed 0:c4152c628df5 685
sPymbed 0:c4152c628df5 686 word32 usedSz = inputBuffer->length - inputBuffer->idx;
sPymbed 0:c4152c628df5 687 word32 bytesToAdd = inputBuffer->idx;
sPymbed 0:c4152c628df5 688 int sendResult;
sPymbed 0:c4152c628df5 689
sPymbed 0:c4152c628df5 690 WLOG(WS_LOG_DEBUG, "Making more room: %u", usedSz);
sPymbed 0:c4152c628df5 691 if (usedSz) {
sPymbed 0:c4152c628df5 692 WLOG(WS_LOG_DEBUG, " ...moving data down");
sPymbed 0:c4152c628df5 693 WMEMMOVE(inputBuffer->buffer,
sPymbed 0:c4152c628df5 694 inputBuffer->buffer + bytesToAdd, usedSz);
sPymbed 0:c4152c628df5 695 }
sPymbed 0:c4152c628df5 696
sPymbed 0:c4152c628df5 697 sendResult = SendChannelWindowAdjust(ssh,
sPymbed 0:c4152c628df5 698 ssh->channelList->peerChannel,
sPymbed 0:c4152c628df5 699 bytesToAdd);
sPymbed 0:c4152c628df5 700 if (sendResult != WS_SUCCESS)
sPymbed 0:c4152c628df5 701 bufSz = sendResult;
sPymbed 0:c4152c628df5 702
sPymbed 0:c4152c628df5 703 WLOG(WS_LOG_INFO, " bytesToAdd = %u", bytesToAdd);
sPymbed 0:c4152c628df5 704 WLOG(WS_LOG_INFO, " windowSz = %u", ssh->channelList->windowSz);
sPymbed 0:c4152c628df5 705 ssh->channelList->windowSz += bytesToAdd;
sPymbed 0:c4152c628df5 706 WLOG(WS_LOG_INFO, " update windowSz = %u", ssh->channelList->windowSz);
sPymbed 0:c4152c628df5 707
sPymbed 0:c4152c628df5 708 inputBuffer->length = usedSz;
sPymbed 0:c4152c628df5 709 inputBuffer->idx = 0;
sPymbed 0:c4152c628df5 710 }
sPymbed 0:c4152c628df5 711
sPymbed 0:c4152c628df5 712 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_stream_read(), rxd = %d", bufSz);
sPymbed 0:c4152c628df5 713 return bufSz;
sPymbed 0:c4152c628df5 714 }
sPymbed 0:c4152c628df5 715
sPymbed 0:c4152c628df5 716
sPymbed 0:c4152c628df5 717 int wolfSSH_stream_send(WOLFSSH* ssh, byte* buf, word32 bufSz)
sPymbed 0:c4152c628df5 718 {
sPymbed 0:c4152c628df5 719 int bytesTxd = 0;
sPymbed 0:c4152c628df5 720
sPymbed 0:c4152c628df5 721 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_stream_send()");
sPymbed 0:c4152c628df5 722
sPymbed 0:c4152c628df5 723 if (ssh == NULL || buf == NULL || ssh->channelList == NULL)
sPymbed 0:c4152c628df5 724 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 725
sPymbed 0:c4152c628df5 726 bytesTxd = SendChannelData(ssh, ssh->channelList->peerChannel, buf, bufSz);
sPymbed 0:c4152c628df5 727
sPymbed 0:c4152c628df5 728 WLOG(WS_LOG_DEBUG, "Leaving wolfSSH_stream_send(), txd = %d", bytesTxd);
sPymbed 0:c4152c628df5 729 return bytesTxd;
sPymbed 0:c4152c628df5 730 }
sPymbed 0:c4152c628df5 731
sPymbed 0:c4152c628df5 732
sPymbed 0:c4152c628df5 733 void wolfSSH_SetUserAuth(WOLFSSH_CTX* ctx, WS_CallbackUserAuth cb)
sPymbed 0:c4152c628df5 734 {
sPymbed 0:c4152c628df5 735 if (ctx != NULL) {
sPymbed 0:c4152c628df5 736 ctx->userAuthCb = cb;
sPymbed 0:c4152c628df5 737 }
sPymbed 0:c4152c628df5 738 }
sPymbed 0:c4152c628df5 739
sPymbed 0:c4152c628df5 740
sPymbed 0:c4152c628df5 741 void wolfSSH_SetUserAuthCtx(WOLFSSH* ssh, void* userAuthCtx)
sPymbed 0:c4152c628df5 742 {
sPymbed 0:c4152c628df5 743 if (ssh != NULL) {
sPymbed 0:c4152c628df5 744 ssh->userAuthCtx = userAuthCtx;
sPymbed 0:c4152c628df5 745 }
sPymbed 0:c4152c628df5 746 }
sPymbed 0:c4152c628df5 747
sPymbed 0:c4152c628df5 748
sPymbed 0:c4152c628df5 749 void* wolfSSH_GetUserAuthCtx(WOLFSSH* ssh)
sPymbed 0:c4152c628df5 750 {
sPymbed 0:c4152c628df5 751 if (ssh != NULL) {
sPymbed 0:c4152c628df5 752 return ssh->userAuthCtx;
sPymbed 0:c4152c628df5 753 }
sPymbed 0:c4152c628df5 754 return NULL;
sPymbed 0:c4152c628df5 755 }
sPymbed 0:c4152c628df5 756
sPymbed 0:c4152c628df5 757
sPymbed 0:c4152c628df5 758 int wolfSSH_SetUsername(WOLFSSH* ssh, const char* username)
sPymbed 0:c4152c628df5 759 {
sPymbed 0:c4152c628df5 760 char* value = NULL;
sPymbed 0:c4152c628df5 761 word32 valueSz;
sPymbed 0:c4152c628df5 762 int ret = WS_SUCCESS;
sPymbed 0:c4152c628df5 763
sPymbed 0:c4152c628df5 764 if (ssh == NULL || ssh->handshake == NULL ||
sPymbed 0:c4152c628df5 765 ssh->ctx->side == WOLFSSH_ENDPOINT_SERVER ||
sPymbed 0:c4152c628df5 766 username == NULL) {
sPymbed 0:c4152c628df5 767
sPymbed 0:c4152c628df5 768 ret = WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 769 }
sPymbed 0:c4152c628df5 770
sPymbed 0:c4152c628df5 771 if (ret == WS_SUCCESS) {
sPymbed 0:c4152c628df5 772 valueSz = (word32)WSTRLEN(username);
sPymbed 0:c4152c628df5 773 if (valueSz > 0)
sPymbed 0:c4152c628df5 774 value = (char*)WMALLOC(valueSz + 1, ssh->ctx->heap, DYNTYPE_STRING);
sPymbed 0:c4152c628df5 775 if (value == NULL)
sPymbed 0:c4152c628df5 776 ret = WS_MEMORY_E;
sPymbed 0:c4152c628df5 777 }
sPymbed 0:c4152c628df5 778
sPymbed 0:c4152c628df5 779 if (ret == WS_SUCCESS) {
sPymbed 0:c4152c628df5 780 WSTRNCPY(value, username, valueSz + 1);
sPymbed 0:c4152c628df5 781 if (ssh->userName != NULL) {
sPymbed 0:c4152c628df5 782 WFREE(ssh->userName, heap, DYNTYPE_STRING);
sPymbed 0:c4152c628df5 783 ssh->userName = NULL;
sPymbed 0:c4152c628df5 784 }
sPymbed 0:c4152c628df5 785 ssh->userName = value;
sPymbed 0:c4152c628df5 786 ssh->userNameSz = valueSz;
sPymbed 0:c4152c628df5 787 }
sPymbed 0:c4152c628df5 788
sPymbed 0:c4152c628df5 789 return ret;
sPymbed 0:c4152c628df5 790 }
sPymbed 0:c4152c628df5 791
sPymbed 0:c4152c628df5 792
sPymbed 0:c4152c628df5 793 int wolfSSH_CTX_SetBanner(WOLFSSH_CTX* ctx,
sPymbed 0:c4152c628df5 794 const char* newBanner)
sPymbed 0:c4152c628df5 795 {
sPymbed 0:c4152c628df5 796 word32 newBannerSz = 0;
sPymbed 0:c4152c628df5 797
sPymbed 0:c4152c628df5 798 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_CTX_SetBanner()");
sPymbed 0:c4152c628df5 799
sPymbed 0:c4152c628df5 800 if (ctx == NULL)
sPymbed 0:c4152c628df5 801 return WS_BAD_ARGUMENT;
sPymbed 0:c4152c628df5 802
sPymbed 0:c4152c628df5 803 if (newBanner != NULL) {
sPymbed 0:c4152c628df5 804 WLOG(WS_LOG_INFO, " setting banner to: \"%s\"", newBanner);
sPymbed 0:c4152c628df5 805 newBannerSz = (word32)WSTRLEN(newBanner);
sPymbed 0:c4152c628df5 806 }
sPymbed 0:c4152c628df5 807
sPymbed 0:c4152c628df5 808 ctx->banner = newBanner;
sPymbed 0:c4152c628df5 809 ctx->bannerSz = newBannerSz;
sPymbed 0:c4152c628df5 810
sPymbed 0:c4152c628df5 811 return WS_SUCCESS;
sPymbed 0:c4152c628df5 812 }
sPymbed 0:c4152c628df5 813
sPymbed 0:c4152c628df5 814
sPymbed 0:c4152c628df5 815 int wolfSSH_CTX_UsePrivateKey_buffer(WOLFSSH_CTX* ctx,
sPymbed 0:c4152c628df5 816 const byte* in, word32 inSz, int format)
sPymbed 0:c4152c628df5 817 {
sPymbed 0:c4152c628df5 818 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_CTX_UsePrivateKey_buffer()");
sPymbed 0:c4152c628df5 819 return wolfSSH_ProcessBuffer(ctx, in, inSz, format, BUFTYPE_PRIVKEY);
sPymbed 0:c4152c628df5 820 }
sPymbed 0:c4152c628df5 821
sPymbed 0:c4152c628df5 822
sPymbed 0:c4152c628df5 823 void wolfSSH_GetStats(WOLFSSH* ssh, word32* txCount, word32* rxCount,
sPymbed 0:c4152c628df5 824 word32* seq, word32* peerSeq)
sPymbed 0:c4152c628df5 825 {
sPymbed 0:c4152c628df5 826 word32 rTxCount = 0;
sPymbed 0:c4152c628df5 827 word32 rRxCount = 0;
sPymbed 0:c4152c628df5 828 word32 rSeq = 0;
sPymbed 0:c4152c628df5 829 word32 rPeerSeq = 0;
sPymbed 0:c4152c628df5 830
sPymbed 0:c4152c628df5 831 if (ssh != NULL) {
sPymbed 0:c4152c628df5 832 rTxCount = ssh->txCount;
sPymbed 0:c4152c628df5 833 rRxCount = ssh->rxCount;
sPymbed 0:c4152c628df5 834 rSeq = ssh->seq;
sPymbed 0:c4152c628df5 835 rPeerSeq = ssh->peerSeq;
sPymbed 0:c4152c628df5 836 }
sPymbed 0:c4152c628df5 837
sPymbed 0:c4152c628df5 838 if (txCount != NULL)
sPymbed 0:c4152c628df5 839 *txCount = rTxCount;
sPymbed 0:c4152c628df5 840 if (rxCount != NULL)
sPymbed 0:c4152c628df5 841 *rxCount = rRxCount;
sPymbed 0:c4152c628df5 842 if (seq != NULL)
sPymbed 0:c4152c628df5 843 *seq = rSeq;
sPymbed 0:c4152c628df5 844 if (peerSeq != NULL)
sPymbed 0:c4152c628df5 845 *peerSeq = rPeerSeq;
sPymbed 0:c4152c628df5 846 }
sPymbed 0:c4152c628df5 847
sPymbed 0:c4152c628df5 848
sPymbed 0:c4152c628df5 849 int wolfSSH_KDF(byte hashId, byte keyId,
sPymbed 0:c4152c628df5 850 byte* key, word32 keySz,
sPymbed 0:c4152c628df5 851 const byte* k, word32 kSz,
sPymbed 0:c4152c628df5 852 const byte* h, word32 hSz,
sPymbed 0:c4152c628df5 853 const byte* sessionId, word32 sessionIdSz)
sPymbed 0:c4152c628df5 854 {
sPymbed 0:c4152c628df5 855 WLOG(WS_LOG_DEBUG, "Entering wolfSSH_KDF()");
sPymbed 0:c4152c628df5 856 return GenerateKey(hashId, keyId, key, keySz, k, kSz, h, hSz,
sPymbed 0:c4152c628df5 857 sessionId, sessionIdSz);
sPymbed 0:c4152c628df5 858 }
sPymbed 0:c4152c628df5 859