| 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 "packet.h" |
| 28 |
#include "msg.h" |
| 29 |
#include "auth.h" |
| 30 |
#include "session.h" |
| 31 |
#include "keys.h" |
| 32 |
#include "keystore.h" |
| 33 |
#include "interop.h" |
| 34 |
#include "blacklist.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static array_header *publickey_fps = NULL; |
| 43 |
|
| 44 |
static const char *trace_channel = "ssh2"; |
| 45 |
|
| 46 |
static int send_pubkey_ok(const char *algo, const unsigned char *pubkey_data, |
| 47 |
uint32_t pubkey_len) { |
| 48 |
struct ssh2_packet *pkt; |
| 49 |
unsigned char *buf, *ptr; |
| 50 |
uint32_t buflen, bufsz; |
| 51 |
int res; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
bufsz = buflen = pubkey_len + 1024; |
| 57 |
|
| 58 |
pkt = sftp_ssh2_packet_create(sftp_pool); |
| 59 |
|
| 60 |
buflen = bufsz; |
| 61 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 62 |
|
| 63 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_USER_AUTH_PK_OK); |
| 64 |
sftp_msg_write_string(&buf, &buflen, algo); |
| 65 |
sftp_msg_write_data(&buf, &buflen, pubkey_data, pubkey_len, TRUE); |
| 66 |
|
| 67 |
pkt->payload = ptr; |
| 68 |
pkt->payload_len = (bufsz - buflen); |
| 69 |
|
| 70 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "sending publickey OK"); |
| 71 |
|
| 72 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 73 |
if (res < 0) { |
| 74 |
destroy_pool(pkt->pool); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
destroy_pool(pkt->pool); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int sftp_auth_publickey(struct ssh2_packet *pkt, cmd_rec *pass_cmd, |
| 83 |
const char *orig_user, const char *user, const char *service, |
| 84 |
unsigned char **buf, uint32_t *buflen, int *send_userauth_fail) { |
| 85 |
int fp_algo_id = 0, have_signature, res; |
| 86 |
enum sftp_key_type_e pubkey_type; |
| 87 |
struct sftp_verify_details *details = NULL; |
| 88 |
pr_table_t *verify_notes = NULL; |
| 89 |
unsigned char *pubkey_data; |
| 90 |
char *pubkey_algo = NULL; |
| 91 |
const char *fp = NULL, *fp_algo = NULL; |
| 92 |
uint32_t pubkey_len; |
| 93 |
struct passwd *pw; |
| 94 |
config_rec *c; |
| 95 |
|
| 96 |
if (pr_cmd_dispatch_phase(pass_cmd, PRE_CMD, 0) < 0) { |
| 97 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 98 |
"authentication request for user '%s' blocked by '%s' handler", |
| 99 |
orig_user, (char *) pass_cmd->argv[0]); |
| 100 |
|
| 101 |
pr_log_auth(PR_LOG_NOTICE, |
| 102 |
"USER %s (Login failed): blocked by '%s' handler", orig_user, |
| 103 |
(char *) pass_cmd->argv[0]); |
| 104 |
|
| 105 |
pr_cmd_dispatch_phase(pass_cmd, POST_CMD_ERR, 0); |
| 106 |
pr_cmd_dispatch_phase(pass_cmd, LOG_CMD_ERR, 0); |
| 107 |
|
| 108 |
*send_userauth_fail = TRUE; |
| 109 |
errno = EPERM; |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
have_signature = sftp_msg_read_bool(pkt->pool, buf, buflen); |
| 114 |
|
| 115 |
if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO)) { |
| 116 |
pubkey_algo = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 117 |
} |
| 118 |
pubkey_len = sftp_msg_read_int(pkt->pool, buf, buflen); |
| 119 |
pubkey_data = sftp_msg_read_data(pkt->pool, buf, buflen, pubkey_len); |
| 120 |
|
| 121 |
if (pubkey_algo == NULL) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
pubkey_algo = sftp_msg_read_string(pkt->pool, &pubkey_data, &pubkey_len); |
| 126 |
} |
| 127 |
|
| 128 |
pr_trace_msg(trace_channel, 9, "client sent '%s' public key %s", |
| 129 |
pubkey_algo, have_signature ? "with signature" : "without signature"); |
| 130 |
|
| 131 |
res = sftp_auth_publickey_isvalid(pubkey_algo, &pubkey_type); |
| 132 |
if (res != TRUE) { |
| 133 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 134 |
"unsupported public key algorithm '%s' requested, rejecting request", |
| 135 |
pubkey_algo); |
| 136 |
|
| 137 |
pr_log_auth(PR_LOG_NOTICE, |
| 138 |
"USER %s (Login failed): unsupported public key algorithm '%s' requested", |
| 139 |
user, pubkey_algo); |
| 140 |
|
| 141 |
*send_userauth_fail = TRUE; |
| 142 |
errno = EINVAL; |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthPublicKeys", FALSE); |
| 151 |
if (c != NULL) { |
| 152 |
register unsigned int i; |
| 153 |
int algo_enabled = FALSE; |
| 154 |
|
| 155 |
for (i = 0; i < c->argc; i++) { |
| 156 |
if (strcmp(pubkey_algo, (const char *) c->argv[i]) == 0) { |
| 157 |
algo_enabled = TRUE; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (algo_enabled == FALSE) { |
| 163 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 164 |
"public key algorithm '%s' disabled by SFTPAuthPublicKeys, " |
| 165 |
"rejecting request", pubkey_algo); |
| 166 |
|
| 167 |
pr_log_auth(PR_LOG_NOTICE, |
| 168 |
"USER %s (Login failed): public key algorithm '%s' disabled by " |
| 169 |
"SFTPAuthPublicKeys", user, pubkey_algo); |
| 170 |
|
| 171 |
*send_userauth_fail = TRUE; |
| 172 |
errno = EINVAL; |
| 173 |
return 0; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
res = sftp_keys_verify_pubkey_type(pkt->pool, pubkey_data, pubkey_len, |
| 178 |
pubkey_type); |
| 179 |
if (res != TRUE) { |
| 180 |
int xerrno = errno; |
| 181 |
|
| 182 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 183 |
"unable to verify that given public key matches given '%s' algorithm", |
| 184 |
pubkey_algo); |
| 185 |
|
| 186 |
if (res < 0) { |
| 187 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 188 |
"error verifying public key algorithm '%s': %s", pubkey_algo, |
| 189 |
strerror(xerrno)); |
| 190 |
} |
| 191 |
|
| 192 |
*send_userauth_fail = TRUE; |
| 193 |
errno = EINVAL; |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
#ifdef OPENSSL_FIPS |
| 198 |
if (FIPS_mode()) { |
| 199 |
# if defined(HAVE_SHA256_OPENSSL) |
| 200 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256; |
| 201 |
fp_algo = "SHA256"; |
| 202 |
# else |
| 203 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA1; |
| 204 |
fp_algo = "SHA1"; |
| 205 |
# endif |
| 206 |
|
| 207 |
fp = sftp_keys_get_fingerprint(pkt->pool, pubkey_data, pubkey_len, |
| 208 |
fp_algo_id); |
| 209 |
if (fp != NULL) { |
| 210 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 211 |
"public key %s fingerprint: %s", fp_algo, fp); |
| 212 |
|
| 213 |
} else { |
| 214 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 215 |
"error obtaining public key %s fingerprint: %s", fp_algo, |
| 216 |
strerror(errno)); |
| 217 |
fp_algo = NULL; |
| 218 |
} |
| 219 |
|
| 220 |
} else { |
| 221 |
#endif |
| 222 |
#if defined(HAVE_SHA256_OPENSSL) |
| 223 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256; |
| 224 |
fp_algo = "SHA256"; |
| 225 |
#else |
| 226 |
fp_algo_id = SFTP_KEYS_FP_DIGEST_MD5; |
| 227 |
fp_algo = "MD5"; |
| 228 |
#endif |
| 229 |
|
| 230 |
fp = sftp_keys_get_fingerprint(pkt->pool, pubkey_data, pubkey_len, |
| 231 |
fp_algo_id); |
| 232 |
if (fp != NULL) { |
| 233 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 234 |
"public key %s fingerprint: %s", fp_algo, fp); |
| 235 |
|
| 236 |
} else { |
| 237 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 238 |
"error obtaining public key %s fingerprint: %s", fp_algo, |
| 239 |
strerror(errno)); |
| 240 |
fp_algo = NULL; |
| 241 |
} |
| 242 |
#ifdef OPENSSL_FIPS |
| 243 |
} |
| 244 |
#endif |
| 245 |
|
| 246 |
if (fp != NULL) { |
| 247 |
const char *k, *v; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
k = pstrdup(session.pool, "SFTP_USER_PUBLICKEY_ALGO"); |
| 254 |
v = pstrdup(session.pool, pubkey_algo); |
| 255 |
pr_env_unset(session.pool, k); |
| 256 |
pr_env_set(session.pool, k, v); |
| 257 |
(void) pr_table_add(session.notes, k, v, 0); |
| 258 |
|
| 259 |
k = pstrdup(session.pool, "SFTP_USER_PUBLICKEY_FINGERPRINT"); |
| 260 |
v = pstrdup(session.pool, fp); |
| 261 |
pr_env_unset(session.pool, k); |
| 262 |
pr_env_set(session.pool, k, v); |
| 263 |
(void) pr_table_add(session.notes, k, v, 0); |
| 264 |
|
| 265 |
k = pstrdup(session.pool, "SFTP_USER_PUBLICKEY_FINGERPRINT_ALGO"); |
| 266 |
v = pstrdup(session.pool, fp_algo); |
| 267 |
pr_env_unset(session.pool, k); |
| 268 |
pr_env_set(session.pool, k, v); |
| 269 |
(void) pr_table_add(session.notes, k, v, 0); |
| 270 |
} |
| 271 |
|
| 272 |
pw = pr_auth_getpwnam(pkt->pool, user); |
| 273 |
if (pw == NULL) { |
| 274 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 275 |
"no account for user '%s' found", user); |
| 276 |
|
| 277 |
pr_log_auth(PR_LOG_NOTICE, |
| 278 |
"USER %s: no such user found from %s [%s] to %s:%d", user, |
| 279 |
session.c->remote_name, pr_netaddr_get_ipstr(session.c->remote_addr), |
| 280 |
pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port); |
| 281 |
|
| 282 |
*send_userauth_fail = TRUE; |
| 283 |
errno = ENOENT; |
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
if (!have_signature) { |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
if (send_pubkey_ok(pubkey_algo, pubkey_data, pubkey_len) < 0) { |
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
return 0; |
| 296 |
|
| 297 |
} else { |
| 298 |
const unsigned char *id; |
| 299 |
unsigned char *buf2, *ptr2, *signature_data; |
| 300 |
uint32_t buflen2, bufsz2, id_len, signature_len; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (sftp_blacklist_reject_key(pkt->pool, pubkey_data, pubkey_len)) { |
| 309 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): requested public " |
| 310 |
"key is blacklisted", user); |
| 311 |
|
| 312 |
*send_userauth_fail = TRUE; |
| 313 |
errno = EPERM; |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
signature_len = sftp_msg_read_int(pkt->pool, buf, buflen); |
| 318 |
signature_data = sftp_msg_read_data(pkt->pool, buf, buflen, signature_len); |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
verify_notes = pr_table_nalloc(pkt->pool, 0, 1); |
| 336 |
if (sftp_keystore_verify_user_key(pkt->pool, user, pubkey_data, |
| 337 |
pubkey_len, verify_notes) < 0) { |
| 338 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): authentication " |
| 339 |
"via '%s' public key failed", user, pubkey_algo); |
| 340 |
|
| 341 |
*send_userauth_fail = TRUE; |
| 342 |
errno = EACCES; |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
id_len = sftp_session_get_id(&id); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
bufsz2 = buflen2 = pubkey_len + 1024; |
| 354 |
ptr2 = buf2 = sftp_msg_getbuf(pkt->pool, bufsz2); |
| 355 |
|
| 356 |
sftp_msg_write_data(&buf2, &buflen2, id, id_len, TRUE); |
| 357 |
sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_USER_AUTH_REQUEST); |
| 358 |
sftp_msg_write_string(&buf2, &buflen2, orig_user); |
| 359 |
|
| 360 |
if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_SERVICE_IN_PUBKEY_SIG)) { |
| 361 |
sftp_msg_write_string(&buf2, &buflen2, service); |
| 362 |
|
| 363 |
} else { |
| 364 |
sftp_msg_write_string(&buf2, &buflen2, "ssh-userauth"); |
| 365 |
} |
| 366 |
|
| 367 |
if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO)) { |
| 368 |
sftp_msg_write_string(&buf2, &buflen2, "publickey"); |
| 369 |
sftp_msg_write_bool(&buf2, &buflen2, TRUE); |
| 370 |
sftp_msg_write_string(&buf2, &buflen2, pubkey_algo); |
| 371 |
|
| 372 |
} else { |
| 373 |
sftp_msg_write_bool(&buf2, &buflen2, TRUE); |
| 374 |
} |
| 375 |
|
| 376 |
sftp_msg_write_data(&buf2, &buflen2, pubkey_data, pubkey_len, TRUE); |
| 377 |
|
| 378 |
details = pcalloc(pkt->pool, sizeof(struct sftp_verify_details)); |
| 379 |
if (sftp_keys_verify_signed_data(pkt->pool, pubkey_algo, pubkey_data, |
| 380 |
pubkey_len, signature_data, signature_len, (unsigned char *) ptr2, |
| 381 |
(bufsz2 - buflen2), details) < 0) { |
| 382 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 383 |
"failed to verify '%s' signature on public key auth request for " |
| 384 |
"user '%s'", pubkey_algo, orig_user); |
| 385 |
|
| 386 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature " |
| 387 |
"verification of '%s' public key failed", user, pubkey_algo); |
| 388 |
|
| 389 |
*send_userauth_fail = TRUE; |
| 390 |
errno = EACCES; |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
if (sftp_keys_permit_key(pkt->pool, pubkey_algo, orig_user, details, |
| 395 |
verify_notes) < 0) { |
| 396 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 397 |
"unable to permit '%s' signature on public key auth request for " |
| 398 |
"user '%s' due to security key policy", pubkey_algo, orig_user); |
| 399 |
|
| 400 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature " |
| 401 |
"validation of '%s' public key failed", user, pubkey_algo); |
| 402 |
|
| 403 |
*send_userauth_fail = TRUE; |
| 404 |
errno = EACCES; |
| 405 |
return 0; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if (pr_auth_authorize(pkt->pool, user) != PR_AUTH_OK) { |
| 415 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 416 |
"authentication for user '%s' failed: User not authorized", user); |
| 417 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User not authorized " |
| 418 |
"for login", user); |
| 419 |
*send_userauth_fail = TRUE; |
| 420 |
errno = EACCES; |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
if (fp != NULL) { |
| 425 |
register unsigned int i; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
for (i = 0; i < publickey_fps->nelts; i++) { |
| 431 |
char *fpi; |
| 432 |
|
| 433 |
fpi = ((char **) publickey_fps->elts)[i]; |
| 434 |
if (strcmp(fp, fpi) == 0) { |
| 435 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 436 |
"publickey request reused previously verified publickey " |
| 437 |
"(fingerprint %s), rejecting", fp); |
| 438 |
|
| 439 |
pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): public key request " |
| 440 |
"reused previously verified public key (fingerprint %s)", user, fp); |
| 441 |
|
| 442 |
*send_userauth_fail = TRUE; |
| 443 |
errno = EACCES; |
| 444 |
return 0; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
*((char **) push_array(publickey_fps)) = pstrdup(sftp_pool, fp); |
| 450 |
} |
| 451 |
|
| 452 |
return 1; |
| 453 |
} |
| 454 |
|
| 455 |
int sftp_auth_publickey_init(pool *p) { |
| 456 |
publickey_fps = make_array(p, 0, sizeof(char *)); |
| 457 |
|
| 458 |
return 0; |
| 459 |
} |