27 #ifndef POLARSSL_SSL_H
28 #define POLARSSL_SSL_H
41 #if defined(POLARSSL_DHM_C)
45 #if defined(POLARSSL_ZLIB_SUPPORT)
49 #if defined(_MSC_VER) && !defined(inline)
50 #define inline _inline
52 #if defined(__ARMCC_VERSION) && !defined(inline)
53 #define inline __inline
60 #define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080
61 #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100
62 #define POLARSSL_ERR_SSL_INVALID_MAC -0x7180
63 #define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200
64 #define POLARSSL_ERR_SSL_CONN_EOF -0x7280
65 #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300
66 #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380
67 #define POLARSSL_ERR_SSL_NO_RNG -0x7400
68 #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480
69 #define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500
70 #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580
71 #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600
72 #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680
73 #define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700
74 #define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780
75 #define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800
76 #define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880
77 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900
78 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980
79 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00
80 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80
81 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00
82 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80
83 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00
84 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP -0x7C80
85 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS -0x7D00
86 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80
87 #define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00
88 #define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80
89 #define POLARSSL_ERR_SSL_MALLOC_FAILED -0x7F00
90 #define POLARSSL_ERR_SSL_HW_ACCEL_FAILED -0x7F80
91 #define POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80
92 #define POLARSSL_ERR_SSL_COMPRESSION_FAILED -0x6F00
93 #define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80
94 #define POLARSSL_ERR_SSL_COUNTER_WRAPPING -0x6B80
99 #define SSL_MAJOR_VERSION_3 3
100 #define SSL_MINOR_VERSION_0 0
101 #define SSL_MINOR_VERSION_1 1
102 #define SSL_MINOR_VERSION_2 2
103 #define SSL_MINOR_VERSION_3 3
105 #define SSL_IS_CLIENT 0
106 #define SSL_IS_SERVER 1
107 #define SSL_COMPRESS_NULL 0
108 #define SSL_COMPRESS_DEFLATE 1
110 #define SSL_VERIFY_NONE 0
111 #define SSL_VERIFY_OPTIONAL 1
112 #define SSL_VERIFY_REQUIRED 2
114 #define SSL_INITIAL_HANDSHAKE 0
115 #define SSL_RENEGOTIATION 1
117 #define SSL_LEGACY_RENEGOTIATION 0
118 #define SSL_SECURE_RENEGOTIATION 1
120 #define SSL_RENEGOTIATION_DISABLED 0
121 #define SSL_RENEGOTIATION_ENABLED 1
123 #define SSL_LEGACY_NO_RENEGOTIATION 0
124 #define SSL_LEGACY_ALLOW_RENEGOTIATION 1
125 #define SSL_LEGACY_BREAK_HANDSHAKE 2
134 #if !defined(POLARSSL_CONFIG_OPTIONS)
135 #define SSL_MAX_CONTENT_LEN 16384
144 #if defined(POLARSSL_ZLIB_SUPPORT)
145 #define SSL_COMPRESSION_ADD 1024
147 #define SSL_COMPRESSION_ADD 0
150 #define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + SSL_COMPRESSION_ADD + 512)
155 #define TLS_RSA_WITH_NULL_MD5 0x01
156 #define TLS_RSA_WITH_NULL_SHA 0x02
157 #define TLS_RSA_WITH_NULL_SHA256 0x3B
158 #define TLS_RSA_WITH_DES_CBC_SHA 0x09
159 #define TLS_DHE_RSA_WITH_DES_CBC_SHA 0x15
161 #define TLS_RSA_WITH_RC4_128_MD5 0x04
162 #define TLS_RSA_WITH_RC4_128_SHA 0x05
164 #define TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x0A
165 #define TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x16
167 #define TLS_RSA_WITH_AES_128_CBC_SHA 0x2F
168 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x33
169 #define TLS_RSA_WITH_AES_256_CBC_SHA 0x35
170 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x39
171 #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x3C
172 #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x3D
173 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x67
174 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x6B
176 #define TLS_RSA_WITH_CAMELLIA_128_CBC_SHA 0x41
177 #define TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 0x45
178 #define TLS_RSA_WITH_CAMELLIA_256_CBC_SHA 0x84
179 #define TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 0x88
180 #define TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBA
181 #define TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBE
182 #define TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC0
183 #define TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC4
185 #define TLS_RSA_WITH_AES_128_GCM_SHA256 0x9C
186 #define TLS_RSA_WITH_AES_256_GCM_SHA384 0x9D
187 #define TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x9E
188 #define TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x9F
190 #define SSL_EMPTY_RENEGOTIATION_INFO 0xFF
195 #define SSL_HASH_NONE 0
196 #define SSL_HASH_MD5 1
197 #define SSL_HASH_SHA1 2
198 #define SSL_HASH_SHA224 3
199 #define SSL_HASH_SHA256 4
200 #define SSL_HASH_SHA384 5
201 #define SSL_HASH_SHA512 6
203 #define SSL_SIG_RSA 1
208 #define SSL_CERT_TYPE_RSA_SIGN 1
213 #define SSL_MSG_CHANGE_CIPHER_SPEC 20
214 #define SSL_MSG_ALERT 21
215 #define SSL_MSG_HANDSHAKE 22
216 #define SSL_MSG_APPLICATION_DATA 23
218 #define SSL_ALERT_LEVEL_WARNING 1
219 #define SSL_ALERT_LEVEL_FATAL 2
221 #define SSL_ALERT_MSG_CLOSE_NOTIFY 0
222 #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
223 #define SSL_ALERT_MSG_BAD_RECORD_MAC 20
224 #define SSL_ALERT_MSG_DECRYPTION_FAILED 21
225 #define SSL_ALERT_MSG_RECORD_OVERFLOW 22
226 #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
227 #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
228 #define SSL_ALERT_MSG_NO_CERT 41
229 #define SSL_ALERT_MSG_BAD_CERT 42
230 #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
231 #define SSL_ALERT_MSG_CERT_REVOKED 44
232 #define SSL_ALERT_MSG_CERT_EXPIRED 45
233 #define SSL_ALERT_MSG_CERT_UNKNOWN 46
234 #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
235 #define SSL_ALERT_MSG_UNKNOWN_CA 48
236 #define SSL_ALERT_MSG_ACCESS_DENIED 49
237 #define SSL_ALERT_MSG_DECODE_ERROR 50
238 #define SSL_ALERT_MSG_DECRYPT_ERROR 51
239 #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
240 #define SSL_ALERT_MSG_PROTOCOL_VERSION 70
241 #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
242 #define SSL_ALERT_MSG_INTERNAL_ERROR 80
243 #define SSL_ALERT_MSG_USER_CANCELED 90
244 #define SSL_ALERT_MSG_NO_RENEGOTIATION 100
245 #define SSL_ALERT_MSG_UNSUPPORTED_EXT 110
246 #define SSL_ALERT_MSG_UNRECOGNIZED_NAME 112
248 #define SSL_HS_HELLO_REQUEST 0
249 #define SSL_HS_CLIENT_HELLO 1
250 #define SSL_HS_SERVER_HELLO 2
251 #define SSL_HS_CERTIFICATE 11
252 #define SSL_HS_SERVER_KEY_EXCHANGE 12
253 #define SSL_HS_CERTIFICATE_REQUEST 13
254 #define SSL_HS_SERVER_HELLO_DONE 14
255 #define SSL_HS_CERTIFICATE_VERIFY 15
256 #define SSL_HS_CLIENT_KEY_EXCHANGE 16
257 #define SSL_HS_FINISHED 20
262 #define TLS_EXT_SERVERNAME 0
263 #define TLS_EXT_SERVERNAME_HOSTNAME 0
265 #define TLS_EXT_SIG_ALG 13
267 #define TLS_EXT_RENEGOTIATION_INFO 0xFF01
275 int (*f_rng)(
void *,
unsigned char *, size_t),
276 void *p_rng,
int mode,
size_t *olen,
277 const unsigned char *input,
unsigned char *output,
278 size_t output_max_len );
280 int (*f_rng)(
void *,
unsigned char *, size_t),
void *p_rng,
281 int mode,
int hash_id,
unsigned int hashlen,
282 const unsigned char *hash,
unsigned char *sig );
324 unsigned char id[32];
356 #if defined(POLARSSL_ZLIB_SUPPORT)
357 z_stream ctx_deflate;
358 z_stream ctx_inflate;
373 #if defined(POLARSSL_DHM_C)
388 int (*
tls_prf)(
unsigned char *, size_t,
char *,
389 unsigned char *, size_t,
390 unsigned char *, size_t);
420 int (*
f_rng)(
void *,
unsigned char *, size_t);
421 void (*
f_dbg)(
void *, int,
const char *);
422 int (*
f_recv)(
void *,
unsigned char *, size_t);
423 int (*
f_send)(
void *,
const unsigned char *, size_t);
508 #if defined(POLARSSL_DHM_C)
535 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
537 const unsigned char *key_enc,
const unsigned char *key_dec,
538 const unsigned char *iv_enc,
const unsigned char *iv_dec,
539 const unsigned char *mac_enc,
const unsigned char *mac_dec);
540 extern int (*ssl_hw_record_reset)(
ssl_context *ssl);
541 extern int (*ssl_hw_record_write)(
ssl_context *ssl);
542 extern int (*ssl_hw_record_read)(
ssl_context *ssl);
543 extern int (*ssl_hw_record_finish)(
ssl_context *ssl);
644 int (*f_vrfy)(
void *,
x509_cert *,
int,
int *),
655 int (*f_rng)(
void *,
unsigned char *,
size_t),
666 void (*f_dbg)(
void *,
int,
const char *),
679 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
680 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send );
720 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
721 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache );
762 const int *ciphersuites,
763 int major,
int minor );
774 x509_crl *ca_crl,
const char *peer_cn );
815 #if defined(POLARSSL_DHM_C)
873 int (*f_sni)(
void *,
ssl_context *,
const unsigned char *,
1073 unsigned char level,
1074 unsigned char message );
1150 const unsigned char *A = (
const unsigned char *) a;
1151 const unsigned char *B = (
const unsigned char *) b;
1152 unsigned char diff = 0;
1154 for( i = 0; i < n; i++ )
1155 diff |= A[i] ^ B[i];
int ssl_send_alert_message(ssl_context *ssl, unsigned char level, unsigned char message)
Send an alert message.
void(* f_dbg)(void *, int, const char *)
int(* f_rng)(void *, unsigned char *, size_t)
SHA-256 context structure.
void(* update_checksum)(ssl_context *, unsigned char *, size_t)
int(* tls_prf)(unsigned char *, size_t, char *, unsigned char *, size_t, unsigned char *, size_t)
Network communication functions.
const int ** ciphersuites
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
char peer_verify_data[36]
void ssl_set_own_cert(ssl_context *ssl, x509_cert *own_cert, rsa_context *rsa_key)
Set own certificate chain and private key.
ssl_transform * transform_out
int ssl_get_ciphersuite_min_version(const int ciphersuite_id)
int(* f_sni)(void *, ssl_context *, const unsigned char *, size_t)
void(* calc_verify)(ssl_context *, unsigned char *)
void ssl_set_verify(ssl_context *ssl, int(*f_vrfy)(void *, x509_cert *, int, int *), void *p_vrfy)
Set the verification callback (Optional).
ssl_session * session_negotiate
void ssl_legacy_renegotiation(ssl_context *ssl, int allow_legacy)
Prevent or allow legacy renegotiation.
int ssl_parse_certificate(ssl_context *ssl)
void ssl_set_dbg(ssl_context *ssl, void(*f_dbg)(void *, int, const char *), void *p_dbg)
Set the debug callback.
void ssl_set_own_cert_alt(ssl_context *ssl, x509_cert *own_cert, void *rsa_key, rsa_decrypt_func rsa_decrypt, rsa_sign_func rsa_sign, rsa_key_len_func rsa_key_len)
Set own certificate and alternate non-PolarSSL private key and handling callbacks, such as the PKCS#11 wrappers or any other external private key handler.
ssl_transform * transform_in
int ssl_parse_finished(ssl_context *ssl)
void ssl_session_free(ssl_session *session)
Free referenced items in an SSL session including the peer certificate and clear memory.
int ssl_write_finished(ssl_context *ssl)
Configuration options (set of defines)
ssl_transform * transform
void ssl_handshake_wrapup(ssl_context *ssl)
int(* f_send)(void *, const unsigned char *, size_t)
int ssl_set_dh_param_ctx(ssl_context *ssl, dhm_context *dhm_ctx)
Set the Diffie-Hellman public P and G values, read from existing context (server-side only) ...
int(* rsa_decrypt_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Container for an X.509 certificate.
int ssl_handshake_server_step(ssl_context *ssl)
void ssl_set_max_version(ssl_context *ssl, int major, int minor)
Set the maximum supported version sent from the client side.
const int ssl_default_ciphersuites[]
void ssl_set_ciphersuites_for_version(ssl_context *ssl, const int *ciphersuites, int major, int minor)
Set the list of allowed ciphersuites for a specific version of the protocol.
int ssl_init(ssl_context *ssl)
Initialize an SSL context.
int ssl_get_ciphersuite_id(const char *ciphersuite_name)
Return the ID of the ciphersuite associated with the given name.
unsigned char premaster[POLARSSL_MPI_MAX_SIZE]
ssl_handshake_params * handshake
int ssl_write_certificate(ssl_context *ssl)
size_t(* rsa_key_len_func)(void *ctx)
SHA-384 and SHA-512 cryptographic hash function.
rsa_key_len_func rsa_key_len
const char * ssl_get_ciphersuite(const ssl_context *ssl)
Return the name of the current ciphersuite.
const char * ssl_get_version(const ssl_context *ssl)
Return the current SSL version (SSLv3/TLSv1/etc)
void ssl_set_renegotiation(ssl_context *ssl, int renegotiation)
Enable / Disable renegotiation support for connection when initiated by peer (Default: SSL_RENEGOTIAT...
SHA-224 and SHA-256 cryptographic hash function.
int ssl_handshake_client_step(ssl_context *ssl)
int(* f_vrfy)(void *, x509_cert *, int, int *)
void ssl_set_endpoint(ssl_context *ssl, int endpoint)
Set the current endpoint type.
void ssl_set_ciphersuites(ssl_context *ssl, const int *ciphersuites)
Set the list of allowed ciphersuites (Default: ssl_default_ciphersuites) (Overrides all version speci...
void ssl_set_rng(ssl_context *ssl, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Set the random number generator callback.
void ssl_set_bio(ssl_context *ssl, int(*f_recv)(void *, unsigned char *, size_t), void *p_recv, int(*f_send)(void *, const unsigned char *, size_t), void *p_send)
Set the underlying BIO read and write callbacks.
void ssl_free(ssl_context *ssl)
Free referenced items in an SSL context and clear memory.
int(* rsa_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
void ssl_handshake_free(ssl_handshake_params *handshake)
Free referenced items in an SSL handshake context and clear memory.
int ssl_flush_output(ssl_context *ssl)
int ssl_handshake(ssl_context *ssl)
Perform the SSL handshake.
rsa_decrypt_func rsa_decrypt
void ssl_set_min_version(ssl_context *ssl, int major, int minor)
Set the minimum accepted SSL/TLS protocol version (Default: SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_0)...
Diffie-Hellman-Merkle key exchange.
void ssl_set_session(ssl_context *ssl, const ssl_session *session)
Request resumption of session (client-side only) Session data is copied from presented session struct...
int ssl_set_hostname(ssl_context *ssl, const char *hostname)
Set hostname for ServerName TLS extension (client-side only)
int ssl_handshake_step(ssl_context *ssl)
Perform a single step of the SSL handshake.
X.509 certificate and private key decoding.
int ssl_parse_change_cipher_spec(ssl_context *ssl)
int ssl_close_notify(ssl_context *ssl)
Notify the peer that the connection is being closed.
void ssl_set_session_cache(ssl_context *ssl, int(*f_get_cache)(void *, ssl_session *), void *p_get_cache, int(*f_set_cache)(void *, const ssl_session *), void *p_set_cache)
Set the session cache callbacks (server-side only) If not set, no session resuming is done...
size_t ssl_get_bytes_avail(const ssl_context *ssl)
Return the number of data bytes available to read.
int allow_legacy_renegotiation
ssl_session * session_out
The RSA public-key cryptosystem.
void(* calc_finished)(ssl_context *, unsigned char *, int)
int ssl_read_record(ssl_context *ssl)
int ssl_set_dh_param(ssl_context *ssl, const char *dhm_P, const char *dhm_G)
Set the Diffie-Hellman public P and G values, read as hexadecimal strings (server-side only) (Default...
SHA-1 cryptographic hash function.
int ssl_get_verify_result(const ssl_context *ssl)
Return the result of the certificate verification.
int ssl_session_reset(ssl_context *ssl)
Reset an already initialized SSL context for re-use while retaining application-set variables...
Certificate revocation list structure.
ssl_transform * transform_negotiate
SHA-512 context structure.
int disable_renegotiation
int ssl_write_change_cipher_spec(ssl_context *ssl)
int(* f_get_cache)(void *, ssl_session *)
int ssl_derive_keys(ssl_context *ssl)
void ssl_set_authmode(ssl_context *ssl, int authmode)
Set the certificate verification mode.
int(* f_set_cache)(void *, const ssl_session *)
void ssl_set_ca_chain(ssl_context *ssl, x509_cert *ca_chain, x509_crl *ca_crl, const char *peer_cn)
Set the data required to verify peer certificate.
static int safer_memcmp(const void *a, const void *b, size_t n)
void ssl_optimize_checksum(ssl_context *ssl, int ciphersuite)
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
int ssl_read(ssl_context *ssl, unsigned char *buf, size_t len)
Read at most 'len' application data bytes.
void ssl_transform_free(ssl_transform *transform)
Free referenced items in an SSL transform context and clear memory.
static const int * ssl_list_ciphersuites(void)
Returns the list of ciphersuites supported by the SSL/TLS module.
const char * ssl_get_ciphersuite_name(const int ciphersuite_id)
Return the name of the ciphersuite associated with the given ID.
MD5 message digest algorithm (hash function)
int ssl_renegotiate(ssl_context *ssl)
Perform an SSL renegotiation on the running connection.
int(* f_recv)(void *, unsigned char *, size_t)
int ssl_write(ssl_context *ssl, const unsigned char *buf, size_t len)
Write exactly 'len' application data bytes.
void ssl_set_sni(ssl_context *ssl, int(*f_sni)(void *, ssl_context *, const unsigned char *, size_t), void *p_sni)
Set server side ServerName TLS extension callback (optional, server-side only).
int ssl_fetch_input(ssl_context *ssl, size_t nb_want)
int ssl_write_record(ssl_context *ssl)
const x509_cert * ssl_get_peer_cert(const ssl_context *ssl)
Return the peer certificate from the current connection.
unsigned char randbytes[64]