18dbcf02cSchristos /* 28dbcf02cSchristos * Base64 encoding/decoding (RFC1341) 33d6c0713Schristos * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi> 48dbcf02cSchristos * 5e604d861Schristos * This software may be distributed under the terms of the BSD license. 6e604d861Schristos * See README for more details. 78dbcf02cSchristos */ 88dbcf02cSchristos 98dbcf02cSchristos #include "includes.h" 103d6c0713Schristos #include <stdint.h> 118dbcf02cSchristos 12*bb618362Schristos #include "utils/common.h" 138dbcf02cSchristos #include "os.h" 148dbcf02cSchristos #include "base64.h" 158dbcf02cSchristos 16*bb618362Schristos static const char base64_table[65] = 178dbcf02cSchristos "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 18*bb618362Schristos static const char base64_url_table[65] = 190a73ee0aSchristos "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 208dbcf02cSchristos 210a73ee0aSchristos 22*bb618362Schristos #define BASE64_PAD BIT(0) 23*bb618362Schristos #define BASE64_LF BIT(1) 24*bb618362Schristos 25*bb618362Schristos 26*bb618362Schristos static char * base64_gen_encode(const unsigned char *src, size_t len, 27*bb618362Schristos size_t *out_len, const char *table, int add_pad) 288dbcf02cSchristos { 29*bb618362Schristos char *out, *pos; 308dbcf02cSchristos const unsigned char *end, *in; 318dbcf02cSchristos size_t olen; 328dbcf02cSchristos int line_len; 338dbcf02cSchristos 343d6c0713Schristos if (len >= SIZE_MAX / 4) 353d6c0713Schristos return NULL; 368dbcf02cSchristos olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ 37*bb618362Schristos if (add_pad & BASE64_LF) 388dbcf02cSchristos olen += olen / 72; /* line feeds */ 398dbcf02cSchristos olen++; /* nul termination */ 408dbcf02cSchristos if (olen < len) 418dbcf02cSchristos return NULL; /* integer overflow */ 428dbcf02cSchristos out = os_malloc(olen); 438dbcf02cSchristos if (out == NULL) 448dbcf02cSchristos return NULL; 458dbcf02cSchristos 468dbcf02cSchristos end = src + len; 478dbcf02cSchristos in = src; 488dbcf02cSchristos pos = out; 498dbcf02cSchristos line_len = 0; 508dbcf02cSchristos while (end - in >= 3) { 510a73ee0aSchristos *pos++ = table[(in[0] >> 2) & 0x3f]; 520a73ee0aSchristos *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f]; 530a73ee0aSchristos *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f]; 540a73ee0aSchristos *pos++ = table[in[2] & 0x3f]; 558dbcf02cSchristos in += 3; 568dbcf02cSchristos line_len += 4; 57*bb618362Schristos if ((add_pad & BASE64_LF) && line_len >= 72) { 588dbcf02cSchristos *pos++ = '\n'; 598dbcf02cSchristos line_len = 0; 608dbcf02cSchristos } 618dbcf02cSchristos } 628dbcf02cSchristos 638dbcf02cSchristos if (end - in) { 640a73ee0aSchristos *pos++ = table[(in[0] >> 2) & 0x3f]; 658dbcf02cSchristos if (end - in == 1) { 660a73ee0aSchristos *pos++ = table[((in[0] & 0x03) << 4) & 0x3f]; 67*bb618362Schristos if (add_pad & BASE64_PAD) 688dbcf02cSchristos *pos++ = '='; 698dbcf02cSchristos } else { 700a73ee0aSchristos *pos++ = table[(((in[0] & 0x03) << 4) | 71bb610346Schristos (in[1] >> 4)) & 0x3f]; 720a73ee0aSchristos *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f]; 738dbcf02cSchristos } 74*bb618362Schristos if (add_pad & BASE64_PAD) 758dbcf02cSchristos *pos++ = '='; 768dbcf02cSchristos line_len += 4; 778dbcf02cSchristos } 788dbcf02cSchristos 79*bb618362Schristos if ((add_pad & BASE64_LF) && line_len) 808dbcf02cSchristos *pos++ = '\n'; 818dbcf02cSchristos 828dbcf02cSchristos *pos = '\0'; 838dbcf02cSchristos if (out_len) 848dbcf02cSchristos *out_len = pos - out; 858dbcf02cSchristos return out; 868dbcf02cSchristos } 878dbcf02cSchristos 888dbcf02cSchristos 89*bb618362Schristos static unsigned char * base64_gen_decode(const char *src, size_t len, 90*bb618362Schristos size_t *out_len, const char *table) 918dbcf02cSchristos { 92111b9fd8Schristos unsigned char dtable[256], *out, *pos, block[4], tmp; 938dbcf02cSchristos size_t i, count, olen; 94111b9fd8Schristos int pad = 0; 950a73ee0aSchristos size_t extra_pad; 968dbcf02cSchristos 978dbcf02cSchristos os_memset(dtable, 0x80, 256); 988dbcf02cSchristos for (i = 0; i < sizeof(base64_table) - 1; i++) 99*bb618362Schristos dtable[(unsigned char) table[i]] = (unsigned char) i; 1008dbcf02cSchristos dtable['='] = 0; 1018dbcf02cSchristos 1028dbcf02cSchristos count = 0; 1038dbcf02cSchristos for (i = 0; i < len; i++) { 104*bb618362Schristos if (dtable[(unsigned char) src[i]] != 0x80) 1058dbcf02cSchristos count++; 1068dbcf02cSchristos } 1078dbcf02cSchristos 1080a73ee0aSchristos if (count == 0) 1098dbcf02cSchristos return NULL; 1100a73ee0aSchristos extra_pad = (4 - count % 4) % 4; 1118dbcf02cSchristos 1120a73ee0aSchristos olen = (count + extra_pad) / 4 * 3; 1138dbcf02cSchristos pos = out = os_malloc(olen); 1148dbcf02cSchristos if (out == NULL) 1158dbcf02cSchristos return NULL; 1168dbcf02cSchristos 1178dbcf02cSchristos count = 0; 1180a73ee0aSchristos for (i = 0; i < len + extra_pad; i++) { 1190a73ee0aSchristos unsigned char val; 1200a73ee0aSchristos 1210a73ee0aSchristos if (i >= len) 1220a73ee0aSchristos val = '='; 1230a73ee0aSchristos else 1240a73ee0aSchristos val = src[i]; 1250a73ee0aSchristos tmp = dtable[val]; 1268dbcf02cSchristos if (tmp == 0x80) 1278dbcf02cSchristos continue; 1288dbcf02cSchristos 1290a73ee0aSchristos if (val == '=') 130111b9fd8Schristos pad++; 1318dbcf02cSchristos block[count] = tmp; 1328dbcf02cSchristos count++; 1338dbcf02cSchristos if (count == 4) { 1348dbcf02cSchristos *pos++ = (block[0] << 2) | (block[1] >> 4); 1358dbcf02cSchristos *pos++ = (block[1] << 4) | (block[2] >> 2); 1368dbcf02cSchristos *pos++ = (block[2] << 6) | block[3]; 1378dbcf02cSchristos count = 0; 138111b9fd8Schristos if (pad) { 139111b9fd8Schristos if (pad == 1) 1408dbcf02cSchristos pos--; 141111b9fd8Schristos else if (pad == 2) 142111b9fd8Schristos pos -= 2; 143111b9fd8Schristos else { 144111b9fd8Schristos /* Invalid padding */ 145111b9fd8Schristos os_free(out); 146111b9fd8Schristos return NULL; 147111b9fd8Schristos } 148111b9fd8Schristos break; 149111b9fd8Schristos } 150111b9fd8Schristos } 1518dbcf02cSchristos } 1528dbcf02cSchristos 1538dbcf02cSchristos *out_len = pos - out; 1548dbcf02cSchristos return out; 1558dbcf02cSchristos } 1560a73ee0aSchristos 1570a73ee0aSchristos 1580a73ee0aSchristos /** 1590a73ee0aSchristos * base64_encode - Base64 encode 1600a73ee0aSchristos * @src: Data to be encoded 1610a73ee0aSchristos * @len: Length of the data to be encoded 1620a73ee0aSchristos * @out_len: Pointer to output length variable, or %NULL if not used 1630a73ee0aSchristos * Returns: Allocated buffer of out_len bytes of encoded data, 1640a73ee0aSchristos * or %NULL on failure 1650a73ee0aSchristos * 1660a73ee0aSchristos * Caller is responsible for freeing the returned buffer. Returned buffer is 1670a73ee0aSchristos * nul terminated to make it easier to use as a C string. The nul terminator is 1680a73ee0aSchristos * not included in out_len. 1690a73ee0aSchristos */ 170*bb618362Schristos char * base64_encode(const void *src, size_t len, size_t *out_len) 1710a73ee0aSchristos { 172*bb618362Schristos return base64_gen_encode(src, len, out_len, base64_table, 173*bb618362Schristos BASE64_PAD | BASE64_LF); 1740a73ee0aSchristos } 1750a73ee0aSchristos 1760a73ee0aSchristos 177*bb618362Schristos char * base64_encode_no_lf(const void *src, size_t len, size_t *out_len) 1780a73ee0aSchristos { 179*bb618362Schristos return base64_gen_encode(src, len, out_len, base64_table, BASE64_PAD); 180*bb618362Schristos } 181*bb618362Schristos 182*bb618362Schristos 183*bb618362Schristos char * base64_url_encode(const void *src, size_t len, size_t *out_len) 184*bb618362Schristos { 185*bb618362Schristos return base64_gen_encode(src, len, out_len, base64_url_table, 0); 1860a73ee0aSchristos } 1870a73ee0aSchristos 1880a73ee0aSchristos 1890a73ee0aSchristos /** 1900a73ee0aSchristos * base64_decode - Base64 decode 1910a73ee0aSchristos * @src: Data to be decoded 1920a73ee0aSchristos * @len: Length of the data to be decoded 1930a73ee0aSchristos * @out_len: Pointer to output length variable 1940a73ee0aSchristos * Returns: Allocated buffer of out_len bytes of decoded data, 1950a73ee0aSchristos * or %NULL on failure 1960a73ee0aSchristos * 1970a73ee0aSchristos * Caller is responsible for freeing the returned buffer. 1980a73ee0aSchristos */ 199*bb618362Schristos unsigned char * base64_decode(const char *src, size_t len, size_t *out_len) 2000a73ee0aSchristos { 2010a73ee0aSchristos return base64_gen_decode(src, len, out_len, base64_table); 2020a73ee0aSchristos } 2030a73ee0aSchristos 2040a73ee0aSchristos 205*bb618362Schristos unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len) 2060a73ee0aSchristos { 2070a73ee0aSchristos return base64_gen_decode(src, len, out_len, base64_url_table); 2080a73ee0aSchristos } 209