blob: 7ddf7b1ffbf7b5ba23fc747472449518515df884 [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 /*
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000218 * Callbacks (RNG, debug, I/O, verification)
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 */
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);
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000224 int (*f_vrfy)(void *, x509_cert *, int, int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
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 Bakkerb63b0af2011-01-13 17:54:59 +0000230 void *p_vrfy; /*!< context for verification */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
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 Bakker40ea7de2009-05-03 10:18:48 +0000273 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000275 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
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 Bakkerff60ee62010-03-16 21:09:09 +0000285 dhm_context dhm_ctx; /*!< DHM key exchange */
286 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 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
317extern "C" {
318#endif
319
320extern int ssl_default_ciphers[];
321
322/**
Paul Bakker72f62662011-01-16 21:27:44 +0000323 * \brief Returns the list of ciphers supported by the SSL/TLS module.
324 *
325 * \return a statically allocated array of ciphers, the last entry
326 * is 0.
327 */
328static inline const int *ssl_list_ciphers( void )
329{
330 return ssl_default_ciphers;
331}
332
333/**
334 * \brief Return the name of the cipher associated with the given ID
335 *
336 * \param cipher_id SSL cipher ID
337 *
338 * \return a string containing the cipher name
339 */
340const char *ssl_get_cipher_name( const int cipher_id );
341
342/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * \brief Initialize an SSL context
344 *
345 * \param ssl SSL context
346 *
347 * \return 0 if successful, or 1 if memory allocation failed
348 */
349int ssl_init( ssl_context *ssl );
350
351/**
352 * \brief Set the current endpoint type
353 *
354 * \param ssl SSL context
355 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
356 */
357void ssl_set_endpoint( ssl_context *ssl, int endpoint );
358
359/**
360 * \brief Set the certificate verification mode
361 *
362 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000363 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 *
365 * SSL_VERIFY_NONE: peer certificate is not checked (default),
366 * this is insecure and SHOULD be avoided.
367 *
368 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
369 * handshake continues even if verification failed;
370 * ssl_get_verify_result() can be called after the
371 * handshake is complete.
372 *
373 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
374 * handshake is aborted if verification failed.
375 */
376void ssl_set_authmode( ssl_context *ssl, int authmode );
377
378/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000379 * \brief Set the verification callback (Optional).
380 *
381 * If set, the verification callback is called once for every
382 * certificate in the chain. The verification function has the
383 * following parameter: (void *parameter, x509_cert certificate,
384 * int certifcate_depth, int preverify_ok). It should
385 * return 0 on SUCCESS.
386 *
387 * \param ssl SSL context
388 * \param f_vrfy verification function
389 * \param p_vrfy verification parameter
390 */
391void ssl_set_verify( ssl_context *ssl,
392 int (*f_vrfy)(void *, x509_cert *, int, int),
393 void *p_vrfy );
394
395/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * \brief Set the random number generator callback
397 *
398 * \param ssl SSL context
399 * \param f_rng RNG function
400 * \param p_rng RNG parameter
401 */
402void ssl_set_rng( ssl_context *ssl,
403 int (*f_rng)(void *),
404 void *p_rng );
405
406/**
407 * \brief Set the debug callback
408 *
409 * \param ssl SSL context
410 * \param f_dbg debug function
411 * \param p_dbg debug parameter
412 */
413void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000414 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 void *p_dbg );
416
417/**
418 * \brief Set the underlying BIO read and write callbacks
419 *
420 * \param ssl SSL context
421 * \param f_recv read callback
422 * \param p_recv read parameter
423 * \param f_send write callback
424 * \param p_send write parameter
425 */
426void ssl_set_bio( ssl_context *ssl,
427 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
428 int (*f_send)(void *, unsigned char *, int), void *p_send );
429
430/**
431 * \brief Set the session callbacks (server-side only)
432 *
433 * \param ssl SSL context
434 * \param s_get session get callback
435 * \param s_set session set callback
436 */
437void ssl_set_scb( ssl_context *ssl,
438 int (*s_get)(ssl_context *),
439 int (*s_set)(ssl_context *) );
440
441/**
442 * \brief Set the session resuming flag, timeout and data
443 *
444 * \param ssl SSL context
445 * \param resume if 0 (default), the session will not be resumed
446 * \param timeout session timeout in seconds, or 0 (no timeout)
447 * \param session session context
448 */
449void ssl_set_session( ssl_context *ssl, int resume, int timeout,
450 ssl_session *session );
451
452/**
453 * \brief Set the list of allowed ciphersuites
454 *
455 * \param ssl SSL context
456 * \param ciphers 0-terminated list of allowed ciphers
457 */
458void ssl_set_ciphers( ssl_context *ssl, int *ciphers );
459
460/**
461 * \brief Set the data required to verify peer certificate
462 *
463 * \param ssl SSL context
464 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000465 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * \param peer_cn expected peer CommonName (or NULL)
467 *
468 * \note TODO: add two more parameters: depth and crl
469 */
470void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000471 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
474 * \brief Set own certificate and private key
475 *
476 * \param ssl SSL context
477 * \param own_cert own public certificate
478 * \param rsa_key own private RSA key
479 */
480void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
481 rsa_context *rsa_key );
482
483/**
484 * \brief Set the Diffie-Hellman public P and G values,
485 * read as hexadecimal strings (server-side only)
486 *
487 * \param ssl SSL context
488 * \param dhm_P Diffie-Hellman-Merkle modulus
489 * \param dhm_G Diffie-Hellman-Merkle generator
490 *
491 * \return 0 if successful
492 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000493int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000496 * \brief Set the Diffie-Hellman public P and G values,
497 * read from existing context (server-side only)
498 *
499 * \param ssl SSL context
500 * \param dhm_ctx Diffie-Hellman-Merkle context
501 *
502 * \return 0 if successful
503 */
504int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
505
506/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 * \brief Set hostname for ServerName TLS Extension
508 *
509 *
510 * \param ssl SSL context
511 * \param hostname the server hostname
512 *
513 * \return 0 if successful
514 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000515int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/**
518 * \brief Return the number of data bytes available to read
519 *
520 * \param ssl SSL context
521 *
522 * \return how many bytes are available in the read buffer
523 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000524int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
527 * \brief Return the result of the certificate verification
528 *
529 * \param ssl SSL context
530 *
531 * \return 0 if successful, or a combination of:
532 * BADCERT_EXPIRED
533 * BADCERT_REVOKED
534 * BADCERT_CN_MISMATCH
535 * BADCERT_NOT_TRUSTED
536 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539/**
540 * \brief Return the name of the current cipher
541 *
542 * \param ssl SSL context
543 *
544 * \return a string containing the cipher name
545 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000546const char *ssl_get_cipher( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548/**
Paul Bakker43ca69c2011-01-15 17:35:19 +0000549 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
550 *
551 * \param ssl SSL context
552 *
553 * \return a string containing the SSL version
554 */
555const char *ssl_get_version( const ssl_context *ssl );
556
557/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 * \brief Perform the SSL handshake
559 *
560 * \param ssl SSL context
561 *
Paul Bakker40e46942009-01-03 21:51:57 +0000562 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 * or a specific SSL error code.
564 */
565int ssl_handshake( ssl_context *ssl );
566
567/**
568 * \brief Read at most 'len' application data bytes
569 *
570 * \param ssl SSL context
571 * \param buf buffer that will hold the data
572 * \param len how many bytes must be read
573 *
574 * \return This function returns the number of bytes read,
575 * or a negative error code.
576 */
577int ssl_read( ssl_context *ssl, unsigned char *buf, int len );
578
579/**
580 * \brief Write exactly 'len' application data bytes
581 *
582 * \param ssl SSL context
583 * \param buf buffer holding the data
584 * \param len how many bytes must be written
585 *
586 * \return This function returns the number of bytes written,
587 * or a negative error code.
588 *
Paul Bakker40e46942009-01-03 21:51:57 +0000589 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 * it must be called later with the *same* arguments,
591 * until it returns a positive value.
592 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595/**
596 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000597 *
598 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
600int ssl_close_notify( ssl_context *ssl );
601
602/**
603 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000604 *
605 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 */
607void ssl_free( ssl_context *ssl );
608
609/*
610 * Internal functions (do not call directly)
611 */
612int ssl_handshake_client( ssl_context *ssl );
613int ssl_handshake_server( ssl_context *ssl );
614
615int ssl_derive_keys( ssl_context *ssl );
616void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
617
618int ssl_read_record( ssl_context *ssl );
619int ssl_fetch_input( ssl_context *ssl, int nb_want );
620
621int ssl_write_record( ssl_context *ssl );
622int ssl_flush_output( ssl_context *ssl );
623
624int ssl_parse_certificate( ssl_context *ssl );
625int ssl_write_certificate( ssl_context *ssl );
626
627int ssl_parse_change_cipher_spec( ssl_context *ssl );
628int ssl_write_change_cipher_spec( ssl_context *ssl );
629
630int ssl_parse_finished( ssl_context *ssl );
631int ssl_write_finished( ssl_context *ssl );
632
633#ifdef __cplusplus
634}
635#endif
636
637#endif /* ssl.h */