mbed TLS Build

Dependents:   Encypting_Funcional

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssl.h Source File

ssl.h

Go to the documentation of this file.
00001 /**
00002  * \file ssl.h
00003  *
00004  * \brief SSL/TLS functions.
00005  *
00006  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00007  *  SPDX-License-Identifier: Apache-2.0
00008  *
00009  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  *  not use this file except in compliance with the License.
00011  *  You may obtain a copy of the License at
00012  *
00013  *  http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  *  Unless required by applicable law or agreed to in writing, software
00016  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  *  See the License for the specific language governing permissions and
00019  *  limitations under the License.
00020  *
00021  *  This file is part of mbed TLS (https://tls.mbed.org)
00022  */
00023 #ifndef MBEDTLS_SSL_H
00024 #define MBEDTLS_SSL_H
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #include "bignum.h"
00033 #include "ecp.h"
00034 
00035 #include "ssl_ciphersuites.h"
00036 
00037 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00038 #include "x509_crt.h"
00039 #include "x509_crl.h"
00040 #endif
00041 
00042 #if defined(MBEDTLS_DHM_C)
00043 #include "dhm.h"
00044 #endif
00045 
00046 #if defined(MBEDTLS_ECDH_C)
00047 #include "ecdh.h"
00048 #endif
00049 
00050 #if defined(MBEDTLS_ZLIB_SUPPORT)
00051 #include "zlib.h"
00052 #endif
00053 
00054 #if defined(MBEDTLS_HAVE_TIME)
00055 #include "mbedtls/platform_time.h"
00056 #endif
00057 
00058 /*
00059  * SSL Error codes
00060  */
00061 #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE               -0x7080  /**< The requested feature is not available. */
00062 #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA                    -0x7100  /**< Bad input parameters to function. */
00063 #define MBEDTLS_ERR_SSL_INVALID_MAC                       -0x7180  /**< Verification of the message MAC failed. */
00064 #define MBEDTLS_ERR_SSL_INVALID_RECORD                    -0x7200  /**< An invalid SSL record was received. */
00065 #define MBEDTLS_ERR_SSL_CONN_EOF                          -0x7280  /**< The connection indicated an EOF. */
00066 #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER                    -0x7300  /**< An unknown cipher was received. */
00067 #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN                  -0x7380  /**< The server has no ciphersuites in common with the client. */
00068 #define MBEDTLS_ERR_SSL_NO_RNG                            -0x7400  /**< No RNG was provided to the SSL module. */
00069 #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE             -0x7480  /**< No client certification received from the client, but required by the authentication mode. */
00070 #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE             -0x7500  /**< Our own certificate(s) is/are too large to send in an SSL message. */
00071 #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED              -0x7580  /**< The own certificate is not set, but needed by the server. */
00072 #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED              -0x7600  /**< The own private key or pre-shared key is not set, but needed. */
00073 #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED                 -0x7680  /**< No CA Chain is set, but required to operate. */
00074 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE                -0x7700  /**< An unexpected message was received from our peer. */
00075 #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE               -0x7780  /**< A fatal alert message was received from our peer. */
00076 #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED                -0x7800  /**< Verification of our peer failed. */
00077 #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY                 -0x7880  /**< The peer notified us that the connection is going to be closed. */
00078 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO               -0x7900  /**< Processing of the ClientHello handshake message failed. */
00079 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO               -0x7980  /**< Processing of the ServerHello handshake message failed. */
00080 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE                -0x7A00  /**< Processing of the Certificate handshake message failed. */
00081 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST        -0x7A80  /**< Processing of the CertificateRequest handshake message failed. */
00082 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE        -0x7B00  /**< Processing of the ServerKeyExchange handshake message failed. */
00083 #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE          -0x7B80  /**< Processing of the ServerHelloDone handshake message failed. */
00084 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE        -0x7C00  /**< Processing of the ClientKeyExchange handshake message failed. */
00085 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP     -0x7C80  /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
00086 #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS     -0x7D00  /**< Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
00087 #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY         -0x7D80  /**< Processing of the CertificateVerify handshake message failed. */
00088 #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC         -0x7E00  /**< Processing of the ChangeCipherSpec handshake message failed. */
00089 #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED                   -0x7E80  /**< Processing of the Finished handshake message failed. */
00090 #define MBEDTLS_ERR_SSL_ALLOC_FAILED                      -0x7F00  /**< Memory allocation failed */
00091 #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED                   -0x7F80  /**< Hardware acceleration function returned with error */
00092 #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH              -0x6F80  /**< Hardware acceleration function skipped / left alone data */
00093 #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED                -0x6F00  /**< Processing of the compression / decompression failed */
00094 #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION           -0x6E80  /**< Handshake protocol not within min/max boundaries */
00095 #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET         -0x6E00  /**< Processing of the NewSessionTicket handshake message failed. */
00096 #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED            -0x6D80  /**< Session ticket has expired. */
00097 #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH                  -0x6D00  /**< Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
00098 #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY                  -0x6C80  /**< Unknown identity received (eg, PSK identity) */
00099 #define MBEDTLS_ERR_SSL_INTERNAL_ERROR                    -0x6C00  /**< Internal error (eg, unexpected failure in lower-level module) */
00100 #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING                  -0x6B80  /**< A counter would wrap (eg, too many messages exchanged). */
00101 #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO       -0x6B00  /**< Unexpected message at ServerHello in renegotiation. */
00102 #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED             -0x6A80  /**< DTLS client must retry for hello verification */
00103 #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL                  -0x6A00  /**< A buffer is too small to receive or write a message */
00104 #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE             -0x6980  /**< None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
00105 #define MBEDTLS_ERR_SSL_WANT_READ                         -0x6900  /**< Connection requires a read call. */
00106 #define MBEDTLS_ERR_SSL_WANT_WRITE                        -0x6880  /**< Connection requires a write call. */
00107 #define MBEDTLS_ERR_SSL_TIMEOUT                           -0x6800  /**< The operation timed out. */
00108 #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT                  -0x6780  /**< The client initiated a reconnect from the same port. */
00109 #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD                 -0x6700  /**< Record header looks valid but is not expected. */
00110 #define MBEDTLS_ERR_SSL_NON_FATAL                         -0x6680  /**< The alert message received indicates a non-fatal error. */
00111 #define MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH               -0x6600  /**< Couldn't set the hash for verifying CertificateVerify */
00112 
00113 /*
00114  * Various constants
00115  */
00116 #define MBEDTLS_SSL_MAJOR_VERSION_3             3
00117 #define MBEDTLS_SSL_MINOR_VERSION_0             0   /*!< SSL v3.0 */
00118 #define MBEDTLS_SSL_MINOR_VERSION_1             1   /*!< TLS v1.0 */
00119 #define MBEDTLS_SSL_MINOR_VERSION_2             2   /*!< TLS v1.1 */
00120 #define MBEDTLS_SSL_MINOR_VERSION_3             3   /*!< TLS v1.2 */
00121 
00122 #define MBEDTLS_SSL_TRANSPORT_STREAM            0   /*!< TLS      */
00123 #define MBEDTLS_SSL_TRANSPORT_DATAGRAM          1   /*!< DTLS     */
00124 
00125 #define MBEDTLS_SSL_MAX_HOST_NAME_LEN           255 /*!< Maximum host name defined in RFC 1035 */
00126 
00127 /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
00128  * NONE must be zero so that memset()ing structure to zero works */
00129 #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE           0   /*!< don't use this extension   */
00130 #define MBEDTLS_SSL_MAX_FRAG_LEN_512            1   /*!< MaxFragmentLength 2^9      */
00131 #define MBEDTLS_SSL_MAX_FRAG_LEN_1024           2   /*!< MaxFragmentLength 2^10     */
00132 #define MBEDTLS_SSL_MAX_FRAG_LEN_2048           3   /*!< MaxFragmentLength 2^11     */
00133 #define MBEDTLS_SSL_MAX_FRAG_LEN_4096           4   /*!< MaxFragmentLength 2^12     */
00134 #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID        5   /*!< first invalid value        */
00135 
00136 #define MBEDTLS_SSL_IS_CLIENT                   0
00137 #define MBEDTLS_SSL_IS_SERVER                   1
00138 
00139 #define MBEDTLS_SSL_IS_NOT_FALLBACK             0
00140 #define MBEDTLS_SSL_IS_FALLBACK                 1
00141 
00142 #define MBEDTLS_SSL_EXTENDED_MS_DISABLED        0
00143 #define MBEDTLS_SSL_EXTENDED_MS_ENABLED         1
00144 
00145 #define MBEDTLS_SSL_ETM_DISABLED                0
00146 #define MBEDTLS_SSL_ETM_ENABLED                 1
00147 
00148 #define MBEDTLS_SSL_COMPRESS_NULL               0
00149 #define MBEDTLS_SSL_COMPRESS_DEFLATE            1
00150 
00151 #define MBEDTLS_SSL_VERIFY_NONE                 0
00152 #define MBEDTLS_SSL_VERIFY_OPTIONAL             1
00153 #define MBEDTLS_SSL_VERIFY_REQUIRED             2
00154 #define MBEDTLS_SSL_VERIFY_UNSET                3 /* Used only for sni_authmode */
00155 
00156 #define MBEDTLS_SSL_LEGACY_RENEGOTIATION        0
00157 #define MBEDTLS_SSL_SECURE_RENEGOTIATION        1
00158 
00159 #define MBEDTLS_SSL_RENEGOTIATION_DISABLED      0
00160 #define MBEDTLS_SSL_RENEGOTIATION_ENABLED       1
00161 
00162 #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED        0
00163 #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED         1
00164 
00165 #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED  -1
00166 #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT  16
00167 
00168 #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION     0
00169 #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION  1
00170 #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE      2
00171 
00172 #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED         0
00173 #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED          1
00174 #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN          10  /* 80 bits, rfc 6066 section 7 */
00175 
00176 #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED     0
00177 #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED      1
00178 
00179 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED    0
00180 #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED     1
00181 
00182 #define MBEDTLS_SSL_ARC4_ENABLED                0
00183 #define MBEDTLS_SSL_ARC4_DISABLED               1
00184 
00185 #define MBEDTLS_SSL_PRESET_DEFAULT              0
00186 #define MBEDTLS_SSL_PRESET_SUITEB               2
00187 
00188 /*
00189  * Default range for DTLS retransmission timer value, in milliseconds.
00190  * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
00191  */
00192 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN    1000
00193 #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX   60000
00194 
00195 /**
00196  * \name SECTION: Module settings
00197  *
00198  * The configuration options you can set for this module are in this section.
00199  * Either change them in config.h or define them on the compiler command line.
00200  * \{
00201  */
00202 
00203 #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
00204 #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME     86400 /**< Lifetime of session tickets (if enabled) */
00205 #endif
00206 
00207 /*
00208  * Maxium fragment length in bytes,
00209  * determines the size of each of the two internal I/O buffers.
00210  *
00211  * Note: the RFC defines the default size of SSL / TLS messages. If you
00212  * change the value here, other clients / servers may not be able to
00213  * communicate with you anymore. Only change this value if you control
00214  * both sides of the connection and have it reduced at both sides, or
00215  * if you're using the Max Fragment Length extension and you know all your
00216  * peers are using it too!
00217  */
00218 #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
00219 #define MBEDTLS_SSL_MAX_CONTENT_LEN         16384   /**< Size of the input / output buffer */
00220 #endif
00221 
00222 /* \} name SECTION: Module settings */
00223 
00224 /*
00225  * Length of the verify data for secure renegotiation
00226  */
00227 #if defined(MBEDTLS_SSL_PROTO_SSL3)
00228 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
00229 #else
00230 #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
00231 #endif
00232 
00233 /*
00234  * Signaling ciphersuite values (SCSV)
00235  */
00236 #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO    0xFF   /**< renegotiation info ext */
00237 #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE         0x5600 /**< RFC 7507 section 2 */
00238 
00239 /*
00240  * Supported Signature and Hash algorithms (For TLS 1.2)
00241  * RFC 5246 section 7.4.1.4.1
00242  */
00243 #define MBEDTLS_SSL_HASH_NONE                0
00244 #define MBEDTLS_SSL_HASH_MD5                 1
00245 #define MBEDTLS_SSL_HASH_SHA1                2
00246 #define MBEDTLS_SSL_HASH_SHA224              3
00247 #define MBEDTLS_SSL_HASH_SHA256              4
00248 #define MBEDTLS_SSL_HASH_SHA384              5
00249 #define MBEDTLS_SSL_HASH_SHA512              6
00250 
00251 #define MBEDTLS_SSL_SIG_ANON                 0
00252 #define MBEDTLS_SSL_SIG_RSA                  1
00253 #define MBEDTLS_SSL_SIG_ECDSA                3
00254 
00255 /*
00256  * Client Certificate Types
00257  * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
00258  */
00259 #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN       1
00260 #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN    64
00261 
00262 /*
00263  * Message, alert and handshake types
00264  */
00265 #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC     20
00266 #define MBEDTLS_SSL_MSG_ALERT                  21
00267 #define MBEDTLS_SSL_MSG_HANDSHAKE              22
00268 #define MBEDTLS_SSL_MSG_APPLICATION_DATA       23
00269 
00270 #define MBEDTLS_SSL_ALERT_LEVEL_WARNING         1
00271 #define MBEDTLS_SSL_ALERT_LEVEL_FATAL           2
00272 
00273 #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY           0  /* 0x00 */
00274 #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE    10  /* 0x0A */
00275 #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC        20  /* 0x14 */
00276 #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED     21  /* 0x15 */
00277 #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW       22  /* 0x16 */
00278 #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30  /* 0x1E */
00279 #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE     40  /* 0x28 */
00280 #define MBEDTLS_SSL_ALERT_MSG_NO_CERT               41  /* 0x29 */
00281 #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT              42  /* 0x2A */
00282 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT      43  /* 0x2B */
00283 #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED          44  /* 0x2C */
00284 #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED          45  /* 0x2D */
00285 #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN          46  /* 0x2E */
00286 #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER     47  /* 0x2F */
00287 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA            48  /* 0x30 */
00288 #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED         49  /* 0x31 */
00289 #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR          50  /* 0x32 */
00290 #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR         51  /* 0x33 */
00291 #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION    60  /* 0x3C */
00292 #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION      70  /* 0x46 */
00293 #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71  /* 0x47 */
00294 #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR        80  /* 0x50 */
00295 #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86  /* 0x56 */
00296 #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED         90  /* 0x5A */
00297 #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION     100  /* 0x64 */
00298 #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT      110  /* 0x6E */
00299 #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME    112  /* 0x70 */
00300 #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115  /* 0x73 */
00301 #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
00302 
00303 #define MBEDTLS_SSL_HS_HELLO_REQUEST            0
00304 #define MBEDTLS_SSL_HS_CLIENT_HELLO             1
00305 #define MBEDTLS_SSL_HS_SERVER_HELLO             2
00306 #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST     3
00307 #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET       4
00308 #define MBEDTLS_SSL_HS_CERTIFICATE             11
00309 #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE     12
00310 #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST     13
00311 #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE       14
00312 #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY      15
00313 #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE     16
00314 #define MBEDTLS_SSL_HS_FINISHED                20
00315 
00316 /*
00317  * TLS extensions
00318  */
00319 #define MBEDTLS_TLS_EXT_SERVERNAME                   0
00320 #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME          0
00321 
00322 #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH          1
00323 
00324 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC               4
00325 
00326 #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES   10
00327 #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS     11
00328 
00329 #define MBEDTLS_TLS_EXT_SIG_ALG                     13
00330 
00331 #define MBEDTLS_TLS_EXT_ALPN                        16
00332 
00333 #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC            22 /* 0x16 */
00334 #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET  0x0017 /* 23 */
00335 
00336 #define MBEDTLS_TLS_EXT_SESSION_TICKET              35
00337 
00338 #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP               256 /* experimental */
00339 
00340 #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO      0xFF01
00341 
00342 /*
00343  * Size defines
00344  */
00345 #if !defined(MBEDTLS_PSK_MAX_LEN)
00346 #define MBEDTLS_PSK_MAX_LEN            32 /* 256 bits */
00347 #endif
00348 
00349 /* Dummy type used only for its size */
00350 union mbedtls_ssl_premaster_secret
00351 {
00352 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
00353     unsigned char _pms_rsa[48];                         /* RFC 5246 8.1.1 */
00354 #endif
00355 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
00356     unsigned char _pms_dhm[MBEDTLS_MPI_MAX_SIZE];      /* RFC 5246 8.1.2 */
00357 #endif
00358 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)    || \
00359     defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)  || \
00360     defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED)     || \
00361     defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
00362     unsigned char _pms_ecdh[MBEDTLS_ECP_MAX_BYTES];    /* RFC 4492 5.10 */
00363 #endif
00364 #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
00365     unsigned char _pms_psk[4 + 2 * MBEDTLS_PSK_MAX_LEN];       /* RFC 4279 2 */
00366 #endif
00367 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
00368     unsigned char _pms_dhe_psk[4 + MBEDTLS_MPI_MAX_SIZE
00369                                  + MBEDTLS_PSK_MAX_LEN];       /* RFC 4279 3 */
00370 #endif
00371 #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
00372     unsigned char _pms_rsa_psk[52 + MBEDTLS_PSK_MAX_LEN];      /* RFC 4279 4 */
00373 #endif
00374 #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
00375     unsigned char _pms_ecdhe_psk[4 + MBEDTLS_ECP_MAX_BYTES
00376                                    + MBEDTLS_PSK_MAX_LEN];     /* RFC 5489 2 */
00377 #endif
00378 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
00379     unsigned char _pms_ecjpake[32];     /* Thread spec: SHA-256 output */
00380 #endif
00381 };
00382 
00383 #define MBEDTLS_PREMASTER_SIZE     sizeof( union mbedtls_ssl_premaster_secret )
00384 
00385 #ifdef __cplusplus
00386 extern "C" {
00387 #endif
00388 
00389 /*
00390  * SSL state machine
00391  */
00392 typedef enum
00393 {
00394     MBEDTLS_SSL_HELLO_REQUEST,
00395     MBEDTLS_SSL_CLIENT_HELLO,
00396     MBEDTLS_SSL_SERVER_HELLO,
00397     MBEDTLS_SSL_SERVER_CERTIFICATE,
00398     MBEDTLS_SSL_SERVER_KEY_EXCHANGE,
00399     MBEDTLS_SSL_CERTIFICATE_REQUEST,
00400     MBEDTLS_SSL_SERVER_HELLO_DONE,
00401     MBEDTLS_SSL_CLIENT_CERTIFICATE,
00402     MBEDTLS_SSL_CLIENT_KEY_EXCHANGE,
00403     MBEDTLS_SSL_CERTIFICATE_VERIFY,
00404     MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC,
00405     MBEDTLS_SSL_CLIENT_FINISHED,
00406     MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC,
00407     MBEDTLS_SSL_SERVER_FINISHED,
00408     MBEDTLS_SSL_FLUSH_BUFFERS,
00409     MBEDTLS_SSL_HANDSHAKE_WRAPUP,
00410     MBEDTLS_SSL_HANDSHAKE_OVER,
00411     MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET,
00412     MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT,
00413 }
00414 mbedtls_ssl_states;
00415 
00416 /**
00417  * \brief          Callback type: send data on the network.
00418  *
00419  * \note           That callback may be either blocking or non-blocking.
00420  *
00421  * \param ctx      Context for the send callback (typically a file descriptor)
00422  * \param buf      Buffer holding the data to send
00423  * \param len      Length of the data to send
00424  *
00425  * \return         The callback must return the number of bytes sent if any,
00426  *                 or a non-zero error code.
00427  *                 If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_WRITE
00428  *                 must be returned when the operation would block.
00429  *
00430  * \note           The callback is allowed to send fewer bytes than requested.
00431  *                 It must always return the number of bytes actually sent.
00432  */
00433 typedef int mbedtls_ssl_send_t( void *ctx,
00434                                 const unsigned char *buf,
00435                                 size_t len );
00436 
00437 /**
00438  * \brief          Callback type: receive data from the network.
00439  *
00440  * \note           That callback may be either blocking or non-blocking.
00441  *
00442  * \param ctx      Context for the receive callback (typically a file
00443  *                 descriptor)
00444  * \param buf      Buffer to write the received data to
00445  * \param len      Length of the receive buffer
00446  *
00447  * \return         The callback must return the number of bytes received,
00448  *                 or a non-zero error code.
00449  *                 If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ
00450  *                 must be returned when the operation would block.
00451  *
00452  * \note           The callback may receive fewer bytes than the length of the
00453  *                 buffer. It must always return the number of bytes actually
00454  *                 received and written to the buffer.
00455  */
00456 typedef int mbedtls_ssl_recv_t( void *ctx,
00457                                 unsigned char *buf,
00458                                 size_t len );
00459 
00460 /**
00461  * \brief          Callback type: receive data from the network, with timeout
00462  *
00463  * \note           That callback must block until data is received, or the
00464  *                 timeout delay expires, or the operation is interrupted by a
00465  *                 signal.
00466  *
00467  * \param ctx      Context for the receive callback (typically a file descriptor)
00468  * \param buf      Buffer to write the received data to
00469  * \param len      Length of the receive buffer
00470  * \param timeout  Maximum nomber of millisecondes to wait for data
00471  *                 0 means no timeout (potentially waiting forever)
00472  *
00473  * \return         The callback must return the number of bytes received,
00474  *                 or a non-zero error code:
00475  *                 \c MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
00476  *                 \c MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
00477  *
00478  * \note           The callback may receive fewer bytes than the length of the
00479  *                 buffer. It must always return the number of bytes actually
00480  *                 received and written to the buffer.
00481  */
00482 typedef int mbedtls_ssl_recv_timeout_t( void *ctx,
00483                                         unsigned char *buf,
00484                                         size_t len,
00485                                         uint32_t timeout );
00486 /**
00487  * \brief          Callback type: set a pair of timers/delays to watch
00488  *
00489  * \param ctx      Context pointer
00490  * \param int_ms   Intermediate delay in milliseconds
00491  * \param fin_ms   Final delay in milliseconds
00492  *                 0 cancels the current timer.
00493  *
00494  * \note           This callback must at least store the necessary information
00495  *                 for the associated \c mbedtls_ssl_get_timer_t callback to
00496  *                 return correct information.
00497  *
00498  * \note           If using a event-driven style of programming, an event must
00499  *                 be generated when the final delay is passed. The event must
00500  *                 cause a call to \c mbedtls_ssl_handshake() with the proper
00501  *                 SSL context to be scheduled. Care must be taken to ensure
00502  *                 that at most one such call happens at a time.
00503  *
00504  * \note           Only one timer at a time must be running. Calling this
00505  *                 function while a timer is running must cancel it. Cancelled
00506  *                 timers must not generate any event.
00507  */
00508 typedef void mbedtls_ssl_set_timer_t( void * ctx,
00509                                       uint32_t int_ms,
00510                                       uint32_t fin_ms );
00511 
00512 /**
00513  * \brief          Callback type: get status of timers/delays
00514  *
00515  * \param ctx      Context pointer
00516  *
00517  * \return         This callback must return:
00518  *                 -1 if cancelled (fin_ms == 0),
00519  *                  0 if none of the delays have passed,
00520  *                  1 if only the intermediate delay has passed,
00521  *                  2 if the final delay has passed.
00522  */
00523 typedef int mbedtls_ssl_get_timer_t( void * ctx );
00524 
00525 
00526 /* Defined below */
00527 typedef struct mbedtls_ssl_session mbedtls_ssl_session;
00528 typedef struct mbedtls_ssl_context mbedtls_ssl_context;
00529 typedef struct mbedtls_ssl_config  mbedtls_ssl_config;
00530 
00531 /* Defined in ssl_internal.h */
00532 typedef struct mbedtls_ssl_transform mbedtls_ssl_transform;
00533 typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params;
00534 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00535 typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert;
00536 #endif
00537 #if defined(MBEDTLS_SSL_PROTO_DTLS)
00538 typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item;
00539 #endif
00540 
00541 /*
00542  * This structure is used for storing current session data.
00543  */
00544 struct mbedtls_ssl_session
00545 {
00546 #if defined(MBEDTLS_HAVE_TIME)
00547     mbedtls_time_t start;       /*!< starting time      */
00548 #endif
00549     int ciphersuite;            /*!< chosen ciphersuite */
00550     int compression;            /*!< chosen compression */
00551     size_t id_len;              /*!< session id length  */
00552     unsigned char id[32];       /*!< session identifier */
00553     unsigned char master[48];   /*!< the master secret  */
00554 
00555 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00556     mbedtls_x509_crt *peer_cert;        /*!< peer X.509 cert chain */
00557 #endif /* MBEDTLS_X509_CRT_PARSE_C */
00558     uint32_t verify_result;          /*!<  verification result     */
00559 
00560 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
00561     unsigned char *ticket;      /*!< RFC 5077 session ticket */
00562     size_t ticket_len;          /*!< session ticket length   */
00563     uint32_t ticket_lifetime;   /*!< ticket lifetime hint    */
00564 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
00565 
00566 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
00567     unsigned char mfl_code;     /*!< MaxFragmentLength negotiated by peer */
00568 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
00569 
00570 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
00571     int trunc_hmac;             /*!< flag for truncated hmac activation   */
00572 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
00573 
00574 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
00575     int encrypt_then_mac;       /*!< flag for EtM activation                */
00576 #endif
00577 };
00578 
00579 /**
00580  * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
00581  */
00582 struct mbedtls_ssl_config
00583 {
00584     /* Group items by size (largest first) to minimize padding overhead */
00585 
00586     /*
00587      * Pointers
00588      */
00589 
00590     const int *ciphersuite_list [4]; /*!< allowed ciphersuites per version   */
00591 
00592     /** Callback for printing debug output                                  */
00593     void (*f_dbg)(void *, int, const char *, int, const char *);
00594     void *p_dbg ;                    /*!< context for the debug function     */
00595 
00596     /** Callback for getting (pseudo-)random numbers                        */
00597     int  (*f_rng)(void *, unsigned char *, size_t);
00598     void *p_rng ;                    /*!< context for the RNG function       */
00599 
00600     /** Callback to retrieve a session from the cache                       */
00601     int (*f_get_cache)(void *, mbedtls_ssl_session *);
00602     /** Callback to store a session into the cache                          */
00603     int (*f_set_cache)(void *, const mbedtls_ssl_session *);
00604     void *p_cache ;                  /*!< context for cache callbacks        */
00605 
00606 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
00607     /** Callback for setting cert according to SNI extension                */
00608     int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
00609     void *p_sni ;                    /*!< context for SNI callback           */
00610 #endif
00611 
00612 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00613     /** Callback to customize X.509 certificate chain verification          */
00614     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
00615     void *p_vrfy ;                   /*!< context for X.509 verify calllback */
00616 #endif
00617 
00618 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
00619     /** Callback to retrieve PSK key from identity                          */
00620     int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
00621     void *p_psk ;                    /*!< context for PSK callback           */
00622 #endif
00623 
00624 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
00625     /** Callback to create & write a cookie for ClientHello veirifcation    */
00626     int (*f_cookie_write)( void *, unsigned char **, unsigned char *,
00627                            const unsigned char *, size_t );
00628     /** Callback to verify validity of a ClientHello cookie                 */
00629     int (*f_cookie_check)( void *, const unsigned char *, size_t,
00630                            const unsigned char *, size_t );
00631     void *p_cookie ;                 /*!< context for the cookie callbacks   */
00632 #endif
00633 
00634 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
00635     /** Callback to create & write a session ticket                         */
00636     int (*f_ticket_write)( void *, const mbedtls_ssl_session *,
00637             unsigned char *, const unsigned char *, size_t *, uint32_t * );
00638     /** Callback to parse a session ticket into a session structure         */
00639     int (*f_ticket_parse)( void *, mbedtls_ssl_session *, unsigned char *, size_t);
00640     void *p_ticket ;                 /*!< context for the ticket callbacks   */
00641 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
00642 
00643 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
00644     /** Callback to export key block and master secret                      */
00645     int (*f_export_keys)( void *, const unsigned char *,
00646             const unsigned char *, size_t, size_t, size_t );
00647     void *p_export_keys ;            /*!< context for key export callback    */
00648 #endif
00649 
00650 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00651     const mbedtls_x509_crt_profile *cert_profile ; /*!< verification profile */
00652     mbedtls_ssl_key_cert *key_cert ; /*!< own certificate/key pair(s)        */
00653     mbedtls_x509_crt *ca_chain ;     /*!< trusted CAs                        */
00654     mbedtls_x509_crl *ca_crl ;       /*!< trusted CAs CRLs                   */
00655 #endif /* MBEDTLS_X509_CRT_PARSE_C */
00656 
00657 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
00658     const int *sig_hashes ;          /*!< allowed signature hashes           */
00659 #endif
00660 
00661 #if defined(MBEDTLS_ECP_C)
00662     const mbedtls_ecp_group_id *curve_list ; /*!< allowed curves             */
00663 #endif
00664 
00665 #if defined(MBEDTLS_DHM_C)
00666     mbedtls_mpi dhm_P ;              /*!< prime modulus for DHM              */
00667     mbedtls_mpi dhm_G ;              /*!< generator for DHM                  */
00668 #endif
00669 
00670 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
00671     unsigned char *psk ;             /*!< pre-shared key                     */
00672     size_t         psk_len ;         /*!< length of the pre-shared key       */
00673     unsigned char *psk_identity ;    /*!< identity for PSK negotiation       */
00674     size_t         psk_identity_len ;/*!< length of identity                 */
00675 #endif
00676 
00677 #if defined(MBEDTLS_SSL_ALPN)
00678     const char **alpn_list ;         /*!< ordered list of protocols          */
00679 #endif
00680 
00681     /*
00682      * Numerical settings (int then char)
00683      */
00684 
00685     uint32_t read_timeout ;          /*!< timeout for mbedtls_ssl_read (ms)  */
00686 
00687 #if defined(MBEDTLS_SSL_PROTO_DTLS)
00688     uint32_t hs_timeout_min ;        /*!< initial value of the handshake
00689                                          retransmission timeout (ms)        */
00690     uint32_t hs_timeout_max ;        /*!< maximum value of the handshake
00691                                          retransmission timeout (ms)        */
00692 #endif
00693 
00694 #if defined(MBEDTLS_SSL_RENEGOTIATION)
00695     int renego_max_records ;         /*!< grace period for renegotiation     */
00696     unsigned char renego_period [8]; /*!< value of the record counters
00697                                          that triggers renegotiation        */
00698 #endif
00699 
00700 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
00701     unsigned int badmac_limit ;      /*!< limit of records with a bad MAC    */
00702 #endif
00703 
00704 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
00705     unsigned int dhm_min_bitlen ;    /*!< min. bit length of the DHM prime   */
00706 #endif
00707 
00708     unsigned char max_major_ver ;    /*!< max. major version used            */
00709     unsigned char max_minor_ver ;    /*!< max. minor version used            */
00710     unsigned char min_major_ver ;    /*!< min. major version used            */
00711     unsigned char min_minor_ver ;    /*!< min. minor version used            */
00712 
00713     /*
00714      * Flags (bitfields)
00715      */
00716 
00717     unsigned int endpoint  : 1;      /*!< 0: client, 1: server               */
00718     unsigned int transport  : 1;     /*!< stream (TLS) or datagram (DTLS)    */
00719     unsigned int authmode  : 2;      /*!< MBEDTLS_SSL_VERIFY_XXX             */
00720     /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE          */
00721     unsigned int allow_legacy_renegotiation  : 2 ; /*!< MBEDTLS_LEGACY_XXX   */
00722 #if defined(MBEDTLS_ARC4_C)
00723     unsigned int arc4_disabled  : 1; /*!< blacklist RC4 ciphersuites?        */
00724 #endif
00725 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
00726     unsigned int mfl_code  : 3;      /*!< desired fragment length            */
00727 #endif
00728 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
00729     unsigned int encrypt_then_mac  : 1 ; /*!< negotiate encrypt-then-mac?    */
00730 #endif
00731 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
00732     unsigned int extended_ms  : 1;   /*!< negotiate extended master secret?  */
00733 #endif
00734 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
00735     unsigned int anti_replay  : 1;   /*!< detect and prevent replay?         */
00736 #endif
00737 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
00738     unsigned int cbc_record_splitting  : 1;  /*!< do cbc record splitting    */
00739 #endif
00740 #if defined(MBEDTLS_SSL_RENEGOTIATION)
00741     unsigned int disable_renegotiation  : 1; /*!< disable renegotiation?     */
00742 #endif
00743 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
00744     unsigned int trunc_hmac  : 1;    /*!< negotiate truncated hmac?          */
00745 #endif
00746 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
00747     unsigned int session_tickets  : 1;   /*!< use session tickets?           */
00748 #endif
00749 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
00750     unsigned int fallback  : 1;      /*!< is this a fallback?                */
00751 #endif
00752 };
00753 
00754 
00755 struct mbedtls_ssl_context
00756 {
00757     const mbedtls_ssl_config *conf; /*!< configuration information          */
00758 
00759     /*
00760      * Miscellaneous
00761      */
00762     int state;                  /*!< SSL handshake: current state     */
00763 #if defined(MBEDTLS_SSL_RENEGOTIATION)
00764     int renego_status;          /*!< Initial, in progress, pending?   */
00765     int renego_records_seen;    /*!< Records since renego request, or with DTLS,
00766                                   number of retransmissions of request if
00767                                   renego_max_records is < 0           */
00768 #endif
00769 
00770     int major_ver;              /*!< equal to  MBEDTLS_SSL_MAJOR_VERSION_3    */
00771     int minor_ver;              /*!< either 0 (SSL3) or 1 (TLS1.0)    */
00772 
00773 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
00774     unsigned badmac_seen;       /*!< records with a bad MAC received    */
00775 #endif
00776 
00777     mbedtls_ssl_send_t *f_send; /*!< Callback for network send */
00778     mbedtls_ssl_recv_t *f_recv; /*!< Callback for network receive */
00779     mbedtls_ssl_recv_timeout_t *f_recv_timeout;
00780                                 /*!< Callback for network receive with timeout */
00781 
00782     void *p_bio;                /*!< context for I/O operations   */
00783 
00784     /*
00785      * Session layer
00786      */
00787     mbedtls_ssl_session *session_in;            /*!<  current session data (in)   */
00788     mbedtls_ssl_session *session_out;           /*!<  current session data (out)  */
00789     mbedtls_ssl_session *session;               /*!<  negotiated session data     */
00790     mbedtls_ssl_session *session_negotiate;     /*!<  session data in negotiation */
00791 
00792     mbedtls_ssl_handshake_params *handshake;    /*!<  params required only during
00793                                               the handshake process        */
00794 
00795     /*
00796      * Record layer transformations
00797      */
00798     mbedtls_ssl_transform *transform_in;        /*!<  current transform params (in)   */
00799     mbedtls_ssl_transform *transform_out;       /*!<  current transform params (in)   */
00800     mbedtls_ssl_transform *transform;           /*!<  negotiated transform params     */
00801     mbedtls_ssl_transform *transform_negotiate; /*!<  transform params in negotiation */
00802 
00803     /*
00804      * Timers
00805      */
00806     void *p_timer;              /*!< context for the timer callbacks */
00807 
00808     mbedtls_ssl_set_timer_t *f_set_timer;       /*!< set timer callback */
00809     mbedtls_ssl_get_timer_t *f_get_timer;       /*!< get timer callback */
00810 
00811     /*
00812      * Record layer (incoming data)
00813      */
00814     unsigned char *in_buf;      /*!< input buffer                     */
00815     unsigned char *in_ctr;      /*!< 64-bit incoming message counter
00816                                      TLS: maintained by us
00817                                      DTLS: read from peer             */
00818     unsigned char *in_hdr;      /*!< start of record header           */
00819     unsigned char *in_len;      /*!< two-bytes message length field   */
00820     unsigned char *in_iv;       /*!< ivlen-byte IV                    */
00821     unsigned char *in_msg;      /*!< message contents (in_iv+ivlen)   */
00822     unsigned char *in_offt;     /*!< read offset in application data  */
00823 
00824     int in_msgtype;             /*!< record header: message type      */
00825     size_t in_msglen;           /*!< record header: message length    */
00826     size_t in_left;             /*!< amount of data read so far       */
00827 #if defined(MBEDTLS_SSL_PROTO_DTLS)
00828     uint16_t in_epoch;          /*!< DTLS epoch for incoming records  */
00829     size_t next_record_offset;  /*!< offset of the next record in datagram
00830                                      (equal to in_left if none)       */
00831 #endif
00832 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
00833     uint64_t in_window_top;     /*!< last validated record seq_num    */
00834     uint64_t in_window;         /*!< bitmask for replay detection     */
00835 #endif
00836 
00837     size_t in_hslen;            /*!< current handshake message length,
00838                                      including the handshake header   */
00839     int nb_zero;                /*!< # of 0-length encrypted messages */
00840     int record_read;            /*!< record is already present        */
00841 
00842     /*
00843      * Record layer (outgoing data)
00844      */
00845     unsigned char *out_buf;     /*!< output buffer                    */
00846     unsigned char *out_ctr;     /*!< 64-bit outgoing message counter  */
00847     unsigned char *out_hdr;     /*!< start of record header           */
00848     unsigned char *out_len;     /*!< two-bytes message length field   */
00849     unsigned char *out_iv;      /*!< ivlen-byte IV                    */
00850     unsigned char *out_msg;     /*!< message contents (out_iv+ivlen)  */
00851 
00852     int out_msgtype;            /*!< record header: message type      */
00853     size_t out_msglen;          /*!< record header: message length    */
00854     size_t out_left;            /*!< amount of data not yet written   */
00855 
00856 #if defined(MBEDTLS_ZLIB_SUPPORT)
00857     unsigned char *compress_buf;        /*!<  zlib data buffer        */
00858 #endif
00859 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
00860     signed char split_done;     /*!< current record already splitted? */
00861 #endif
00862 
00863     /*
00864      * PKI layer
00865      */
00866     int client_auth;                    /*!<  flag for client auth.   */
00867 
00868     /*
00869      * User settings
00870      */
00871 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00872     char *hostname;             /*!< expected peer CN for verification
00873                                      (and SNI if available)                 */
00874 #endif
00875 
00876 #if defined(MBEDTLS_SSL_ALPN)
00877     const char *alpn_chosen;    /*!<  negotiated protocol                   */
00878 #endif
00879 
00880     /*
00881      * Information for DTLS hello verify
00882      */
00883 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
00884     unsigned char  *cli_id;         /*!<  transport-level ID of the client  */
00885     size_t          cli_id_len;     /*!<  length of cli_id                  */
00886 #endif
00887 
00888     /*
00889      * Secure renegotiation
00890      */
00891     /* needed to know when to send extension on server */
00892     int secure_renegotiation;           /*!<  does peer support legacy or
00893                                               secure renegotiation           */
00894 #if defined(MBEDTLS_SSL_RENEGOTIATION)
00895     size_t verify_data_len;             /*!<  length of verify data stored   */
00896     char own_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!<  previous handshake verify data */
00897     char peer_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!<  previous handshake verify data */
00898 #endif
00899 };
00900 
00901 #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
00902 
00903 #define MBEDTLS_SSL_CHANNEL_OUTBOUND    0
00904 #define MBEDTLS_SSL_CHANNEL_INBOUND     1
00905 
00906 extern int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
00907                 const unsigned char *key_enc, const unsigned char *key_dec,
00908                 size_t keylen,
00909                 const unsigned char *iv_enc,  const unsigned char *iv_dec,
00910                 size_t ivlen,
00911                 const unsigned char *mac_enc, const unsigned char *mac_dec,
00912                 size_t maclen);
00913 extern int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction);
00914 extern int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl);
00915 extern int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl);
00916 extern int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl);
00917 extern int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl);
00918 #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
00919 
00920 /**
00921  * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
00922  *
00923  * \return              a statically allocated array of ciphersuites, the last
00924  *                      entry is 0.
00925  */
00926 const int *mbedtls_ssl_list_ciphersuites( void );
00927 
00928 /**
00929  * \brief               Return the name of the ciphersuite associated with the
00930  *                      given ID
00931  *
00932  * \param ciphersuite_id SSL ciphersuite ID
00933  *
00934  * \return              a string containing the ciphersuite name
00935  */
00936 const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id );
00937 
00938 /**
00939  * \brief               Return the ID of the ciphersuite associated with the
00940  *                      given name
00941  *
00942  * \param ciphersuite_name SSL ciphersuite name
00943  *
00944  * \return              the ID with the ciphersuite or 0 if not found
00945  */
00946 int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name );
00947 
00948 /**
00949  * \brief          Initialize an SSL context
00950  *                 Just makes the context ready for mbedtls_ssl_setup() or
00951  *                 mbedtls_ssl_free()
00952  *
00953  * \param ssl      SSL context
00954  */
00955 void mbedtls_ssl_init( mbedtls_ssl_context *ssl );
00956 
00957 /**
00958  * \brief          Set up an SSL context for use
00959  *
00960  * \note           No copy of the configuration context is made, it can be
00961  *                 shared by many mbedtls_ssl_context structures.
00962  *
00963  * \warning        Modifying the conf structure after it has been used in this
00964  *                 function is unsupported!
00965  *
00966  * \param ssl      SSL context
00967  * \param conf     SSL configuration to use
00968  *
00969  * \return         0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
00970  *                 memory allocation failed
00971  */
00972 int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
00973                        const mbedtls_ssl_config *conf );
00974 
00975 /**
00976  * \brief          Reset an already initialized SSL context for re-use
00977  *                 while retaining application-set variables, function
00978  *                 pointers and data.
00979  *
00980  * \param ssl      SSL context
00981  * \return         0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED,
00982                    MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
00983  *                 MBEDTLS_ERR_SSL_COMPRESSION_FAILED
00984  */
00985 int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl );
00986 
00987 /**
00988  * \brief          Set the current endpoint type
00989  *
00990  * \param conf     SSL configuration
00991  * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
00992  */
00993 void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint );
00994 
00995 /**
00996  * \brief           Set the transport type (TLS or DTLS).
00997  *                  Default: TLS
00998  *
00999  * \note            For DTLS, you must either provide a recv callback that
01000  *                  doesn't block, or one that handles timeouts, see
01001  *                  \c mbedtls_ssl_set_bio(). You also need to provide timer
01002  *                  callbacks with \c mbedtls_ssl_set_timer_cb().
01003  *
01004  * \param conf      SSL configuration
01005  * \param transport transport type:
01006  *                  MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
01007  *                  MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
01008  */
01009 void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport );
01010 
01011 /**
01012  * \brief          Set the certificate verification mode
01013  *                 Default: NONE on server, REQUIRED on client
01014  *
01015  * \param conf     SSL configuration
01016  * \param authmode can be:
01017  *
01018  *  MBEDTLS_SSL_VERIFY_NONE:      peer certificate is not checked
01019  *                        (default on server)
01020  *                        (insecure on client)
01021  *
01022  *  MBEDTLS_SSL_VERIFY_OPTIONAL:  peer certificate is checked, however the
01023  *                        handshake continues even if verification failed;
01024  *                        mbedtls_ssl_get_verify_result() can be called after the
01025  *                        handshake is complete.
01026  *
01027  *  MBEDTLS_SSL_VERIFY_REQUIRED:  peer *must* present a valid certificate,
01028  *                        handshake is aborted if verification failed.
01029  *                        (default on client)
01030  *
01031  * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
01032  * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
01033  * the right time(s), which may not be obvious, while REQUIRED always perform
01034  * the verification as soon as possible. For example, REQUIRED was protecting
01035  * against the "triple handshake" attack even before it was found.
01036  */
01037 void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode );
01038 
01039 #if defined(MBEDTLS_X509_CRT_PARSE_C)
01040 /**
01041  * \brief          Set the verification callback (Optional).
01042  *
01043  *                 If set, the verify callback is called for each
01044  *                 certificate in the chain. For implementation
01045  *                 information, please see \c x509parse_verify()
01046  *
01047  * \param conf     SSL configuration
01048  * \param f_vrfy   verification function
01049  * \param p_vrfy   verification parameter
01050  */
01051 void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
01052                      int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
01053                      void *p_vrfy );
01054 #endif /* MBEDTLS_X509_CRT_PARSE_C */
01055 
01056 /**
01057  * \brief          Set the random number generator callback
01058  *
01059  * \param conf     SSL configuration
01060  * \param f_rng    RNG function
01061  * \param p_rng    RNG parameter
01062  */
01063 void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
01064                   int (*f_rng)(void *, unsigned char *, size_t),
01065                   void *p_rng );
01066 
01067 /**
01068  * \brief          Set the debug callback
01069  *
01070  *                 The callback has the following argument:
01071  *                 void *           opaque context for the callback
01072  *                 int              debug level
01073  *                 const char *     file name
01074  *                 int              line number
01075  *                 const char *     message
01076  *
01077  * \param conf     SSL configuration
01078  * \param f_dbg    debug function
01079  * \param p_dbg    debug parameter
01080  */
01081 void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
01082                   void (*f_dbg)(void *, int, const char *, int, const char *),
01083                   void  *p_dbg );
01084 
01085 /**
01086  * \brief          Set the underlying BIO callbacks for write, read and
01087  *                 read-with-timeout.
01088  *
01089  * \param ssl      SSL context
01090  * \param p_bio    parameter (context) shared by BIO callbacks
01091  * \param f_send   write callback
01092  * \param f_recv   read callback
01093  * \param f_recv_timeout blocking read callback with timeout.
01094  *
01095  * \note           One of f_recv or f_recv_timeout can be NULL, in which case
01096  *                 the other is used. If both are non-NULL, f_recv_timeout is
01097  *                 used and f_recv is ignored (as if it were NULL).
01098  *
01099  * \note           The two most common use cases are:
01100  *                 - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
01101  *                 - blocking I/O, f_recv == NULL, f_recv_timout != NULL
01102  *
01103  * \note           For DTLS, you need to provide either a non-NULL
01104  *                 f_recv_timeout callback, or a f_recv that doesn't block.
01105  *
01106  * \note           See the documentations of \c mbedtls_ssl_sent_t,
01107  *                 \c mbedtls_ssl_recv_t and \c mbedtls_ssl_recv_timeout_t for
01108  *                 the conventions those callbacks must follow.
01109  *
01110  * \note           On some platforms, net_sockets.c provides
01111  *                 \c mbedtls_net_send(), \c mbedtls_net_recv() and
01112  *                 \c mbedtls_net_recv_timeout() that are suitable to be used
01113  *                 here.
01114  */
01115 void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
01116                           void *p_bio,
01117                           mbedtls_ssl_send_t *f_send,
01118                           mbedtls_ssl_recv_t *f_recv,
01119                           mbedtls_ssl_recv_timeout_t *f_recv_timeout );
01120 
01121 /**
01122  * \brief          Set the timeout period for mbedtls_ssl_read()
01123  *                 (Default: no timeout.)
01124  *
01125  * \param conf     SSL configuration context
01126  * \param timeout  Timeout value in milliseconds.
01127  *                 Use 0 for no timeout (default).
01128  *
01129  * \note           With blocking I/O, this will only work if a non-NULL
01130  *                 \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
01131  *                 With non-blocking I/O, this will only work if timer
01132  *                 callbacks were set with \c mbedtls_ssl_set_timer_cb().
01133  *
01134  * \note           With non-blocking I/O, you may also skip this function
01135  *                 altogether and handle timeouts at the application layer.
01136  */
01137 void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout );
01138 
01139 /**
01140  * \brief          Set the timer callbacks (Mandatory for DTLS.)
01141  *
01142  * \param ssl      SSL context
01143  * \param p_timer  parameter (context) shared by timer callbacks
01144  * \param f_set_timer   set timer callback
01145  * \param f_get_timer   get timer callback. Must return:
01146  *
01147  * \note           See the documentation of \c mbedtls_ssl_set_timer_t and
01148  *                 \c mbedtls_ssl_get_timer_t for the conventions this pair of
01149  *                 callbacks must fallow.
01150  *
01151  * \note           On some platforms, timing.c provides
01152  *                 \c mbedtls_timing_set_delay() and
01153  *                 \c mbedtls_timing_get_delay() that are suitable for using
01154  *                 here, except if using an event-driven style.
01155  *
01156  * \note           See also the "DTLS tutorial" article in our knowledge base.
01157  *                 https://tls.mbed.org/kb/how-to/dtls-tutorial
01158  */
01159 void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
01160                                void *p_timer,
01161                                mbedtls_ssl_set_timer_t *f_set_timer,
01162                                mbedtls_ssl_get_timer_t *f_get_timer );
01163 
01164 /**
01165  * \brief           Callback type: generate and write session ticket
01166  *
01167  * \note            This describes what a callback implementation should do.
01168  *                  This callback should generate an encrypted and
01169  *                  authenticated ticket for the session and write it to the
01170  *                  output buffer. Here, ticket means the opaque ticket part
01171  *                  of the NewSessionTicket structure of RFC 5077.
01172  *
01173  * \param p_ticket  Context for the callback
01174  * \param session   SSL session to be written in the ticket
01175  * \param start     Start of the output buffer
01176  * \param end       End of the output buffer
01177  * \param tlen      On exit, holds the length written
01178  * \param lifetime  On exit, holds the lifetime of the ticket in seconds
01179  *
01180  * \return          0 if successful, or
01181  *                  a specific MBEDTLS_ERR_XXX code.
01182  */
01183 typedef int mbedtls_ssl_ticket_write_t( void *p_ticket,
01184                                         const mbedtls_ssl_session *session,
01185                                         unsigned char *start,
01186                                         const unsigned char *end,
01187                                         size_t *tlen,
01188                                         uint32_t *lifetime );
01189 
01190 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
01191 /**
01192  * \brief           Callback type: Export key block and master secret
01193  *
01194  * \note            This is required for certain uses of TLS, e.g. EAP-TLS
01195  *                  (RFC 5216) and Thread. The key pointers are ephemeral and
01196  *                  therefore must not be stored. The master secret and keys
01197  *                  should not be used directly except as an input to a key
01198  *                  derivation function.
01199  *
01200  * \param p_expkey  Context for the callback
01201  * \param ms        Pointer to master secret (fixed length: 48 bytes)
01202  * \param kb        Pointer to key block, see RFC 5246 section 6.3
01203  *                  (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
01204  * \param maclen    MAC length
01205  * \param keylen    Key length
01206  * \param ivlen     IV length
01207  *
01208  * \return          0 if successful, or
01209  *                  a specific MBEDTLS_ERR_XXX code.
01210  */
01211 typedef int mbedtls_ssl_export_keys_t( void *p_expkey,
01212                                 const unsigned char *ms,
01213                                 const unsigned char *kb,
01214                                 size_t maclen,
01215                                 size_t keylen,
01216                                 size_t ivlen );
01217 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
01218 
01219 /**
01220  * \brief           Callback type: parse and load session ticket
01221  *
01222  * \note            This describes what a callback implementation should do.
01223  *                  This callback should parse a session ticket as generated
01224  *                  by the corresponding mbedtls_ssl_ticket_write_t function,
01225  *                  and, if the ticket is authentic and valid, load the
01226  *                  session.
01227  *
01228  * \note            The implementation is allowed to modify the first len
01229  *                  bytes of the input buffer, eg to use it as a temporary
01230  *                  area for the decrypted ticket contents.
01231  *
01232  * \param p_ticket  Context for the callback
01233  * \param session   SSL session to be loaded
01234  * \param buf       Start of the buffer containing the ticket
01235  * \param len       Length of the ticket.
01236  *
01237  * \return          0 if successful, or
01238  *                  MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
01239  *                  MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
01240  *                  any other non-zero code for other failures.
01241  */
01242 typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket,
01243                                         mbedtls_ssl_session *session,
01244                                         unsigned char *buf,
01245                                         size_t len );
01246 
01247 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
01248 /**
01249  * \brief           Configure SSL session ticket callbacks (server only).
01250  *                  (Default: none.)
01251  *
01252  * \note            On server, session tickets are enabled by providing
01253  *                  non-NULL callbacks.
01254  *
01255  * \note            On client, use \c mbedtls_ssl_conf_session_tickets().
01256  *
01257  * \param conf      SSL configuration context
01258  * \param f_ticket_write    Callback for writing a ticket
01259  * \param f_ticket_parse    Callback for parsing a ticket
01260  * \param p_ticket          Context shared by the two callbacks
01261  */
01262 void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
01263         mbedtls_ssl_ticket_write_t *f_ticket_write,
01264         mbedtls_ssl_ticket_parse_t *f_ticket_parse,
01265         void *p_ticket );
01266 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
01267 
01268 #if defined(MBEDTLS_SSL_EXPORT_KEYS)
01269 /**
01270  * \brief           Configure key export callback.
01271  *                  (Default: none.)
01272  *
01273  * \note            See \c mbedtls_ssl_export_keys_t.
01274  *
01275  * \param conf      SSL configuration context
01276  * \param f_export_keys     Callback for exporting keys
01277  * \param p_export_keys     Context for the callback
01278  */
01279 void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
01280         mbedtls_ssl_export_keys_t *f_export_keys,
01281         void *p_export_keys );
01282 #endif /* MBEDTLS_SSL_EXPORT_KEYS */
01283 
01284 /**
01285  * \brief          Callback type: generate a cookie
01286  *
01287  * \param ctx      Context for the callback
01288  * \param p        Buffer to write to,
01289  *                 must be updated to point right after the cookie
01290  * \param end      Pointer to one past the end of the output buffer
01291  * \param info     Client ID info that was passed to
01292  *                 \c mbedtls_ssl_set_client_transport_id()
01293  * \param ilen     Length of info in bytes
01294  *
01295  * \return         The callback must return 0 on success,
01296  *                 or a negative error code.
01297  */
01298 typedef int mbedtls_ssl_cookie_write_t( void *ctx,
01299                                 unsigned char **p, unsigned char *end,
01300                                 const unsigned char *info, size_t ilen );
01301 
01302 /**
01303  * \brief          Callback type: verify a cookie
01304  *
01305  * \param ctx      Context for the callback
01306  * \param cookie   Cookie to verify
01307  * \param clen     Length of cookie
01308  * \param info     Client ID info that was passed to
01309  *                 \c mbedtls_ssl_set_client_transport_id()
01310  * \param ilen     Length of info in bytes
01311  *
01312  * \return         The callback must return 0 if cookie is valid,
01313  *                 or a negative error code.
01314  */
01315 typedef int mbedtls_ssl_cookie_check_t( void *ctx,
01316                                 const unsigned char *cookie, size_t clen,
01317                                 const unsigned char *info, size_t ilen );
01318 
01319 #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
01320 /**
01321  * \brief           Register callbacks for DTLS cookies
01322  *                  (Server only. DTLS only.)
01323  *
01324  *                  Default: dummy callbacks that fail, in order to force you to
01325  *                  register working callbacks (and initialize their context).
01326  *
01327  *                  To disable HelloVerifyRequest, register NULL callbacks.
01328  *
01329  * \warning         Disabling hello verification allows your server to be used
01330  *                  for amplification in DoS attacks against other hosts.
01331  *                  Only disable if you known this can't happen in your
01332  *                  particular environment.
01333  *
01334  * \note            See comments on \c mbedtls_ssl_handshake() about handling
01335  *                  the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
01336  *                  on the first handshake attempt when this is enabled.
01337  *
01338  * \note            This is also necessary to handle client reconnection from
01339  *                  the same port as described in RFC 6347 section 4.2.8 (only
01340  *                  the variant with cookies is supported currently). See
01341  *                  comments on \c mbedtls_ssl_read() for details.
01342  *
01343  * \param conf              SSL configuration
01344  * \param f_cookie_write    Cookie write callback
01345  * \param f_cookie_check    Cookie check callback
01346  * \param p_cookie          Context for both callbacks
01347  */
01348 void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
01349                            mbedtls_ssl_cookie_write_t *f_cookie_write,
01350                            mbedtls_ssl_cookie_check_t *f_cookie_check,
01351                            void *p_cookie );
01352 
01353 /**
01354  * \brief          Set client's transport-level identification info.
01355  *                 (Server only. DTLS only.)
01356  *
01357  *                 This is usually the IP address (and port), but could be
01358  *                 anything identify the client depending on the underlying
01359  *                 network stack. Used for HelloVerifyRequest with DTLS.
01360  *                 This is *not* used to route the actual packets.
01361  *
01362  * \param ssl      SSL context
01363  * \param info     Transport-level info identifying the client (eg IP + port)
01364  * \param ilen     Length of info in bytes
01365  *
01366  * \note           An internal copy is made, so the info buffer can be reused.
01367  *
01368  * \return         0 on success,
01369  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
01370  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
01371  */
01372 int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
01373                                  const unsigned char *info,
01374                                  size_t ilen );
01375 
01376 #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
01377 
01378 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
01379 /**
01380  * \brief          Enable or disable anti-replay protection for DTLS.
01381  *                 (DTLS only, no effect on TLS.)
01382  *                 Default: enabled.
01383  *
01384  * \param conf     SSL configuration
01385  * \param mode     MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
01386  *
01387  * \warning        Disabling this is a security risk unless the application
01388  *                 protocol handles duplicated packets in a safe way. You
01389  *                 should not disable this without careful consideration.
01390  *                 However, if your application already detects duplicated
01391  *                 packets and needs information about them to adjust its
01392  *                 transmission strategy, then you'll want to disable this.
01393  */
01394 void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode );
01395 #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
01396 
01397 #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
01398 /**
01399  * \brief          Set a limit on the number of records with a bad MAC
01400  *                 before terminating the connection.
01401  *                 (DTLS only, no effect on TLS.)
01402  *                 Default: 0 (disabled).
01403  *
01404  * \param conf     SSL configuration
01405  * \param limit    Limit, or 0 to disable.
01406  *
01407  * \note           If the limit is N, then the connection is terminated when
01408  *                 the Nth non-authentic record is seen.
01409  *
01410  * \note           Records with an invalid header are not counted, only the
01411  *                 ones going through the authentication-decryption phase.
01412  *
01413  * \note           This is a security trade-off related to the fact that it's
01414  *                 often relatively easy for an active attacker ot inject UDP
01415  *                 datagrams. On one hand, setting a low limit here makes it
01416  *                 easier for such an attacker to forcibly terminated a
01417  *                 connection. On the other hand, a high limit or no limit
01418  *                 might make us waste resources checking authentication on
01419  *                 many bogus packets.
01420  */
01421 void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit );
01422 #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
01423 
01424 #if defined(MBEDTLS_SSL_PROTO_DTLS)
01425 /**
01426  * \brief          Set retransmit timeout values for the DTLS handshake.
01427  *                 (DTLS only, no effect on TLS.)
01428  *
01429  * \param conf     SSL configuration
01430  * \param min      Initial timeout value in milliseconds.
01431  *                 Default: 1000 (1 second).
01432  * \param max      Maximum timeout value in milliseconds.
01433  *                 Default: 60000 (60 seconds).
01434  *
01435  * \note           Default values are from RFC 6347 section 4.2.4.1.
01436  *
01437  * \note           The 'min' value should typically be slightly above the
01438  *                 expected round-trip time to your peer, plus whatever time
01439  *                 it takes for the peer to process the message. For example,
01440  *                 if your RTT is about 600ms and you peer needs up to 1s to
01441  *                 do the cryptographic operations in the handshake, then you
01442  *                 should set 'min' slightly above 1600. Lower values of 'min'
01443  *                 might cause spurious resends which waste network resources,
01444  *                 while larger value of 'min' will increase overall latency
01445  *                 on unreliable network links.
01446  *
01447  * \note           The more unreliable your network connection is, the larger
01448  *                 your max / min ratio needs to be in order to achieve
01449  *                 reliable handshakes.
01450  *
01451  * \note           Messages are retransmitted up to log2(ceil(max/min)) times.
01452  *                 For example, if min = 1s and max = 5s, the retransmit plan
01453  *                 goes: send ... 1s -> resend ... 2s -> resend ... 4s ->
01454  *                 resend ... 5s -> give up and return a timeout error.
01455  */
01456 void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max );
01457 #endif /* MBEDTLS_SSL_PROTO_DTLS */
01458 
01459 #if defined(MBEDTLS_SSL_SRV_C)
01460 /**
01461  * \brief          Set the session cache callbacks (server-side only)
01462  *                 If not set, no session resuming is done (except if session
01463  *                 tickets are enabled too).
01464  *
01465  *                 The session cache has the responsibility to check for stale
01466  *                 entries based on timeout. See RFC 5246 for recommendations.
01467  *
01468  *                 Warning: session.peer_cert is cleared by the SSL/TLS layer on
01469  *                 connection shutdown, so do not cache the pointer! Either set
01470  *                 it to NULL or make a full copy of the certificate.
01471  *
01472  *                 The get callback is called once during the initial handshake
01473  *                 to enable session resuming. The get function has the
01474  *                 following parameters: (void *parameter, mbedtls_ssl_session *session)
01475  *                 If a valid entry is found, it should fill the master of
01476  *                 the session object with the cached values and return 0,
01477  *                 return 1 otherwise. Optionally peer_cert can be set as well
01478  *                 if it is properly present in cache entry.
01479  *
01480  *                 The set callback is called once during the initial handshake
01481  *                 to enable session resuming after the entire handshake has
01482  *                 been finished. The set function has the following parameters:
01483  *                 (void *parameter, const mbedtls_ssl_session *session). The function
01484  *                 should create a cache entry for future retrieval based on
01485  *                 the data in the session structure and should keep in mind
01486  *                 that the mbedtls_ssl_session object presented (and all its referenced
01487  *                 data) is cleared by the SSL/TLS layer when the connection is
01488  *                 terminated. It is recommended to add metadata to determine if
01489  *                 an entry is still valid in the future. Return 0 if
01490  *                 successfully cached, return 1 otherwise.
01491  *
01492  * \param conf           SSL configuration
01493  * \param p_cache        parmater (context) for both callbacks
01494  * \param f_get_cache    session get callback
01495  * \param f_set_cache    session set callback
01496  */
01497 void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
01498         void *p_cache,
01499         int (*f_get_cache)(void *, mbedtls_ssl_session *),
01500         int (*f_set_cache)(void *, const mbedtls_ssl_session *) );
01501 #endif /* MBEDTLS_SSL_SRV_C */
01502 
01503 #if defined(MBEDTLS_SSL_CLI_C)
01504 /**
01505  * \brief          Request resumption of session (client-side only)
01506  *                 Session data is copied from presented session structure.
01507  *
01508  * \param ssl      SSL context
01509  * \param session  session context
01510  *
01511  * \return         0 if successful,
01512  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
01513  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
01514  *                 arguments are otherwise invalid
01515  *
01516  * \sa             mbedtls_ssl_get_session()
01517  */
01518 int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session );
01519 #endif /* MBEDTLS_SSL_CLI_C */
01520 
01521 /**
01522  * \brief               Set the list of allowed ciphersuites and the preference
01523  *                      order. First in the list has the highest preference.
01524  *                      (Overrides all version-specific lists)
01525  *
01526  *                      The ciphersuites array is not copied, and must remain
01527  *                      valid for the lifetime of the ssl_config.
01528  *
01529  *                      Note: The server uses its own preferences
01530  *                      over the preference of the client unless
01531  *                      MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
01532  *
01533  * \param conf          SSL configuration
01534  * \param ciphersuites  0-terminated list of allowed ciphersuites
01535  */
01536 void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
01537                                    const int *ciphersuites );
01538 
01539 /**
01540  * \brief               Set the list of allowed ciphersuites and the
01541  *                      preference order for a specific version of the protocol.
01542  *                      (Only useful on the server side)
01543  *
01544  *                      The ciphersuites array is not copied, and must remain
01545  *                      valid for the lifetime of the ssl_config.
01546  *
01547  * \param conf          SSL configuration
01548  * \param ciphersuites  0-terminated list of allowed ciphersuites
01549  * \param major         Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
01550  *                      supported)
01551  * \param minor         Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
01552  *                      MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
01553  *                      MBEDTLS_SSL_MINOR_VERSION_3 supported)
01554  *
01555  * \note                With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
01556  *                      and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
01557  */
01558 void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
01559                                        const int *ciphersuites,
01560                                        int major, int minor );
01561 
01562 #if defined(MBEDTLS_X509_CRT_PARSE_C)
01563 /**
01564  * \brief          Set the X.509 security profile used for verification
01565  *
01566  * \note           The restrictions are enforced for all certificates in the
01567  *                 chain. However, signatures in the handshake are not covered
01568  *                 by this setting but by \b mbedtls_ssl_conf_sig_hashes().
01569  *
01570  * \param conf     SSL configuration
01571  * \param profile  Profile to use
01572  */
01573 void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
01574                                     const mbedtls_x509_crt_profile *profile );
01575 
01576 /**
01577  * \brief          Set the data required to verify peer certificate
01578  *
01579  * \param conf     SSL configuration
01580  * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
01581  * \param ca_crl   trusted CA CRLs
01582  */
01583 void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
01584                                mbedtls_x509_crt *ca_chain,
01585                                mbedtls_x509_crl *ca_crl );
01586 
01587 /**
01588  * \brief          Set own certificate chain and private key
01589  *
01590  * \note           own_cert should contain in order from the bottom up your
01591  *                 certificate chain. The top certificate (self-signed)
01592  *                 can be omitted.
01593  *
01594  * \note           On server, this function can be called multiple times to
01595  *                 provision more than one cert/key pair (eg one ECDSA, one
01596  *                 RSA with SHA-256, one RSA with SHA-1). An adequate
01597  *                 certificate will be selected according to the client's
01598  *                 advertised capabilities. In case mutliple certificates are
01599  *                 adequate, preference is given to the one set by the first
01600  *                 call to this function, then second, etc.
01601  *
01602  * \note           On client, only the first call has any effect. That is,
01603  *                 only one client certificate can be provisioned. The
01604  *                 server's preferences in its CertficateRequest message will
01605  *                 be ignored and our only cert will be sent regardless of
01606  *                 whether it matches those preferences - the server can then
01607  *                 decide what it wants to do with it.
01608  *
01609  * \param conf     SSL configuration
01610  * \param own_cert own public certificate chain
01611  * \param pk_key   own private key
01612  *
01613  * \return         0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
01614  */
01615 int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
01616                               mbedtls_x509_crt *own_cert,
01617                               mbedtls_pk_context *pk_key );
01618 #endif /* MBEDTLS_X509_CRT_PARSE_C */
01619 
01620 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
01621 /**
01622  * \brief          Set the Pre Shared Key (PSK) and the expected identity name
01623  *
01624  * \note           This is mainly useful for clients. Servers will usually
01625  *                 want to use \c mbedtls_ssl_conf_psk_cb() instead.
01626  *
01627  * \note           Currently clients can only register one pre-shared key.
01628  *                 In other words, the servers' identity hint is ignored.
01629  *                 Support for setting multiple PSKs on clients and selecting
01630  *                 one based on the identity hint is not a planned feature but
01631  *                 feedback is welcomed.
01632  *
01633  * \param conf     SSL configuration
01634  * \param psk      pointer to the pre-shared key
01635  * \param psk_len  pre-shared key length
01636  * \param psk_identity      pointer to the pre-shared key identity
01637  * \param psk_identity_len  identity key length
01638  *
01639  * \return         0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
01640  */
01641 int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
01642                 const unsigned char *psk, size_t psk_len,
01643                 const unsigned char *psk_identity, size_t psk_identity_len );
01644 
01645 
01646 /**
01647  * \brief          Set the Pre Shared Key (PSK) for the current handshake
01648  *
01649  * \note           This should only be called inside the PSK callback,
01650  *                 ie the function passed to \c mbedtls_ssl_conf_psk_cb().
01651  *
01652  * \param ssl      SSL context
01653  * \param psk      pointer to the pre-shared key
01654  * \param psk_len  pre-shared key length
01655  *
01656  * \return         0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
01657  */
01658 int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
01659                             const unsigned char *psk, size_t psk_len );
01660 
01661 /**
01662  * \brief          Set the PSK callback (server-side only).
01663  *
01664  *                 If set, the PSK callback is called for each
01665  *                 handshake where a PSK ciphersuite was negotiated.
01666  *                 The caller provides the identity received and wants to
01667  *                 receive the actual PSK data and length.
01668  *
01669  *                 The callback has the following parameters: (void *parameter,
01670  *                 mbedtls_ssl_context *ssl, const unsigned char *psk_identity,
01671  *                 size_t identity_len)
01672  *                 If a valid PSK identity is found, the callback should use
01673  *                 \c mbedtls_ssl_set_hs_psk() on the ssl context to set the
01674  *                 correct PSK and return 0.
01675  *                 Any other return value will result in a denied PSK identity.
01676  *
01677  * \note           If you set a PSK callback using this function, then you
01678  *                 don't need to set a PSK key and identity using
01679  *                 \c mbedtls_ssl_conf_psk().
01680  *
01681  * \param conf     SSL configuration
01682  * \param f_psk    PSK identity function
01683  * \param p_psk    PSK identity parameter
01684  */
01685 void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
01686                      int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
01687                                   size_t),
01688                      void *p_psk );
01689 #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
01690 
01691 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
01692 /**
01693  * \brief          Set the Diffie-Hellman public P and G values,
01694  *                 read as hexadecimal strings (server-side only)
01695  *                 (Default: MBEDTLS_DHM_RFC5114_MODP_2048_[PG])
01696  *
01697  * \param conf     SSL configuration
01698  * \param dhm_P    Diffie-Hellman-Merkle modulus
01699  * \param dhm_G    Diffie-Hellman-Merkle generator
01700  *
01701  * \return         0 if successful
01702  */
01703 int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G );
01704 
01705 /**
01706  * \brief          Set the Diffie-Hellman public P and G values,
01707  *                 read from existing context (server-side only)
01708  *
01709  * \param conf     SSL configuration
01710  * \param dhm_ctx  Diffie-Hellman-Merkle context
01711  *
01712  * \return         0 if successful
01713  */
01714 int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx );
01715 #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
01716 
01717 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
01718 /**
01719  * \brief          Set the minimum length for Diffie-Hellman parameters.
01720  *                 (Client-side only.)
01721  *                 (Default: 1024 bits.)
01722  *
01723  * \param conf     SSL configuration
01724  * \param bitlen   Minimum bit length of the DHM prime
01725  */
01726 void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
01727                                       unsigned int bitlen );
01728 #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
01729 
01730 #if defined(MBEDTLS_ECP_C)
01731 /**
01732  * \brief          Set the allowed curves in order of preference.
01733  *                 (Default: all defined curves.)
01734  *
01735  *                 On server: this only affects selection of the ECDHE curve;
01736  *                 the curves used for ECDH and ECDSA are determined by the
01737  *                 list of available certificates instead.
01738  *
01739  *                 On client: this affects the list of curves offered for any
01740  *                 use. The server can override our preference order.
01741  *
01742  *                 Both sides: limits the set of curves accepted for use in
01743  *                 ECDHE and in the peer's end-entity certificate.
01744  *
01745  * \note           This has no influence on which curves are allowed inside the
01746  *                 certificate chains, see \c mbedtls_ssl_conf_cert_profile()
01747  *                 for that. For the end-entity certificate however, the key
01748  *                 will be accepted only if it is allowed both by this list
01749  *                 and by the cert profile.
01750  *
01751  * \note           This list should be ordered by decreasing preference
01752  *                 (preferred curve first).
01753  *
01754  * \param conf     SSL configuration
01755  * \param curves   Ordered list of allowed curves,
01756  *                 terminated by MBEDTLS_ECP_DP_NONE.
01757  */
01758 void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
01759                               const mbedtls_ecp_group_id *curves );
01760 #endif /* MBEDTLS_ECP_C */
01761 
01762 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
01763 /**
01764  * \brief          Set the allowed hashes for signatures during the handshake.
01765  *                 (Default: all available hashes except MD5.)
01766  *
01767  * \note           This only affects which hashes are offered and can be used
01768  *                 for signatures during the handshake. Hashes for message
01769  *                 authentication and the TLS PRF are controlled by the
01770  *                 ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
01771  *                 used for certificate signature are controlled by the
01772  *                 verification profile, see \c mbedtls_ssl_conf_cert_profile().
01773  *
01774  * \note           This list should be ordered by decreasing preference
01775  *                 (preferred hash first).
01776  *
01777  * \param conf     SSL configuration
01778  * \param hashes   Ordered list of allowed signature hashes,
01779  *                 terminated by \c MBEDTLS_MD_NONE.
01780  */
01781 void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
01782                                   const int *hashes );
01783 #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
01784 
01785 #if defined(MBEDTLS_X509_CRT_PARSE_C)
01786 /**
01787  * \brief          Set the hostname to check against the received server
01788  *                 certificate. It sets the ServerName TLS extension too,
01789  *                 if the extension is enabled.
01790  *                 (client-side only)
01791  *
01792  * \param ssl      SSL context
01793  * \param hostname the server hostname
01794  *
01795  * \return         0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
01796  */
01797 int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname );
01798 #endif /* MBEDTLS_X509_CRT_PARSE_C */
01799 
01800 #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
01801 /**
01802  * \brief          Set own certificate and key for the current handshake
01803  *
01804  * \note           Same as \c mbedtls_ssl_conf_own_cert() but for use within
01805  *                 the SNI callback.
01806  *
01807  * \param ssl      SSL context
01808  * \param own_cert own public certificate chain
01809  * \param pk_key   own private key
01810  *
01811  * \return         0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
01812  */
01813 int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
01814                                  mbedtls_x509_crt *own_cert,
01815                                  mbedtls_pk_context *pk_key );
01816 
01817 /**
01818  * \brief          Set the data required to verify peer certificate for the
01819  *                 current handshake
01820  *
01821  * \note           Same as \c mbedtls_ssl_conf_ca_chain() but for use within
01822  *                 the SNI callback.
01823  *
01824  * \param ssl      SSL context
01825  * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
01826  * \param ca_crl   trusted CA CRLs
01827  */
01828 void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
01829                                   mbedtls_x509_crt *ca_chain,
01830                                   mbedtls_x509_crl *ca_crl );
01831 
01832 /**
01833  * \brief          Set authmode for the current handshake.
01834  *
01835  * \note           Same as \c mbedtls_ssl_conf_authmode() but for use within
01836  *                 the SNI callback.
01837  *
01838  * \param ssl      SSL context
01839  * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
01840  *                 MBEDTLS_SSL_VERIFY_REQUIRED
01841  */
01842 void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
01843                                   int authmode );
01844 
01845 /**
01846  * \brief          Set server side ServerName TLS extension callback
01847  *                 (optional, server-side only).
01848  *
01849  *                 If set, the ServerName callback is called whenever the
01850  *                 server receives a ServerName TLS extension from the client
01851  *                 during a handshake. The ServerName callback has the
01852  *                 following parameters: (void *parameter, mbedtls_ssl_context *ssl,
01853  *                 const unsigned char *hostname, size_t len). If a suitable
01854  *                 certificate is found, the callback must set the
01855  *                 certificate(s) and key(s) to use with \c
01856  *                 mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
01857  *                 and may optionally adjust the CA and associated CRL with \c
01858  *                 mbedtls_ssl_set_hs_ca_chain() as well as the client
01859  *                 authentication mode with \c mbedtls_ssl_set_hs_authmode(),
01860  *                 then must return 0. If no matching name is found, the
01861  *                 callback must either set a default cert, or
01862  *                 return non-zero to abort the handshake at this point.
01863  *
01864  * \param conf     SSL configuration
01865  * \param f_sni    verification function
01866  * \param p_sni    verification parameter
01867  */
01868 void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
01869                   int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *,
01870                                size_t),
01871                   void *p_sni );
01872 #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
01873 
01874 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
01875 /**
01876  * \brief          Set the EC J-PAKE password for current handshake.
01877  *
01878  * \note           An internal copy is made, and destroyed as soon as the
01879  *                 handshake is completed, or when the SSL context is reset or
01880  *                 freed.
01881  *
01882  * \note           The SSL context needs to be already set up. The right place
01883  *                 to call this function is between \c mbedtls_ssl_setup() or
01884  *                 \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
01885  *
01886  * \param ssl      SSL context
01887  * \param pw       EC J-PAKE password (pre-shared secret)
01888  * \param pw_len   length of pw in bytes
01889  *
01890  * \return         0 on success, or a negative error code.
01891  */
01892 int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
01893                                          const unsigned char *pw,
01894                                          size_t pw_len );
01895 #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
01896 
01897 #if defined(MBEDTLS_SSL_ALPN)
01898 /**
01899  * \brief          Set the supported Application Layer Protocols.
01900  *
01901  * \param conf     SSL configuration
01902  * \param protos   Pointer to a NULL-terminated list of supported protocols,
01903  *                 in decreasing preference order. The pointer to the list is
01904  *                 recorded by the library for later reference as required, so
01905  *                 the lifetime of the table must be atleast as long as the
01906  *                 lifetime of the SSL configuration structure.
01907  *
01908  * \return         0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
01909  */
01910 int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos );
01911 
01912 /**
01913  * \brief          Get the name of the negotiated Application Layer Protocol.
01914  *                 This function should be called after the handshake is
01915  *                 completed.
01916  *
01917  * \param ssl      SSL context
01918  *
01919  * \return         Protcol name, or NULL if no protocol was negotiated.
01920  */
01921 const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl );
01922 #endif /* MBEDTLS_SSL_ALPN */
01923 
01924 /**
01925  * \brief          Set the maximum supported version sent from the client side
01926  *                 and/or accepted at the server side
01927  *                 (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
01928  *
01929  * \note           This ignores ciphersuites from higher versions.
01930  *
01931  * \note           With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
01932  *                 MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
01933  *
01934  * \param conf     SSL configuration
01935  * \param major    Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
01936  * \param minor    Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
01937  *                 MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
01938  *                 MBEDTLS_SSL_MINOR_VERSION_3 supported)
01939  */
01940 void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor );
01941 
01942 /**
01943  * \brief          Set the minimum accepted SSL/TLS protocol version
01944  *                 (Default: TLS 1.0)
01945  *
01946  * \note           Input outside of the SSL_MAX_XXXXX_VERSION and
01947  *                 SSL_MIN_XXXXX_VERSION range is ignored.
01948  *
01949  * \note           MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
01950  *
01951  * \note           With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
01952  *                 MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
01953  *
01954  * \param conf     SSL configuration
01955  * \param major    Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
01956  * \param minor    Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
01957  *                 MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
01958  *                 MBEDTLS_SSL_MINOR_VERSION_3 supported)
01959  */
01960 void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor );
01961 
01962 #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
01963 /**
01964  * \brief          Set the fallback flag (client-side only).
01965  *                 (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
01966  *
01967  * \note           Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
01968  *                 connection, that is a connection with max_version set to a
01969  *                 lower value than the value you're willing to use. Such
01970  *                 fallback connections are not recommended but are sometimes
01971  *                 necessary to interoperate with buggy (version-intolerant)
01972  *                 servers.
01973  *
01974  * \warning        You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
01975  *                 non-fallback connections! This would appear to work for a
01976  *                 while, then cause failures when the server is upgraded to
01977  *                 support a newer TLS version.
01978  *
01979  * \param conf     SSL configuration
01980  * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
01981  */
01982 void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback );
01983 #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
01984 
01985 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
01986 /**
01987  * \brief           Enable or disable Encrypt-then-MAC
01988  *                  (Default: MBEDTLS_SSL_ETM_ENABLED)
01989  *
01990  * \note            This should always be enabled, it is a security
01991  *                  improvement, and should not cause any interoperability
01992  *                  issue (used only if the peer supports it too).
01993  *
01994  * \param conf      SSL configuration
01995  * \param etm       MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
01996  */
01997 void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm );
01998 #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
01999 
02000 #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
02001 /**
02002  * \brief           Enable or disable Extended Master Secret negotiation.
02003  *                  (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
02004  *
02005  * \note            This should always be enabled, it is a security fix to the
02006  *                  protocol, and should not cause any interoperability issue
02007  *                  (used only if the peer supports it too).
02008  *
02009  * \param conf      SSL configuration
02010  * \param ems       MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
02011  */
02012 void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems );
02013 #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
02014 
02015 #if defined(MBEDTLS_ARC4_C)
02016 /**
02017  * \brief          Disable or enable support for RC4
02018  *                 (Default: MBEDTLS_SSL_ARC4_DISABLED)
02019  *
02020  * \warning        Use of RC4 in DTLS/TLS has been prohibited by RFC 7465
02021  *                 for security reasons. Use at your own risk.
02022  *
02023  * \note           This function is deprecated and will likely be removed in
02024  *                 a future version of the library.
02025  *                 RC4 is disabled by default at compile time and needs to be
02026  *                 actively enabled for use with legacy systems.
02027  *
02028  * \param conf     SSL configuration
02029  * \param arc4     MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
02030  */
02031 void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 );
02032 #endif /* MBEDTLS_ARC4_C */
02033 
02034 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
02035 /**
02036  * \brief          Set the maximum fragment length to emit and/or negotiate
02037  *                 (Default: MBEDTLS_SSL_MAX_CONTENT_LEN, usually 2^14 bytes)
02038  *                 (Server: set maximum fragment length to emit,
02039  *                 usually negotiated by the client during handshake
02040  *                 (Client: set maximum fragment length to emit *and*
02041  *                 negotiate with the server during handshake)
02042  *
02043  * \param conf     SSL configuration
02044  * \param mfl_code Code for maximum fragment length (allowed values:
02045  *                 MBEDTLS_SSL_MAX_FRAG_LEN_512,  MBEDTLS_SSL_MAX_FRAG_LEN_1024,
02046  *                 MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
02047  *
02048  * \return         0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
02049  */
02050 int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code );
02051 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
02052 
02053 #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
02054 /**
02055  * \brief          Activate negotiation of truncated HMAC
02056  *                 (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
02057  *
02058  * \param conf     SSL configuration
02059  * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
02060  *                                    MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
02061  */
02062 void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate );
02063 #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
02064 
02065 #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
02066 /**
02067  * \brief          Enable / Disable 1/n-1 record splitting
02068  *                 (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
02069  *
02070  * \note           Only affects SSLv3 and TLS 1.0, not higher versions.
02071  *                 Does not affect non-CBC ciphersuites in any version.
02072  *
02073  * \param conf     SSL configuration
02074  * \param split    MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
02075  *                 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
02076  */
02077 void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split );
02078 #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
02079 
02080 #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
02081 /**
02082  * \brief          Enable / Disable session tickets (client only).
02083  *                 (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
02084  *
02085  * \note           On server, use \c mbedtls_ssl_conf_session_tickets_cb().
02086  *
02087  * \param conf     SSL configuration
02088  * \param use_tickets   Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
02089  *                                         MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
02090  */
02091 void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets );
02092 #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
02093 
02094 #if defined(MBEDTLS_SSL_RENEGOTIATION)
02095 /**
02096  * \brief          Enable / Disable renegotiation support for connection when
02097  *                 initiated by peer
02098  *                 (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
02099  *
02100  * \warning        It is recommended to always disable renegotation unless you
02101  *                 know you need it and you know what you're doing. In the
02102  *                 past, there have been several issues associated with
02103  *                 renegotiation or a poor understanding of its properties.
02104  *
02105  * \note           Server-side, enabling renegotiation also makes the server
02106  *                 susceptible to a resource DoS by a malicious client.
02107  *
02108  * \param conf    SSL configuration
02109  * \param renegotiation     Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
02110  *                                             MBEDTLS_SSL_RENEGOTIATION_DISABLED)
02111  */
02112 void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation );
02113 #endif /* MBEDTLS_SSL_RENEGOTIATION */
02114 
02115 /**
02116  * \brief          Prevent or allow legacy renegotiation.
02117  *                 (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
02118  *
02119  *                 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
02120  *                 be established even if the peer does not support
02121  *                 secure renegotiation, but does not allow renegotiation
02122  *                 to take place if not secure.
02123  *                 (Interoperable and secure option)
02124  *
02125  *                 MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
02126  *                 with non-upgraded peers. Allowing legacy renegotiation
02127  *                 makes the connection vulnerable to specific man in the
02128  *                 middle attacks. (See RFC 5746)
02129  *                 (Most interoperable and least secure option)
02130  *
02131  *                 MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
02132  *                 if peer does not support secure renegotiation. Results
02133  *                 in interoperability issues with non-upgraded peers
02134  *                 that do not support renegotiation altogether.
02135  *                 (Most secure option, interoperability issues)
02136  *
02137  * \param conf     SSL configuration
02138  * \param allow_legacy  Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
02139  *                                        SSL_ALLOW_LEGACY_RENEGOTIATION or
02140  *                                        MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
02141  */
02142 void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy );
02143 
02144 #if defined(MBEDTLS_SSL_RENEGOTIATION)
02145 /**
02146  * \brief          Enforce renegotiation requests.
02147  *                 (Default: enforced, max_records = 16)
02148  *
02149  *                 When we request a renegotiation, the peer can comply or
02150  *                 ignore the request. This function allows us to decide
02151  *                 whether to enforce our renegotiation requests by closing
02152  *                 the connection if the peer doesn't comply.
02153  *
02154  *                 However, records could already be in transit from the peer
02155  *                 when the request is emitted. In order to increase
02156  *                 reliability, we can accept a number of records before the
02157  *                 expected handshake records.
02158  *
02159  *                 The optimal value is highly dependent on the specific usage
02160  *                 scenario.
02161  *
02162  * \note           With DTLS and server-initiated renegotiation, the
02163  *                 HelloRequest is retransmited every time mbedtls_ssl_read() times
02164  *                 out or receives Application Data, until:
02165  *                 - max_records records have beens seen, if it is >= 0, or
02166  *                 - the number of retransmits that would happen during an
02167  *                 actual handshake has been reached.
02168  *                 Please remember the request might be lost a few times
02169  *                 if you consider setting max_records to a really low value.
02170  *
02171  * \warning        On client, the grace period can only happen during
02172  *                 mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
02173  *                 which always behave as if max_record was 0. The reason is,
02174  *                 if we receive application data from the server, we need a
02175  *                 place to write it, which only happens during mbedtls_ssl_read().
02176  *
02177  * \param conf     SSL configuration
02178  * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
02179  *                 enforce renegotiation, or a non-negative value to enforce
02180  *                 it but allow for a grace period of max_records records.
02181  */
02182 void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records );
02183 
02184 /**
02185  * \brief          Set record counter threshold for periodic renegotiation.
02186  *                 (Default: 2^64 - 256.)
02187  *
02188  *                 Renegotiation is automatically triggered when a record
02189  *                 counter (outgoing or ingoing) crosses the defined
02190  *                 threshold. The default value is meant to prevent the
02191  *                 connection from being closed when the counter is about to
02192  *                 reached its maximal value (it is not allowed to wrap).
02193  *
02194  *                 Lower values can be used to enforce policies such as "keys
02195  *                 must be refreshed every N packets with cipher X".
02196  *
02197  * \param conf     SSL configuration
02198  * \param period   The threshold value: a big-endian 64-bit number.
02199  *                 Set to 2^64 - 1 to disable periodic renegotiation
02200  */
02201 void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
02202                                    const unsigned char period[8] );
02203 #endif /* MBEDTLS_SSL_RENEGOTIATION */
02204 
02205 /**
02206  * \brief          Return the number of data bytes available to read
02207  *
02208  * \param ssl      SSL context
02209  *
02210  * \return         how many bytes are available in the read buffer
02211  */
02212 size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl );
02213 
02214 /**
02215  * \brief          Return the result of the certificate verification
02216  *
02217  * \param ssl      SSL context
02218  *
02219  * \return         0 if successful,
02220  *                 -1 if result is not available (eg because the handshake was
02221  *                 aborted too early), or
02222  *                 a combination of BADCERT_xxx and BADCRL_xxx flags, see
02223  *                 x509.h
02224  */
02225 uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl );
02226 
02227 /**
02228  * \brief          Return the name of the current ciphersuite
02229  *
02230  * \param ssl      SSL context
02231  *
02232  * \return         a string containing the ciphersuite name
02233  */
02234 const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl );
02235 
02236 /**
02237  * \brief          Return the current SSL version (SSLv3/TLSv1/etc)
02238  *
02239  * \param ssl      SSL context
02240  *
02241  * \return         a string containing the SSL version
02242  */
02243 const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl );
02244 
02245 /**
02246  * \brief          Return the (maximum) number of bytes added by the record
02247  *                 layer: header + encryption/MAC overhead (inc. padding)
02248  *
02249  * \param ssl      SSL context
02250  *
02251  * \return         Current maximum record expansion in bytes, or
02252  *                 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
02253  *                 enabled, which makes expansion much less predictable
02254  */
02255 int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl );
02256 
02257 #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
02258 /**
02259  * \brief          Return the maximum fragment length (payload, in bytes).
02260  *                 This is the value negotiated with peer if any,
02261  *                 or the locally configured value.
02262  *
02263  * \note           With DTLS, \c mbedtls_ssl_write() will return an error if
02264  *                 called with a larger length value.
02265  *                 With TLS, \c mbedtls_ssl_write() will fragment the input if
02266  *                 necessary and return the number of bytes written; it is up
02267  *                 to the caller to call \c mbedtls_ssl_write() again in
02268  *                 order to send the remaining bytes if any.
02269  *
02270  * \param ssl      SSL context
02271  *
02272  * \return         Current maximum fragment length.
02273  */
02274 size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl );
02275 #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
02276 
02277 #if defined(MBEDTLS_X509_CRT_PARSE_C)
02278 /**
02279  * \brief          Return the peer certificate from the current connection
02280  *
02281  *                 Note: Can be NULL in case no certificate was sent during
02282  *                 the handshake. Different calls for the same connection can
02283  *                 return the same or different pointers for the same
02284  *                 certificate and even a different certificate altogether.
02285  *                 The peer cert CAN change in a single connection if
02286  *                 renegotiation is performed.
02287  *
02288  * \param ssl      SSL context
02289  *
02290  * \return         the current peer certificate
02291  */
02292 const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl );
02293 #endif /* MBEDTLS_X509_CRT_PARSE_C */
02294 
02295 #if defined(MBEDTLS_SSL_CLI_C)
02296 /**
02297  * \brief          Save session in order to resume it later (client-side only)
02298  *                 Session data is copied to presented session structure.
02299  *
02300  * \warning        Currently, peer certificate is lost in the operation.
02301  *
02302  * \param ssl      SSL context
02303  * \param session  session context
02304  *
02305  * \return         0 if successful,
02306  *                 MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
02307  *                 MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
02308  *                 arguments are otherwise invalid
02309  *
02310  * \sa             mbedtls_ssl_set_session()
02311  */
02312 int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *session );
02313 #endif /* MBEDTLS_SSL_CLI_C */
02314 
02315 /**
02316  * \brief          Perform the SSL handshake
02317  *
02318  * \param ssl      SSL context
02319  *
02320  * \return         0 if successful, or
02321  *                 MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
02322  *                 MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED (see below), or
02323  *                 a specific SSL error code.
02324  *
02325  * \note           If this function returns something other than 0 or
02326  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
02327  *                 becomes unusable, and you should either free it or call
02328  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02329  *                 a new connection; the current connection must be closed.
02330  *
02331  * \note           If DTLS is in use, then you may choose to handle
02332  *                 MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
02333  *                 purposes, as it is an expected return value rather than an
02334  *                 actual error, but you still need to reset/free the context.
02335  */
02336 int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl );
02337 
02338 /**
02339  * \brief          Perform a single step of the SSL handshake
02340  *
02341  * \note           The state of the context (ssl->state) will be at
02342  *                 the next state after execution of this function. Do not
02343  *                 call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
02344  *
02345  * \note           If this function returns something other than 0 or
02346  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
02347  *                 becomes unusable, and you should either free it or call
02348  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02349  *                 a new connection; the current connection must be closed.
02350  *
02351  * \param ssl      SSL context
02352  *
02353  * \return         0 if successful, or
02354  *                 MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
02355  *                 a specific SSL error code.
02356  */
02357 int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl );
02358 
02359 #if defined(MBEDTLS_SSL_RENEGOTIATION)
02360 /**
02361  * \brief          Initiate an SSL renegotiation on the running connection.
02362  *                 Client: perform the renegotiation right now.
02363  *                 Server: request renegotiation, which will be performed
02364  *                 during the next call to mbedtls_ssl_read() if honored by
02365  *                 client.
02366  *
02367  * \param ssl      SSL context
02368  *
02369  * \return         0 if successful, or any mbedtls_ssl_handshake() return
02370  *                 value.
02371  *
02372  * \note           If this function returns something other than 0 or
02373  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
02374  *                 becomes unusable, and you should either free it or call
02375  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02376  *                 a new connection; the current connection must be closed.
02377  */
02378 int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl );
02379 #endif /* MBEDTLS_SSL_RENEGOTIATION */
02380 
02381 /**
02382  * \brief          Read at most 'len' application data bytes
02383  *
02384  * \param ssl      SSL context
02385  * \param buf      buffer that will hold the data
02386  * \param len      maximum number of bytes to read
02387  *
02388  * \return         the number of bytes read, or
02389  *                 0 for EOF, or
02390  *                 MBEDTLS_ERR_SSL_WANT_READ or MBEDTLS_ERR_SSL_WANT_WRITE, or
02391  *                 MBEDTLS_ERR_SSL_CLIENT_RECONNECT (see below), or
02392  *                 another negative error code.
02393  *
02394  * \note           If this function returns something other than a positive
02395  *                 value or MBEDTLS_ERR_SSL_WANT_READ/WRITE or
02396  *                 MBEDTLS_ERR_SSL_CLIENT_RECONNECT, then the ssl context
02397  *                 becomes unusable, and you should either free it or call
02398  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02399  *                 a new connection; the current connection must be closed.
02400  *
02401  * \note           When this function return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
02402  *                 (which can only happen server-side), it means that a client
02403  *                 is initiating a new connection using the same source port.
02404  *                 You can either treat that as a connection close and wait
02405  *                 for the client to resend a ClientHello, or directly
02406  *                 continue with \c mbedtls_ssl_handshake() with the same
02407  *                 context (as it has beeen reset internally). Either way, you
02408  *                 should make sure this is seen by the application as a new
02409  *                 connection: application state, if any, should be reset, and
02410  *                 most importantly the identity of the client must be checked
02411  *                 again. WARNING: not validating the identity of the client
02412  *                 again, or not transmitting the new identity to the
02413  *                 application layer, would allow authentication bypass!
02414  */
02415 int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len );
02416 
02417 /**
02418  * \brief          Try to write exactly 'len' application data bytes
02419  *
02420  * \warning        This function will do partial writes in some cases. If the
02421  *                 return value is non-negative but less than length, the
02422  *                 function must be called again with updated arguments:
02423  *                 buf + ret, len - ret (if ret is the return value) until
02424  *                 it returns a value equal to the last 'len' argument.
02425  *
02426  * \param ssl      SSL context
02427  * \param buf      buffer holding the data
02428  * \param len      how many bytes must be written
02429  *
02430  * \return         the number of bytes actually written (may be less than len),
02431  *                 or MBEDTLS_ERR_SSL_WANT_WRITE of MBEDTLS_ERR_SSL_WANT_READ,
02432  *                 or another negative error code.
02433  *
02434  * \note           If this function returns something other than a positive
02435  *                 value or MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context
02436  *                 becomes unusable, and you should either free it or call
02437  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02438  *                 a new connection; the current connection must be closed.
02439  *
02440  * \note           When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ,
02441  *                 it must be called later with the *same* arguments,
02442  *                 until it returns a positive value.
02443  *
02444  * \note           If the requested length is greater than the maximum
02445  *                 fragment length (either the built-in limit or the one set
02446  *                 or negotiated with the peer), then:
02447  *                 - with TLS, less bytes than requested are written.
02448  *                 - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
02449  *                 \c mbedtls_ssl_get_max_frag_len() may be used to query the
02450  *                 active maximum fragment length.
02451  */
02452 int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len );
02453 
02454 /**
02455  * \brief           Send an alert message
02456  *
02457  * \param ssl       SSL context
02458  * \param level     The alert level of the message
02459  *                  (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
02460  * \param message   The alert message (SSL_ALERT_MSG_*)
02461  *
02462  * \return          0 if successful, or a specific SSL error code.
02463  *
02464  * \note           If this function returns something other than 0 or
02465  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
02466  *                 becomes unusable, and you should either free it or call
02467  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02468  *                 a new connection; the current connection must be closed.
02469  */
02470 int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
02471                             unsigned char level,
02472                             unsigned char message );
02473 /**
02474  * \brief          Notify the peer that the connection is being closed
02475  *
02476  * \param ssl      SSL context
02477  *
02478  * \return          0 if successful, or a specific SSL error code.
02479  *
02480  * \note           If this function returns something other than 0 or
02481  *                 MBEDTLS_ERR_SSL_WANT_READ/WRITE, then the ssl context
02482  *                 becomes unusable, and you should either free it or call
02483  *                 \c mbedtls_ssl_session_reset() on it before re-using it for
02484  *                 a new connection; the current connection must be closed.
02485  */
02486 int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl );
02487 
02488 /**
02489  * \brief          Free referenced items in an SSL context and clear memory
02490  *
02491  * \param ssl      SSL context
02492  */
02493 void mbedtls_ssl_free( mbedtls_ssl_context *ssl );
02494 
02495 /**
02496  * \brief          Initialize an SSL configuration context
02497  *                 Just makes the context ready for
02498  *                 mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
02499  *
02500  * \note           You need to call mbedtls_ssl_config_defaults() unless you
02501  *                 manually set all of the relevent fields yourself.
02502  *
02503  * \param conf     SSL configuration context
02504  */
02505 void mbedtls_ssl_config_init( mbedtls_ssl_config *conf );
02506 
02507 /**
02508  * \brief          Load reasonnable default SSL configuration values.
02509  *                 (You need to call mbedtls_ssl_config_init() first.)
02510  *
02511  * \param conf     SSL configuration context
02512  * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
02513  * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
02514  *                  MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
02515  * \param preset   a MBEDTLS_SSL_PRESET_XXX value
02516  *
02517  * \note           See \c mbedtls_ssl_conf_transport() for notes on DTLS.
02518  *
02519  * \return         0 if successful, or
02520  *                 MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
02521  */
02522 int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
02523                                  int endpoint, int transport, int preset );
02524 
02525 /**
02526  * \brief          Free an SSL configuration context
02527  *
02528  * \param conf     SSL configuration context
02529  */
02530 void mbedtls_ssl_config_free( mbedtls_ssl_config *conf );
02531 
02532 /**
02533  * \brief          Initialize SSL session structure
02534  *
02535  * \param session  SSL session
02536  */
02537 void mbedtls_ssl_session_init( mbedtls_ssl_session *session );
02538 
02539 /**
02540  * \brief          Free referenced items in an SSL session including the
02541  *                 peer certificate and clear memory
02542  *
02543  * \param session  SSL session
02544  */
02545 void mbedtls_ssl_session_free( mbedtls_ssl_session *session );
02546 
02547 #ifdef __cplusplus
02548 }
02549 #endif
02550 
02551 #endif /* ssl.h */