blob: 3d3c020b1289d0b113a6755215c787467ebfb99b [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/**
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 */
329int 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 */
337void ssl_set_endpoint( ssl_context *ssl, int endpoint );
338
339/**
340 * \brief Set the certificate verification mode
341 *
342 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000343 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 *
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 */
356void ssl_set_authmode( ssl_context *ssl, int authmode );
357
358/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000359 * \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 */
371void ssl_set_verify( ssl_context *ssl,
372 int (*f_vrfy)(void *, x509_cert *, int, int),
373 void *p_vrfy );
374
375/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 * \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 */
382void 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 */
393void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000394 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 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 */
406void 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 */
417void 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 */
429void 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 */
438void 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 Bakker40ea7de2009-05-03 10:18:48 +0000445 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * \param peer_cn expected peer CommonName (or NULL)
447 *
448 * \note TODO: add two more parameters: depth and crl
449 */
450void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000451 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
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 */
460void 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 Bakkerff60ee62010-03-16 21:09:09 +0000473int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000476 * \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 */
484int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
485
486/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 * \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 Bakkerff60ee62010-03-16 21:09:09 +0000495int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
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 Bakkerff60ee62010-03-16 21:09:09 +0000504int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
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 Bakkerff60ee62010-03-16 21:09:09 +0000517int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
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 Bakkerff60ee62010-03-16 21:09:09 +0000526const char *ssl_get_cipher( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/**
529 * \brief Perform the SSL handshake
530 *
531 * \param ssl SSL context
532 *
Paul Bakker40e46942009-01-03 21:51:57 +0000533 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 * or a specific SSL error code.
535 */
536int 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 */
548int 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 Bakker40e46942009-01-03 21:51:57 +0000560 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * it must be called later with the *same* arguments,
562 * until it returns a positive value.
563 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000564int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566/**
567 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000568 *
569 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 */
571int ssl_close_notify( ssl_context *ssl );
572
573/**
574 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000575 *
576 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 */
578void ssl_free( ssl_context *ssl );
579
580/*
581 * Internal functions (do not call directly)
582 */
583int ssl_handshake_client( ssl_context *ssl );
584int ssl_handshake_server( ssl_context *ssl );
585
586int ssl_derive_keys( ssl_context *ssl );
587void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
588
589int ssl_read_record( ssl_context *ssl );
590int ssl_fetch_input( ssl_context *ssl, int nb_want );
591
592int ssl_write_record( ssl_context *ssl );
593int ssl_flush_output( ssl_context *ssl );
594
595int ssl_parse_certificate( ssl_context *ssl );
596int ssl_write_certificate( ssl_context *ssl );
597
598int ssl_parse_change_cipher_spec( ssl_context *ssl );
599int ssl_write_change_cipher_spec( ssl_context *ssl );
600
601int ssl_parse_finished( ssl_context *ssl );
602int ssl_write_finished( ssl_context *ssl );
603
604#ifdef __cplusplus
605}
606#endif
607
608#endif /* ssl.h */