blob: 715a4e886faa878d18d4813a308088681573d620 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file ssl.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SSL/TLS functions.
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * 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 Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_SSL_H
28#define POLARSSL_SSL_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <time.h>
31
Paul Bakker8e831ed2009-01-03 21:24:11 +000032#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 Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker13e2dfe2009-07-28 07:18:38 +000039/*
40 * SSL Error codes
41 */
Paul Bakker3391b122009-07-28 20:11:54 +000042#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 Bakker5121ce52009-01-03 21:22:43 +000069
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 Bakker2e11f7d2010-07-25 14:24:53 +000097#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 Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakkerff60ee62010-03-16 21:09:09 +0000106#define SSL_RSA_CAMELLIA_128_SHA 0x41
Paul Bakker77a43582010-06-15 21:32:46 +0000107#define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
Paul Bakkerff60ee62010-03-16 21:09:09 +0000108#define SSL_RSA_CAMELLIA_256_SHA 0x84
109#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111/*
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 Bakker2e11f7d2010-07-25 14:24:53 +0000119#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 Bakker5121ce52009-01-03 21:22:43 +0000146
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 */
167typedef 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}
186ssl_states;
187
188typedef struct _ssl_session ssl_session;
189typedef struct _ssl_context ssl_context;
190
191/*
192 * This structure is used for session resuming.
193 */
194struct _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
204struct _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 /*
218 * Callbacks (RNG, debug, I/O)
219 */
220 int (*f_rng)(void *);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000221 void (*f_dbg)(void *, int, const char *);
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 int (*f_recv)(void *, unsigned char *, int);
223 int (*f_send)(void *, unsigned char *, int);
224
225 void *p_rng; /*!< context for the RNG function */
226 void *p_dbg; /*!< context for the debug function */
227 void *p_recv; /*!< context for reading operations */
228 void *p_send; /*!< context for writing operations */
229
230 /*
231 * Session layer
232 */
233 int resume; /*!< session resuming flag */
234 int timeout; /*!< sess. expiration time */
235 ssl_session *session; /*!< current session data */
236 int (*s_get)(ssl_context *); /*!< (server) get callback */
237 int (*s_set)(ssl_context *); /*!< (server) set callback */
238
239 /*
240 * Record layer (incoming data)
241 */
242 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
243 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
244 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
245 unsigned char *in_offt; /*!< read offset in application data */
246
247 int in_msgtype; /*!< record header: message type */
248 int in_msglen; /*!< record header: message length */
249 int in_left; /*!< amount of data read so far */
250
251 int in_hslen; /*!< current handshake message length */
252 int nb_zero; /*!< # of 0-length encrypted messages */
253
254 /*
255 * Record layer (outgoing data)
256 */
257 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
258 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
259 unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
260
261 int out_msgtype; /*!< record header: message type */
262 int out_msglen; /*!< record header: message length */
263 int out_left; /*!< amount of data not yet written */
264
265 /*
266 * PKI layer
267 */
268 rsa_context *rsa_key; /*!< own RSA private key */
269 x509_cert *own_cert; /*!< own X.509 certificate */
270 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000271 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000273 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 int endpoint; /*!< 0: client, 1: server */
276 int authmode; /*!< verification mode */
277 int client_auth; /*!< flag for client auth. */
278 int verify_result; /*!< verification result */
279
280 /*
281 * Crypto layer
282 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283 dhm_context dhm_ctx; /*!< DHM key exchange */
284 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
286
287 int do_crypt; /*!< en(de)cryption flag */
288 int *ciphers; /*!< allowed ciphersuites */
289 int pmslen; /*!< premaster length */
290 int keylen; /*!< symmetric key length */
291 int minlen; /*!< min. ciphertext length */
292 int ivlen; /*!< IV length */
293 int maclen; /*!< MAC length */
294
295 unsigned char randbytes[64]; /*!< random bytes */
296 unsigned char premaster[256]; /*!< premaster secret */
297
298 unsigned char iv_enc[16]; /*!< IV (encryption) */
299 unsigned char iv_dec[16]; /*!< IV (decryption) */
300
301 unsigned char mac_enc[32]; /*!< MAC (encryption) */
302 unsigned char mac_dec[32]; /*!< MAC (decryption) */
303
304 unsigned long ctx_enc[128]; /*!< encryption context */
305 unsigned long ctx_dec[128]; /*!< decryption context */
306
307 /*
308 * TLS extensions
309 */
310 unsigned char *hostname;
311 unsigned long hostname_len;
312};
313
314#ifdef __cplusplus
315extern "C" {
316#endif
317
318extern int ssl_default_ciphers[];
319
320/**
321 * \brief Initialize an SSL context
322 *
323 * \param ssl SSL context
324 *
325 * \return 0 if successful, or 1 if memory allocation failed
326 */
327int ssl_init( ssl_context *ssl );
328
329/**
330 * \brief Set the current endpoint type
331 *
332 * \param ssl SSL context
333 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
334 */
335void ssl_set_endpoint( ssl_context *ssl, int endpoint );
336
337/**
338 * \brief Set the certificate verification mode
339 *
340 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000341 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 *
343 * SSL_VERIFY_NONE: peer certificate is not checked (default),
344 * this is insecure and SHOULD be avoided.
345 *
346 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
347 * handshake continues even if verification failed;
348 * ssl_get_verify_result() can be called after the
349 * handshake is complete.
350 *
351 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
352 * handshake is aborted if verification failed.
353 */
354void ssl_set_authmode( ssl_context *ssl, int authmode );
355
356/**
357 * \brief Set the random number generator callback
358 *
359 * \param ssl SSL context
360 * \param f_rng RNG function
361 * \param p_rng RNG parameter
362 */
363void ssl_set_rng( ssl_context *ssl,
364 int (*f_rng)(void *),
365 void *p_rng );
366
367/**
368 * \brief Set the debug callback
369 *
370 * \param ssl SSL context
371 * \param f_dbg debug function
372 * \param p_dbg debug parameter
373 */
374void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000375 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 void *p_dbg );
377
378/**
379 * \brief Set the underlying BIO read and write callbacks
380 *
381 * \param ssl SSL context
382 * \param f_recv read callback
383 * \param p_recv read parameter
384 * \param f_send write callback
385 * \param p_send write parameter
386 */
387void ssl_set_bio( ssl_context *ssl,
388 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
389 int (*f_send)(void *, unsigned char *, int), void *p_send );
390
391/**
392 * \brief Set the session callbacks (server-side only)
393 *
394 * \param ssl SSL context
395 * \param s_get session get callback
396 * \param s_set session set callback
397 */
398void ssl_set_scb( ssl_context *ssl,
399 int (*s_get)(ssl_context *),
400 int (*s_set)(ssl_context *) );
401
402/**
403 * \brief Set the session resuming flag, timeout and data
404 *
405 * \param ssl SSL context
406 * \param resume if 0 (default), the session will not be resumed
407 * \param timeout session timeout in seconds, or 0 (no timeout)
408 * \param session session context
409 */
410void ssl_set_session( ssl_context *ssl, int resume, int timeout,
411 ssl_session *session );
412
413/**
414 * \brief Set the list of allowed ciphersuites
415 *
416 * \param ssl SSL context
417 * \param ciphers 0-terminated list of allowed ciphers
418 */
419void ssl_set_ciphers( ssl_context *ssl, int *ciphers );
420
421/**
422 * \brief Set the data required to verify peer certificate
423 *
424 * \param ssl SSL context
425 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000426 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 * \param peer_cn expected peer CommonName (or NULL)
428 *
429 * \note TODO: add two more parameters: depth and crl
430 */
431void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000432 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434/**
435 * \brief Set own certificate and private key
436 *
437 * \param ssl SSL context
438 * \param own_cert own public certificate
439 * \param rsa_key own private RSA key
440 */
441void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
442 rsa_context *rsa_key );
443
444/**
445 * \brief Set the Diffie-Hellman public P and G values,
446 * read as hexadecimal strings (server-side only)
447 *
448 * \param ssl SSL context
449 * \param dhm_P Diffie-Hellman-Merkle modulus
450 * \param dhm_G Diffie-Hellman-Merkle generator
451 *
452 * \return 0 if successful
453 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000454int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000457 * \brief Set the Diffie-Hellman public P and G values,
458 * read from existing context (server-side only)
459 *
460 * \param ssl SSL context
461 * \param dhm_ctx Diffie-Hellman-Merkle context
462 *
463 * \return 0 if successful
464 */
465int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
466
467/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * \brief Set hostname for ServerName TLS Extension
469 *
470 *
471 * \param ssl SSL context
472 * \param hostname the server hostname
473 *
474 * \return 0 if successful
475 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000476int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478/**
479 * \brief Return the number of data bytes available to read
480 *
481 * \param ssl SSL context
482 *
483 * \return how many bytes are available in the read buffer
484 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000485int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
488 * \brief Return the result of the certificate verification
489 *
490 * \param ssl SSL context
491 *
492 * \return 0 if successful, or a combination of:
493 * BADCERT_EXPIRED
494 * BADCERT_REVOKED
495 * BADCERT_CN_MISMATCH
496 * BADCERT_NOT_TRUSTED
497 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000498int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
501 * \brief Return the name of the current cipher
502 *
503 * \param ssl SSL context
504 *
505 * \return a string containing the cipher name
506 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507const char *ssl_get_cipher( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/**
510 * \brief Perform the SSL handshake
511 *
512 * \param ssl SSL context
513 *
Paul Bakker40e46942009-01-03 21:51:57 +0000514 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 * or a specific SSL error code.
516 */
517int ssl_handshake( ssl_context *ssl );
518
519/**
520 * \brief Read at most 'len' application data bytes
521 *
522 * \param ssl SSL context
523 * \param buf buffer that will hold the data
524 * \param len how many bytes must be read
525 *
526 * \return This function returns the number of bytes read,
527 * or a negative error code.
528 */
529int ssl_read( ssl_context *ssl, unsigned char *buf, int len );
530
531/**
532 * \brief Write exactly 'len' application data bytes
533 *
534 * \param ssl SSL context
535 * \param buf buffer holding the data
536 * \param len how many bytes must be written
537 *
538 * \return This function returns the number of bytes written,
539 * or a negative error code.
540 *
Paul Bakker40e46942009-01-03 21:51:57 +0000541 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 * it must be called later with the *same* arguments,
543 * until it returns a positive value.
544 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000545int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
548 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000549 *
550 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
552int ssl_close_notify( ssl_context *ssl );
553
554/**
555 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000556 *
557 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
559void ssl_free( ssl_context *ssl );
560
561/*
562 * Internal functions (do not call directly)
563 */
564int ssl_handshake_client( ssl_context *ssl );
565int ssl_handshake_server( ssl_context *ssl );
566
567int ssl_derive_keys( ssl_context *ssl );
568void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
569
570int ssl_read_record( ssl_context *ssl );
571int ssl_fetch_input( ssl_context *ssl, int nb_want );
572
573int ssl_write_record( ssl_context *ssl );
574int ssl_flush_output( ssl_context *ssl );
575
576int ssl_parse_certificate( ssl_context *ssl );
577int ssl_write_certificate( ssl_context *ssl );
578
579int ssl_parse_change_cipher_spec( ssl_context *ssl );
580int ssl_write_change_cipher_spec( ssl_context *ssl );
581
582int ssl_parse_finished( ssl_context *ssl );
583int ssl_write_finished( ssl_context *ssl );
584
585#ifdef __cplusplus
586}
587#endif
588
589#endif /* ssl.h */