Skip to content

Commit 8a16fd0

Browse files
pjaroszynskimcb30
authored andcommittedMay 28, 2010
[iscsi] Allow base64 encoding in large binary values
Modified-by: Michael Brown <mcb30@ipxe.org> Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent b3d8238 commit 8a16fd0

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed
 

‎src/net/tcp/iscsi.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
3636
#include <ipxe/settings.h>
3737
#include <ipxe/features.h>
3838
#include <ipxe/base16.h>
39+
#include <ipxe/base64.h>
3940
#include <ipxe/iscsi.h>
4041

4142
/** @file
@@ -56,7 +57,7 @@ FEATURE ( FEATURE_PROTOCOL, "iSCSI", DHCP_EB_FEATURE_ISCSI, 1 );
5657
#define EPERM_INITIATOR_AUTHORISATION ( EPERM | EUNIQ_02 )
5758
#define EPROTO_INVALID_CHAP_ALGORITHM ( EPROTO | EUNIQ_01 )
5859
#define EPROTO_INVALID_CHAP_IDENTIFIER ( EPROTO | EUNIQ_02 )
59-
#define EPROTO_INVALID_CHAP_CHALLENGE ( EPROTO | EUNIQ_03 )
60+
#define EPROTO_INVALID_LARGE_BINARY ( EPROTO | EUNIQ_03 )
6061
#define EPROTO_INVALID_CHAP_RESPONSE ( EPROTO | EUNIQ_04 )
6162

6263
/** iSCSI initiator name (explicitly specified) */
@@ -613,6 +614,41 @@ static int iscsi_tx_login_request ( struct iscsi_session *iscsi ) {
613614
return xfer_deliver_iob ( &iscsi->socket, iobuf );
614615
}
615616

617+
/**
618+
* Calculate maximum length of decoded large binary value
619+
*
620+
* @v encoded Encoded large binary value
621+
* @v max_raw_len Maximum length of raw data
622+
*/
623+
static inline size_t
624+
iscsi_large_binary_decoded_max_len ( const char *encoded ) {
625+
return ( strlen ( encoded ) ); /* Decoding never expands data */
626+
}
627+
628+
/**
629+
* Decode large binary value
630+
*
631+
* @v encoded Encoded large binary value
632+
* @v raw Raw data
633+
* @ret len Length of raw data, or negative error
634+
*/
635+
static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw ) {
636+
637+
if ( encoded[0] != '0' )
638+
return -EPROTO_INVALID_LARGE_BINARY;
639+
640+
switch ( encoded[1] ) {
641+
case 'x' :
642+
case 'X' :
643+
return base16_decode ( ( encoded + 2 ), raw );
644+
case 'b' :
645+
case 'B' :
646+
return base64_decode ( ( encoded + 2 ), raw );
647+
default:
648+
return -EPROTO_INVALID_LARGE_BINARY;
649+
}
650+
}
651+
616652
/**
617653
* Handle iSCSI TargetAddress text value
618654
*
@@ -737,24 +773,19 @@ static int iscsi_handle_chap_i_value ( struct iscsi_session *iscsi,
737773
*/
738774
static int iscsi_handle_chap_c_value ( struct iscsi_session *iscsi,
739775
const char *value ) {
740-
uint8_t buf[ strlen ( value ) ]; /* Decoding never expands data */
776+
uint8_t buf[ iscsi_large_binary_decoded_max_len ( value ) ];
741777
unsigned int i;
742-
int len;
743-
744-
/* Check and strip leading "0x" */
745-
if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
746-
DBGC ( iscsi, "iSCSI %p saw invalid CHAP challenge \"%s\"\n",
747-
iscsi, value );
748-
return -EPROTO_INVALID_CHAP_CHALLENGE;
749-
}
778+
size_t len;
779+
int rc;
750780

751781
/* Process challenge */
752-
len = base16_decode ( ( value + 2 ), buf );
753-
if ( len < 0 ) {
782+
rc = iscsi_large_binary_decode ( value, buf );
783+
if ( rc < 0 ) {
754784
DBGC ( iscsi, "iSCSI %p invalid CHAP challenge \"%s\": %s\n",
755-
iscsi, value, strerror ( len ) );
756-
return len;
785+
iscsi, value, strerror ( rc ) );
786+
return rc;
757787
}
788+
len = rc;
758789
chap_update ( &iscsi->chap, buf, len );
759790

760791
/* Build CHAP response */
@@ -812,7 +843,7 @@ static int iscsi_handle_chap_n_value ( struct iscsi_session *iscsi,
812843
*/
813844
static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
814845
const char *value ) {
815-
uint8_t buf[ strlen ( value ) ]; /* Decoding never expands data */
846+
uint8_t buf[ iscsi_large_binary_decoded_max_len ( value ) ];
816847
size_t len;
817848
int rc;
818849

@@ -832,15 +863,8 @@ static int iscsi_handle_chap_r_value ( struct iscsi_session *iscsi,
832863
( sizeof ( iscsi->chap_challenge ) - 1 ) );
833864
chap_respond ( &iscsi->chap );
834865

835-
/* Check and strip leading "0x" */
836-
if ( ( value[0] != '0' ) || ( value[1] != 'x' ) ) {
837-
DBGC ( iscsi, "iSCSI %p saw invalid CHAP response \"%s\"\n",
838-
iscsi, value );
839-
return -EPROTO_INVALID_CHAP_RESPONSE;
840-
}
841-
842866
/* Process response */
843-
rc = base16_decode ( ( value + 2 ), buf );
867+
rc = iscsi_large_binary_decode ( value, buf );
844868
if ( rc < 0 ) {
845869
DBGC ( iscsi, "iSCSI %p invalid CHAP response \"%s\": %s\n",
846870
iscsi, value, strerror ( rc ) );

0 commit comments

Comments
 (0)
Please sign in to comment.