This is a fork of the mbed port of axTLS

Dependents:   TLS_axTLS-Example HTTPSClientExample

Overview

This library is a fork from the mbed port of axTLS. It attempts to :

  • reduce the usage of dynamic memory
  • verify certificates with key size up to 2048 bits
  • provide a simple interface

Encryption

This library uses either RC4 or AES for encryption.

Memory usage

During the establishment of a connection, about 10KB of memory is allocated dynamically (it depends on certificates). Once the connection is established, the memory consumption is relatively low. This means that your program must not use too much static memory or allocate memory before you establish a TLS connection.

Certificates

Certificates are the major source of problem and will often be the reason why your program will crash. Due to memory constraint, there are some limitations on certificates :

  • Each certificate must not be bigger than 2KB
  • TLS client can only handle a chain of up to three certificates (excluding the root certificate). This means that the server must not send more than three certificates.

Also, this library can only load certificates following these specifications :

  • encoded in binary DER format (PKCS1)
  • The public key must use RSA only

Once the connection is established, you should free all loaded certificates by calling CertificateManager::clear(). This will free a few kilobytes (it depends on your certificates). In addition, to enable certificate verification during the connection, this library has a "precomputed mode". This mode uses much less memory than a normal certificate verification.

Normal mode

You need to copy the root certificate in binary-DER format on the mbed. Then in your code, let's say that your root certificate is saved on the mbed as "root.der", assuming that you include CertificateManager.h and that you created a LocalFileSystem, you can load this certificate as this ;

Load root certificate

CertificateManager::add("/local/root.der");
CertificateManager::load();

Do not forget that this mode takes quite a lot of memory ( the memory peak is high while verifying certificates) and will only work if the key size is not bigger than 1024 bits (otherwise it will crash while verifying certificates).

Precomputed mode

In this mode, you need to save the entire chain of certificates (in binary-DER format) including the root certificate on the mbed. In practice, this means that you must first retrieve all certificates that the server sends during a connection and then find the right root certificate. In your code, you must call CertificateManager::add for each certificate and in the right order : from the server certificate to the root certificate. Here is how you shoud load certificates in this mode :

Loadcertificates in precomputed mode

CertificateManager::add("/local/server1.der");
CertificateManager::add("/local/server2.der");
CertificateManager::add("/local/server3.der");
CertificateManager::add("/local/root.der");
CertificateManager::load(true);

Using this mode, you should be able to verify certificates with key size up to 2048 bits.

How do I find these certificates ?

I posted an entry in my notebook detailing how to get certificates from a server. You should be able to get all certificates you need except the root certificate. Here is a way how to get the root certificate on windows :

  1. Open (double-click) the last certificate sent by the server
  2. Go to details panel and click on the entry called Issuer. The first line gives you the name of this certificate and the second line indicates the company who created this certificate
  3. Open firefox
  4. Go to options, advanced panel and click on View Certificates
  5. Go to Authorities panel
  6. Choose the certificate whose name match the issuer of the last certificate sent by the server
  7. Export this certificate to binary-DER format.

Connect to mbed.org !

Import programTLS_axTLS-Example

Establishing a connection to mbed.org using TLS

Committer:
feb11
Date:
Thu Sep 12 15:18:04 2013 +0000
Revision:
0:85fceccc1a7c
intial import

Who changed what in which revision?

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