Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * \file ssl_ticket.h |
| 3 | * |
| 4 | * \brief Internal functions shared by the SSL modules |
| 5 | * |
| 6 | * Copyright (C) 2015, ARM Limited, All Rights Reserved |
| 7 | * |
| 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | #ifndef MBEDTLS_SSL_INTERNAL_H |
| 25 | #define MBEDTLS_SSL_INTERNAL_H |
| 26 | |
| 27 | #include "ssl.h" |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ); |
| 34 | int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); |
| 35 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); |
| 36 | |
| 37 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ); |
| 38 | |
| 39 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ); |
| 40 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ); |
| 41 | |
| 42 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl ); |
| 43 | int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ); |
| 44 | |
| 45 | int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl ); |
| 46 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ); |
| 47 | |
| 48 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ); |
| 49 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ); |
| 50 | |
| 51 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ); |
| 52 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ); |
| 53 | |
| 54 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ); |
| 55 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ); |
| 56 | |
| 57 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 58 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ); |
| 59 | |
| 60 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 61 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ); |
| 62 | #endif |
| 63 | |
| 64 | #if defined(MBEDTLS_PK_C) |
| 65 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ); |
| 66 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ); |
| 67 | #endif |
| 68 | |
| 69 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ); |
| 70 | |
| 71 | #if defined(MBEDTLS_SSL_SET_CURVES) |
| 72 | int mbedtls_ssl_curve_is_acceptable( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ); |
| 73 | #endif |
| 74 | |
| 75 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 76 | static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl ) |
| 77 | { |
| 78 | mbedtls_ssl_key_cert *key_cert; |
| 79 | |
| 80 | if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL ) |
| 81 | key_cert = ssl->handshake->key_cert; |
| 82 | else |
| 83 | key_cert = ssl->conf->key_cert; |
| 84 | |
| 85 | return( key_cert == NULL ? NULL : key_cert->key ); |
| 86 | } |
| 87 | |
| 88 | static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl ) |
| 89 | { |
| 90 | mbedtls_ssl_key_cert *key_cert; |
| 91 | |
| 92 | if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL ) |
| 93 | key_cert = ssl->handshake->key_cert; |
| 94 | else |
| 95 | key_cert = ssl->conf->key_cert; |
| 96 | |
| 97 | return( key_cert == NULL ? NULL : key_cert->cert ); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Check usage of a certificate wrt extensions: |
| 102 | * keyUsage, extendedKeyUsage (later), and nSCertType (later). |
| 103 | * |
| 104 | * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we |
| 105 | * check a cert we received from them)! |
| 106 | * |
| 107 | * Return 0 if everything is OK, -1 if not. |
| 108 | */ |
| 109 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 110 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
| 111 | int cert_endpoint, |
| 112 | uint32_t *flags ); |
| 113 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 114 | |
| 115 | void mbedtls_ssl_write_version( int major, int minor, int transport, |
| 116 | unsigned char ver[2] ); |
| 117 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, |
| 118 | const unsigned char ver[2] ); |
| 119 | |
| 120 | static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl ) |
| 121 | { |
| 122 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 123 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 124 | return( 13 ); |
| 125 | #else |
| 126 | ((void) ssl); |
| 127 | #endif |
| 128 | return( 5 ); |
| 129 | } |
| 130 | |
| 131 | static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl ) |
| 132 | { |
| 133 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 134 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 135 | return( 12 ); |
| 136 | #else |
| 137 | ((void) ssl); |
| 138 | #endif |
| 139 | return( 4 ); |
| 140 | } |
| 141 | |
| 142 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 143 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ); |
| 144 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ); |
| 145 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ); |
| 146 | #endif |
| 147 | |
| 148 | /* Visible for testing purposes only */ |
| 149 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 150 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ); |
| 151 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ); |
| 152 | #endif |
| 153 | |
| 154 | /* constant-time buffer comparison */ |
| 155 | static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n ) |
| 156 | { |
| 157 | size_t i; |
| 158 | const unsigned char *A = (const unsigned char *) a; |
| 159 | const unsigned char *B = (const unsigned char *) b; |
| 160 | unsigned char diff = 0; |
| 161 | |
| 162 | for( i = 0; i < n; i++ ) |
| 163 | diff |= A[i] ^ B[i]; |
| 164 | |
| 165 | return( diff ); |
| 166 | } |
| 167 | |
| 168 | #ifdef __cplusplus |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | #endif /* ssl_internal.h */ |