Rough and ready port of axTLS

Committer:
ashleymills
Date:
Mon May 13 18:15:18 2013 +0000
Revision:
0:5a29fd060ac8
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:5a29fd060ac8 1 /*
ashleymills 0:5a29fd060ac8 2 * Copyright (c) 2007, Cameron Rich
ashleymills 0:5a29fd060ac8 3 *
ashleymills 0:5a29fd060ac8 4 * All rights reserved.
ashleymills 0:5a29fd060ac8 5 *
ashleymills 0:5a29fd060ac8 6 * Redistribution and use in source and binary forms, with or without
ashleymills 0:5a29fd060ac8 7 * modification, are permitted provided that the following conditions are met:
ashleymills 0:5a29fd060ac8 8 *
ashleymills 0:5a29fd060ac8 9 * * Redistributions of source code must retain the above copyright notice,
ashleymills 0:5a29fd060ac8 10 * this list of conditions and the following disclaimer.
ashleymills 0:5a29fd060ac8 11 * * Redistributions in binary form must reproduce the above copyright notice,
ashleymills 0:5a29fd060ac8 12 * this list of conditions and the following disclaimer in the documentation
ashleymills 0:5a29fd060ac8 13 * and/or other materials provided with the distribution.
ashleymills 0:5a29fd060ac8 14 * * Neither the name of the axTLS project nor the names of its contributors
ashleymills 0:5a29fd060ac8 15 * may be used to endorse or promote products derived from this software
ashleymills 0:5a29fd060ac8 16 * without specific prior written permission.
ashleymills 0:5a29fd060ac8 17 *
ashleymills 0:5a29fd060ac8 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
ashleymills 0:5a29fd060ac8 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
ashleymills 0:5a29fd060ac8 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
ashleymills 0:5a29fd060ac8 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ashleymills 0:5a29fd060ac8 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ashleymills 0:5a29fd060ac8 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ashleymills 0:5a29fd060ac8 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ashleymills 0:5a29fd060ac8 25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ashleymills 0:5a29fd060ac8 26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ashleymills 0:5a29fd060ac8 27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ashleymills 0:5a29fd060ac8 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ashleymills 0:5a29fd060ac8 29 */
ashleymills 0:5a29fd060ac8 30
ashleymills 0:5a29fd060ac8 31 /**
ashleymills 0:5a29fd060ac8 32 * Some misc. routines to help things out
ashleymills 0:5a29fd060ac8 33 */
ashleymills 0:5a29fd060ac8 34
ashleymills 0:5a29fd060ac8 35 #include <stdlib.h>
ashleymills 0:5a29fd060ac8 36 #include <string.h>
ashleymills 0:5a29fd060ac8 37 #include <stdarg.h>
ashleymills 0:5a29fd060ac8 38 #include <stdio.h>
ashleymills 0:5a29fd060ac8 39 #include "os_port.h"
ashleymills 0:5a29fd060ac8 40 #include <lwip/def.h>
ashleymills 0:5a29fd060ac8 41 #include "sockets.h"
ashleymills 0:5a29fd060ac8 42 #include "crypto_misc.h"
ashleymills 0:5a29fd060ac8 43 #include "config.h"
ashleymills 0:5a29fd060ac8 44
ashleymills 0:5a29fd060ac8 45 #include <time.h>
ashleymills 0:5a29fd060ac8 46 #ifdef CONFIG_WIN32_USE_CRYPTO_LIB
ashleymills 0:5a29fd060ac8 47 #include "wincrypt.h"
ashleymills 0:5a29fd060ac8 48 #endif
ashleymills 0:5a29fd060ac8 49
ashleymills 0:5a29fd060ac8 50 #ifndef WIN32
ashleymills 0:5a29fd060ac8 51 static int rng_fd = -1;
ashleymills 0:5a29fd060ac8 52 #elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
ashleymills 0:5a29fd060ac8 53 static HCRYPTPROV gCryptProv;
ashleymills 0:5a29fd060ac8 54 #endif
ashleymills 0:5a29fd060ac8 55
ashleymills 0:5a29fd060ac8 56 #if (!defined(CONFIG_USE_DEV_URANDOM) && !defined(CONFIG_WIN32_USE_CRYPTO_LIB))
ashleymills 0:5a29fd060ac8 57 /* change to processor registers as appropriate */
ashleymills 0:5a29fd060ac8 58 #define ENTROPY_POOL_SIZE 32
ashleymills 0:5a29fd060ac8 59 #define ENTROPY_COUNTER1 ((((uint64_t)tv.tv_sec)<<32) | tv.tv_usec)
ashleymills 0:5a29fd060ac8 60 #define ENTROPY_COUNTER2 rand()
ashleymills 0:5a29fd060ac8 61 static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
ashleymills 0:5a29fd060ac8 62 #endif
ashleymills 0:5a29fd060ac8 63
ashleymills 0:5a29fd060ac8 64 const char * const unsupported_str = "Error: Feature not supported\n";
ashleymills 0:5a29fd060ac8 65
ashleymills 0:5a29fd060ac8 66 #ifndef CONFIG_SSL_SKELETON_MODE
ashleymills 0:5a29fd060ac8 67 /**
ashleymills 0:5a29fd060ac8 68 * Retrieve a file and put it into memory
ashleymills 0:5a29fd060ac8 69 * @return The size of the file, or -1 on failure.
ashleymills 0:5a29fd060ac8 70 */
ashleymills 0:5a29fd060ac8 71 int get_file(const char *filename, uint8_t **buf)
ashleymills 0:5a29fd060ac8 72 {
ashleymills 0:5a29fd060ac8 73 int total_bytes = 0;
ashleymills 0:5a29fd060ac8 74 int bytes_read = 0;
ashleymills 0:5a29fd060ac8 75 int filesize;
ashleymills 0:5a29fd060ac8 76 FILE *stream = fopen(filename, "rb");
ashleymills 0:5a29fd060ac8 77
ashleymills 0:5a29fd060ac8 78 if (stream == NULL)
ashleymills 0:5a29fd060ac8 79 {
ashleymills 0:5a29fd060ac8 80 #ifdef CONFIG_SSL_FULL_MODE
ashleymills 0:5a29fd060ac8 81 printf("file '%s' does not exist\n", filename); TTY_FLUSH();
ashleymills 0:5a29fd060ac8 82 #endif
ashleymills 0:5a29fd060ac8 83 return -1;
ashleymills 0:5a29fd060ac8 84 }
ashleymills 0:5a29fd060ac8 85
ashleymills 0:5a29fd060ac8 86 /* Win CE doesn't support stat() */
ashleymills 0:5a29fd060ac8 87 fseek(stream, 0, SEEK_END);
ashleymills 0:5a29fd060ac8 88 filesize = ftell(stream);
ashleymills 0:5a29fd060ac8 89 *buf = (uint8_t *)malloc(filesize);
ashleymills 0:5a29fd060ac8 90 fseek(stream, 0, SEEK_SET);
ashleymills 0:5a29fd060ac8 91
ashleymills 0:5a29fd060ac8 92 do
ashleymills 0:5a29fd060ac8 93 {
ashleymills 0:5a29fd060ac8 94 bytes_read = fread(*buf+total_bytes, 1, filesize-total_bytes, stream);
ashleymills 0:5a29fd060ac8 95 total_bytes += bytes_read;
ashleymills 0:5a29fd060ac8 96 } while (total_bytes < filesize && bytes_read > 0);
ashleymills 0:5a29fd060ac8 97
ashleymills 0:5a29fd060ac8 98 fclose(stream);
ashleymills 0:5a29fd060ac8 99 return filesize;
ashleymills 0:5a29fd060ac8 100 }
ashleymills 0:5a29fd060ac8 101 #endif
ashleymills 0:5a29fd060ac8 102
ashleymills 0:5a29fd060ac8 103 /**
ashleymills 0:5a29fd060ac8 104 * Initialise the Random Number Generator engine.
ashleymills 0:5a29fd060ac8 105 * - On Win32 use the platform SDK's crypto engine.
ashleymills 0:5a29fd060ac8 106 * - On Linux use /dev/urandom
ashleymills 0:5a29fd060ac8 107 * - If none of these work then use a custom RNG.
ashleymills 0:5a29fd060ac8 108 */
ashleymills 0:5a29fd060ac8 109 EXP_FUNC void STDCALL RNG_initialize()
ashleymills 0:5a29fd060ac8 110 {
ashleymills 0:5a29fd060ac8 111 #if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
ashleymills 0:5a29fd060ac8 112 rng_fd = ax_open("/dev/urandom", O_RDONLY);
ashleymills 0:5a29fd060ac8 113 #elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
ashleymills 0:5a29fd060ac8 114 if (!CryptAcquireContext(&gCryptProv,
ashleymills 0:5a29fd060ac8 115 NULL, NULL, PROV_RSA_FULL, 0))
ashleymills 0:5a29fd060ac8 116 {
ashleymills 0:5a29fd060ac8 117 if (GetLastError() == NTE_BAD_KEYSET &&
ashleymills 0:5a29fd060ac8 118 !CryptAcquireContext(&gCryptProv,
ashleymills 0:5a29fd060ac8 119 NULL,
ashleymills 0:5a29fd060ac8 120 NULL,
ashleymills 0:5a29fd060ac8 121 PROV_RSA_FULL,
ashleymills 0:5a29fd060ac8 122 CRYPT_NEWKEYSET))
ashleymills 0:5a29fd060ac8 123 {
ashleymills 0:5a29fd060ac8 124 printf("CryptoLib: %x\n", unsupported_str, GetLastError());
ashleymills 0:5a29fd060ac8 125 exit(1);
ashleymills 0:5a29fd060ac8 126 }
ashleymills 0:5a29fd060ac8 127 }
ashleymills 0:5a29fd060ac8 128 #else
ashleymills 0:5a29fd060ac8 129 /* start of with a stack to copy across */
ashleymills 0:5a29fd060ac8 130 int i;
ashleymills 0:5a29fd060ac8 131 memcpy(entropy_pool, &i, ENTROPY_POOL_SIZE);
ashleymills 0:5a29fd060ac8 132 srand((unsigned int)&i);
ashleymills 0:5a29fd060ac8 133 #endif
ashleymills 0:5a29fd060ac8 134 }
ashleymills 0:5a29fd060ac8 135
ashleymills 0:5a29fd060ac8 136 /**
ashleymills 0:5a29fd060ac8 137 * If no /dev/urandom, then initialise the RNG with something interesting.
ashleymills 0:5a29fd060ac8 138 */
ashleymills 0:5a29fd060ac8 139 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
ashleymills 0:5a29fd060ac8 140 {
ashleymills 0:5a29fd060ac8 141 #if defined(WIN32) || defined(CONFIG_WIN32_USE_CRYPTO_LIB)
ashleymills 0:5a29fd060ac8 142 int i;
ashleymills 0:5a29fd060ac8 143
ashleymills 0:5a29fd060ac8 144 for (i = 0; i < ENTROPY_POOL_SIZE && i < size; i++)
ashleymills 0:5a29fd060ac8 145 entropy_pool[i] ^= seed_buf[i];
ashleymills 0:5a29fd060ac8 146 #endif
ashleymills 0:5a29fd060ac8 147 }
ashleymills 0:5a29fd060ac8 148
ashleymills 0:5a29fd060ac8 149 /**
ashleymills 0:5a29fd060ac8 150 * Terminate the RNG engine.
ashleymills 0:5a29fd060ac8 151 */
ashleymills 0:5a29fd060ac8 152 EXP_FUNC void STDCALL RNG_terminate(void)
ashleymills 0:5a29fd060ac8 153 {
ashleymills 0:5a29fd060ac8 154 #ifndef WIN32
ashleymills 0:5a29fd060ac8 155 //close(rng_fd);
ashleymills 0:5a29fd060ac8 156 #elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
ashleymills 0:5a29fd060ac8 157 CryptReleaseContext(gCryptProv, 0);
ashleymills 0:5a29fd060ac8 158 #endif
ashleymills 0:5a29fd060ac8 159 }
ashleymills 0:5a29fd060ac8 160
ashleymills 0:5a29fd060ac8 161 /**
ashleymills 0:5a29fd060ac8 162 * Set a series of bytes with a random number. Individual bytes can be 0
ashleymills 0:5a29fd060ac8 163 */
ashleymills 0:5a29fd060ac8 164 EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
ashleymills 0:5a29fd060ac8 165 {
ashleymills 0:5a29fd060ac8 166 #if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
ashleymills 0:5a29fd060ac8 167 /* use the Linux default */
ashleymills 0:5a29fd060ac8 168 read(rng_fd, rand_data, num_rand_bytes); /* read from /dev/urandom */
ashleymills 0:5a29fd060ac8 169 #elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
ashleymills 0:5a29fd060ac8 170 /* use Microsoft Crypto Libraries */
ashleymills 0:5a29fd060ac8 171 CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
ashleymills 0:5a29fd060ac8 172 #else /* nothing else to use, so use a custom RNG */
ashleymills 0:5a29fd060ac8 173 /* The method we use when we've got nothing better. Use RC4, time
ashleymills 0:5a29fd060ac8 174 and a couple of random seeds to generate a random sequence */
ashleymills 0:5a29fd060ac8 175 RC4_CTX rng_ctx;
ashleymills 0:5a29fd060ac8 176 struct timeval tv;
ashleymills 0:5a29fd060ac8 177 MD5_CTX rng_digest_ctx;
ashleymills 0:5a29fd060ac8 178 uint8_t digest[MD5_SIZE];
ashleymills 0:5a29fd060ac8 179 uint64_t *ep;
ashleymills 0:5a29fd060ac8 180 int i;
ashleymills 0:5a29fd060ac8 181
ashleymills 0:5a29fd060ac8 182 /* A proper implementation would use counters etc for entropy */
ashleymills 0:5a29fd060ac8 183 // XXX XXX XX X need to seed this properly
ashleymills 0:5a29fd060ac8 184 gettimeofday(&tv, NULL);
ashleymills 0:5a29fd060ac8 185 ep = (uint64_t *)entropy_pool;
ashleymills 0:5a29fd060ac8 186
ashleymills 0:5a29fd060ac8 187 ep[0] ^= ENTROPY_COUNTER1;
ashleymills 0:5a29fd060ac8 188 ep[1] ^= ENTROPY_COUNTER2;
ashleymills 0:5a29fd060ac8 189
ashleymills 0:5a29fd060ac8 190
ashleymills 0:5a29fd060ac8 191 /* use a digested version of the entropy pool as a key */
ashleymills 0:5a29fd060ac8 192 MD5_Init(&rng_digest_ctx);
ashleymills 0:5a29fd060ac8 193 MD5_Update(&rng_digest_ctx, entropy_pool, ENTROPY_POOL_SIZE);
ashleymills 0:5a29fd060ac8 194 MD5_Final(digest, &rng_digest_ctx);
ashleymills 0:5a29fd060ac8 195
ashleymills 0:5a29fd060ac8 196 /* come up with the random sequence */
ashleymills 0:5a29fd060ac8 197 RC4_setup(&rng_ctx, digest, MD5_SIZE); /* use as a key */
ashleymills 0:5a29fd060ac8 198 memcpy(rand_data, entropy_pool, num_rand_bytes < ENTROPY_POOL_SIZE ?
ashleymills 0:5a29fd060ac8 199 num_rand_bytes : ENTROPY_POOL_SIZE);
ashleymills 0:5a29fd060ac8 200 RC4_crypt(&rng_ctx, rand_data, rand_data, num_rand_bytes);
ashleymills 0:5a29fd060ac8 201
ashleymills 0:5a29fd060ac8 202 /* move things along */
ashleymills 0:5a29fd060ac8 203 for (i = ENTROPY_POOL_SIZE-1; i >= MD5_SIZE ; i--)
ashleymills 0:5a29fd060ac8 204 entropy_pool[i] = entropy_pool[i-MD5_SIZE];
ashleymills 0:5a29fd060ac8 205
ashleymills 0:5a29fd060ac8 206 /* insert the digest at the start of the entropy pool */
ashleymills 0:5a29fd060ac8 207 memcpy(entropy_pool, digest, MD5_SIZE);
ashleymills 0:5a29fd060ac8 208 #endif
ashleymills 0:5a29fd060ac8 209 }
ashleymills 0:5a29fd060ac8 210
ashleymills 0:5a29fd060ac8 211 /**
ashleymills 0:5a29fd060ac8 212 * Set a series of bytes with a random number. Individual bytes are not zero.
ashleymills 0:5a29fd060ac8 213 */
ashleymills 0:5a29fd060ac8 214 void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
ashleymills 0:5a29fd060ac8 215 {
ashleymills 0:5a29fd060ac8 216 int i;
ashleymills 0:5a29fd060ac8 217 get_random(num_rand_bytes, rand_data);
ashleymills 0:5a29fd060ac8 218
ashleymills 0:5a29fd060ac8 219 for (i = 0; i < num_rand_bytes; i++)
ashleymills 0:5a29fd060ac8 220 {
ashleymills 0:5a29fd060ac8 221 while (rand_data[i] == 0) /* can't be 0 */
ashleymills 0:5a29fd060ac8 222 rand_data[i] = (uint8_t)(rand());
ashleymills 0:5a29fd060ac8 223 }
ashleymills 0:5a29fd060ac8 224 }
ashleymills 0:5a29fd060ac8 225
ashleymills 0:5a29fd060ac8 226 /**
ashleymills 0:5a29fd060ac8 227 * Some useful diagnostic routines
ashleymills 0:5a29fd060ac8 228 */
ashleymills 0:5a29fd060ac8 229 #if defined(CONFIG_SSL_FULL_MODE) || defined(CONFIG_DEBUG)
ashleymills 0:5a29fd060ac8 230 int hex_finish;
ashleymills 0:5a29fd060ac8 231 int hex_index;
ashleymills 0:5a29fd060ac8 232
ashleymills 0:5a29fd060ac8 233 static void print_hex_init(int finish)
ashleymills 0:5a29fd060ac8 234 {
ashleymills 0:5a29fd060ac8 235 hex_finish = finish;
ashleymills 0:5a29fd060ac8 236 hex_index = 0;
ashleymills 0:5a29fd060ac8 237 }
ashleymills 0:5a29fd060ac8 238
ashleymills 0:5a29fd060ac8 239 static void print_hex(uint8_t hex)
ashleymills 0:5a29fd060ac8 240 {
ashleymills 0:5a29fd060ac8 241 static int column;
ashleymills 0:5a29fd060ac8 242
ashleymills 0:5a29fd060ac8 243 if (hex_index == 0)
ashleymills 0:5a29fd060ac8 244 {
ashleymills 0:5a29fd060ac8 245 column = 0;
ashleymills 0:5a29fd060ac8 246 }
ashleymills 0:5a29fd060ac8 247
ashleymills 0:5a29fd060ac8 248 printf("%02x ", hex);
ashleymills 0:5a29fd060ac8 249 if (++column == 8)
ashleymills 0:5a29fd060ac8 250 {
ashleymills 0:5a29fd060ac8 251 printf(": ");
ashleymills 0:5a29fd060ac8 252 }
ashleymills 0:5a29fd060ac8 253 else if (column >= 16)
ashleymills 0:5a29fd060ac8 254 {
ashleymills 0:5a29fd060ac8 255 printf("\r\n");
ashleymills 0:5a29fd060ac8 256 column = 0;
ashleymills 0:5a29fd060ac8 257 }
ashleymills 0:5a29fd060ac8 258
ashleymills 0:5a29fd060ac8 259 if (++hex_index >= hex_finish && column > 0)
ashleymills 0:5a29fd060ac8 260 {
ashleymills 0:5a29fd060ac8 261 printf("\r\n");
ashleymills 0:5a29fd060ac8 262 }
ashleymills 0:5a29fd060ac8 263 }
ashleymills 0:5a29fd060ac8 264
ashleymills 0:5a29fd060ac8 265 /**
ashleymills 0:5a29fd060ac8 266 * Spit out a blob of data for diagnostics. The data is is a nice column format
ashleymills 0:5a29fd060ac8 267 * for easy reading.
ashleymills 0:5a29fd060ac8 268 *
ashleymills 0:5a29fd060ac8 269 * @param format [in] The string (with possible embedded format characters)
ashleymills 0:5a29fd060ac8 270 * @param size [in] The number of numbers to print
ashleymills 0:5a29fd060ac8 271 * @param data [in] The start of data to use
ashleymills 0:5a29fd060ac8 272 * @param ... [in] Any additional arguments
ashleymills 0:5a29fd060ac8 273 */
ashleymills 0:5a29fd060ac8 274 EXP_FUNC void STDCALL print_blob(const char *format,
ashleymills 0:5a29fd060ac8 275 const uint8_t *data, int size, ...)
ashleymills 0:5a29fd060ac8 276 {
ashleymills 0:5a29fd060ac8 277 int i;
ashleymills 0:5a29fd060ac8 278 char tmp[80];
ashleymills 0:5a29fd060ac8 279 va_list(ap);
ashleymills 0:5a29fd060ac8 280
ashleymills 0:5a29fd060ac8 281 va_start(ap, size);
ashleymills 0:5a29fd060ac8 282 sprintf(tmp, "%s\n", format);
ashleymills 0:5a29fd060ac8 283 vprintf(tmp, ap);
ashleymills 0:5a29fd060ac8 284 print_hex_init(size);
ashleymills 0:5a29fd060ac8 285 for (i = 0; i < size; i++)
ashleymills 0:5a29fd060ac8 286 {
ashleymills 0:5a29fd060ac8 287 print_hex(data[i]);
ashleymills 0:5a29fd060ac8 288 }
ashleymills 0:5a29fd060ac8 289
ashleymills 0:5a29fd060ac8 290 va_end(ap);
ashleymills 0:5a29fd060ac8 291 TTY_FLUSH();
ashleymills 0:5a29fd060ac8 292 }
ashleymills 0:5a29fd060ac8 293 #elif defined(WIN32)
ashleymills 0:5a29fd060ac8 294 /* VC6.0 doesn't handle variadic macros */
ashleymills 0:5a29fd060ac8 295 EXP_FUNC void STDCALL print_blob(const char *format, const unsigned char *data,
ashleymills 0:5a29fd060ac8 296 int size, ...) {}
ashleymills 0:5a29fd060ac8 297 #endif
ashleymills 0:5a29fd060ac8 298
ashleymills 0:5a29fd060ac8 299 #if defined(CONFIG_SSL_HAS_PEM) || defined(CONFIG_HTTP_HAS_AUTHORIZATION)
ashleymills 0:5a29fd060ac8 300 /* base64 to binary lookup table */
ashleymills 0:5a29fd060ac8 301 static const uint8_t map[128] =
ashleymills 0:5a29fd060ac8 302 {
ashleymills 0:5a29fd060ac8 303 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
ashleymills 0:5a29fd060ac8 304 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
ashleymills 0:5a29fd060ac8 305 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
ashleymills 0:5a29fd060ac8 306 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
ashleymills 0:5a29fd060ac8 307 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
ashleymills 0:5a29fd060ac8 308 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
ashleymills 0:5a29fd060ac8 309 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
ashleymills 0:5a29fd060ac8 310 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
ashleymills 0:5a29fd060ac8 311 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
ashleymills 0:5a29fd060ac8 312 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
ashleymills 0:5a29fd060ac8 313 49, 50, 51, 255, 255, 255, 255, 255
ashleymills 0:5a29fd060ac8 314 };
ashleymills 0:5a29fd060ac8 315
ashleymills 0:5a29fd060ac8 316 EXP_FUNC int STDCALL base64_decode(const char *in, int len,
ashleymills 0:5a29fd060ac8 317 uint8_t *out, int *outlen)
ashleymills 0:5a29fd060ac8 318 {
ashleymills 0:5a29fd060ac8 319 int g, t, x, y, z;
ashleymills 0:5a29fd060ac8 320 uint8_t c;
ashleymills 0:5a29fd060ac8 321 int ret = -1;
ashleymills 0:5a29fd060ac8 322
ashleymills 0:5a29fd060ac8 323 g = 3;
ashleymills 0:5a29fd060ac8 324 for (x = y = z = t = 0; x < len; x++)
ashleymills 0:5a29fd060ac8 325 {
ashleymills 0:5a29fd060ac8 326 if ((c = map[in[x]&0x7F]) == 0xff)
ashleymills 0:5a29fd060ac8 327 continue;
ashleymills 0:5a29fd060ac8 328
ashleymills 0:5a29fd060ac8 329 if (c == 254) /* this is the end... */
ashleymills 0:5a29fd060ac8 330 {
ashleymills 0:5a29fd060ac8 331 c = 0;
ashleymills 0:5a29fd060ac8 332
ashleymills 0:5a29fd060ac8 333 if (--g < 0)
ashleymills 0:5a29fd060ac8 334 goto error;
ashleymills 0:5a29fd060ac8 335 }
ashleymills 0:5a29fd060ac8 336 else if (g != 3) /* only allow = at end */
ashleymills 0:5a29fd060ac8 337 goto error;
ashleymills 0:5a29fd060ac8 338
ashleymills 0:5a29fd060ac8 339 t = (t<<6) | c;
ashleymills 0:5a29fd060ac8 340
ashleymills 0:5a29fd060ac8 341 if (++y == 4)
ashleymills 0:5a29fd060ac8 342 {
ashleymills 0:5a29fd060ac8 343 out[z++] = (uint8_t)((t>>16)&255);
ashleymills 0:5a29fd060ac8 344
ashleymills 0:5a29fd060ac8 345 if (g > 1)
ashleymills 0:5a29fd060ac8 346 out[z++] = (uint8_t)((t>>8)&255);
ashleymills 0:5a29fd060ac8 347
ashleymills 0:5a29fd060ac8 348 if (g > 2)
ashleymills 0:5a29fd060ac8 349 out[z++] = (uint8_t)(t&255);
ashleymills 0:5a29fd060ac8 350
ashleymills 0:5a29fd060ac8 351 y = t = 0;
ashleymills 0:5a29fd060ac8 352 }
ashleymills 0:5a29fd060ac8 353
ashleymills 0:5a29fd060ac8 354 /* check that we don't go past the output buffer */
ashleymills 0:5a29fd060ac8 355 if (z > *outlen)
ashleymills 0:5a29fd060ac8 356 goto error;
ashleymills 0:5a29fd060ac8 357 }
ashleymills 0:5a29fd060ac8 358
ashleymills 0:5a29fd060ac8 359 if (y != 0)
ashleymills 0:5a29fd060ac8 360 goto error;
ashleymills 0:5a29fd060ac8 361
ashleymills 0:5a29fd060ac8 362 *outlen = z;
ashleymills 0:5a29fd060ac8 363 ret = 0;
ashleymills 0:5a29fd060ac8 364
ashleymills 0:5a29fd060ac8 365 error:
ashleymills 0:5a29fd060ac8 366 #ifdef CONFIG_SSL_FULL_MODE
ashleymills 0:5a29fd060ac8 367 if (ret < 0)
ashleymills 0:5a29fd060ac8 368 printf("Error: Invalid base64\n"); TTY_FLUSH();
ashleymills 0:5a29fd060ac8 369 #endif
ashleymills 0:5a29fd060ac8 370 TTY_FLUSH();
ashleymills 0:5a29fd060ac8 371 return ret;
ashleymills 0:5a29fd060ac8 372
ashleymills 0:5a29fd060ac8 373 }
ashleymills 0:5a29fd060ac8 374 #endif
ashleymills 0:5a29fd060ac8 375