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 * @mainpage axTLS API
feb11 0:85fceccc1a7c 33 *
feb11 0:85fceccc1a7c 34 * @image html axolotl.jpg
feb11 0:85fceccc1a7c 35 *
feb11 0:85fceccc1a7c 36 * The axTLS library has features such as:
feb11 0:85fceccc1a7c 37 * - The TLSv1 SSL client/server protocol
feb11 0:85fceccc1a7c 38 * - No requirement to use any openssl libraries.
feb11 0:85fceccc1a7c 39 * - A choice between AES block (128/256 bit) and RC4 (128 bit) stream ciphers.
feb11 0:85fceccc1a7c 40 * - RSA encryption/decryption with variable sized keys (up to 4096 bits).
feb11 0:85fceccc1a7c 41 * - Certificate chaining and peer authentication.
feb11 0:85fceccc1a7c 42 * - Session resumption, session renegotiation.
feb11 0:85fceccc1a7c 43 * - ASN.1, X.509, PKCS#8, PKCS#12 keys/certificates with DER/PEM encoding.
feb11 0:85fceccc1a7c 44 * - Highly configurable compile time options.
feb11 0:85fceccc1a7c 45 * - Portable across many platforms (written in ANSI C), and has language
feb11 0:85fceccc1a7c 46 * bindings in C, C#, VB.NET, Java, Perl and Lua.
feb11 0:85fceccc1a7c 47 * - Partial openssl API compatibility (via a wrapper).
feb11 0:85fceccc1a7c 48 * - A very small footprint (around 50-60kB for the library in 'server-only'
feb11 0:85fceccc1a7c 49 * mode).
feb11 0:85fceccc1a7c 50 * - No dependencies on sockets - can use serial connections for example.
feb11 0:85fceccc1a7c 51 * - A very simple API - ~ 20 functions/methods.
feb11 0:85fceccc1a7c 52 *
feb11 0:85fceccc1a7c 53 * A list of these functions/methods are described below.
feb11 0:85fceccc1a7c 54 *
feb11 0:85fceccc1a7c 55 * @ref c_api
feb11 0:85fceccc1a7c 56 *
feb11 0:85fceccc1a7c 57 * @ref bigint_api
feb11 0:85fceccc1a7c 58 *
feb11 0:85fceccc1a7c 59 * @ref csharp_api
feb11 0:85fceccc1a7c 60 *
feb11 0:85fceccc1a7c 61 * @ref java_api
feb11 0:85fceccc1a7c 62 */
feb11 0:85fceccc1a7c 63 #ifndef HEADER_SSL_H
feb11 0:85fceccc1a7c 64 #define HEADER_SSL_H
feb11 0:85fceccc1a7c 65
feb11 0:85fceccc1a7c 66 #ifdef __cplusplus
feb11 0:85fceccc1a7c 67 extern "C" {
feb11 0:85fceccc1a7c 68 #endif
feb11 0:85fceccc1a7c 69
feb11 0:85fceccc1a7c 70 #include <time.h>
feb11 0:85fceccc1a7c 71
feb11 0:85fceccc1a7c 72
feb11 0:85fceccc1a7c 73 /* need to predefine before ssl_lib.h gets to it */
feb11 0:85fceccc1a7c 74 #define SSL_SESSION_ID_SIZE 32
feb11 0:85fceccc1a7c 75
feb11 0:85fceccc1a7c 76
feb11 0:85fceccc1a7c 77 #include "tls1.h"
feb11 0:85fceccc1a7c 78
feb11 0:85fceccc1a7c 79 /* The optional parameters that can be given to the client/server SSL engine */
feb11 0:85fceccc1a7c 80 #define SSL_CLIENT_AUTHENTICATION 0x00010000
feb11 0:85fceccc1a7c 81 #define SSL_SERVER_VERIFY_LATER 0x00020000
feb11 0:85fceccc1a7c 82 #define SSL_NO_DEFAULT_KEY 0x00040000
feb11 0:85fceccc1a7c 83 #define SSL_DISPLAY_STATES 0x00080000
feb11 0:85fceccc1a7c 84 #define SSL_DISPLAY_BYTES 0x00100000
feb11 0:85fceccc1a7c 85 #define SSL_DISPLAY_CERTS 0x00200000
feb11 0:85fceccc1a7c 86 #define SSL_DISPLAY_RSA 0x00400000
feb11 0:85fceccc1a7c 87 #define SSL_CONNECT_IN_PARTS 0x00800000
feb11 0:85fceccc1a7c 88
feb11 0:85fceccc1a7c 89 /* errors that can be generated */
feb11 0:85fceccc1a7c 90 #define SSL_OK 0
feb11 0:85fceccc1a7c 91 #define SSL_NOT_OK -1
feb11 0:85fceccc1a7c 92 #define SSL_ERROR_DEAD -2
feb11 0:85fceccc1a7c 93 #define SSL_CLOSE_NOTIFY -3
feb11 0:85fceccc1a7c 94 #define SSL_ERROR_CONN_LOST -256
feb11 0:85fceccc1a7c 95 #define SSL_ERROR_SOCK_SETUP_FAILURE -258
feb11 0:85fceccc1a7c 96 #define SSL_ERROR_INVALID_HANDSHAKE -260
feb11 0:85fceccc1a7c 97 #define SSL_ERROR_INVALID_PROT_MSG -261
feb11 0:85fceccc1a7c 98 #define SSL_ERROR_INVALID_HMAC -262
feb11 0:85fceccc1a7c 99 #define SSL_ERROR_INVALID_VERSION -263
feb11 0:85fceccc1a7c 100 #define SSL_ERROR_INVALID_SESSION -265
feb11 0:85fceccc1a7c 101 #define SSL_ERROR_NO_CIPHER -266
feb11 0:85fceccc1a7c 102 #define SSL_ERROR_BAD_CERTIFICATE -268
feb11 0:85fceccc1a7c 103 #define SSL_ERROR_INVALID_KEY -269
feb11 0:85fceccc1a7c 104 #define SSL_ERROR_FINISHED_INVALID -271
feb11 0:85fceccc1a7c 105 #define SSL_ERROR_NO_CERT_DEFINED -272
feb11 0:85fceccc1a7c 106 #define SSL_ERROR_NO_CLIENT_RENOG -273
feb11 0:85fceccc1a7c 107 #define SSL_ERROR_NOT_SUPPORTED -274
feb11 0:85fceccc1a7c 108 #define SSL_X509_OFFSET -512
feb11 0:85fceccc1a7c 109 #define SSL_X509_ERROR(A) (SSL_X509_OFFSET+A)
feb11 0:85fceccc1a7c 110
feb11 0:85fceccc1a7c 111 /* alert types that are recognized */
feb11 0:85fceccc1a7c 112 #define SSL_ALERT_TYPE_WARNING 1
feb11 0:85fceccc1a7c 113 #define SLL_ALERT_TYPE_FATAL 2
feb11 0:85fceccc1a7c 114
feb11 0:85fceccc1a7c 115 /* these are all the alerts that are recognized */
feb11 0:85fceccc1a7c 116 #define SSL_ALERT_CLOSE_NOTIFY 0
feb11 0:85fceccc1a7c 117 #define SSL_ALERT_UNEXPECTED_MESSAGE 10
feb11 0:85fceccc1a7c 118 #define SSL_ALERT_BAD_RECORD_MAC 20
feb11 0:85fceccc1a7c 119 #define SSL_ALERT_HANDSHAKE_FAILURE 40
feb11 0:85fceccc1a7c 120 #define SSL_ALERT_BAD_CERTIFICATE 42
feb11 0:85fceccc1a7c 121 #define SSL_ALERT_ILLEGAL_PARAMETER 47
feb11 0:85fceccc1a7c 122 #define SSL_ALERT_DECODE_ERROR 50
feb11 0:85fceccc1a7c 123 #define SSL_ALERT_DECRYPT_ERROR 51
feb11 0:85fceccc1a7c 124 #define SSL_ALERT_INVALID_VERSION 70
feb11 0:85fceccc1a7c 125 #define SSL_ALERT_NO_RENEGOTIATION 100
feb11 0:85fceccc1a7c 126
feb11 0:85fceccc1a7c 127 /* The ciphers that are supported */
feb11 0:85fceccc1a7c 128 #define SSL_AES128_SHA 0x2f
feb11 0:85fceccc1a7c 129 #define SSL_AES256_SHA 0x35
feb11 0:85fceccc1a7c 130 #define SSL_RC4_128_SHA 0x05
feb11 0:85fceccc1a7c 131 #define SSL_RC4_128_MD5 0x04
feb11 0:85fceccc1a7c 132
feb11 0:85fceccc1a7c 133 /* build mode ids' */
feb11 0:85fceccc1a7c 134 #define SSL_BUILD_SKELETON_MODE 0x01
feb11 0:85fceccc1a7c 135 #define SSL_BUILD_SERVER_ONLY 0x02
feb11 0:85fceccc1a7c 136 #define SSL_BUILD_ENABLE_VERIFICATION 0x03
feb11 0:85fceccc1a7c 137 #define SSL_BUILD_ENABLE_CLIENT 0x04
feb11 0:85fceccc1a7c 138 #define SSL_BUILD_FULL_MODE 0x05
feb11 0:85fceccc1a7c 139
feb11 0:85fceccc1a7c 140 /* offsets to retrieve configuration information */
feb11 0:85fceccc1a7c 141 #define SSL_BUILD_MODE 0
feb11 0:85fceccc1a7c 142 #define SSL_MAX_CERT_CFG_OFFSET 1
feb11 0:85fceccc1a7c 143 #define SSL_MAX_CA_CERT_CFG_OFFSET 2
feb11 0:85fceccc1a7c 144 #define SSL_HAS_PEM 3
feb11 0:85fceccc1a7c 145
feb11 0:85fceccc1a7c 146 /* default session sizes */
feb11 0:85fceccc1a7c 147 #define SSL_DEFAULT_SVR_SESS 5
feb11 0:85fceccc1a7c 148 #define SSL_DEFAULT_CLNT_SESS 1
feb11 0:85fceccc1a7c 149
feb11 0:85fceccc1a7c 150 /* X.509/X.520 distinguished name types */
feb11 0:85fceccc1a7c 151 #define SSL_X509_CERT_COMMON_NAME 0
feb11 0:85fceccc1a7c 152 #define SSL_X509_CERT_ORGANIZATION 1
feb11 0:85fceccc1a7c 153 #define SSL_X509_CERT_ORGANIZATIONAL_NAME 2
feb11 0:85fceccc1a7c 154 #define SSL_X509_CA_CERT_COMMON_NAME 3
feb11 0:85fceccc1a7c 155 #define SSL_X509_CA_CERT_ORGANIZATION 4
feb11 0:85fceccc1a7c 156 #define SSL_X509_CA_CERT_ORGANIZATIONAL_NAME 5
feb11 0:85fceccc1a7c 157
feb11 0:85fceccc1a7c 158 /* SSL object loader types */
feb11 0:85fceccc1a7c 159 #define SSL_OBJ_X509_CERT 1
feb11 0:85fceccc1a7c 160 #define SSL_OBJ_X509_CACERT 2
feb11 0:85fceccc1a7c 161 #define SSL_OBJ_RSA_KEY 3
feb11 0:85fceccc1a7c 162 #define SSL_OBJ_PKCS8 4
feb11 0:85fceccc1a7c 163 #define SSL_OBJ_PKCS12 5
feb11 0:85fceccc1a7c 164
feb11 0:85fceccc1a7c 165 /**
feb11 0:85fceccc1a7c 166 * @defgroup c_api Standard C API
feb11 0:85fceccc1a7c 167 * @brief The standard interface in C.
feb11 0:85fceccc1a7c 168 * @{
feb11 0:85fceccc1a7c 169 */
feb11 0:85fceccc1a7c 170
feb11 0:85fceccc1a7c 171 /**
feb11 0:85fceccc1a7c 172 * @brief Establish a new client/server context.
feb11 0:85fceccc1a7c 173 *
feb11 0:85fceccc1a7c 174 * This function is called before any client/server SSL connections are made.
feb11 0:85fceccc1a7c 175 *
feb11 0:85fceccc1a7c 176 * Each new connection will use the this context's private key and
feb11 0:85fceccc1a7c 177 * certificate chain. If a different certificate chain is required, then a
feb11 0:85fceccc1a7c 178 * different context needs to be be used.
feb11 0:85fceccc1a7c 179 *
feb11 0:85fceccc1a7c 180 * There are two threading models supported - a single thread with one
feb11 0:85fceccc1a7c 181 * SSL_CTX can support any number of SSL connections - and multiple threads can
feb11 0:85fceccc1a7c 182 * support one SSL_CTX object each (the default). But if a single SSL_CTX
feb11 0:85fceccc1a7c 183 * object uses many SSL objects in individual threads, then the
feb11 0:85fceccc1a7c 184 * CONFIG_SSL_CTX_MUTEXING option needs to be configured.
feb11 0:85fceccc1a7c 185 *
feb11 0:85fceccc1a7c 186 * @param options [in] Any particular options. At present the options
feb11 0:85fceccc1a7c 187 * supported are:
feb11 0:85fceccc1a7c 188 * - SSL_SERVER_VERIFY_LATER (client only): Don't stop a handshake if the server
feb11 0:85fceccc1a7c 189 * authentication fails. The certificate can be authenticated later with a
feb11 0:85fceccc1a7c 190 * call to ssl_verify_cert().
feb11 0:85fceccc1a7c 191 * - SSL_CLIENT_AUTHENTICATION (server only): Enforce client authentication
feb11 0:85fceccc1a7c 192 * i.e. each handshake will include a "certificate request" message from the
feb11 0:85fceccc1a7c 193 * server. Only available if verification has been enabled.
feb11 0:85fceccc1a7c 194 * - SSL_DISPLAY_BYTES (full mode build only): Display the byte sequences
feb11 0:85fceccc1a7c 195 * during the handshake.
feb11 0:85fceccc1a7c 196 * - SSL_DISPLAY_STATES (full mode build only): Display the state changes
feb11 0:85fceccc1a7c 197 * during the handshake.
feb11 0:85fceccc1a7c 198 * - SSL_DISPLAY_CERTS (full mode build only): Display the certificates that
feb11 0:85fceccc1a7c 199 * are passed during a handshake.
feb11 0:85fceccc1a7c 200 * - SSL_DISPLAY_RSA (full mode build only): Display the RSA key details that
feb11 0:85fceccc1a7c 201 * are passed during a handshake.
feb11 0:85fceccc1a7c 202 * - SSL_CONNECT_IN_PARTS (client only): To use a non-blocking version of
feb11 0:85fceccc1a7c 203 * ssl_client_new().
feb11 0:85fceccc1a7c 204 * @param num_sessions [in] The number of sessions to be used for session
feb11 0:85fceccc1a7c 205 * caching. If this value is 0, then there is no session caching. This option
feb11 0:85fceccc1a7c 206 * is not used in skeleton mode.
feb11 0:85fceccc1a7c 207 * @return A client/server context.
feb11 0:85fceccc1a7c 208 */
feb11 0:85fceccc1a7c 209 EXP_FUNC SSL_CTX * STDCALL ssl_ctx_new(SSL_CTX *ssl_ctx, uint32_t options, int num_sessions);
feb11 0:85fceccc1a7c 210
feb11 0:85fceccc1a7c 211 /**
feb11 0:85fceccc1a7c 212 * @brief Remove a client/server context.
feb11 0:85fceccc1a7c 213 *
feb11 0:85fceccc1a7c 214 * Frees any used resources used by this context. Each connection will be
feb11 0:85fceccc1a7c 215 * sent a "Close Notify" alert (if possible).
feb11 0:85fceccc1a7c 216 * @param ssl_ctx [in] The client/server context.
feb11 0:85fceccc1a7c 217 */
feb11 0:85fceccc1a7c 218 EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx);
feb11 0:85fceccc1a7c 219
feb11 0:85fceccc1a7c 220 /**
feb11 0:85fceccc1a7c 221 * @brief (server only) Establish a new SSL connection to an SSL client.
feb11 0:85fceccc1a7c 222 *
feb11 0:85fceccc1a7c 223 * It is up to the application to establish the logical connection (whether it
feb11 0:85fceccc1a7c 224 * is a socket, serial connection etc).
feb11 0:85fceccc1a7c 225 * @param ssl_ctx [in] The server context.
feb11 0:85fceccc1a7c 226 * @param client_fd [in] The client's file descriptor.
feb11 0:85fceccc1a7c 227 * @return An SSL object reference.
feb11 0:85fceccc1a7c 228 */
feb11 0:85fceccc1a7c 229 EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd);
feb11 0:85fceccc1a7c 230
feb11 0:85fceccc1a7c 231 /**
feb11 0:85fceccc1a7c 232 * @brief (client only) Establish a new SSL connection to an SSL server.
feb11 0:85fceccc1a7c 233 *
feb11 0:85fceccc1a7c 234 * It is up to the application to establish the initial logical connection
feb11 0:85fceccc1a7c 235 * (whether it is a socket, serial connection etc).
feb11 0:85fceccc1a7c 236 *
feb11 0:85fceccc1a7c 237 * This is a normally a blocking call - it will finish when the handshake is
feb11 0:85fceccc1a7c 238 * complete (or has failed). To use in non-blocking mode, set
feb11 0:85fceccc1a7c 239 * SSL_CONNECT_IN_PARTS in ssl_ctx_new().
feb11 0:85fceccc1a7c 240 * @param ssl_ctx [in] The client context.
feb11 0:85fceccc1a7c 241 * @param client_fd [in] The client's file descriptor.
feb11 0:85fceccc1a7c 242 * @param session_id [in] A 32 byte session id for session resumption. This
feb11 0:85fceccc1a7c 243 * can be null if no session resumption is being used or required. This option
feb11 0:85fceccc1a7c 244 * is not used in skeleton mode.
feb11 0:85fceccc1a7c 245 * @param sess_id_size The size of the session id (max 32)
feb11 0:85fceccc1a7c 246 * @return An SSL object reference. Use ssl_handshake_status() to check
feb11 0:85fceccc1a7c 247 * if a handshake succeeded.
feb11 0:85fceccc1a7c 248 */
feb11 0:85fceccc1a7c 249 EXP_FUNC SSL * STDCALL ssl_client_new(SSL *ssl, int client_fd, const uint8_t *session_id, uint8_t sess_id_size);
feb11 0:85fceccc1a7c 250
feb11 0:85fceccc1a7c 251 /**
feb11 0:85fceccc1a7c 252 * @brief Free any used resources on this connection.
feb11 0:85fceccc1a7c 253
feb11 0:85fceccc1a7c 254 * A "Close Notify" message is sent on this connection (if possible). It is up
feb11 0:85fceccc1a7c 255 * to the application to close the socket or file descriptor.
feb11 0:85fceccc1a7c 256 * @param ssl [in] The ssl object reference.
feb11 0:85fceccc1a7c 257 */
feb11 0:85fceccc1a7c 258 EXP_FUNC void STDCALL ssl_free(SSL *ssl);
feb11 0:85fceccc1a7c 259
feb11 0:85fceccc1a7c 260 /**
feb11 0:85fceccc1a7c 261 * @brief Read the SSL data stream.
feb11 0:85fceccc1a7c 262 * If the socket is non-blocking and data is blocked then SSO_OK will be
feb11 0:85fceccc1a7c 263 * returned.
feb11 0:85fceccc1a7c 264 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 265 * @param in_data [out] If the read was successful, a pointer to the read
feb11 0:85fceccc1a7c 266 * buffer will be here. Do NOT ever free this memory as this buffer is used in
feb11 0:85fceccc1a7c 267 * sucessive calls. If the call was unsuccessful, this value will be null.
feb11 0:85fceccc1a7c 268 * @return The number of decrypted bytes:
feb11 0:85fceccc1a7c 269 * - if > 0, then the handshaking is complete and we are returning the number
feb11 0:85fceccc1a7c 270 * of decrypted bytes.
feb11 0:85fceccc1a7c 271 * - SSL_OK if the handshaking stage is successful (but not yet complete).
feb11 0:85fceccc1a7c 272 * - < 0 if an error.
feb11 0:85fceccc1a7c 273 * @see ssl.h for the error code list.
feb11 0:85fceccc1a7c 274 * @note Use in_data before doing any successive ssl calls.
feb11 0:85fceccc1a7c 275 */
feb11 0:85fceccc1a7c 276 //EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data);
feb11 0:85fceccc1a7c 277
feb11 0:85fceccc1a7c 278 /**
feb11 0:85fceccc1a7c 279 * @brief Write to the SSL data stream.
feb11 0:85fceccc1a7c 280 * if the socket is non-blocking and data is blocked then a check is made
feb11 0:85fceccc1a7c 281 * to ensure that all data is sent (i.e. blocked mode is forced).
feb11 0:85fceccc1a7c 282 * @param ssl [in] An SSL obect reference.
feb11 0:85fceccc1a7c 283 * @param out_data [in] The data to be written
feb11 0:85fceccc1a7c 284 * @param out_len [in] The number of bytes to be written.
feb11 0:85fceccc1a7c 285 * @return The number of bytes sent, or if < 0 if an error.
feb11 0:85fceccc1a7c 286 * @see ssl.h for the error code list.
feb11 0:85fceccc1a7c 287 */
feb11 0:85fceccc1a7c 288 EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len);
feb11 0:85fceccc1a7c 289
feb11 0:85fceccc1a7c 290 /**
feb11 0:85fceccc1a7c 291 * @brief Find an ssl object based on a file descriptor.
feb11 0:85fceccc1a7c 292 *
feb11 0:85fceccc1a7c 293 * Goes through the list of SSL objects maintained in a client/server context
feb11 0:85fceccc1a7c 294 * to look for a file descriptor match.
feb11 0:85fceccc1a7c 295 * @param ssl_ctx [in] The client/server context.
feb11 0:85fceccc1a7c 296 * @param client_fd [in] The file descriptor.
feb11 0:85fceccc1a7c 297 * @return A reference to the SSL object. Returns null if the object could not
feb11 0:85fceccc1a7c 298 * be found.
feb11 0:85fceccc1a7c 299 */
feb11 0:85fceccc1a7c 300 EXP_FUNC SSL * STDCALL ssl_find(SSL_CTX *ssl_ctx, int client_fd);
feb11 0:85fceccc1a7c 301
feb11 0:85fceccc1a7c 302 /**
feb11 0:85fceccc1a7c 303 * @brief Get the session id for a handshake.
feb11 0:85fceccc1a7c 304 *
feb11 0:85fceccc1a7c 305 * This will be a 32 byte sequence and is available after the first
feb11 0:85fceccc1a7c 306 * handshaking messages are sent.
feb11 0:85fceccc1a7c 307 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 308 * @return The session id as a 32 byte sequence.
feb11 0:85fceccc1a7c 309 * @note A SSLv23 handshake may have only 16 valid bytes.
feb11 0:85fceccc1a7c 310 */
feb11 0:85fceccc1a7c 311 EXP_FUNC const uint8_t * STDCALL ssl_get_session_id(const SSL *ssl);
feb11 0:85fceccc1a7c 312
feb11 0:85fceccc1a7c 313 /**
feb11 0:85fceccc1a7c 314 * @brief Get the session id size for a handshake.
feb11 0:85fceccc1a7c 315 *
feb11 0:85fceccc1a7c 316 * This will normally be 32 but could be 0 (no session id) or something else.
feb11 0:85fceccc1a7c 317 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 318 * @return The size of the session id.
feb11 0:85fceccc1a7c 319 */
feb11 0:85fceccc1a7c 320 EXP_FUNC uint8_t STDCALL ssl_get_session_id_size(const SSL *ssl);
feb11 0:85fceccc1a7c 321
feb11 0:85fceccc1a7c 322 /**
feb11 0:85fceccc1a7c 323 * @brief Return the cipher id (in the SSL form).
feb11 0:85fceccc1a7c 324 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 325 * @return The cipher id. This will be one of the following:
feb11 0:85fceccc1a7c 326 * - SSL_AES128_SHA (0x2f)
feb11 0:85fceccc1a7c 327 * - SSL_AES256_SHA (0x35)
feb11 0:85fceccc1a7c 328 * - SSL_RC4_128_SHA (0x05)
feb11 0:85fceccc1a7c 329 * - SSL_RC4_128_MD5 (0x04)
feb11 0:85fceccc1a7c 330 */
feb11 0:85fceccc1a7c 331 EXP_FUNC uint8_t STDCALL ssl_get_cipher_id(const SSL *ssl);
feb11 0:85fceccc1a7c 332
feb11 0:85fceccc1a7c 333 /**
feb11 0:85fceccc1a7c 334 * @brief Return the status of the handshake.
feb11 0:85fceccc1a7c 335 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 336 * @return SSL_OK if the handshake is complete and ok.
feb11 0:85fceccc1a7c 337 * @see ssl.h for the error code list.
feb11 0:85fceccc1a7c 338 */
feb11 0:85fceccc1a7c 339 EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl);
feb11 0:85fceccc1a7c 340
feb11 0:85fceccc1a7c 341 /**
feb11 0:85fceccc1a7c 342 * @brief Retrieve various parameters about the axTLS engine.
feb11 0:85fceccc1a7c 343 * @param offset [in] The configuration offset. It will be one of the following:
feb11 0:85fceccc1a7c 344 * - SSL_BUILD_MODE The build mode. This will be one of the following:
feb11 0:85fceccc1a7c 345 * - SSL_BUILD_SERVER_ONLY (basic server mode)
feb11 0:85fceccc1a7c 346 * - SSL_BUILD_ENABLE_VERIFICATION (server can do client authentication)
feb11 0:85fceccc1a7c 347 * - SSL_BUILD_ENABLE_CLIENT (client/server capabilties)
feb11 0:85fceccc1a7c 348 * - SSL_BUILD_FULL_MODE (client/server with diagnostics)
feb11 0:85fceccc1a7c 349 * - SSL_BUILD_SKELETON_MODE (skeleton mode)
feb11 0:85fceccc1a7c 350 * - SSL_MAX_CERT_CFG_OFFSET The maximum number of certificates allowed.
feb11 0:85fceccc1a7c 351 * - SSL_MAX_CA_CERT_CFG_OFFSET The maximum number of CA certificates allowed.
feb11 0:85fceccc1a7c 352 * - SSL_HAS_PEM 1 if supported
feb11 0:85fceccc1a7c 353 * @return The value of the requested parameter.
feb11 0:85fceccc1a7c 354 */
feb11 0:85fceccc1a7c 355 EXP_FUNC int STDCALL ssl_get_config(int offset);
feb11 0:85fceccc1a7c 356
feb11 0:85fceccc1a7c 357 /**
feb11 0:85fceccc1a7c 358 * @brief Display why the handshake failed.
feb11 0:85fceccc1a7c 359 *
feb11 0:85fceccc1a7c 360 * This call is only useful in a 'full mode' build. The output is to stdout.
feb11 0:85fceccc1a7c 361 * @param error_code [in] An error code.
feb11 0:85fceccc1a7c 362 * @see ssl.h for the error code list.
feb11 0:85fceccc1a7c 363 */
feb11 0:85fceccc1a7c 364 EXP_FUNC void STDCALL ssl_display_error(int error_code);
feb11 0:85fceccc1a7c 365
feb11 0:85fceccc1a7c 366 /**
feb11 0:85fceccc1a7c 367 * @brief Authenticate a received certificate.
feb11 0:85fceccc1a7c 368 *
feb11 0:85fceccc1a7c 369 * This call is usually made by a client after a handshake is complete and the
feb11 0:85fceccc1a7c 370 * context is in SSL_SERVER_VERIFY_LATER mode.
feb11 0:85fceccc1a7c 371 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 372 * @return SSL_OK if the certificate is verified.
feb11 0:85fceccc1a7c 373 */
feb11 0:85fceccc1a7c 374 EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl, PrecomputedCertificate *cert);
feb11 0:85fceccc1a7c 375
feb11 0:85fceccc1a7c 376 /**
feb11 0:85fceccc1a7c 377 * @brief Retrieve an X.509 distinguished name component.
feb11 0:85fceccc1a7c 378 *
feb11 0:85fceccc1a7c 379 * When a handshake is complete and a certificate has been exchanged, then the
feb11 0:85fceccc1a7c 380 * details of the remote certificate can be retrieved.
feb11 0:85fceccc1a7c 381 *
feb11 0:85fceccc1a7c 382 * This will usually be used by a client to check that the server's common
feb11 0:85fceccc1a7c 383 * name matches the URL.
feb11 0:85fceccc1a7c 384 *
feb11 0:85fceccc1a7c 385 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 386 * @param component [in] one of:
feb11 0:85fceccc1a7c 387 * - SSL_X509_CERT_COMMON_NAME
feb11 0:85fceccc1a7c 388 * - SSL_X509_CERT_ORGANIZATION
feb11 0:85fceccc1a7c 389 * - SSL_X509_CERT_ORGANIZATIONAL_NAME
feb11 0:85fceccc1a7c 390 * - SSL_X509_CA_CERT_COMMON_NAME
feb11 0:85fceccc1a7c 391 * - SSL_X509_CA_CERT_ORGANIZATION
feb11 0:85fceccc1a7c 392 * - SSL_X509_CA_CERT_ORGANIZATIONAL_NAME
feb11 0:85fceccc1a7c 393 * @return The appropriate string (or null if not defined)
feb11 0:85fceccc1a7c 394 * @note Verification build mode must be enabled.
feb11 0:85fceccc1a7c 395 */
feb11 0:85fceccc1a7c 396 EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component);
feb11 0:85fceccc1a7c 397
feb11 0:85fceccc1a7c 398 /**
feb11 0:85fceccc1a7c 399 * @brief Retrieve a Subject Alternative DNSName
feb11 0:85fceccc1a7c 400 *
feb11 0:85fceccc1a7c 401 * When a handshake is complete and a certificate has been exchanged, then the
feb11 0:85fceccc1a7c 402 * details of the remote certificate can be retrieved.
feb11 0:85fceccc1a7c 403 *
feb11 0:85fceccc1a7c 404 * This will usually be used by a client to check that the server's DNS
feb11 0:85fceccc1a7c 405 * name matches the URL.
feb11 0:85fceccc1a7c 406 *
feb11 0:85fceccc1a7c 407 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 408 * @param dnsindex [in] The index of the DNS name to retrieve.
feb11 0:85fceccc1a7c 409 * @return The appropriate string (or null if not defined)
feb11 0:85fceccc1a7c 410 * @note Verification build mode must be enabled.
feb11 0:85fceccc1a7c 411 */
feb11 0:85fceccc1a7c 412 EXP_FUNC const char * STDCALL ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int dnsindex);
feb11 0:85fceccc1a7c 413
feb11 0:85fceccc1a7c 414 /**
feb11 0:85fceccc1a7c 415 * @brief Force the client to perform its handshake again.
feb11 0:85fceccc1a7c 416 *
feb11 0:85fceccc1a7c 417 * For a client this involves sending another "client hello" message.
feb11 0:85fceccc1a7c 418 * For the server is means sending a "hello request" message.
feb11 0:85fceccc1a7c 419 *
feb11 0:85fceccc1a7c 420 * This is a blocking call on the client (until the handshake completes).
feb11 0:85fceccc1a7c 421 *
feb11 0:85fceccc1a7c 422 * @param ssl [in] An SSL object reference.
feb11 0:85fceccc1a7c 423 * @return SSL_OK if renegotiation instantiation was ok
feb11 0:85fceccc1a7c 424 */
feb11 0:85fceccc1a7c 425 EXP_FUNC int STDCALL ssl_renegotiate(SSL *ssl);
feb11 0:85fceccc1a7c 426
feb11 0:85fceccc1a7c 427 /**
feb11 0:85fceccc1a7c 428 * @brief Process a file that is in binary DER or ASCII PEM format.
feb11 0:85fceccc1a7c 429 *
feb11 0:85fceccc1a7c 430 * These are temporary objects that are used to load private keys,
feb11 0:85fceccc1a7c 431 * certificates etc into memory.
feb11 0:85fceccc1a7c 432 * @param ssl_ctx [in] The client/server context.
feb11 0:85fceccc1a7c 433 * @param obj_type [in] The format of the file. Can be one of:
feb11 0:85fceccc1a7c 434 * - SSL_OBJ_X509_CERT (no password required)
feb11 0:85fceccc1a7c 435 * - SSL_OBJ_X509_CACERT (no password required)
feb11 0:85fceccc1a7c 436 * - SSL_OBJ_RSA_KEY (AES128/AES256 PEM encryption supported)
feb11 0:85fceccc1a7c 437 * - SSL_OBJ_PKCS8 (RC4-128 encrypted data supported)
feb11 0:85fceccc1a7c 438 * - SSL_OBJ_PKCS12 (RC4-128 encrypted data supported)
feb11 0:85fceccc1a7c 439 *
feb11 0:85fceccc1a7c 440 * PEM files are automatically detected (if supported). The object type is
feb11 0:85fceccc1a7c 441 * also detected, and so is not relevant for these types of files.
feb11 0:85fceccc1a7c 442 * @param filename [in] The location of a file in DER/PEM format.
feb11 0:85fceccc1a7c 443 * @param password [in] The password used. Can be null if not required.
feb11 0:85fceccc1a7c 444 * @return SSL_OK if all ok
feb11 0:85fceccc1a7c 445 * @note Not available in skeleton build mode.
feb11 0:85fceccc1a7c 446 */
feb11 0:85fceccc1a7c 447 EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type, const char *filename, const char *password);
feb11 0:85fceccc1a7c 448
feb11 0:85fceccc1a7c 449 /**
feb11 0:85fceccc1a7c 450 * @brief Process binary data.
feb11 0:85fceccc1a7c 451 *
feb11 0:85fceccc1a7c 452 * These are temporary objects that are used to load private keys,
feb11 0:85fceccc1a7c 453 * certificates etc into memory.
feb11 0:85fceccc1a7c 454 * @param ssl_ctx [in] The client/server context.
feb11 0:85fceccc1a7c 455 * @param obj_type [in] The format of the memory data.
feb11 0:85fceccc1a7c 456 * @param data [in] The binary data to be loaded.
feb11 0:85fceccc1a7c 457 * @param len [in] The amount of data to be loaded.
feb11 0:85fceccc1a7c 458 * @param password [in] The password used. Can be null if not required.
feb11 0:85fceccc1a7c 459 * @return SSL_OK if all ok
feb11 0:85fceccc1a7c 460 * @see ssl_obj_load for more details on obj_type.
feb11 0:85fceccc1a7c 461 */
feb11 0:85fceccc1a7c 462 EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int obj_type, const uint8_t *data, int len, const char *password);
feb11 0:85fceccc1a7c 463
feb11 0:85fceccc1a7c 464 #ifdef CONFIG_SSL_GENERATE_X509_CERT
feb11 0:85fceccc1a7c 465 /**
feb11 0:85fceccc1a7c 466 * @brief Create an X.509 certificate.
feb11 0:85fceccc1a7c 467 *
feb11 0:85fceccc1a7c 468 * This certificate is a self-signed v1 cert with a fixed start/stop validity
feb11 0:85fceccc1a7c 469 * times. It is signed with an internal private key in ssl_ctx.
feb11 0:85fceccc1a7c 470 *
feb11 0:85fceccc1a7c 471 * @param ssl_ctx [in] The client/server context.
feb11 0:85fceccc1a7c 472 * @param options [in] Not used yet.
feb11 0:85fceccc1a7c 473 * @param dn [in] An array of distinguished name strings. The array is defined
feb11 0:85fceccc1a7c 474 * by:
feb11 0:85fceccc1a7c 475 * - SSL_X509_CERT_COMMON_NAME (0)
feb11 0:85fceccc1a7c 476 * - If SSL_X509_CERT_COMMON_NAME is empty or not defined, then the
feb11 0:85fceccc1a7c 477 * hostname will be used.
feb11 0:85fceccc1a7c 478 * - SSL_X509_CERT_ORGANIZATION (1)
feb11 0:85fceccc1a7c 479 * - If SSL_X509_CERT_ORGANIZATION is empty or not defined, then $USERNAME
feb11 0:85fceccc1a7c 480 * will be used.
feb11 0:85fceccc1a7c 481 * - SSL_X509_CERT_ORGANIZATIONAL_NAME (2)
feb11 0:85fceccc1a7c 482 * - SSL_X509_CERT_ORGANIZATIONAL_NAME is optional.
feb11 0:85fceccc1a7c 483 * @param cert_data [out] The certificate as a sequence of bytes.
feb11 0:85fceccc1a7c 484 * @return < 0 if an error, or the size of the certificate in bytes.
feb11 0:85fceccc1a7c 485 * @note cert_data must be freed when there is no more need for it.
feb11 0:85fceccc1a7c 486 */
feb11 0:85fceccc1a7c 487 EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const char * dn[], uint8_t **cert_data);
feb11 0:85fceccc1a7c 488 #endif
feb11 0:85fceccc1a7c 489
feb11 0:85fceccc1a7c 490 /**
feb11 0:85fceccc1a7c 491 * @brief Return the axTLS library version as a string.
feb11 0:85fceccc1a7c 492 */
feb11 0:85fceccc1a7c 493 EXP_FUNC const char * STDCALL ssl_version(void);
feb11 0:85fceccc1a7c 494
feb11 0:85fceccc1a7c 495 /** @} */
feb11 0:85fceccc1a7c 496
feb11 0:85fceccc1a7c 497 #ifdef __cplusplus
feb11 0:85fceccc1a7c 498 }
feb11 0:85fceccc1a7c 499 #endif
feb11 0:85fceccc1a7c 500
feb11 0:85fceccc1a7c 501 #endif
feb11 0:85fceccc1a7c 502
feb11 0:85fceccc1a7c 503