blob: 90de64932bf1be799819f77bf03e991e2ca7ea8f [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 Bakker314052f2011-08-15 09:07:52 +000038#include "x509.h"
39#include "config.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000040
41#if defined(POLARSSL_PKCS11_C)
Paul Bakker314052f2011-08-15 09:07:52 +000042#include "pkcs11.h"
Paul Bakker43b7e352011-01-18 15:27:19 +000043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakker09b1ec62011-07-27 16:28:54 +000045#if defined(_MSC_VER) && !defined(inline)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000046#define inline _inline
Paul Bakker569df2c2011-06-21 07:48:07 +000047#else
Paul Bakker09b1ec62011-07-27 16:28:54 +000048#if defined(__ARMCC_VERSION) && !defined(inline)
Paul Bakker569df2c2011-06-21 07:48:07 +000049#define inline __inline
Paul Bakker74fb74e2011-06-21 13:36:18 +000050#endif /* __ARMCC_VERSION */
Paul Bakker569df2c2011-06-21 07:48:07 +000051#endif /*_MSC_VER */
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000052
Paul Bakker13e2dfe2009-07-28 07:18:38 +000053/*
54 * SSL Error codes
55 */
Paul Bakker9d781402011-05-09 16:17:09 +000056#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
57#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
58#define POLARSSL_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
59#define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
Paul Bakker831a7552011-05-18 13:32:51 +000060#define POLARSSL_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
Paul Bakker9d781402011-05-09 16:17:09 +000061#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
62#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
63#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x7400 /**< No session to recover was found. */
64#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
65#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message.*/
66#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
67#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key is not set, but needed. */
68#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
69#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
70#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
71#define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
72#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
73#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
74#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
75#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
76#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
77#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
78#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
79#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
80#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM Read Public. */
81#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM Calculate Secret. */
82#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
83#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
84#define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
Paul Bakker69e095c2011-12-10 21:55:01 +000085#define POLARSSL_ERR_SSL_MALLOC_FAILED -0x7F00 /**< Memory allocation failed */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87/*
88 * Various constants
89 */
90#define SSL_MAJOR_VERSION_3 3
91#define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
92#define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
93#define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +000094#define SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96#define SSL_IS_CLIENT 0
97#define SSL_IS_SERVER 1
98#define SSL_COMPRESS_NULL 0
99
100#define SSL_VERIFY_NONE 0
101#define SSL_VERIFY_OPTIONAL 1
102#define SSL_VERIFY_REQUIRED 2
103
104#define SSL_MAX_CONTENT_LEN 16384
105
106/*
107 * Allow an extra 512 bytes for the record header
108 * and encryption overhead (counter + MAC + padding).
109 */
110#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512)
111
112/*
113 * Supported ciphersuites
114 */
Paul Bakkerfab5c822012-02-06 16:45:10 +0000115#define SSL_RSA_NULL_MD5 0x01 /**< Weak! */
116#define SSL_RSA_NULL_SHA 0x02 /**< Weak! */
117#define SSL_RSA_NULL_SHA256 0x3B /**< Weak! */
Paul Bakker10cd2252012-04-12 21:26:34 +0000118#define SSL_RSA_DES_SHA 0x09 /**< Weak! Not in TLS 1.2 */
119#define SSL_EDH_RSA_DES_SHA 0x15 /**< Weak! Not in TLS 1.2 */
Paul Bakkerfab5c822012-02-06 16:45:10 +0000120
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000121#define SSL_RSA_RC4_128_MD5 0x04
122#define SSL_RSA_RC4_128_SHA 0x05
Paul Bakker10cd2252012-04-12 21:26:34 +0000123
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000124#define SSL_RSA_DES_168_SHA 0x0A
125#define SSL_EDH_RSA_DES_168_SHA 0x16
Paul Bakker10cd2252012-04-12 21:26:34 +0000126
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000127#define SSL_RSA_AES_128_SHA 0x2F
128#define SSL_EDH_RSA_AES_128_SHA 0x33
129#define SSL_RSA_AES_256_SHA 0x35
130#define SSL_EDH_RSA_AES_256_SHA 0x39
Paul Bakker10cd2252012-04-12 21:26:34 +0000131#define SSL_RSA_AES_128_SHA256 0x3C /**< TLS 1.2 */
132#define SSL_RSA_AES_256_SHA256 0x3D /**< TLS 1.2 */
133#define SSL_EDH_RSA_AES_128_SHA256 0x67 /**< TLS 1.2 */
134#define SSL_EDH_RSA_AES_256_SHA256 0x6B /**< TLS 1.2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Paul Bakker10cd2252012-04-12 21:26:34 +0000136#define SSL_RSA_CAMELLIA_128_SHA 0x41
137#define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
138#define SSL_RSA_CAMELLIA_256_SHA 0x84
139#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
140#define SSL_RSA_CAMELLIA_128_SHA256 0xBA /**< TLS 1.2 */
141#define SSL_EDH_RSA_CAMELLIA_128_SHA256 0xBE /**< TLS 1.2 */
142#define SSL_RSA_CAMELLIA_256_SHA256 0xC0 /**< TLS 1.2 */
143#define SSL_EDH_RSA_CAMELLIA_256_SHA256 0xC4 /**< TLS 1.2 */
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145/*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000146 * Supported Signature and Hash algorithms (For TLS 1.2)
147 */
148#define SSL_HASH_NONE 0
149#define SSL_HASH_MD5 1
150#define SSL_HASH_SHA1 2
151#define SSL_HASH_SHA224 3
152#define SSL_HASH_SHA256 4
153#define SSL_HASH_SHA384 5
154#define SSL_HASH_SHA512 6
155
156#define SSL_SIG_RSA 1
157
158/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * Message, alert and handshake types
160 */
161#define SSL_MSG_CHANGE_CIPHER_SPEC 20
162#define SSL_MSG_ALERT 21
163#define SSL_MSG_HANDSHAKE 22
164#define SSL_MSG_APPLICATION_DATA 23
165
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000166#define SSL_ALERT_LEVEL_WARNING 1
167#define SSL_ALERT_LEVEL_FATAL 2
168
Paul Bakkere93dfa72012-04-10 08:03:03 +0000169#define SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
170#define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
171#define SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
172#define SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
173#define SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
174#define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
175#define SSL_ALERT_MSG_HANDSHAKE_FAILURE 41 /* 0x29 */
176#define SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
177#define SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
178#define SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
179#define SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
180#define SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
181#define SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
182#define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
183#define SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
184#define SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
185#define SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
186#define SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
187#define SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
188#define SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
189#define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
190#define SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
191#define SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
192#define SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000193#define SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195#define SSL_HS_HELLO_REQUEST 0
196#define SSL_HS_CLIENT_HELLO 1
197#define SSL_HS_SERVER_HELLO 2
198#define SSL_HS_CERTIFICATE 11
199#define SSL_HS_SERVER_KEY_EXCHANGE 12
200#define SSL_HS_CERTIFICATE_REQUEST 13
201#define SSL_HS_SERVER_HELLO_DONE 14
202#define SSL_HS_CERTIFICATE_VERIFY 15
203#define SSL_HS_CLIENT_KEY_EXCHANGE 16
204#define SSL_HS_FINISHED 20
205
206/*
207 * TLS extensions
208 */
209#define TLS_EXT_SERVERNAME 0
210#define TLS_EXT_SERVERNAME_HOSTNAME 0
211
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000212#define TLS_EXT_SIG_ALG 13
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/*
215 * SSL state machine
216 */
217typedef enum
218{
219 SSL_HELLO_REQUEST,
220 SSL_CLIENT_HELLO,
221 SSL_SERVER_HELLO,
222 SSL_SERVER_CERTIFICATE,
223 SSL_SERVER_KEY_EXCHANGE,
224 SSL_CERTIFICATE_REQUEST,
225 SSL_SERVER_HELLO_DONE,
226 SSL_CLIENT_CERTIFICATE,
227 SSL_CLIENT_KEY_EXCHANGE,
228 SSL_CERTIFICATE_VERIFY,
229 SSL_CLIENT_CHANGE_CIPHER_SPEC,
230 SSL_CLIENT_FINISHED,
231 SSL_SERVER_CHANGE_CIPHER_SPEC,
232 SSL_SERVER_FINISHED,
233 SSL_FLUSH_BUFFERS,
234 SSL_HANDSHAKE_OVER
235}
236ssl_states;
237
238typedef struct _ssl_session ssl_session;
239typedef struct _ssl_context ssl_context;
240
241/*
242 * This structure is used for session resuming.
243 */
244struct _ssl_session
245{
246 time_t start; /*!< starting time */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000247 int ciphersuite; /*!< chosen ciphersuite */
Paul Bakker23986e52011-04-24 08:57:21 +0000248 size_t length; /*!< session id length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 unsigned char id[32]; /*!< session identifier */
250 unsigned char master[48]; /*!< the master secret */
251 ssl_session *next; /*!< next session entry */
252};
253
254struct _ssl_context
255{
256 /*
257 * Miscellaneous
258 */
259 int state; /*!< SSL handshake: current state */
260
261 int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
262 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
263
264 int max_major_ver; /*!< max. major version from client */
265 int max_minor_ver; /*!< max. minor version from client */
266
267 /*
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000268 * Callbacks (RNG, debug, I/O, verification)
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000270 int (*f_rng)(void *, unsigned char *, size_t);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000271 void (*f_dbg)(void *, int, const char *);
Paul Bakker23986e52011-04-24 08:57:21 +0000272 int (*f_recv)(void *, unsigned char *, size_t);
Paul Bakker39bb4182011-06-21 07:36:43 +0000273 int (*f_send)(void *, const unsigned char *, size_t);
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000274 int (*f_vrfy)(void *, x509_cert *, int, int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276 void *p_rng; /*!< context for the RNG function */
277 void *p_dbg; /*!< context for the debug function */
278 void *p_recv; /*!< context for reading operations */
279 void *p_send; /*!< context for writing operations */
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000280 void *p_vrfy; /*!< context for verification */
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282 /*
283 * Session layer
284 */
285 int resume; /*!< session resuming flag */
286 int timeout; /*!< sess. expiration time */
287 ssl_session *session; /*!< current session data */
288 int (*s_get)(ssl_context *); /*!< (server) get callback */
289 int (*s_set)(ssl_context *); /*!< (server) set callback */
290
291 /*
292 * Record layer (incoming data)
293 */
294 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
295 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
296 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
297 unsigned char *in_offt; /*!< read offset in application data */
298
299 int in_msgtype; /*!< record header: message type */
Paul Bakker23986e52011-04-24 08:57:21 +0000300 size_t in_msglen; /*!< record header: message length */
301 size_t in_left; /*!< amount of data read so far */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Paul Bakker23986e52011-04-24 08:57:21 +0000303 size_t in_hslen; /*!< current handshake message length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 int nb_zero; /*!< # of 0-length encrypted messages */
305
306 /*
307 * Record layer (outgoing data)
308 */
309 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
310 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
311 unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
312
313 int out_msgtype; /*!< record header: message type */
Paul Bakker23986e52011-04-24 08:57:21 +0000314 size_t out_msglen; /*!< record header: message length */
315 size_t out_left; /*!< amount of data not yet written */
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 /*
318 * PKI layer
319 */
320 rsa_context *rsa_key; /*!< own RSA private key */
Paul Bakker43b7e352011-01-18 15:27:19 +0000321#if defined(POLARSSL_PKCS11_C)
322 pkcs11_context *pkcs11_key; /*!< own PKCS#11 RSA private key */
323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 x509_cert *own_cert; /*!< own X.509 certificate */
325 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000326 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 x509_cert *peer_cert; /*!< peer X.509 cert chain */
Paul Bakker57b79142010-03-24 06:51:15 +0000328 const char *peer_cn; /*!< expected peer CN */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 int endpoint; /*!< 0: client, 1: server */
331 int authmode; /*!< verification mode */
332 int client_auth; /*!< flag for client auth. */
333 int verify_result; /*!< verification result */
334
335 /*
336 * Crypto layer
337 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338 dhm_context dhm_ctx; /*!< DHM key exchange */
339 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000341 sha2_context fin_sha2; /*!< Finished SHA-256 checksum */
342
343 void (*calc_finished)(ssl_context *, unsigned char *, int);
344 int (*tls_prf)(unsigned char *, size_t, char *,
345 unsigned char *, size_t,
346 unsigned char *, size_t);
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 int do_crypt; /*!< en(de)cryption flag */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000349 int *ciphersuites; /*!< allowed ciphersuites */
Paul Bakker23986e52011-04-24 08:57:21 +0000350 size_t pmslen; /*!< premaster length */
351 unsigned int keylen; /*!< symmetric key length */
352 size_t minlen; /*!< min. ciphertext length */
353 size_t ivlen; /*!< IV length */
354 size_t maclen; /*!< MAC length */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356 unsigned char randbytes[64]; /*!< random bytes */
357 unsigned char premaster[256]; /*!< premaster secret */
358
359 unsigned char iv_enc[16]; /*!< IV (encryption) */
360 unsigned char iv_dec[16]; /*!< IV (decryption) */
361
362 unsigned char mac_enc[32]; /*!< MAC (encryption) */
363 unsigned char mac_dec[32]; /*!< MAC (decryption) */
364
365 unsigned long ctx_enc[128]; /*!< encryption context */
366 unsigned long ctx_dec[128]; /*!< decryption context */
367
368 /*
369 * TLS extensions
370 */
371 unsigned char *hostname;
Paul Bakker23986e52011-04-24 08:57:21 +0000372 size_t hostname_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373};
374
375#ifdef __cplusplus
376extern "C" {
377#endif
378
Paul Bakkere3166ce2011-01-27 17:40:50 +0000379extern int ssl_default_ciphersuites[];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000382 * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
Paul Bakker72f62662011-01-16 21:27:44 +0000383 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000384 * \return a statically allocated array of ciphersuites, the last
385 * entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000386 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000387static inline const int *ssl_list_ciphersuites( void )
Paul Bakker72f62662011-01-16 21:27:44 +0000388{
Paul Bakkere3166ce2011-01-27 17:40:50 +0000389 return ssl_default_ciphersuites;
Paul Bakker72f62662011-01-16 21:27:44 +0000390}
391
392/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000393 * \brief Return the name of the ciphersuite associated with the given
394 * ID
Paul Bakker72f62662011-01-16 21:27:44 +0000395 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000396 * \param ciphersuite_id SSL ciphersuite ID
Paul Bakker72f62662011-01-16 21:27:44 +0000397 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000398 * \return a string containing the ciphersuite name
Paul Bakker72f62662011-01-16 21:27:44 +0000399 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000400const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
401
402/**
403 * \brief Return the ID of the ciphersuite associated with the given
404 * name
405 *
406 * \param ciphersuite_name SSL ciphersuite name
407 *
408 * \return the ID with the ciphersuite or 0 if not found
409 */
410int ssl_get_ciphersuite_id( const char *ciphersuite_name );
Paul Bakker72f62662011-01-16 21:27:44 +0000411
412/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 * \brief Initialize an SSL context
414 *
415 * \param ssl SSL context
416 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000417 * \return 0 if successful, or POLARSSL_ERR_SSL_MALLOC_FAILED if
418 * memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 */
420int ssl_init( ssl_context *ssl );
421
422/**
Paul Bakker7eb013f2011-10-06 12:37:39 +0000423 * \brief Reset an already initialized SSL context for re-use
424 * while retaining application-set variables, function
425 * pointers and data.
426 *
427 * \param ssl SSL context
428 */
429void ssl_session_reset( ssl_context *ssl );
430
431/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \brief Set the current endpoint type
433 *
434 * \param ssl SSL context
435 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
436 */
437void ssl_set_endpoint( ssl_context *ssl, int endpoint );
438
439/**
440 * \brief Set the certificate verification mode
441 *
442 * \param ssl SSL context
Paul Bakker37ca75d2011-01-06 12:28:03 +0000443 * \param authmode can be:
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 *
445 * SSL_VERIFY_NONE: peer certificate is not checked (default),
446 * this is insecure and SHOULD be avoided.
447 *
448 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
449 * handshake continues even if verification failed;
450 * ssl_get_verify_result() can be called after the
451 * handshake is complete.
452 *
453 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
454 * handshake is aborted if verification failed.
455 */
456void ssl_set_authmode( ssl_context *ssl, int authmode );
457
458/**
Paul Bakkerb63b0af2011-01-13 17:54:59 +0000459 * \brief Set the verification callback (Optional).
460 *
461 * If set, the verification callback is called once for every
462 * certificate in the chain. The verification function has the
463 * following parameter: (void *parameter, x509_cert certificate,
464 * int certifcate_depth, int preverify_ok). It should
465 * return 0 on SUCCESS.
466 *
467 * \param ssl SSL context
468 * \param f_vrfy verification function
469 * \param p_vrfy verification parameter
470 */
471void ssl_set_verify( ssl_context *ssl,
472 int (*f_vrfy)(void *, x509_cert *, int, int),
473 void *p_vrfy );
474
475/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \brief Set the random number generator callback
477 *
478 * \param ssl SSL context
479 * \param f_rng RNG function
480 * \param p_rng RNG parameter
481 */
482void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000483 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 void *p_rng );
485
486/**
487 * \brief Set the debug callback
488 *
489 * \param ssl SSL context
490 * \param f_dbg debug function
491 * \param p_dbg debug parameter
492 */
493void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 void *p_dbg );
496
497/**
498 * \brief Set the underlying BIO read and write callbacks
499 *
500 * \param ssl SSL context
501 * \param f_recv read callback
502 * \param p_recv read parameter
503 * \param f_send write callback
504 * \param p_send write parameter
505 */
506void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +0000507 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +0000508 int (*f_send)(void *, const unsigned char *, size_t), void *p_send );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
511 * \brief Set the session callbacks (server-side only)
512 *
513 * \param ssl SSL context
514 * \param s_get session get callback
515 * \param s_set session set callback
516 */
517void ssl_set_scb( ssl_context *ssl,
518 int (*s_get)(ssl_context *),
519 int (*s_set)(ssl_context *) );
520
521/**
522 * \brief Set the session resuming flag, timeout and data
523 *
524 * \param ssl SSL context
525 * \param resume if 0 (default), the session will not be resumed
526 * \param timeout session timeout in seconds, or 0 (no timeout)
527 * \param session session context
528 */
529void ssl_set_session( ssl_context *ssl, int resume, int timeout,
530 ssl_session *session );
531
532/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000533 * \brief Set the list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000535 * \param ssl SSL context
536 * \param ciphersuites 0-terminated list of allowed ciphersuites
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000538void ssl_set_ciphersuites( ssl_context *ssl, int *ciphersuites );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
541 * \brief Set the data required to verify peer certificate
542 *
543 * \param ssl SSL context
544 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000545 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * \param peer_cn expected peer CommonName (or NULL)
547 *
548 * \note TODO: add two more parameters: depth and crl
549 */
550void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +0000551 x509_crl *ca_crl, const char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/**
554 * \brief Set own certificate and private key
555 *
556 * \param ssl SSL context
557 * \param own_cert own public certificate
558 * \param rsa_key own private RSA key
559 */
560void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
561 rsa_context *rsa_key );
562
Paul Bakker43b7e352011-01-18 15:27:19 +0000563#if defined(POLARSSL_PKCS11_C)
564/**
565 * \brief Set own certificate and PKCS#11 private key
566 *
567 * \param ssl SSL context
568 * \param own_cert own public certificate
569 * \param pkcs11_key own PKCS#11 RSA key
570 */
571void ssl_set_own_cert_pkcs11( ssl_context *ssl, x509_cert *own_cert,
572 pkcs11_context *pkcs11_key );
573#endif
574
Paul Bakker5121ce52009-01-03 21:22:43 +0000575/**
576 * \brief Set the Diffie-Hellman public P and G values,
577 * read as hexadecimal strings (server-side only)
578 *
579 * \param ssl SSL context
580 * \param dhm_P Diffie-Hellman-Merkle modulus
581 * \param dhm_G Diffie-Hellman-Merkle generator
582 *
583 * \return 0 if successful
584 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000585int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587/**
Paul Bakker1b57b062011-01-06 15:48:19 +0000588 * \brief Set the Diffie-Hellman public P and G values,
589 * read from existing context (server-side only)
590 *
591 * \param ssl SSL context
592 * \param dhm_ctx Diffie-Hellman-Merkle context
593 *
594 * \return 0 if successful
595 */
596int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
597
598/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 * \brief Set hostname for ServerName TLS Extension
600 *
601 *
602 * \param ssl SSL context
603 * \param hostname the server hostname
604 *
Paul Bakkerb15b8512012-01-13 13:44:06 +0000605 * \return 0 if successful or POLARSSL_ERR_SSL_MALLOC_FAILED
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000607int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609/**
Paul Bakker490ecc82011-10-06 13:04:09 +0000610 * \brief Set the maximum supported version sent from the client side
611 *
612 * \param ssl SSL context
613 * \param major Major version number (only SSL_MAJOR_VERSION_3 supported)
614 * \param minor Minor version number (SSL_MINOR_VERSION_0,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 * SSL_MINOR_VERSION_1 and SSL_MINOR_VERSION_2,
616 * SSL_MINOR_VERSION_3 supported)
Paul Bakker490ecc82011-10-06 13:04:09 +0000617 */
618void ssl_set_max_version( ssl_context *ssl, int major, int minor );
619
620/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * \brief Return the number of data bytes available to read
622 *
623 * \param ssl SSL context
624 *
625 * \return how many bytes are available in the read buffer
626 */
Paul Bakker23986e52011-04-24 08:57:21 +0000627size_t ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/**
630 * \brief Return the result of the certificate verification
631 *
632 * \param ssl SSL context
633 *
634 * \return 0 if successful, or a combination of:
635 * BADCERT_EXPIRED
636 * BADCERT_REVOKED
637 * BADCERT_CN_MISMATCH
638 * BADCERT_NOT_TRUSTED
639 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000640int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642/**
Paul Bakkere3166ce2011-01-27 17:40:50 +0000643 * \brief Return the name of the current ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 *
645 * \param ssl SSL context
646 *
Paul Bakkere3166ce2011-01-27 17:40:50 +0000647 * \return a string containing the ciphersuite name
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 */
Paul Bakkere3166ce2011-01-27 17:40:50 +0000649const char *ssl_get_ciphersuite( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651/**
Paul Bakker43ca69c2011-01-15 17:35:19 +0000652 * \brief Return the current SSL version (SSLv3/TLSv1/etc)
653 *
654 * \param ssl SSL context
655 *
656 * \return a string containing the SSL version
657 */
658const char *ssl_get_version( const ssl_context *ssl );
659
660/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * \brief Perform the SSL handshake
662 *
663 * \param ssl SSL context
664 *
Paul Bakker831a7552011-05-18 13:32:51 +0000665 * \return 0 if successful, POLARSSL_ERR_NET_WANT_READ,
666 * POLARSSL_ERR_NET_WANT_WRITE, or a specific SSL error code.
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 */
668int ssl_handshake( ssl_context *ssl );
669
670/**
671 * \brief Read at most 'len' application data bytes
672 *
673 * \param ssl SSL context
674 * \param buf buffer that will hold the data
675 * \param len how many bytes must be read
676 *
Paul Bakker831a7552011-05-18 13:32:51 +0000677 * \return This function returns the number of bytes read, 0 for EOF,
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * or a negative error code.
679 */
Paul Bakker23986e52011-04-24 08:57:21 +0000680int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682/**
683 * \brief Write exactly 'len' application data bytes
684 *
685 * \param ssl SSL context
686 * \param buf buffer holding the data
687 * \param len how many bytes must be written
688 *
689 * \return This function returns the number of bytes written,
690 * or a negative error code.
691 *
Paul Bakker831a7552011-05-18 13:32:51 +0000692 * \note When this function returns POLARSSL_ERR_NET_WANT_WRITE,
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 * it must be called later with the *same* arguments,
694 * until it returns a positive value.
695 */
Paul Bakker23986e52011-04-24 08:57:21 +0000696int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
698/**
Paul Bakker0a925182012-04-16 06:46:41 +0000699 * \brief Send an alert message
700 *
701 * \param ssl SSL context
702 * \param level The alert level of the message
703 * (SSL_ALERT_LEVEL_WARNING or SSL_ALERT_LEVEL_FATAL)
704 * \param message The alert message (SSL_ALERT_MSG_*)
705 *
706 * \return 1 if successful, or a specific SSL error code.
707 */
708int ssl_send_alert_message( ssl_context *ssl,
709 unsigned char level,
710 unsigned char message );
711/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000713 *
714 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 */
716int ssl_close_notify( ssl_context *ssl );
717
718/**
719 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000720 *
721 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 */
723void ssl_free( ssl_context *ssl );
724
725/*
726 * Internal functions (do not call directly)
727 */
728int ssl_handshake_client( ssl_context *ssl );
729int ssl_handshake_server( ssl_context *ssl );
730
731int ssl_derive_keys( ssl_context *ssl );
732void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
733
734int ssl_read_record( ssl_context *ssl );
Paul Bakker831a7552011-05-18 13:32:51 +0000735/**
736 * \return 0 if successful, POLARSSL_ERR_SSL_CONN_EOF on EOF or
737 * another negative error code.
738 */
Paul Bakker23986e52011-04-24 08:57:21 +0000739int ssl_fetch_input( ssl_context *ssl, size_t nb_want );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741int ssl_write_record( ssl_context *ssl );
742int ssl_flush_output( ssl_context *ssl );
743
744int ssl_parse_certificate( ssl_context *ssl );
745int ssl_write_certificate( ssl_context *ssl );
746
747int ssl_parse_change_cipher_spec( ssl_context *ssl );
748int ssl_write_change_cipher_spec( ssl_context *ssl );
749
750int ssl_parse_finished( ssl_context *ssl );
751int ssl_write_finished( ssl_context *ssl );
752
753#ifdef __cplusplus
754}
755#endif
756
757#endif /* ssl.h */