@@ -480,6 +480,24 @@ libssh2_userauth_password_ex(LIBSSH2_SESSION *session, const char *username,
480480 return rc ;
481481}
482482
483+ /*
484+ * upgrade_publickey_method
485+ *
486+ * Change method from ssh-rsa to rsa-sha2-256 is RSA SHA2 is supported.
487+ */
488+ static void upgrade_publickey_method (LIBSSH2_SESSION * session ,
489+ unsigned char * * method ,
490+ size_t * method_len ) {
491+ #if LIBSSH2_RSA_SHA2
492+ if (* method_len == 7 && memcmp (* method , "ssh-rsa" , 7 ) == 0 ) {
493+ LIBSSH2_FREE (session , * method );
494+ * method_len = 12 ;
495+ * method = LIBSSH2_ALLOC (session , 12 );
496+ memcpy (* method , "rsa-sha2-256" , 12 );
497+ }
498+ #endif
499+ }
500+
483501static int
484502memory_read_publickey (LIBSSH2_SESSION * session , unsigned char * * method ,
485503 size_t * method_len ,
@@ -546,6 +564,9 @@ memory_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
546564 * method = pubkey ;
547565 * method_len = sp1 - pubkey - 1 ;
548566
567+ /* upgrade to sha2 for rsa keys when supported */
568+ upgrade_publickey_method (session , method , method_len );
569+
549570 * pubkeydata = tmp ;
550571 * pubkeydata_len = tmp_len ;
551572
@@ -648,6 +669,9 @@ file_read_publickey(LIBSSH2_SESSION * session, unsigned char **method,
648669 * it a wash */
649670 * method = pubkey ;
650671 * method_len = sp1 - pubkey - 1 ;
672+
673+ /* upgrade to sha2 for rsa keys when supported */
674+ upgrade_publickey_method (session , method , method_len );
651675
652676 * pubkeydata = tmp ;
653677 * pubkeydata_len = tmp_len ;
@@ -1150,10 +1174,19 @@ _libssh2_userauth_publickey(LIBSSH2_SESSION *session,
11501174 * TODO: The data should match too but we don't check that. Should we?
11511175 */
11521176 else if (session -> userauth_pblc_method_len !=
1153- _libssh2_ntohu32 (pubkeydata ))
1154- return _libssh2_error (session , LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED ,
1177+ _libssh2_ntohu32 (pubkeydata )) {
1178+
1179+ // we accept mismatch if public key is "ssh-rsa" and
1180+ // method has prefix "rsa-sha2-" to satisfy RFC 8332
1181+ if (session -> userauth_pblc_method_len < 9 ||
1182+ _libssh2_ntohu32 (pubkeydata ) != 7 ||
1183+ memcmp (pubkeydata + 4 , "ssh-rsa" , 7 ) ||
1184+ memcmp (session -> userauth_pblc_method , "rsa-sha2-" , 9 )) {
1185+
1186+ return _libssh2_error (session , LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED ,
11551187 "Invalid public key" );
1156-
1188+ }
1189+ }
11571190 /*
11581191 * 45 = packet_type(1) + username_len(4) + servicename_len(4) +
11591192 * service_name(14)"ssh-connection" + authmethod_len(4) +
0 commit comments