Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
tls1.h
00001 /* 00002 * Copyright (c) 2007, Cameron Rich 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, 00010 * this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * * Neither the name of the axTLS project nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 /** 00032 * @file tls1.h 00033 * 00034 * @brief The definitions for the TLS library. 00035 */ 00036 #ifndef HEADER_SSL_LIB_H 00037 #define HEADER_SSL_LIB_H 00038 00039 #ifdef __cplusplus 00040 extern "C" { 00041 #endif 00042 00043 #include "version.h" 00044 #include "os_int.h" 00045 #include "crypto.h " 00046 #include "crypto_misc.h " 00047 00048 #include "config.h" 00049 00050 #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */ 00051 #define SSL_PROTOCOL_MINOR_VERSION 0x02 /* TLS v1.1 */ 00052 #define SSL_PROTOCOL_VERSION_MAX 0x32 /* TLS v1.1 */ 00053 #define SSL_PROTOCOL_VERSION1_1 0x32 /* TLS v1.1 */ 00054 #define SSL_RANDOM_SIZE 32 00055 #define SSL_SECRET_SIZE 48 00056 #define SSL_FINISHED_HASH_SIZE 12 00057 #define SSL_RECORD_SIZE 5 00058 #define SSL_SERVER_READ 0 00059 #define SSL_SERVER_WRITE 1 00060 #define SSL_CLIENT_READ 2 00061 #define SSL_CLIENT_WRITE 3 00062 #define SSL_HS_HDR_SIZE 4 00063 00064 /* the flags we use while establishing a connection */ 00065 #define SSL_NEED_RECORD 0x0001 00066 #define SSL_TX_ENCRYPTED 0x0002 00067 #define SSL_RX_ENCRYPTED 0x0004 00068 #define SSL_SESSION_RESUME 0x0008 00069 #define SSL_IS_CLIENT 0x0010 00070 #define SSL_HAS_CERT_REQ 0x0020 00071 #define SSL_SENT_CLOSE_NOTIFY 0x0040 00072 00073 /* some macros to muck around with flag bits */ 00074 #define SET_SSL_FLAG(A) (ssl->flag |= A) 00075 #define CLR_SSL_FLAG(A) (ssl->flag &= ~A) 00076 #define IS_SET_SSL_FLAG(A) (ssl->flag & A) 00077 00078 #define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */ 00079 #define RT_MAX_PLAIN_LENGTH 2048//16384 00080 #define RT_EXTRA 512//1024 00081 #define BM_RECORD_OFFSET 5 00082 00083 #ifdef CONFIG_SSL_SKELETON_MODE 00084 #define NUM_PROTOCOLS 1 00085 #else 00086 #define NUM_PROTOCOLS 4 00087 #endif 00088 00089 #define PARANOIA_CHECK(A, B) if (A < B) { \ 00090 ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; } 00091 00092 /* protocol types */ 00093 enum 00094 { 00095 PT_CHANGE_CIPHER_SPEC = 20, 00096 PT_ALERT_PROTOCOL, 00097 PT_HANDSHAKE_PROTOCOL, 00098 PT_APP_PROTOCOL_DATA 00099 }; 00100 00101 /* handshaking types */ 00102 enum 00103 { 00104 HS_HELLO_REQUEST, 00105 HS_CLIENT_HELLO, 00106 HS_SERVER_HELLO, 00107 HS_CERTIFICATE = 11, 00108 HS_SERVER_KEY_XCHG, 00109 HS_CERT_REQ, 00110 HS_SERVER_HELLO_DONE, 00111 HS_CERT_VERIFY, 00112 HS_CLIENT_KEY_XCHG, 00113 HS_FINISHED = 20 00114 }; 00115 00116 typedef struct 00117 { 00118 uint8_t cipher; 00119 uint8_t key_size; 00120 uint8_t iv_size; 00121 uint8_t key_block_size; 00122 uint8_t padding_size; 00123 uint8_t digest_size; 00124 hmac_func hmac; 00125 crypt_func encrypt; 00126 crypt_func decrypt; 00127 } cipher_info_t; 00128 00129 struct _SSLObjLoader 00130 { 00131 uint8_t *buf; 00132 int len; 00133 }; 00134 00135 typedef struct _SSLObjLoader SSLObjLoader; 00136 00137 typedef struct 00138 { 00139 time_t conn_time; 00140 uint8_t session_id[SSL_SESSION_ID_SIZE]; 00141 uint8_t master_secret[SSL_SECRET_SIZE]; 00142 } SSL_SESSION; 00143 00144 typedef struct 00145 { 00146 uint8_t *buf; 00147 int size; 00148 } SSL_CERT; 00149 00150 typedef struct 00151 { 00152 MD5_CTX md5_ctx; 00153 SHA1_CTX sha1_ctx; 00154 uint8_t final_finish_mac[SSL_FINISHED_HASH_SIZE]; 00155 uint8_t *key_block; 00156 uint8_t master_secret[SSL_SECRET_SIZE]; 00157 uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */ 00158 uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */ 00159 uint16_t bm_proc_index; 00160 } DISPOSABLE_CTX; 00161 00162 struct _SSL 00163 { 00164 uint32_t flag; 00165 uint16_t need_bytes; 00166 uint16_t got_bytes; 00167 uint8_t record_type; 00168 uint8_t cipher; 00169 uint8_t sess_id_size; 00170 uint8_t version; 00171 uint8_t client_version; 00172 int16_t next_state; 00173 int16_t hs_status; 00174 DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */ 00175 int client_fd; 00176 const cipher_info_t *cipher_info; 00177 void *encrypt_ctx; 00178 void *decrypt_ctx; 00179 uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA]; 00180 uint8_t *bm_data; 00181 uint16_t bm_index; 00182 uint16_t bm_read_index; 00183 struct _SSL *next; /* doubly linked list */ 00184 struct _SSL *prev; 00185 struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */ 00186 #ifndef CONFIG_SSL_SKELETON_MODE 00187 uint16_t session_index; 00188 SSL_SESSION *session; 00189 #endif 00190 #ifdef CONFIG_SSL_CERT_VERIFICATION 00191 X509_CTX *x509_ctx; 00192 #endif 00193 00194 uint8_t session_id[SSL_SESSION_ID_SIZE]; 00195 uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */ 00196 uint8_t server_mac[SHA1_SIZE]; /* for HMAC verification */ 00197 uint8_t read_sequence[8]; /* 64 bit sequence number */ 00198 uint8_t write_sequence[8]; /* 64 bit sequence number */ 00199 uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */ 00200 }; 00201 00202 typedef struct _SSL SSL; 00203 00204 struct _SSL_CTX 00205 { 00206 uint32_t options; 00207 uint8_t chain_length; 00208 RSA_CTX *rsa_ctx; 00209 #ifdef CONFIG_SSL_CERT_VERIFICATION 00210 CA_CERT_CTX *ca_cert_ctx; 00211 #endif 00212 SSL *head; 00213 SSL *tail; 00214 SSL_CERT certs[CONFIG_SSL_MAX_CERTS]; 00215 #ifndef CONFIG_SSL_SKELETON_MODE 00216 uint16_t num_sessions; 00217 SSL_SESSION **ssl_sessions; 00218 #endif 00219 #ifdef CONFIG_SSL_CTX_MUTEXING 00220 SSL_CTX_MUTEX_TYPE mutex; 00221 #endif 00222 #ifdef CONFIG_OPENSSL_COMPATIBLE 00223 void *bonus_attr; 00224 #endif 00225 }; 00226 00227 typedef struct _SSL_CTX SSL_CTX; 00228 00229 /* backwards compatibility */ 00230 typedef struct _SSL_CTX SSLCTX; 00231 00232 extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS]; 00233 00234 SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd); 00235 void disposable_new(SSL *ssl); 00236 void disposable_free(SSL *ssl); 00237 int send_packet(SSL *ssl, uint8_t protocol, 00238 const uint8_t *in, int length); 00239 int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len); 00240 int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len); 00241 int process_finished(SSL *ssl, uint8_t *buf, int hs_len); 00242 int process_sslv23_client_hello(SSL *ssl); 00243 int send_alert(SSL *ssl, int error_code); 00244 int send_finished(SSL *ssl); 00245 int send_certificate(SSL *ssl); 00246 int basic_read(SSL *ssl, uint8_t **in_data); 00247 int send_change_cipher_spec(SSL *ssl); 00248 void finished_digest(SSL *ssl, const char *label, uint8_t *digest); 00249 void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret); 00250 void add_packet(SSL *ssl, const uint8_t *pkt, int len); 00251 int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len); 00252 int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj); 00253 void ssl_obj_free(SSLObjLoader *ssl_obj); 00254 int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password); 00255 int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password); 00256 int load_key_certs(SSL_CTX *ssl_ctx); 00257 #ifdef CONFIG_SSL_CERT_VERIFICATION 00258 int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len); 00259 void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx); 00260 #endif 00261 #ifdef CONFIG_SSL_ENABLE_CLIENT 00262 int do_client_connect(SSL *ssl); 00263 #endif 00264 00265 #ifdef CONFIG_SSL_FULL_MODE 00266 void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok); 00267 void DISPLAY_BYTES(SSL *ssl, const char *format, 00268 const uint8_t *data, int size, ...); 00269 void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx); 00270 void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx); 00271 void DISPLAY_ALERT(SSL *ssl, int alert); 00272 #else 00273 #define DISPLAY_STATE(A,B,C,D) 00274 #define DISPLAY_CERT(A,B) 00275 #define DISPLAY_RSA(A,B) 00276 #define DISPLAY_ALERT(A, B) 00277 #ifdef WIN32 00278 void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */ 00279 const uint8_t *data, int size, ...); 00280 #else 00281 #define DISPLAY_BYTES(A,B,C,D,...) 00282 #endif 00283 #endif 00284 00285 #ifdef CONFIG_SSL_CERT_VERIFICATION 00286 int process_certificate(SSL *ssl, X509_CTX **x509_ctx); 00287 #endif 00288 00289 SSL_SESSION *ssl_session_update(int max_sessions, 00290 SSL_SESSION *ssl_sessions[], SSL *ssl, 00291 const uint8_t *session_id); 00292 void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl); 00293 00294 #ifdef __cplusplus 00295 } 00296 #endif 00297 00298 #endif
Generated on Tue Jul 12 2022 18:48:01 by
1.7.2