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 #include <stdlib.h>
feb11 0:85fceccc1a7c 32 #include <string.h>
feb11 0:85fceccc1a7c 33 #include <stdio.h>
feb11 0:85fceccc1a7c 34 #include "os_port.h"
feb11 0:85fceccc1a7c 35 #include "ssl.h"
feb11 0:85fceccc1a7c 36
feb11 0:85fceccc1a7c 37 static const uint8_t g_hello_done[] = { HS_SERVER_HELLO_DONE, 0, 0, 0 };
feb11 0:85fceccc1a7c 38
feb11 0:85fceccc1a7c 39 static int process_client_hello(SSL *ssl);
feb11 0:85fceccc1a7c 40 static int send_server_hello_sequence(SSL *ssl);
feb11 0:85fceccc1a7c 41 static int send_server_hello(SSL *ssl);
feb11 0:85fceccc1a7c 42 static int send_server_hello_done(SSL *ssl);
feb11 0:85fceccc1a7c 43 static int process_client_key_xchg(SSL *ssl);
feb11 0:85fceccc1a7c 44 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 45 static int send_certificate_request(SSL *ssl);
feb11 0:85fceccc1a7c 46 static int process_cert_verify(SSL *ssl);
feb11 0:85fceccc1a7c 47 #endif
feb11 0:85fceccc1a7c 48
feb11 0:85fceccc1a7c 49 /*
feb11 0:85fceccc1a7c 50 * Establish a new SSL connection to an SSL client.
feb11 0:85fceccc1a7c 51 */
feb11 0:85fceccc1a7c 52 EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd)
feb11 0:85fceccc1a7c 53 {
feb11 0:85fceccc1a7c 54 SSL *ssl;
feb11 0:85fceccc1a7c 55
feb11 0:85fceccc1a7c 56 ssl = ssl_new(ssl, client_fd);
feb11 0:85fceccc1a7c 57 ssl->next_state = HS_CLIENT_HELLO;
feb11 0:85fceccc1a7c 58
feb11 0:85fceccc1a7c 59 #ifdef CONFIG_SSL_FULL_MODE
feb11 0:85fceccc1a7c 60 if (ssl_ctx->chain_length == 0)
feb11 0:85fceccc1a7c 61 printf("Warning - no server certificate defined\n"); TTY_FLUSH();
feb11 0:85fceccc1a7c 62 #endif
feb11 0:85fceccc1a7c 63
feb11 0:85fceccc1a7c 64 return ssl;
feb11 0:85fceccc1a7c 65 }
feb11 0:85fceccc1a7c 66
feb11 0:85fceccc1a7c 67 /*
feb11 0:85fceccc1a7c 68 * Process the handshake record.
feb11 0:85fceccc1a7c 69 */
feb11 0:85fceccc1a7c 70 int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
feb11 0:85fceccc1a7c 71 {
feb11 0:85fceccc1a7c 72 int ret = SSL_OK;
feb11 0:85fceccc1a7c 73 ssl->hs_status = SSL_NOT_OK; /* not connected */
feb11 0:85fceccc1a7c 74
feb11 0:85fceccc1a7c 75 /* To get here the state must be valid */
feb11 0:85fceccc1a7c 76 switch (handshake_type)
feb11 0:85fceccc1a7c 77 {
feb11 0:85fceccc1a7c 78 case HS_CLIENT_HELLO:
feb11 0:85fceccc1a7c 79 if ((ret = process_client_hello(ssl)) == SSL_OK)
feb11 0:85fceccc1a7c 80 ret = send_server_hello_sequence(ssl);
feb11 0:85fceccc1a7c 81 break;
feb11 0:85fceccc1a7c 82
feb11 0:85fceccc1a7c 83 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 84 case HS_CERTIFICATE:/* the client sends its cert */
feb11 0:85fceccc1a7c 85 ret = process_certificate(ssl, &ssl->x509_ctx);
feb11 0:85fceccc1a7c 86
feb11 0:85fceccc1a7c 87 if (ret == SSL_OK) /* verify the cert */
feb11 0:85fceccc1a7c 88 {
feb11 0:85fceccc1a7c 89 int cert_res = 0;
feb11 0:85fceccc1a7c 90
feb11 0:85fceccc1a7c 91 /* cert_res = x509_verify(
feb11 0:85fceccc1a7c 92 ssl->x509_ctx);*/
feb11 0:85fceccc1a7c 93 ret = (cert_res == 0) ? SSL_OK : SSL_X509_ERROR(cert_res);
feb11 0:85fceccc1a7c 94 }
feb11 0:85fceccc1a7c 95 break;
feb11 0:85fceccc1a7c 96
feb11 0:85fceccc1a7c 97 case HS_CERT_VERIFY:
feb11 0:85fceccc1a7c 98 ret = process_cert_verify(ssl);
feb11 0:85fceccc1a7c 99 add_packet(ssl, buf, hs_len); /* needs to be done after */
feb11 0:85fceccc1a7c 100 break;
feb11 0:85fceccc1a7c 101 #endif
feb11 0:85fceccc1a7c 102 case HS_CLIENT_KEY_XCHG:
feb11 0:85fceccc1a7c 103 ret = process_client_key_xchg(ssl);
feb11 0:85fceccc1a7c 104 break;
feb11 0:85fceccc1a7c 105
feb11 0:85fceccc1a7c 106 case HS_FINISHED:
feb11 0:85fceccc1a7c 107 ret = process_finished(ssl, buf, hs_len);
feb11 0:85fceccc1a7c 108 disposable_free(ssl); /* free up some memory */
feb11 0:85fceccc1a7c 109 break;
feb11 0:85fceccc1a7c 110 }
feb11 0:85fceccc1a7c 111
feb11 0:85fceccc1a7c 112 return ret;
feb11 0:85fceccc1a7c 113 }
feb11 0:85fceccc1a7c 114
feb11 0:85fceccc1a7c 115 /*
feb11 0:85fceccc1a7c 116 * Process a client hello message.
feb11 0:85fceccc1a7c 117 */
feb11 0:85fceccc1a7c 118 static int process_client_hello(SSL *ssl)
feb11 0:85fceccc1a7c 119 {
feb11 0:85fceccc1a7c 120 uint8_t *buf = ssl->bm_data;
feb11 0:85fceccc1a7c 121 int pkt_size = ssl->bm_index;
feb11 0:85fceccc1a7c 122 int i, j, cs_len, id_len, offset = 6 + SSL_RANDOM_SIZE;
feb11 0:85fceccc1a7c 123 int ret = SSL_OK;
feb11 0:85fceccc1a7c 124
feb11 0:85fceccc1a7c 125 uint8_t version = (buf[4] << 4) + buf[5];
feb11 0:85fceccc1a7c 126 ssl->version = ssl->client_version = version;
feb11 0:85fceccc1a7c 127
feb11 0:85fceccc1a7c 128 if (version > SSL_PROTOCOL_VERSION_MAX)
feb11 0:85fceccc1a7c 129 {
feb11 0:85fceccc1a7c 130 /* use client's version instead */
feb11 0:85fceccc1a7c 131 ssl->version = SSL_PROTOCOL_VERSION_MAX;
feb11 0:85fceccc1a7c 132 }
feb11 0:85fceccc1a7c 133 else if (version < SSL_PROTOCOL_MIN_VERSION) /* old version supported? */
feb11 0:85fceccc1a7c 134 {
feb11 0:85fceccc1a7c 135 ret = SSL_ERROR_INVALID_VERSION;
feb11 0:85fceccc1a7c 136 ssl_display_error(ret);
feb11 0:85fceccc1a7c 137 goto error;
feb11 0:85fceccc1a7c 138 }
feb11 0:85fceccc1a7c 139
feb11 0:85fceccc1a7c 140 memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
feb11 0:85fceccc1a7c 141
feb11 0:85fceccc1a7c 142 /* process the session id */
feb11 0:85fceccc1a7c 143 id_len = buf[offset++];
feb11 0:85fceccc1a7c 144 if (id_len > SSL_SESSION_ID_SIZE)
feb11 0:85fceccc1a7c 145 {
feb11 0:85fceccc1a7c 146 return SSL_ERROR_INVALID_SESSION;
feb11 0:85fceccc1a7c 147 }
feb11 0:85fceccc1a7c 148
feb11 0:85fceccc1a7c 149 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 150 ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
feb11 0:85fceccc1a7c 151 ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
feb11 0:85fceccc1a7c 152 #endif
feb11 0:85fceccc1a7c 153
feb11 0:85fceccc1a7c 154 offset += id_len;
feb11 0:85fceccc1a7c 155 cs_len = (buf[offset]<<8) + buf[offset+1];
feb11 0:85fceccc1a7c 156 offset += 3; /* add 1 due to all cipher suites being 8 bit */
feb11 0:85fceccc1a7c 157
feb11 0:85fceccc1a7c 158 PARANOIA_CHECK(pkt_size, offset);
feb11 0:85fceccc1a7c 159
feb11 0:85fceccc1a7c 160 /* work out what cipher suite we are going to use - client defines
feb11 0:85fceccc1a7c 161 the preference */
feb11 0:85fceccc1a7c 162 for (i = 0; i < cs_len; i += 2)
feb11 0:85fceccc1a7c 163 {
feb11 0:85fceccc1a7c 164 for (j = 0; j < NUM_PROTOCOLS; j++)
feb11 0:85fceccc1a7c 165 {
feb11 0:85fceccc1a7c 166 if (ssl_prot_prefs[j] == buf[offset+i]) /* got a match? */
feb11 0:85fceccc1a7c 167 {
feb11 0:85fceccc1a7c 168 ssl->cipher = ssl_prot_prefs[j];
feb11 0:85fceccc1a7c 169 goto do_state;
feb11 0:85fceccc1a7c 170 }
feb11 0:85fceccc1a7c 171 }
feb11 0:85fceccc1a7c 172 }
feb11 0:85fceccc1a7c 173
feb11 0:85fceccc1a7c 174 /* ouch! protocol is not supported */
feb11 0:85fceccc1a7c 175 ret = SSL_ERROR_NO_CIPHER;
feb11 0:85fceccc1a7c 176
feb11 0:85fceccc1a7c 177 do_state:
feb11 0:85fceccc1a7c 178 error:
feb11 0:85fceccc1a7c 179 return ret;
feb11 0:85fceccc1a7c 180 }
feb11 0:85fceccc1a7c 181
feb11 0:85fceccc1a7c 182 #ifdef CONFIG_SSL_ENABLE_V23_HANDSHAKE
feb11 0:85fceccc1a7c 183 /*
feb11 0:85fceccc1a7c 184 * Some browsers use a hybrid SSLv2 "client hello"
feb11 0:85fceccc1a7c 185 */
feb11 0:85fceccc1a7c 186 int process_sslv23_client_hello(SSL *ssl)
feb11 0:85fceccc1a7c 187 {
feb11 0:85fceccc1a7c 188 uint8_t *buf = ssl->bm_data;
feb11 0:85fceccc1a7c 189 int bytes_needed = ((buf[0] & 0x7f) << 8) + buf[1];
feb11 0:85fceccc1a7c 190 int ret = SSL_OK;
feb11 0:85fceccc1a7c 191
feb11 0:85fceccc1a7c 192 /* we have already read 3 extra bytes so far */
feb11 0:85fceccc1a7c 193 int read_len = SOCKET_READ(ssl->client_fd, buf, bytes_needed-3);
feb11 0:85fceccc1a7c 194 int cs_len = buf[1];
feb11 0:85fceccc1a7c 195 int id_len = buf[3];
feb11 0:85fceccc1a7c 196 int ch_len = buf[5];
feb11 0:85fceccc1a7c 197 int i, j, offset = 8; /* start at first cipher */
feb11 0:85fceccc1a7c 198 int random_offset = 0;
feb11 0:85fceccc1a7c 199
feb11 0:85fceccc1a7c 200 DISPLAY_BYTES(ssl, "received %d bytes", buf, read_len, read_len);
feb11 0:85fceccc1a7c 201
feb11 0:85fceccc1a7c 202 add_packet(ssl, buf, read_len);
feb11 0:85fceccc1a7c 203
feb11 0:85fceccc1a7c 204 /* connection has gone, so die */
feb11 0:85fceccc1a7c 205 if (bytes_needed < 0)
feb11 0:85fceccc1a7c 206 {
feb11 0:85fceccc1a7c 207 return SSL_ERROR_CONN_LOST;
feb11 0:85fceccc1a7c 208 }
feb11 0:85fceccc1a7c 209
feb11 0:85fceccc1a7c 210 /* now work out what cipher suite we are going to use */
feb11 0:85fceccc1a7c 211 for (j = 0; j < NUM_PROTOCOLS; j++)
feb11 0:85fceccc1a7c 212 {
feb11 0:85fceccc1a7c 213 for (i = 0; i < cs_len; i += 3)
feb11 0:85fceccc1a7c 214 {
feb11 0:85fceccc1a7c 215 if (ssl_prot_prefs[j] == buf[offset+i])
feb11 0:85fceccc1a7c 216 {
feb11 0:85fceccc1a7c 217 ssl->cipher = ssl_prot_prefs[j];
feb11 0:85fceccc1a7c 218 goto server_hello;
feb11 0:85fceccc1a7c 219 }
feb11 0:85fceccc1a7c 220 }
feb11 0:85fceccc1a7c 221 }
feb11 0:85fceccc1a7c 222
feb11 0:85fceccc1a7c 223 /* ouch! protocol is not supported */
feb11 0:85fceccc1a7c 224 ret = SSL_ERROR_NO_CIPHER;
feb11 0:85fceccc1a7c 225 goto error;
feb11 0:85fceccc1a7c 226
feb11 0:85fceccc1a7c 227 server_hello:
feb11 0:85fceccc1a7c 228 /* get the session id */
feb11 0:85fceccc1a7c 229 offset += cs_len - 2; /* we've gone 2 bytes past the end */
feb11 0:85fceccc1a7c 230 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 231 ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
feb11 0:85fceccc1a7c 232 ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
feb11 0:85fceccc1a7c 233 #endif
feb11 0:85fceccc1a7c 234
feb11 0:85fceccc1a7c 235 /* get the client random data */
feb11 0:85fceccc1a7c 236 offset += id_len;
feb11 0:85fceccc1a7c 237
feb11 0:85fceccc1a7c 238 /* random can be anywhere between 16 and 32 bytes long - so it is padded
feb11 0:85fceccc1a7c 239 * with 0's to the left */
feb11 0:85fceccc1a7c 240 if (ch_len == 0x10)
feb11 0:85fceccc1a7c 241 {
feb11 0:85fceccc1a7c 242 random_offset += 0x10;
feb11 0:85fceccc1a7c 243 }
feb11 0:85fceccc1a7c 244
feb11 0:85fceccc1a7c 245 memcpy(&ssl->dc->client_random[random_offset], &buf[offset], ch_len);
feb11 0:85fceccc1a7c 246 ret = send_server_hello_sequence(ssl);
feb11 0:85fceccc1a7c 247
feb11 0:85fceccc1a7c 248 error:
feb11 0:85fceccc1a7c 249 return ret;
feb11 0:85fceccc1a7c 250 }
feb11 0:85fceccc1a7c 251 #endif
feb11 0:85fceccc1a7c 252
feb11 0:85fceccc1a7c 253 /*
feb11 0:85fceccc1a7c 254 * Send the entire server hello sequence
feb11 0:85fceccc1a7c 255 */
feb11 0:85fceccc1a7c 256 static int send_server_hello_sequence(SSL *ssl)
feb11 0:85fceccc1a7c 257 {
feb11 0:85fceccc1a7c 258 int ret;
feb11 0:85fceccc1a7c 259
feb11 0:85fceccc1a7c 260 if ((ret = send_server_hello(ssl)) == SSL_OK)
feb11 0:85fceccc1a7c 261 {
feb11 0:85fceccc1a7c 262 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 263 /* resume handshake? */
feb11 0:85fceccc1a7c 264 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
feb11 0:85fceccc1a7c 265 {
feb11 0:85fceccc1a7c 266 if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
feb11 0:85fceccc1a7c 267 {
feb11 0:85fceccc1a7c 268 ret = send_finished(ssl);
feb11 0:85fceccc1a7c 269 ssl->next_state = HS_FINISHED;
feb11 0:85fceccc1a7c 270 }
feb11 0:85fceccc1a7c 271 }
feb11 0:85fceccc1a7c 272 else
feb11 0:85fceccc1a7c 273 #endif
feb11 0:85fceccc1a7c 274 if ((ret = send_certificate(ssl)) == SSL_OK)
feb11 0:85fceccc1a7c 275 {
feb11 0:85fceccc1a7c 276 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 277 /* ask the client for its certificate */
feb11 0:85fceccc1a7c 278 if (IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION))
feb11 0:85fceccc1a7c 279 {
feb11 0:85fceccc1a7c 280 if ((ret = send_certificate_request(ssl)) == SSL_OK)
feb11 0:85fceccc1a7c 281 {
feb11 0:85fceccc1a7c 282 ret = send_server_hello_done(ssl);
feb11 0:85fceccc1a7c 283 ssl->next_state = HS_CERTIFICATE;
feb11 0:85fceccc1a7c 284 }
feb11 0:85fceccc1a7c 285 }
feb11 0:85fceccc1a7c 286 else
feb11 0:85fceccc1a7c 287 #endif
feb11 0:85fceccc1a7c 288 {
feb11 0:85fceccc1a7c 289 ret = send_server_hello_done(ssl);
feb11 0:85fceccc1a7c 290 ssl->next_state = HS_CLIENT_KEY_XCHG;
feb11 0:85fceccc1a7c 291 }
feb11 0:85fceccc1a7c 292 }
feb11 0:85fceccc1a7c 293 }
feb11 0:85fceccc1a7c 294
feb11 0:85fceccc1a7c 295 return ret;
feb11 0:85fceccc1a7c 296 }
feb11 0:85fceccc1a7c 297
feb11 0:85fceccc1a7c 298 /*
feb11 0:85fceccc1a7c 299 * Send a server hello message.
feb11 0:85fceccc1a7c 300 */
feb11 0:85fceccc1a7c 301 static int send_server_hello(SSL *ssl)
feb11 0:85fceccc1a7c 302 {
feb11 0:85fceccc1a7c 303 uint8_t *buf = ssl->bm_data;
feb11 0:85fceccc1a7c 304 int offset = 0;
feb11 0:85fceccc1a7c 305
feb11 0:85fceccc1a7c 306 buf[0] = HS_SERVER_HELLO;
feb11 0:85fceccc1a7c 307 buf[1] = 0;
feb11 0:85fceccc1a7c 308 buf[2] = 0;
feb11 0:85fceccc1a7c 309 /* byte 3 is calculated later */
feb11 0:85fceccc1a7c 310 buf[4] = 0x03;
feb11 0:85fceccc1a7c 311 buf[5] = ssl->version & 0x0f;
feb11 0:85fceccc1a7c 312
feb11 0:85fceccc1a7c 313 /* server random value */
feb11 0:85fceccc1a7c 314 get_random(SSL_RANDOM_SIZE, &buf[6]);
feb11 0:85fceccc1a7c 315 memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
feb11 0:85fceccc1a7c 316 offset = 6 + SSL_RANDOM_SIZE;
feb11 0:85fceccc1a7c 317
feb11 0:85fceccc1a7c 318 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 319 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
feb11 0:85fceccc1a7c 320 {
feb11 0:85fceccc1a7c 321 /* retrieve id from session cache */
feb11 0:85fceccc1a7c 322 buf[offset++] = SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 323 memcpy(&buf[offset], ssl->session->session_id, SSL_SESSION_ID_SIZE);
feb11 0:85fceccc1a7c 324 memcpy(ssl->session_id, ssl->session->session_id, SSL_SESSION_ID_SIZE);
feb11 0:85fceccc1a7c 325 ssl->sess_id_size = SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 326 offset += SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 327 }
feb11 0:85fceccc1a7c 328 else /* generate our own session id */
feb11 0:85fceccc1a7c 329 #endif
feb11 0:85fceccc1a7c 330 {
feb11 0:85fceccc1a7c 331 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 332 buf[offset++] = SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 333 get_random(SSL_SESSION_ID_SIZE, &buf[offset]);
feb11 0:85fceccc1a7c 334 memcpy(ssl->session_id, &buf[offset], SSL_SESSION_ID_SIZE);
feb11 0:85fceccc1a7c 335 ssl->sess_id_size = SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 336
feb11 0:85fceccc1a7c 337 /* store id in session cache */
feb11 0:85fceccc1a7c 338 if (ssl->ssl_ctx->num_sessions)
feb11 0:85fceccc1a7c 339 {
feb11 0:85fceccc1a7c 340 memcpy(ssl->session->session_id,
feb11 0:85fceccc1a7c 341 ssl->session_id, SSL_SESSION_ID_SIZE);
feb11 0:85fceccc1a7c 342 }
feb11 0:85fceccc1a7c 343
feb11 0:85fceccc1a7c 344 offset += SSL_SESSION_ID_SIZE;
feb11 0:85fceccc1a7c 345 #else
feb11 0:85fceccc1a7c 346 buf[offset++] = 0; /* don't bother with session id in skelton mode */
feb11 0:85fceccc1a7c 347 #endif
feb11 0:85fceccc1a7c 348 }
feb11 0:85fceccc1a7c 349
feb11 0:85fceccc1a7c 350 buf[offset++] = 0; /* cipher we are using */
feb11 0:85fceccc1a7c 351 buf[offset++] = ssl->cipher;
feb11 0:85fceccc1a7c 352 buf[offset++] = 0; /* no compression */
feb11 0:85fceccc1a7c 353 buf[3] = offset - 4; /* handshake size */
feb11 0:85fceccc1a7c 354 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
feb11 0:85fceccc1a7c 355 }
feb11 0:85fceccc1a7c 356
feb11 0:85fceccc1a7c 357 /*
feb11 0:85fceccc1a7c 358 * Send the server hello done message.
feb11 0:85fceccc1a7c 359 */
feb11 0:85fceccc1a7c 360 static int send_server_hello_done(SSL *ssl)
feb11 0:85fceccc1a7c 361 {
feb11 0:85fceccc1a7c 362 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
feb11 0:85fceccc1a7c 363 g_hello_done, sizeof(g_hello_done));
feb11 0:85fceccc1a7c 364 }
feb11 0:85fceccc1a7c 365
feb11 0:85fceccc1a7c 366 /*
feb11 0:85fceccc1a7c 367 * Pull apart a client key exchange message. Decrypt the pre-master key (using
feb11 0:85fceccc1a7c 368 * our RSA private key) and then work out the master key. Initialise the
feb11 0:85fceccc1a7c 369 * ciphers.
feb11 0:85fceccc1a7c 370 */
feb11 0:85fceccc1a7c 371 static int process_client_key_xchg(SSL *ssl)
feb11 0:85fceccc1a7c 372 {
feb11 0:85fceccc1a7c 373 uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
feb11 0:85fceccc1a7c 374 int pkt_size = ssl->bm_index;
feb11 0:85fceccc1a7c 375 int premaster_size, secret_length = (buf[2] << 8) + buf[3];
feb11 0:85fceccc1a7c 376 uint8_t premaster_secret[MAX_KEY_BYTE_SIZE];
feb11 0:85fceccc1a7c 377 RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
feb11 0:85fceccc1a7c 378 int offset = 4;
feb11 0:85fceccc1a7c 379 int ret = SSL_OK;
feb11 0:85fceccc1a7c 380
feb11 0:85fceccc1a7c 381 if (rsa_ctx == NULL)
feb11 0:85fceccc1a7c 382 {
feb11 0:85fceccc1a7c 383 ret = SSL_ERROR_NO_CERT_DEFINED;
feb11 0:85fceccc1a7c 384 goto error;
feb11 0:85fceccc1a7c 385 }
feb11 0:85fceccc1a7c 386
feb11 0:85fceccc1a7c 387 /* is there an extra size field? */
feb11 0:85fceccc1a7c 388 if ((secret_length - 2) == rsa_ctx->num_octets)
feb11 0:85fceccc1a7c 389 offset += 2;
feb11 0:85fceccc1a7c 390
feb11 0:85fceccc1a7c 391 PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset);
feb11 0:85fceccc1a7c 392
feb11 0:85fceccc1a7c 393 /* rsa_ctx->bi_ctx is not thread-safe */
feb11 0:85fceccc1a7c 394 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
feb11 0:85fceccc1a7c 395 premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret, 1);
feb11 0:85fceccc1a7c 396 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
feb11 0:85fceccc1a7c 397
feb11 0:85fceccc1a7c 398 if (premaster_size != SSL_SECRET_SIZE ||
feb11 0:85fceccc1a7c 399 premaster_secret[0] != 0x03 || /* must be the same as client
feb11 0:85fceccc1a7c 400 offered version */
feb11 0:85fceccc1a7c 401 premaster_secret[1] != (ssl->client_version & 0x0f))
feb11 0:85fceccc1a7c 402 {
feb11 0:85fceccc1a7c 403 /* guard against a Bleichenbacher attack */
feb11 0:85fceccc1a7c 404 get_random(SSL_SECRET_SIZE, premaster_secret);
feb11 0:85fceccc1a7c 405 /* and continue - will die eventually when checking the mac */
feb11 0:85fceccc1a7c 406 }
feb11 0:85fceccc1a7c 407
feb11 0:85fceccc1a7c 408 #if 0
feb11 0:85fceccc1a7c 409 print_blob("pre-master", premaster_secret, SSL_SECRET_SIZE);
feb11 0:85fceccc1a7c 410 #endif
feb11 0:85fceccc1a7c 411
feb11 0:85fceccc1a7c 412 generate_master_secret(ssl, premaster_secret);
feb11 0:85fceccc1a7c 413
feb11 0:85fceccc1a7c 414 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 415 ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ?
feb11 0:85fceccc1a7c 416 HS_CERT_VERIFY : HS_FINISHED;
feb11 0:85fceccc1a7c 417 #else
feb11 0:85fceccc1a7c 418 ssl->next_state = HS_FINISHED;
feb11 0:85fceccc1a7c 419 #endif
feb11 0:85fceccc1a7c 420
feb11 0:85fceccc1a7c 421 ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset;
feb11 0:85fceccc1a7c 422 error:
feb11 0:85fceccc1a7c 423 return ret;
feb11 0:85fceccc1a7c 424 }
feb11 0:85fceccc1a7c 425
feb11 0:85fceccc1a7c 426 #ifdef CONFIG_SSL_CERT_VERIFICATION
feb11 0:85fceccc1a7c 427 static const uint8_t g_cert_request[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 };
feb11 0:85fceccc1a7c 428
feb11 0:85fceccc1a7c 429 /*
feb11 0:85fceccc1a7c 430 * Send the certificate request message.
feb11 0:85fceccc1a7c 431 */
feb11 0:85fceccc1a7c 432 static int send_certificate_request(SSL *ssl)
feb11 0:85fceccc1a7c 433 {
feb11 0:85fceccc1a7c 434 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
feb11 0:85fceccc1a7c 435 g_cert_request, sizeof(g_cert_request));
feb11 0:85fceccc1a7c 436 }
feb11 0:85fceccc1a7c 437
feb11 0:85fceccc1a7c 438 /*
feb11 0:85fceccc1a7c 439 * Ensure the client has the private key by first decrypting the packet and
feb11 0:85fceccc1a7c 440 * then checking the packet digests.
feb11 0:85fceccc1a7c 441 */
feb11 0:85fceccc1a7c 442 static int process_cert_verify(SSL *ssl)
feb11 0:85fceccc1a7c 443 {
feb11 0:85fceccc1a7c 444 uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
feb11 0:85fceccc1a7c 445 int pkt_size = ssl->bm_index;
feb11 0:85fceccc1a7c 446 uint8_t dgst_buf[MAX_KEY_BYTE_SIZE];
feb11 0:85fceccc1a7c 447 uint8_t dgst[MD5_SIZE+SHA1_SIZE];
feb11 0:85fceccc1a7c 448 X509_CTX *x509_ctx = ssl->x509_ctx;
feb11 0:85fceccc1a7c 449 int ret = SSL_OK;
feb11 0:85fceccc1a7c 450 int n;
feb11 0:85fceccc1a7c 451
feb11 0:85fceccc1a7c 452 PARANOIA_CHECK(pkt_size, x509_ctx->rsa_ctx->num_octets+6);
feb11 0:85fceccc1a7c 453 DISPLAY_RSA(ssl, x509_ctx->rsa_ctx);
feb11 0:85fceccc1a7c 454
feb11 0:85fceccc1a7c 455 /* rsa_ctx->bi_ctx is not thread-safe */
feb11 0:85fceccc1a7c 456 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
feb11 0:85fceccc1a7c 457 n = RSA_decrypt(x509_ctx->rsa_ctx, &buf[6], dgst_buf, 0);
feb11 0:85fceccc1a7c 458 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
feb11 0:85fceccc1a7c 459
feb11 0:85fceccc1a7c 460 if (n != SHA1_SIZE + MD5_SIZE)
feb11 0:85fceccc1a7c 461 {
feb11 0:85fceccc1a7c 462 ret = SSL_ERROR_INVALID_KEY;
feb11 0:85fceccc1a7c 463 goto end_cert_vfy;
feb11 0:85fceccc1a7c 464 }
feb11 0:85fceccc1a7c 465
feb11 0:85fceccc1a7c 466 finished_digest(ssl, NULL, dgst); /* calculate the digest */
feb11 0:85fceccc1a7c 467 if (memcmp(dgst_buf, dgst, MD5_SIZE + SHA1_SIZE))
feb11 0:85fceccc1a7c 468 {
feb11 0:85fceccc1a7c 469 ret = SSL_ERROR_INVALID_KEY;
feb11 0:85fceccc1a7c 470 }
feb11 0:85fceccc1a7c 471
feb11 0:85fceccc1a7c 472 end_cert_vfy:
feb11 0:85fceccc1a7c 473 ssl->next_state = HS_FINISHED;
feb11 0:85fceccc1a7c 474 error:
feb11 0:85fceccc1a7c 475 return ret;
feb11 0:85fceccc1a7c 476 }
feb11 0:85fceccc1a7c 477
feb11 0:85fceccc1a7c 478 #endif
feb11 0:85fceccc1a7c 479
feb11 0:85fceccc1a7c 480