Преглед изворни кода

[base64] Add buffer size parameter to base64_encode() and base64_decode()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 9 година
родитељ
комит
1205721cbd
7 измењених фајлова са 84 додато и 77 уклоњено
  1. 46
    54
      src/core/base64.c
  2. 1
    1
      src/crypto/ocsp.c
  3. 3
    2
      src/include/ipxe/base64.h
  4. 2
    1
      src/net/tcp/httpcore.c
  5. 1
    1
      src/net/tcp/iscsi.c
  6. 2
    1
      src/net/validator.c
  7. 29
    17
      src/tests/base64_test.c

+ 46
- 54
src/core/base64.c Прегледај датотеку

@@ -43,80 +43,73 @@ static const char base64[64] =
43 43
  * Base64-encode data
44 44
  *
45 45
  * @v raw		Raw data
46
- * @v len		Length of raw data
47
- * @v encoded		Buffer for encoded string
48
- *
49
- * The buffer must be the correct length for the encoded string.  Use
50
- * something like
51
- *
52
- *     char buf[ base64_encoded_len ( len ) + 1 ];
53
- *
54
- * (the +1 is for the terminating NUL) to provide a buffer of the
55
- * correct size.
46
+ * @v raw_len		Length of raw data
47
+ * @v data		Buffer
48
+ * @v len		Length of buffer
49
+ * @ret len		Encoded length
56 50
  */
57
-void base64_encode ( const uint8_t *raw, size_t len, char *encoded ) {
51
+size_t base64_encode ( const void *raw, size_t raw_len, char *data,
52
+		       size_t len ) {
58 53
 	const uint8_t *raw_bytes = ( ( const uint8_t * ) raw );
59
-	uint8_t *encoded_bytes = ( ( uint8_t * ) encoded );
60
-	size_t raw_bit_len = ( 8 * len );
54
+	size_t raw_bit_len = ( 8 * raw_len );
55
+	size_t used = 0;
61 56
 	unsigned int bit;
62 57
 	unsigned int byte;
63 58
 	unsigned int shift;
64 59
 	unsigned int tmp;
65 60
 
66
-	for ( bit = 0 ; bit < raw_bit_len ; bit += 6 ) {
61
+	for ( bit = 0 ; bit < raw_bit_len ; bit += 6, used++ ) {
67 62
 		byte = ( bit / 8 );
68 63
 		shift = ( bit % 8 );
69 64
 		tmp = ( raw_bytes[byte] << shift );
70
-		if ( ( byte + 1 ) < len )
65
+		if ( ( byte + 1 ) < raw_len )
71 66
 			tmp |= ( raw_bytes[ byte + 1 ] >> ( 8 - shift ) );
72 67
 		tmp = ( ( tmp >> 2 ) & 0x3f );
73
-		*(encoded_bytes++) = base64[tmp];
68
+		if ( used < len )
69
+			data[used] = base64[tmp];
74 70
 	}
75
-	for ( ; ( bit % 8 ) != 0 ; bit += 6 )
76
-		*(encoded_bytes++) = '=';
77
-	*(encoded_bytes++) = '\0';
71
+	for ( ; ( bit % 8 ) != 0 ; bit += 6, used++ ) {
72
+		if ( used < len )
73
+			data[used] = '=';
74
+	}
75
+	if ( used < len )
76
+		data[used] = '\0';
77
+	if ( len )
78
+		data[ len - 1 ] = '\0'; /* Ensure terminator exists */
78 79
 
79
-	DBG ( "Base64-encoded to \"%s\":\n", encoded );
80
-	DBG_HDA ( 0, raw, len );
81
-	assert ( strlen ( encoded ) == base64_encoded_len ( len ) );
80
+	return used;
82 81
 }
83 82
 
84 83
 /**
85 84
  * Base64-decode string
86 85
  *
87 86
  * @v encoded		Encoded string
88
- * @v raw		Raw data
89
- * @ret len		Length of raw data, or negative error
90
- *
91
- * The buffer must be large enough to contain the decoded data.  Use
92
- * something like
93
- *
94
- *     char buf[ base64_decoded_max_len ( encoded ) ];
95
- *
96
- * to provide a buffer of the correct size.
87
+ * @v data		Buffer
88
+ * @v len		Length of buffer
89
+ * @ret len		Length of data, or negative error
97 90
  */
98
-int base64_decode ( const char *encoded, uint8_t *raw ) {
99
-	const uint8_t *encoded_bytes = ( ( const uint8_t * ) encoded );
100
-	uint8_t *raw_bytes = ( ( uint8_t * ) raw );
101
-	uint8_t encoded_byte;
91
+int base64_decode ( const char *encoded, void *data, size_t len ) {
92
+	const char *in = encoded;
93
+	uint8_t *out = data;
94
+	uint8_t in_char;
102 95
 	char *match;
103
-	int decoded;
96
+	int in_bits;
104 97
 	unsigned int bit = 0;
105 98
 	unsigned int pad_count = 0;
106
-	size_t len;
99
+	size_t offset;
107 100
 
108
-	/* Zero the raw data */
109
-	memset ( raw, 0, base64_decoded_max_len ( encoded ) );
101
+	/* Zero the output buffer */
102
+	memset ( data, 0, len );
110 103
 
111 104
 	/* Decode string */
112
-	while ( ( encoded_byte = *(encoded_bytes++) ) ) {
105
+	while ( ( in_char = *(in++) ) ) {
113 106
 
114 107
 		/* Ignore whitespace characters */
115
-		if ( isspace ( encoded_byte ) )
108
+		if ( isspace ( in_char ) )
116 109
 			continue;
117 110
 
118 111
 		/* Process pad characters */
119
-		if ( encoded_byte == '=' ) {
112
+		if ( in_char == '=' ) {
120 113
 			if ( pad_count >= 2 ) {
121 114
 				DBG ( "Base64-encoded string \"%s\" has too "
122 115
 				      "many pad characters\n", encoded );
@@ -133,18 +126,22 @@ int base64_decode ( const char *encoded, uint8_t *raw ) {
133 126
 		}
134 127
 
135 128
 		/* Process normal characters */
136
-		match = strchr ( base64, encoded_byte );
129
+		match = strchr ( base64, in_char );
137 130
 		if ( ! match ) {
138 131
 			DBG ( "Base64-encoded string \"%s\" contains invalid "
139
-			      "character '%c'\n", encoded, encoded_byte );
132
+			      "character '%c'\n", encoded, in_char );
140 133
 			return -EINVAL;
141 134
 		}
142
-		decoded = ( match - base64 );
135
+		in_bits = ( match - base64 );
143 136
 
144 137
 		/* Add to raw data */
145
-		decoded <<= 2;
146
-		raw_bytes[ bit / 8 ] |= ( decoded >> ( bit % 8 ) );
147
-		raw_bytes[ bit / 8 + 1 ] |= ( decoded << ( 8 - ( bit % 8 ) ) );
138
+		in_bits <<= 2;
139
+		offset = ( bit / 8 );
140
+		if ( offset < len )
141
+			out[offset] |= ( in_bits >> ( bit % 8 ) );
142
+		offset++;
143
+		if ( offset < len )
144
+			out[offset] |= ( in_bits << ( 8 - ( bit % 8 ) ) );
148 145
 		bit += 6;
149 146
 	}
150 147
 
@@ -154,12 +151,7 @@ int base64_decode ( const char *encoded, uint8_t *raw ) {
154 151
 		      "%d\n", encoded, bit );
155 152
 		return -EINVAL;
156 153
 	}
157
-	len = ( bit / 8 );
158
-
159
-	DBG ( "Base64-decoded \"%s\" to:\n", encoded );
160
-	DBG_HDA ( 0, raw, len );
161
-	assert ( len <= base64_decoded_max_len ( encoded ) );
162 154
 
163 155
 	/* Return length in bytes */
164
-	return ( len );
156
+	return ( bit / 8 );
165 157
 }

+ 1
- 1
src/crypto/ocsp.c Прегледај датотеку

@@ -233,7 +233,7 @@ static int ocsp_uri_string ( struct ocsp_check *ocsp ) {
233 233
 		goto err_path_base64;
234 234
 	}
235 235
 	base64_encode ( ocsp->request.builder.data, ocsp->request.builder.len,
236
-			path_base64_string );
236
+			path_base64_string, path_len );
237 237
 
238 238
 	/* URI-encode the Base64-encoded request */
239 239
 	memset ( &path_uri, 0, sizeof ( path_uri ) );

+ 3
- 2
src/include/ipxe/base64.h Прегледај датотеку

@@ -35,7 +35,8 @@ static inline size_t base64_decoded_max_len ( const char *encoded ) {
35 35
 	return ( ( ( strlen ( encoded ) + 4 - 1 ) / 4 ) * 3 );
36 36
 }
37 37
 
38
-extern void base64_encode ( const uint8_t *raw, size_t len, char *encoded );
39
-extern int base64_decode ( const char *encoded, uint8_t *raw );
38
+extern size_t base64_encode ( const void *raw, size_t raw_len, char *data,
39
+			      size_t len );
40
+extern int base64_decode ( const char *encoded, void *data, size_t len );
40 41
 
41 42
 #endif /* _IPXE_BASE64_H */

+ 2
- 1
src/net/tcp/httpcore.c Прегледај датотеку

@@ -1081,7 +1081,8 @@ static char * http_basic_auth ( struct http_request *http ) {
1081 1081
 	snprintf ( user_pw, sizeof ( user_pw ), "%s:%s", user, password );
1082 1082
 
1083 1083
 	/* Base64-encode the "user:password" string */
1084
-	base64_encode ( ( void * ) user_pw, user_pw_len, user_pw_base64 );
1084
+	base64_encode ( user_pw, user_pw_len, user_pw_base64,
1085
+			sizeof ( user_pw_base64 ) );
1085 1086
 
1086 1087
 	/* Generate the authorisation string */
1087 1088
 	len = asprintf ( &auth, "Authorization: Basic %s\r\n",

+ 1
- 1
src/net/tcp/iscsi.c Прегледај датотеку

@@ -845,7 +845,7 @@ static int iscsi_large_binary_decode ( const char *encoded, uint8_t *raw,
845 845
 		case 'x' :
846 846
 			return base16_decode ( encoded, raw, len );
847 847
 		case 'b' :
848
-			return base64_decode ( encoded, raw );
848
+			return base64_decode ( encoded, raw, len );
849 849
 		}
850 850
 	}
851 851
 

+ 2
- 1
src/net/validator.c Прегледај датотеку

@@ -254,7 +254,8 @@ static int validator_start_download ( struct validator *validator,
254 254
 	/* Generate URI string */
255 255
 	len = snprintf ( uri_string, uri_string_len, "%s/%08x.der?subject=",
256 256
 			 crosscert, crc );
257
-	base64_encode ( issuer->data, issuer->len, ( uri_string + len ) );
257
+	base64_encode ( issuer->data, issuer->len, ( uri_string + len ),
258
+			( uri_string_len - len ) );
258 259
 	DBGC ( validator, "VALIDATOR %p downloading cross-signed certificate "
259 260
 	       "from %s\n", validator, uri_string );
260 261
 

+ 29
- 17
src/tests/base64_test.c Прегледај датотеку

@@ -80,30 +80,42 @@ BASE64 ( random_test,
80 80
  * Report a base64 encoding test result
81 81
  *
82 82
  * @v test		Base64 test
83
+ * @v file		Test code file
84
+ * @v line		Test code line
83 85
  */
84
-#define base64_encode_ok( test ) do {					\
85
-	size_t len = base64_encoded_len ( (test)->len );		\
86
-	char buf[ len + 1 /* NUL */ ];					\
87
-	ok ( len == strlen ( (test)->encoded ) );			\
88
-	base64_encode ( (test)->data, (test)->len, buf );		\
89
-	ok ( strcmp ( (test)->encoded, buf ) == 0 );			\
90
-	} while ( 0 )
86
+static void base64_encode_okx ( struct base64_test *test, const char *file,
87
+				unsigned int line ) {
88
+	size_t len = base64_encoded_len ( test->len );
89
+	char buf[ len + 1 /* NUL */ ];
90
+	size_t check_len;
91
+
92
+	okx ( len == strlen ( test->encoded ), file, line );
93
+	check_len = base64_encode ( test->data, test->len, buf, sizeof ( buf ));
94
+	okx ( check_len == len, file, line );
95
+	okx ( strcmp ( test->encoded, buf ) == 0, file, line );
96
+}
97
+#define base64_encode_ok( test ) base64_encode_okx ( test, __FILE__, __LINE__ )
91 98
 
92 99
 /**
93 100
  * Report a base64 decoding test result
94 101
  *
95 102
  * @v test		Base64 test
103
+ * @v file		Test code file
104
+ * @v line		Test code line
96 105
  */
97
-#define base64_decode_ok( test ) do {					\
98
-	size_t max_len = base64_decoded_max_len ( (test)->encoded );	\
99
-	uint8_t buf[max_len];						\
100
-	int len;							\
101
-	len = base64_decode ( (test)->encoded, buf );			\
102
-	ok ( len >= 0 );						\
103
-	ok ( ( size_t ) len <= max_len );				\
104
-	ok ( ( size_t ) len == (test)->len );				\
105
-	ok ( memcmp ( (test)->data, buf, len ) == 0 );			\
106
-	} while ( 0 )
106
+static void base64_decode_okx ( struct base64_test *test, const char *file,
107
+				unsigned int line ) {
108
+	size_t max_len = base64_decoded_max_len ( test->encoded );
109
+	uint8_t buf[max_len];
110
+	int len;
111
+
112
+	len = base64_decode ( test->encoded, buf, sizeof ( buf ) );
113
+	okx ( len >= 0, file, line );
114
+	okx ( ( size_t ) len <= max_len, file, line );
115
+	okx ( ( size_t ) len == test->len, file, line );
116
+	okx ( memcmp ( test->data, buf, len ) == 0, file, line );
117
+}
118
+#define base64_decode_ok( test ) base64_decode_okx ( test, __FILE__, __LINE__ )
107 119
 
108 120
 /**
109 121
  * Perform Base64 self-tests

Loading…
Откажи
Сачувај