Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file ssl.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief SSL/TLS functions. |
| 5 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_SSL_H |
| 28 | #define POLARSSL_SSL_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | #include <time.h> |
| 31 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 32 | #include "polarssl/net.h" |
| 33 | #include "polarssl/dhm.h" |
| 34 | #include "polarssl/rsa.h" |
| 35 | #include "polarssl/md5.h" |
| 36 | #include "polarssl/sha1.h" |
| 37 | #include "polarssl/x509.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 39 | /* |
| 40 | * SSL Error codes |
| 41 | */ |
Paul Bakker | 3391b12 | 2009-07-28 20:11:54 +0000 | [diff] [blame] | 42 | #define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x1000 |
| 43 | #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x1800 |
| 44 | #define POLARSSL_ERR_SSL_INVALID_MAC -0x2000 |
| 45 | #define POLARSSL_ERR_SSL_INVALID_RECORD -0x2800 |
| 46 | #define POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE -0x3000 |
| 47 | #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x3800 |
| 48 | #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x4000 |
| 49 | #define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x4800 |
| 50 | #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x5000 |
| 51 | #define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x5800 |
| 52 | #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x6000 |
| 53 | #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x6800 |
| 54 | #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7000 |
| 55 | #define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7800 |
| 56 | #define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x8000 |
| 57 | #define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x8800 |
| 58 | #define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x9000 |
| 59 | #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x9800 |
| 60 | #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0xA000 |
| 61 | #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0xA800 |
| 62 | #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0xB000 |
| 63 | #define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0xB800 |
| 64 | #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0xC000 |
| 65 | #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0xC800 |
| 66 | #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0xD000 |
| 67 | #define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0xD800 |
| 68 | #define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0xE000 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Various constants |
| 72 | */ |
| 73 | #define SSL_MAJOR_VERSION_3 3 |
| 74 | #define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */ |
| 75 | #define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */ |
| 76 | #define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */ |
| 77 | |
| 78 | #define SSL_IS_CLIENT 0 |
| 79 | #define SSL_IS_SERVER 1 |
| 80 | #define SSL_COMPRESS_NULL 0 |
| 81 | |
| 82 | #define SSL_VERIFY_NONE 0 |
| 83 | #define SSL_VERIFY_OPTIONAL 1 |
| 84 | #define SSL_VERIFY_REQUIRED 2 |
| 85 | |
| 86 | #define SSL_MAX_CONTENT_LEN 16384 |
| 87 | |
| 88 | /* |
| 89 | * Allow an extra 512 bytes for the record header |
| 90 | * and encryption overhead (counter + MAC + padding). |
| 91 | */ |
| 92 | #define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512) |
| 93 | |
| 94 | /* |
| 95 | * Supported ciphersuites |
| 96 | */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 97 | #define SSL_RSA_RC4_128_MD5 0x04 |
| 98 | #define SSL_RSA_RC4_128_SHA 0x05 |
| 99 | #define SSL_RSA_DES_168_SHA 0x0A |
| 100 | #define SSL_EDH_RSA_DES_168_SHA 0x16 |
| 101 | #define SSL_RSA_AES_128_SHA 0x2F |
| 102 | #define SSL_EDH_RSA_AES_128_SHA 0x33 |
| 103 | #define SSL_RSA_AES_256_SHA 0x35 |
| 104 | #define SSL_EDH_RSA_AES_256_SHA 0x39 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 106 | #define SSL_RSA_CAMELLIA_128_SHA 0x41 |
Paul Bakker | 77a4358 | 2010-06-15 21:32:46 +0000 | [diff] [blame] | 107 | #define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45 |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 108 | #define SSL_RSA_CAMELLIA_256_SHA 0x84 |
| 109 | #define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88 |
Paul Bakker | b5ef0ba | 2009-01-11 20:25:36 +0000 | [diff] [blame] | 110 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Message, alert and handshake types |
| 113 | */ |
| 114 | #define SSL_MSG_CHANGE_CIPHER_SPEC 20 |
| 115 | #define SSL_MSG_ALERT 21 |
| 116 | #define SSL_MSG_HANDSHAKE 22 |
| 117 | #define SSL_MSG_APPLICATION_DATA 23 |
| 118 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 119 | #define SSL_ALERT_LEVEL_WARNING 1 |
| 120 | #define SSL_ALERT_LEVEL_FATAL 2 |
| 121 | |
| 122 | #define SSL_ALERT_MSG_CLOSE_NOTIFY 0 |
| 123 | #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 |
| 124 | #define SSL_ALERT_MSG_BAD_RECORD_MAD 20 |
| 125 | #define SSL_ALERT_MSG_DECRYPTION_FAILED 21 |
| 126 | #define SSL_ALERT_MSG_RECORD_OVERFLOW 22 |
| 127 | #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 |
| 128 | #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 |
| 129 | #define SSL_ALERT_MSG_NO_CERT 41 |
| 130 | #define SSL_ALERT_MSG_BAD_CERT 42 |
| 131 | #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43 |
| 132 | #define SSL_ALERT_MSG_CERT_REVOKED 44 |
| 133 | #define SSL_ALERT_MSG_CERT_EXPIRED 45 |
| 134 | #define SSL_ALERT_MSG_CERT_UNKNOWN 46 |
| 135 | #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 |
| 136 | #define SSL_ALERT_MSG_UNKNOWN_CA 48 |
| 137 | #define SSL_ALERT_MSG_ACCESS_DENIED 49 |
| 138 | #define SSL_ALERT_MSG_DECODE_ERROR 50 |
| 139 | #define SSL_ALERT_MSG_DECRYPT_ERROR 51 |
| 140 | #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60 |
| 141 | #define SSL_ALERT_MSG_PROTOCOL_VERSION 70 |
| 142 | #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 |
| 143 | #define SSL_ALERT_MSG_INTERNAL_ERROR 80 |
| 144 | #define SSL_ALERT_MSG_USER_CANCELED 90 |
| 145 | #define SSL_ALERT_MSG_NO_RENEGOTIATION 100 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | |
| 147 | #define SSL_HS_HELLO_REQUEST 0 |
| 148 | #define SSL_HS_CLIENT_HELLO 1 |
| 149 | #define SSL_HS_SERVER_HELLO 2 |
| 150 | #define SSL_HS_CERTIFICATE 11 |
| 151 | #define SSL_HS_SERVER_KEY_EXCHANGE 12 |
| 152 | #define SSL_HS_CERTIFICATE_REQUEST 13 |
| 153 | #define SSL_HS_SERVER_HELLO_DONE 14 |
| 154 | #define SSL_HS_CERTIFICATE_VERIFY 15 |
| 155 | #define SSL_HS_CLIENT_KEY_EXCHANGE 16 |
| 156 | #define SSL_HS_FINISHED 20 |
| 157 | |
| 158 | /* |
| 159 | * TLS extensions |
| 160 | */ |
| 161 | #define TLS_EXT_SERVERNAME 0 |
| 162 | #define TLS_EXT_SERVERNAME_HOSTNAME 0 |
| 163 | |
| 164 | /* |
| 165 | * SSL state machine |
| 166 | */ |
| 167 | typedef enum |
| 168 | { |
| 169 | SSL_HELLO_REQUEST, |
| 170 | SSL_CLIENT_HELLO, |
| 171 | SSL_SERVER_HELLO, |
| 172 | SSL_SERVER_CERTIFICATE, |
| 173 | SSL_SERVER_KEY_EXCHANGE, |
| 174 | SSL_CERTIFICATE_REQUEST, |
| 175 | SSL_SERVER_HELLO_DONE, |
| 176 | SSL_CLIENT_CERTIFICATE, |
| 177 | SSL_CLIENT_KEY_EXCHANGE, |
| 178 | SSL_CERTIFICATE_VERIFY, |
| 179 | SSL_CLIENT_CHANGE_CIPHER_SPEC, |
| 180 | SSL_CLIENT_FINISHED, |
| 181 | SSL_SERVER_CHANGE_CIPHER_SPEC, |
| 182 | SSL_SERVER_FINISHED, |
| 183 | SSL_FLUSH_BUFFERS, |
| 184 | SSL_HANDSHAKE_OVER |
| 185 | } |
| 186 | ssl_states; |
| 187 | |
| 188 | typedef struct _ssl_session ssl_session; |
| 189 | typedef struct _ssl_context ssl_context; |
| 190 | |
| 191 | /* |
| 192 | * This structure is used for session resuming. |
| 193 | */ |
| 194 | struct _ssl_session |
| 195 | { |
| 196 | time_t start; /*!< starting time */ |
| 197 | int cipher; /*!< chosen cipher */ |
| 198 | int length; /*!< session id length */ |
| 199 | unsigned char id[32]; /*!< session identifier */ |
| 200 | unsigned char master[48]; /*!< the master secret */ |
| 201 | ssl_session *next; /*!< next session entry */ |
| 202 | }; |
| 203 | |
| 204 | struct _ssl_context |
| 205 | { |
| 206 | /* |
| 207 | * Miscellaneous |
| 208 | */ |
| 209 | int state; /*!< SSL handshake: current state */ |
| 210 | |
| 211 | int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */ |
| 212 | int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */ |
| 213 | |
| 214 | int max_major_ver; /*!< max. major version from client */ |
| 215 | int max_minor_ver; /*!< max. minor version from client */ |
| 216 | |
| 217 | /* |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 218 | * Callbacks (RNG, debug, I/O, verification) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | */ |
| 220 | int (*f_rng)(void *); |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 221 | void (*f_dbg)(void *, int, const char *); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 222 | int (*f_recv)(void *, unsigned char *, int); |
| 223 | int (*f_send)(void *, unsigned char *, int); |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 224 | int (*f_vrfy)(void *, x509_cert *, int, int); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | |
| 226 | void *p_rng; /*!< context for the RNG function */ |
| 227 | void *p_dbg; /*!< context for the debug function */ |
| 228 | void *p_recv; /*!< context for reading operations */ |
| 229 | void *p_send; /*!< context for writing operations */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 230 | void *p_vrfy; /*!< context for verification */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * Session layer |
| 234 | */ |
| 235 | int resume; /*!< session resuming flag */ |
| 236 | int timeout; /*!< sess. expiration time */ |
| 237 | ssl_session *session; /*!< current session data */ |
| 238 | int (*s_get)(ssl_context *); /*!< (server) get callback */ |
| 239 | int (*s_set)(ssl_context *); /*!< (server) set callback */ |
| 240 | |
| 241 | /* |
| 242 | * Record layer (incoming data) |
| 243 | */ |
| 244 | unsigned char *in_ctr; /*!< 64-bit incoming message counter */ |
| 245 | unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */ |
| 246 | unsigned char *in_msg; /*!< the message contents (in_hdr+5) */ |
| 247 | unsigned char *in_offt; /*!< read offset in application data */ |
| 248 | |
| 249 | int in_msgtype; /*!< record header: message type */ |
| 250 | int in_msglen; /*!< record header: message length */ |
| 251 | int in_left; /*!< amount of data read so far */ |
| 252 | |
| 253 | int in_hslen; /*!< current handshake message length */ |
| 254 | int nb_zero; /*!< # of 0-length encrypted messages */ |
| 255 | |
| 256 | /* |
| 257 | * Record layer (outgoing data) |
| 258 | */ |
| 259 | unsigned char *out_ctr; /*!< 64-bit outgoing message counter */ |
| 260 | unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */ |
| 261 | unsigned char *out_msg; /*!< the message contents (out_hdr+5) */ |
| 262 | |
| 263 | int out_msgtype; /*!< record header: message type */ |
| 264 | int out_msglen; /*!< record header: message length */ |
| 265 | int out_left; /*!< amount of data not yet written */ |
| 266 | |
| 267 | /* |
| 268 | * PKI layer |
| 269 | */ |
| 270 | rsa_context *rsa_key; /*!< own RSA private key */ |
| 271 | x509_cert *own_cert; /*!< own X.509 certificate */ |
| 272 | x509_cert *ca_chain; /*!< own trusted CA chain */ |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 273 | x509_crl *ca_crl; /*!< trusted CA CRLs */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 274 | x509_cert *peer_cert; /*!< peer X.509 cert chain */ |
Paul Bakker | 57b7914 | 2010-03-24 06:51:15 +0000 | [diff] [blame] | 275 | const char *peer_cn; /*!< expected peer CN */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 | |
| 277 | int endpoint; /*!< 0: client, 1: server */ |
| 278 | int authmode; /*!< verification mode */ |
| 279 | int client_auth; /*!< flag for client auth. */ |
| 280 | int verify_result; /*!< verification result */ |
| 281 | |
| 282 | /* |
| 283 | * Crypto layer |
| 284 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 285 | dhm_context dhm_ctx; /*!< DHM key exchange */ |
| 286 | md5_context fin_md5; /*!< Finished MD5 checksum */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 287 | sha1_context fin_sha1; /*!< Finished SHA-1 checksum */ |
| 288 | |
| 289 | int do_crypt; /*!< en(de)cryption flag */ |
| 290 | int *ciphers; /*!< allowed ciphersuites */ |
| 291 | int pmslen; /*!< premaster length */ |
| 292 | int keylen; /*!< symmetric key length */ |
| 293 | int minlen; /*!< min. ciphertext length */ |
| 294 | int ivlen; /*!< IV length */ |
| 295 | int maclen; /*!< MAC length */ |
| 296 | |
| 297 | unsigned char randbytes[64]; /*!< random bytes */ |
| 298 | unsigned char premaster[256]; /*!< premaster secret */ |
| 299 | |
| 300 | unsigned char iv_enc[16]; /*!< IV (encryption) */ |
| 301 | unsigned char iv_dec[16]; /*!< IV (decryption) */ |
| 302 | |
| 303 | unsigned char mac_enc[32]; /*!< MAC (encryption) */ |
| 304 | unsigned char mac_dec[32]; /*!< MAC (decryption) */ |
| 305 | |
| 306 | unsigned long ctx_enc[128]; /*!< encryption context */ |
| 307 | unsigned long ctx_dec[128]; /*!< decryption context */ |
| 308 | |
| 309 | /* |
| 310 | * TLS extensions |
| 311 | */ |
| 312 | unsigned char *hostname; |
| 313 | unsigned long hostname_len; |
| 314 | }; |
| 315 | |
| 316 | #ifdef __cplusplus |
| 317 | extern "C" { |
| 318 | #endif |
| 319 | |
| 320 | extern int ssl_default_ciphers[]; |
| 321 | |
| 322 | /** |
| 323 | * \brief Initialize an SSL context |
| 324 | * |
| 325 | * \param ssl SSL context |
| 326 | * |
| 327 | * \return 0 if successful, or 1 if memory allocation failed |
| 328 | */ |
| 329 | int ssl_init( ssl_context *ssl ); |
| 330 | |
| 331 | /** |
| 332 | * \brief Set the current endpoint type |
| 333 | * |
| 334 | * \param ssl SSL context |
| 335 | * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER |
| 336 | */ |
| 337 | void ssl_set_endpoint( ssl_context *ssl, int endpoint ); |
| 338 | |
| 339 | /** |
| 340 | * \brief Set the certificate verification mode |
| 341 | * |
| 342 | * \param ssl SSL context |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 343 | * \param authmode can be: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | * |
| 345 | * SSL_VERIFY_NONE: peer certificate is not checked (default), |
| 346 | * this is insecure and SHOULD be avoided. |
| 347 | * |
| 348 | * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the |
| 349 | * handshake continues even if verification failed; |
| 350 | * ssl_get_verify_result() can be called after the |
| 351 | * handshake is complete. |
| 352 | * |
| 353 | * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate, |
| 354 | * handshake is aborted if verification failed. |
| 355 | */ |
| 356 | void ssl_set_authmode( ssl_context *ssl, int authmode ); |
| 357 | |
| 358 | /** |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 359 | * \brief Set the verification callback (Optional). |
| 360 | * |
| 361 | * If set, the verification callback is called once for every |
| 362 | * certificate in the chain. The verification function has the |
| 363 | * following parameter: (void *parameter, x509_cert certificate, |
| 364 | * int certifcate_depth, int preverify_ok). It should |
| 365 | * return 0 on SUCCESS. |
| 366 | * |
| 367 | * \param ssl SSL context |
| 368 | * \param f_vrfy verification function |
| 369 | * \param p_vrfy verification parameter |
| 370 | */ |
| 371 | void ssl_set_verify( ssl_context *ssl, |
| 372 | int (*f_vrfy)(void *, x509_cert *, int, int), |
| 373 | void *p_vrfy ); |
| 374 | |
| 375 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | * \brief Set the random number generator callback |
| 377 | * |
| 378 | * \param ssl SSL context |
| 379 | * \param f_rng RNG function |
| 380 | * \param p_rng RNG parameter |
| 381 | */ |
| 382 | void ssl_set_rng( ssl_context *ssl, |
| 383 | int (*f_rng)(void *), |
| 384 | void *p_rng ); |
| 385 | |
| 386 | /** |
| 387 | * \brief Set the debug callback |
| 388 | * |
| 389 | * \param ssl SSL context |
| 390 | * \param f_dbg debug function |
| 391 | * \param p_dbg debug parameter |
| 392 | */ |
| 393 | void ssl_set_dbg( ssl_context *ssl, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 394 | void (*f_dbg)(void *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 395 | void *p_dbg ); |
| 396 | |
| 397 | /** |
| 398 | * \brief Set the underlying BIO read and write callbacks |
| 399 | * |
| 400 | * \param ssl SSL context |
| 401 | * \param f_recv read callback |
| 402 | * \param p_recv read parameter |
| 403 | * \param f_send write callback |
| 404 | * \param p_send write parameter |
| 405 | */ |
| 406 | void ssl_set_bio( ssl_context *ssl, |
| 407 | int (*f_recv)(void *, unsigned char *, int), void *p_recv, |
| 408 | int (*f_send)(void *, unsigned char *, int), void *p_send ); |
| 409 | |
| 410 | /** |
| 411 | * \brief Set the session callbacks (server-side only) |
| 412 | * |
| 413 | * \param ssl SSL context |
| 414 | * \param s_get session get callback |
| 415 | * \param s_set session set callback |
| 416 | */ |
| 417 | void ssl_set_scb( ssl_context *ssl, |
| 418 | int (*s_get)(ssl_context *), |
| 419 | int (*s_set)(ssl_context *) ); |
| 420 | |
| 421 | /** |
| 422 | * \brief Set the session resuming flag, timeout and data |
| 423 | * |
| 424 | * \param ssl SSL context |
| 425 | * \param resume if 0 (default), the session will not be resumed |
| 426 | * \param timeout session timeout in seconds, or 0 (no timeout) |
| 427 | * \param session session context |
| 428 | */ |
| 429 | void ssl_set_session( ssl_context *ssl, int resume, int timeout, |
| 430 | ssl_session *session ); |
| 431 | |
| 432 | /** |
| 433 | * \brief Set the list of allowed ciphersuites |
| 434 | * |
| 435 | * \param ssl SSL context |
| 436 | * \param ciphers 0-terminated list of allowed ciphers |
| 437 | */ |
| 438 | void ssl_set_ciphers( ssl_context *ssl, int *ciphers ); |
| 439 | |
| 440 | /** |
| 441 | * \brief Set the data required to verify peer certificate |
| 442 | * |
| 443 | * \param ssl SSL context |
| 444 | * \param ca_chain trusted CA chain |
Paul Bakker | 40ea7de | 2009-05-03 10:18:48 +0000 | [diff] [blame] | 445 | * \param ca_crl trusted CA CRLs |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | * \param peer_cn expected peer CommonName (or NULL) |
| 447 | * |
| 448 | * \note TODO: add two more parameters: depth and crl |
| 449 | */ |
| 450 | void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain, |
Paul Bakker | 57b7914 | 2010-03-24 06:51:15 +0000 | [diff] [blame] | 451 | x509_crl *ca_crl, const char *peer_cn ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * \brief Set own certificate and private key |
| 455 | * |
| 456 | * \param ssl SSL context |
| 457 | * \param own_cert own public certificate |
| 458 | * \param rsa_key own private RSA key |
| 459 | */ |
| 460 | void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert, |
| 461 | rsa_context *rsa_key ); |
| 462 | |
| 463 | /** |
| 464 | * \brief Set the Diffie-Hellman public P and G values, |
| 465 | * read as hexadecimal strings (server-side only) |
| 466 | * |
| 467 | * \param ssl SSL context |
| 468 | * \param dhm_P Diffie-Hellman-Merkle modulus |
| 469 | * \param dhm_G Diffie-Hellman-Merkle generator |
| 470 | * |
| 471 | * \return 0 if successful |
| 472 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 473 | int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 474 | |
| 475 | /** |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 476 | * \brief Set the Diffie-Hellman public P and G values, |
| 477 | * read from existing context (server-side only) |
| 478 | * |
| 479 | * \param ssl SSL context |
| 480 | * \param dhm_ctx Diffie-Hellman-Merkle context |
| 481 | * |
| 482 | * \return 0 if successful |
| 483 | */ |
| 484 | int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx ); |
| 485 | |
| 486 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | * \brief Set hostname for ServerName TLS Extension |
| 488 | * |
| 489 | * |
| 490 | * \param ssl SSL context |
| 491 | * \param hostname the server hostname |
| 492 | * |
| 493 | * \return 0 if successful |
| 494 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 495 | int ssl_set_hostname( ssl_context *ssl, const char *hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * \brief Return the number of data bytes available to read |
| 499 | * |
| 500 | * \param ssl SSL context |
| 501 | * |
| 502 | * \return how many bytes are available in the read buffer |
| 503 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 504 | int ssl_get_bytes_avail( const ssl_context *ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | |
| 506 | /** |
| 507 | * \brief Return the result of the certificate verification |
| 508 | * |
| 509 | * \param ssl SSL context |
| 510 | * |
| 511 | * \return 0 if successful, or a combination of: |
| 512 | * BADCERT_EXPIRED |
| 513 | * BADCERT_REVOKED |
| 514 | * BADCERT_CN_MISMATCH |
| 515 | * BADCERT_NOT_TRUSTED |
| 516 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 517 | int ssl_get_verify_result( const ssl_context *ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * \brief Return the name of the current cipher |
| 521 | * |
| 522 | * \param ssl SSL context |
| 523 | * |
| 524 | * \return a string containing the cipher name |
| 525 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 526 | const char *ssl_get_cipher( const ssl_context *ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * \brief Perform the SSL handshake |
| 530 | * |
| 531 | * \param ssl SSL context |
| 532 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 533 | * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 534 | * or a specific SSL error code. |
| 535 | */ |
| 536 | int ssl_handshake( ssl_context *ssl ); |
| 537 | |
| 538 | /** |
| 539 | * \brief Read at most 'len' application data bytes |
| 540 | * |
| 541 | * \param ssl SSL context |
| 542 | * \param buf buffer that will hold the data |
| 543 | * \param len how many bytes must be read |
| 544 | * |
| 545 | * \return This function returns the number of bytes read, |
| 546 | * or a negative error code. |
| 547 | */ |
| 548 | int ssl_read( ssl_context *ssl, unsigned char *buf, int len ); |
| 549 | |
| 550 | /** |
| 551 | * \brief Write exactly 'len' application data bytes |
| 552 | * |
| 553 | * \param ssl SSL context |
| 554 | * \param buf buffer holding the data |
| 555 | * \param len how many bytes must be written |
| 556 | * |
| 557 | * \return This function returns the number of bytes written, |
| 558 | * or a negative error code. |
| 559 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 560 | * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 561 | * it must be called later with the *same* arguments, |
| 562 | * until it returns a positive value. |
| 563 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 564 | int ssl_write( ssl_context *ssl, const unsigned char *buf, int len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 565 | |
| 566 | /** |
| 567 | * \brief Notify the peer that the connection is being closed |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 568 | * |
| 569 | * \param ssl SSL context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 570 | */ |
| 571 | int ssl_close_notify( ssl_context *ssl ); |
| 572 | |
| 573 | /** |
| 574 | * \brief Free an SSL context |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 575 | * |
| 576 | * \param ssl SSL context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | */ |
| 578 | void ssl_free( ssl_context *ssl ); |
| 579 | |
| 580 | /* |
| 581 | * Internal functions (do not call directly) |
| 582 | */ |
| 583 | int ssl_handshake_client( ssl_context *ssl ); |
| 584 | int ssl_handshake_server( ssl_context *ssl ); |
| 585 | |
| 586 | int ssl_derive_keys( ssl_context *ssl ); |
| 587 | void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] ); |
| 588 | |
| 589 | int ssl_read_record( ssl_context *ssl ); |
| 590 | int ssl_fetch_input( ssl_context *ssl, int nb_want ); |
| 591 | |
| 592 | int ssl_write_record( ssl_context *ssl ); |
| 593 | int ssl_flush_output( ssl_context *ssl ); |
| 594 | |
| 595 | int ssl_parse_certificate( ssl_context *ssl ); |
| 596 | int ssl_write_certificate( ssl_context *ssl ); |
| 597 | |
| 598 | int ssl_parse_change_cipher_spec( ssl_context *ssl ); |
| 599 | int ssl_write_change_cipher_spec( ssl_context *ssl ); |
| 600 | |
| 601 | int ssl_parse_finished( ssl_context *ssl ); |
| 602 | int ssl_write_finished( ssl_context *ssl ); |
| 603 | |
| 604 | #ifdef __cplusplus |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | #endif /* ssl.h */ |