mbed TLS v1.3.11
ssl.h
Go to the documentation of this file.
1 
24 #ifndef POLARSSL_SSL_H
25 #define POLARSSL_SSL_H
26 
27 #if !defined(POLARSSL_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include POLARSSL_CONFIG_FILE
31 #endif
32 
33 /* Temporary compatibility trick for the current stable branch */
34 #if !defined(POLARSSL_SSL_DISABLE_RENEGOTIATION)
35 #define POLARSSL_SSL_RENEGOTIATION
36 #endif
37 
38 #include "net.h"
39 #include "bignum.h"
40 #include "ecp.h"
41 
42 #include "ssl_ciphersuites.h"
43 
44 #if defined(POLARSSL_MD5_C)
45 #include "md5.h"
46 #endif
47 
48 #if defined(POLARSSL_SHA1_C)
49 #include "sha1.h"
50 #endif
51 
52 #if defined(POLARSSL_SHA256_C)
53 #include "sha256.h"
54 #endif
55 
56 #if defined(POLARSSL_SHA512_C)
57 #include "sha512.h"
58 #endif
59 
60 // for session tickets
61 #if defined(POLARSSL_AES_C)
62 #include "aes.h"
63 #endif
64 
65 #if defined(POLARSSL_X509_CRT_PARSE_C)
66 #include "x509_crt.h"
67 #include "x509_crl.h"
68 #endif
69 
70 #if defined(POLARSSL_DHM_C)
71 #include "dhm.h"
72 #endif
73 
74 #if defined(POLARSSL_ECDH_C)
75 #include "ecdh.h"
76 #endif
77 
78 #if defined(POLARSSL_ZLIB_SUPPORT)
79 #include "zlib.h"
80 #endif
81 
82 #if defined(POLARSSL_HAVE_TIME)
83 #include <time.h>
84 #endif
85 
86 /* For convenience below and in programs */
87 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
88  defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) || \
89  defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
90  defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
91 #define POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED
92 #endif
93 
94 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
95  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
96  defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
97 #define POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED
98 #endif
99 
100 #if defined(_MSC_VER) && !defined(inline)
101 #define inline _inline
102 #else
103 #if defined(__ARMCC_VERSION) && !defined(inline)
104 #define inline __inline
105 #endif /* __ARMCC_VERSION */
106 #endif /*_MSC_VER */
107 
108 /*
109  * SSL Error codes
110  */
111 #define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080
112 #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100
113 #define POLARSSL_ERR_SSL_INVALID_MAC -0x7180
114 #define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200
115 #define POLARSSL_ERR_SSL_CONN_EOF -0x7280
116 #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300
117 #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380
118 #define POLARSSL_ERR_SSL_NO_RNG -0x7400
119 #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480
120 #define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500
121 #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580
122 #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600
123 #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680
124 #define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700
125 #define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780
126 #define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800
127 #define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880
128 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900
129 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980
130 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00
131 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80
132 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00
133 #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80
134 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00
135 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80
136 #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00
137 #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80
138 #define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00
139 #define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80
140 #define POLARSSL_ERR_SSL_MALLOC_FAILED -0x7F00
141 #define POLARSSL_ERR_SSL_HW_ACCEL_FAILED -0x7F80
142 #define POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80
143 #define POLARSSL_ERR_SSL_COMPRESSION_FAILED -0x6F00
144 #define POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80
145 #define POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00
146 #define POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80
147 #define POLARSSL_ERR_SSL_PK_TYPE_MISMATCH -0x6D00
148 #define POLARSSL_ERR_SSL_UNKNOWN_IDENTITY -0x6C80
149 #define POLARSSL_ERR_SSL_INTERNAL_ERROR -0x6C00
150 #define POLARSSL_ERR_SSL_COUNTER_WRAPPING -0x6B80
151 #define POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00
152 #define POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6A80
154 /*
155  * Various constants
156  */
157 #define SSL_MAJOR_VERSION_3 3
158 #define SSL_MINOR_VERSION_0 0
159 #define SSL_MINOR_VERSION_1 1
160 #define SSL_MINOR_VERSION_2 2
161 #define SSL_MINOR_VERSION_3 3
163 /* Determine minimum supported version */
164 #define SSL_MIN_MAJOR_VERSION SSL_MAJOR_VERSION_3
165 
166 #if defined(POLARSSL_SSL_PROTO_SSL3)
167 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_0
168 #else
169 #if defined(POLARSSL_SSL_PROTO_TLS1)
170 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_1
171 #else
172 #if defined(POLARSSL_SSL_PROTO_TLS1_1)
173 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_2
174 #else
175 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
176 #define SSL_MIN_MINOR_VERSION SSL_MINOR_VERSION_3
177 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
178 #endif /* POLARSSL_SSL_PROTO_TLS1_1 */
179 #endif /* POLARSSL_SSL_PROTO_TLS1 */
180 #endif /* POLARSSL_SSL_PROTO_SSL3 */
181 
182 /* Determine maximum supported version */
183 #define SSL_MAX_MAJOR_VERSION SSL_MAJOR_VERSION_3
184 
185 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
186 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_3
187 #else
188 #if defined(POLARSSL_SSL_PROTO_TLS1_1)
189 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_2
190 #else
191 #if defined(POLARSSL_SSL_PROTO_TLS1)
192 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_1
193 #else
194 #if defined(POLARSSL_SSL_PROTO_SSL3)
195 #define SSL_MAX_MINOR_VERSION SSL_MINOR_VERSION_0
196 #endif /* POLARSSL_SSL_PROTO_SSL3 */
197 #endif /* POLARSSL_SSL_PROTO_TLS1 */
198 #endif /* POLARSSL_SSL_PROTO_TLS1_1 */
199 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
200 
201 /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
202  * NONE must be zero so that memset()ing structure to zero works */
203 #define SSL_MAX_FRAG_LEN_NONE 0
204 #define SSL_MAX_FRAG_LEN_512 1
205 #define SSL_MAX_FRAG_LEN_1024 2
206 #define SSL_MAX_FRAG_LEN_2048 3
207 #define SSL_MAX_FRAG_LEN_4096 4
208 #define SSL_MAX_FRAG_LEN_INVALID 5
210 #define SSL_IS_CLIENT 0
211 #define SSL_IS_SERVER 1
212 
213 #define SSL_IS_NOT_FALLBACK 0
214 #define SSL_IS_FALLBACK 1
215 
216 #define SSL_EXTENDED_MS_DISABLED 0
217 #define SSL_EXTENDED_MS_ENABLED 1
218 
219 #define SSL_ETM_DISABLED 0
220 #define SSL_ETM_ENABLED 1
221 
222 #define SSL_COMPRESS_NULL 0
223 #define SSL_COMPRESS_DEFLATE 1
224 
225 #define SSL_VERIFY_NONE 0
226 #define SSL_VERIFY_OPTIONAL 1
227 #define SSL_VERIFY_REQUIRED 2
228 
229 #define SSL_INITIAL_HANDSHAKE 0
230 #define SSL_RENEGOTIATION 1 /* In progress */
231 #define SSL_RENEGOTIATION_DONE 2 /* Done */
232 #define SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
233 
234 #define SSL_LEGACY_RENEGOTIATION 0
235 #define SSL_SECURE_RENEGOTIATION 1
236 
237 #define SSL_RENEGOTIATION_DISABLED 0
238 #define SSL_RENEGOTIATION_ENABLED 1
239 
240 #define SSL_RENEGOTIATION_NOT_ENFORCED -1
241 #define SSL_RENEGO_MAX_RECORDS_DEFAULT 16
242 
243 #define SSL_LEGACY_NO_RENEGOTIATION 0
244 #define SSL_LEGACY_ALLOW_RENEGOTIATION 1
245 #define SSL_LEGACY_BREAK_HANDSHAKE 2
246 
247 #define SSL_TRUNC_HMAC_DISABLED 0
248 #define SSL_TRUNC_HMAC_ENABLED 1
249 #define SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */
250 
251 #define SSL_SESSION_TICKETS_DISABLED 0
252 #define SSL_SESSION_TICKETS_ENABLED 1
253 
254 #define SSL_CBC_RECORD_SPLITTING_DISABLED -1
255 #define SSL_CBC_RECORD_SPLITTING_ENABLED 0
256 
257 #define SSL_ARC4_ENABLED 0
258 #define SSL_ARC4_DISABLED 1
259 
268 #if !defined(SSL_DEFAULT_TICKET_LIFETIME)
269 #define SSL_DEFAULT_TICKET_LIFETIME 86400
270 #endif
271 
272 /*
273  * Size of the input / output buffer.
274  * Note: the RFC defines the default size of SSL / TLS messages. If you
275  * change the value here, other clients / servers may not be able to
276  * communicate with you anymore. Only change this value if you control
277  * both sides of the connection and have it reduced at both sides, or
278  * if you're using the Max Fragment Length extension and you know all your
279  * peers are using it too!
280  */
281 #if !defined(SSL_MAX_CONTENT_LEN)
282 #define SSL_MAX_CONTENT_LEN 16384
283 #endif
284 
285 /* \} name SECTION: Module settings */
286 
287 /*
288  * Allow extra bytes for record, authentication and encryption overhead:
289  * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
290  * and allow for a maximum of 1024 of compression expansion if
291  * enabled.
292  */
293 #if defined(POLARSSL_ZLIB_SUPPORT)
294 #define SSL_COMPRESSION_ADD 1024
295 #else
296 #define SSL_COMPRESSION_ADD 0
297 #endif
298 
299 #if defined(POLARSSL_RC4_C) || defined(POLARSSL_CIPHER_MODE_CBC)
300 /* Ciphersuites using HMAC */
301 #if defined(POLARSSL_SHA512_C)
302 #define SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
303 #elif defined(POLARSSL_SHA256_C)
304 #define SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
305 #else
306 #define SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
307 #endif
308 #else
309 /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
310 #define SSL_MAC_ADD 16
311 #endif
312 
313 #if defined(POLARSSL_CIPHER_MODE_CBC)
314 #define SSL_PADDING_ADD 256
315 #else
316 #define SSL_PADDING_ADD 0
317 #endif
318 
319 #define SSL_BUFFER_LEN ( SSL_MAX_CONTENT_LEN \
320  + SSL_COMPRESSION_ADD \
321  + 29 /* counter + header + IV */ \
322  + SSL_MAC_ADD \
323  + SSL_PADDING_ADD \
324  )
325 
326 /*
327  * Length of the verify data for secure renegotiation
328  */
329 #if defined(POLARSSL_SSL_PROTO_SSL3)
330 #define SSL_VERIFY_DATA_MAX_LEN 36
331 #else
332 #define SSL_VERIFY_DATA_MAX_LEN 12
333 #endif
334 
335 /*
336  * Signaling ciphersuite values (SCSV)
337  */
338 #define SSL_EMPTY_RENEGOTIATION_INFO 0xFF
339 #define SSL_FALLBACK_SCSV 0x5600
341 /*
342  * Supported Signature and Hash algorithms (For TLS 1.2)
343  * RFC 5246 section 7.4.1.4.1
344  */
345 #define SSL_HASH_NONE 0
346 #define SSL_HASH_MD5 1
347 #define SSL_HASH_SHA1 2
348 #define SSL_HASH_SHA224 3
349 #define SSL_HASH_SHA256 4
350 #define SSL_HASH_SHA384 5
351 #define SSL_HASH_SHA512 6
352 
353 #define SSL_SIG_ANON 0
354 #define SSL_SIG_RSA 1
355 #define SSL_SIG_ECDSA 3
356 
357 /*
358  * Client Certificate Types
359  * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
360  */
361 #define SSL_CERT_TYPE_RSA_SIGN 1
362 #define SSL_CERT_TYPE_ECDSA_SIGN 64
363 
364 /*
365  * Message, alert and handshake types
366  */
367 #define SSL_MSG_CHANGE_CIPHER_SPEC 20
368 #define SSL_MSG_ALERT 21
369 #define SSL_MSG_HANDSHAKE 22
370 #define SSL_MSG_APPLICATION_DATA 23
371 
372 #define SSL_ALERT_LEVEL_WARNING 1
373 #define SSL_ALERT_LEVEL_FATAL 2
374 
375 #define SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
376 #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
377 #define SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
378 #define SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
379 #define SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
380 #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
381 #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
382 #define SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
383 #define SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
384 #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
385 #define SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
386 #define SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
387 #define SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
388 #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
389 #define SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
390 #define SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
391 #define SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
392 #define SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
393 #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
394 #define SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
395 #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
396 #define SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
397 #define SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */
398 #define SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
399 #define SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
400 #define SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
401 #define SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */
402 #define SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */
403 #define SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
404 
405 #define SSL_HS_HELLO_REQUEST 0
406 #define SSL_HS_CLIENT_HELLO 1
407 #define SSL_HS_SERVER_HELLO 2
408 #define SSL_HS_NEW_SESSION_TICKET 4
409 #define SSL_HS_CERTIFICATE 11
410 #define SSL_HS_SERVER_KEY_EXCHANGE 12
411 #define SSL_HS_CERTIFICATE_REQUEST 13
412 #define SSL_HS_SERVER_HELLO_DONE 14
413 #define SSL_HS_CERTIFICATE_VERIFY 15
414 #define SSL_HS_CLIENT_KEY_EXCHANGE 16
415 #define SSL_HS_FINISHED 20
416 
417 /*
418  * TLS extensions
419  */
420 #define TLS_EXT_SERVERNAME 0
421 #define TLS_EXT_SERVERNAME_HOSTNAME 0
422 
423 #define TLS_EXT_MAX_FRAGMENT_LENGTH 1
424 
425 #define TLS_EXT_TRUNCATED_HMAC 4
426 
427 #define TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
428 #define TLS_EXT_SUPPORTED_POINT_FORMATS 11
429 
430 #define TLS_EXT_SIG_ALG 13
431 
432 #define TLS_EXT_ALPN 16
433 
434 #define TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */
435 #define TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */
436 
437 #define TLS_EXT_SESSION_TICKET 35
438 
439 #define TLS_EXT_RENEGOTIATION_INFO 0xFF01
440 
441 /*
442  * TLS extension flags (for extensions with outgoing ServerHello content
443  * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
444  * of state of the renegotiation flag, so no indicator is required)
445  */
446 #define TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
447 
448 /*
449  * Size defines
450  */
451 #if !defined(POLARSSL_PSK_MAX_LEN)
452 #define POLARSSL_PSK_MAX_LEN 32 /* 256 bits */
453 #endif
454 
455 /* Dummy type used only for its size */
457 {
458 #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
459  unsigned char _pms_rsa[48]; /* RFC 5246 8.1.1 */
460 #endif
461 #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
462  unsigned char _pms_dhm[POLARSSL_MPI_MAX_SIZE]; /* RFC 5246 8.1.2 */
463 #endif
464 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
465  defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
466  defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
467  defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
468  unsigned char _pms_ecdh[POLARSSL_ECP_MAX_BYTES]; /* RFC 4492 5.10 */
469 #endif
470 #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
471  unsigned char _pms_psk[4 + 2 * POLARSSL_PSK_MAX_LEN]; /* RFC 4279 2 */
472 #endif
473 #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
474  unsigned char _pms_dhe_psk[4 + POLARSSL_MPI_MAX_SIZE
475  + POLARSSL_PSK_MAX_LEN]; /* RFC 4279 3 */
476 #endif
477 #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
478  unsigned char _pms_rsa_psk[52 + POLARSSL_PSK_MAX_LEN]; /* RFC 4279 4 */
479 #endif
480 #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
481  unsigned char _pms_ecdhe_psk[4 + POLARSSL_ECP_MAX_BYTES
482  + POLARSSL_PSK_MAX_LEN]; /* RFC 5489 2 */
483 #endif
484 };
485 
486 #define POLARSSL_PREMASTER_SIZE sizeof( union _ssl_premaster_secret )
487 
488 #ifdef __cplusplus
489 extern "C" {
490 #endif
491 
492 /*
493  * Generic function pointers for allowing external RSA private key
494  * implementations.
495  */
496 typedef int (*rsa_decrypt_func)( void *ctx, int mode, size_t *olen,
497  const unsigned char *input, unsigned char *output,
498  size_t output_max_len );
499 typedef int (*rsa_sign_func)( void *ctx,
500  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
501  int mode, md_type_t md_alg, unsigned int hashlen,
502  const unsigned char *hash, unsigned char *sig );
503 typedef size_t (*rsa_key_len_func)( void *ctx );
504 
505 /*
506  * SSL state machine
507  */
508 typedef enum
509 {
528 }
529 ssl_states;
530 
531 typedef struct _ssl_session ssl_session;
532 typedef struct _ssl_context ssl_context;
535 #if defined(POLARSSL_SSL_SESSION_TICKETS)
537 #endif
538 #if defined(POLARSSL_X509_CRT_PARSE_C)
540 #endif
541 
542 /*
543  * This structure is used for storing current session data.
544  */
546 {
547 #if defined(POLARSSL_HAVE_TIME)
548  time_t start;
549 #endif
552  size_t length;
553  unsigned char id[32];
554  unsigned char master[48];
556 #if defined(POLARSSL_X509_CRT_PARSE_C)
558 #endif /* POLARSSL_X509_CRT_PARSE_C */
561 #if defined(POLARSSL_SSL_SESSION_TICKETS)
562  unsigned char *ticket;
563  size_t ticket_len;
564  uint32_t ticket_lifetime;
565 #endif /* POLARSSL_SSL_SESSION_TICKETS */
566 
567 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
568  unsigned char mfl_code;
569 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
570 
571 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
573 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
574 
575 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
577 #endif
578 };
579 
580 /*
581  * This structure contains a full set of runtime transform parameters
582  * either in negotiation or active.
583  */
585 {
586  /*
587  * Session specific crypto layer
588  */
591  unsigned int keylen;
592  size_t minlen;
593  size_t ivlen;
594  size_t fixed_ivlen;
595  size_t maclen;
597  unsigned char iv_enc[16];
598  unsigned char iv_dec[16];
600 #if defined(POLARSSL_SSL_PROTO_SSL3)
601  /* Needed only for SSL v3.0 secret */
602  unsigned char mac_enc[20];
603  unsigned char mac_dec[20];
604 #endif /* POLARSSL_SSL_PROTO_SSL3 */
605 
612  /*
613  * Session specific compression layer
614  */
615 #if defined(POLARSSL_ZLIB_SUPPORT)
616  z_stream ctx_deflate;
617  z_stream ctx_inflate;
618 #endif
619 };
620 
621 /*
622  * This structure contains the parameters only needed during handshake.
623  */
625 {
626  /*
627  * Handshake specific crypto variables
628  */
629  int sig_alg;
630  int cert_type;
632 #if defined(POLARSSL_DHM_C)
634 #endif
635 #if defined(POLARSSL_ECDH_C)
637 #endif
638 #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
640 #endif
641 #if defined(POLARSSL_X509_CRT_PARSE_C)
642 
649 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
651 #endif
652 #endif /* POLARSSL_X509_CRT_PARSE_C */
653 
654  /*
655  * Checksum contexts
656  */
657 #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
658  defined(POLARSSL_SSL_PROTO_TLS1_1)
661 #endif
662 #if defined(POLARSSL_SSL_PROTO_TLS1_2)
663 #if defined(POLARSSL_SHA256_C)
665 #endif
666 #if defined(POLARSSL_SHA512_C)
668 #endif
669 #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
670 
671  void (*update_checksum)(ssl_context *, const unsigned char *, size_t);
672  void (*calc_verify)(ssl_context *, unsigned char *);
673  void (*calc_finished)(ssl_context *, unsigned char *, int);
674  int (*tls_prf)(const unsigned char *, size_t, const char *,
675  const unsigned char *, size_t,
676  unsigned char *, size_t);
677 
678  size_t pmslen;
680  unsigned char randbytes[64];
684  int resume;
687  int cli_exts;
689 #if defined(POLARSSL_SSL_SESSION_TICKETS)
691 #endif /* POLARSSL_SSL_SESSION_TICKETS */
692 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
694 #endif
695 };
696 
697 #if defined(POLARSSL_SSL_SESSION_TICKETS)
698 /*
699  * Parameters needed to secure session tickets
700  */
702 {
703  unsigned char key_name[16];
706  unsigned char mac_key[16];
707 };
708 #endif /* POLARSSL_SSL_SESSION_TICKETS */
709 
710 #if defined(POLARSSL_X509_CRT_PARSE_C)
711 /*
712  * List of certificate + private key pairs
713  */
715 {
720 };
721 #endif /* POLARSSL_X509_CRT_PARSE_C */
722 
724 {
725  /*
726  * Miscellaneous
727  */
728  int state;
730 #if defined(POLARSSL_SSL_RENEGOTIATION)
732 #endif
733 
734  int major_ver;
735  int minor_ver;
742 #if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
743  char fallback;
744 #endif
745 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
747 #endif
748 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
749  char extended_ms;
750 #endif
753  /*
754  * Callbacks (RNG, debug, I/O, verification)
755  */
756  int (*f_rng)(void *, unsigned char *, size_t);
757  void (*f_dbg)(void *, int, const char *);
758  int (*f_recv)(void *, unsigned char *, size_t);
759  int (*f_send)(void *, const unsigned char *, size_t);
760  int (*f_get_cache)(void *, ssl_session *);
761  int (*f_set_cache)(void *, const ssl_session *);
762 
763  void *p_rng;
764  void *p_dbg;
765  void *p_recv;
766  void *p_send;
767  void *p_get_cache;
768  void *p_set_cache;
769  void *p_hw_data;
771 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
772  int (*f_sni)(void *, ssl_context *, const unsigned char *, size_t);
773  void *p_sni;
774 #endif
775 
776 #if defined(POLARSSL_X509_CRT_PARSE_C)
777  int (*f_vrfy)(void *, x509_crt *, int, int *);
778  void *p_vrfy;
779 #endif
780 
781 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
782  int (*f_psk)(void *, ssl_context *, const unsigned char *, size_t);
783  void *p_psk;
784 #endif
785 
786  /*
787  * Session layer
788  */
797  /*
798  * Record layer transformations
799  */
805  /*
806  * Record layer (incoming data)
807  */
808  unsigned char *in_ctr;
809  unsigned char *in_hdr;
810  unsigned char *in_iv;
811  unsigned char *in_msg;
812  unsigned char *in_offt;
815  size_t in_msglen;
816  size_t in_left;
818  size_t in_hslen;
819  int nb_zero;
822  /*
823  * Record layer (outgoing data)
824  */
825  unsigned char *out_ctr;
826  unsigned char *out_hdr;
827  unsigned char *out_iv;
828  unsigned char *out_msg;
831  size_t out_msglen;
832  size_t out_left;
834 #if defined(POLARSSL_ZLIB_SUPPORT)
835  unsigned char *compress_buf;
836 #endif
837 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
838  unsigned char mfl_code;
839 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
840 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
841  signed char split_done;
843 #endif
844 
845  /*
846  * PKI layer
847  */
848 #if defined(POLARSSL_X509_CRT_PARSE_C)
853  const char *peer_cn;
854 #endif /* POLARSSL_X509_CRT_PARSE_C */
855 
856  /*
857  * Support for generating and checking session tickets
858  */
859 #if defined(POLARSSL_SSL_SESSION_TICKETS)
861 #endif /* POLARSSL_SSL_SESSION_TICKETS */
862 
863  /*
864  * User settings
865  */
866  int endpoint;
867  int authmode;
870 #if defined(POLARSSL_SSL_RENEGOTIATION)
873  unsigned char renego_period[8];
875 #endif
877  const int *ciphersuite_list[4];
878 #if defined(POLARSSL_SSL_SET_CURVES)
879  const ecp_group_id *curve_list;
880 #endif
881 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
883 #endif
884 #if defined(POLARSSL_SSL_SESSION_TICKETS)
887 #endif
888 
889 #if defined(POLARSSL_DHM_C)
892 #endif
893 
894 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
895  /*
896  * PSK values
897  */
898  unsigned char *psk;
899  size_t psk_len;
900  unsigned char *psk_identity;
902 #endif
903 
904 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
905  /*
906  * SNI extension
907  */
908  unsigned char *hostname;
909  size_t hostname_len;
910 #endif
911 
912 #if defined(POLARSSL_SSL_ALPN)
913  /*
914  * ALPN extension
915  */
916  const char **alpn_list;
917  const char *alpn_chosen;
918 #endif
919 
920  /*
921  * Secure renegotiation
922  */
925 #if defined(POLARSSL_SSL_RENEGOTIATION)
929 #endif
930 };
931 
932 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
933 
934 #define SSL_CHANNEL_OUTBOUND 0
935 #define SSL_CHANNEL_INBOUND 1
936 
937 extern int (*ssl_hw_record_init)(ssl_context *ssl,
938  const unsigned char *key_enc, const unsigned char *key_dec,
939  size_t keylen,
940  const unsigned char *iv_enc, const unsigned char *iv_dec,
941  size_t ivlen,
942  const unsigned char *mac_enc, const unsigned char *mac_dec,
943  size_t maclen);
944 extern int (*ssl_hw_record_activate)(ssl_context *ssl, int direction);
945 extern int (*ssl_hw_record_reset)(ssl_context *ssl);
946 extern int (*ssl_hw_record_write)(ssl_context *ssl);
947 extern int (*ssl_hw_record_read)(ssl_context *ssl);
948 extern int (*ssl_hw_record_finish)(ssl_context *ssl);
949 #endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
950 
957 const int *ssl_list_ciphersuites( void );
958 
967 const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
968 
977 int ssl_get_ciphersuite_id( const char *ciphersuite_name );
978 
988 int ssl_init( ssl_context *ssl );
989 
1000 int ssl_session_reset( ssl_context *ssl );
1001 
1011 void ssl_set_endpoint( ssl_context *ssl, int endpoint );
1012 
1036 void ssl_set_authmode( ssl_context *ssl, int authmode );
1037 
1038 #if defined(POLARSSL_X509_CRT_PARSE_C)
1039 
1050 void ssl_set_verify( ssl_context *ssl,
1051  int (*f_vrfy)(void *, x509_crt *, int, int *),
1052  void *p_vrfy );
1053 #endif /* POLARSSL_X509_CRT_PARSE_C */
1054 
1062 void ssl_set_rng( ssl_context *ssl,
1063  int (*f_rng)(void *, unsigned char *, size_t),
1064  void *p_rng );
1065 
1073 void ssl_set_dbg( ssl_context *ssl,
1074  void (*f_dbg)(void *, int, const char *),
1075  void *p_dbg );
1076 
1086 void ssl_set_bio( ssl_context *ssl,
1087  int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
1088  int (*f_send)(void *, const unsigned char *, size_t), void *p_send );
1089 
1090 #if defined(POLARSSL_SSL_SRV_C)
1091 
1130  int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
1131  int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache );
1132 #endif /* POLARSSL_SSL_SRV_C */
1133 
1134 #if defined(POLARSSL_SSL_CLI_C)
1135 
1149 int ssl_set_session( ssl_context *ssl, const ssl_session *session );
1150 #endif /* POLARSSL_SSL_CLI_C */
1151 
1167 void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites );
1168 
1183  const int *ciphersuites,
1184  int major, int minor );
1185 
1186 #if defined(POLARSSL_X509_CRT_PARSE_C)
1187 
1195 void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
1196  x509_crl *ca_crl, const char *peer_cn );
1197 
1216 int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
1217  pk_context *pk_key );
1218 
1219 #if ! defined(POLARSSL_DEPRECATED_REMOVED)
1220 #if defined(POLARSSL_DEPRECATED_WARNING)
1221 #define DEPRECATED __attribute__((deprecated))
1222 #else
1223 #define DEPRECATED
1224 #endif
1225 #if defined(POLARSSL_RSA_C)
1226 
1241 int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
1242  rsa_context *rsa_key ) DEPRECATED;
1243 #endif /* POLARSSL_RSA_C */
1244 
1269 int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
1270  void *rsa_key,
1271  rsa_decrypt_func rsa_decrypt,
1272  rsa_sign_func rsa_sign,
1273  rsa_key_len_func rsa_key_len ) DEPRECATED;
1274 #undef DEPRECATED
1275 #endif /* POLARSSL_DEPRECATED_REMOVED */
1276 #endif /* POLARSSL_X509_CRT_PARSE_C */
1277 
1278 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1279 
1291 int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
1292  const unsigned char *psk_identity, size_t psk_identity_len );
1293 
1314 void ssl_set_psk_cb( ssl_context *ssl,
1315  int (*f_psk)(void *, ssl_context *, const unsigned char *,
1316  size_t),
1317  void *p_psk );
1318 #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1319 
1320 #if defined(POLARSSL_DHM_C)
1321 
1332 int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
1333 
1343 int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
1344 #endif /* POLARSSL_DHM_C */
1345 
1346 #if defined(POLARSSL_SSL_SET_CURVES)
1347 
1365 void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curves );
1366 #endif /* POLARSSL_SSL_SET_CURVES */
1367 
1368 #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
1369 
1379 int ssl_set_hostname( ssl_context *ssl, const char *hostname );
1380 
1400 void ssl_set_sni( ssl_context *ssl,
1401  int (*f_sni)(void *, ssl_context *, const unsigned char *,
1402  size_t),
1403  void *p_sni );
1404 #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
1405 
1406 #if defined(POLARSSL_SSL_ALPN)
1407 
1416 int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos );
1417 
1427 const char *ssl_get_alpn_protocol( const ssl_context *ssl );
1428 #endif /* POLARSSL_SSL_ALPN */
1429 
1445 void ssl_set_max_version( ssl_context *ssl, int major, int minor );
1446 
1462 void ssl_set_min_version( ssl_context *ssl, int major, int minor );
1463 
1464 #if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
1465 
1484 void ssl_set_fallback( ssl_context *ssl, char fallback );
1485 #endif /* POLARSSL_SSL_FALLBACK_SCSV && POLARSSL_SSL_CLI_C */
1486 
1487 #if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1488 
1499 void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm );
1500 #endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1501 
1502 #if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1503 
1514 void ssl_set_extended_master_secret( ssl_context *ssl, char ems );
1515 #endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1516 
1530 void ssl_set_arc4_support( ssl_context *ssl, char arc4 );
1531 
1532 #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
1533 
1548 int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code );
1549 #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
1550 
1551 #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
1552 
1563 int ssl_set_truncated_hmac( ssl_context *ssl, int truncate );
1564 #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
1565 
1566 #if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
1567 
1578 void ssl_set_cbc_record_splitting( ssl_context *ssl, char split );
1579 #endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
1580 
1581 #if defined(POLARSSL_SSL_SESSION_TICKETS)
1582 
1598 int ssl_set_session_tickets( ssl_context *ssl, int use_tickets );
1599 
1607 void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime );
1608 #endif /* POLARSSL_SSL_SESSION_TICKETS */
1609 
1610 #if defined(POLARSSL_SSL_RENEGOTIATION)
1611 
1624 void ssl_set_renegotiation( ssl_context *ssl, int renegotiation );
1625 #endif /* POLARSSL_SSL_RENEGOTIATION */
1626 
1654 void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy );
1655 
1656 #if defined(POLARSSL_SSL_RENEGOTIATION)
1657 
1685 void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records );
1686 
1705  const unsigned char period[8] );
1706 #endif /* POLARSSL_SSL_RENEGOTIATION */
1707 
1715 size_t ssl_get_bytes_avail( const ssl_context *ssl );
1716 
1728 int ssl_get_verify_result( const ssl_context *ssl );
1729 
1737 const char *ssl_get_ciphersuite( const ssl_context *ssl );
1738 
1746 const char *ssl_get_version( const ssl_context *ssl );
1747 
1748 #if defined(POLARSSL_X509_CRT_PARSE_C)
1749 
1763 const x509_crt *ssl_get_peer_cert( const ssl_context *ssl );
1764 #endif /* POLARSSL_X509_CRT_PARSE_C */
1765 
1766 #if defined(POLARSSL_SSL_CLI_C)
1767 
1783 int ssl_get_session( const ssl_context *ssl, ssl_session *session );
1784 #endif /* POLARSSL_SSL_CLI_C */
1785 
1794 int ssl_handshake( ssl_context *ssl );
1795 
1808 int ssl_handshake_step( ssl_context *ssl );
1809 
1810 #if defined(POLARSSL_SSL_RENEGOTIATION)
1811 
1821 int ssl_renegotiate( ssl_context *ssl );
1822 #endif /* POLARSSL_SSL_RENEGOTIATION */
1823 
1834 int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
1835 
1854 int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len );
1855 
1867  unsigned char level,
1868  unsigned char message );
1874 int ssl_close_notify( ssl_context *ssl );
1875 
1881 void ssl_free( ssl_context *ssl );
1882 
1888 void ssl_session_init( ssl_session *session );
1889 
1896 void ssl_session_free( ssl_session *session );
1897 
1904 void ssl_transform_free( ssl_transform *transform );
1905 
1912 void ssl_handshake_free( ssl_handshake_params *handshake );
1913 
1914 /*
1915  * Internal functions (do not call directly)
1916  */
1919 void ssl_handshake_wrapup( ssl_context *ssl );
1920 
1922 
1923 int ssl_derive_keys( ssl_context *ssl );
1924 
1925 int ssl_read_record( ssl_context *ssl );
1930 int ssl_fetch_input( ssl_context *ssl, size_t nb_want );
1931 
1932 int ssl_write_record( ssl_context *ssl );
1933 int ssl_flush_output( ssl_context *ssl );
1934 
1937 
1940 
1941 int ssl_parse_finished( ssl_context *ssl );
1942 int ssl_write_finished( ssl_context *ssl );
1943 
1945  const ssl_ciphersuite_t *ciphersuite_info );
1946 
1947 #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1949 #endif
1950 
1951 #if defined(POLARSSL_PK_C)
1952 unsigned char ssl_sig_from_pk( pk_context *pk );
1953 pk_type_t ssl_pk_alg_from_sig( unsigned char sig );
1954 #endif
1955 
1956 md_type_t ssl_md_alg_from_hash( unsigned char hash );
1957 
1958 #if defined(POLARSSL_SSL_SET_CURVES)
1959 int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id );
1960 #endif
1961 
1962 #if defined(POLARSSL_X509_CRT_PARSE_C)
1963 static inline pk_context *ssl_own_key( ssl_context *ssl )
1964 {
1965  return( ssl->handshake->key_cert == NULL ? NULL
1966  : ssl->handshake->key_cert->key );
1967 }
1968 
1969 static inline x509_crt *ssl_own_cert( ssl_context *ssl )
1970 {
1971  return( ssl->handshake->key_cert == NULL ? NULL
1972  : ssl->handshake->key_cert->cert );
1973 }
1974 
1975 /*
1976  * Check usage of a certificate wrt extensions:
1977  * keyUsage, extendedKeyUsage (later), and nSCertType (later).
1978  *
1979  * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
1980  * check a cert we received from them)!
1981  *
1982  * Return 0 if everything is OK, -1 if not.
1983  */
1984 int ssl_check_cert_usage( const x509_crt *cert,
1985  const ssl_ciphersuite_t *ciphersuite,
1986  int cert_endpoint,
1987  int *flags );
1988 #endif /* POLARSSL_X509_CRT_PARSE_C */
1989 
1990 /* constant-time buffer comparison */
1991 static inline int safer_memcmp( const void *a, const void *b, size_t n )
1992 {
1993  size_t i;
1994  const unsigned char *A = (const unsigned char *) a;
1995  const unsigned char *B = (const unsigned char *) b;
1996  unsigned char diff = 0;
1997 
1998  for( i = 0; i < n; i++ )
1999  diff |= A[i] ^ B[i];
2000 
2001  return( diff );
2002 }
2003 
2004 #ifdef __cplusplus
2005 }
2006 #endif
2007 
2008 #endif /* ssl.h */
const ecp_curve_info ** curves
Definition: ssl.h:639
unsigned char * hostname
Definition: ssl.h:908
ssl_session * session_in
Definition: ssl.h:789
unsigned char mfl_code
Definition: ssl.h:838
size_t length
Definition: ssl.h:552
void * p_set_cache
Definition: ssl.h:768
md_context_t md_ctx_dec
Definition: ssl.h:607
int ciphersuite
Definition: ssl.h:550
int trunc_hmac
Definition: ssl.h:882
size_t in_hslen
Definition: ssl.h:818
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 *)
Definition: ssl.h:757
int(* f_rng)(void *, unsigned char *, size_t)
Definition: ssl.h:756
sha256_context fin_sha256
Definition: ssl.h:664
size_t ivlen
Definition: ssl.h:593
int record_read
Definition: ssl.h:820
int major_ver
Definition: ssl.h:734
int renego_records_seen
Definition: ssl.h:731
Generic cipher context.
Definition: cipher.h:267
#define POLARSSL_PREMASTER_SIZE
Definition: ssl.h:486
SHA-1 context structure.
Definition: sha1.h:55
char extended_ms
Definition: ssl.h:749
unsigned char mac_enc[20]
Definition: ssl.h:602
sha1_context fin_sha1
Definition: ssl.h:660
int compression
Definition: ssl.h:551
pk_type_t ssl_pk_alg_from_sig(unsigned char sig)
Network communication functions.
int state
Definition: ssl.h:728
const char * peer_cn
Definition: ssl.h:853
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
Definition: bignum.h:93
unsigned char master[48]
Definition: ssl.h:554
int ssl_set_truncated_hmac(ssl_context *ssl, int truncate)
Activate negotiation of truncated HMAC (Default: SSL_TRUNC_HMAC_DISABLED on client, SSL_TRUNC_HMAC_ENABLED on server.)
ssl_transform * transform_out
Definition: ssl.h:801
void ssl_set_cbc_record_splitting(ssl_context *ssl, char split)
Enable / Disable 1/n-1 record splitting (Default: SSL_CBC_RECORD_SPLITTING_ENABLED) ...
int(* f_sni)(void *, ssl_context *, const unsigned char *, size_t)
Definition: ssl.h:772
int nb_zero
Definition: ssl.h:819
void(* calc_verify)(ssl_context *, unsigned char *)
Definition: ssl.h:672
int ssl_check_cert_usage(const x509_crt *cert, const ssl_ciphersuite_t *ciphersuite, int cert_endpoint, int *flags)
DHM context structure.
Definition: dhm.h:153
void ssl_set_extended_master_secret(ssl_context *ssl, char ems)
Enable or disable Extended Master Secret negotiation.
void * p_psk
Definition: ssl.h:783
Elliptic curves over GF(p)
size_t ticket_len
Definition: ssl.h:563
ssl_session * session_negotiate
Definition: ssl.h:792
ssl_session * session
Definition: ssl.h:791
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.
char fallback
Definition: ssl.h:743
ssl_key_cert * key_cert
Definition: ssl.h:849
ssl_key_cert * sni_key_cert
Definition: ssl.h:650
int ssl_set_session_tickets(ssl_context *ssl, int use_tickets)
Enable / Disable session tickets (Default: SSL_SESSION_TICKETS_ENABLED on client, SSL_SESSION_TICKETS...
unsigned char iv_enc[16]
Definition: ssl.h:597
size_t out_msglen
Definition: ssl.h:831
void ssl_set_verify(ssl_context *ssl, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Set the verification callback (Optional).
int ticket_lifetime
Definition: ssl.h:886
ssl_transform * transform_in
Definition: ssl.h:800
cipher_context_t cipher_ctx_enc
Definition: ssl.h:609
const int * ciphersuite_list[4]
Definition: ssl.h:877
int ssl_parse_finished(ssl_context *ssl)
void * p_rng
Definition: ssl.h:763
mpi dhm_P
Definition: ssl.h:890
ssl_states
Definition: ssl.h:508
unsigned char premaster[POLARSSL_PREMASTER_SIZE]
Definition: ssl.h:681
void ssl_session_free(ssl_session *session)
Free referenced items in an SSL session including the peer certificate and clear memory.
AES context structure.
Definition: aes.h:65
int ssl_write_finished(ssl_context *ssl)
Configuration options (set of defines)
unsigned char _pms_dhe_psk[4+POLARSSL_MPI_MAX_SIZE+POLARSSL_PSK_MAX_LEN]
Definition: ssl.h:475
ssl_transform * transform
Definition: ssl.h:802
x509_crt * cert
Definition: ssl.h:716
size_t psk_identity_len
Definition: ssl.h:901
unsigned char * out_ctr
Definition: ssl.h:825
void ssl_handshake_wrapup(ssl_context *ssl)
int(* f_send)(void *, const unsigned char *, size_t)
Definition: ssl.h:759
size_t in_msglen
Definition: ssl.h:815
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) ...
unsigned char * in_hdr
Definition: ssl.h:809
int secure_renegotiation
Definition: ssl.h:923
time_t start
Definition: ssl.h:548
MPI structure.
Definition: bignum.h:182
sha512_context fin_sha512
Definition: ssl.h:667
int ssl_handshake_server_step(ssl_context *ssl)
SSL Ciphersuites for mbed TLS.
int(* tls_prf)(const unsigned char *, size_t, const char *, const unsigned char *, size_t, unsigned char *, size_t)
Definition: ssl.h:674
unsigned char mac_key[16]
Definition: ssl.h:706
void * p_vrfy
Definition: ssl.h:778
size_t psk_len
Definition: ssl.h:899
void ssl_set_max_version(ssl_context *ssl, int major, int minor)
Set the maximum supported version sent from the client side and/or accepted at the server side (Defau...
const ssl_ciphersuite_t * ciphersuite_info
Definition: ssl.h:589
void * p_recv
Definition: ssl.h:765
unsigned char * psk
Definition: ssl.h:898
int renego_max_records
Definition: ssl.h:872
Multi-precision integer library.
void ssl_set_ciphersuites_for_version(ssl_context *ssl, const int *ciphersuites, int major, int minor)
Set the list of allowed ciphersuites and the preference order for a specific version of the protocol...
void ssl_set_encrypt_then_mac(ssl_context *ssl, char etm)
Enable or disable Encrypt-then-MAC (Default: SSL_ETM_ENABLED)
int ssl_init(ssl_context *ssl)
Initialize an SSL context (An individual SSL context is not thread-safe)
int max_major_ver
Definition: ssl.h:737
int ssl_get_ciphersuite_id(const char *ciphersuite_name)
Return the ID of the ciphersuite associated with the given name.
int ssl_set_psk(ssl_context *ssl, const unsigned char *psk, size_t psk_len, const unsigned char *psk_identity, size_t psk_identity_len)
Set the Pre Shared Key (PSK) and the identity name connected to it.
char peer_verify_data[SSL_VERIFY_DATA_MAX_LEN]
Definition: ssl.h:928
void ssl_set_psk_cb(ssl_context *ssl, int(*f_psk)(void *, ssl_context *, const unsigned char *, size_t), void *p_psk)
Set the PSK callback (server-side only) (Optional).
unsigned int keylen
Definition: ssl.h:591
int ssl_get_session(const ssl_context *ssl, ssl_session *session)
Save session in order to resume it later (client-side only) Session data is copied to presented sessi...
md_type_t
Definition: md.h:48
int verify_result
Definition: ssl.h:559
const char * ssl_get_alpn_protocol(const ssl_context *ssl)
Get the name of the negotiated Application Layer Protocol.
const char ** alpn_list
Definition: ssl.h:916
int max_minor_ver
Definition: ssl.h:738
const char * alpn_chosen
Definition: ssl.h:917
void * p_hw_data
Definition: ssl.h:769
char arc4_disabled
Definition: ssl.h:751
unsigned char * in_ctr
Definition: ssl.h:808
ssl_handshake_params * handshake
Definition: ssl.h:794
void(* update_checksum)(ssl_context *, const unsigned char *, size_t)
Definition: ssl.h:671
size_t fixed_ivlen
Definition: ssl.h:594
int ssl_write_certificate(ssl_context *ssl)
size_t(* rsa_key_len_func)(void *ctx)
Definition: ssl.h:503
RSA context structure.
Definition: rsa.h:80
cipher_context_t cipher_ctx_dec
Definition: ssl.h:610
signed char split_done
Definition: ssl.h:841
#define POLARSSL_PSK_MAX_LEN
Definition: ssl.h:452
int in_msgtype
Definition: ssl.h:814
Container for an X.509 certificate.
Definition: x509_crt.h:53
unsigned char renego_period[8]
Definition: ssl.h:873
size_t verify_data_len
Definition: ssl.h:926
const char * ssl_get_ciphersuite(const ssl_context *ssl)
Return the name of the current ciphersuite.
mpi dhm_G
Definition: ssl.h:891
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...
int min_minor_ver
Definition: ssl.h:740
unsigned char * out_msg
Definition: ssl.h:828
unsigned char _pms_dhm[POLARSSL_MPI_MAX_SIZE]
Definition: ssl.h:462
int client_auth
Definition: ssl.h:868
void * p_dbg
Definition: ssl.h:764
ssl_key_cert * key_cert
Current key/cert or key/cert list.
Definition: ssl.h:648
void * p_send
Definition: ssl.h:766
ecdh_context ecdh_ctx
Definition: ssl.h:636
x509_crl * ca_crl
Definition: ssl.h:852
static x509_crt * ssl_own_cert(ssl_context *ssl)
Definition: ssl.h:1969
int ssl_set_max_frag_len(ssl_context *ssl, unsigned char mfl_code)
Set the maximum fragment length to emit and/or negotiate (Default: SSL_MAX_CONTENT_LEN, usually 2^14 bytes) (Server: set maximum fragment length to emit, usually negotiated by the client during handshake (Client: set maximum fragment length to emit and negotiate with the server during handshake)
SHA-512 context structure.
Definition: sha512.h:54
int ssl_handshake_client_step(ssl_context *ssl)
unsigned char _pms_ecdhe_psk[4+POLARSSL_ECP_MAX_BYTES+POLARSSL_PSK_MAX_LEN]
Definition: ssl.h:482
unsigned char * ticket
Definition: ssl.h:562
size_t maclen
Definition: ssl.h:595
int new_session_ticket
Definition: ssl.h:690
void ssl_set_fallback(ssl_context *ssl, char fallback)
Set the fallback flag (client-side only).
unsigned char * out_hdr
Definition: ssl.h:826
AES block cipher.
int ssl_set_own_cert(ssl_context *ssl, x509_crt *own_cert, pk_context *pk_key)
Set own certificate chain and private key.
int trunc_hmac
Definition: ssl.h:572
void ssl_set_endpoint(ssl_context *ssl, int endpoint)
Set the current endpoint type.
#define DEPRECATED
Definition: ssl.h:1223
void ssl_set_ciphersuites(ssl_context *ssl, const int *ciphersuites)
Set the list of allowed ciphersuites and the preference order.
Curve information for use by other modules.
Definition: ecp.h:84
int ssl_set_own_cert_alt(ssl_context *ssl, x509_crt *own_cert, void *rsa_key, rsa_decrypt_func rsa_decrypt, rsa_sign_func rsa_sign, rsa_key_len_func rsa_key_len) DEPRECATED
Set own certificate and external RSA private key and handling callbacks, such as the PKCS#11 wrappers...
int encrypt_then_mac
Definition: ssl.h:576
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 * p_get_cache
Definition: ssl.h:767
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.
unsigned char _pms_rsa[48]
Definition: ssl.h:459
unsigned char ssl_sig_from_pk(pk_context *pk)
void ssl_handshake_free(ssl_handshake_params *handshake)
Free referenced items in an SSL handshake context and clear memory.
int authmode
Definition: ssl.h:867
int ssl_flush_output(ssl_context *ssl)
int ssl_handshake(ssl_context *ssl)
Perform the SSL handshake.
unsigned char * in_offt
Definition: ssl.h:812
void ssl_set_min_version(ssl_context *ssl, int major, int minor)
Set the minimum accepted SSL/TLS protocol version (Default: SSL_MIN_MAJOR_VERSION, SSL_MIN_MINOR_VERSION)
char encrypt_then_mac
Definition: ssl.h:746
Diffie-Hellman-Merkle key exchange.
X.509 certificate parsing and writing.
int(* rsa_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition: ssl.h:499
unsigned char * in_msg
Definition: ssl.h:811
int ssl_set_hostname(ssl_context *ssl, const char *hostname)
Set hostname for ServerName TLS extension (client-side only)
aes_context dec
Definition: ssl.h:705
int ssl_handshake_step(ssl_context *ssl)
Perform a single step of the SSL handshake.
MD5 context structure.
Definition: md5.h:55
pk_type_t
Public key types.
Definition: pk.h:92
aes_context enc
Definition: ssl.h:704
unsigned char iv_dec[16]
Definition: ssl.h:598
unsigned char _pms_ecdh[POLARSSL_ECP_MAX_BYTES]
Definition: ssl.h:468
int ssl_parse_change_cipher_spec(ssl_context *ssl)
size_t hostname_len
Definition: ssl.h:909
Elliptic curve Diffie-Hellman.
int minor_ver
Definition: ssl.h:735
#define POLARSSL_ECP_MAX_BYTES
Definition: ecp.h:183
ECDH context structure.
Definition: ecdh.h:45
This structure is used for storing ciphersuite information.
int ssl_close_notify(ssl_context *ssl)
Notify the peer that the connection is being closed.
const x509_crt * ssl_get_peer_cert(const ssl_context *ssl)
Return the peer certificate from the current connection.
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 (except if...
size_t ssl_get_bytes_avail(const ssl_context *ssl)
Return the number of data bytes available to read.
void ssl_set_arc4_support(ssl_context *ssl, char arc4)
Disable or enable support for RC4 (Default: SSL_ARC4_ENABLED)
int 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...
size_t in_left
Definition: ssl.h:816
int session_tickets
Definition: ssl.h:885
pk_context * key
Definition: ssl.h:717
int allow_legacy_renegotiation
Definition: ssl.h:876
ssl_session * session_out
Definition: ssl.h:790
void ssl_set_renegotiation_period(ssl_context *ssl, const unsigned char period[8])
Set record counter threshold for periodic renegotiation.
unsigned char _pms_psk[4+2 *POLARSSL_PSK_MAX_LEN]
Definition: ssl.h:471
void(* calc_finished)(ssl_context *, unsigned char *, int)
Definition: ssl.h:673
int ssl_read_record(ssl_context *ssl)
ecp_group_id
Domain parameters (curve, subgroup and generator) identifiers.
Definition: ecp.h:54
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...
int(* f_vrfy)(void *, x509_crt *, int, int *)
Definition: ssl.h:777
int out_msgtype
Definition: ssl.h:830
void ssl_set_session_ticket_lifetime(ssl_context *ssl, int lifetime)
Set session ticket lifetime (server only) (Default: SSL_DEFAULT_TICKET_LIFETIME (86400 secs / 1 day))...
size_t out_left
Definition: ssl.h:832
unsigned char mac_dec[20]
Definition: ssl.h:603
SHA-1 cryptographic hash function.
md_context_t md_ctx_enc
Definition: ssl.h:606
int ssl_get_verify_result(const ssl_context *ssl)
Return the result of the certificate verification.
X.509 certificate revocation list parsing.
int ssl_session_reset(ssl_context *ssl)
Reset an already initialized SSL context for re-use while retaining application-set variables...
void ssl_session_init(ssl_session *session)
Initialize SSL session structure.
int min_major_ver
Definition: ssl.h:739
Certificate revocation list structure.
Definition: x509_crl.h:70
const int * ssl_list_ciphersuites(void)
Returns the list of ciphersuites supported by the SSL/TLS module.
SHA-384 and SHA-512 cryptographic hash function.
ssl_transform * transform_negotiate
Definition: ssl.h:803
int ssl_set_alpn_protocols(ssl_context *ssl, const char **protos)
Set the supported Application Layer Protocols.
unsigned char _pms_rsa_psk[52+POLARSSL_PSK_MAX_LEN]
Definition: ssl.h:478
uint32_t ticket_lifetime
Definition: ssl.h:564
void ssl_set_renegotiation_enforced(ssl_context *ssl, int max_records)
Enforce requested renegotiation.
unsigned char * in_iv
Definition: ssl.h:810
int disable_renegotiation
Definition: ssl.h:871
int verify_result
Definition: ssl.h:869
int ssl_write_change_cipher_spec(ssl_context *ssl)
int(* f_get_cache)(void *, ssl_session *)
Definition: ssl.h:760
int ssl_derive_keys(ssl_context *ssl)
void ssl_set_authmode(ssl_context *ssl, int authmode)
Set the certificate verification mode.
static pk_context * ssl_own_key(ssl_context *ssl)
Definition: ssl.h:1963
int(* f_set_cache)(void *, const ssl_session *)
Definition: ssl.h:761
SHA-256 context structure.
Definition: sha256.h:55
key_exchange_type_t
unsigned char mfl_code
Definition: ssl.h:568
int ssl_psk_derive_premaster(ssl_context *ssl, key_exchange_type_t key_ex)
int renegotiation
Definition: ssl.h:729
dhm_context dhm_ctx
Definition: ssl.h:633
static int safer_memcmp(const void *a, const void *b, size_t n)
Definition: ssl.h:1991
int ssl_send_fatal_handshake_failure(ssl_context *ssl)
ssl_ticket_keys * ticket_keys
Definition: ssl.h:860
size_t minlen
Definition: ssl.h:592
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.
unsigned char * psk_identity
Definition: ssl.h:900
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)
Initiate an SSL renegotiation on the running connection.
SHA-224 and SHA-256 cryptographic hash function.
int(* f_recv)(void *, unsigned char *, size_t)
Definition: ssl.h:758
unsigned char key_name[16]
Definition: ssl.h:703
int key_own_alloc
Definition: ssl.h:718
int ssl_write(ssl_context *ssl, const unsigned char *buf, size_t len)
Write exactly 'len' application data bytes.
ssl_key_cert * next
Definition: ssl.h:719
#define SSL_VERIFY_DATA_MAX_LEN
Definition: ssl.h:330
void ssl_set_ca_chain(ssl_context *ssl, x509_crt *ca_chain, x509_crl *ca_crl, const char *peer_cn)
Set the data required to verify peer certificate.
x509_crt * ca_chain
Definition: ssl.h:851
md5_context fin_md5
Definition: ssl.h:659
int endpoint
Definition: ssl.h:866
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(* f_psk)(void *, ssl_context *, const unsigned char *, size_t)
Definition: ssl.h:782
int ssl_write_record(ssl_context *ssl)
Public key container.
Definition: pk.h:194
unsigned char * out_iv
Definition: ssl.h:827
unsigned char randbytes[64]
Definition: ssl.h:680
char own_verify_data[SSL_VERIFY_DATA_MAX_LEN]
Definition: ssl.h:927
int(* rsa_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Definition: ssl.h:496
Generic message digest context.
Definition: md.h:129
void ssl_optimize_checksum(ssl_context *ssl, const ssl_ciphersuite_t *ciphersuite_info)
int ssl_set_own_cert_rsa(ssl_context *ssl, x509_crt *own_cert, rsa_context *rsa_key) DEPRECATED
Set own certificate chain and private RSA key.
x509_crt * peer_cert
Definition: ssl.h:557
md_type_t ssl_md_alg_from_hash(unsigned char hash)
void * p_sni
Definition: ssl.h:773