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 * RFC 1115/1319 compliant MD2 implementation
feb11 0:85fceccc1a7c 33 * The MD2 algorithm was designed by Ron Rivest in 1989.
feb11 0:85fceccc1a7c 34 *
feb11 0:85fceccc1a7c 35 * http://www.ietf.org/rfc/rfc1115.txt
feb11 0:85fceccc1a7c 36 * http://www.ietf.org/rfc/rfc1319.txt
feb11 0:85fceccc1a7c 37 */
feb11 0:85fceccc1a7c 38
feb11 0:85fceccc1a7c 39 #include <string.h>
feb11 0:85fceccc1a7c 40 #include <stdio.h>
feb11 0:85fceccc1a7c 41 #include "os_port.h"
feb11 0:85fceccc1a7c 42 #include "crypto.h"
feb11 0:85fceccc1a7c 43 #include "config.h"
feb11 0:85fceccc1a7c 44
feb11 0:85fceccc1a7c 45 /**
feb11 0:85fceccc1a7c 46 * This code is only here to enable the verification of Verisign root
feb11 0:85fceccc1a7c 47 * certificates. So only enable it for verification mode.
feb11 0:85fceccc1a7c 48 */
feb11 0:85fceccc1a7c 49 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 50
feb11 0:85fceccc1a7c 51 static const uint8_t PI_SUBST[256] =
feb11 0:85fceccc1a7c 52 {
feb11 0:85fceccc1a7c 53 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
feb11 0:85fceccc1a7c 54 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
feb11 0:85fceccc1a7c 55 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
feb11 0:85fceccc1a7c 56 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
feb11 0:85fceccc1a7c 57 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
feb11 0:85fceccc1a7c 58 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
feb11 0:85fceccc1a7c 59 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
feb11 0:85fceccc1a7c 60 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
feb11 0:85fceccc1a7c 61 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
feb11 0:85fceccc1a7c 62 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
feb11 0:85fceccc1a7c 63 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
feb11 0:85fceccc1a7c 64 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
feb11 0:85fceccc1a7c 65 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
feb11 0:85fceccc1a7c 66 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
feb11 0:85fceccc1a7c 67 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
feb11 0:85fceccc1a7c 68 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
feb11 0:85fceccc1a7c 69 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
feb11 0:85fceccc1a7c 70 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
feb11 0:85fceccc1a7c 71 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
feb11 0:85fceccc1a7c 72 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
feb11 0:85fceccc1a7c 73 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
feb11 0:85fceccc1a7c 74 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
feb11 0:85fceccc1a7c 75 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
feb11 0:85fceccc1a7c 76 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
feb11 0:85fceccc1a7c 77 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
feb11 0:85fceccc1a7c 78 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
feb11 0:85fceccc1a7c 79 };
feb11 0:85fceccc1a7c 80
feb11 0:85fceccc1a7c 81 /*
feb11 0:85fceccc1a7c 82 * MD2 context setup
feb11 0:85fceccc1a7c 83 */
feb11 0:85fceccc1a7c 84 EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx)
feb11 0:85fceccc1a7c 85 {
feb11 0:85fceccc1a7c 86 memset(ctx, 0, sizeof *ctx);
feb11 0:85fceccc1a7c 87 }
feb11 0:85fceccc1a7c 88
feb11 0:85fceccc1a7c 89 static void md2_process(MD2_CTX *ctx)
feb11 0:85fceccc1a7c 90 {
feb11 0:85fceccc1a7c 91 int i, j;
feb11 0:85fceccc1a7c 92 uint8_t t = 0;
feb11 0:85fceccc1a7c 93
feb11 0:85fceccc1a7c 94 for (i = 0; i < 16; i++)
feb11 0:85fceccc1a7c 95 {
feb11 0:85fceccc1a7c 96 ctx->state[i + 16] = ctx->buffer[i];
feb11 0:85fceccc1a7c 97 ctx->state[i + 32] = ctx->buffer[i] ^ ctx->state[i];
feb11 0:85fceccc1a7c 98 }
feb11 0:85fceccc1a7c 99
feb11 0:85fceccc1a7c 100 for (i = 0; i < 18; i++)
feb11 0:85fceccc1a7c 101 {
feb11 0:85fceccc1a7c 102 for (j = 0; j < 48; j++)
feb11 0:85fceccc1a7c 103 t = (ctx->state[j] ^= PI_SUBST[t]);
feb11 0:85fceccc1a7c 104
feb11 0:85fceccc1a7c 105 t = (t + i) & 0xFF;
feb11 0:85fceccc1a7c 106 }
feb11 0:85fceccc1a7c 107
feb11 0:85fceccc1a7c 108 t = ctx->cksum[15];
feb11 0:85fceccc1a7c 109
feb11 0:85fceccc1a7c 110 for (i = 0; i < 16; i++)
feb11 0:85fceccc1a7c 111 t = (ctx->cksum[i] ^= PI_SUBST[ctx->buffer[i] ^ t]);
feb11 0:85fceccc1a7c 112 }
feb11 0:85fceccc1a7c 113
feb11 0:85fceccc1a7c 114 /*
feb11 0:85fceccc1a7c 115 * MD2 process buffer
feb11 0:85fceccc1a7c 116 */
feb11 0:85fceccc1a7c 117 EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen)
feb11 0:85fceccc1a7c 118 {
feb11 0:85fceccc1a7c 119 int fill;
feb11 0:85fceccc1a7c 120
feb11 0:85fceccc1a7c 121 while (ilen > 0)
feb11 0:85fceccc1a7c 122 {
feb11 0:85fceccc1a7c 123 if (ctx->left + ilen > 16)
feb11 0:85fceccc1a7c 124 fill = 16 - ctx->left;
feb11 0:85fceccc1a7c 125 else
feb11 0:85fceccc1a7c 126 fill = ilen;
feb11 0:85fceccc1a7c 127
feb11 0:85fceccc1a7c 128 memcpy(ctx->buffer + ctx->left, input, fill);
feb11 0:85fceccc1a7c 129
feb11 0:85fceccc1a7c 130 ctx->left += fill;
feb11 0:85fceccc1a7c 131 input += fill;
feb11 0:85fceccc1a7c 132 ilen -= fill;
feb11 0:85fceccc1a7c 133
feb11 0:85fceccc1a7c 134 if (ctx->left == 16)
feb11 0:85fceccc1a7c 135 {
feb11 0:85fceccc1a7c 136 ctx->left = 0;
feb11 0:85fceccc1a7c 137 md2_process(ctx);
feb11 0:85fceccc1a7c 138 }
feb11 0:85fceccc1a7c 139 }
feb11 0:85fceccc1a7c 140 }
feb11 0:85fceccc1a7c 141
feb11 0:85fceccc1a7c 142 /*
feb11 0:85fceccc1a7c 143 * MD2 final digest
feb11 0:85fceccc1a7c 144 */
feb11 0:85fceccc1a7c 145 EXP_FUNC void STDCALL MD2_Final(uint8_t *output, MD2_CTX *ctx)
feb11 0:85fceccc1a7c 146 {
feb11 0:85fceccc1a7c 147 int i;
feb11 0:85fceccc1a7c 148 uint8_t x;
feb11 0:85fceccc1a7c 149
feb11 0:85fceccc1a7c 150 x = (uint8_t)(16 - ctx->left);
feb11 0:85fceccc1a7c 151
feb11 0:85fceccc1a7c 152 for (i = ctx->left; i < 16; i++)
feb11 0:85fceccc1a7c 153 ctx->buffer[i] = x;
feb11 0:85fceccc1a7c 154
feb11 0:85fceccc1a7c 155 md2_process(ctx);
feb11 0:85fceccc1a7c 156
feb11 0:85fceccc1a7c 157 memcpy(ctx->buffer, ctx->cksum, 16);
feb11 0:85fceccc1a7c 158 md2_process(ctx);
feb11 0:85fceccc1a7c 159
feb11 0:85fceccc1a7c 160 memcpy(output, ctx->state, 16);
feb11 0:85fceccc1a7c 161 }
feb11 0:85fceccc1a7c 162
feb11 0:85fceccc1a7c 163 #endif
feb11 0:85fceccc1a7c 164
feb11 0:85fceccc1a7c 165