@@ -602,28 +602,34 @@ handle_kexdh_init(#ssh_msg_kexdh_init{e = E},
602602 % % server
603603 {G , P } = dh_group (Kex ),
604604 if
605- 1 = <E , E = < (P - 1 ) ->
605+ 1 < E , E < (P - 1 ) ->
606606 Sz = dh_bits (Algs ),
607607 {Public , Private } = generate_key (dh , [P ,G ,2 * Sz ]),
608608 K = compute_key (dh , E , Private , [P ,G ]),
609- MyPrivHostKey = get_host_key (SignAlg , Opts ),
610- MyPubHostKey = ssh_file :extract_public_key (MyPrivHostKey ),
611- H = kex_hash (Ssh0 , MyPubHostKey , sha (Kex ), {E ,Public ,K }),
612- case sign (H , SignAlg , MyPrivHostKey , Ssh0 ) of
613- {ok ,H_SIG } ->
614- {SshPacket , Ssh1 } =
615- ssh_packet (# ssh_msg_kexdh_reply {public_host_key = {MyPubHostKey ,SignAlg },
616- f = Public ,
617- h_sig = H_SIG
618- }, Ssh0 ),
619- {ok , SshPacket , Ssh1 # ssh {keyex_key = {{Private , Public }, {G , P }},
620- shared_secret = ssh_bits :mpint (K ),
621- exchanged_hash = H ,
622- session_id = sid (Ssh1 , H )}};
623- {error ,unsupported_sign_alg } ->
609+ if
610+ 1 < K , K < (P - 1 ) ->
611+ MyPrivHostKey = get_host_key (SignAlg , Opts ),
612+ MyPubHostKey = ssh_file :extract_public_key (MyPrivHostKey ),
613+ H = kex_hash (Ssh0 , MyPubHostKey , sha (Kex ), {E ,Public ,K }),
614+ case sign (H , SignAlg , MyPrivHostKey , Ssh0 ) of
615+ {ok ,H_SIG } ->
616+ {SshPacket , Ssh1 } =
617+ ssh_packet (# ssh_msg_kexdh_reply {public_host_key = {MyPubHostKey ,SignAlg },
618+ f = Public ,
619+ h_sig = H_SIG
620+ }, Ssh0 ),
621+ {ok , SshPacket , Ssh1 # ssh {keyex_key = {{Private , Public }, {G , P }},
622+ shared_secret = ssh_bits :mpint (K ),
623+ exchanged_hash = H ,
624+ session_id = sid (Ssh1 , H )}};
625+ {error ,unsupported_sign_alg } ->
626+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
627+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
628+ [{chars_limit , ssh_lib :max_log_len (Opts )}]))
629+ end ;
630+ true ->
624631 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
625- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
626- [{chars_limit , ssh_lib :max_log_len (Opts )}]))
632+ " Key exchange failed, 'K' out of bounds" )
627633 end ;
628634 true ->
629635 MsgFun =
@@ -643,22 +649,28 @@ handle_kexdh_reply(#ssh_msg_kexdh_reply{public_host_key = PeerPubHostKey,
643649 # ssh {keyex_key = {{Private , Public }, {G , P }},
644650 algorithms = # alg {kex = Kex }} = Ssh0 ) ->
645651 % % client
646- if
647- 1 = <F , F = < (P - 1 )->
652+ if
653+ 1 < F , F < (P - 1 ) ->
648654 K = compute_key (dh , F , Private , [P ,G ]),
649- H = kex_hash (Ssh0 , PeerPubHostKey , sha (Kex ), {Public ,F ,K }),
650- case verify_host_key (Ssh0 , PeerPubHostKey , H , H_SIG ) of
651- ok ->
652- {SshPacket , Ssh } = ssh_packet (# ssh_msg_newkeys {}, Ssh0 ),
653- {ok , SshPacket , install_alg (snd , Ssh # ssh {shared_secret = ssh_bits :mpint (K ),
654- exchanged_hash = H ,
655- session_id = sid (Ssh , H )})};
656- Error ->
655+ if
656+ 1 < K , K < (P - 1 ) ->
657+ H = kex_hash (Ssh0 , PeerPubHostKey , sha (Kex ), {Public ,F ,K }),
658+ case verify_host_key (Ssh0 , PeerPubHostKey , H , H_SIG ) of
659+ ok ->
660+ {SshPacket , Ssh } = ssh_packet (# ssh_msg_newkeys {}, Ssh0 ),
661+ {ok , SshPacket , install_alg (snd , Ssh # ssh {shared_secret = ssh_bits :mpint (K ),
662+ exchanged_hash = H ,
663+ session_id = sid (Ssh , H )})};
664+ Error ->
665+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
666+ io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ],
667+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
668+ )
669+ end ;
670+ true ->
657671 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
658- io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ],
659- [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
660- )
661- end ;
672+ " Key exchange failed, 'K' out of bounds" )
673+ end ;
662674
663675 true ->
664676 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
@@ -760,7 +772,7 @@ handle_kex_dh_gex_init(#ssh_msg_kex_dh_gex_init{e = E},
760772 opts = Opts } = Ssh0 ) ->
761773 % % server
762774 if
763- 1 = <E , E = < (P - 1 ) ->
775+ 1 < E , E < (P - 1 ) ->
764776 K = compute_key (dh , E , Private , [P ,G ]),
765777 if
766778 1 < K , K < (P - 1 ) ->
@@ -805,8 +817,8 @@ handle_kex_dh_gex_reply(#ssh_msg_kex_dh_gex_reply{public_host_key = PeerPubHostK
805817 algorithms = # alg {kex = Kex }} =
806818 Ssh0 ) ->
807819 % % client
808- if
809- 1 = <F , F = < (P - 1 )->
820+ if
821+ 1 < F , F < (P - 1 ) ->
810822 K = compute_key (dh , F , Private , [P ,G ]),
811823 if
812824 1 < K , K < (P - 1 ) ->
0 commit comments