Skip to content

Commit 70e053f

Browse files
committed
Fix unsafe base64url decode
1 parent e27cacb commit 70e053f

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/crypto/kmc/base64url.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,23 @@ int32_t base64urlDecode(const char_t *input, size_t inputLen, void *output, size
210210
size_t n;
211211
uint8_t *p;
212212

213-
// This function does not handle equals signs at the end of base64 encoded output!
214-
while (input[inputLen - 1] == '=')
215-
{
216-
inputLen--;
217-
}
218-
219213
// Check parameters
220214
if (input == NULL && inputLen != 0)
221215
return ERROR_INVALID_PARAMETER;
222216
if (outputLen == NULL)
223217
return ERROR_INVALID_PARAMETER;
224218

219+
// Empty input is valid; produce empty output
220+
if (inputLen == 0) {
221+
*outputLen = 0;
222+
return NO_ERROR;
223+
}
224+
225+
// Safely strip optional '=' padding
226+
while (inputLen > 0 && input[inputLen - 1] == '=') {
227+
inputLen--;
228+
}
229+
225230
// Check the length of the input string
226231
if ((inputLen % 4) == 1)
227232
return ERROR_INVALID_LENGTH;

0 commit comments

Comments
 (0)