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