@@ -404,8 +404,9 @@ handle_kexinit_msg(#ssh_msg_kexinit{} = CounterPart, #ssh_msg_kexinit{} = Own,
404404 key_exchange_first_msg (Algos # alg .kex ,
405405 Ssh # ssh {algorithms = Algos })
406406 catch
407- Class :Error ->
408- Msg = kexinit_error (Class , Error , client , Own , CounterPart ),
407+ Class :Reason0 ->
408+ Reason = ssh_lib :trim_reason (Reason0 ),
409+ Msg = kexinit_error (Class , Reason , client , Own , CounterPart , Ssh ),
409410 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , Msg )
410411 end ;
411412
@@ -421,31 +422,38 @@ handle_kexinit_msg(#ssh_msg_kexinit{} = CounterPart, #ssh_msg_kexinit{} = Own,
421422 Algos ->
422423 {ok , Ssh # ssh {algorithms = Algos }}
423424 catch
424- Class :Error ->
425- Msg = kexinit_error (Class , Error , server , Own , CounterPart ),
425+ Class :Reason0 ->
426+ Reason = ssh_lib :trim_reason (Reason0 ),
427+ Msg = kexinit_error (Class , Reason , server , Own , CounterPart , Ssh ),
426428 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , Msg )
427429 end .
428430
429- kexinit_error (Class , Error , Role , Own , CounterPart ) ->
431+ kexinit_error (Class , Error , Role , Own , CounterPart , Ssh ) ->
430432 {Fmt ,Args } =
431433 case {Class ,Error } of
432434 {error , {badmatch ,{false ,Alg }}} ->
433435 {Txt ,W ,C } = alg_info (Role , Alg ),
434- {" No common ~s algorithm,~n "
435- " we have:~n ~s~n "
436- " peer have:~n ~s~n " ,
437- [Txt ,
438- lists :join (" , " , element (W ,Own )),
439- lists :join (" , " , element (C ,CounterPart ))
440- ]};
436+ MsgFun =
437+ fun (debug ) ->
438+ {" No common ~s algorithm,~n "
439+ " we have:~n ~s~n "
440+ " peer have:~n ~s~n " ,
441+ [Txt ,
442+ lists :join (" , " , element (W ,Own )),
443+ lists :join (" , " , element (C ,CounterPart ))]};
444+ (_ ) ->
445+ {" No common ~s algorithm" , [Txt ]}
446+ end ,
447+ ? SELECT_MSG (MsgFun );
441448 _ ->
442449 {" Kexinit failed in ~p : ~p :~p " , [Role ,Class ,Error ]}
443450 end ,
444- try io_lib :format (Fmt , Args ) of
451+ try io_lib :format (Fmt , Args , [{ chars_limit , ssh_lib : max_log_len ( Ssh )}] ) of
445452 R -> R
446453 catch
447454 _ :_ ->
448- io_lib :format (" Kexinit failed in ~p : ~p :~p " , [Role , Class , Error ])
455+ io_lib :format (" Kexinit failed in ~p : ~p :~p " , [Role , Class , Error ],
456+ [{chars_limit , ssh_lib :max_log_len (Ssh )}])
449457 end .
450458
451459alg_info (client , Alg ) ->
@@ -597,14 +605,19 @@ handle_kexdh_init(#ssh_msg_kexdh_init{e = E},
597605 session_id = sid (Ssh1 , H )}};
598606 {error ,unsupported_sign_alg } ->
599607 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
600- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
601- )
608+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
609+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
602610 end ;
603611 true ->
604- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
612+ MsgFun =
613+ fun (debug ) ->
605614 io_lib :format (" Kexdh init failed, received 'e' out of bounds~n E=~p~n P=~p " ,
606- [E ,P ])
607- )
615+ [E ,P ], [{chars_limit , ssh_lib :max_log_len (Opts )}]);
616+ (_ ) ->
617+ io_lib :format (" Kexdh init failed, received 'e' out of bounds" , [],
618+ [{chars_limit , ssh_lib :max_log_len (Opts )}] )
619+ end ,
620+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
608621 end .
609622
610623handle_kexdh_reply (# ssh_msg_kexdh_reply {public_host_key = PeerPubHostKey ,
@@ -625,14 +638,15 @@ handle_kexdh_reply(#ssh_msg_kexdh_reply{public_host_key = PeerPubHostKey,
625638 session_id = sid (Ssh , H )})};
626639 Error ->
627640 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
628- io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ])
641+ io_lib :format (" Kexdh init failed. Verify host key: ~p " ,[Error ],
642+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
629643 )
630644 end ;
631645
632646 true ->
633647 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
634648 io_lib :format (" Kexdh init failed, received 'f' out of bounds~n F=~p~n P=~p " ,
635- [F ,P ])
649+ [F ,P ], [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}] )
636650 )
637651 end .
638652
@@ -658,7 +672,8 @@ handle_kex_dh_gex_request(#ssh_msg_kex_dh_gex_request{min = Min0,
658672 }};
659673 {error ,_ } ->
660674 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
661- io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[])
675+ io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[],
676+ [{chars_limit , ssh_lib :max_log_len (Opts )}])
662677 )
663678 end ;
664679
@@ -690,8 +705,8 @@ handle_kex_dh_gex_request(#ssh_msg_kex_dh_gex_request_old{n = NBits},
690705 }};
691706 {error ,_ } ->
692707 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
693- io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[])
694- )
708+ io_lib :format (" No possible diffie-hellman-group-exchange group found" ,[],
709+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
695710 end ;
696711
697712handle_kex_dh_gex_request (_ , _ ) ->
@@ -717,7 +732,6 @@ handle_kex_dh_gex_group(#ssh_msg_kex_dh_gex_group{p = P, g = G}, Ssh0) ->
717732 {Public , Private } = generate_key (dh , [P ,G ,2 * Sz ]),
718733 {SshPacket , Ssh1 } =
719734 ssh_packet (# ssh_msg_kex_dh_gex_init {e = Public }, Ssh0 ), % Pub = G^Priv mod P (def)
720-
721735 {ok , SshPacket ,
722736 Ssh1 # ssh {keyex_key = {{Private , Public }, {G , P }}}}.
723737
@@ -748,19 +762,22 @@ handle_kex_dh_gex_init(#ssh_msg_kex_dh_gex_init{e = E},
748762 }};
749763 {error ,unsupported_sign_alg } ->
750764 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
751- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
752- )
765+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
766+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
753767 end ;
754768 true ->
755769 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
756- " Kexdh init failed, received 'k' out of bounds"
757- )
770+ " Kexdh init failed, received 'k' out of bounds" )
758771 end ;
759772 true ->
760- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
761- io_lib :format (" Kexdh gex init failed, received 'e' out of bounds~n E=~p~n P=~p " ,
762- [E ,P ])
763- )
773+ MsgFun =
774+ fun (debug ) ->
775+ io_lib :format (" Kexdh gex init failed, received 'e' out of bounds~n "
776+ " E=~p~n P=~p " , [E ,P ]);
777+ (_ ) ->
778+ io_lib :format (" Kexdh gex init failed, received 'e' out of bounds" , [])
779+ end ,
780+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
764781 end .
765782
766783handle_kex_dh_gex_reply (# ssh_msg_kex_dh_gex_reply {public_host_key = PeerPubHostKey ,
@@ -785,20 +802,18 @@ handle_kex_dh_gex_reply(#ssh_msg_kex_dh_gex_reply{public_host_key = PeerPubHostK
785802 session_id = sid (Ssh , H )})};
786803 Error ->
787804 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
788- io_lib :format (" Kexdh gex reply failed. Verify host key: ~p " ,[ Error ])
789- )
805+ io_lib :format (" Kexdh gex reply failed. Verify host key: ~p " ,
806+ [ Error ], [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}]) )
790807 end ;
791808
792809 true ->
793810 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
794- " Kexdh gex init failed, 'K' out of bounds"
795- )
811+ " Kexdh gex init failed, 'K' out of bounds" )
796812 end ;
797813 true ->
798814 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
799815 io_lib :format (" Kexdh gex init failed, received 'f' out of bounds~n F=~p~n P=~p " ,
800- [F ,P ])
801- )
816+ [F ,P ], [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]))
802817 end .
803818
804819% %%----------------------------------------------------------------
@@ -832,17 +847,25 @@ handle_kex_ecdh_init(#ssh_msg_kex_ecdh_init{q_c = PeerPublic},
832847 session_id = sid (Ssh1 , H )}};
833848 {error ,unsupported_sign_alg } ->
834849 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
835- io_lib :format (" Unsupported algorithm ~p " , [SignAlg ])
836- )
850+ io_lib :format (" Unsupported algorithm ~p " , [SignAlg ],
851+ [{ chars_limit , ssh_lib : max_log_len ( Opts )}]) )
837852 end
838853 catch
839- Class :Error ->
840- ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
854+ Class :Reason0 ->
855+ Reason = ssh_lib :trim_reason (Reason0 ),
856+ MsgFun =
857+ fun (debug ) ->
841858 io_lib :format (" ECDH compute key failed in server: ~p :~p~n "
842859 " Kex: ~p , Curve: ~p~n "
843860 " PeerPublic: ~p " ,
844- [Class ,Error ,Kex ,Curve ,PeerPublic ])
845- )
861+ [Class ,Reason ,Kex ,Curve ,PeerPublic ],
862+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]);
863+ (_ ) ->
864+ io_lib :format (" ECDH compute key failed in server: ~p :~p " ,
865+ [Class ,Reason ],
866+ [{chars_limit , ssh_lib :max_log_len (Ssh0 )}])
867+ end ,
868+ ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED , ? SELECT_MSG (MsgFun ))
846869 end .
847870
848871handle_kex_ecdh_reply (# ssh_msg_kex_ecdh_reply {public_host_key = PeerPubHostKey ,
@@ -865,15 +888,14 @@ handle_kex_ecdh_reply(#ssh_msg_kex_ecdh_reply{public_host_key = PeerPubHostKey,
865888 session_id = sid (Ssh , H )})};
866889 Error ->
867890 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
868- io_lib :format (" ECDH reply failed. Verify host key: ~p " ,[Error ])
869- )
891+ io_lib :format (" ECDH reply failed. Verify host key: ~p " ,[Error ],
892+ [{ chars_limit , ssh_lib : max_log_len ( Ssh0 )}]) )
870893 end
871894 catch
872895 Class :Error ->
873896 ? DISCONNECT (? SSH_DISCONNECT_KEY_EXCHANGE_FAILED ,
874897 io_lib :format (" Peer ECDH public key seem invalid: ~p :~p " ,
875- [Class ,Error ])
876- )
898+ [Class ,Error ], [{chars_limit , ssh_lib :max_log_len (Ssh0 )}]))
877899 end .
878900
879901
0 commit comments