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