comparison libtomcrypt/src/misc/base64/base64_decode.c @ 1712:d885a77b98e0

avoid zero length array in base64_decode
author Matt Johnston <matt@ucc.asn.au>
date Wed, 10 Jun 2020 23:26:05 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1711:e9dba7abd939 1712:d885a77b98e0
41 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 41 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
42 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 42 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43 255, 255, 255, 255 }; 43 255, 255, 255, 255 };
44 #endif /* LTC_BASE64 */ 44 #endif /* LTC_BASE64 */
45 45
46 #if defined(LTC_BASE64_URL)
46 static const unsigned char map_base64url[] = { 47 static const unsigned char map_base64url[] = {
47 #if defined(LTC_BASE64_URL)
48 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 48 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
49 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 49 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
50 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 50 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 51 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255,
52 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 52 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
65 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 65 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 66 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
67 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 67 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
68 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 68 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
69 255, 255, 255, 255 69 255, 255, 255, 255
70 };
70 #endif /* LTC_BASE64_URL */ 71 #endif /* LTC_BASE64_URL */
71 };
72 72
73 enum { 73 enum {
74 relaxed = 0, 74 relaxed = 0,
75 strict = 1 75 strict = 1
76 }; 76 };
115 y = t = 0; 115 y = t = 0;
116 } 116 }
117 } 117 }
118 118
119 if (y != 0) { 119 if (y != 0) {
120 int allow_b64url = 0;
121 #ifdef LTC_BASE64_URL
122 if (map == map_base64url) {
123 allow_b64url = 1;
124 }
125 #endif
120 if (y == 1) return CRYPT_INVALID_PACKET; 126 if (y == 1) return CRYPT_INVALID_PACKET;
121 if ((y + g) != 4 && is_strict && map != map_base64url) return CRYPT_INVALID_PACKET; 127 if ((y + g) != 4 && is_strict && !allow_b64url) return CRYPT_INVALID_PACKET;
122 t = t << (6 * (4 - y)); 128 t = t << (6 * (4 - y));
123 if (z + y - 1 > *outlen) return CRYPT_BUFFER_OVERFLOW; 129 if (z + y - 1 > *outlen) return CRYPT_BUFFER_OVERFLOW;
124 if (y >= 2) out[z++] = (unsigned char) ((t >> 16) & 255); 130 if (y >= 2) out[z++] = (unsigned char) ((t >> 16) & 255);
125 if (y == 3) out[z++] = (unsigned char) ((t >> 8) & 255); 131 if (y == 3) out[z++] = (unsigned char) ((t >> 8) & 255);
126 } 132 }