Browse Source

[iscsi] Use generic base16 functions for iSCSI

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
7b267ee6db
1 changed files with 21 additions and 30 deletions
  1. 21
    30
      src/net/tcp/iscsi.c

+ 21
- 30
src/net/tcp/iscsi.c View File

35
 #include <ipxe/tcpip.h>
35
 #include <ipxe/tcpip.h>
36
 #include <ipxe/settings.h>
36
 #include <ipxe/settings.h>
37
 #include <ipxe/features.h>
37
 #include <ipxe/features.h>
38
+#include <ipxe/base16.h>
38
 #include <ipxe/iscsi.h>
39
 #include <ipxe/iscsi.h>
39
 
40
 
40
 /** @file
41
 /** @file
481
 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
482
 static int iscsi_build_login_request_strings ( struct iscsi_session *iscsi,
482
 					       void *data, size_t len ) {
483
 					       void *data, size_t len ) {
483
 	unsigned int used = 0;
484
 	unsigned int used = 0;
484
-	unsigned int i;
485
 	const char *auth_method;
485
 	const char *auth_method;
486
 
486
 
487
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
487
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_SECURITY ) {
508
 	}
508
 	}
509
 	
509
 	
510
 	if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
510
 	if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_RESPONSE ) ) {
511
+		char buf[ base16_encoded_len ( iscsi->chap.response_len ) + 1 ];
511
 		assert ( iscsi->initiator_username != NULL );
512
 		assert ( iscsi->initiator_username != NULL );
513
+		base16_encode ( iscsi->chap.response, iscsi->chap.response_len,
514
+				buf );
512
 		used += ssnprintf ( data + used, len - used,
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
 	if ( ( iscsi->status & ISCSI_STATUS_STRINGS_CHAP_CHALLENGE ) ) {
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
 		used += ssnprintf ( data + used, len - used,
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
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
530
 	if ( iscsi->status & ISCSI_STATUS_STRINGS_OPERATIONAL ) {
740
  */
737
  */
741
 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
738
 static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
742
 				       const char *value ) {
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
 	unsigned int i;
741
 	unsigned int i;
742
+	int len;
747
 
743
 
748
 	/* Check and strip leading "0x" */
744
 	/* Check and strip leading "0x" */
749
 	if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
745
 	if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
751
 		       iscsi, value );
747
 		       iscsi, value );
752
 		return -EPROTO_INVALID_CHAP_CHALLENGE;
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
 	/* Build CHAP response */
760
 	/* Build CHAP response */
770
 	DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );
761
 	DBGC ( iscsi, "iSCSI %p sending CHAP response\n", iscsi );

Loading…
Cancel
Save