2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 * 2009, 2010 Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
46 #include <sys/types.h>
49 #include <sys/socket.h>
52 /* some systems had problems with long long int, thus,
60 #include <gnutls/gnutls.h>
64 * They are not needed any more. You can simply enable
65 * the gnutls_log callback to get error descriptions.
70 #define HANDSHAKE_DEBUG // Prints some information on handshake
71 #define COMPRESSION_DEBUG
75 /* The size of a handshake message should not
76 * be larger than this value.
78 #define MAX_HANDSHAKE_PACKET_SIZE 48*1024
80 #define TLS_MAX_SESSION_ID_SIZE 32
82 /* The maximum digest size of hash algorithms.
84 #define MAX_HASH_SIZE 64
85 #define MAX_CIPHER_BLOCK_SIZE 16
86 #define MAX_CIPHER_KEY_SIZE 32
88 #define MAX_USERNAME_SIZE 128
89 #define MAX_SERVER_NAME_SIZE 128
91 #define SESSION_TICKET_KEY_NAME_SIZE 16
92 #define SESSION_TICKET_KEY_SIZE 16
93 #define SESSION_TICKET_IV_SIZE 16
94 #define SESSION_TICKET_MAC_SECRET_SIZE 32
96 /* we can receive up to MAX_EXT_TYPES extensions.
98 #define MAX_EXT_TYPES 32
100 /* The initial size of the receive
101 * buffer size. This will grow if larger
102 * packets are received.
104 #define INITIAL_RECV_BUFFER_SIZE 256
106 /* the default for TCP */
107 #define DEFAULT_LOWAT 0
109 /* expire time for resuming sessions */
110 #define DEFAULT_EXPIRE_TIME 3600
112 /* the maximum size of encrypted packets */
113 #define DEFAULT_MAX_RECORD_SIZE 16384
114 #define RECORD_HEADER_SIZE 5
115 #define MAX_RECORD_SEND_SIZE (size_t)session->security_parameters.max_record_send_size
116 #define MAX_RECORD_RECV_SIZE (size_t)session->security_parameters.max_record_recv_size
117 #define MAX_PAD_SIZE 255
118 #define EXTRA_COMP_SIZE 2048
119 #define MAX_RECORD_OVERHEAD (MAX_CIPHER_BLOCK_SIZE/*iv*/+MAX_PAD_SIZE+EXTRA_COMP_SIZE)
120 #define MAX_RECV_SIZE (MAX_RECORD_OVERHEAD+MAX_RECORD_RECV_SIZE+RECORD_HEADER_SIZE)
122 #define HANDSHAKE_HEADER_SIZE 4
124 /* defaults for verification functions
126 #define DEFAULT_VERIFY_DEPTH 32
127 #define DEFAULT_VERIFY_BITS 16*1024
129 #include <gnutls_mem.h>
131 #define MEMSUB(x,y) ((ssize_t)((ptrdiff_t)x-(ptrdiff_t)y))
133 #define DECR_LEN(len, x) do { len-=x; if (len<0) {gnutls_assert(); return GNUTLS_E_UNEXPECTED_PACKET_LENGTH;} } while (0)
134 #define DECR_LENGTH_RET(len, x, RET) do { len-=x; if (len<0) {gnutls_assert(); return RET;} } while (0)
135 #define DECR_LENGTH_COM(len, x, COM) do { len-=x; if (len<0) {gnutls_assert(); COM;} } while (0)
137 #define HASH2MAC(x) ((gnutls_mac_algorithm_t)x)
139 #define GNUTLS_POINTER_TO_INT(_) ((int) GNUTLS_POINTER_TO_INT_CAST (_))
140 #define GNUTLS_INT_TO_POINTER(_) ((void*) GNUTLS_POINTER_TO_INT_CAST (_))
142 typedef unsigned char opaque
;
148 #include <gnutls_mpi.h>
150 typedef enum change_cipher_spec_t
151 { GNUTLS_TYPE_CHANGE_CIPHER_SPEC
= 1
152 } change_cipher_spec_t
;
154 typedef enum handshake_state_t
155 { STATE0
= 0, STATE1
, STATE2
,
156 STATE3
, STATE4
, STATE5
,
157 STATE6
, STATE7
, STATE8
, STATE9
, STATE20
= 20, STATE21
, STATE22
,
158 STATE30
= 30, STATE31
, STATE40
= 40, STATE41
, STATE50
= 50,
159 STATE60
= 60, STATE61
, STATE62
, STATE70
, STATE71
162 #include <gnutls_str.h>
164 /* This is the maximum number of algorithms (ciphers or macs etc).
165 * keep it synced with GNUTLS_MAX_ALGORITHM_NUM in gnutls.h
169 #define MAX_CIPHERSUITES 256
171 typedef enum extensions_t
173 GNUTLS_EXTENSION_SERVER_NAME
= 0,
174 GNUTLS_EXTENSION_MAX_RECORD_SIZE
= 1,
175 GNUTLS_EXTENSION_CERT_TYPE
= 9,
176 GNUTLS_EXTENSION_SRP
= 12,
177 GNUTLS_EXTENSION_SIGNATURE_ALGORITHMS
= 13,
178 GNUTLS_EXTENSION_SESSION_TICKET
= 35,
179 GNUTLS_EXTENSION_INNER_APPLICATION
= 37703,
180 GNUTLS_EXTENSION_SAFE_RENEGOTIATION
= 65281 /* aka: 0xff01 */
184 { CIPHER_STREAM
, CIPHER_BLOCK
} cipher_type_t
;
186 #define RESUME_TRUE 0
187 #define RESUME_FALSE -1
189 /* Record Protocol */
190 typedef enum content_type_t
192 GNUTLS_CHANGE_CIPHER_SPEC
= 20, GNUTLS_ALERT
,
193 GNUTLS_HANDSHAKE
, GNUTLS_APPLICATION_DATA
,
194 GNUTLS_INNER_APPLICATION
= 24
197 #define GNUTLS_PK_ANY (gnutls_pk_algorithm_t)-1
198 #define GNUTLS_PK_NONE (gnutls_pk_algorithm_t)-2
200 /* Message buffers (mbuffers) structures */
202 typedef struct mbuffer_st
204 struct mbuffer_st
*next
;
207 /* msg->size - mark = number of bytes left to process in this
208 message. Mark should only be non-zero when this buffer is the
209 head of the queue. */
211 unsigned int user_mark
; /* only used during fill in */
215 typedef struct mbuffer_head_st
226 HANDSHAKE_MAC_TYPE_10
= 1, /* TLS 1.0 style */
227 HANDSHAKE_MAC_TYPE_12
/* TLS 1.2 style */
228 } handshake_mac_type_t
;
230 /* Store & Retrieve functions defines:
233 typedef struct auth_cred_st
235 gnutls_credentials_type_t algorithm
;
237 /* the type of credentials depends on algorithm
240 struct auth_cred_st
*next
;
263 /* this is used to hold the peers authentication data
265 /* auth_info_t structures SHOULD NOT contain malloced
266 * elements. Check gnutls_session_pack.c, and gnutls_auth.c.
267 * Rememember that this should be calloced!
270 gnutls_credentials_type_t auth_info_type
;
271 int auth_info_size
; /* needed in order to store to db for restoring
275 auth_cred_st
*cred
; /* used to specify keys/certificates etc */
277 int certificate_requested
;
278 /* some ciphersuites use this
279 * to provide client authentication.
280 * 1 if client auth was requested
281 * by the peer, 0 otherwise
282 *** In case of a server this
283 * holds 1 if we should wait
284 * for a client certificate verify
287 typedef struct gnutls_key_st
*gnutls_key_st
;
290 struct record_state_st
;
291 typedef struct record_state_st record_state_st
;
293 struct record_parameters_st
;
294 typedef struct record_parameters_st record_parameters_st
;
298 #include <gnutls_hash_int.h>
299 #include <gnutls_cipher_int.h>
300 #include <gnutls_compress.h>
301 #include <gnutls_cert.h>
310 uint8_t hash_algorithm
;
311 uint8_t sign_algorithm
; /* pk algorithm actually */
314 /* This structure holds parameters got from TLS extension
315 * mechanism. (some extensions may hold parameters in auth_info_t
316 * structures also - see SRP).
319 #define MAX_SIGNATURE_ALGORITHMS 16
321 #define MAX_VERIFY_DATA_SIZE 36 /* in SSL 3.0, 12 in TLS 1.0 */
323 /* auth_info_t structures now MAY contain malloced
327 /* This structure and auth_info_t, are stored in the resume database,
328 * and are restored, in case of resume.
329 * Holds all the required parameters to resume the current
333 /* if you add anything in Security_Parameters struct, then
334 * also modify CPY_COMMON in gnutls_constate.c.
337 /* Note that the security parameters structure is set up after the
338 * handshake has finished. The only value you may depend on while
339 * the handshake is in progress is the cipher suite value.
343 gnutls_connection_end_t entity
;
344 gnutls_kx_algorithm_t kx_algorithm
;
345 handshake_mac_type_t handshake_mac_handle_type
; /* one of HANDSHAKE_TYPE_10 and HANDSHAKE_TYPE_12 */
347 /* The epoch used to read and write */
349 uint16_t epoch_write
;
351 /* The epoch that the next handshake will initialize. */
354 /* The epoch at index 0 of record_parameters. */
357 /* this is the ciphersuite we are going to use
358 * moved here from internals in order to be restored
361 cipher_suite_st current_cipher_suite
;
362 opaque master_secret
[GNUTLS_MASTER_SIZE
];
363 opaque client_random
[GNUTLS_RANDOM_SIZE
];
364 opaque server_random
[GNUTLS_RANDOM_SIZE
];
365 opaque session_id
[TLS_MAX_SESSION_ID_SIZE
];
366 uint8_t session_id_size
;
369 /* The send size is the one requested by the programmer.
370 * The recv size is the one negotiated with the peer.
372 uint16_t max_record_send_size
;
373 uint16_t max_record_recv_size
;
374 /* holds the negotiated certificate type */
375 gnutls_certificate_type_t cert_type
;
376 gnutls_protocol_t version
; /* moved here */
378 /* FIXME: The following are not saved in the session storage
379 * for session resumption.
382 /* Used by extensions that enable supplemental data: Which ones
383 * do that? Do they belong in security parameters?
385 int do_recv_supplemental
, do_send_supplemental
;
386 } security_parameters_st
;
388 struct record_state_st
390 gnutls_datum_t mac_secret
;
393 cipher_hd_st cipher_state
;
394 comp_hd_t compression_state
;
395 uint64 sequence_number
;
398 /* These are used to resolve relative epochs. These values are just
399 outside the 16 bit range to prevent off-by-one errors. An absolute
400 epoch may be referred to by its numeric id in the range
402 #define EPOCH_READ_CURRENT 70000
403 #define EPOCH_WRITE_CURRENT 70001
404 #define EPOCH_NEXT 70002
406 struct record_parameters_st
411 gnutls_cipher_algorithm_t cipher_algorithm
;
412 gnutls_mac_algorithm_t mac_algorithm
;
413 gnutls_compression_method_t compression_algorithm
;
415 record_state_st read
;
416 record_state_st write
;
421 unsigned int priority
[MAX_ALGOS
];
422 unsigned int algorithms
;
431 } safe_renegotiation_t
;
433 /* For the external api */
434 struct gnutls_priority_st
439 priority_st compression
;
440 priority_st protocol
;
441 priority_st cert_type
;
442 priority_st sign_algo
;
444 /* to disable record padding */
446 int allow_large_records
:1;
447 safe_renegotiation_t sr
;
448 int ssl3_record_version
:1;
449 int additional_verify_flags
;
453 /* DH and RSA parameters types.
455 typedef struct gnutls_dh_params_int
457 /* [0] is the prime, [1] is the generator.
464 gnutls_dh_params_t dh_params
;
466 gnutls_rsa_params_t rsa_params
;
468 } internal_params_st
;
474 opaque header
[HANDSHAKE_HEADER_SIZE
];
475 /* this holds the number of bytes in the handshake_header[] */
477 /* this holds the length of the handshake packet */
478 size_t packet_length
;
479 gnutls_handshake_description_t recv_type
;
480 } handshake_header_buffer_st
;
486 } extension_priv_data_t
;
490 gnutls_buffer_st application_data_buffer
; /* holds data to be delivered to application layer */
491 gnutls_buffer_st handshake_hash_buffer
; /* used to keep the last received handshake
497 digest_hd_st sha
; /* hash of the handshake messages */
498 digest_hd_st md5
; /* hash of the handshake messages */
502 digest_hd_st sha1
; /* hash of the handshake messages for TLS 1.2+ */
503 digest_hd_st sha256
; /* hash of the handshake messages for TLS 1.2+ */
505 } handshake_mac_handle
;
506 int handshake_mac_handle_init
; /* 1 when the previous union and type were initialized */
508 gnutls_buffer_st handshake_data_buffer
; /* this is a buffer that holds the current handshake message */
509 gnutls_buffer_st ia_data_buffer
; /* holds inner application data (TLS/IA) */
510 int resumable
:1; /* TRUE or FALSE - if we can resume that session */
511 handshake_state_t handshake_state
; /* holds
512 * a number which indicates where
513 * the handshake procedure has been
514 * interrupted. If it is 0 then
515 * no interruption has happened.
518 int invalid_connection
:1; /* true or FALSE - if this session is valid */
520 int may_not_read
:1; /* if it's 0 then we can read/write, otherwise it's forbiden to read/write
523 int read_eof
:1; /* non-zero if we have received a closure alert. */
525 int last_alert
; /* last alert received */
527 /* The last handshake messages sent or received.
529 int last_handshake_in
;
530 int last_handshake_out
;
532 /* this is the compression method we are going to use */
533 gnutls_compression_method_t compression_method
;
536 struct gnutls_priority_st priorities
;
538 /* resumed session */
539 int resumed
:1; /* RESUME_TRUE or FALSE - if we are resuming a session */
540 security_parameters_st resumed_security_parameters
;
541 gnutls_compression_method_t resumed_compression_method
;
543 /* sockets internals */
546 /* These buffers are used in the handshake
547 * protocol only. freed using _gnutls_handshake_io_buffer_clear();
549 mbuffer_head_st handshake_send_buffer
;
550 gnutls_handshake_description_t handshake_send_buffer_htype
;
551 content_type_t handshake_recv_buffer_type
;
552 gnutls_handshake_description_t handshake_recv_buffer_htype
;
553 gnutls_buffer_st handshake_recv_buffer
;
555 /* this buffer holds a record packet -mostly used for
558 mbuffer_head_st record_recv_buffer
;
559 mbuffer_head_st record_send_buffer
; /* holds cached data
560 * for the gnutls_io_write_buffered()
563 size_t record_send_buffer_user_size
; /* holds the
564 * size of the user specified data to
569 /* 0 if no peeked data was kept, 1 otherwise.
571 int have_peeked_data
:1;
573 int expire_time
; /* after expire_time seconds this session will expire */
574 struct mod_auth_st_int
*auth_struct
; /* used in handshake packets and KX algorithms */
575 int v2_hello
; /* 0 if the client hello is v3+.
576 * non-zero if we got a v2 hello.
578 /* keeps the headers of the handshake packet
580 handshake_header_buffer_st handshake_header_buffer
;
582 /* this is the highest version available
583 * to the peer. (advertized version).
584 * This is obtained by the Handshake Client Hello
585 * message. (some implementations read the Record version)
587 uint8_t adv_version_major
;
588 uint8_t adv_version_minor
;
590 /* if this is non zero a certificate request message
591 * will be sent to the client. - only if the ciphersuite
596 /* bits to use for DHE and DHA
597 * use _gnutls_dh_get_prime_bits() and gnutls_dh_set_prime_bits()
600 uint16_t dh_prime_bits
;
602 size_t max_handshake_data_buffer_size
;
604 /* PUSH & PULL functions.
606 gnutls_pull_func pull_func
;
607 gnutls_push_func push_func
;
608 gnutls_vec_push_func vec_push_func
;
609 gnutls_errno_func errno_func
;
610 /* Holds the first argument of PUSH and PULL
613 gnutls_transport_ptr_t transport_recv_ptr
;
614 gnutls_transport_ptr_t transport_send_ptr
;
616 /* STORE & RETRIEVE functions. Only used if other
617 * backend than gdbm is used.
619 gnutls_db_store_func db_store_func
;
620 gnutls_db_retr_func db_retrieve_func
;
621 gnutls_db_remove_func db_remove_func
;
624 /* post client hello callback (server side only)
626 gnutls_handshake_post_client_hello_func user_hello_func
;
628 /* holds the selected certificate and key.
629 * use _gnutls_selected_certs_deinit() and _gnutls_selected_certs_set()
632 gnutls_cert
*selected_cert_list
;
633 int selected_cert_list_length
;
634 struct gnutls_privkey_st
*selected_key
;
635 int selected_need_free
:1;
637 /* holds the extensions we sent to the peer
638 * (in case of a client)
640 uint16_t extensions_sent
[MAX_EXT_TYPES
];
641 uint16_t extensions_sent_size
;
643 /* is 0 if we are to send the whole PGP key, or non zero
644 * if the fingerprint is to be sent.
648 /* This holds the default version that our first
649 * record packet will have. */
650 opaque default_record_version
[2];
654 int enable_private
; /* non zero to
655 * enable cipher suites
656 * which have 0xFF status.
659 /* Holds 0 if the last called function was interrupted while
660 * receiving, and non zero otherwise.
664 /* This callback will be used (if set) to receive an
665 * openpgp key. (if the peer sends a fingerprint)
667 gnutls_openpgp_recv_key_func openpgp_recv_key_func
;
669 /* If non zero the server will not advertize the CA's he
670 * trusts (do not send an RDN sequence).
672 int ignore_rdn_sequence
;
674 /* This is used to set an arbitary version in the RSA
675 * PMS secret. Can be used by clients to test whether the
676 * server checks that version. (** only used in gnutls-cli-debug)
678 opaque rsa_pms_version
[2];
680 /* Here we cache the DH or RSA parameters got from the
681 * credentials structure, or from a callback. That is to
682 * minimize external calls.
684 internal_params_st params
;
686 /* This buffer is used by the record recv functions,
687 * as a temporary store buffer.
689 gnutls_datum_t recv_buffer
;
691 /* To avoid using global variables, and especially on Windows where
692 * the application may use a different errno variable than GnuTLS,
693 * it is possible to use gnutls_transport_set_errno to set a
694 * session-specific errno variable in the user-replaceable push/pull
695 * functions. This value is used by the send/recv functions. (The
696 * strange name of this variable is because 'errno' is typically
701 /* Function used to perform public-key signing operation during
702 handshake. Used by gnutls_sig.c:_gnutls_tls_sign(), see also
703 gnutls_sign_callback_set(). */
704 gnutls_sign_func sign_func
;
705 void *sign_func_userdata
;
707 /* Callback to extract TLS Finished message. */
708 gnutls_finished_callback_func finished_func
;
710 /* minimum bits to allow for SRP
711 * use gnutls_srp_set_prime_bits() to adjust it.
713 uint16_t srp_prime_bits
;
715 int initial_negotiation_completed
:1;
720 extension_priv_data_t priv
;
722 } extension_int_data
[MAX_EXT_TYPES
];
727 extension_priv_data_t priv
;
729 } resumed_extension_int_data
[MAX_EXT_TYPES
];
731 unsigned int cb_tls_unique_len
;
732 unsigned char cb_tls_unique
[MAX_VERIFY_DATA_SIZE
];
734 /* If you add anything here, check _gnutls_handshake_internal_state_clear().
738 /* Maximum number of epochs we keep around. */
739 #define MAX_EPOCH_INDEX 16
741 struct gnutls_session_int
743 security_parameters_st security_parameters
;
744 record_parameters_st
*record_parameters
[MAX_EPOCH_INDEX
];
745 internals_st internals
;
753 void _gnutls_set_current_version (gnutls_session_t session
,
754 gnutls_protocol_t version
);
755 void _gnutls_free_auth_info (gnutls_session_t session
);
757 /* These two macros return the advertized TLS version of
760 #define _gnutls_get_adv_version_major( session) \
761 session->internals.adv_version_major
763 #define _gnutls_get_adv_version_minor( session) \
764 session->internals.adv_version_minor
766 #define set_adv_version( session, major, minor) \
767 session->internals.adv_version_major = major; \
768 session->internals.adv_version_minor = minor
770 void _gnutls_set_adv_version (gnutls_session_t
, gnutls_protocol_t
);
771 gnutls_protocol_t
_gnutls_get_adv_version (gnutls_session_t
);
773 int _gnutls_is_secure_mem_null (const void *);
775 #endif /* GNUTLS_INT_H */