blob: 2201bde8172566b81663cf5b5a8e4b19781a632a [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 Bakkerfab5c822012-02-06 16:45:10 +00006 * Copyright (C) 2006-2012, 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 Bakker314052f2011-08-15 09:07:52 +000032#include "net.h"
33#include "dhm.h"
34#include "rsa.h"
35#include "md5.h"
36#include "sha1.h"
Paul Bakker1ef83d62012-04-11 12:09:53 +000037#include "sha2.h"
Paul Bakkerca4ab492012-04-18 14:23:57 +000038#include "sha4.h"
Paul Bakker314052f2011-08-15 09:07:52 +000039#include "x509.h"
40#include "config.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000041
42#if defined(POLARSSL_PKCS11_C)
Paul Bakker314052f2011-08-15 09:07:52 +000043#include "pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000044#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Paul Bakker2770fbd2012-07-03 13:30:23 +000046#if defined(POLARSSL_ZLIB_SUPPORT)
47#include "zlib.h"
48#endif
49
Paul Bakker09b1ec62011-07-27 16:28:54 +000050#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000051#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000052#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000053#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000054#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000055#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000056#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000057
Paul Bakker13e2dfe2009-07-28 07:18:38 +000058/*
59 * SSL Error codes
60 */
Paul Bakker9d781402011-05-09 16:17:09 +000061#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
62#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
63#define POLARSSL_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
64#define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
Paul Bakker831a7552011-05-18 13:32:51 +000065#define POLARSSL_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
Paul Bakker9d781402011-05-09 16:17:09 +000066#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
67#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
68#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x7400 /**< No session to recover was found. */
69#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
70#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message.*/
71#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
72#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key is not set, but needed. */
73#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
74#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
75#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
76#define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
77#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
78#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
79#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
80#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
81#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
82#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
83#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
84#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
85#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM Read Public. */
86#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM Calculate Secret. */
87#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
88#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
89#define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
Paul Bakker69e095c2011-12-10 21:55:01 +000090#define POLARSSL_ERR_SSL_MALLOC_FAILED -0x7F00 /**< Memory allocation failed */
Paul Bakker05ef8352012-05-08 09:17:57 +000091#define POLARSSL_ERR_SSL_HW_ACCEL_FAILED -0x7F80 /**< Hardware acceleration function returned with error */
92#define POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80 /**< Hardware acceleration function skipped / left alone data */
Paul Bakker2770fbd2012-07-03 13:30:23 +000093#define POLARSSL_ERR_SSL_COMPRESSION_FAILED -0x6F00 /**< Processing of the compression / decompression failed */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/*
96 * Various constants
97 */
98#define SSL_MAJOR_VERSION_3 3
99#define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
100#define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
101#define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000102#define SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104#define SSL_IS_CLIENT 0
105#define SSL_IS_SERVER 1
106#define SSL_COMPRESS_NULL 0
Paul Bakker2770fbd2012-07-03 13:30:23 +0000107#define SSL_COMPRESS_DEFLATE 1
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109#define SSL_VERIFY_NONE 0
110#define SSL_VERIFY_OPTIONAL 1
111#define SSL_VERIFY_REQUIRED 2
112
113#define SSL_MAX_CONTENT_LEN 16384
114
115/*
116 * Allow an extra 512 bytes for the record header
Paul Bakker2770fbd2012-07-03 13:30:23 +0000117 * and encryption overhead (counter + MAC + padding)
118 * and allow for a maximum of 1024 of compression expansion if
119 * enabled.
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000121#if defined(POLARSSL_ZLIB_SUPPORT)
122#define SSL_COMPRESSION_ADD 1024
123#else
124#define SSL_COMPRESSION_ADD 0
125#endif
126
127#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + SSL_COMPRESSION_ADD + 512)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/*
130 * Supported ciphersuites
131 */
Paul Bakkerfab5c822012-02-06 16:45:10 +0000132#define SSL_RSA_NULL_MD5 0x01 /**< Weak! */
133#define SSL_RSA_NULL_SHA 0x02 /**< Weak! */
134#define SSL_RSA_NULL_SHA256 0x3B /**< Weak! */
Paul Bakker10cd2252012-04-12 21:26:34 +0000135#define SSL_RSA_DES_SHA 0x09 /**< Weak! Not in TLS 1.2 */
136#define SSL_EDH_RSA_DES_SHA 0x15 /**< Weak! Not in TLS 1.2 */
Paul Bakkerfab5c822012-02-06 16:45:10 +0000137
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000138#define SSL_RSA_RC4_128_MD5 0x04
139#define SSL_RSA_RC4_128_SHA 0x05
Paul Bakker10cd2252012-04-12 21:26:34 +0000140
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000141#define SSL_RSA_DES_168_SHA 0x0A
142#define SSL_EDH_RSA_DES_168_SHA 0x16
Paul Bakker10cd2252012-04-12 21:26:34 +0000143
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000144#define SSL_RSA_AES_128_SHA 0x2F
145#define SSL_EDH_RSA_AES_128_SHA 0x33
146#define SSL_RSA_AES_256_SHA 0x35
147#define SSL_EDH_RSA_AES_256_SHA 0x39
Paul Bakker10cd2252012-04-12 21:26:34 +0000148#define SSL_RSA_AES_128_SHA256 0x3C /**< TLS 1.2 */
149#define SSL_RSA_AES_256_SHA256 0x3D /**< TLS 1.2 */
150#define SSL_EDH_RSA_AES_128_SHA256 0x67 /**< TLS 1.2 */
151#define SSL_EDH_RSA_AES_256_SHA256 0x6B /**< TLS 1.2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker10cd2252012-04-12 21:26:34 +0000153#define SSL_RSA_CAMELLIA_128_SHA 0x41
154#define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
155#define SSL_RSA_CAMELLIA_256_SHA 0x84
156#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
157#define SSL_RSA_CAMELLIA_128_SHA256 0xBA /**< TLS 1.2 */
158#define SSL_EDH_RSA_CAMELLIA_128_SHA256 0xBE /**< TLS 1.2 */
159#define SSL_RSA_CAMELLIA_256_SHA256 0xC0 /**< TLS 1.2 */
160#define SSL_EDH_RSA_CAMELLIA_256_SHA256 0xC4 /**< TLS 1.2 */
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000161
Paul Bakkerca4ab492012-04-18 14:23:57 +0000162#define SSL_RSA_AES_128_GCM_SHA256 0x9C
163#define SSL_RSA_AES_256_GCM_SHA384 0x9D
164#define SSL_EDH_RSA_AES_128_GCM_SHA256 0x9E
165#define SSL_EDH_RSA_AES_256_GCM_SHA384 0x9F
166
Paul Bakker5121ce52009-01-03 21:22:43 +0000167/*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000168 * Supported Signature and Hash algorithms (For TLS 1.2)
169 */
170#define SSL_HASH_NONE 0
171#define SSL_HASH_MD5 1
172#define SSL_HASH_SHA1 2
173#define SSL_HASH_SHA224 3
174#define SSL_HASH_SHA256 4
175#define SSL_HASH_SHA384 5
176#define SSL_HASH_SHA512 6
177
178#define SSL_SIG_RSA 1
179
180/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * Message, alert and handshake types
182 */
183#define SSL_MSG_CHANGE_CIPHER_SPEC 20
184#define SSL_MSG_ALERT 21
185#define SSL_MSG_HANDSHAKE 22
186#define SSL_MSG_APPLICATION_DATA 23
187
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000188#define SSL_ALERT_LEVEL_WARNING 1
189#define SSL_ALERT_LEVEL_FATAL 2
190
Paul Bakkere93dfa72012-04-10 08:03:03 +0000191#define SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
192#define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
193#define SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
194#define SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
195#define SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
196#define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000197#define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
Paul Bakkere93dfa72012-04-10 08:03:03 +0000198#define SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
199#define SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
200#define SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
201#define SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
202#define SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
203#define SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
204#define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
205#define SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
206#define SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
207#define SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
208#define SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
209#define SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
210#define SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
211#define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
212#define SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
213#define SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
214#define SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000215#define SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217#define SSL_HS_HELLO_REQUEST 0
218#define SSL_HS_CLIENT_HELLO 1
219#define SSL_HS_SERVER_HELLO 2
220#define SSL_HS_CERTIFICATE 11
221#define SSL_HS_SERVER_KEY_EXCHANGE 12
222#define SSL_HS_CERTIFICATE_REQUEST 13
223#define SSL_HS_SERVER_HELLO_DONE 14
224#define SSL_HS_CERTIFICATE_VERIFY 15
225#define SSL_HS_CLIENT_KEY_EXCHANGE 16
226#define SSL_HS_FINISHED 20
227
228/*
229 * TLS extensions
230 */
231#define TLS_EXT_SERVERNAME 0
232#define TLS_EXT_SERVERNAME_HOSTNAME 0
233
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000234#define TLS_EXT_SIG_ALG 13
235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236/*
237 * SSL state machine
238 */
239typedef enum
240{
241 SSL_HELLO_REQUEST,
242 SSL_CLIENT_HELLO,
243 SSL_SERVER_HELLO,
244 SSL_SERVER_CERTIFICATE,
245 SSL_SERVER_KEY_EXCHANGE,
246 SSL_CERTIFICATE_REQUEST,
247 SSL_SERVER_HELLO_DONE,
248 SSL_CLIENT_CERTIFICATE,
249 SSL_CLIENT_KEY_EXCHANGE,
250 SSL_CERTIFICATE_VERIFY,
251 SSL_CLIENT_CHANGE_CIPHER_SPEC,
252 SSL_CLIENT_FINISHED,
253 SSL_SERVER_CHANGE_CIPHER_SPEC,
254 SSL_SERVER_FINISHED,
255 SSL_FLUSH_BUFFERS,
256 SSL_HANDSHAKE_OVER
257}
258ssl_states;
259
260typedef struct _ssl_session ssl_session;
261typedef struct _ssl_context ssl_context;
262
263/*
264 * This structure is used for session resuming.
265 */
266struct _ssl_session
267{
268 time_t start; /*!< starting time */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000269 int ciphersuite; /*!< chosen ciphersuite */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000270 int compression; /*!< chosen compression */
Paul Bakker23986e52011-04-24 08:57:21 +0000271 size_t length; /*!< session id length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 unsigned char id[32]; /*!< session identifier */
273 unsigned char master[48]; /*!< the master secret */
274 ssl_session *next; /*!< next session entry */
275};
276
277struct _ssl_context
278{
279 /*
280 * Miscellaneous
281 */
282 int state; /*!< SSL handshake: current state */
283
284 int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
285 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
286
287 int max_major_ver; /*!< max. major version from client */
288 int max_minor_ver; /*!< max. minor version from client */
289
290 /*
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000291 * Callbacks (RNG, debug, I/O, verification)
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000293 int (*f_rng)(void *, unsigned char *, size_t);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000294 void (*f_dbg)(void *, int, const char *);
Paul Bakker23986e52011-04-24 08:57:21 +0000295 int (*f_recv)(void *, unsigned char *, size_t);
Paul Bakker39bb4182011-06-21 07:36:43 +0000296 int (*f_send)(void *, const unsigned char *, size_t);
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000297 int (*f_vrfy)(void *, x509_cert *, int, int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
299 void *p_rng; /*!< context for the RNG function */
300 void *p_dbg; /*!< context for the debug function */
301 void *p_recv; /*!< context for reading operations */
302 void *p_send; /*!< context for writing operations */
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000303 void *p_vrfy; /*!< context for verification */
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305 /*
306 * Session layer
307 */
308 int resume; /*!< session resuming flag */
309 int timeout; /*!< sess. expiration time */
310 ssl_session *session; /*!< current session data */
311 int (*s_get)(ssl_context *); /*!< (server) get callback */
312 int (*s_set)(ssl_context *); /*!< (server) set callback */
313
314 /*
315 * Record layer (incoming data)
316 */
317 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
318 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
319 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
320 unsigned char *in_offt; /*!< read offset in application data */
321
322 int in_msgtype; /*!< record header: message type */
Paul Bakker23986e52011-04-24 08:57:21 +0000323 size_t in_msglen; /*!< record header: message length */
324 size_t in_left; /*!< amount of data read so far */
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Paul Bakker23986e52011-04-24 08:57:21 +0000326 size_t in_hslen; /*!< current handshake message length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 int nb_zero; /*!< # of 0-length encrypted messages */
328
329 /*
330 * Record layer (outgoing data)
331 */
332 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
333 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
Paul Bakker186751d2012-05-08 13:16:14 +0000334 unsigned char *out_msg; /*!< the message contents (out_hdr+32)*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336 int out_msgtype; /*!< record header: message type */
Paul Bakker23986e52011-04-24 08:57:21 +0000337 size_t out_msglen; /*!< record header: message length */
338 size_t out_left; /*!< amount of data not yet written */
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 /*
341 * PKI layer
342 */
343 rsa_context *rsa_key; /*!< own RSA private key */
Paul Bakker43b7e352011-01-18 15:27:19 +0000344#if defined(POLARSSL_PKCS11_C)
345 pkcs11_context *pkcs11_key; /*!< own PKCS#11 RSA private key */
346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 x509_cert *own_cert; /*!< own X.509 certificate */
348 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000349 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000351 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353 int endpoint; /*!< 0: client, 1: server */
354 int authmode; /*!< verification mode */
355 int client_auth; /*!< flag for client auth. */
356 int verify_result; /*!< verification result */
357
358 /*
359 * Crypto layer
360 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000361 dhm_context dhm_ctx; /*!< DHM key exchange */
Paul Bakker380da532012-04-18 16:10:25 +0000362 unsigned char ctx_checksum[500]; /*!< Checksum context(s) */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000363
Paul Bakker380da532012-04-18 16:10:25 +0000364 void (*update_checksum)(ssl_context *, unsigned char *, size_t);
365 void (*calc_verify)(ssl_context *, unsigned char *);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000366 void (*calc_finished)(ssl_context *, unsigned char *, int);
367 int (*tls_prf)(unsigned char *, size_t, char *,
368 unsigned char *, size_t,
369 unsigned char *, size_t);
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371 int do_crypt; /*!< en(de)cryption flag */
Paul Bakkerb68cad62012-08-23 08:34:18 +0000372 const int *ciphersuites; /*!< allowed ciphersuites */
Paul Bakker23986e52011-04-24 08:57:21 +0000373 size_t pmslen; /*!< premaster length */
374 unsigned int keylen; /*!< symmetric key length */
375 size_t minlen; /*!< min. ciphertext length */
376 size_t ivlen; /*!< IV length */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000377 size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
Paul Bakker23986e52011-04-24 08:57:21 +0000378 size_t maclen; /*!< MAC length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 unsigned char randbytes[64]; /*!< random bytes */
381 unsigned char premaster[256]; /*!< premaster secret */
382
383 unsigned char iv_enc[16]; /*!< IV (encryption) */
384 unsigned char iv_dec[16]; /*!< IV (decryption) */
385
386 unsigned char mac_enc[32]; /*!< MAC (encryption) */
387 unsigned char mac_dec[32]; /*!< MAC (decryption) */
388
Paul Bakkerca4ab492012-04-18 14:23:57 +0000389 unsigned long ctx_enc[134]; /*!< encryption context */
390 unsigned long ctx_dec[134]; /*!< decryption context */
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Paul Bakker2770fbd2012-07-03 13:30:23 +0000392#if defined(POLARSSL_ZLIB_SUPPORT)
393 z_stream ctx_deflate; /*!< compression context */
394 z_stream ctx_inflate; /*!< decompression context */
395#endif
396
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 /*
398 * TLS extensions
399 */
400 unsigned char *hostname;
Paul Bakker23986e52011-04-24 08:57:21 +0000401 size_t hostname_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402};
403
404#ifdef __cplusplus
405extern "C" {
406#endif
407
Paul Bakkerb68cad62012-08-23 08:34:18 +0000408extern const int ssl_default_ciphersuites[];
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Paul Bakker05ef8352012-05-08 09:17:57 +0000410#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
411extern int (*ssl_hw_record_init)(ssl_context *ssl,
412 const unsigned char *key_enc, const unsigned char *key_dec,
413 const unsigned char *iv_enc, const unsigned char *iv_dec,
414 const unsigned char *mac_enc, const unsigned char *mac_dec);
415extern int (*ssl_hw_record_reset)(ssl_context *ssl);
416extern int (*ssl_hw_record_write)(ssl_context *ssl);
417extern int (*ssl_hw_record_read)(ssl_context *ssl);
418extern int (*ssl_hw_record_finish)(ssl_context *ssl);
419#endif
420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000422 * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
Paul Bakker72f62662011-01-16 21:27:44 +0000423 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000424 * \return a statically allocated array of ciphersuites, the last
425 * entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000426 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000427static inline const int *ssl_list_ciphersuites( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000428{
Paul Bakkere3166ce2011-01-27 17:40:50 +0000429 return ssl_default_ciphersuites;
Paul Bakker72f62662011-01-16 21:27:44 +0000430}
431
432/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000433 * \brief Return the name of the ciphersuite associated with the given
434 * ID
Paul Bakker72f62662011-01-16 21:27:44 +0000435 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000436 * \param ciphersuite_id SSL ciphersuite ID
Paul Bakker72f62662011-01-16 21:27:44 +0000437 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000438 * \return a string containing the ciphersuite name
Paul Bakker72f62662011-01-16 21:27:44 +0000439 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000440const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
441
442/**
443 * \brief Return the ID of the ciphersuite associated with the given
444 * name
445 *
446 * \param ciphersuite_name SSL ciphersuite name
447 *
448 * \return the ID with the ciphersuite or 0 if not found
449 */
450int ssl_get_ciphersuite_id( const char *ciphersuite_name );
Paul Bakker72f62662011-01-16 21:27:44 +0000451
452/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 * \brief Initialize an SSL context
454 *
455 * \param ssl SSL context
456 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000457 * \return 0 if successful, or POLARSSL_ERR_SSL_MALLOC_FAILED if
458 * memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
460int ssl_init( ssl_context *ssl );
461
462/**
Paul Bakker7eb013f2011-10-06 12:37:39 +0000463 * \brief Reset an already initialized SSL context for re-use
464 * while retaining application-set variables, function
465 * pointers and data.
466 *
467 * \param ssl SSL context
Paul Bakker2770fbd2012-07-03 13:30:23 +0000468 * \return 0 if successful, or POLARSSL_ERR_SSL_HW_ACCEL_FAILED or
469 * POLARSSL_ERR_SSL_COMPRESSION_FAILED
Paul Bakker7eb013f2011-10-06 12:37:39 +0000470 */
Paul Bakker2770fbd2012-07-03 13:30:23 +0000471int ssl_session_reset( ssl_context *ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +0000472
473/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 * \brief Set the current endpoint type
475 *
476 * \param ssl SSL context
477 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
478 */
479void ssl_set_endpoint( ssl_context *ssl, int endpoint );
480
481/**
482 * \brief Set the certificate verification mode
483 *
484 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000485 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
487 * SSL_VERIFY_NONE: peer certificate is not checked (default),
488 * this is insecure and SHOULD be avoided.
489 *
490 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
491 * handshake continues even if verification failed;
492 * ssl_get_verify_result() can be called after the
493 * handshake is complete.
494 *
495 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
496 * handshake is aborted if verification failed.
497 */
498void ssl_set_authmode( ssl_context *ssl, int authmode );
499
500/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000501 * \brief Set the verification callback (Optional).
502 *
503 * If set, the verification callback is called once for every
504 * certificate in the chain. The verification function has the
505 * following parameter: (void *parameter, x509_cert certificate,
506 * int certifcate_depth, int preverify_ok). It should
507 * return 0 on SUCCESS.
508 *
509 * \param ssl SSL context
510 * \param f_vrfy verification function
511 * \param p_vrfy verification parameter
512 */
513void ssl_set_verify( ssl_context *ssl,
514 int (*f_vrfy)(void *, x509_cert *, int, int),
515 void *p_vrfy );
516
517/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 * \brief Set the random number generator callback
519 *
520 * \param ssl SSL context
521 * \param f_rng RNG function
522 * \param p_rng RNG parameter
523 */
524void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000525 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 void *p_rng );
527
528/**
529 * \brief Set the debug callback
530 *
531 * \param ssl SSL context
532 * \param f_dbg debug function
533 * \param p_dbg debug parameter
534 */
535void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 void *p_dbg );
538
539/**
540 * \brief Set the underlying BIO read and write callbacks
541 *
542 * \param ssl SSL context
543 * \param f_recv read callback
544 * \param p_recv read parameter
545 * \param f_send write callback
546 * \param p_send write parameter
547 */
548void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +0000549 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +0000550 int (*f_send)(void *, const unsigned char *, size_t), void *p_send );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552/**
553 * \brief Set the session callbacks (server-side only)
554 *
555 * \param ssl SSL context
556 * \param s_get session get callback
557 * \param s_set session set callback
558 */
559void ssl_set_scb( ssl_context *ssl,
560 int (*s_get)(ssl_context *),
561 int (*s_set)(ssl_context *) );
562
563/**
564 * \brief Set the session resuming flag, timeout and data
565 *
566 * \param ssl SSL context
567 * \param resume if 0 (default), the session will not be resumed
568 * \param timeout session timeout in seconds, or 0 (no timeout)
569 * \param session session context
570 */
571void ssl_set_session( ssl_context *ssl, int resume, int timeout,
572 ssl_session *session );
573
574/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000575 * \brief Set the list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000577 * \param ssl SSL context
578 * \param ciphersuites 0-terminated list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Paul Bakkerb68cad62012-08-23 08:34:18 +0000580void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582/**
583 * \brief Set the data required to verify peer certificate
584 *
585 * \param ssl SSL context
586 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000587 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \param peer_cn expected peer CommonName (or NULL)
589 *
590 * \note TODO: add two more parameters: depth and crl
591 */
592void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000593 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595/**
596 * \brief Set own certificate and private key
597 *
598 * \param ssl SSL context
599 * \param own_cert own public certificate
600 * \param rsa_key own private RSA key
601 */
602void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
603 rsa_context *rsa_key );
604
Paul Bakker43b7e352011-01-18 15:27:19 +0000605#if defined(POLARSSL_PKCS11_C)
606/**
607 * \brief Set own certificate and PKCS#11 private key
608 *
609 * \param ssl SSL context
610 * \param own_cert own public certificate
611 * \param pkcs11_key own PKCS#11 RSA key
612 */
613void ssl_set_own_cert_pkcs11( ssl_context *ssl, x509_cert *own_cert,
614 pkcs11_context *pkcs11_key );
615#endif
616
Paul Bakker5121ce52009-01-03 21:22:43 +0000617/**
618 * \brief Set the Diffie-Hellman public P and G values,
619 * read as hexadecimal strings (server-side only)
620 *
621 * \param ssl SSL context
622 * \param dhm_P Diffie-Hellman-Merkle modulus
623 * \param dhm_G Diffie-Hellman-Merkle generator
624 *
625 * \return 0 if successful
626 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000627int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000630 * \brief Set the Diffie-Hellman public P and G values,
631 * read from existing context (server-side only)
632 *
633 * \param ssl SSL context
634 * \param dhm_ctx Diffie-Hellman-Merkle context
635 *
636 * \return 0 if successful
637 */
638int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
639
640/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 * \brief Set hostname for ServerName TLS Extension
642 *
643 *
644 * \param ssl SSL context
645 * \param hostname the server hostname
646 *
Paul Bakkerb15b8512012-01-13 13:44:06 +0000647 * \return 0 if successful or POLARSSL_ERR_SSL_MALLOC_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000649int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651/**
Paul Bakker490ecc82011-10-06 13:04:09 +0000652 * \brief Set the maximum supported version sent from the client side
653 *
654 * \param ssl SSL context
655 * \param major Major version number (only SSL_MAJOR_VERSION_3 supported)
656 * \param minor Minor version number (SSL_MINOR_VERSION_0,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657 * SSL_MINOR_VERSION_1 and SSL_MINOR_VERSION_2,
658 * SSL_MINOR_VERSION_3 supported)
Paul Bakker490ecc82011-10-06 13:04:09 +0000659 */
660void ssl_set_max_version( ssl_context *ssl, int major, int minor );
661
662/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 * \brief Return the number of data bytes available to read
664 *
665 * \param ssl SSL context
666 *
667 * \return how many bytes are available in the read buffer
668 */
Paul Bakker23986e52011-04-24 08:57:21 +0000669size_t ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671/**
672 * \brief Return the result of the certificate verification
673 *
674 * \param ssl SSL context
675 *
676 * \return 0 if successful, or a combination of:
677 * BADCERT_EXPIRED
678 * BADCERT_REVOKED
679 * BADCERT_CN_MISMATCH
680 * BADCERT_NOT_TRUSTED
681 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000682int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
684/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000685 * \brief Return the name of the current ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 *
687 * \param ssl SSL context
688 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000689 * \return a string containing the ciphersuite name
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000691const char *ssl_get_ciphersuite( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693/**
Paul Bakker43ca69c2011-01-15 17:35:19 +0000694 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
695 *
696 * \param ssl SSL context
697 *
698 * \return a string containing the SSL version
699 */
700const char *ssl_get_version( const ssl_context *ssl );
701
702/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 * \brief Perform the SSL handshake
704 *
705 * \param ssl SSL context
706 *
Paul Bakker831a7552011-05-18 13:32:51 +0000707 * \return 0 if successful, POLARSSL_ERR_NET_WANT_READ,
708 * POLARSSL_ERR_NET_WANT_WRITE, or a specific SSL error code.
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 */
710int ssl_handshake( ssl_context *ssl );
711
712/**
713 * \brief Read at most 'len' application data bytes
714 *
715 * \param ssl SSL context
716 * \param buf buffer that will hold the data
717 * \param len how many bytes must be read
718 *
Paul Bakker831a7552011-05-18 13:32:51 +0000719 * \return This function returns the number of bytes read, 0 for EOF,
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 * or a negative error code.
721 */
Paul Bakker23986e52011-04-24 08:57:21 +0000722int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724/**
725 * \brief Write exactly 'len' application data bytes
726 *
727 * \param ssl SSL context
728 * \param buf buffer holding the data
729 * \param len how many bytes must be written
730 *
731 * \return This function returns the number of bytes written,
732 * or a negative error code.
733 *
Paul Bakker831a7552011-05-18 13:32:51 +0000734 * \note When this function returns POLARSSL_ERR_NET_WANT_WRITE,
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 * it must be called later with the *same* arguments,
736 * until it returns a positive value.
737 */
Paul Bakker23986e52011-04-24 08:57:21 +0000738int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
740/**
Paul Bakker0a925182012-04-16 06:46:41 +0000741 * \brief Send an alert message
742 *
743 * \param ssl SSL context
744 * \param level The alert level of the message
745 * (SSL_ALERT_LEVEL_WARNING or SSL_ALERT_LEVEL_FATAL)
746 * \param message The alert message (SSL_ALERT_MSG_*)
747 *
748 * \return 1 if successful, or a specific SSL error code.
749 */
750int ssl_send_alert_message( ssl_context *ssl,
751 unsigned char level,
752 unsigned char message );
753/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000755 *
756 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 */
758int ssl_close_notify( ssl_context *ssl );
759
760/**
761 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000762 *
763 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 */
765void ssl_free( ssl_context *ssl );
766
767/*
768 * Internal functions (do not call directly)
769 */
770int ssl_handshake_client( ssl_context *ssl );
771int ssl_handshake_server( ssl_context *ssl );
772
773int ssl_derive_keys( ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000774
775int ssl_read_record( ssl_context *ssl );
Paul Bakker831a7552011-05-18 13:32:51 +0000776/**
777 * \return 0 if successful, POLARSSL_ERR_SSL_CONN_EOF on EOF or
778 * another negative error code.
779 */
Paul Bakker23986e52011-04-24 08:57:21 +0000780int ssl_fetch_input( ssl_context *ssl, size_t nb_want );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
782int ssl_write_record( ssl_context *ssl );
783int ssl_flush_output( ssl_context *ssl );
784
785int ssl_parse_certificate( ssl_context *ssl );
786int ssl_write_certificate( ssl_context *ssl );
787
788int ssl_parse_change_cipher_spec( ssl_context *ssl );
789int ssl_write_change_cipher_spec( ssl_context *ssl );
790
791int ssl_parse_finished( ssl_context *ssl );
792int ssl_write_finished( ssl_context *ssl );
793
Paul Bakker380da532012-04-18 16:10:25 +0000794void ssl_kickstart_checksum( ssl_context *ssl, int ciphersuite,
795 unsigned char *input_buf, size_t len );
796
Paul Bakker5121ce52009-01-03 21:22:43 +0000797#ifdef __cplusplus
798}
799#endif
800
801#endif /* ssl.h */