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_SESSION_FOUND -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
98 #define SSL_MAJOR_VERSION_3 3
99 #define SSL_MINOR_VERSION_0 0
100 #define SSL_MINOR_VERSION_1 1
101 #define SSL_MINOR_VERSION_2 2
102 #define SSL_MINOR_VERSION_3 3
104 #define SSL_IS_CLIENT 0
105 #define SSL_IS_SERVER 1
106 #define SSL_COMPRESS_NULL 0
107 #define SSL_COMPRESS_DEFLATE 1
109 #define SSL_VERIFY_NONE 0
110 #define SSL_VERIFY_OPTIONAL 1
111 #define SSL_VERIFY_REQUIRED 2
113 #define SSL_INITIAL_HANDSHAKE 0
114 #define SSL_RENEGOTIATION 1
116 #define SSL_LEGACY_RENEGOTIATION 0
117 #define SSL_SECURE_RENEGOTIATION 1
119 #define SSL_RENEGOTIATION_DISABLED 0
120 #define SSL_RENEGOTIATION_ENABLED 1
122 #define SSL_LEGACY_NO_RENEGOTIATION 0
123 #define SSL_LEGACY_ALLOW_RENEGOTIATION 1
124 #define SSL_LEGACY_BREAK_HANDSHAKE 2
133 #if !defined(POLARSSL_CONFIG_OPTIONS)
134 #define SSL_MAX_CONTENT_LEN 16384
143 #if defined(POLARSSL_ZLIB_SUPPORT)
144 #define SSL_COMPRESSION_ADD 1024
146 #define SSL_COMPRESSION_ADD 0
149 #define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + SSL_COMPRESSION_ADD + 512)
154 #define TLS_RSA_WITH_NULL_MD5 0x01
155 #define TLS_RSA_WITH_NULL_SHA 0x02
156 #define TLS_RSA_WITH_NULL_SHA256 0x3B
157 #define TLS_RSA_WITH_DES_CBC_SHA 0x09
158 #define TLS_DHE_RSA_WITH_DES_CBC_SHA 0x15
160 #define TLS_RSA_WITH_RC4_128_MD5 0x04
161 #define TLS_RSA_WITH_RC4_128_SHA 0x05
163 #define TLS_RSA_WITH_3DES_EDE_CBC_SHA 0x0A
164 #define TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x16
166 #define TLS_RSA_WITH_AES_128_CBC_SHA 0x2F
167 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x33
168 #define TLS_RSA_WITH_AES_256_CBC_SHA 0x35
169 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x39
170 #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x3C
171 #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x3D
172 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x67
173 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x6B
175 #define TLS_RSA_WITH_CAMELLIA_128_CBC_SHA 0x41
176 #define TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA 0x45
177 #define TLS_RSA_WITH_CAMELLIA_256_CBC_SHA 0x84
178 #define TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA 0x88
179 #define TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBA
180 #define TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 0xBE
181 #define TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC0
182 #define TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 0xC4
184 #define TLS_RSA_WITH_AES_128_GCM_SHA256 0x9C
185 #define TLS_RSA_WITH_AES_256_GCM_SHA384 0x9D
186 #define TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x9E
187 #define TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x9F
189 #define SSL_EMPTY_RENEGOTIATION_INFO 0xFF
194 #define SSL_HASH_NONE 0
195 #define SSL_HASH_MD5 1
196 #define SSL_HASH_SHA1 2
197 #define SSL_HASH_SHA224 3
198 #define SSL_HASH_SHA256 4
199 #define SSL_HASH_SHA384 5
200 #define SSL_HASH_SHA512 6
202 #define SSL_SIG_RSA 1
207 #define SSL_CERT_TYPE_RSA_SIGN 1
212 #define SSL_MSG_CHANGE_CIPHER_SPEC 20
213 #define SSL_MSG_ALERT 21
214 #define SSL_MSG_HANDSHAKE 22
215 #define SSL_MSG_APPLICATION_DATA 23
217 #define SSL_ALERT_LEVEL_WARNING 1
218 #define SSL_ALERT_LEVEL_FATAL 2
220 #define SSL_ALERT_MSG_CLOSE_NOTIFY 0
221 #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
222 #define SSL_ALERT_MSG_BAD_RECORD_MAC 20
223 #define SSL_ALERT_MSG_DECRYPTION_FAILED 21
224 #define SSL_ALERT_MSG_RECORD_OVERFLOW 22
225 #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
226 #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
227 #define SSL_ALERT_MSG_NO_CERT 41
228 #define SSL_ALERT_MSG_BAD_CERT 42
229 #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
230 #define SSL_ALERT_MSG_CERT_REVOKED 44
231 #define SSL_ALERT_MSG_CERT_EXPIRED 45
232 #define SSL_ALERT_MSG_CERT_UNKNOWN 46
233 #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
234 #define SSL_ALERT_MSG_UNKNOWN_CA 48
235 #define SSL_ALERT_MSG_ACCESS_DENIED 49
236 #define SSL_ALERT_MSG_DECODE_ERROR 50
237 #define SSL_ALERT_MSG_DECRYPT_ERROR 51
238 #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
239 #define SSL_ALERT_MSG_PROTOCOL_VERSION 70
240 #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
241 #define SSL_ALERT_MSG_INTERNAL_ERROR 80
242 #define SSL_ALERT_MSG_USER_CANCELED 90
243 #define SSL_ALERT_MSG_NO_RENEGOTIATION 100
244 #define SSL_ALERT_MSG_UNSUPPORTED_EXT 110
245 #define SSL_ALERT_MSG_UNRECOGNIZED_NAME 112
247 #define SSL_HS_HELLO_REQUEST 0
248 #define SSL_HS_CLIENT_HELLO 1
249 #define SSL_HS_SERVER_HELLO 2
250 #define SSL_HS_CERTIFICATE 11
251 #define SSL_HS_SERVER_KEY_EXCHANGE 12
252 #define SSL_HS_CERTIFICATE_REQUEST 13
253 #define SSL_HS_SERVER_HELLO_DONE 14
254 #define SSL_HS_CERTIFICATE_VERIFY 15
255 #define SSL_HS_CLIENT_KEY_EXCHANGE 16
256 #define SSL_HS_FINISHED 20
261 #define TLS_EXT_SERVERNAME 0
262 #define TLS_EXT_SERVERNAME_HOSTNAME 0
264 #define TLS_EXT_SIG_ALG 13
266 #define TLS_EXT_RENEGOTIATION_INFO 0xFF01
274 int (*f_rng)(
void *,
unsigned char *, size_t),
275 void *p_rng,
int mode,
size_t *olen,
276 const unsigned char *input,
unsigned char *output,
277 size_t output_max_len );
279 int (*f_rng)(
void *,
unsigned char *, size_t),
void *p_rng,
280 int mode,
int hash_id,
unsigned int hashlen,
281 const unsigned char *hash,
unsigned char *sig );
323 unsigned char id[32];
355 #if defined(POLARSSL_ZLIB_SUPPORT)
356 z_stream ctx_deflate;
357 z_stream ctx_inflate;
372 #if defined(POLARSSL_DHM_C)
387 int (*
tls_prf)(
unsigned char *, size_t,
char *,
388 unsigned char *, size_t,
389 unsigned char *, size_t);
419 int (*
f_rng)(
void *,
unsigned char *, size_t);
420 void (*
f_dbg)(
void *, int,
const char *);
421 int (*
f_recv)(
void *,
unsigned char *, size_t);
422 int (*
f_send)(
void *,
const unsigned char *, size_t);
507 #if defined(POLARSSL_DHM_C)
534 #if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
536 const unsigned char *key_enc,
const unsigned char *key_dec,
537 const unsigned char *iv_enc,
const unsigned char *iv_dec,
538 const unsigned char *mac_enc,
const unsigned char *mac_dec);
539 extern int (*ssl_hw_record_reset)(
ssl_context *ssl);
540 extern int (*ssl_hw_record_write)(
ssl_context *ssl);
541 extern int (*ssl_hw_record_read)(
ssl_context *ssl);
542 extern int (*ssl_hw_record_finish)(
ssl_context *ssl);
637 int (*f_vrfy)(
void *,
x509_cert *,
int,
int *),
648 int (*f_rng)(
void *,
unsigned char *,
size_t),
659 void (*f_dbg)(
void *,
int,
const char *),
672 int (*f_recv)(
void *,
unsigned char *,
size_t),
void *p_recv,
673 int (*f_send)(
void *,
const unsigned char *,
size_t),
void *p_send );
713 int (*f_get_cache)(
void *,
ssl_session *),
void *p_get_cache,
714 int (*f_set_cache)(
void *,
const ssl_session *),
void *p_set_cache );
755 const int *ciphersuites,
756 int major,
int minor );
767 x509_crl *ca_crl,
const char *peer_cn );
808 #if defined(POLARSSL_DHM_C)
866 int (*f_sni)(
void *,
ssl_context *,
const unsigned char *,
1066 unsigned char level,
1067 unsigned char message );
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.
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]