@@ -403,8 +403,9 @@ handle_kexinit_msg(#ssh_msg_kexinit{} = CounterPart, #ssh_msg_kexinit{} = Own,
403403 key_exchange_first_msg (Algos # alg .kex ,
404404 Ssh # ssh {algorithms = Algos })
405405 catch
406- Class :Error ->
407- Msg = kexinit_error (Class , Error , client , Own , CounterPart ),
406+ Class :Reason0 ->
407+ Reason = ssh_lib :trim_reason (Reason0 ),
408+ Msg = kexinit_error (Class , Reason , client , Own , CounterPart , Ssh ),
408409 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , Msg )
409410 end ;
410411
@@ -420,31 +421,38 @@ handle_kexinit_msg(#ssh_msg_kexinit{} = CounterPart, #ssh_msg_kexinit{} = Own,
420421 Algos ->
421422 {ok , Ssh # ssh {algorithms = Algos }}
422423 catch
423- Class :Error ->
424- Msg = kexinit_error (Class , Error , server , Own , CounterPart ),
424+ Class :Reason0 ->
425+ Reason = ssh_lib :trim_reason (Reason0 ),
426+ Msg = kexinit_error (Class , Reason , server , Own , CounterPart , Ssh ),
425427 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , Msg )
426428 end .
427429
428- kexinit_error (Class , Error , Role , Own , CounterPart ) ->
430+ kexinit_error (Class , Error , Role , Own , CounterPart , Ssh ) ->
429431 {Fmt ,Args } =
430432 case {Class ,Error } of
431433 {error , {badmatch ,{false ,Alg }}} ->
432434 {Txt ,W ,C } = alg_info (Role , Alg ),
433- {" No common ~s algorithm,~n "
434- " we have:~n ~s~n "
435- " peer have:~n ~s~n " ,
436- [Txt ,
437- lists :join (" , " , element (W ,Own )),
438- lists :join (" , " , element (C ,CounterPart ))
439- ]};
435+ MsgFun =
436+ fun (debug ) ->
437+ {" No common ~s algorithm,~n "
438+ " we have:~n ~s~n "
439+ " peer have:~n ~s~n " ,
440+ [Txt ,
441+ lists :join (" , " , element (W ,Own )),
442+ lists :join (" , " , element (C ,CounterPart ))]};
443+ (_ ) ->
444+ {" No common ~s algorithm" , [Txt ]}
445+ end ,
446+ ? SELECT_MSG (MsgFun );
440447 _ ->
441448 {" Kexinit failed in ~p : ~p :~p " , [Role ,Class ,Error ]}
442449 end ,
443- try io_lib :format (Fmt , Args ) of
450+ try io_lib :format (Fmt , Args , [{ chars_limit , ssh_lib : max_log_len ( Ssh )}] ) of
444451 R -> R
445452 catch
446453 _ :_ ->
447- io_lib :format (" Kexinit failed in ~p : ~p :~p " , [Role , Class , Error ])
454+ io_lib :format (" Kexinit failed in ~p : ~p :~p " , [Role , Class , Error ],
455+ [{chars_limit , ssh_lib :max_log_len (Ssh )}])
448456 end .
449457
450458alg_info (client , Alg ) ->
@@ -596,14 +604,19 @@ handle_kexdh_init(#ssh_msg_kexdh_init{e = E},
596604 session_id = sid (Ssh1 , H )}};
597605 {error ,unsupported_sign_alg } ->
598606 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
599- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
600- )
607+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
608+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
601609 end ;
602610 true ->
603- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
611+ MsgFun =
612+ fun (debug ) ->
604613 io_lib :format (" Kexdh init failed, received 'e' out of bounds~n E=~p~n P=~p " ,
605- [E ,P ])
606- )
614+ [E ,P ], [{chars_limit , ssh_lib :max_log_len (Opts )}]);
615+ (_ ) ->
616+ io_lib :format (" Kexdh init failed, received 'e' out of bounds" , [],
617+ [{chars_limit , ssh_lib :max_log_len (Opts )}] )
618+ end ,
619+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
607620 end .
608621
609622handle_kexdh_reply (# ssh_msg_kexdh_reply {public_host_key = PeerPubHostKey ,
@@ -624,14 +637,15 @@ handle_kexdh_reply(#ssh_msg_kexdh_reply{public_host_key = PeerPubHostKey,
624637 session_id = sid (Ssh , H )})};
625638 Error ->
626639 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
627- io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ])
640+ io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ],
641+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
628642 )
629643 end ;
630644
631645 true ->
632646 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
633647 io_lib :format (" Kexdh init failed, received 'f' out of bounds~n F=~p~n P=~p " ,
634- [F ,P ])
648+ [F ,P ], [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}] )
635649 )
636650 end .
637651
@@ -657,7 +671,8 @@ handle_kex_dh_gex_request(#ssh_msg_kex_dh_gex_request{min = Min0,
657671 }};
658672 {error ,_ } ->
659673 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
660- io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[])
674+ io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[],
675+ [{chars_limit , ssh_lib :max_log_len (Opts )}])
661676 )
662677 end ;
663678
@@ -689,8 +704,8 @@ handle_kex_dh_gex_request(#ssh_msg_kex_dh_gex_request_old{n = NBits},
689704 }};
690705 {error ,_ } ->
691706 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
692- io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[])
693- )
707+ io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[],
708+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
694709 end ;
695710
696711handle_kex_dh_gex_request (_ , _ ) ->
@@ -716,7 +731,6 @@ handle_kex_dh_gex_group(#ssh_msg_kex_dh_gex_group{p = P, g = G}, Ssh0) ->
716731 {Public , Private } = generate_key (dh , [P ,G ,2 * Sz ]),
717732 {SshPacket , Ssh1 } =
718733 ssh_packet (# ssh_msg_kex_dh_gex_init {e = Public }, Ssh0 ), % Pub = G^Priv mod P (def)
719-
720734 {ok , SshPacket ,
721735 Ssh1 # ssh {keyex_key = {{Private , Public }, {G , P }}}}.
722736
@@ -747,19 +761,22 @@ handle_kex_dh_gex_init(#ssh_msg_kex_dh_gex_init{e = E},
747761 }};
748762 {error ,unsupported_sign_alg } ->
749763 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
750- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
751- )
764+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
765+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
752766 end ;
753767 true ->
754768 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
755- " Kexdh init failed, received 'k' out of bounds"
756- )
769+ " Kexdh init failed, received 'k' out of bounds" )
757770 end ;
758771 true ->
759- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
760- io_lib :format (" Kexdh gex init failed, received 'e' out of bounds~n E=~p~n P=~p " ,
761- [E ,P ])
762- )
772+ MsgFun =
773+ fun (debug ) ->
774+ io_lib :format (" Kexdh gex init failed, received 'e' out of bounds~n "
775+ " E=~p~n P=~p " , [E ,P ]);
776+ (_ ) ->
777+ io_lib :format (" Kexdh gex init failed, received 'e' out of bounds" , [])
778+ end ,
779+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
763780 end .
764781
765782handle_kex_dh_gex_reply (# ssh_msg_kex_dh_gex_reply {public_host_key = PeerPubHostKey ,
@@ -784,20 +801,18 @@ handle_kex_dh_gex_reply(#ssh_msg_kex_dh_gex_reply{public_host_key = PeerPubHostK
784801 session_id = sid (Ssh , H )})};
785802 Error ->
786803 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
787- io_lib :format (" Kexdh gex reply failed. Verify host key: ~p " ,[ Error ])
788- )
804+ io_lib :format (" Kexdh gex reply failed. Verify host key: ~p " ,
805+ [ Error ], [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}]) )
789806 end ;
790807
791808 true ->
792809 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
793- " Kexdh gex init failed, 'K' out of bounds"
794- )
810+ " Kexdh gex init failed, 'K' out of bounds" )
795811 end ;
796812 true ->
797813 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
798814 io_lib :format (" Kexdh gex init failed, received 'f' out of bounds~n F=~p~n P=~p " ,
799- [F ,P ])
800- )
815+ [F ,P ], [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]))
801816 end .
802817
803818% %%----------------------------------------------------------------
@@ -831,17 +846,25 @@ handle_kex_ecdh_init(#ssh_msg_kex_ecdh_init{q_c = PeerPublic},
831846 session_id = sid (Ssh1 , H )}};
832847 {error ,unsupported_sign_alg } ->
833848 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
834- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
835- )
849+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
850+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
836851 end
837852 catch
838- Class :Error ->
839- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
853+ Class :Reason0 ->
854+ Reason = ssh_lib :trim_reason (Reason0 ),
855+ MsgFun =
856+ fun (debug ) ->
840857 io_lib :format (" ECDH compute key failed in server: ~p :~p~n "
841858 " Kex: ~p , Curve: ~p~n "
842859 " PeerPublic: ~p " ,
843- [Class ,Error ,Kex ,Curve ,PeerPublic ])
844- )
860+ [Class ,Reason ,Kex ,Curve ,PeerPublic ],
861+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]);
862+ (_ ) ->
863+ io_lib :format (" ECDH compute key failed in server: ~p :~p " ,
864+ [Class ,Reason ],
865+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
866+ end ,
867+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
845868 end .
846869
847870handle_kex_ecdh_reply (# ssh_msg_kex_ecdh_reply {public_host_key = PeerPubHostKey ,
@@ -864,15 +887,14 @@ handle_kex_ecdh_reply(#ssh_msg_kex_ecdh_reply{public_host_key = PeerPubHostKey,
864887 session_id = sid (Ssh , H )})};
865888 Error ->
866889 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
867- io_lib :format (" ECDH reply failed. Verify host key: ~p " ,[Error ])
868- )
890+ io_lib :format (" ECDH reply failed. Verify host key: ~p " ,[Error ],
891+ [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}]) )
869892 end
870893 catch
871894 Class :Error ->
872895 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
873896 io_lib :format (" Peer ECDH public key seem invalid: ~p :~p " ,
874- [Class ,Error ])
875- )
897+ [Class ,Error ], [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]))
876898 end .
877899
878900
0 commit comments