|
Lines 253-258
ssh_alloc_session_state(void)
Link Here
|
| 253 |
return NULL; |
253 |
return NULL; |
| 254 |
} |
254 |
} |
| 255 |
|
255 |
|
|
|
256 |
/* Returns nonzero if rekeying is in progress */ |
| 257 |
int |
| 258 |
ssh_packet_is_rekeying(struct ssh *ssh) |
| 259 |
{ |
| 260 |
return ssh->state->rekeying || |
| 261 |
(ssh->kex != NULL && ssh->kex->done == 0); |
| 262 |
} |
| 263 |
|
| 256 |
/* |
264 |
/* |
| 257 |
* Sets the descriptors used for communication. Disables encryption until |
265 |
* Sets the descriptors used for communication. Disables encryption until |
| 258 |
* packet_set_encryption_key is called. |
266 |
* packet_set_encryption_key is called. |
|
Lines 1016-1021
ssh_set_newkeys(struct ssh *ssh, int mode)
Link Here
|
| 1016 |
return 0; |
1024 |
return 0; |
| 1017 |
} |
1025 |
} |
| 1018 |
|
1026 |
|
|
|
1027 |
#define MAX_PACKETS (1U<<31) |
| 1028 |
static int |
| 1029 |
ssh_packet_need_rekeying(struct ssh *ssh, u_int outbound_packet_len) |
| 1030 |
{ |
| 1031 |
struct session_state *state = ssh->state; |
| 1032 |
u_int32_t out_blocks; |
| 1033 |
|
| 1034 |
/* XXX client can't cope with rekeying pre-auth */ |
| 1035 |
if (!state->after_authentication) |
| 1036 |
return 0; |
| 1037 |
|
| 1038 |
/* Haven't keyed yet or KEX in progress. */ |
| 1039 |
if (ssh->kex == NULL || ssh_packet_is_rekeying(ssh)) |
| 1040 |
return 0; |
| 1041 |
|
| 1042 |
/* Peer can't rekey */ |
| 1043 |
if (ssh->compat & SSH_BUG_NOREKEY) |
| 1044 |
return 0; |
| 1045 |
|
| 1046 |
/* |
| 1047 |
* Permit one packet in or out per rekey - this allows us to |
| 1048 |
* make progress when rekey limits are very small. |
| 1049 |
*/ |
| 1050 |
if (state->p_send.packets == 0 && state->p_read.packets == 0) |
| 1051 |
return 0; |
| 1052 |
|
| 1053 |
/* Time-based rekeying */ |
| 1054 |
if (state->rekey_interval != 0 && |
| 1055 |
state->rekey_time + state->rekey_interval <= monotime()) |
| 1056 |
return 1; |
| 1057 |
|
| 1058 |
/* Always rekey when MAX_PACKETS sent in either direction */ |
| 1059 |
if (state->p_send.packets > MAX_PACKETS || |
| 1060 |
state->p_read.packets > MAX_PACKETS) |
| 1061 |
return 1; |
| 1062 |
|
| 1063 |
/* Rekey after (cipher-specific) maxiumum blocks */ |
| 1064 |
out_blocks = roundup(outbound_packet_len, |
| 1065 |
state->newkeys[MODE_IN]->enc.block_size); |
| 1066 |
return (state->max_blocks_out && |
| 1067 |
(state->p_send.blocks + out_blocks > state->max_blocks_out)) || |
| 1068 |
(state->max_blocks_in && |
| 1069 |
(state->p_read.blocks > state->max_blocks_in)); |
| 1070 |
} |
| 1071 |
|
| 1019 |
/* |
1072 |
/* |
| 1020 |
* Delayed compression for SSH2 is enabled after authentication: |
1073 |
* Delayed compression for SSH2 is enabled after authentication: |
| 1021 |
* This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
1074 |
* This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent, |
|
Lines 1219-1253
ssh_packet_send2_wrapped(struct ssh *ssh)
Link Here
|
| 1219 |
return r; |
1272 |
return r; |
| 1220 |
} |
1273 |
} |
| 1221 |
|
1274 |
|
|
|
1275 |
/* returns non-zero if the specified packet type is usec by KEX */ |
| 1276 |
static int |
| 1277 |
ssh_packet_type_is_kex(u_char type) |
| 1278 |
{ |
| 1279 |
return |
| 1280 |
type >= SSH2_MSG_TRANSPORT_MIN && |
| 1281 |
type <= SSH2_MSG_TRANSPORT_MAX && |
| 1282 |
type != SSH2_MSG_SERVICE_REQUEST && |
| 1283 |
type != SSH2_MSG_SERVICE_ACCEPT && |
| 1284 |
type != SSH2_MSG_EXT_INFO; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1222 |
int |
1288 |
int |
| 1223 |
ssh_packet_send2(struct ssh *ssh) |
1289 |
ssh_packet_send2(struct ssh *ssh) |
| 1224 |
{ |
1290 |
{ |
| 1225 |
struct session_state *state = ssh->state; |
1291 |
struct session_state *state = ssh->state; |
| 1226 |
struct packet *p; |
1292 |
struct packet *p; |
| 1227 |
u_char type; |
1293 |
u_char type; |
| 1228 |
int r; |
1294 |
int r, need_rekey; |
| 1229 |
|
1295 |
|
|
|
1296 |
if (sshbuf_len(state->outgoing_packet) < 6) |
| 1297 |
return SSH_ERR_INTERNAL_ERROR; |
| 1230 |
type = sshbuf_ptr(state->outgoing_packet)[5]; |
1298 |
type = sshbuf_ptr(state->outgoing_packet)[5]; |
|
|
1299 |
need_rekey = !ssh_packet_type_is_kex(type) && |
| 1300 |
ssh_packet_need_rekeying(ssh, sshbuf_len(state->outgoing_packet)); |
| 1231 |
|
1301 |
|
| 1232 |
/* during rekeying we can only send key exchange messages */ |
1302 |
/* |
| 1233 |
if (state->rekeying) { |
1303 |
* During rekeying we can only send key exchange messages. |
| 1234 |
if ((type < SSH2_MSG_TRANSPORT_MIN) || |
1304 |
* Queue everything else. |
| 1235 |
(type > SSH2_MSG_TRANSPORT_MAX) || |
1305 |
*/ |
| 1236 |
(type == SSH2_MSG_SERVICE_REQUEST) || |
1306 |
if ((need_rekey || state->rekeying) && !ssh_packet_type_is_kex(type)) { |
| 1237 |
(type == SSH2_MSG_SERVICE_ACCEPT) || |
1307 |
if (need_rekey) |
| 1238 |
(type == SSH2_MSG_EXT_INFO)) { |
1308 |
debug3("%s: rekex triggered", __func__); |
| 1239 |
debug("enqueue packet: %u", type); |
1309 |
debug("enqueue packet: %u", type); |
| 1240 |
p = calloc(1, sizeof(*p)); |
1310 |
p = calloc(1, sizeof(*p)); |
| 1241 |
if (p == NULL) |
1311 |
if (p == NULL) |
| 1242 |
return SSH_ERR_ALLOC_FAIL; |
1312 |
return SSH_ERR_ALLOC_FAIL; |
| 1243 |
p->type = type; |
1313 |
p->type = type; |
| 1244 |
p->payload = state->outgoing_packet; |
1314 |
p->payload = state->outgoing_packet; |
| 1245 |
TAILQ_INSERT_TAIL(&state->outgoing, p, next); |
1315 |
TAILQ_INSERT_TAIL(&state->outgoing, p, next); |
| 1246 |
state->outgoing_packet = sshbuf_new(); |
1316 |
state->outgoing_packet = sshbuf_new(); |
| 1247 |
if (state->outgoing_packet == NULL) |
1317 |
if (state->outgoing_packet == NULL) |
| 1248 |
return SSH_ERR_ALLOC_FAIL; |
1318 |
return SSH_ERR_ALLOC_FAIL; |
| 1249 |
return 0; |
1319 |
if (need_rekey) { |
|
|
1320 |
/* |
| 1321 |
* This packet triggered a rekey, so send the |
| 1322 |
* KEXINIT now. |
| 1323 |
* NB. reenters this function via kex_start_rekex(). |
| 1324 |
*/ |
| 1325 |
return kex_start_rekex(ssh); |
| 1250 |
} |
1326 |
} |
|
|
1327 |
return 0; |
| 1251 |
} |
1328 |
} |
| 1252 |
|
1329 |
|
| 1253 |
/* rekeying starts with sending KEXINIT */ |
1330 |
/* rekeying starts with sending KEXINIT */ |
|
Lines 1263-1272
ssh_packet_send2(struct ssh *ssh)
Link Here
|
| 1263 |
state->rekey_time = monotime(); |
1340 |
state->rekey_time = monotime(); |
| 1264 |
while ((p = TAILQ_FIRST(&state->outgoing))) { |
1341 |
while ((p = TAILQ_FIRST(&state->outgoing))) { |
| 1265 |
type = p->type; |
1342 |
type = p->type; |
|
|
1343 |
/* |
| 1344 |
* If this packet triggers a rekex, then skip the |
| 1345 |
* remaining packets in the queue for now. |
| 1346 |
* NB. re-enters this function via kex_start_rekex. |
| 1347 |
*/ |
| 1348 |
if (ssh_packet_need_rekeying(ssh, |
| 1349 |
sshbuf_len(p->payload))) { |
| 1350 |
debug3("%s: queued packet triggered rekex", |
| 1351 |
__func__); |
| 1352 |
return kex_start_rekex(ssh); |
| 1353 |
} |
| 1266 |
debug("dequeue packet: %u", type); |
1354 |
debug("dequeue packet: %u", type); |
| 1267 |
sshbuf_free(state->outgoing_packet); |
1355 |
sshbuf_free(state->outgoing_packet); |
| 1268 |
state->outgoing_packet = p->payload; |
1356 |
state->outgoing_packet = p->payload; |
| 1269 |
TAILQ_REMOVE(&state->outgoing, p, next); |
1357 |
TAILQ_REMOVE(&state->outgoing, p, next); |
|
|
1358 |
memset(p, 0, sizeof(*p)); |
| 1270 |
free(p); |
1359 |
free(p); |
| 1271 |
if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
1360 |
if ((r = ssh_packet_send2_wrapped(ssh)) != 0) |
| 1272 |
return r; |
1361 |
return r; |
|
Lines 1770-1775
ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Link Here
|
| 1770 |
#endif |
1859 |
#endif |
| 1771 |
/* reset for next packet */ |
1860 |
/* reset for next packet */ |
| 1772 |
state->packlen = 0; |
1861 |
state->packlen = 0; |
|
|
1862 |
|
| 1863 |
/* do we need to rekey? */ |
| 1864 |
if (ssh_packet_need_rekeying(ssh, 0)) { |
| 1865 |
debug3("%s: rekex triggered", __func__); |
| 1866 |
if ((r = kex_start_rekex(ssh)) != 0) |
| 1867 |
return r; |
| 1868 |
} |
| 1773 |
out: |
1869 |
out: |
| 1774 |
return r; |
1870 |
return r; |
| 1775 |
} |
1871 |
} |
|
Lines 2246-2270
ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
Link Here
|
| 2246 |
} |
2342 |
} |
| 2247 |
} |
2343 |
} |
| 2248 |
|
2344 |
|
| 2249 |
#define MAX_PACKETS (1U<<31) |
|
|
| 2250 |
int |
| 2251 |
ssh_packet_need_rekeying(struct ssh *ssh) |
| 2252 |
{ |
| 2253 |
struct session_state *state = ssh->state; |
| 2254 |
|
| 2255 |
if (ssh->compat & SSH_BUG_NOREKEY) |
| 2256 |
return 0; |
| 2257 |
return |
| 2258 |
(state->p_send.packets > MAX_PACKETS) || |
| 2259 |
(state->p_read.packets > MAX_PACKETS) || |
| 2260 |
(state->max_blocks_out && |
| 2261 |
(state->p_send.blocks > state->max_blocks_out)) || |
| 2262 |
(state->max_blocks_in && |
| 2263 |
(state->p_read.blocks > state->max_blocks_in)) || |
| 2264 |
(state->rekey_interval != 0 && state->rekey_time + |
| 2265 |
state->rekey_interval <= monotime()); |
| 2266 |
} |
| 2267 |
|
| 2268 |
void |
2345 |
void |
| 2269 |
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds) |
2346 |
ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds) |
| 2270 |
{ |
2347 |
{ |