Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

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