| 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 |
#include "mod_sftp.h" |
| 26 |
#include "ssh2.h" |
| 27 |
#include "msg.h" |
| 28 |
#include "crypto.h" |
| 29 |
#include "disconnect.h" |
| 30 |
|
| 31 |
#if defined(PR_USE_OPENSSL_ECC) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
# define MAX_ECPOINT_LEN ((528*2 / 8) + 1) |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static unsigned char msg_buf[8 * 1024]; |
| 43 |
|
| 44 |
static const char *trace_channel = "ssh2"; |
| 45 |
|
| 46 |
unsigned char *sftp_msg_getbuf(pool *p, size_t sz) { |
| 47 |
if (sz <= sizeof(msg_buf)) { |
| 48 |
return msg_buf; |
| 49 |
} |
| 50 |
|
| 51 |
return palloc(p, sz); |
| 52 |
} |
| 53 |
|
| 54 |
uint32_t sftp_msg_read_byte2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 55 |
unsigned char *byte) { |
| 56 |
(void) p; |
| 57 |
|
| 58 |
if (*buflen < sizeof(char)) { |
| 59 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 60 |
"message format error: unable to read byte (buflen = %lu)", |
| 61 |
(unsigned long) *buflen); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
memcpy(byte, *buf, sizeof(unsigned char)); |
| 66 |
(*buf) += sizeof(unsigned char); |
| 67 |
(*buflen) -= sizeof(unsigned char); |
| 68 |
|
| 69 |
return sizeof(unsigned char); |
| 70 |
} |
| 71 |
|
| 72 |
unsigned char sftp_msg_read_byte(pool *p, unsigned char **buf, |
| 73 |
uint32_t *buflen) { |
| 74 |
unsigned char byte = 0; |
| 75 |
uint32_t len; |
| 76 |
|
| 77 |
len = sftp_msg_read_byte2(p, buf, buflen, &byte); |
| 78 |
if (len == 0) { |
| 79 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 80 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 81 |
} |
| 82 |
|
| 83 |
return byte; |
| 84 |
} |
| 85 |
|
| 86 |
uint32_t sftp_msg_read_bool2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 87 |
int *b) { |
| 88 |
unsigned char byte = 0; |
| 89 |
uint32_t len; |
| 90 |
|
| 91 |
(void) p; |
| 92 |
|
| 93 |
len = sftp_msg_read_byte2(p, buf, buflen, &byte); |
| 94 |
if (len == 0) { |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
*b = byte; |
| 99 |
return len; |
| 100 |
} |
| 101 |
|
| 102 |
int sftp_msg_read_bool(pool *p, unsigned char **buf, uint32_t *buflen) { |
| 103 |
int b = 0; |
| 104 |
uint32_t len; |
| 105 |
|
| 106 |
len = sftp_msg_read_bool2(p, buf, buflen, &b); |
| 107 |
if (len == 0) { |
| 108 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 109 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 110 |
} |
| 111 |
|
| 112 |
if (b == FALSE) { |
| 113 |
return FALSE; |
| 114 |
} |
| 115 |
|
| 116 |
return TRUE; |
| 117 |
} |
| 118 |
|
| 119 |
uint32_t sftp_msg_read_data2(pool *p, unsigned char **buf, |
| 120 |
uint32_t *buflen, size_t datalen, unsigned char **data) { |
| 121 |
uint32_t len; |
| 122 |
unsigned char *ptr = NULL; |
| 123 |
|
| 124 |
len = sftp_msg_read_data2_direct(p, buf, buflen, datalen, &ptr); |
| 125 |
if (len == 0) { |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
*data = palloc(p, datalen); |
| 130 |
memcpy(*data, ptr, datalen); |
| 131 |
|
| 132 |
return datalen; |
| 133 |
} |
| 134 |
|
| 135 |
uint32_t sftp_msg_read_data2_direct(pool *p, unsigned char **buf, |
| 136 |
uint32_t *buflen, size_t datalen, unsigned char **data) { |
| 137 |
if (datalen == 0) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
if (*buflen < datalen) { |
| 142 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 143 |
"message format error: unable to read %lu bytes of raw data " |
| 144 |
"(buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
*data = *buf; |
| 149 |
(*buf) += datalen; |
| 150 |
(*buflen) -= datalen; |
| 151 |
|
| 152 |
return datalen; |
| 153 |
} |
| 154 |
|
| 155 |
unsigned char *sftp_msg_read_data(pool *p, unsigned char **buf, |
| 156 |
uint32_t *buflen, size_t datalen) { |
| 157 |
unsigned char *data = NULL; |
| 158 |
uint32_t len; |
| 159 |
|
| 160 |
if (datalen == 0) { |
| 161 |
errno = EINVAL; |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
len = sftp_msg_read_data2(p, buf, buflen, datalen, &data); |
| 166 |
if (len == 0) { |
| 167 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 168 |
"message format error: unable to read %lu bytes of raw data " |
| 169 |
"(buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 170 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 171 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 172 |
} |
| 173 |
|
| 174 |
return data; |
| 175 |
} |
| 176 |
|
| 177 |
unsigned char *sftp_msg_read_data_direct(pool *p, unsigned char **buf, |
| 178 |
uint32_t *buflen, size_t datalen) { |
| 179 |
unsigned char *data = NULL; |
| 180 |
uint32_t len; |
| 181 |
|
| 182 |
if (datalen == 0) { |
| 183 |
errno = EINVAL; |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
len = sftp_msg_read_data2_direct(p, buf, buflen, datalen, &data); |
| 188 |
if (len == 0) { |
| 189 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 190 |
"message format error: unable to read %lu bytes of raw data " |
| 191 |
"(buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 192 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 193 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 194 |
} |
| 195 |
|
| 196 |
return data; |
| 197 |
} |
| 198 |
|
| 199 |
uint32_t sftp_msg_read_int2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 200 |
uint32_t *val) { |
| 201 |
|
| 202 |
(void) p; |
| 203 |
|
| 204 |
if (*buflen < sizeof(uint32_t)) { |
| 205 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 206 |
"message format error: unable to read int (buflen = %lu)", |
| 207 |
(unsigned long) *buflen); |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
memcpy(val, *buf, sizeof(uint32_t)); |
| 212 |
(*buf) += sizeof(uint32_t); |
| 213 |
(*buflen) -= sizeof(uint32_t); |
| 214 |
|
| 215 |
*val = ntohl(*val); |
| 216 |
return sizeof(uint32_t); |
| 217 |
} |
| 218 |
|
| 219 |
uint32_t sftp_msg_read_int(pool *p, unsigned char **buf, uint32_t *buflen) { |
| 220 |
uint32_t val = 0; |
| 221 |
uint32_t len; |
| 222 |
|
| 223 |
len = sftp_msg_read_int2(p, buf, buflen, &val); |
| 224 |
if (len == 0) { |
| 225 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 226 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 227 |
} |
| 228 |
|
| 229 |
return val; |
| 230 |
} |
| 231 |
|
| 232 |
uint32_t sftp_msg_read_long2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 233 |
uint64_t *val) { |
| 234 |
unsigned char data[8]; |
| 235 |
|
| 236 |
if (*buflen < sizeof(data)) { |
| 237 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 238 |
"message format error: unable to read long (buflen = %lu)", |
| 239 |
(unsigned long) *buflen); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
memcpy(data, *buf, sizeof(data)); |
| 244 |
(*buf) += sizeof(data); |
| 245 |
(*buflen) -= sizeof(data); |
| 246 |
|
| 247 |
(*val) = (uint64_t) data[0] << 56; |
| 248 |
(*val) |= (uint64_t) data[1] << 48; |
| 249 |
(*val) |= (uint64_t) data[2] << 40; |
| 250 |
(*val) |= (uint64_t) data[3] << 32; |
| 251 |
(*val) |= (uint64_t) data[4] << 24; |
| 252 |
(*val) |= (uint64_t) data[5] << 16; |
| 253 |
(*val) |= (uint64_t) data[6] << 8; |
| 254 |
(*val) |= (uint64_t) data[7]; |
| 255 |
|
| 256 |
return sizeof(data); |
| 257 |
} |
| 258 |
|
| 259 |
uint64_t sftp_msg_read_long(pool *p, unsigned char **buf, uint32_t *buflen) { |
| 260 |
uint64_t val = 0; |
| 261 |
uint32_t len; |
| 262 |
|
| 263 |
len = sftp_msg_read_long2(p, buf, buflen, &val); |
| 264 |
if (len == 0) { |
| 265 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 266 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 267 |
} |
| 268 |
|
| 269 |
return val; |
| 270 |
} |
| 271 |
|
| 272 |
uint32_t sftp_msg_read_mpint2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 273 |
const BIGNUM **mpint) { |
| 274 |
unsigned char *mpint_data = NULL; |
| 275 |
const unsigned char *data = NULL, *ptr = NULL; |
| 276 |
uint32_t datalen = 0, mpint_len = 0, len = 0, total_len = 0; |
| 277 |
|
| 278 |
len = sftp_msg_read_int2(p, buf, buflen, &mpint_len); |
| 279 |
if (len == 0) { |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
if (*buflen < mpint_len) { |
| 284 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 285 |
"message format error: unable to read %lu bytes of mpint (buflen = %lu)", |
| 286 |
(unsigned long) len, (unsigned long) *buflen); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
if (mpint_len > (1024 * 16)) { |
| 291 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 292 |
"message format error: unable to handle mpint of %lu bytes", |
| 293 |
(unsigned long) mpint_len); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
total_len += len; |
| 298 |
|
| 299 |
len = sftp_msg_read_data2(p, buf, buflen, mpint_len, &mpint_data); |
| 300 |
if (len == 0) { |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
total_len += len; |
| 305 |
|
| 306 |
ptr = (const unsigned char *) mpint_data; |
| 307 |
if ((ptr[0] & 0x80) != 0) { |
| 308 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 309 |
"message format error: negative mpint numbers not supported"); |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
data = ptr; |
| 315 |
datalen = mpint_len; |
| 316 |
while (datalen > 0 && |
| 317 |
*data == 0x00) { |
| 318 |
pr_signals_handle(); |
| 319 |
data++; |
| 320 |
datalen--; |
| 321 |
} |
| 322 |
|
| 323 |
*mpint = BN_bin2bn(data, (int) datalen, NULL); |
| 324 |
if (*mpint == NULL) { |
| 325 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 326 |
"message format error: unable to convert binary mpint: %s", |
| 327 |
sftp_crypto_get_errors()); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
return total_len; |
| 332 |
} |
| 333 |
|
| 334 |
const BIGNUM *sftp_msg_read_mpint(pool *p, unsigned char **buf, |
| 335 |
uint32_t *buflen) { |
| 336 |
const BIGNUM *mpint = NULL; |
| 337 |
uint32_t len; |
| 338 |
|
| 339 |
len = sftp_msg_read_mpint2(p, buf, buflen, &mpint); |
| 340 |
if (len == 0) { |
| 341 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 342 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 343 |
} |
| 344 |
|
| 345 |
return mpint; |
| 346 |
} |
| 347 |
|
| 348 |
uint32_t sftp_msg_read_string2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 349 |
char **str) { |
| 350 |
uint32_t data_len = 0, len = 0; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (*buflen == 0) { |
| 356 |
pr_trace_msg(trace_channel, 9, |
| 357 |
"malformed message format (buflen = %lu) for reading string, using \"\"", |
| 358 |
(unsigned long) *buflen); |
| 359 |
*str = pstrdup(p, ""); |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
len = sftp_msg_read_int2(p, buf, buflen, &data_len); |
| 364 |
if (len == 0) { |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (*buflen < data_len) { |
| 374 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 375 |
"message format error: unable to read %lu bytes of string data " |
| 376 |
"(buflen = %lu)", (unsigned long) data_len, (unsigned long) *buflen); |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
*str = palloc(p, data_len + 1); |
| 381 |
|
| 382 |
if (data_len > 0) { |
| 383 |
memcpy(*str, *buf, data_len); |
| 384 |
(*buf) += data_len; |
| 385 |
(*buflen) -= data_len; |
| 386 |
} |
| 387 |
(*str)[data_len] = '\0'; |
| 388 |
|
| 389 |
return len + data_len; |
| 390 |
} |
| 391 |
|
| 392 |
char *sftp_msg_read_string(pool *p, unsigned char **buf, uint32_t *buflen) { |
| 393 |
char *str = NULL; |
| 394 |
uint32_t len; |
| 395 |
|
| 396 |
len = sftp_msg_read_string2(p, buf, buflen, &str); |
| 397 |
if (len == 0) { |
| 398 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 399 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 400 |
} |
| 401 |
|
| 402 |
return str; |
| 403 |
} |
| 404 |
|
| 405 |
#if defined(PR_USE_OPENSSL_ECC) |
| 406 |
uint32_t sftp_msg_read_ecpoint2(pool *p, unsigned char **buf, uint32_t *buflen, |
| 407 |
const EC_GROUP *curve, EC_POINT **point) { |
| 408 |
BN_CTX *bn_ctx; |
| 409 |
unsigned char *data = NULL; |
| 410 |
uint32_t datalen = 0, len = 0, total_len = 0; |
| 411 |
|
| 412 |
len = sftp_msg_read_int2(p, buf, buflen, &datalen); |
| 413 |
if (len == 0) { |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
if (*buflen < datalen) { |
| 418 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 419 |
"message format error: unable to read %lu bytes of EC point" |
| 420 |
" (buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
if (datalen > MAX_ECPOINT_LEN) { |
| 425 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 426 |
"message format error: EC point length too long (%lu > max %lu)", |
| 427 |
(unsigned long) datalen, (unsigned long) MAX_ECPOINT_LEN); |
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
total_len += len; |
| 432 |
|
| 433 |
len = sftp_msg_read_data2(p, buf, buflen, datalen, &data); |
| 434 |
if (len == 0) { |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
if (data == NULL) { |
| 439 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 440 |
"message format error: unable to read %lu bytes of EC point data", |
| 441 |
(unsigned long) datalen); |
| 442 |
return 0; |
| 443 |
} |
| 444 |
|
| 445 |
total_len += len; |
| 446 |
|
| 447 |
if (data[0] != POINT_CONVERSION_UNCOMPRESSED) { |
| 448 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 449 |
"message format error: EC point data formatted incorrectly " |
| 450 |
"(leading byte 0x%02x should be 0x%02x)", data[0], |
| 451 |
POINT_CONVERSION_UNCOMPRESSED); |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
bn_ctx = BN_CTX_new(); |
| 456 |
if (bn_ctx == NULL) { |
| 457 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 458 |
"error allocating new BN_CTX: %s", sftp_crypto_get_errors()); |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
if (EC_POINT_oct2point(curve, *point, data, datalen, bn_ctx) != 1) { |
| 463 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 464 |
"message format error: unable to convert binary EC point data: %s", |
| 465 |
sftp_crypto_get_errors()); |
| 466 |
BN_CTX_free(bn_ctx); |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
BN_CTX_free(bn_ctx); |
| 471 |
pr_memscrub(data, datalen); |
| 472 |
|
| 473 |
return total_len; |
| 474 |
} |
| 475 |
|
| 476 |
EC_POINT *sftp_msg_read_ecpoint(pool *p, unsigned char **buf, uint32_t *buflen, |
| 477 |
const EC_GROUP *curve, EC_POINT *point) { |
| 478 |
uint32_t len; |
| 479 |
|
| 480 |
len = sftp_msg_read_ecpoint2(p, buf, buflen, curve, &point); |
| 481 |
if (len == 0) { |
| 482 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 483 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 484 |
} |
| 485 |
|
| 486 |
return point; |
| 487 |
} |
| 488 |
#endif |
| 489 |
|
| 490 |
uint32_t sftp_msg_write_byte(unsigned char **buf, uint32_t *buflen, |
| 491 |
unsigned char byte) { |
| 492 |
uint32_t len = 0; |
| 493 |
|
| 494 |
if (*buflen < sizeof(char)) { |
| 495 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 496 |
"message format error: unable to write byte (buflen = %lu)", |
| 497 |
(unsigned long) *buflen); |
| 498 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 499 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 500 |
} |
| 501 |
|
| 502 |
len = sizeof(unsigned char); |
| 503 |
|
| 504 |
memcpy(*buf, &byte, len); |
| 505 |
(*buf) += len; |
| 506 |
(*buflen) -= len; |
| 507 |
|
| 508 |
return len; |
| 509 |
} |
| 510 |
|
| 511 |
uint32_t sftp_msg_write_bool(unsigned char **buf, uint32_t *buflen, |
| 512 |
unsigned char b) { |
| 513 |
return sftp_msg_write_byte(buf, buflen, b == 0 ? 0 : 1); |
| 514 |
} |
| 515 |
|
| 516 |
uint32_t sftp_msg_write_data(unsigned char **buf, uint32_t *buflen, |
| 517 |
const unsigned char *data, size_t datalen, int write_len) { |
| 518 |
uint32_t len = 0; |
| 519 |
|
| 520 |
if (write_len) { |
| 521 |
len += sftp_msg_write_int(buf, buflen, datalen); |
| 522 |
} |
| 523 |
|
| 524 |
if (*buflen < datalen) { |
| 525 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 526 |
"message format error: unable to write %lu bytes of raw data " |
| 527 |
"(buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 528 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 529 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 530 |
} |
| 531 |
|
| 532 |
if (datalen > 0) { |
| 533 |
memcpy(*buf, data, datalen); |
| 534 |
(*buf) += datalen; |
| 535 |
(*buflen) -= datalen; |
| 536 |
|
| 537 |
len += datalen; |
| 538 |
} |
| 539 |
|
| 540 |
return len; |
| 541 |
} |
| 542 |
|
| 543 |
uint32_t sftp_msg_write_int(unsigned char **buf, uint32_t *buflen, |
| 544 |
uint32_t val) { |
| 545 |
uint32_t len; |
| 546 |
|
| 547 |
if (*buflen < sizeof(uint32_t)) { |
| 548 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 549 |
"message format error: unable to write int (buflen = %lu)", |
| 550 |
(unsigned long) *buflen); |
| 551 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 552 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 553 |
} |
| 554 |
|
| 555 |
len = sizeof(uint32_t); |
| 556 |
|
| 557 |
val = htonl(val); |
| 558 |
memcpy(*buf, &val, len); |
| 559 |
(*buf) += len; |
| 560 |
(*buflen) -= len; |
| 561 |
|
| 562 |
return len; |
| 563 |
} |
| 564 |
|
| 565 |
uint32_t sftp_msg_write_long(unsigned char **buf, uint32_t *buflen, |
| 566 |
uint64_t val) { |
| 567 |
unsigned char data[8]; |
| 568 |
|
| 569 |
if (*buflen < sizeof(uint64_t)) { |
| 570 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 571 |
"message format error: unable to write long (buflen = %lu)", |
| 572 |
(unsigned long) *buflen); |
| 573 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 574 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 575 |
} |
| 576 |
|
| 577 |
data[0] = (unsigned char) (val >> 56) & 0xFF; |
| 578 |
data[1] = (unsigned char) (val >> 48) & 0xFF; |
| 579 |
data[2] = (unsigned char) (val >> 40) & 0xFF; |
| 580 |
data[3] = (unsigned char) (val >> 32) & 0xFF; |
| 581 |
data[4] = (unsigned char) (val >> 24) & 0xFF; |
| 582 |
data[5] = (unsigned char) (val >> 16) & 0xFF; |
| 583 |
data[6] = (unsigned char) (val >> 8) & 0xFF; |
| 584 |
data[7] = (unsigned char) val & 0xFF; |
| 585 |
|
| 586 |
return sftp_msg_write_data(buf, buflen, data, sizeof(data), FALSE); |
| 587 |
} |
| 588 |
|
| 589 |
uint32_t sftp_msg_write_mpint(unsigned char **buf, uint32_t *buflen, |
| 590 |
const BIGNUM *mpint) { |
| 591 |
unsigned char *data = NULL; |
| 592 |
size_t datalen = 0; |
| 593 |
int res = 0; |
| 594 |
uint32_t len = 0; |
| 595 |
|
| 596 |
if (BN_is_zero(mpint)) { |
| 597 |
return sftp_msg_write_int(buf, buflen, 0); |
| 598 |
} |
| 599 |
|
| 600 |
#if OPENSSL_VERSION_NUMBER >= 0x0090801fL |
| 601 |
if (BN_is_negative(mpint)) { |
| 602 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 603 |
"message format error: unable to write mpint (negative numbers not " |
| 604 |
"supported)"); |
| 605 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 606 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 607 |
} |
| 608 |
#endif |
| 609 |
|
| 610 |
datalen = BN_num_bytes(mpint) + 1; |
| 611 |
|
| 612 |
if (*buflen < datalen) { |
| 613 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 614 |
"message format error: unable to write %lu bytes of mpint (buflen = %lu)", |
| 615 |
(unsigned long) datalen, (unsigned long) *buflen); |
| 616 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 617 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 618 |
} |
| 619 |
|
| 620 |
data = malloc(datalen); |
| 621 |
if (data == NULL) { |
| 622 |
pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!"); |
| 623 |
_exit(1); |
| 624 |
} |
| 625 |
|
| 626 |
data[0] = 0; |
| 627 |
|
| 628 |
res = BN_bn2bin(mpint, data + 1); |
| 629 |
if (res < 0 || |
| 630 |
res != (int) (datalen - 1)) { |
| 631 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 632 |
"message format error: BN_bn2bin() failed: expected %lu bytes, got %d", |
| 633 |
(unsigned long) (datalen - 1), res); |
| 634 |
pr_memscrub(data, datalen); |
| 635 |
free(data); |
| 636 |
|
| 637 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 638 |
|
| 639 |
|
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
if (data[1] & 0x80) { |
| 644 |
len += sftp_msg_write_data(buf, buflen, data, datalen, TRUE); |
| 645 |
|
| 646 |
} else { |
| 647 |
len += sftp_msg_write_data(buf, buflen, data + 1, datalen - 1, |
| 648 |
TRUE); |
| 649 |
} |
| 650 |
|
| 651 |
pr_memscrub(data, datalen); |
| 652 |
free(data); |
| 653 |
|
| 654 |
return len; |
| 655 |
} |
| 656 |
|
| 657 |
uint32_t sftp_msg_write_string(unsigned char **buf, uint32_t *buflen, |
| 658 |
const char *str) { |
| 659 |
uint32_t len = 0; |
| 660 |
|
| 661 |
len = strlen(str); |
| 662 |
return sftp_msg_write_data(buf, buflen, (const unsigned char *) str, len, |
| 663 |
TRUE); |
| 664 |
} |
| 665 |
|
| 666 |
#if defined(PR_USE_OPENSSL_ECC) |
| 667 |
uint32_t sftp_msg_write_ecpoint(unsigned char **buf, uint32_t *buflen, |
| 668 |
const EC_GROUP *curve, const EC_POINT *point) { |
| 669 |
unsigned char *data = NULL; |
| 670 |
size_t datalen = 0; |
| 671 |
uint32_t len = 0; |
| 672 |
BN_CTX *bn_ctx; |
| 673 |
|
| 674 |
bn_ctx = BN_CTX_new(); |
| 675 |
if (bn_ctx == NULL) { |
| 676 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 677 |
"error allocating new BN_CTX: %s", sftp_crypto_get_errors()); |
| 678 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 679 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 680 |
} |
| 681 |
|
| 682 |
datalen = EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED, |
| 683 |
NULL, 0, bn_ctx); |
| 684 |
if (datalen > MAX_ECPOINT_LEN) { |
| 685 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 686 |
"message format error: EC point length too long (%lu > max %lu)", |
| 687 |
(unsigned long) datalen, (unsigned long) MAX_ECPOINT_LEN); |
| 688 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 689 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 690 |
} |
| 691 |
|
| 692 |
if (*buflen < datalen) { |
| 693 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 694 |
"message format error: unable to write %lu bytes of EC point " |
| 695 |
"(buflen = %lu)", (unsigned long) datalen, (unsigned long) *buflen); |
| 696 |
pr_log_stacktrace(sftp_logfd, MOD_SFTP_VERSION); |
| 697 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 698 |
} |
| 699 |
|
| 700 |
data = malloc(datalen); |
| 701 |
if (data == NULL) { |
| 702 |
pr_log_pri(PR_LOG_ALERT, MOD_SFTP_VERSION ": Out of memory!"); |
| 703 |
_exit(1); |
| 704 |
} |
| 705 |
|
| 706 |
if (EC_POINT_point2oct(curve, point, POINT_CONVERSION_UNCOMPRESSED, data, |
| 707 |
datalen, bn_ctx) != datalen) { |
| 708 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 709 |
"error writing EC point data: Length mismatch"); |
| 710 |
pr_memscrub(data, datalen); |
| 711 |
free(data); |
| 712 |
BN_CTX_free(bn_ctx); |
| 713 |
|
| 714 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL); |
| 715 |
|
| 716 |
|
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
len = sftp_msg_write_data(buf, buflen, (const unsigned char *) data, datalen, |
| 721 |
TRUE); |
| 722 |
|
| 723 |
pr_memscrub(data, datalen); |
| 724 |
free(data); |
| 725 |
BN_CTX_free(bn_ctx); |
| 726 |
|
| 727 |
return len; |
| 728 |
} |
| 729 |
#endif |
| 730 |
|