Bladeren bron

[iscsi] Use generic base16 functions for iSCSI

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 jaren geleden
bovenliggende
commit
7b267ee6db
1 gewijzigde bestanden met toevoegingen van 21 en 30 verwijderingen
  1. 21
    30
      src/net/tcp/iscsi.c

+ 21
- 30
src/net/tcp/iscsi.c Bestand weergeven

@@ -35,6 +35,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
35 35
 #include <ipxe/tcpip.h>
36 36
 #include <ipxe/settings.h>
37 37
 #include <ipxe/features.h>
38
+#include <ipxe/base16.h>
38 39
 #include <ipxe/iscsi.h>
39 40
 
40 41
 /** @file
@@ -481,7 +482,6 @@ static int iscsi_tx_data_out ( struct iscsi_session *iscsi ) {
481 482
 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
482 483
 					       void *data, size_t len ) {
483 484
 	unsigned int used = 0;
484
-	unsigned int i;
485 485
 	const char *auth_method;
486 486
 
487 487
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
@@ -508,26 +508,23 @@ static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
508 508
 	}
509 509
 	
510 510
 	if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
511
+		char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
511 512
 		assert ( iscsi->initiator_username != NULL );
513
+		base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
514
+				buf );
512 515
 		used += ssnprintf ( data + used, len - used,
513
-				    "CHAP_N=%s%cCHAP_R=0x",
514
-				    iscsi->initiator_username, 0 );
515
-		for ( i = 0 ; i < iscsi->chap.response_len ; i++ ) {
516
-			used += ssnprintf ( data + used, len - used, "%02x",
517
-					    iscsi->chap.response[i] );
518
-		}
519
-		used += ssnprintf ( data + used, len - used, "%c", 0 );
516
+				    "CHAP_N=%s%cCHAP_R=0x%s%c",
517
+				    iscsi->initiator_username, 0, buf, 0 );
520 518
 	}
521 519
 
522 520
 	if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
521
+		size_t challenge_len = ( sizeof ( iscsi->chap_challenge ) - 1 );
522
+		char buf[ base16_encoded_len ( challenge_len ) + 1 ];
523
+		base16_encode ( ( iscsi->chap_challenge + 1 ), challenge_len,
524
+				buf );
523 525
 		used += ssnprintf ( data + used, len - used,
524
-				    "CHAP_I=%d%cCHAP_C=0x",
525
-				    iscsi->chap_challenge[0], 0 );
526
-		for ( i = 1 ; i < sizeof ( iscsi->chap_challenge ) ; i++ ) {
527
-			used += ssnprintf ( data + used, len - used, "%02x",
528
-					    iscsi->chap_challenge[i] );
529
-		}
530
-		used += ssnprintf ( data + used, len - used, "%c", 0 );
526
+				    "CHAP_I=%d%cCHAP_C=0x%s%c",
527
+				    iscsi->chap_challenge[0], 0, buf, 0 );
531 528
 	}
532 529
 
533 530
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
@@ -740,10 +737,9 @@ static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
740 737
  */
741 738
 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
742 739
 				       const char *value ) {
743
-	char buf[3];
744
-	char *endp;
745
-	uint8_t byte;
740
+	uint8_t buf[ strlen ( value ) ]; /* Decoding never expands data */
746 741
 	unsigned int i;
742
+	int len;
747 743
 
748 744
 	/* Check and strip leading "0x" */
749 745
 	if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
@@ -751,20 +747,15 @@ static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
751 747
 		       iscsi, value );
752 748
 		return -EPROTO_INVALID_CHAP_CHALLENGE;
753 749
 	}
754
-	value += 2;
755 750
 
756
-	/* Process challenge an octet at a time */
757
-	for ( ; ( value[0] && value[1] ) ; value += 2 ) {
758
-		memcpy ( buf, value, 2 );
759
-		buf[2] = 0;
760
-		byte = strtoul ( buf, &endp, 16 );
761
-		if ( *endp != '\0' ) {
762
-			DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge "
763
-			       "byte \"%s\"\n", iscsi, buf );
764
-			return -EPROTO_INVALID_CHAP_CHALLENGE;
765
-		}
766
-		chap_update ( &iscsi->chap, &byte, sizeof ( byte ) );
751
+	/* Process challenge */
752
+	len = base16_decode ( ( value + 2 ), buf );
753
+	if ( len < 0 ) {
754
+		DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
755
+		       iscsi, value, strerror ( len ) );
756
+		return len;
767 757
 	}
758
+	chap_update ( &iscsi->chap, buf, len );
768 759
 
769 760
 	/* Build CHAP response */
770 761
 	DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );

Laden…
Annuleren
Opslaan