blob: 1e01253d472d616dfdba3aca41114ae917118aac [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 Bakker13e2dfe2009-07-28 07:18:38 +000044/*
45 * SSL Error codes
46 */
Paul Bakker3391b122009-07-28 20:11:54 +000047#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x1000
48#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x1800
49#define POLARSSL_ERR_SSL_INVALID_MAC -0x2000
50#define POLARSSL_ERR_SSL_INVALID_RECORD -0x2800
51#define POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE -0x3000
52#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x3800
53#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x4000
54#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x4800
55#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x5000
56#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x5800
57#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x6000
58#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x6800
59#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7000
60#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7800
61#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x8000
62#define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x8800
63#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x9000
64#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x9800
65#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0xA000
66#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0xA800
67#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0xB000
68#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0xB800
69#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0xC000
70#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0xC800
71#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0xD000
72#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0xD800
73#define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0xE000
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/*
76 * Various constants
77 */
78#define SSL_MAJOR_VERSION_3 3
79#define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
80#define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
81#define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
82
83#define SSL_IS_CLIENT 0
84#define SSL_IS_SERVER 1
85#define SSL_COMPRESS_NULL 0
86
87#define SSL_VERIFY_NONE 0
88#define SSL_VERIFY_OPTIONAL 1
89#define SSL_VERIFY_REQUIRED 2
90
91#define SSL_MAX_CONTENT_LEN 16384
92
93/*
94 * Allow an extra 512 bytes for the record header
95 * and encryption overhead (counter + MAC + padding).
96 */
97#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512)
98
99/*
100 * Supported ciphersuites
101 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000102#define SSL_RSA_RC4_128_MD5 0x04
103#define SSL_RSA_RC4_128_SHA 0x05
104#define SSL_RSA_DES_168_SHA 0x0A
105#define SSL_EDH_RSA_DES_168_SHA 0x16
106#define SSL_RSA_AES_128_SHA 0x2F
107#define SSL_EDH_RSA_AES_128_SHA 0x33
108#define SSL_RSA_AES_256_SHA 0x35
109#define SSL_EDH_RSA_AES_256_SHA 0x39
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111#define SSL_RSA_CAMELLIA_128_SHA 0x41
Paul Bakker77a43582010-06-15 21:32:46 +0000112#define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113#define SSL_RSA_CAMELLIA_256_SHA 0x84
114#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116/*
117 * Message, alert and handshake types
118 */
119#define SSL_MSG_CHANGE_CIPHER_SPEC 20
120#define SSL_MSG_ALERT 21
121#define SSL_MSG_HANDSHAKE 22
122#define SSL_MSG_APPLICATION_DATA 23
123
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000124#define SSL_ALERT_LEVEL_WARNING 1
125#define SSL_ALERT_LEVEL_FATAL 2
126
127#define SSL_ALERT_MSG_CLOSE_NOTIFY 0
128#define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
Paul Bakkerfbc4a452011-03-25 09:07:46 +0000129#define SSL_ALERT_MSG_BAD_RECORD_MAC 20
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000130#define SSL_ALERT_MSG_DECRYPTION_FAILED 21
131#define SSL_ALERT_MSG_RECORD_OVERFLOW 22
132#define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
133#define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
134#define SSL_ALERT_MSG_NO_CERT 41
135#define SSL_ALERT_MSG_BAD_CERT 42
136#define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
137#define SSL_ALERT_MSG_CERT_REVOKED 44
138#define SSL_ALERT_MSG_CERT_EXPIRED 45
139#define SSL_ALERT_MSG_CERT_UNKNOWN 46
140#define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
141#define SSL_ALERT_MSG_UNKNOWN_CA 48
142#define SSL_ALERT_MSG_ACCESS_DENIED 49
143#define SSL_ALERT_MSG_DECODE_ERROR 50
144#define SSL_ALERT_MSG_DECRYPT_ERROR 51
145#define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
146#define SSL_ALERT_MSG_PROTOCOL_VERSION 70
147#define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
148#define SSL_ALERT_MSG_INTERNAL_ERROR 80
149#define SSL_ALERT_MSG_USER_CANCELED 90
150#define SSL_ALERT_MSG_NO_RENEGOTIATION 100
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152#define SSL_HS_HELLO_REQUEST 0
153#define SSL_HS_CLIENT_HELLO 1
154#define SSL_HS_SERVER_HELLO 2
155#define SSL_HS_CERTIFICATE 11
156#define SSL_HS_SERVER_KEY_EXCHANGE 12
157#define SSL_HS_CERTIFICATE_REQUEST 13
158#define SSL_HS_SERVER_HELLO_DONE 14
159#define SSL_HS_CERTIFICATE_VERIFY 15
160#define SSL_HS_CLIENT_KEY_EXCHANGE 16
161#define SSL_HS_FINISHED 20
162
163/*
164 * TLS extensions
165 */
166#define TLS_EXT_SERVERNAME 0
167#define TLS_EXT_SERVERNAME_HOSTNAME 0
168
169/*
170 * SSL state machine
171 */
172typedef enum
173{
174 SSL_HELLO_REQUEST,
175 SSL_CLIENT_HELLO,
176 SSL_SERVER_HELLO,
177 SSL_SERVER_CERTIFICATE,
178 SSL_SERVER_KEY_EXCHANGE,
179 SSL_CERTIFICATE_REQUEST,
180 SSL_SERVER_HELLO_DONE,
181 SSL_CLIENT_CERTIFICATE,
182 SSL_CLIENT_KEY_EXCHANGE,
183 SSL_CERTIFICATE_VERIFY,
184 SSL_CLIENT_CHANGE_CIPHER_SPEC,
185 SSL_CLIENT_FINISHED,
186 SSL_SERVER_CHANGE_CIPHER_SPEC,
187 SSL_SERVER_FINISHED,
188 SSL_FLUSH_BUFFERS,
189 SSL_HANDSHAKE_OVER
190}
191ssl_states;
192
193typedef struct _ssl_session ssl_session;
194typedef struct _ssl_context ssl_context;
195
196/*
197 * This structure is used for session resuming.
198 */
199struct _ssl_session
200{
201 time_t start; /*!< starting time */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000202 int ciphersuite; /*!< chosen ciphersuite */
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 int length; /*!< session id length */
204 unsigned char id[32]; /*!< session identifier */
205 unsigned char master[48]; /*!< the master secret */
206 ssl_session *next; /*!< next session entry */
207};
208
209struct _ssl_context
210{
211 /*
212 * Miscellaneous
213 */
214 int state; /*!< SSL handshake: current state */
215
216 int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
217 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
218
219 int max_major_ver; /*!< max. major version from client */
220 int max_minor_ver; /*!< max. minor version from client */
221
222 /*
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000223 * Callbacks (RNG, debug, I/O, verification)
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 */
225 int (*f_rng)(void *);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000226 void (*f_dbg)(void *, int, const char *);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 int (*f_recv)(void *, unsigned char *, int);
228 int (*f_send)(void *, unsigned char *, int);
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000229 int (*f_vrfy)(void *, x509_cert *, int, int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231 void *p_rng; /*!< context for the RNG function */
232 void *p_dbg; /*!< context for the debug function */
233 void *p_recv; /*!< context for reading operations */
234 void *p_send; /*!< context for writing operations */
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000235 void *p_vrfy; /*!< context for verification */
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 /*
238 * Session layer
239 */
240 int resume; /*!< session resuming flag */
241 int timeout; /*!< sess. expiration time */
242 ssl_session *session; /*!< current session data */
243 int (*s_get)(ssl_context *); /*!< (server) get callback */
244 int (*s_set)(ssl_context *); /*!< (server) set callback */
245
246 /*
247 * Record layer (incoming data)
248 */
249 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
250 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
251 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
252 unsigned char *in_offt; /*!< read offset in application data */
253
254 int in_msgtype; /*!< record header: message type */
255 int in_msglen; /*!< record header: message length */
256 int in_left; /*!< amount of data read so far */
257
258 int in_hslen; /*!< current handshake message length */
259 int nb_zero; /*!< # of 0-length encrypted messages */
260
261 /*
262 * Record layer (outgoing data)
263 */
264 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
265 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
266 unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
267
268 int out_msgtype; /*!< record header: message type */
269 int out_msglen; /*!< record header: message length */
270 int out_left; /*!< amount of data not yet written */
271
272 /*
273 * PKI layer
274 */
275 rsa_context *rsa_key; /*!< own RSA private key */
Paul Bakker43b7e352011-01-18 15:27:19 +0000276#if defined(POLARSSL_PKCS11_C)
277 pkcs11_context *pkcs11_key; /*!< own PKCS#11 RSA private key */
278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 x509_cert *own_cert; /*!< own X.509 certificate */
280 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000281 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000283 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 int endpoint; /*!< 0: client, 1: server */
286 int authmode; /*!< verification mode */
287 int client_auth; /*!< flag for client auth. */
288 int verify_result; /*!< verification result */
289
290 /*
291 * Crypto layer
292 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000293 dhm_context dhm_ctx; /*!< DHM key exchange */
294 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
296
297 int do_crypt; /*!< en(de)cryption flag */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000298 int *ciphersuites; /*!< allowed ciphersuites */
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 int pmslen; /*!< premaster length */
300 int keylen; /*!< symmetric key length */
301 int minlen; /*!< min. ciphertext length */
302 int ivlen; /*!< IV length */
303 int maclen; /*!< MAC length */
304
305 unsigned char randbytes[64]; /*!< random bytes */
306 unsigned char premaster[256]; /*!< premaster secret */
307
308 unsigned char iv_enc[16]; /*!< IV (encryption) */
309 unsigned char iv_dec[16]; /*!< IV (decryption) */
310
311 unsigned char mac_enc[32]; /*!< MAC (encryption) */
312 unsigned char mac_dec[32]; /*!< MAC (decryption) */
313
314 unsigned long ctx_enc[128]; /*!< encryption context */
315 unsigned long ctx_dec[128]; /*!< decryption context */
316
317 /*
318 * TLS extensions
319 */
320 unsigned char *hostname;
321 unsigned long hostname_len;
322};
323
324#ifdef __cplusplus
325extern "C" {
326#endif
327
Paul Bakkere3166ce2011-01-27 17:40:50 +0000328extern int ssl_default_ciphersuites[];
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000331 * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
Paul Bakker72f62662011-01-16 21:27:44 +0000332 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000333 * \return a statically allocated array of ciphersuites, the last
334 * entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000335 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000336static inline const int *ssl_list_ciphersuites( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000337{
Paul Bakkere3166ce2011-01-27 17:40:50 +0000338 return ssl_default_ciphersuites;
Paul Bakker72f62662011-01-16 21:27:44 +0000339}
340
341/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000342 * \brief Return the name of the ciphersuite associated with the given
343 * ID
Paul Bakker72f62662011-01-16 21:27:44 +0000344 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000345 * \param ciphersuite_id SSL ciphersuite ID
Paul Bakker72f62662011-01-16 21:27:44 +0000346 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000347 * \return a string containing the ciphersuite name
Paul Bakker72f62662011-01-16 21:27:44 +0000348 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000349const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
350
351/**
352 * \brief Return the ID of the ciphersuite associated with the given
353 * name
354 *
355 * \param ciphersuite_name SSL ciphersuite name
356 *
357 * \return the ID with the ciphersuite or 0 if not found
358 */
359int ssl_get_ciphersuite_id( const char *ciphersuite_name );
Paul Bakker72f62662011-01-16 21:27:44 +0000360
361/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 * \brief Initialize an SSL context
363 *
364 * \param ssl SSL context
365 *
366 * \return 0 if successful, or 1 if memory allocation failed
367 */
368int ssl_init( ssl_context *ssl );
369
370/**
371 * \brief Set the current endpoint type
372 *
373 * \param ssl SSL context
374 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
375 */
376void ssl_set_endpoint( ssl_context *ssl, int endpoint );
377
378/**
379 * \brief Set the certificate verification mode
380 *
381 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000382 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 *
384 * SSL_VERIFY_NONE: peer certificate is not checked (default),
385 * this is insecure and SHOULD be avoided.
386 *
387 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
388 * handshake continues even if verification failed;
389 * ssl_get_verify_result() can be called after the
390 * handshake is complete.
391 *
392 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
393 * handshake is aborted if verification failed.
394 */
395void ssl_set_authmode( ssl_context *ssl, int authmode );
396
397/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000398 * \brief Set the verification callback (Optional).
399 *
400 * If set, the verification callback is called once for every
401 * certificate in the chain. The verification function has the
402 * following parameter: (void *parameter, x509_cert certificate,
403 * int certifcate_depth, int preverify_ok). It should
404 * return 0 on SUCCESS.
405 *
406 * \param ssl SSL context
407 * \param f_vrfy verification function
408 * \param p_vrfy verification parameter
409 */
410void ssl_set_verify( ssl_context *ssl,
411 int (*f_vrfy)(void *, x509_cert *, int, int),
412 void *p_vrfy );
413
414/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 * \brief Set the random number generator callback
416 *
417 * \param ssl SSL context
418 * \param f_rng RNG function
419 * \param p_rng RNG parameter
420 */
421void ssl_set_rng( ssl_context *ssl,
422 int (*f_rng)(void *),
423 void *p_rng );
424
425/**
426 * \brief Set the debug callback
427 *
428 * \param ssl SSL context
429 * \param f_dbg debug function
430 * \param p_dbg debug parameter
431 */
432void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 void *p_dbg );
435
436/**
437 * \brief Set the underlying BIO read and write callbacks
438 *
439 * \param ssl SSL context
440 * \param f_recv read callback
441 * \param p_recv read parameter
442 * \param f_send write callback
443 * \param p_send write parameter
444 */
445void ssl_set_bio( ssl_context *ssl,
446 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
447 int (*f_send)(void *, unsigned char *, int), void *p_send );
448
449/**
450 * \brief Set the session callbacks (server-side only)
451 *
452 * \param ssl SSL context
453 * \param s_get session get callback
454 * \param s_set session set callback
455 */
456void ssl_set_scb( ssl_context *ssl,
457 int (*s_get)(ssl_context *),
458 int (*s_set)(ssl_context *) );
459
460/**
461 * \brief Set the session resuming flag, timeout and data
462 *
463 * \param ssl SSL context
464 * \param resume if 0 (default), the session will not be resumed
465 * \param timeout session timeout in seconds, or 0 (no timeout)
466 * \param session session context
467 */
468void ssl_set_session( ssl_context *ssl, int resume, int timeout,
469 ssl_session *session );
470
471/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000472 * \brief Set the list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000474 * \param ssl SSL context
475 * \param ciphersuites 0-terminated list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000477void ssl_set_ciphersuites( ssl_context *ssl, int *ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479/**
480 * \brief Set the data required to verify peer certificate
481 *
482 * \param ssl SSL context
483 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000484 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 * \param peer_cn expected peer CommonName (or NULL)
486 *
487 * \note TODO: add two more parameters: depth and crl
488 */
489void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000490 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/**
493 * \brief Set own certificate and private key
494 *
495 * \param ssl SSL context
496 * \param own_cert own public certificate
497 * \param rsa_key own private RSA key
498 */
499void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
500 rsa_context *rsa_key );
501
Paul Bakker43b7e352011-01-18 15:27:19 +0000502#if defined(POLARSSL_PKCS11_C)
503/**
504 * \brief Set own certificate and PKCS#11 private key
505 *
506 * \param ssl SSL context
507 * \param own_cert own public certificate
508 * \param pkcs11_key own PKCS#11 RSA key
509 */
510void ssl_set_own_cert_pkcs11( ssl_context *ssl, x509_cert *own_cert,
511 pkcs11_context *pkcs11_key );
512#endif
513
Paul Bakker5121ce52009-01-03 21:22:43 +0000514/**
515 * \brief Set the Diffie-Hellman public P and G values,
516 * read as hexadecimal strings (server-side only)
517 *
518 * \param ssl SSL context
519 * \param dhm_P Diffie-Hellman-Merkle modulus
520 * \param dhm_G Diffie-Hellman-Merkle generator
521 *
522 * \return 0 if successful
523 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000524int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000527 * \brief Set the Diffie-Hellman public P and G values,
528 * read from existing context (server-side only)
529 *
530 * \param ssl SSL context
531 * \param dhm_ctx Diffie-Hellman-Merkle context
532 *
533 * \return 0 if successful
534 */
535int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
536
537/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * \brief Set hostname for ServerName TLS Extension
539 *
540 *
541 * \param ssl SSL context
542 * \param hostname the server hostname
543 *
544 * \return 0 if successful
545 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000546int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548/**
549 * \brief Return the number of data bytes available to read
550 *
551 * \param ssl SSL context
552 *
553 * \return how many bytes are available in the read buffer
554 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
558 * \brief Return the result of the certificate verification
559 *
560 * \param ssl SSL context
561 *
562 * \return 0 if successful, or a combination of:
563 * BADCERT_EXPIRED
564 * BADCERT_REVOKED
565 * BADCERT_CN_MISMATCH
566 * BADCERT_NOT_TRUSTED
567 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000571 * \brief Return the name of the current ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
573 * \param ssl SSL context
574 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000575 * \return a string containing the ciphersuite name
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000577const char *ssl_get_ciphersuite( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579/**
Paul Bakker43ca69c2011-01-15 17:35:19 +0000580 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
581 *
582 * \param ssl SSL context
583 *
584 * \return a string containing the SSL version
585 */
586const char *ssl_get_version( const ssl_context *ssl );
587
588/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \brief Perform the SSL handshake
590 *
591 * \param ssl SSL context
592 *
Paul Bakker40e46942009-01-03 21:51:57 +0000593 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 * or a specific SSL error code.
595 */
596int ssl_handshake( ssl_context *ssl );
597
598/**
599 * \brief Read at most 'len' application data bytes
600 *
601 * \param ssl SSL context
602 * \param buf buffer that will hold the data
603 * \param len how many bytes must be read
604 *
605 * \return This function returns the number of bytes read,
606 * or a negative error code.
607 */
608int ssl_read( ssl_context *ssl, unsigned char *buf, int len );
609
610/**
611 * \brief Write exactly 'len' application data bytes
612 *
613 * \param ssl SSL context
614 * \param buf buffer holding the data
615 * \param len how many bytes must be written
616 *
617 * \return This function returns the number of bytes written,
618 * or a negative error code.
619 *
Paul Bakker40e46942009-01-03 21:51:57 +0000620 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * it must be called later with the *same* arguments,
622 * until it returns a positive value.
623 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000624int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626/**
627 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000628 *
629 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 */
631int ssl_close_notify( ssl_context *ssl );
632
633/**
634 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000635 *
636 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 */
638void ssl_free( ssl_context *ssl );
639
640/*
641 * Internal functions (do not call directly)
642 */
643int ssl_handshake_client( ssl_context *ssl );
644int ssl_handshake_server( ssl_context *ssl );
645
646int ssl_derive_keys( ssl_context *ssl );
647void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
648
649int ssl_read_record( ssl_context *ssl );
650int ssl_fetch_input( ssl_context *ssl, int nb_want );
651
652int ssl_write_record( ssl_context *ssl );
653int ssl_flush_output( ssl_context *ssl );
654
655int ssl_parse_certificate( ssl_context *ssl );
656int ssl_write_certificate( ssl_context *ssl );
657
658int ssl_parse_change_cipher_spec( ssl_context *ssl );
659int ssl_write_change_cipher_spec( ssl_context *ssl );
660
661int ssl_parse_finished( ssl_context *ssl );
662int ssl_write_finished( ssl_context *ssl );
663
664#ifdef __cplusplus
665}
666#endif
667
668#endif /* ssl.h */