Revert "Revert ABI version, make libgnutls-extra use another ABI version."
[gnutls.git] / lib / gnutls_state.c
blobf09257481b0ab5df1e3df58d53cc2b7a24e950f2
1 /*
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* Functions to manipulate the session (gnutls_int.h), and some other stuff
26 * are included here. The file's name is traditionally gnutls_state even if the
27 * state has been renamed to session.
30 #include <gnutls_int.h>
31 #include <gnutls_errors.h>
32 #include <gnutls_auth_int.h>
33 #include <gnutls_num.h>
34 #include <gnutls_datum.h>
35 #include <gnutls_db.h>
36 #include <gnutls_record.h>
37 #include <gnutls_handshake.h>
38 #include <gnutls_dh.h>
39 #include <gnutls_buffers.h>
40 #include <gnutls_state.h>
41 #include <auth_cert.h>
42 #include <auth_anon.h>
43 #include <auth_psk.h>
44 #include <gnutls_algorithms.h>
45 #include <gnutls_rsa_export.h>
47 void
48 _gnutls_session_cert_type_set (gnutls_session_t session,
49 gnutls_certificate_type_t ct)
51 session->security_parameters.cert_type = ct;
54 /**
55 * gnutls_cipher_get - Returns the currently used cipher.
56 * @session: is a #gnutls_session_t structure.
58 * Get currently used cipher.
60 * Returns: the currently used cipher, an #gnutls_cipher_algorithm_t
61 * type.
62 **/
63 gnutls_cipher_algorithm_t
64 gnutls_cipher_get (gnutls_session_t session)
66 return session->security_parameters.read_bulk_cipher_algorithm;
69 /**
70 * gnutls_certificate_type_get - Returns the currently used certificate type.
71 * @session: is a #gnutls_session_t structure.
73 * The certificate type is by default X.509, unless it is negotiated
74 * as a TLS extension.
76 * Returns: the currently used #gnutls_certificate_type_t certificate
77 * type.
78 **/
79 gnutls_certificate_type_t
80 gnutls_certificate_type_get (gnutls_session_t session)
82 return session->security_parameters.cert_type;
85 /**
86 * gnutls_kx_get - Returns the key exchange algorithm.
87 * @session: is a #gnutls_session_t structure.
89 * Get currently used key exchange algorithm.
91 * Returns: the key exchange algorithm used in the last handshake, a
92 * #gnutls_kx_algorithm_t value.
93 **/
94 gnutls_kx_algorithm_t
95 gnutls_kx_get (gnutls_session_t session)
97 return session->security_parameters.kx_algorithm;
101 * gnutls_mac_get - Returns the currently used mac algorithm.
102 * @session: is a #gnutls_session_t structure.
104 * Get currently used MAC algorithm.
106 * Returns: the currently used mac algorithm, a
107 * #gnutls_mac_algorithm_t value.
109 gnutls_mac_algorithm_t
110 gnutls_mac_get (gnutls_session_t session)
112 return session->security_parameters.read_mac_algorithm;
116 * gnutls_compression_get - Returns the currently used compression algorithm.
117 * @session: is a #gnutls_session_t structure.
119 * Get currently used compression algorithm.
121 * Returns: the currently used compression method, a
122 * #gnutls_compression_method_t value.
124 gnutls_compression_method_t
125 gnutls_compression_get (gnutls_session_t session)
127 return session->security_parameters.read_compression_algorithm;
130 /* Check if the given certificate type is supported.
131 * This means that it is enabled by the priority functions,
132 * and a matching certificate exists.
135 _gnutls_session_cert_type_supported (gnutls_session_t session,
136 gnutls_certificate_type_t cert_type)
138 unsigned i;
139 unsigned cert_found = 0;
140 gnutls_certificate_credentials_t cred;
142 if (session->security_parameters.entity == GNUTLS_SERVER)
144 cred = (gnutls_certificate_credentials_t)
145 _gnutls_get_cred (session->key, GNUTLS_CRD_CERTIFICATE, NULL);
147 if (cred == NULL)
148 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE;
150 if( cred->server_get_cert_callback == NULL)
152 for (i = 0; i < cred->ncerts; i++)
154 if (cred->cert_list[i][0].cert_type == cert_type)
156 cert_found = 1;
157 break;
161 if (cert_found == 0)
162 /* no certificate is of that type.
164 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE;
168 if (session->internals.priorities.cert_type.algorithms == 0
169 && cert_type == DEFAULT_CERT_TYPE)
170 return 0;
172 for (i = 0; i < session->internals.priorities.cert_type.algorithms; i++)
174 if (session->internals.priorities.cert_type.priority[i] == cert_type)
176 return 0; /* ok */
180 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE;
183 /* this function deinitializes all the internal parameters stored
184 * in a session struct.
186 inline static void
187 deinit_internal_params (gnutls_session_t session)
189 if (session->internals.params.free_dh_params)
190 gnutls_dh_params_deinit (session->internals.params.dh_params);
192 if (session->internals.params.free_rsa_params)
193 gnutls_rsa_params_deinit (session->internals.params.rsa_params);
195 memset (&session->internals.params, 0, sizeof (session->internals.params));
198 /* This function will clear all the variables in internals
199 * structure within the session, which depend on the current handshake.
200 * This is used to allow further handshakes.
202 void
203 _gnutls_handshake_internal_state_clear (gnutls_session_t session)
205 session->internals.extensions_sent_size = 0;
207 /* by default no selected certificate */
208 session->internals.proposed_record_size = DEFAULT_MAX_RECORD_SIZE;
209 session->internals.adv_version_major = 0;
210 session->internals.adv_version_minor = 0;
211 session->internals.v2_hello = 0;
212 memset (&session->internals.handshake_header_buffer, 0,
213 sizeof (handshake_header_buffer_st));
214 session->internals.adv_version_minor = 0;
215 session->internals.adv_version_minor = 0;
216 session->internals.direction = 0;
218 /* use out of band data for the last
219 * handshake messages received.
221 session->internals.last_handshake_in = -1;
222 session->internals.last_handshake_out = -1;
224 session->internals.resumable = RESUME_TRUE;
225 _gnutls_free_datum (&session->internals.recv_buffer);
227 deinit_internal_params (session);
231 #define MIN_DH_BITS 727
233 * gnutls_init - initialize the session to null (null encryption etc...).
234 * @con_end: indicate if this session is to be used for server or client.
235 * @session: is a pointer to a #gnutls_session_t structure.
237 * This function initializes the current session to null. Every
238 * session must be initialized before use, so internal structures can
239 * be allocated. This function allocates structures which can only
240 * be free'd by calling gnutls_deinit(). Returns zero on success.
242 * @con_end can be one of %GNUTLS_CLIENT and %GNUTLS_SERVER.
244 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
247 gnutls_init (gnutls_session_t * session, gnutls_connection_end_t con_end)
249 *session = gnutls_calloc (1, sizeof (struct gnutls_session_int));
250 if (*session == NULL)
251 return GNUTLS_E_MEMORY_ERROR;
253 (*session)->security_parameters.entity = con_end;
255 /* the default certificate type for TLS */
256 (*session)->security_parameters.cert_type = DEFAULT_CERT_TYPE;
258 /* Set the defaults for initial handshake */
259 (*session)->security_parameters.read_bulk_cipher_algorithm =
260 (*session)->security_parameters.write_bulk_cipher_algorithm =
261 GNUTLS_CIPHER_NULL;
263 (*session)->security_parameters.read_mac_algorithm =
264 (*session)->security_parameters.write_mac_algorithm = GNUTLS_MAC_NULL;
266 (*session)->security_parameters.read_compression_algorithm =
267 GNUTLS_COMP_NULL;
268 (*session)->security_parameters.write_compression_algorithm =
269 GNUTLS_COMP_NULL;
271 (*session)->internals.enable_private = 0;
273 /* Initialize buffers */
274 _gnutls_buffer_init (&(*session)->internals.application_data_buffer);
275 _gnutls_buffer_init (&(*session)->internals.handshake_data_buffer);
276 _gnutls_buffer_init (&(*session)->internals.handshake_hash_buffer);
277 _gnutls_buffer_init (&(*session)->internals.ia_data_buffer);
279 _gnutls_buffer_init (&(*session)->internals.record_send_buffer);
280 _gnutls_buffer_init (&(*session)->internals.record_recv_buffer);
282 _gnutls_buffer_init (&(*session)->internals.handshake_send_buffer);
283 _gnutls_buffer_init (&(*session)->internals.handshake_recv_buffer);
285 (*session)->key = gnutls_calloc (1, sizeof (struct gnutls_key_st));
286 if ((*session)->key == NULL)
288 cleanup_session:
289 gnutls_free (*session);
290 *session = NULL;
291 return GNUTLS_E_MEMORY_ERROR;
294 (*session)->internals.expire_time = DEFAULT_EXPIRE_TIME; /* one hour default */
296 gnutls_dh_set_prime_bits ((*session), MIN_DH_BITS);
298 gnutls_transport_set_lowat ((*session), DEFAULT_LOWAT); /* the default for tcp */
300 gnutls_handshake_set_max_packet_length ((*session),
301 MAX_HANDSHAKE_PACKET_SIZE);
303 /* Allocate a minimum size for recv_data
304 * This is allocated in order to avoid small messages, making
305 * the receive procedure slow.
307 (*session)->internals.record_recv_buffer.data =
308 gnutls_malloc (INITIAL_RECV_BUFFER_SIZE);
309 if ((*session)->internals.record_recv_buffer.data == NULL)
311 gnutls_free ((*session)->key);
312 goto cleanup_session;
315 /* set the socket pointers to -1;
317 (*session)->internals.transport_recv_ptr = (gnutls_transport_ptr_t) - 1;
318 (*session)->internals.transport_send_ptr = (gnutls_transport_ptr_t) - 1;
320 /* set the default maximum record size for TLS
322 (*session)->security_parameters.max_record_recv_size =
323 DEFAULT_MAX_RECORD_SIZE;
324 (*session)->security_parameters.max_record_send_size =
325 DEFAULT_MAX_RECORD_SIZE;
327 /* everything else not initialized here is initialized
328 * as NULL or 0. This is why calloc is used.
331 _gnutls_handshake_internal_state_clear (*session);
333 return 0;
336 /* returns RESUME_FALSE or RESUME_TRUE.
339 _gnutls_session_is_resumable (gnutls_session_t session)
341 return session->internals.resumable;
346 * gnutls_deinit - clear all buffers associated with a session
347 * @session: is a #gnutls_session_t structure.
349 * This function clears all buffers associated with the @session.
350 * This function will also remove session data from the session
351 * database if the session was terminated abnormally.
353 void
354 gnutls_deinit (gnutls_session_t session)
357 if (session == NULL)
358 return;
360 /* remove auth info firstly */
361 _gnutls_free_auth_info (session);
363 _gnutls_handshake_internal_state_clear (session);
364 _gnutls_handshake_io_buffer_clear (session);
366 _gnutls_free_datum (&session->connection_state.read_mac_secret);
367 _gnutls_free_datum (&session->connection_state.write_mac_secret);
369 _gnutls_buffer_clear (&session->internals.ia_data_buffer);
370 _gnutls_buffer_clear (&session->internals.handshake_hash_buffer);
371 _gnutls_buffer_clear (&session->internals.handshake_data_buffer);
372 _gnutls_buffer_clear (&session->internals.application_data_buffer);
373 _gnutls_buffer_clear (&session->internals.record_recv_buffer);
374 _gnutls_buffer_clear (&session->internals.record_send_buffer);
376 gnutls_credentials_clear (session);
377 _gnutls_selected_certs_deinit (session);
379 _gnutls_cipher_deinit (&session->connection_state.read_cipher_state);
380 _gnutls_cipher_deinit (&session->connection_state.write_cipher_state);
382 if (session->connection_state.read_compression_state != NULL)
383 _gnutls_comp_deinit (session->connection_state.read_compression_state, 1);
384 if (session->connection_state.write_compression_state != NULL)
385 _gnutls_comp_deinit (session->connection_state.
386 write_compression_state, 0);
388 _gnutls_free_datum (&session->cipher_specs.server_write_mac_secret);
389 _gnutls_free_datum (&session->cipher_specs.client_write_mac_secret);
390 _gnutls_free_datum (&session->cipher_specs.server_write_IV);
391 _gnutls_free_datum (&session->cipher_specs.client_write_IV);
392 _gnutls_free_datum (&session->cipher_specs.server_write_key);
393 _gnutls_free_datum (&session->cipher_specs.client_write_key);
395 if (session->key != NULL)
397 _gnutls_mpi_release (&session->key->KEY);
398 _gnutls_mpi_release (&session->key->client_Y);
399 _gnutls_mpi_release (&session->key->client_p);
400 _gnutls_mpi_release (&session->key->client_g);
402 _gnutls_mpi_release (&session->key->u);
403 _gnutls_mpi_release (&session->key->a);
404 _gnutls_mpi_release (&session->key->x);
405 _gnutls_mpi_release (&session->key->A);
406 _gnutls_mpi_release (&session->key->B);
407 _gnutls_mpi_release (&session->key->b);
409 /* RSA */
410 _gnutls_mpi_release (&session->key->rsa[0]);
411 _gnutls_mpi_release (&session->key->rsa[1]);
413 _gnutls_mpi_release (&session->key->dh_secret);
414 gnutls_free (session->key);
416 session->key = NULL;
419 gnutls_free (session->internals.srp_username);
421 if (session->internals.srp_password)
423 memset (session->internals.srp_password, 0,
424 strlen (session->internals.srp_password));
425 gnutls_free (session->internals.srp_password);
428 memset (session, 0, sizeof (struct gnutls_session_int));
429 gnutls_free (session);
432 /* Returns the minimum prime bits that are acceptable.
435 _gnutls_dh_get_allowed_prime_bits (gnutls_session_t session)
437 return session->internals.dh_prime_bits;
441 _gnutls_dh_set_peer_public (gnutls_session_t session, mpi_t public)
443 dh_info_st *dh;
444 int ret;
446 switch (gnutls_auth_get_type (session))
448 case GNUTLS_CRD_ANON:
450 anon_auth_info_t info;
451 info = _gnutls_get_auth_info (session);
452 if (info == NULL)
453 return GNUTLS_E_INTERNAL_ERROR;
455 dh = &info->dh;
456 break;
458 case GNUTLS_CRD_PSK:
460 psk_auth_info_t info;
461 info = _gnutls_get_auth_info (session);
462 if (info == NULL)
463 return GNUTLS_E_INTERNAL_ERROR;
465 dh = &info->dh;
466 break;
468 case GNUTLS_CRD_CERTIFICATE:
470 cert_auth_info_t info;
472 info = _gnutls_get_auth_info (session);
473 if (info == NULL)
474 return GNUTLS_E_INTERNAL_ERROR;
476 dh = &info->dh;
477 break;
479 default:
480 gnutls_assert ();
481 return GNUTLS_E_INTERNAL_ERROR;
484 ret = _gnutls_mpi_dprint_lz (&dh->public_key, public);
485 if (ret < 0)
487 gnutls_assert ();
488 return ret;
491 return 0;
495 _gnutls_dh_set_secret_bits (gnutls_session_t session, unsigned bits)
497 switch (gnutls_auth_get_type (session))
499 case GNUTLS_CRD_ANON:
501 anon_auth_info_t info;
502 info = _gnutls_get_auth_info (session);
503 if (info == NULL)
504 return GNUTLS_E_INTERNAL_ERROR;
505 info->dh.secret_bits = bits;
506 break;
508 case GNUTLS_CRD_PSK:
510 psk_auth_info_t info;
511 info = _gnutls_get_auth_info (session);
512 if (info == NULL)
513 return GNUTLS_E_INTERNAL_ERROR;
514 info->dh.secret_bits = bits;
515 break;
517 case GNUTLS_CRD_CERTIFICATE:
519 cert_auth_info_t info;
521 info = _gnutls_get_auth_info (session);
522 if (info == NULL)
523 return GNUTLS_E_INTERNAL_ERROR;
525 info->dh.secret_bits = bits;
526 break;
527 default:
528 gnutls_assert ();
529 return GNUTLS_E_INTERNAL_ERROR;
533 return 0;
536 /* This function will set in the auth info structure the
537 * RSA exponent and the modulus.
540 _gnutls_rsa_export_set_pubkey (gnutls_session_t session,
541 mpi_t exponent, mpi_t modulus)
543 cert_auth_info_t info;
544 int ret;
546 info = _gnutls_get_auth_info (session);
547 if (info == NULL)
548 return GNUTLS_E_INTERNAL_ERROR;
550 ret = _gnutls_mpi_dprint_lz (&info->rsa_export.modulus, modulus);
551 if (ret < 0)
553 gnutls_assert ();
554 return ret;
557 ret = _gnutls_mpi_dprint_lz (&info->rsa_export.exponent, exponent);
558 if (ret < 0)
560 gnutls_assert ();
561 _gnutls_free_datum (&info->rsa_export.modulus);
562 return ret;
565 return 0;
569 /* Sets the prime and the generator in the auth info structure.
572 _gnutls_dh_set_group (gnutls_session_t session, mpi_t gen, mpi_t prime)
574 dh_info_st *dh;
575 int ret;
577 switch (gnutls_auth_get_type (session))
579 case GNUTLS_CRD_ANON:
581 anon_auth_info_t info;
582 info = _gnutls_get_auth_info (session);
583 if (info == NULL)
584 return GNUTLS_E_INTERNAL_ERROR;
586 dh = &info->dh;
587 break;
589 case GNUTLS_CRD_PSK:
591 psk_auth_info_t info;
592 info = _gnutls_get_auth_info (session);
593 if (info == NULL)
594 return GNUTLS_E_INTERNAL_ERROR;
596 dh = &info->dh;
597 break;
599 case GNUTLS_CRD_CERTIFICATE:
601 cert_auth_info_t info;
603 info = _gnutls_get_auth_info (session);
604 if (info == NULL)
605 return GNUTLS_E_INTERNAL_ERROR;
607 dh = &info->dh;
608 break;
610 default:
611 gnutls_assert ();
612 return GNUTLS_E_INTERNAL_ERROR;
615 /* prime
617 ret = _gnutls_mpi_dprint_lz (&dh->prime, prime);
618 if (ret < 0)
620 gnutls_assert ();
621 return ret;
624 /* generator
626 ret = _gnutls_mpi_dprint_lz (&dh->generator, gen);
627 if (ret < 0)
629 gnutls_assert ();
630 _gnutls_free_datum (&dh->prime);
631 return ret;
634 return 0;
637 #ifdef ENABLE_OPENPGP
639 * gnutls_openpgp_send_cert - order gnutls to send the openpgp fingerprint instead of the key
640 * @session: is a pointer to a #gnutls_session_t structure.
641 * @status: is one of GNUTLS_OPENPGP_CERT, or GNUTLS_OPENPGP_CERT_FINGERPRINT
643 * This function will order gnutls to send the key fingerprint
644 * instead of the key in the initial handshake procedure. This should
645 * be used with care and only when there is indication or knowledge
646 * that the server can obtain the client's key.
648 void
649 gnutls_openpgp_send_cert (gnutls_session_t session,
650 gnutls_openpgp_crt_status_t status)
652 session->internals.pgp_fingerprint = status;
654 #endif
657 * gnutls_certificate_send_x509_rdn_sequence - order gnutls to send or not the x.509 rdn sequence
658 * @session: is a pointer to a #gnutls_session_t structure.
659 * @status: is 0 or 1
661 * If status is non zero, this function will order gnutls not to send
662 * the rdnSequence in the certificate request message. That is the
663 * server will not advertize it's trusted CAs to the peer. If status
664 * is zero then the default behaviour will take effect, which is to
665 * advertize the server's trusted CAs.
667 * This function has no effect in clients, and in authentication
668 * methods other than certificate with X.509 certificates.
670 void
671 gnutls_certificate_send_x509_rdn_sequence (gnutls_session_t session,
672 int status)
674 session->internals.ignore_rdn_sequence = status;
677 #ifdef ENABLE_OPENPGP
679 _gnutls_openpgp_send_fingerprint (gnutls_session_t session)
681 return session->internals.pgp_fingerprint;
683 #endif
686 * _gnutls_record_set_default_version - Used to set the default version for the first record packet
687 * @session: is a #gnutls_session_t structure.
688 * @major: is a tls major version
689 * @minor: is a tls minor version
691 * This function sets the default version that we will use in the first
692 * record packet (client hello). This function is only useful to people
693 * that know TLS internals and want to debug other implementations.
696 void
697 _gnutls_record_set_default_version (gnutls_session_t session,
698 unsigned char major, unsigned char minor)
700 session->internals.default_record_version[0] = major;
701 session->internals.default_record_version[1] = minor;
705 * gnutls_handshake_set_private_extensions - Used to enable the private cipher suites
706 * @session: is a #gnutls_session_t structure.
707 * @allow: is an integer (0 or 1)
709 * This function will enable or disable the use of private cipher
710 * suites (the ones that start with 0xFF). By default or if @allow
711 * is 0 then these cipher suites will not be advertized nor used.
713 * Unless this function is called with the option to allow (1), then
714 * no compression algorithms, like LZO. That is because these
715 * algorithms are not yet defined in any RFC or even internet draft.
717 * Enabling the private ciphersuites when talking to other than
718 * gnutls servers and clients may cause interoperability problems.
720 void
721 gnutls_handshake_set_private_extensions (gnutls_session_t session, int allow)
723 session->internals.enable_private = allow;
726 inline static int
727 _gnutls_cal_PRF_A (gnutls_mac_algorithm_t algorithm,
728 const void *secret, int secret_size,
729 const void *seed, int seed_size, void *result)
731 digest_hd_st td1;
732 int ret;
734 ret = _gnutls_hmac_init (&td1, algorithm, secret, secret_size);
735 if (ret < 0)
737 gnutls_assert ();
738 return ret;
741 _gnutls_hmac (&td1, seed, seed_size);
742 _gnutls_hmac_deinit (&td1, result);
744 return 0;
747 #define MAX_SEED_SIZE 200
749 /* Produces "total_bytes" bytes using the hash algorithm specified.
750 * (used in the PRF function)
752 static int
753 _gnutls_P_hash (gnutls_mac_algorithm_t algorithm,
754 const opaque * secret, int secret_size,
755 const opaque * seed, int seed_size,
756 int total_bytes, opaque * ret)
759 digest_hd_st td2;
760 int i, times, how, blocksize, A_size;
761 opaque final[20], Atmp[MAX_SEED_SIZE];
762 int output_bytes, result;
764 if (seed_size > MAX_SEED_SIZE || total_bytes <= 0)
766 gnutls_assert ();
767 return GNUTLS_E_INTERNAL_ERROR;
770 blocksize = _gnutls_hmac_get_algo_len (algorithm);
772 output_bytes = 0;
775 output_bytes += blocksize;
777 while (output_bytes < total_bytes);
779 /* calculate A(0) */
781 memcpy (Atmp, seed, seed_size);
782 A_size = seed_size;
784 times = output_bytes / blocksize;
786 for (i = 0; i < times; i++)
788 result = _gnutls_hmac_init (&td2, algorithm, secret, secret_size);
789 if (result < 0)
791 gnutls_assert ();
792 return result;
795 /* here we calculate A(i+1) */
796 if ((result =
797 _gnutls_cal_PRF_A (algorithm, secret, secret_size, Atmp,
798 A_size, Atmp)) < 0)
800 gnutls_assert ();
801 _gnutls_hmac_deinit (&td2, final);
802 return result;
805 A_size = blocksize;
807 _gnutls_hmac (&td2, Atmp, A_size);
808 _gnutls_hmac (&td2, seed, seed_size);
809 _gnutls_hmac_deinit (&td2, final);
811 if ((1 + i) * blocksize < total_bytes)
813 how = blocksize;
815 else
817 how = total_bytes - (i) * blocksize;
820 if (how > 0)
822 memcpy (&ret[i * blocksize], final, how);
826 return 0;
829 /* Xor's two buffers and puts the output in the first one.
831 inline static void
832 _gnutls_xor (opaque * o1, opaque * o2, int length)
834 int i;
835 for (i = 0; i < length; i++)
837 o1[i] ^= o2[i];
843 #define MAX_PRF_BYTES 200
845 /* The PRF function expands a given secret
846 * needed by the TLS specification. ret must have a least total_bytes
847 * available.
850 _gnutls_PRF (gnutls_session_t session,
851 const opaque * secret, int secret_size, const char *label,
852 int label_size, const opaque * seed, int seed_size,
853 int total_bytes, void *ret)
855 int l_s, s_seed_size;
856 const opaque *s1, *s2;
857 opaque s_seed[MAX_SEED_SIZE];
858 opaque o1[MAX_PRF_BYTES], o2[MAX_PRF_BYTES];
859 int result;
860 gnutls_protocol_t ver = gnutls_protocol_get_version (session);
862 if (total_bytes > MAX_PRF_BYTES)
864 gnutls_assert ();
865 return GNUTLS_E_INTERNAL_ERROR;
867 /* label+seed = s_seed */
868 s_seed_size = seed_size + label_size;
870 if (s_seed_size > MAX_SEED_SIZE)
872 gnutls_assert ();
873 return GNUTLS_E_INTERNAL_ERROR;
876 memcpy (s_seed, label, label_size);
877 memcpy (&s_seed[label_size], seed, seed_size);
879 if (ver >= GNUTLS_TLS1_2)
881 result =
882 _gnutls_P_hash (GNUTLS_MAC_SHA1, secret, secret_size,
883 s_seed, s_seed_size,
884 total_bytes, ret);
885 if (result < 0)
887 gnutls_assert ();
888 return result;
891 else
893 l_s = secret_size / 2;
895 s1 = &secret[0];
896 s2 = &secret[l_s];
898 if (secret_size % 2 != 0)
900 l_s++;
903 result =
904 _gnutls_P_hash (GNUTLS_MAC_MD5, s1, l_s, s_seed, s_seed_size,
905 total_bytes, o1);
906 if (result < 0)
908 gnutls_assert ();
909 return result;
912 result =
913 _gnutls_P_hash (GNUTLS_MAC_SHA1, s2, l_s, s_seed, s_seed_size,
914 total_bytes, o2);
915 if (result < 0)
917 gnutls_assert ();
918 return result;
921 _gnutls_xor (o1, o2, total_bytes);
923 memcpy (ret, o1, total_bytes);
926 return 0; /* ok */
931 * gnutls_prf_raw - access the TLS PRF directly
932 * @session: is a #gnutls_session_t structure.
933 * @label_size: length of the @label variable.
934 * @label: label used in PRF computation, typically a short string.
935 * @seed_size: length of the @seed variable.
936 * @seed: optional extra data to seed the PRF with.
937 * @outsize: size of pre-allocated output buffer to hold the output.
938 * @out: pre-allocate buffer to hold the generated data.
940 * Apply the TLS Pseudo-Random-Function (PRF) using the master secret
941 * on some data.
943 * The @label variable usually contain a string denoting the purpose
944 * for the generated data. The @seed usually contain data such as the
945 * client and server random, perhaps together with some additional
946 * data that is added to guarantee uniqueness of the output for a
947 * particular purpose.
949 * Because the output is not guaranteed to be unique for a particular
950 * session unless @seed include the client random and server random
951 * fields (the PRF would output the same data on another connection
952 * resumed from the first one), it is not recommended to use this
953 * function directly. The gnutls_prf() function seed the PRF with the
954 * client and server random fields directly, and is recommended if you
955 * want to generate pseudo random data unique for each session.
957 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
960 gnutls_prf_raw (gnutls_session_t session,
961 size_t label_size,
962 const char *label,
963 size_t seed_size, const char *seed, size_t outsize, char *out)
965 int ret;
967 ret = _gnutls_PRF (session,
968 session->security_parameters.master_secret,
969 TLS_MASTER_SIZE,
970 label,
971 label_size, (opaque *) seed, seed_size, outsize, out);
973 return ret;
977 * gnutls_prf - derive pseudo-random data using the TLS PRF
978 * @session: is a #gnutls_session_t structure.
979 * @label_size: length of the @label variable.
980 * @label: label used in PRF computation, typically a short string.
981 * @server_random_first: non-0 if server random field should be first in seed
982 * @extra_size: length of the @extra variable.
983 * @extra: optional extra data to seed the PRF with.
984 * @outsize: size of pre-allocated output buffer to hold the output.
985 * @out: pre-allocate buffer to hold the generated data.
987 * Apply the TLS Pseudo-Random-Function (PRF) using the master secret
988 * on some data, seeded with the client and server random fields.
990 * The @label variable usually contain a string denoting the purpose
991 * for the generated data. The @server_random_first indicate whether
992 * the client random field or the server random field should be first
993 * in the seed. Non-0 indicate that the server random field is first,
994 * 0 that the client random field is first.
996 * The @extra variable can be used to add more data to the seed, after
997 * the random variables. It can be used to tie make sure the
998 * generated output is strongly connected to some additional data
999 * (e.g., a string used in user authentication).
1001 * The output is placed in *@OUT, which must be pre-allocated.
1003 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1006 gnutls_prf (gnutls_session_t session,
1007 size_t label_size,
1008 const char *label,
1009 int server_random_first,
1010 size_t extra_size, const char *extra, size_t outsize, char *out)
1012 int ret;
1013 opaque *seed;
1014 size_t seedsize = 2 * TLS_RANDOM_SIZE + extra_size;
1016 seed = gnutls_malloc (seedsize);
1017 if (!seed)
1019 gnutls_assert ();
1020 return GNUTLS_E_MEMORY_ERROR;
1023 memcpy (seed, server_random_first ?
1024 session->security_parameters.server_random :
1025 session->security_parameters.client_random, TLS_RANDOM_SIZE);
1026 memcpy (seed + TLS_RANDOM_SIZE, server_random_first ?
1027 session->security_parameters.client_random :
1028 session->security_parameters.server_random, TLS_RANDOM_SIZE);
1030 memcpy (seed + 2 * TLS_RANDOM_SIZE, extra, extra_size);
1032 ret = _gnutls_PRF (session, session->security_parameters.master_secret,
1033 TLS_MASTER_SIZE,
1034 label, label_size, seed, seedsize, outsize, out);
1036 gnutls_free (seed);
1038 return ret;
1042 * gnutls_session_get_client_random - get the session's client random value
1043 * @session: is a #gnutls_session_t structure.
1045 * Return a pointer to the 32-byte client random field used in the
1046 * session. The pointer must not be modified or deallocated.
1048 * If a client random value has not yet been established, the output
1049 * will be garbage; in particular, a %NULL return value should not be
1050 * expected.
1052 * Returns: pointer to client random data.
1054 const void *
1055 gnutls_session_get_client_random (gnutls_session_t session)
1057 return (char *) session->security_parameters.client_random;
1061 * gnutls_session_get_server_random - get the session's server random value
1062 * @session: is a #gnutls_session_t structure.
1064 * Return a pointer to the 32-byte server random field used in the
1065 * session. The pointer must not be modified or deallocated.
1067 * If a server random value has not yet been established, the output
1068 * will be garbage; in particular, a %NULL return value should not be
1069 * expected.
1071 * Returns: pointer to server random data.
1073 const void *
1074 gnutls_session_get_server_random (gnutls_session_t session)
1076 return (char *) session->security_parameters.server_random;
1080 * gnutls_session_get_master_secret - get the session's master secret value
1081 * @session: is a #gnutls_session_t structure.
1083 * Return a pointer to the 48-byte master secret in the session. The
1084 * pointer must not be modified or deallocated.
1086 * If a master secret value has not yet been established, the output
1087 * will be garbage; in particular, a %NULL return value should not be
1088 * expected.
1090 * Consider using gnutls_prf() rather than extracting the master
1091 * secret and use it to derive further data.
1093 * Returns: pointer to master secret data.
1095 const void *
1096 gnutls_session_get_master_secret (gnutls_session_t session)
1098 return (char *) session->security_parameters.master_secret;
1102 * gnutls_session_is_resumed - check whether this session is a resumed one
1103 * @session: is a #gnutls_session_t structure.
1105 * Check whether session is resumed or not.
1107 * Returns: non zero if this session is resumed, or a zero if this is
1108 * a new session.
1111 gnutls_session_is_resumed (gnutls_session_t session)
1113 if (session->security_parameters.entity == GNUTLS_CLIENT)
1115 if (session->security_parameters.session_id_size > 0 &&
1116 session->security_parameters.session_id_size ==
1117 session->internals.resumed_security_parameters.session_id_size
1118 && memcmp (session->security_parameters.session_id,
1119 session->internals.resumed_security_parameters.
1120 session_id,
1121 session->security_parameters.session_id_size) == 0)
1122 return 1;
1124 else
1126 if (session->internals.resumed == RESUME_TRUE)
1127 return 1;
1130 return 0;
1134 * _gnutls_session_is_export - Used to check whether this session is of export grade
1135 * @session: is a #gnutls_session_t structure.
1137 * This function will return non zero if this session is of export grade.
1141 _gnutls_session_is_export (gnutls_session_t session)
1143 gnutls_cipher_algorithm_t cipher;
1145 cipher =
1146 _gnutls_cipher_suite_get_cipher_algo (&session->security_parameters.
1147 current_cipher_suite);
1149 if (_gnutls_cipher_get_export_flag (cipher) != 0)
1150 return 1;
1152 return 0;
1156 * gnutls_session_get_ptr - Get the user pointer from the session structure
1157 * @session: is a #gnutls_session_t structure.
1159 * Get user pointer for session. Useful in callbacks. This is the
1160 * pointer set with gnutls_session_set_ptr().
1162 * Returns: the user given pointer from the session structure, or
1163 * %NULL if it was never set.
1165 void *
1166 gnutls_session_get_ptr (gnutls_session_t session)
1168 return session->internals.user_ptr;
1172 * gnutls_session_set_ptr - Used to set the user pointer to the session structure
1173 * @session: is a #gnutls_session_t structure.
1174 * @ptr: is the user pointer
1176 * This function will set (associate) the user given pointer @ptr to
1177 * the session structure. This is pointer can be accessed with
1178 * gnutls_session_get_ptr().
1180 void
1181 gnutls_session_set_ptr (gnutls_session_t session, void *ptr)
1183 session->internals.user_ptr = ptr;
1188 * gnutls_record_get_direction - return the direction of the last interrupted function call
1189 * @session: is a #gnutls_session_t structure.
1191 * This function provides information about the internals of the
1192 * record protocol and is only useful if a prior gnutls function call
1193 * (e.g. gnutls_handshake()) was interrupted for some reason, that
1194 * is, if a function returned %GNUTLS_E_INTERRUPTED or
1195 * %GNUTLS_E_AGAIN. In such a case, you might want to call select()
1196 * or poll() before calling the interrupted gnutls function again. To
1197 * tell you whether a file descriptor should be selected for either
1198 * reading or writing, gnutls_record_get_direction() returns 0 if the
1199 * interrupted function was trying to read data, and 1 if it was
1200 * trying to write data.
1202 * Returns: 0 if trying to read data, 1 if trying to write data.
1205 gnutls_record_get_direction (gnutls_session_t session)
1207 return session->internals.direction;
1211 * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS
1212 * @session: is a #gnutls_session_t structure.
1213 * @major: is the major version to use
1214 * @minor: is the minor version to use
1216 * This function will set the given version number to be used at the
1217 * RSA PMS secret. This is only useful to clients, which want to
1218 * test server's capabilities.
1221 void
1222 _gnutls_rsa_pms_set_version (gnutls_session_t session,
1223 unsigned char major, unsigned char minor)
1225 session->internals.rsa_pms_version[0] = major;
1226 session->internals.rsa_pms_version[1] = minor;
1230 * gnutls_handshake_set_post_client_hello_function - set callback to be called after the client hello is received
1231 * @res: is a gnutls_anon_server_credentials_t structure
1232 * @func: is the function to be called
1234 * This function will set a callback to be called after the client
1235 * hello has been received (callback valid in server side only). This
1236 * allows the server to adjust settings based on received extensions.
1238 * Those settings could be ciphersuites, requesting certificate, or
1239 * anything else except for version negotiation (this is done before
1240 * the hello message is parsed).
1242 * This callback must return 0 on success or a gnutls error code to
1243 * terminate the handshake.
1245 * Warning: You should not use this function to terminate the
1246 * handshake based on client input unless you know what you are
1247 * doing. Before the handshake is finished there is no way to know if
1248 * there is a man-in-the-middle attack being performed.
1250 void
1251 gnutls_handshake_set_post_client_hello_function (gnutls_session_t session,
1252 gnutls_handshake_post_client_hello_func func)
1254 session->internals.user_hello_func = func;
1258 * gnutls_session_enable_compatibility_mode - disable certain features in TLS in order to honour compatibility
1259 * @session: is a #gnutls_session_t structure.
1261 * This function can be used to disable certain (security) features in
1262 * TLS in order to maintain maximum compatibility with buggy
1263 * clients. It is equivalent to calling:
1264 * gnutls_record_disable_padding()
1266 * Normally only servers that require maximum compatibility with
1267 * everything out there, need to call this function.
1269 void
1270 gnutls_session_enable_compatibility_mode (gnutls_session_t session)
1272 gnutls_record_disable_padding( session);