27 #ifndef POLARSSL_SSL_H
28 #define POLARSSL_SSL_H
30 #if !defined(POLARSSL_CONFIG_FILE)
33 #include POLARSSL_CONFIG_FILE
41 #if defined(POLARSSL_MD5_C)
45 #if defined(POLARSSL_SHA1_C)
49 #if defined(POLARSSL_SHA256_C)
53 #if defined(POLARSSL_SHA512_C)
58 #if defined(POLARSSL_AES_C)
62 #if defined(POLARSSL_X509_CRT_PARSE_C)
67 #if defined(POLARSSL_DHM_C)
71 #if defined(POLARSSL_ECDH_C)
75 #if defined(POLARSSL_ZLIB_SUPPORT)
79 #if defined(POLARSSL_HAVE_TIME)
84 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
85 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
86 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
87 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
88 #define POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED
91 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
92 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
93 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
94 #define POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED
97 #if defined(_MSC_VER) && !defined(inline)
98 #define inline _inline
100 #if defined(__ARMCC_VERSION) && !defined(inline)
101 #define inline __inline
108 #define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080
109 #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100
110 #define POLARSSL_ERR_SSL_INVALID_MAC -0x7180
111 #define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200
112 #define POLARSSL_ERR_SSL_CONN_EOF -0x7280
113 #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300
114 #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380
115 #define POLARSSL_ERR_SSL_NO_RNG -0x7400
116 #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480
117 #define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500
118 #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580
119 #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600
120 #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680
121 #define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700
122 #define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780
123 #define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800
124 #define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880
125 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900
126 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980
127 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00
128 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80
129 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00
130 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80
131 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00
132 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80
133 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00
134 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80
135 #define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00
136 #define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80
137 #define POLARSSL_ERR_SSL_MALLOC_FAILED -0x7F00
138 #define POLARSSL_ERR_SSL_HW_ACCEL_FAILED -0x7F80
139 #define POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80
140 #define POLARSSL_ERR_SSL_COMPRESSION_FAILED -0x6F00
141 #define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80
142 #define POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00
143 #define POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80
144 #define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH -0x6D00
145 #define POLARSSL_ERR_SSL_UNKNOWN_IDENTITY -0x6C80
146 #define POLARSSL_ERR_SSL_INTERNAL_ERROR -0x6C00
147 #define POLARSSL_ERR_SSL_COUNTER_WRAPPING -0x6B80
148 #define POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00
153 #define SSL_MAJOR_VERSION_3 3
154 #define SSL_MINOR_VERSION_0 0
155 #define SSL_MINOR_VERSION_1 1
156 #define SSL_MINOR_VERSION_2 2
157 #define SSL_MINOR_VERSION_3 3
160 #define SSL_MIN_MAJOR_VERSION SSL_MAJOR_VERSION_3
162 #if defined(POLARSSL_SSL_PROTO_SSL3)
163 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_0
165 #if defined(POLARSSL_SSL_PROTO_TLS1)
166 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_1
168 #if defined(POLARSSL_SSL_PROTO_TLS1_1)
169 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_2
171 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
172 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_3
179 #define SSL_MAX_MAJOR_VERSION SSL_MAJOR_VERSION_3
181 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
182 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_3
184 #if defined(POLARSSL_SSL_PROTO_TLS1_1)
185 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_2
187 #if defined(POLARSSL_SSL_PROTO_TLS1)
188 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_1
190 #if defined(POLARSSL_SSL_PROTO_SSL3)
191 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_0
199 #define SSL_MAX_FRAG_LEN_NONE 0
200 #define SSL_MAX_FRAG_LEN_512 1
201 #define SSL_MAX_FRAG_LEN_1024 2
202 #define SSL_MAX_FRAG_LEN_2048 3
203 #define SSL_MAX_FRAG_LEN_4096 4
204 #define SSL_MAX_FRAG_LEN_INVALID 5
206 #define SSL_IS_CLIENT 0
207 #define SSL_IS_SERVER 1
209 #define SSL_COMPRESS_NULL 0
210 #define SSL_COMPRESS_DEFLATE 1
212 #define SSL_VERIFY_NONE 0
213 #define SSL_VERIFY_OPTIONAL 1
214 #define SSL_VERIFY_REQUIRED 2
216 #define SSL_INITIAL_HANDSHAKE 0
217 #define SSL_RENEGOTIATION 1
218 #define SSL_RENEGOTIATION_DONE 2
219 #define SSL_RENEGOTIATION_PENDING 3
221 #define SSL_LEGACY_RENEGOTIATION 0
222 #define SSL_SECURE_RENEGOTIATION 1
224 #define SSL_RENEGOTIATION_DISABLED 0
225 #define SSL_RENEGOTIATION_ENABLED 1
227 #define SSL_RENEGOTIATION_NOT_ENFORCED -1
228 #define SSL_RENEGO_MAX_RECORDS_DEFAULT 16
230 #define SSL_LEGACY_NO_RENEGOTIATION 0
231 #define SSL_LEGACY_ALLOW_RENEGOTIATION 1
232 #define SSL_LEGACY_BREAK_HANDSHAKE 2
234 #define SSL_TRUNC_HMAC_DISABLED 0
235 #define SSL_TRUNC_HMAC_ENABLED 1
236 #define SSL_TRUNCATED_HMAC_LEN 10
238 #define SSL_SESSION_TICKETS_DISABLED 0
239 #define SSL_SESSION_TICKETS_ENABLED 1
249 #if !defined(SSL_DEFAULT_TICKET_LIFETIME)
250 #define SSL_DEFAULT_TICKET_LIFETIME 86400
262 #if !defined(SSL_MAX_CONTENT_LEN)
263 #define SSL_MAX_CONTENT_LEN 16384
274 #if defined(POLARSSL_ZLIB_SUPPORT)
275 #define SSL_COMPRESSION_ADD 1024
277 #define SSL_COMPRESSION_ADD 0
280 #if defined(POLARSSL_RC4_C) || defined(POLARSSL_CIPHER_MODE_CBC)
282 #if defined(POLARSSL_SHA512_C)
283 #define SSL_MAC_ADD 48
284 #elif defined(POLARSSL_SHA256_C)
285 #define SSL_MAC_ADD 32
287 #define SSL_MAC_ADD 20
291 #define SSL_MAC_ADD 16
294 #if defined(POLARSSL_CIPHER_MODE_CBC)
295 #define SSL_PADDING_ADD 256
297 #define SSL_PADDING_ADD 0
300 #define SSL_BUFFER_LEN ( SSL_MAX_CONTENT_LEN \
301 + SSL_COMPRESSION_ADD \
310 #define SSL_EMPTY_RENEGOTIATION_INFO 0xFF
316 #define SSL_HASH_NONE 0
317 #define SSL_HASH_MD5 1
318 #define SSL_HASH_SHA1 2
319 #define SSL_HASH_SHA224 3
320 #define SSL_HASH_SHA256 4
321 #define SSL_HASH_SHA384 5
322 #define SSL_HASH_SHA512 6
324 #define SSL_SIG_ANON 0
325 #define SSL_SIG_RSA 1
326 #define SSL_SIG_ECDSA 3
332 #define SSL_CERT_TYPE_RSA_SIGN 1
333 #define SSL_CERT_TYPE_ECDSA_SIGN 64
338 #define SSL_MSG_CHANGE_CIPHER_SPEC 20
339 #define SSL_MSG_ALERT 21
340 #define SSL_MSG_HANDSHAKE 22
341 #define SSL_MSG_APPLICATION_DATA 23
343 #define SSL_ALERT_LEVEL_WARNING 1
344 #define SSL_ALERT_LEVEL_FATAL 2
346 #define SSL_ALERT_MSG_CLOSE_NOTIFY 0
347 #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
348 #define SSL_ALERT_MSG_BAD_RECORD_MAC 20
349 #define SSL_ALERT_MSG_DECRYPTION_FAILED 21
350 #define SSL_ALERT_MSG_RECORD_OVERFLOW 22
351 #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
352 #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
353 #define SSL_ALERT_MSG_NO_CERT 41
354 #define SSL_ALERT_MSG_BAD_CERT 42
355 #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
356 #define SSL_ALERT_MSG_CERT_REVOKED 44
357 #define SSL_ALERT_MSG_CERT_EXPIRED 45
358 #define SSL_ALERT_MSG_CERT_UNKNOWN 46
359 #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
360 #define SSL_ALERT_MSG_UNKNOWN_CA 48
361 #define SSL_ALERT_MSG_ACCESS_DENIED 49
362 #define SSL_ALERT_MSG_DECODE_ERROR 50
363 #define SSL_ALERT_MSG_DECRYPT_ERROR 51
364 #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
365 #define SSL_ALERT_MSG_PROTOCOL_VERSION 70
366 #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
367 #define SSL_ALERT_MSG_INTERNAL_ERROR 80
368 #define SSL_ALERT_MSG_USER_CANCELED 90
369 #define SSL_ALERT_MSG_NO_RENEGOTIATION 100
370 #define SSL_ALERT_MSG_UNSUPPORTED_EXT 110
371 #define SSL_ALERT_MSG_UNRECOGNIZED_NAME 112
372 #define SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115
373 #define SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120
375 #define SSL_HS_HELLO_REQUEST 0
376 #define SSL_HS_CLIENT_HELLO 1
377 #define SSL_HS_SERVER_HELLO 2
378 #define SSL_HS_NEW_SESSION_TICKET 4
379 #define SSL_HS_CERTIFICATE 11
380 #define SSL_HS_SERVER_KEY_EXCHANGE 12
381 #define SSL_HS_CERTIFICATE_REQUEST 13
382 #define SSL_HS_SERVER_HELLO_DONE 14
383 #define SSL_HS_CERTIFICATE_VERIFY 15
384 #define SSL_HS_CLIENT_KEY_EXCHANGE 16
385 #define SSL_HS_FINISHED 20
390 #define TLS_EXT_SERVERNAME 0
391 #define TLS_EXT_SERVERNAME_HOSTNAME 0
393 #define TLS_EXT_MAX_FRAGMENT_LENGTH 1
395 #define TLS_EXT_TRUNCATED_HMAC 4
397 #define TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
398 #define TLS_EXT_SUPPORTED_POINT_FORMATS 11
400 #define TLS_EXT_SIG_ALG 13
402 #define TLS_EXT_ALPN 16
404 #define TLS_EXT_SESSION_TICKET 35
406 #define TLS_EXT_RENEGOTIATION_INFO 0xFF01
413 #define TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
418 #if !defined(POLARSSL_PSK_MAX_LEN)
419 #define POLARSSL_PSK_MAX_LEN 32
425 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
428 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
431 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
432 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
433 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
434 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
437 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
440 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
444 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
447 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
453 #define POLARSSL_PREMASTER_SIZE sizeof( union _ssl_premaster_secret )
464 const unsigned char *input,
unsigned char *output,
465 size_t output_max_len );
467 int (*f_rng)(
void *,
unsigned char *, size_t),
void *p_rng,
468 int mode,
md_type_t md_alg,
unsigned int hashlen,
469 const unsigned char *hash,
unsigned char *sig );
502 #if defined(POLARSSL_SSL_SESSION_TICKETS)
505 #if defined(POLARSSL_X509_CRT_PARSE_C)
514 #if defined(POLARSSL_HAVE_TIME)
520 unsigned char id[32];
523 #if defined(POLARSSL_X509_CRT_PARSE_C)
528 #if defined(POLARSSL_SSL_SESSION_TICKETS)
534 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
538 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
563 #if defined(POLARSSL_SSL_PROTO_SSL3)
578 #if defined(POLARSSL_ZLIB_SUPPORT)
579 z_stream ctx_deflate;
580 z_stream ctx_inflate;
595 #if defined(POLARSSL_DHM_C)
598 #if defined(POLARSSL_ECDH_C)
601 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
604 #if defined(POLARSSL_X509_CRT_PARSE_C)
612 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
620 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
621 defined(POLARSSL_SSL_PROTO_TLS1_1)
625 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
626 #if defined(POLARSSL_SHA256_C)
629 #if defined(POLARSSL_SHA512_C)
637 int (*
tls_prf)(
const unsigned char *, size_t,
const char *,
638 const unsigned char *, size_t,
639 unsigned char *, size_t);
652 #if defined(POLARSSL_SSL_SESSION_TICKETS)
657 #if defined(POLARSSL_SSL_SESSION_TICKETS)
670 #if defined(POLARSSL_X509_CRT_PARSE_C)
703 int (*
f_rng)(
void *,
unsigned char *, size_t);
704 void (*
f_dbg)(
void *, int,
const char *);
705 int (*
f_recv)(
void *,
unsigned char *, size_t);
706 int (*
f_send)(
void *,
const unsigned char *, size_t);
718 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
723 #if defined(POLARSSL_X509_CRT_PARSE_C)
728 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
781 #if defined(POLARSSL_ZLIB_SUPPORT)
782 unsigned char *compress_buf;
784 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
791 #if defined(POLARSSL_X509_CRT_PARSE_C)
802 #if defined(POLARSSL_SSL_SESSION_TICKETS)
817 #if defined(POLARSSL_SSL_SET_CURVES)
820 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
823 #if defined(POLARSSL_SSL_SESSION_TICKETS)
828 #if defined(POLARSSL_DHM_C)
833 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
843 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
851 #if defined(POLARSSL_SSL_ALPN)
869 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
871 #define SSL_CHANNEL_OUTBOUND 0
872 #define SSL_CHANNEL_INBOUND 1
875 const unsigned char *key_enc,
const unsigned char *key_dec,
877 const unsigned char *iv_enc,
const unsigned char *iv_dec,
879 const unsigned char *mac_enc,
const unsigned char *mac_dec,
881 extern int (*ssl_hw_record_activate)(
ssl_context *ssl,
int direction);
882 extern int (*ssl_hw_record_reset)(
ssl_context *ssl);
883 extern int (*ssl_hw_record_write)(
ssl_context *ssl);
884 extern int (*ssl_hw_record_read)(
ssl_context *ssl);
885 extern int (*ssl_hw_record_finish)(
ssl_context *ssl);
975 #if defined(POLARSSL_X509_CRT_PARSE_C)
988 int (*f_vrfy)(
void *,
x509_crt *,
int,
int *),
1000 int (*f_rng)(
void *,
unsigned char *,
size_t),
1011 void (*f_dbg)(
void *,
int,
const char *),
1024 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
1025 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send );
1065 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
1066 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache );
1112 const int *ciphersuites,
1113 int major,
int minor );
1115 #if defined(POLARSSL_X509_CRT_PARSE_C)
1125 x509_crl *ca_crl,
const char *peer_cn );
1148 #if defined(POLARSSL_RSA_C)
1201 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1215 const unsigned char *psk_identity,
size_t psk_identity_len );
1238 int (*f_psk)(
void *,
ssl_context *,
const unsigned char *,
1243 #if defined(POLARSSL_DHM_C)
1269 #if defined(POLARSSL_SSL_SET_CURVES)
1291 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1324 int (*f_sni)(
void *,
ssl_context *,
const unsigned char *,
1329 #if defined(POLARSSL_SSL_ALPN)
1386 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1405 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1420 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1563 #if defined(POLARSSL_X509_CRT_PARSE_C)
1674 unsigned char level,
1675 unsigned char message );
1754 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1758 #if defined(POLARSSL_PK_C)
1765 #if defined(POLARSSL_SSL_SET_CURVES)
1769 #if defined(POLARSSL_X509_CRT_PARSE_C)
1793 int cert_endpoint );
1800 const unsigned char *A = (
const unsigned char *) a;
1801 const unsigned char *B = (
const unsigned char *) b;
1802 unsigned char diff = 0;
1804 for( i = 0; i < n; i++ )
1805 diff |= A[i] ^ B[i];