blob: 649b547d73cbdc27b04ff5d0b004a8c33b8a49ec [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file ssl.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerfc8c4362010-03-21 17:37:16 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_SSL_H
22#define POLARSSL_SSL_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
24#include <time.h>
25
Paul Bakker8e831ed2009-01-03 21:24:11 +000026#include "polarssl/net.h"
27#include "polarssl/dhm.h"
28#include "polarssl/rsa.h"
29#include "polarssl/md5.h"
30#include "polarssl/sha1.h"
31#include "polarssl/x509.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker13e2dfe2009-07-28 07:18:38 +000033/*
34 * SSL Error codes
35 */
Paul Bakker3391b122009-07-28 20:11:54 +000036#define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x1000
37#define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x1800
38#define POLARSSL_ERR_SSL_INVALID_MAC -0x2000
39#define POLARSSL_ERR_SSL_INVALID_RECORD -0x2800
40#define POLARSSL_ERR_SSL_INVALID_MODULUS_SIZE -0x3000
41#define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x3800
42#define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x4000
43#define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x4800
44#define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x5000
45#define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x5800
46#define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x6000
47#define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x6800
48#define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7000
49#define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7800
50#define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x8000
51#define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x8800
52#define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x9000
53#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x9800
54#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0xA000
55#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0xA800
56#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0xB000
57#define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0xB800
58#define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0xC000
59#define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0xC800
60#define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0xD000
61#define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0xD800
62#define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0xE000
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/*
65 * Various constants
66 */
67#define SSL_MAJOR_VERSION_3 3
68#define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
69#define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
70#define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
71
72#define SSL_IS_CLIENT 0
73#define SSL_IS_SERVER 1
74#define SSL_COMPRESS_NULL 0
75
76#define SSL_VERIFY_NONE 0
77#define SSL_VERIFY_OPTIONAL 1
78#define SSL_VERIFY_REQUIRED 2
79
80#define SSL_MAX_CONTENT_LEN 16384
81
82/*
83 * Allow an extra 512 bytes for the record header
84 * and encryption overhead (counter + MAC + padding).
85 */
86#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512)
87
88/*
89 * Supported ciphersuites
90 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000091#define SSL_RSA_RC4_128_MD5 4
92#define SSL_RSA_RC4_128_SHA 5
93#define SSL_RSA_DES_168_SHA 10
94#define SSL_EDH_RSA_DES_168_SHA 22
95#define SSL_RSA_AES_128_SHA 47
96#define SSL_RSA_AES_256_SHA 53
97#define SSL_EDH_RSA_AES_256_SHA 57
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Paul Bakkerff60ee62010-03-16 21:09:09 +000099#define SSL_RSA_CAMELLIA_128_SHA 0x41
100#define SSL_RSA_CAMELLIA_256_SHA 0x84
101#define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
Paul Bakkerb5ef0ba2009-01-11 20:25:36 +0000102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103/*
104 * Message, alert and handshake types
105 */
106#define SSL_MSG_CHANGE_CIPHER_SPEC 20
107#define SSL_MSG_ALERT 21
108#define SSL_MSG_HANDSHAKE 22
109#define SSL_MSG_APPLICATION_DATA 23
110
111#define SSL_ALERT_CLOSE_NOTIFY 0
112#define SSL_ALERT_WARNING 1
113#define SSL_ALERT_FATAL 2
114#define SSL_ALERT_NO_CERTIFICATE 41
115
116#define SSL_HS_HELLO_REQUEST 0
117#define SSL_HS_CLIENT_HELLO 1
118#define SSL_HS_SERVER_HELLO 2
119#define SSL_HS_CERTIFICATE 11
120#define SSL_HS_SERVER_KEY_EXCHANGE 12
121#define SSL_HS_CERTIFICATE_REQUEST 13
122#define SSL_HS_SERVER_HELLO_DONE 14
123#define SSL_HS_CERTIFICATE_VERIFY 15
124#define SSL_HS_CLIENT_KEY_EXCHANGE 16
125#define SSL_HS_FINISHED 20
126
127/*
128 * TLS extensions
129 */
130#define TLS_EXT_SERVERNAME 0
131#define TLS_EXT_SERVERNAME_HOSTNAME 0
132
133/*
134 * SSL state machine
135 */
136typedef enum
137{
138 SSL_HELLO_REQUEST,
139 SSL_CLIENT_HELLO,
140 SSL_SERVER_HELLO,
141 SSL_SERVER_CERTIFICATE,
142 SSL_SERVER_KEY_EXCHANGE,
143 SSL_CERTIFICATE_REQUEST,
144 SSL_SERVER_HELLO_DONE,
145 SSL_CLIENT_CERTIFICATE,
146 SSL_CLIENT_KEY_EXCHANGE,
147 SSL_CERTIFICATE_VERIFY,
148 SSL_CLIENT_CHANGE_CIPHER_SPEC,
149 SSL_CLIENT_FINISHED,
150 SSL_SERVER_CHANGE_CIPHER_SPEC,
151 SSL_SERVER_FINISHED,
152 SSL_FLUSH_BUFFERS,
153 SSL_HANDSHAKE_OVER
154}
155ssl_states;
156
157typedef struct _ssl_session ssl_session;
158typedef struct _ssl_context ssl_context;
159
160/*
161 * This structure is used for session resuming.
162 */
163struct _ssl_session
164{
165 time_t start; /*!< starting time */
166 int cipher; /*!< chosen cipher */
167 int length; /*!< session id length */
168 unsigned char id[32]; /*!< session identifier */
169 unsigned char master[48]; /*!< the master secret */
170 ssl_session *next; /*!< next session entry */
171};
172
173struct _ssl_context
174{
175 /*
176 * Miscellaneous
177 */
178 int state; /*!< SSL handshake: current state */
179
180 int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
181 int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
182
183 int max_major_ver; /*!< max. major version from client */
184 int max_minor_ver; /*!< max. minor version from client */
185
186 /*
187 * Callbacks (RNG, debug, I/O)
188 */
189 int (*f_rng)(void *);
Paul Bakkerff60ee62010-03-16 21:09:09 +0000190 void (*f_dbg)(void *, int, const char *);
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 int (*f_recv)(void *, unsigned char *, int);
192 int (*f_send)(void *, unsigned char *, int);
193
194 void *p_rng; /*!< context for the RNG function */
195 void *p_dbg; /*!< context for the debug function */
196 void *p_recv; /*!< context for reading operations */
197 void *p_send; /*!< context for writing operations */
198
199 /*
200 * Session layer
201 */
202 int resume; /*!< session resuming flag */
203 int timeout; /*!< sess. expiration time */
204 ssl_session *session; /*!< current session data */
205 int (*s_get)(ssl_context *); /*!< (server) get callback */
206 int (*s_set)(ssl_context *); /*!< (server) set callback */
207
208 /*
209 * Record layer (incoming data)
210 */
211 unsigned char *in_ctr; /*!< 64-bit incoming message counter */
212 unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
213 unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
214 unsigned char *in_offt; /*!< read offset in application data */
215
216 int in_msgtype; /*!< record header: message type */
217 int in_msglen; /*!< record header: message length */
218 int in_left; /*!< amount of data read so far */
219
220 int in_hslen; /*!< current handshake message length */
221 int nb_zero; /*!< # of 0-length encrypted messages */
222
223 /*
224 * Record layer (outgoing data)
225 */
226 unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
227 unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
228 unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
229
230 int out_msgtype; /*!< record header: message type */
231 int out_msglen; /*!< record header: message length */
232 int out_left; /*!< amount of data not yet written */
233
234 /*
235 * PKI layer
236 */
237 rsa_context *rsa_key; /*!< own RSA private key */
238 x509_cert *own_cert; /*!< own X.509 certificate */
239 x509_cert *ca_chain; /*!< own trusted CA chain */
Paul Bakker40ea7de2009-05-03 10:18:48 +0000240 x509_crl *ca_crl; /*!< trusted CA CRLs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 x509_cert *peer_cert; /*!< peer X.509 cert chain */
242 char *peer_cn; /*!< expected peer CN */
243
244 int endpoint; /*!< 0: client, 1: server */
245 int authmode; /*!< verification mode */
246 int client_auth; /*!< flag for client auth. */
247 int verify_result; /*!< verification result */
248
249 /*
250 * Crypto layer
251 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000252 dhm_context dhm_ctx; /*!< DHM key exchange */
253 md5_context fin_md5; /*!< Finished MD5 checksum */
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
255
256 int do_crypt; /*!< en(de)cryption flag */
257 int *ciphers; /*!< allowed ciphersuites */
258 int pmslen; /*!< premaster length */
259 int keylen; /*!< symmetric key length */
260 int minlen; /*!< min. ciphertext length */
261 int ivlen; /*!< IV length */
262 int maclen; /*!< MAC length */
263
264 unsigned char randbytes[64]; /*!< random bytes */
265 unsigned char premaster[256]; /*!< premaster secret */
266
267 unsigned char iv_enc[16]; /*!< IV (encryption) */
268 unsigned char iv_dec[16]; /*!< IV (decryption) */
269
270 unsigned char mac_enc[32]; /*!< MAC (encryption) */
271 unsigned char mac_dec[32]; /*!< MAC (decryption) */
272
273 unsigned long ctx_enc[128]; /*!< encryption context */
274 unsigned long ctx_dec[128]; /*!< decryption context */
275
276 /*
277 * TLS extensions
278 */
279 unsigned char *hostname;
280 unsigned long hostname_len;
281};
282
283#ifdef __cplusplus
284extern "C" {
285#endif
286
287extern int ssl_default_ciphers[];
288
289/**
290 * \brief Initialize an SSL context
291 *
292 * \param ssl SSL context
293 *
294 * \return 0 if successful, or 1 if memory allocation failed
295 */
296int ssl_init( ssl_context *ssl );
297
298/**
299 * \brief Set the current endpoint type
300 *
301 * \param ssl SSL context
302 * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
303 */
304void ssl_set_endpoint( ssl_context *ssl, int endpoint );
305
306/**
307 * \brief Set the certificate verification mode
308 *
309 * \param ssl SSL context
310 * \param mode can be:
311 *
312 * SSL_VERIFY_NONE: peer certificate is not checked (default),
313 * this is insecure and SHOULD be avoided.
314 *
315 * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
316 * handshake continues even if verification failed;
317 * ssl_get_verify_result() can be called after the
318 * handshake is complete.
319 *
320 * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
321 * handshake is aborted if verification failed.
322 */
323void ssl_set_authmode( ssl_context *ssl, int authmode );
324
325/**
326 * \brief Set the random number generator callback
327 *
328 * \param ssl SSL context
329 * \param f_rng RNG function
330 * \param p_rng RNG parameter
331 */
332void ssl_set_rng( ssl_context *ssl,
333 int (*f_rng)(void *),
334 void *p_rng );
335
336/**
337 * \brief Set the debug callback
338 *
339 * \param ssl SSL context
340 * \param f_dbg debug function
341 * \param p_dbg debug parameter
342 */
343void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000344 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 void *p_dbg );
346
347/**
348 * \brief Set the underlying BIO read and write callbacks
349 *
350 * \param ssl SSL context
351 * \param f_recv read callback
352 * \param p_recv read parameter
353 * \param f_send write callback
354 * \param p_send write parameter
355 */
356void ssl_set_bio( ssl_context *ssl,
357 int (*f_recv)(void *, unsigned char *, int), void *p_recv,
358 int (*f_send)(void *, unsigned char *, int), void *p_send );
359
360/**
361 * \brief Set the session callbacks (server-side only)
362 *
363 * \param ssl SSL context
364 * \param s_get session get callback
365 * \param s_set session set callback
366 */
367void ssl_set_scb( ssl_context *ssl,
368 int (*s_get)(ssl_context *),
369 int (*s_set)(ssl_context *) );
370
371/**
372 * \brief Set the session resuming flag, timeout and data
373 *
374 * \param ssl SSL context
375 * \param resume if 0 (default), the session will not be resumed
376 * \param timeout session timeout in seconds, or 0 (no timeout)
377 * \param session session context
378 */
379void ssl_set_session( ssl_context *ssl, int resume, int timeout,
380 ssl_session *session );
381
382/**
383 * \brief Set the list of allowed ciphersuites
384 *
385 * \param ssl SSL context
386 * \param ciphers 0-terminated list of allowed ciphers
387 */
388void ssl_set_ciphers( ssl_context *ssl, int *ciphers );
389
390/**
391 * \brief Set the data required to verify peer certificate
392 *
393 * \param ssl SSL context
394 * \param ca_chain trusted CA chain
Paul Bakker40ea7de2009-05-03 10:18:48 +0000395 * \param ca_crl trusted CA CRLs
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * \param peer_cn expected peer CommonName (or NULL)
397 *
398 * \note TODO: add two more parameters: depth and crl
399 */
400void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
Paul Bakker40ea7de2009-05-03 10:18:48 +0000401 x509_crl *ca_crl, char *peer_cn );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403/**
404 * \brief Set own certificate and private key
405 *
406 * \param ssl SSL context
407 * \param own_cert own public certificate
408 * \param rsa_key own private RSA key
409 */
410void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
411 rsa_context *rsa_key );
412
413/**
414 * \brief Set the Diffie-Hellman public P and G values,
415 * read as hexadecimal strings (server-side only)
416 *
417 * \param ssl SSL context
418 * \param dhm_P Diffie-Hellman-Merkle modulus
419 * \param dhm_G Diffie-Hellman-Merkle generator
420 *
421 * \return 0 if successful
422 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
426 * \brief Set hostname for ServerName TLS Extension
427 *
428 *
429 * \param ssl SSL context
430 * \param hostname the server hostname
431 *
432 * \return 0 if successful
433 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000434int ssl_set_hostname( ssl_context *ssl, const char *hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436/**
437 * \brief Return the number of data bytes available to read
438 *
439 * \param ssl SSL context
440 *
441 * \return how many bytes are available in the read buffer
442 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000443int ssl_get_bytes_avail( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
446 * \brief Return the result of the certificate verification
447 *
448 * \param ssl SSL context
449 *
450 * \return 0 if successful, or a combination of:
451 * BADCERT_EXPIRED
452 * BADCERT_REVOKED
453 * BADCERT_CN_MISMATCH
454 * BADCERT_NOT_TRUSTED
455 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000456int ssl_get_verify_result( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458/**
459 * \brief Return the name of the current cipher
460 *
461 * \param ssl SSL context
462 *
463 * \return a string containing the cipher name
464 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000465const char *ssl_get_cipher( const ssl_context *ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467/**
468 * \brief Perform the SSL handshake
469 *
470 * \param ssl SSL context
471 *
Paul Bakker40e46942009-01-03 21:51:57 +0000472 * \return 0 if successful, POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 * or a specific SSL error code.
474 */
475int ssl_handshake( ssl_context *ssl );
476
477/**
478 * \brief Read at most 'len' application data bytes
479 *
480 * \param ssl SSL context
481 * \param buf buffer that will hold the data
482 * \param len how many bytes must be read
483 *
484 * \return This function returns the number of bytes read,
485 * or a negative error code.
486 */
487int ssl_read( ssl_context *ssl, unsigned char *buf, int len );
488
489/**
490 * \brief Write exactly 'len' application data bytes
491 *
492 * \param ssl SSL context
493 * \param buf buffer holding the data
494 * \param len how many bytes must be written
495 *
496 * \return This function returns the number of bytes written,
497 * or a negative error code.
498 *
Paul Bakker40e46942009-01-03 21:51:57 +0000499 * \note When this function returns POLARSSL_ERR_NET_TRY_AGAIN,
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 * it must be called later with the *same* arguments,
501 * until it returns a positive value.
502 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000503int ssl_write( ssl_context *ssl, const unsigned char *buf, int len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505/**
506 * \brief Notify the peer that the connection is being closed
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000507 *
508 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
510int ssl_close_notify( ssl_context *ssl );
511
512/**
513 * \brief Free an SSL context
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 *
515 * \param ssl SSL context
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
517void ssl_free( ssl_context *ssl );
518
519/*
520 * Internal functions (do not call directly)
521 */
522int ssl_handshake_client( ssl_context *ssl );
523int ssl_handshake_server( ssl_context *ssl );
524
525int ssl_derive_keys( ssl_context *ssl );
526void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
527
528int ssl_read_record( ssl_context *ssl );
529int ssl_fetch_input( ssl_context *ssl, int nb_want );
530
531int ssl_write_record( ssl_context *ssl );
532int ssl_flush_output( ssl_context *ssl );
533
534int ssl_parse_certificate( ssl_context *ssl );
535int ssl_write_certificate( ssl_context *ssl );
536
537int ssl_parse_change_cipher_spec( ssl_context *ssl );
538int ssl_write_change_cipher_spec( ssl_context *ssl );
539
540int ssl_parse_finished( ssl_context *ssl );
541int ssl_write_finished( ssl_context *ssl );
542
543#ifdef __cplusplus
544}
545#endif
546
547#endif /* ssl.h */