blob: 772bb556eb2c4a036bce04cd173d766885985a45 [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 Bakker43b7e352011-01-18 15:27:19 +000038#include "polarssl/config.h"
39
40#if defined(POLARSSL_PKCS11_C)
41#include "polarssl/pkcs11.h"
42#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000044#ifdef _MSC_VER
45#define inline _inline
46#endif
47
Paul Bakker13e2dfe2009-07-28 07:18:38 +000048/*
49 * SSL Error codes
50 */
Paul Bakker3391b122009-07-28 20:11:54 +000051#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x1000
52#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x1800
53#define POLARSSL_ERR_SSL_INVALID_MAC -0x2000
54#define POLARSSL_ERR_SSL_INVALID_RECORD -0x2800
55#define POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE -0x3000
56#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x3800
57#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x4000
58#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x4800
59#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x5000
60#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x5800
61#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x6000
62#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x6800
63#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7000
64#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7800
65#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x8000
66#define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x8800
67#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x9000
68#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x9800
69#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0xA000
70#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0xA800
71#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0xB000
72#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0xB800
73#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0xC000
74#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0xC800
75#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0xD000
76#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0xD800
77#define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0xE000
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/*
80 * Various constants
81 */
82#define SSL_MAJOR_VERSION_3 3
83#define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
84#define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
85#define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
86
87#define SSL_IS_CLIENT 0
88#define SSL_IS_SERVER 1
89#define SSL_COMPRESS_NULL 0
90
91#define SSL_VERIFY_NONE 0
92#define SSL_VERIFY_OPTIONAL 1
93#define SSL_VERIFY_REQUIRED 2
94
95#define SSL_MAX_CONTENT_LEN 16384
96
97/*
98 * Allow an extra 512 bytes for the record header
99 * and encryption overhead (counter + MAC + padding).
100 */
101#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512)
102
103/*
104 * Supported ciphersuites
105 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000106#define SSL_RSA_RC4_128_MD5 0x04
107#define SSL_RSA_RC4_128_SHA 0x05
108#define SSL_RSA_DES_168_SHA 0x0A
109#define SSL_EDH_RSA_DES_168_SHA 0x16
110#define SSL_RSA_AES_128_SHA 0x2F
111#define SSL_EDH_RSA_AES_128_SHA 0x33
112#define SSL_RSA_AES_256_SHA 0x35
113#define SSL_EDH_RSA_AES_256_SHA 0x39
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115#define SSL_RSA_CAMELLIA_128_SHA 0x41
Paul Bakker77a43582010-06-15 21:32:46 +0000116#define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117#define SSL_RSA_CAMELLIA_256_SHA 0x84
118#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000119
Paul Bakker5121ce52009-01-03 21:22:43 +0000120/*
121 * Message, alert and handshake types
122 */
123#define SSL_MSG_CHANGE_CIPHER_SPEC 20
124#define SSL_MSG_ALERT 21
125#define SSL_MSG_HANDSHAKE 22
126#define SSL_MSG_APPLICATION_DATA 23
127
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000128#define SSL_ALERT_LEVEL_WARNING 1
129#define SSL_ALERT_LEVEL_FATAL 2
130
131#define SSL_ALERT_MSG_CLOSE_NOTIFY 0
132#define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
Paul Bakkerfbc4a452011-03-25 09:07:46 +0000133#define SSL_ALERT_MSG_BAD_RECORD_MAC 20
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000134#define SSL_ALERT_MSG_DECRYPTION_FAILED 21
135#define SSL_ALERT_MSG_RECORD_OVERFLOW 22
136#define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
137#define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
138#define SSL_ALERT_MSG_NO_CERT 41
139#define SSL_ALERT_MSG_BAD_CERT 42
140#define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
141#define SSL_ALERT_MSG_CERT_REVOKED 44
142#define SSL_ALERT_MSG_CERT_EXPIRED 45
143#define SSL_ALERT_MSG_CERT_UNKNOWN 46
144#define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
145#define SSL_ALERT_MSG_UNKNOWN_CA 48
146#define SSL_ALERT_MSG_ACCESS_DENIED 49
147#define SSL_ALERT_MSG_DECODE_ERROR 50
148#define SSL_ALERT_MSG_DECRYPT_ERROR 51
149#define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
150#define SSL_ALERT_MSG_PROTOCOL_VERSION 70
151#define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
152#define SSL_ALERT_MSG_INTERNAL_ERROR 80
153#define SSL_ALERT_MSG_USER_CANCELED 90
154#define SSL_ALERT_MSG_NO_RENEGOTIATION 100
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156#define SSL_HS_HELLO_REQUEST 0
157#define SSL_HS_CLIENT_HELLO 1
158#define SSL_HS_SERVER_HELLO 2
159#define SSL_HS_CERTIFICATE 11
160#define SSL_HS_SERVER_KEY_EXCHANGE 12
161#define SSL_HS_CERTIFICATE_REQUEST 13
162#define SSL_HS_SERVER_HELLO_DONE 14
163#define SSL_HS_CERTIFICATE_VERIFY 15
164#define SSL_HS_CLIENT_KEY_EXCHANGE 16
165#define SSL_HS_FINISHED 20
166
167/*
168 * TLS extensions
169 */
170#define TLS_EXT_SERVERNAME 0
171#define TLS_EXT_SERVERNAME_HOSTNAME 0
172
173/*
174 * SSL state machine
175 */
176typedef enum
177{
178 SSL_HELLO_REQUEST,
179 SSL_CLIENT_HELLO,
180 SSL_SERVER_HELLO,
181 SSL_SERVER_CERTIFICATE,
182 SSL_SERVER_KEY_EXCHANGE,
183 SSL_CERTIFICATE_REQUEST,
184 SSL_SERVER_HELLO_DONE,
185 SSL_CLIENT_CERTIFICATE,
186 SSL_CLIENT_KEY_EXCHANGE,
187 SSL_CERTIFICATE_VERIFY,
188 SSL_CLIENT_CHANGE_CIPHER_SPEC,
189 SSL_CLIENT_FINISHED,
190 SSL_SERVER_CHANGE_CIPHER_SPEC,
191 SSL_SERVER_FINISHED,
192 SSL_FLUSH_BUFFERS,
193 SSL_HANDSHAKE_OVER
194}
195ssl_states;
196
197typedef struct _ssl_session ssl_session;
198typedef struct _ssl_context ssl_context;
199
200/*
201 * This structure is used for session resuming.
202 */
203struct _ssl_session
204{
205 time_t start; /*!< starting time */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000206 int ciphersuite; /*!< chosen ciphersuite */
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 int length; /*!< session id length */
208 unsigned char id[32]; /*!< session identifier */
209 unsigned char master[48]; /*!< the master secret */
210 ssl_session *next; /*!< next session entry */
211};
212
213struct _ssl_context
214{
215 /*
216 * Miscellaneous
217 */
218 int state; /*!< SSL handshake: current state */
219
220 int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
221 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
222
223 int max_major_ver; /*!< max. major version from client */
224 int max_minor_ver; /*!< max. minor version from client */
225
226 /*
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000227 * Callbacks (RNG, debug, I/O, verification)
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
229 int (*f_rng)(void *);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000230 void (*f_dbg)(void *, int, const char *);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 int (*f_recv)(void *, unsigned char *, int);
232 int (*f_send)(void *, unsigned char *, int);
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000233 int (*f_vrfy)(void *, x509_cert *, int, int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 void *p_rng; /*!< context for the RNG function */
236 void *p_dbg; /*!< context for the debug function */
237 void *p_recv; /*!< context for reading operations */
238 void *p_send; /*!< context for writing operations */
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000239 void *p_vrfy; /*!< context for verification */
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241 /*
242 * Session layer
243 */
244 int resume; /*!< session resuming flag */
245 int timeout; /*!< sess. expiration time */
246 ssl_session *session; /*!< current session data */
247 int (*s_get)(ssl_context *); /*!< (server) get callback */
248 int (*s_set)(ssl_context *); /*!< (server) set callback */
249
250 /*
251 * Record layer (incoming data)
252 */
253 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
254 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
255 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
256 unsigned char *in_offt; /*!< read offset in application data */
257
258 int in_msgtype; /*!< record header: message type */
259 int in_msglen; /*!< record header: message length */
260 int in_left; /*!< amount of data read so far */
261
262 int in_hslen; /*!< current handshake message length */
263 int nb_zero; /*!< # of 0-length encrypted messages */
264
265 /*
266 * Record layer (outgoing data)
267 */
268 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
269 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
270 unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
271
272 int out_msgtype; /*!< record header: message type */
273 int out_msglen; /*!< record header: message length */
274 int out_left; /*!< amount of data not yet written */
275
276 /*
277 * PKI layer
278 */
279 rsa_context *rsa_key; /*!< own RSA private key */
Paul Bakker43b7e352011-01-18 15:27:19 +0000280#if defined(POLARSSL_PKCS11_C)
281 pkcs11_context *pkcs11_key; /*!< own PKCS#11 RSA private key */
282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 x509_cert *own_cert; /*!< own X.509 certificate */
284 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000285 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000287 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 int endpoint; /*!< 0: client, 1: server */
290 int authmode; /*!< verification mode */
291 int client_auth; /*!< flag for client auth. */
292 int verify_result; /*!< verification result */
293
294 /*
295 * Crypto layer
296 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000297 dhm_context dhm_ctx; /*!< DHM key exchange */
298 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
300
301 int do_crypt; /*!< en(de)cryption flag */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000302 int *ciphersuites; /*!< allowed ciphersuites */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 int pmslen; /*!< premaster length */
304 int keylen; /*!< symmetric key length */
305 int minlen; /*!< min. ciphertext length */
306 int ivlen; /*!< IV length */
307 int maclen; /*!< MAC length */
308
309 unsigned char randbytes[64]; /*!< random bytes */
310 unsigned char premaster[256]; /*!< premaster secret */
311
312 unsigned char iv_enc[16]; /*!< IV (encryption) */
313 unsigned char iv_dec[16]; /*!< IV (decryption) */
314
315 unsigned char mac_enc[32]; /*!< MAC (encryption) */
316 unsigned char mac_dec[32]; /*!< MAC (decryption) */
317
318 unsigned long ctx_enc[128]; /*!< encryption context */
319 unsigned long ctx_dec[128]; /*!< decryption context */
320
321 /*
322 * TLS extensions
323 */
324 unsigned char *hostname;
325 unsigned long hostname_len;
326};
327
328#ifdef __cplusplus
329extern "C" {
330#endif
331
Paul Bakkere3166ce2011-01-27 17:40:50 +0000332extern int ssl_default_ciphersuites[];
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000335 * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
Paul Bakker72f62662011-01-16 21:27:44 +0000336 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000337 * \return a statically allocated array of ciphersuites, the last
338 * entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000339 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000340static inline const int *ssl_list_ciphersuites( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000341{
Paul Bakkere3166ce2011-01-27 17:40:50 +0000342 return ssl_default_ciphersuites;
Paul Bakker72f62662011-01-16 21:27:44 +0000343}
344
345/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000346 * \brief Return the name of the ciphersuite associated with the given
347 * ID
Paul Bakker72f62662011-01-16 21:27:44 +0000348 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000349 * \param ciphersuite_id SSL ciphersuite ID
Paul Bakker72f62662011-01-16 21:27:44 +0000350 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000351 * \return a string containing the ciphersuite name
Paul Bakker72f62662011-01-16 21:27:44 +0000352 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000353const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
354
355/**
356 * \brief Return the ID of the ciphersuite associated with the given
357 * name
358 *
359 * \param ciphersuite_name SSL ciphersuite name
360 *
361 * \return the ID with the ciphersuite or 0 if not found
362 */
363int ssl_get_ciphersuite_id( const char *ciphersuite_name );
Paul Bakker72f62662011-01-16 21:27:44 +0000364
365/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 * \brief Initialize an SSL context
367 *
368 * \param ssl SSL context
369 *
370 * \return 0 if successful, or 1 if memory allocation failed
371 */
372int ssl_init( ssl_context *ssl );
373
374/**
375 * \brief Set the current endpoint type
376 *
377 * \param ssl SSL context
378 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
379 */
380void ssl_set_endpoint( ssl_context *ssl, int endpoint );
381
382/**
383 * \brief Set the certificate verification mode
384 *
385 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000386 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 *
388 * SSL_VERIFY_NONE: peer certificate is not checked (default),
389 * this is insecure and SHOULD be avoided.
390 *
391 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
392 * handshake continues even if verification failed;
393 * ssl_get_verify_result() can be called after the
394 * handshake is complete.
395 *
396 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
397 * handshake is aborted if verification failed.
398 */
399void ssl_set_authmode( ssl_context *ssl, int authmode );
400
401/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000402 * \brief Set the verification callback (Optional).
403 *
404 * If set, the verification callback is called once for every
405 * certificate in the chain. The verification function has the
406 * following parameter: (void *parameter, x509_cert certificate,
407 * int certifcate_depth, int preverify_ok). It should
408 * return 0 on SUCCESS.
409 *
410 * \param ssl SSL context
411 * \param f_vrfy verification function
412 * \param p_vrfy verification parameter
413 */
414void ssl_set_verify( ssl_context *ssl,
415 int (*f_vrfy)(void *, x509_cert *, int, int),
416 void *p_vrfy );
417
418/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * \brief Set the random number generator callback
420 *
421 * \param ssl SSL context
422 * \param f_rng RNG function
423 * \param p_rng RNG parameter
424 */
425void ssl_set_rng( ssl_context *ssl,
426 int (*f_rng)(void *),
427 void *p_rng );
428
429/**
430 * \brief Set the debug callback
431 *
432 * \param ssl SSL context
433 * \param f_dbg debug function
434 * \param p_dbg debug parameter
435 */
436void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000437 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 void *p_dbg );
439
440/**
441 * \brief Set the underlying BIO read and write callbacks
442 *
443 * \param ssl SSL context
444 * \param f_recv read callback
445 * \param p_recv read parameter
446 * \param f_send write callback
447 * \param p_send write parameter
448 */
449void ssl_set_bio( ssl_context *ssl,
450 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
451 int (*f_send)(void *, unsigned char *, int), void *p_send );
452
453/**
454 * \brief Set the session callbacks (server-side only)
455 *
456 * \param ssl SSL context
457 * \param s_get session get callback
458 * \param s_set session set callback
459 */
460void ssl_set_scb( ssl_context *ssl,
461 int (*s_get)(ssl_context *),
462 int (*s_set)(ssl_context *) );
463
464/**
465 * \brief Set the session resuming flag, timeout and data
466 *
467 * \param ssl SSL context
468 * \param resume if 0 (default), the session will not be resumed
469 * \param timeout session timeout in seconds, or 0 (no timeout)
470 * \param session session context
471 */
472void ssl_set_session( ssl_context *ssl, int resume, int timeout,
473 ssl_session *session );
474
475/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000476 * \brief Set the list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000478 * \param ssl SSL context
479 * \param ciphersuites 0-terminated list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000481void ssl_set_ciphersuites( ssl_context *ssl, int *ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483/**
484 * \brief Set the data required to verify peer certificate
485 *
486 * \param ssl SSL context
487 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000488 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 * \param peer_cn expected peer CommonName (or NULL)
490 *
491 * \note TODO: add two more parameters: depth and crl
492 */
493void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000494 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
497 * \brief Set own certificate and private key
498 *
499 * \param ssl SSL context
500 * \param own_cert own public certificate
501 * \param rsa_key own private RSA key
502 */
503void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
504 rsa_context *rsa_key );
505
Paul Bakker43b7e352011-01-18 15:27:19 +0000506#if defined(POLARSSL_PKCS11_C)
507/**
508 * \brief Set own certificate and PKCS#11 private key
509 *
510 * \param ssl SSL context
511 * \param own_cert own public certificate
512 * \param pkcs11_key own PKCS#11 RSA key
513 */
514void ssl_set_own_cert_pkcs11( ssl_context *ssl, x509_cert *own_cert,
515 pkcs11_context *pkcs11_key );
516#endif
517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518/**
519 * \brief Set the Diffie-Hellman public P and G values,
520 * read as hexadecimal strings (server-side only)
521 *
522 * \param ssl SSL context
523 * \param dhm_P Diffie-Hellman-Merkle modulus
524 * \param dhm_G Diffie-Hellman-Merkle generator
525 *
526 * \return 0 if successful
527 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000528int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000531 * \brief Set the Diffie-Hellman public P and G values,
532 * read from existing context (server-side only)
533 *
534 * \param ssl SSL context
535 * \param dhm_ctx Diffie-Hellman-Merkle context
536 *
537 * \return 0 if successful
538 */
539int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
540
541/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 * \brief Set hostname for ServerName TLS Extension
543 *
544 *
545 * \param ssl SSL context
546 * \param hostname the server hostname
547 *
548 * \return 0 if successful
549 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000550int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552/**
553 * \brief Return the number of data bytes available to read
554 *
555 * \param ssl SSL context
556 *
557 * \return how many bytes are available in the read buffer
558 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000559int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561/**
562 * \brief Return the result of the certificate verification
563 *
564 * \param ssl SSL context
565 *
566 * \return 0 if successful, or a combination of:
567 * BADCERT_EXPIRED
568 * BADCERT_REVOKED
569 * BADCERT_CN_MISMATCH
570 * BADCERT_NOT_TRUSTED
571 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000572int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000575 * \brief Return the name of the current ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 *
577 * \param ssl SSL context
578 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000579 * \return a string containing the ciphersuite name
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000581const char *ssl_get_ciphersuite( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
Paul Bakker43ca69c2011-01-15 17:35:19 +0000584 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
585 *
586 * \param ssl SSL context
587 *
588 * \return a string containing the SSL version
589 */
590const char *ssl_get_version( const ssl_context *ssl );
591
592/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 * \brief Perform the SSL handshake
594 *
595 * \param ssl SSL context
596 *
Paul Bakker40e46942009-01-03 21:51:57 +0000597 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 * or a specific SSL error code.
599 */
600int ssl_handshake( ssl_context *ssl );
601
602/**
603 * \brief Read at most 'len' application data bytes
604 *
605 * \param ssl SSL context
606 * \param buf buffer that will hold the data
607 * \param len how many bytes must be read
608 *
609 * \return This function returns the number of bytes read,
610 * or a negative error code.
611 */
612int ssl_read( ssl_context *ssl, unsigned char *buf, int len );
613
614/**
615 * \brief Write exactly 'len' application data bytes
616 *
617 * \param ssl SSL context
618 * \param buf buffer holding the data
619 * \param len how many bytes must be written
620 *
621 * \return This function returns the number of bytes written,
622 * or a negative error code.
623 *
Paul Bakker40e46942009-01-03 21:51:57 +0000624 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 * it must be called later with the *same* arguments,
626 * until it returns a positive value.
627 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000628int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
631 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000632 *
633 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 */
635int ssl_close_notify( ssl_context *ssl );
636
637/**
638 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000639 *
640 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 */
642void ssl_free( ssl_context *ssl );
643
644/*
645 * Internal functions (do not call directly)
646 */
647int ssl_handshake_client( ssl_context *ssl );
648int ssl_handshake_server( ssl_context *ssl );
649
650int ssl_derive_keys( ssl_context *ssl );
651void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
652
653int ssl_read_record( ssl_context *ssl );
654int ssl_fetch_input( ssl_context *ssl, int nb_want );
655
656int ssl_write_record( ssl_context *ssl );
657int ssl_flush_output( ssl_context *ssl );
658
659int ssl_parse_certificate( ssl_context *ssl );
660int ssl_write_certificate( ssl_context *ssl );
661
662int ssl_parse_change_cipher_spec( ssl_context *ssl );
663int ssl_write_change_cipher_spec( ssl_context *ssl );
664
665int ssl_parse_finished( ssl_context *ssl );
666int ssl_write_finished( ssl_context *ssl );
667
668#ifdef __cplusplus
669}
670#endif
671
672#endif /* ssl.h */