2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS 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 3 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 License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* Functions to manipulate the session (gnutls_int.h), and some other stuff
24 * are included here. The file's name is traditionally gnutls_state even if the
25 * state has been renamed to session.
28 #include <gnutls_int.h>
29 #include <gnutls_errors.h>
30 #include <gnutls_auth.h>
31 #include <gnutls_num.h>
32 #include <gnutls_datum.h>
33 #include <gnutls_db.h>
34 #include <gnutls_record.h>
35 #include <gnutls_handshake.h>
36 #include <gnutls_dh.h>
37 #include <gnutls_buffers.h>
38 #include <gnutls_mbuffers.h>
39 #include <gnutls_state.h>
40 #include <gnutls_constate.h>
41 #include <auth/cert.h>
42 #include <auth/anon.h>
44 #include <algorithms.h>
45 #include <gnutls_rsa_export.h>
46 #include <gnutls_extensions.h>
48 #include <gnutls/dtls.h>
51 /* These should really be static, but src/tests.c calls them. Make
52 them public functions? */
54 _gnutls_rsa_pms_set_version (gnutls_session_t session
,
55 unsigned char major
, unsigned char minor
);
58 _gnutls_session_cert_type_set (gnutls_session_t session
,
59 gnutls_certificate_type_t ct
)
61 _gnutls_handshake_log("HSK[%p]: Selected certificate type %s (%d)\n", session
,
62 gnutls_certificate_type_get_name(ct
), ct
);
63 session
->security_parameters
.cert_type
= ct
;
67 _gnutls_session_ecc_curve_set (gnutls_session_t session
,
70 _gnutls_handshake_log("HSK[%p]: Selected ECC curve %s (%d)\n", session
, gnutls_ecc_curve_get_name(c
), c
);
71 session
->security_parameters
.ecc_curve
= c
;
76 * @session: is a #gnutls_session_t structure.
78 * Get currently used cipher.
80 * Returns: the currently used cipher, a #gnutls_cipher_algorithm_t
83 gnutls_cipher_algorithm_t
84 gnutls_cipher_get (gnutls_session_t session
)
86 record_parameters_st
*record_params
;
89 ret
= _gnutls_epoch_get (session
, EPOCH_READ_CURRENT
, &record_params
);
91 return gnutls_assert_val(GNUTLS_CIPHER_NULL
);
93 return record_params
->cipher_algorithm
;
97 * gnutls_certificate_type_get:
98 * @session: is a #gnutls_session_t structure.
100 * The certificate type is by default X.509, unless it is negotiated
101 * as a TLS extension.
103 * Returns: the currently used #gnutls_certificate_type_t certificate
106 gnutls_certificate_type_t
107 gnutls_certificate_type_get (gnutls_session_t session
)
109 return session
->security_parameters
.cert_type
;
114 * @session: is a #gnutls_session_t structure.
116 * Get currently used key exchange algorithm.
118 * Returns: the key exchange algorithm used in the last handshake, a
119 * #gnutls_kx_algorithm_t value.
121 gnutls_kx_algorithm_t
122 gnutls_kx_get (gnutls_session_t session
)
124 return session
->security_parameters
.kx_algorithm
;
129 * @session: is a #gnutls_session_t structure.
131 * Get currently used MAC algorithm.
133 * Returns: the currently used mac algorithm, a
134 * #gnutls_mac_algorithm_t value.
136 gnutls_mac_algorithm_t
137 gnutls_mac_get (gnutls_session_t session
)
139 record_parameters_st
*record_params
;
142 ret
= _gnutls_epoch_get (session
, EPOCH_READ_CURRENT
, &record_params
);
144 return gnutls_assert_val(GNUTLS_MAC_NULL
);
146 return record_params
->mac_algorithm
;
150 * gnutls_compression_get:
151 * @session: is a #gnutls_session_t structure.
153 * Get currently used compression algorithm.
155 * Returns: the currently used compression method, a
156 * #gnutls_compression_method_t value.
158 gnutls_compression_method_t
159 gnutls_compression_get (gnutls_session_t session
)
161 record_parameters_st
*record_params
;
164 ret
= _gnutls_epoch_get (session
, EPOCH_READ_CURRENT
, &record_params
);
166 return gnutls_assert_val(GNUTLS_COMP_NULL
);
168 return record_params
->compression_algorithm
;
171 /* Check if the given certificate type is supported.
172 * This means that it is enabled by the priority functions,
173 * and a matching certificate exists.
176 _gnutls_session_cert_type_supported (gnutls_session_t session
,
177 gnutls_certificate_type_t cert_type
)
180 unsigned cert_found
= 0;
181 gnutls_certificate_credentials_t cred
;
183 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
185 cred
= (gnutls_certificate_credentials_t
)
186 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
189 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
191 if (cred
->server_get_cert_callback
== NULL
192 && cred
->get_cert_callback
== NULL
)
194 for (i
= 0; i
< cred
->ncerts
; i
++)
196 if (cred
->certs
[i
].cert_list
[0].type
== cert_type
)
204 /* no certificate is of that type.
206 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
210 if (session
->internals
.priorities
.cert_type
.algorithms
== 0
211 && cert_type
== DEFAULT_CERT_TYPE
)
214 for (i
= 0; i
< session
->internals
.priorities
.cert_type
.algorithms
; i
++)
216 if (session
->internals
.priorities
.cert_type
.priority
[i
] == cert_type
)
222 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
226 /* this function deinitializes all the internal parameters stored
227 * in a session struct.
230 deinit_internal_params (gnutls_session_t session
)
232 if (session
->internals
.params
.free_dh_params
)
233 gnutls_dh_params_deinit (session
->internals
.params
.dh_params
);
235 if (session
->internals
.params
.free_rsa_params
)
236 gnutls_rsa_params_deinit (session
->internals
.params
.rsa_params
);
238 _gnutls_handshake_hash_buffers_clear (session
);
240 memset (&session
->internals
.params
, 0, sizeof (session
->internals
.params
));
243 /* This function will clear all the variables in internals
244 * structure within the session, which depend on the current handshake.
245 * This is used to allow further handshakes.
248 _gnutls_handshake_internal_state_init (gnutls_session_t session
)
250 session
->internals
.extensions_sent_size
= 0;
252 /* by default no selected certificate */
253 session
->internals
.adv_version_major
= 0;
254 session
->internals
.adv_version_minor
= 0;
255 session
->internals
.direction
= 0;
257 /* use out of band data for the last
258 * handshake messages received.
260 session
->internals
.last_handshake_in
= -1;
261 session
->internals
.last_handshake_out
= -1;
263 session
->internals
.resumable
= RESUME_TRUE
;
265 session
->internals
.dtls
.hsk_read_seq
= 0;
266 session
->internals
.dtls
.hsk_write_seq
= 0;
267 gettime(&session
->internals
.dtls
.handshake_start_time
);
271 _gnutls_handshake_internal_state_clear (gnutls_session_t session
)
273 _gnutls_handshake_internal_state_init (session
);
275 deinit_internal_params (session
);
277 _gnutls_epoch_gc(session
);
279 session
->internals
.handshake_endtime
= 0;
282 #define MIN_DH_BITS 727
285 * @session: is a pointer to a #gnutls_session_t structure.
286 * @flags: indicate if this session is to be used for server or client.
288 * This function initializes the current session to null. Every
289 * session must be initialized before use, so internal structures can
290 * be allocated. This function allocates structures which can only
291 * be free'd by calling gnutls_deinit(). Returns %GNUTLS_E_SUCCESS (0) on success.
293 * @flags can be one of %GNUTLS_CLIENT and %GNUTLS_SERVER. For a DTLS
294 * entity, the flags %GNUTLS_DATAGRAM and %GNUTLS_NONBLOCK are
295 * also available. The latter flag will enable a non-blocking
296 * operation of the DTLS timers. The flag %GNUTLS_STATELESS_COMPRESSION
297 * would disable keeping state across records when compressing.
299 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
302 gnutls_init (gnutls_session_t
* session
, unsigned int flags
)
305 record_parameters_st
*epoch
;
307 *session
= gnutls_calloc (1, sizeof (struct gnutls_session_int
));
308 if (*session
== NULL
)
309 return GNUTLS_E_MEMORY_ERROR
;
311 ret
= _gnutls_epoch_alloc (*session
, 0, &epoch
);
315 return GNUTLS_E_MEMORY_ERROR
;
318 /* Set all NULL algos on epoch 0 */
319 _gnutls_epoch_set_null_algos (*session
, epoch
);
321 (*session
)->security_parameters
.epoch_next
= 1;
323 (*session
)->security_parameters
.entity
= (flags
&GNUTLS_SERVER
?GNUTLS_SERVER
:GNUTLS_CLIENT
);
325 /* the default certificate type for TLS */
326 (*session
)->security_parameters
.cert_type
= DEFAULT_CERT_TYPE
;
328 /* Initialize buffers */
329 _gnutls_buffer_init (&(*session
)->internals
.handshake_hash_buffer
);
330 _gnutls_buffer_init (&(*session
)->internals
.heartbeat_payload
);
332 _mbuffer_head_init (&(*session
)->internals
.record_buffer
);
333 _mbuffer_head_init (&(*session
)->internals
.record_send_buffer
);
334 _mbuffer_head_init (&(*session
)->internals
.record_recv_buffer
);
336 _mbuffer_head_init (&(*session
)->internals
.handshake_send_buffer
);
337 _gnutls_handshake_recv_buffer_init(*session
);
339 (*session
)->key
= gnutls_calloc (1, sizeof (struct gnutls_key_st
));
340 if ((*session
)->key
== NULL
)
342 gnutls_free (*session
);
344 return GNUTLS_E_MEMORY_ERROR
;
347 (*session
)->internals
.expire_time
= DEFAULT_EXPIRE_TIME
; /* one hour default */
349 gnutls_dh_set_prime_bits ((*session
), MIN_DH_BITS
);
351 gnutls_handshake_set_max_packet_length ((*session
),
352 MAX_HANDSHAKE_PACKET_SIZE
);
354 /* set the socket pointers to -1;
356 (*session
)->internals
.transport_recv_ptr
= (gnutls_transport_ptr_t
) - 1;
357 (*session
)->internals
.transport_send_ptr
= (gnutls_transport_ptr_t
) - 1;
359 /* set the default maximum record size for TLS
361 (*session
)->security_parameters
.max_record_recv_size
=
362 DEFAULT_MAX_RECORD_SIZE
;
363 (*session
)->security_parameters
.max_record_send_size
=
364 DEFAULT_MAX_RECORD_SIZE
;
366 /* everything else not initialized here is initialized
367 * as NULL or 0. This is why calloc is used.
370 _gnutls_handshake_internal_state_init (*session
);
372 /* emulate old gnutls behavior for old applications that do not use the priority_*
375 (*session
)->internals
.priorities
.sr
= SR_PARTIAL
;
378 gnutls_transport_set_vec_push_function (*session
, system_writev
);
380 gnutls_transport_set_push_function (*session
, system_write
);
382 gnutls_transport_set_pull_function (*session
, system_read
);
383 gnutls_transport_set_errno_function (*session
, system_errno
);
384 gnutls_transport_set_pull_timeout_function (*session
, system_recv_timeout
);
386 if (flags
& GNUTLS_DATAGRAM
)
388 (*session
)->internals
.dtls
.mtu
= DTLS_DEFAULT_MTU
;
389 (*session
)->internals
.transport
= GNUTLS_DGRAM
;
391 (*session
)->internals
.dtls
.retrans_timeout_ms
= 1000;
392 (*session
)->internals
.dtls
.total_timeout_ms
= 60000;
395 (*session
)->internals
.transport
= GNUTLS_STREAM
;
397 if (flags
& GNUTLS_STATELESS_COMPRESSION
)
398 (*session
)->internals
.stateless_compression
= 1;
400 if (flags
& GNUTLS_NONBLOCK
)
401 (*session
)->internals
.dtls
.blocking
= 0;
403 (*session
)->internals
.dtls
.blocking
= 1;
408 /* returns RESUME_FALSE or RESUME_TRUE.
411 _gnutls_session_is_resumable (gnutls_session_t session
)
413 return session
->internals
.resumable
;
419 * @session: is a #gnutls_session_t structure.
421 * This function clears all buffers associated with the @session.
422 * This function will also remove session data from the session
423 * database if the session was terminated abnormally.
426 gnutls_deinit (gnutls_session_t session
)
433 /* remove auth info firstly */
434 _gnutls_free_auth_info (session
);
436 _gnutls_handshake_internal_state_clear (session
);
437 _gnutls_handshake_io_buffer_clear (session
);
438 _gnutls_ext_free_session_data (session
);
440 for (i
= 0; i
< MAX_EPOCH_INDEX
; i
++)
441 if (session
->record_parameters
[i
] != NULL
)
443 _gnutls_epoch_free (session
, session
->record_parameters
[i
]);
444 session
->record_parameters
[i
] = NULL
;
447 _gnutls_buffer_clear (&session
->internals
.handshake_hash_buffer
);
448 _gnutls_buffer_clear (&session
->internals
.heartbeat_payload
);
450 _mbuffer_head_clear (&session
->internals
.record_buffer
);
451 _mbuffer_head_clear (&session
->internals
.record_recv_buffer
);
452 _mbuffer_head_clear (&session
->internals
.record_send_buffer
);
454 gnutls_credentials_clear (session
);
455 _gnutls_selected_certs_deinit (session
);
457 if (session
->key
!= NULL
)
459 gnutls_pk_params_release(&session
->key
->ecdh_params
);
460 _gnutls_mpi_release (&session
->key
->ecdh_x
);
461 _gnutls_mpi_release (&session
->key
->ecdh_y
);
463 _gnutls_mpi_release (&session
->key
->KEY
);
464 _gnutls_mpi_release (&session
->key
->client_Y
);
465 _gnutls_mpi_release (&session
->key
->client_p
);
466 _gnutls_mpi_release (&session
->key
->client_g
);
468 _gnutls_mpi_release (&session
->key
->u
);
469 _gnutls_mpi_release (&session
->key
->a
);
470 _gnutls_mpi_release (&session
->key
->x
);
471 _gnutls_mpi_release (&session
->key
->A
);
472 _gnutls_mpi_release (&session
->key
->B
);
473 _gnutls_mpi_release (&session
->key
->b
);
476 _gnutls_mpi_release (&session
->key
->rsa
[0]);
477 _gnutls_mpi_release (&session
->key
->rsa
[1]);
479 _gnutls_mpi_release (&session
->key
->dh_secret
);
480 gnutls_free (session
->key
);
485 memset (session
, 0, sizeof (struct gnutls_session_int
));
486 gnutls_free (session
);
489 /* Returns the minimum prime bits that are acceptable.
492 _gnutls_dh_get_allowed_prime_bits (gnutls_session_t session
)
494 return session
->internals
.dh_prime_bits
;
498 _gnutls_dh_set_peer_public (gnutls_session_t session
, bigint_t
public)
503 switch (gnutls_auth_get_type (session
))
505 case GNUTLS_CRD_ANON
:
507 anon_auth_info_t info
;
508 info
= _gnutls_get_auth_info (session
);
510 return GNUTLS_E_INTERNAL_ERROR
;
517 psk_auth_info_t info
;
518 info
= _gnutls_get_auth_info (session
);
520 return GNUTLS_E_INTERNAL_ERROR
;
525 case GNUTLS_CRD_CERTIFICATE
:
527 cert_auth_info_t info
;
529 info
= _gnutls_get_auth_info (session
);
531 return GNUTLS_E_INTERNAL_ERROR
;
538 return GNUTLS_E_INTERNAL_ERROR
;
541 if (dh
->public_key
.data
)
542 _gnutls_free_datum (&dh
->public_key
);
544 ret
= _gnutls_mpi_dprint_lz (public, &dh
->public_key
);
555 _gnutls_dh_set_secret_bits (gnutls_session_t session
, unsigned bits
)
557 switch (gnutls_auth_get_type (session
))
559 case GNUTLS_CRD_ANON
:
561 anon_auth_info_t info
;
562 info
= _gnutls_get_auth_info (session
);
564 return GNUTLS_E_INTERNAL_ERROR
;
565 info
->dh
.secret_bits
= bits
;
570 psk_auth_info_t info
;
571 info
= _gnutls_get_auth_info (session
);
573 return GNUTLS_E_INTERNAL_ERROR
;
574 info
->dh
.secret_bits
= bits
;
577 case GNUTLS_CRD_CERTIFICATE
:
579 cert_auth_info_t info
;
581 info
= _gnutls_get_auth_info (session
);
583 return GNUTLS_E_INTERNAL_ERROR
;
585 info
->dh
.secret_bits
= bits
;
589 return GNUTLS_E_INTERNAL_ERROR
;
596 /* This function will set in the auth info structure the
597 * RSA exponent and the modulus.
600 _gnutls_rsa_export_set_pubkey (gnutls_session_t session
,
601 bigint_t exponent
, bigint_t modulus
)
603 cert_auth_info_t info
;
606 info
= _gnutls_get_auth_info (session
);
608 return GNUTLS_E_INTERNAL_ERROR
;
610 if (info
->rsa_export
.modulus
.data
)
611 _gnutls_free_datum (&info
->rsa_export
.modulus
);
613 if (info
->rsa_export
.exponent
.data
)
614 _gnutls_free_datum (&info
->rsa_export
.exponent
);
616 ret
= _gnutls_mpi_dprint_lz (modulus
, &info
->rsa_export
.modulus
);
623 ret
= _gnutls_mpi_dprint_lz (exponent
, &info
->rsa_export
.exponent
);
627 _gnutls_free_datum (&info
->rsa_export
.modulus
);
635 /* Sets the prime and the generator in the auth info structure.
638 _gnutls_dh_set_group (gnutls_session_t session
, bigint_t gen
, bigint_t prime
)
643 switch (gnutls_auth_get_type (session
))
645 case GNUTLS_CRD_ANON
:
647 anon_auth_info_t info
;
648 info
= _gnutls_get_auth_info (session
);
650 return GNUTLS_E_INTERNAL_ERROR
;
657 psk_auth_info_t info
;
658 info
= _gnutls_get_auth_info (session
);
660 return GNUTLS_E_INTERNAL_ERROR
;
665 case GNUTLS_CRD_CERTIFICATE
:
667 cert_auth_info_t info
;
669 info
= _gnutls_get_auth_info (session
);
671 return GNUTLS_E_INTERNAL_ERROR
;
678 return GNUTLS_E_INTERNAL_ERROR
;
682 _gnutls_free_datum (&dh
->prime
);
684 if (dh
->generator
.data
)
685 _gnutls_free_datum (&dh
->generator
);
689 ret
= _gnutls_mpi_dprint_lz (prime
, &dh
->prime
);
698 ret
= _gnutls_mpi_dprint_lz (gen
, &dh
->generator
);
702 _gnutls_free_datum (&dh
->prime
);
709 #ifdef ENABLE_OPENPGP
711 * gnutls_openpgp_send_cert:
712 * @session: is a pointer to a #gnutls_session_t structure.
713 * @status: is one of GNUTLS_OPENPGP_CERT, or GNUTLS_OPENPGP_CERT_FINGERPRINT
715 * This function will order gnutls to send the key fingerprint
716 * instead of the key in the initial handshake procedure. This should
717 * be used with care and only when there is indication or knowledge
718 * that the server can obtain the client's key.
721 gnutls_openpgp_send_cert (gnutls_session_t session
,
722 gnutls_openpgp_crt_status_t status
)
724 session
->internals
.pgp_fingerprint
= status
;
729 * gnutls_certificate_send_x509_rdn_sequence:
730 * @session: is a pointer to a #gnutls_session_t structure.
733 * If status is non zero, this function will order gnutls not to send
734 * the rdnSequence in the certificate request message. That is the
735 * server will not advertise its trusted CAs to the peer. If status
736 * is zero then the default behaviour will take effect, which is to
737 * advertise the server's trusted CAs.
739 * This function has no effect in clients, and in authentication
740 * methods other than certificate with X.509 certificates.
743 gnutls_certificate_send_x509_rdn_sequence (gnutls_session_t session
,
746 session
->internals
.ignore_rdn_sequence
= status
;
749 #ifdef ENABLE_OPENPGP
751 _gnutls_openpgp_send_fingerprint (gnutls_session_t session
)
753 return session
->internals
.pgp_fingerprint
;
758 * _gnutls_record_set_default_version - Used to set the default version for the first record packet
759 * @session: is a #gnutls_session_t structure.
760 * @major: is a tls major version
761 * @minor: is a tls minor version
763 * This function sets the default version that we will use in the first
764 * record packet (client hello). This function is only useful to people
765 * that know TLS internals and want to debug other implementations.
768 _gnutls_record_set_default_version (gnutls_session_t session
,
769 unsigned char major
, unsigned char minor
)
771 session
->internals
.default_record_version
[0] = major
;
772 session
->internals
.default_record_version
[1] = minor
;
776 * gnutls_handshake_set_private_extensions:
777 * @session: is a #gnutls_session_t structure.
778 * @allow: is an integer (0 or 1)
780 * This function will enable or disable the use of private cipher
781 * suites (the ones that start with 0xFF). By default or if @allow
782 * is 0 then these cipher suites will not be advertized nor used.
784 * Currently GnuTLS does not include such cipher-suites or
785 * compression algorithms.
787 * Enabling the private ciphersuites when talking to other than
788 * gnutls servers and clients may cause interoperability problems.
791 gnutls_handshake_set_private_extensions (gnutls_session_t session
, int allow
)
793 session
->internals
.enable_private
= allow
;
797 _gnutls_cal_PRF_A (gnutls_mac_algorithm_t algorithm
,
798 const void *secret
, int secret_size
,
799 const void *seed
, int seed_size
, void *result
)
803 ret
= _gnutls_hmac_fast (algorithm
, secret
, secret_size
, seed
, seed_size
, result
);
805 return gnutls_assert_val(ret
);
810 #define MAX_SEED_SIZE 200
812 /* Produces "total_bytes" bytes using the hash algorithm specified.
813 * (used in the PRF function)
816 P_hash (gnutls_mac_algorithm_t algorithm
,
817 const uint8_t * secret
, int secret_size
,
818 const uint8_t * seed
, int seed_size
,
819 int total_bytes
, uint8_t * ret
)
823 int i
, times
, how
, blocksize
, A_size
;
824 uint8_t final
[MAX_HASH_SIZE
], Atmp
[MAX_SEED_SIZE
];
825 int output_bytes
, result
;
827 if (seed_size
> MAX_SEED_SIZE
|| total_bytes
<= 0)
830 return GNUTLS_E_INTERNAL_ERROR
;
833 blocksize
= _gnutls_hmac_get_algo_len (algorithm
);
838 output_bytes
+= blocksize
;
840 while (output_bytes
< total_bytes
);
844 memcpy (Atmp
, seed
, seed_size
);
847 times
= output_bytes
/ blocksize
;
849 for (i
= 0; i
< times
; i
++)
851 result
= _gnutls_hmac_init (&td2
, algorithm
, secret
, secret_size
);
858 /* here we calculate A(i+1) */
860 _gnutls_cal_PRF_A (algorithm
, secret
, secret_size
, Atmp
,
864 _gnutls_hmac_deinit (&td2
, final
);
870 _gnutls_hmac (&td2
, Atmp
, A_size
);
871 _gnutls_hmac (&td2
, seed
, seed_size
);
872 _gnutls_hmac_deinit (&td2
, final
);
874 if ((1 + i
) * blocksize
< total_bytes
)
880 how
= total_bytes
- (i
) * blocksize
;
885 memcpy (&ret
[i
* blocksize
], final
, how
);
892 #define MAX_PRF_BYTES 200
894 /* The PRF function expands a given secret
895 * needed by the TLS specification. ret must have a least total_bytes
899 _gnutls_PRF (gnutls_session_t session
,
900 const uint8_t * secret
, unsigned int secret_size
, const char *label
,
901 int label_size
, const uint8_t * seed
, int seed_size
,
902 int total_bytes
, void *ret
)
904 int l_s
, s_seed_size
;
905 const uint8_t *s1
, *s2
;
906 uint8_t s_seed
[MAX_SEED_SIZE
];
907 uint8_t o1
[MAX_PRF_BYTES
], o2
[MAX_PRF_BYTES
];
909 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
911 if (total_bytes
> MAX_PRF_BYTES
)
914 return GNUTLS_E_INTERNAL_ERROR
;
916 /* label+seed = s_seed */
917 s_seed_size
= seed_size
+ label_size
;
919 if (s_seed_size
> MAX_SEED_SIZE
)
922 return GNUTLS_E_INTERNAL_ERROR
;
925 memcpy (s_seed
, label
, label_size
);
926 memcpy (&s_seed
[label_size
], seed
, seed_size
);
928 if (_gnutls_version_has_selectable_prf (ver
))
931 P_hash (_gnutls_cipher_suite_get_prf(session
->security_parameters
.cipher_suite
),
933 s_seed
, s_seed_size
, total_bytes
, ret
);
942 l_s
= secret_size
/ 2;
947 if (secret_size
% 2 != 0)
953 P_hash (GNUTLS_MAC_MD5
, s1
, l_s
, s_seed
, s_seed_size
,
962 P_hash (GNUTLS_MAC_SHA1
, s2
, l_s
, s_seed
, s_seed_size
,
970 memxor (o1
, o2
, total_bytes
);
972 memcpy (ret
, o1
, total_bytes
);
981 * @session: is a #gnutls_session_t structure.
982 * @label_size: length of the @label variable.
983 * @label: label used in PRF computation, typically a short string.
984 * @seed_size: length of the @seed variable.
985 * @seed: optional extra data to seed the PRF with.
986 * @outsize: size of pre-allocated output buffer to hold the output.
987 * @out: pre-allocate buffer to hold the generated data.
989 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
990 * and the provided data.
992 * The @label variable usually contains a string denoting the purpose
993 * for the generated data. The @seed usually contains data such as the
994 * client and server random, perhaps together with some additional
995 * data that is added to guarantee uniqueness of the output for a
996 * particular purpose.
998 * Because the output is not guaranteed to be unique for a particular
999 * session unless @seed includes the client random and server random
1000 * fields (the PRF would output the same data on another connection
1001 * resumed from the first one), it is not recommended to use this
1002 * function directly. The gnutls_prf() function seeds the PRF with the
1003 * client and server random fields directly, and is recommended if you
1004 * want to generate pseudo random data unique for each session.
1006 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1009 gnutls_prf_raw (gnutls_session_t session
,
1012 size_t seed_size
, const char *seed
, size_t outsize
, char *out
)
1016 ret
= _gnutls_PRF (session
,
1017 session
->security_parameters
.master_secret
,
1020 label_size
, (uint8_t *) seed
, seed_size
, outsize
, out
);
1027 * @session: is a #gnutls_session_t structure.
1028 * @label_size: length of the @label variable.
1029 * @label: label used in PRF computation, typically a short string.
1030 * @server_random_first: non-0 if server random field should be first in seed
1031 * @extra_size: length of the @extra variable.
1032 * @extra: optional extra data to seed the PRF with.
1033 * @outsize: size of pre-allocated output buffer to hold the output.
1034 * @out: pre-allocate buffer to hold the generated data.
1036 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
1037 * and the provided data, seeded with the client and server random fields.
1039 * The @label variable usually contains a string denoting the purpose
1040 * for the generated data. The @server_random_first indicates whether
1041 * the client random field or the server random field should be first
1042 * in the seed. Non-0 indicates that the server random field is first,
1043 * 0 that the client random field is first.
1045 * The @extra variable can be used to add more data to the seed, after
1046 * the random variables. It can be used to make sure the
1047 * generated output is strongly connected to some additional data
1048 * (e.g., a string used in user authentication).
1050 * The output is placed in @out, which must be pre-allocated.
1052 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
1055 gnutls_prf (gnutls_session_t session
,
1058 int server_random_first
,
1059 size_t extra_size
, const char *extra
, size_t outsize
, char *out
)
1063 size_t seedsize
= 2 * GNUTLS_RANDOM_SIZE
+ extra_size
;
1065 seed
= gnutls_malloc (seedsize
);
1069 return GNUTLS_E_MEMORY_ERROR
;
1072 memcpy (seed
, server_random_first
?
1073 session
->security_parameters
.server_random
:
1074 session
->security_parameters
.client_random
, GNUTLS_RANDOM_SIZE
);
1075 memcpy (seed
+ GNUTLS_RANDOM_SIZE
, server_random_first
?
1076 session
->security_parameters
.client_random
:
1077 session
->security_parameters
.server_random
, GNUTLS_RANDOM_SIZE
);
1079 memcpy (seed
+ 2 * GNUTLS_RANDOM_SIZE
, extra
, extra_size
);
1081 ret
= _gnutls_PRF (session
, session
->security_parameters
.master_secret
,
1083 label
, label_size
, seed
, seedsize
, outsize
, out
);
1091 * gnutls_session_is_resumed:
1092 * @session: is a #gnutls_session_t structure.
1094 * Check whether session is resumed or not.
1096 * Returns: non zero if this session is resumed, or a zero if this is
1100 gnutls_session_is_resumed (gnutls_session_t session
)
1102 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
1104 if (session
->security_parameters
.session_id_size
> 0 &&
1105 session
->security_parameters
.session_id_size
==
1106 session
->internals
.resumed_security_parameters
.session_id_size
1107 && memcmp (session
->security_parameters
.session_id
,
1109 resumed_security_parameters
.session_id
,
1110 session
->security_parameters
.session_id_size
) == 0)
1115 if (session
->internals
.resumed
!= RESUME_FALSE
)
1123 * gnutls_session_resumption_requested:
1124 * @session: is a #gnutls_session_t structure.
1126 * Check whether the client has asked for session resumption.
1127 * This function is valid only on server side.
1129 * Returns: non zero if session resumption was asked, or a zero if not.
1132 gnutls_session_resumption_requested(gnutls_session_t session
)
1134 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
1140 return session
->internals
.resumption_requested
;
1145 * _gnutls_session_is_export - Used to check whether this session is of export grade
1146 * @session: is a #gnutls_session_t structure.
1148 * This function will return non zero if this session is of export grade.
1151 _gnutls_session_is_export (gnutls_session_t session
)
1153 gnutls_cipher_algorithm_t cipher
;
1156 _gnutls_cipher_suite_get_cipher_algo (session
->
1157 security_parameters
.cipher_suite
);
1159 if (_gnutls_cipher_get_export_flag (cipher
) != 0)
1166 * _gnutls_session_is_psk - Used to check whether this session uses PSK kx
1167 * @session: is a #gnutls_session_t structure.
1169 * This function will return non zero if this session uses a PSK key
1170 * exchange algorithm.
1173 _gnutls_session_is_psk (gnutls_session_t session
)
1175 gnutls_kx_algorithm_t kx
;
1178 _gnutls_cipher_suite_get_kx_algo (session
->
1179 security_parameters
.cipher_suite
);
1180 if (kx
== GNUTLS_KX_PSK
|| kx
== GNUTLS_KX_DHE_PSK
)
1187 * _gnutls_session_is_ecc - Used to check whether this session uses ECC kx
1188 * @session: is a #gnutls_session_t structure.
1190 * This function will return non zero if this session uses an elliptic
1191 * curves key exchange exchange algorithm.
1194 _gnutls_session_is_ecc (gnutls_session_t session
)
1196 gnutls_kx_algorithm_t kx
;
1198 /* We get the key exchange algorithm through the ciphersuite because
1199 * the negotiated key exchange might not have been set yet.
1202 _gnutls_cipher_suite_get_kx_algo (session
->
1203 security_parameters
.cipher_suite
);
1205 return _gnutls_kx_is_ecc(kx
);
1209 * gnutls_session_get_ptr:
1210 * @session: is a #gnutls_session_t structure.
1212 * Get user pointer for session. Useful in callbacks. This is the
1213 * pointer set with gnutls_session_set_ptr().
1215 * Returns: the user given pointer from the session structure, or
1216 * %NULL if it was never set.
1219 gnutls_session_get_ptr (gnutls_session_t session
)
1221 return session
->internals
.user_ptr
;
1225 * gnutls_session_set_ptr:
1226 * @session: is a #gnutls_session_t structure.
1227 * @ptr: is the user pointer
1229 * This function will set (associate) the user given pointer @ptr to
1230 * the session structure. This pointer can be accessed with
1231 * gnutls_session_get_ptr().
1234 gnutls_session_set_ptr (gnutls_session_t session
, void *ptr
)
1236 session
->internals
.user_ptr
= ptr
;
1241 * gnutls_record_get_direction:
1242 * @session: is a #gnutls_session_t structure.
1244 * This function provides information about the internals of the
1245 * record protocol and is only useful if a prior gnutls function call
1246 * (e.g. gnutls_handshake()) was interrupted for some reason, that
1247 * is, if a function returned %GNUTLS_E_INTERRUPTED or
1248 * %GNUTLS_E_AGAIN. In such a case, you might want to call select()
1249 * or poll() before calling the interrupted gnutls function again. To
1250 * tell you whether a file descriptor should be selected for either
1251 * reading or writing, gnutls_record_get_direction() returns 0 if the
1252 * interrupted function was trying to read data, and 1 if it was
1253 * trying to write data.
1255 * Returns: 0 if trying to read data, 1 if trying to write data.
1258 gnutls_record_get_direction (gnutls_session_t session
)
1260 return session
->internals
.direction
;
1264 * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS
1265 * @session: is a #gnutls_session_t structure.
1266 * @major: is the major version to use
1267 * @minor: is the minor version to use
1269 * This function will set the given version number to be used at the
1270 * RSA PMS secret. This is only useful to clients, which want to
1271 * test server's capabilities.
1274 _gnutls_rsa_pms_set_version (gnutls_session_t session
,
1275 unsigned char major
, unsigned char minor
)
1277 session
->internals
.rsa_pms_version
[0] = major
;
1278 session
->internals
.rsa_pms_version
[1] = minor
;
1282 * gnutls_handshake_set_post_client_hello_function:
1283 * @session: is a #gnutls_session_t structure.
1284 * @func: is the function to be called
1286 * This function will set a callback to be called after the client
1287 * hello has been received (callback valid in server side only). This
1288 * allows the server to adjust settings based on received extensions.
1290 * Those settings could be ciphersuites, requesting certificate, or
1291 * anything else except for version negotiation (this is done before
1292 * the hello message is parsed).
1294 * This callback must return 0 on success or a gnutls error code to
1295 * terminate the handshake.
1297 * Warning: You should not use this function to terminate the
1298 * handshake based on client input unless you know what you are
1299 * doing. Before the handshake is finished there is no way to know if
1300 * there is a man-in-the-middle attack being performed.
1303 gnutls_handshake_set_post_client_hello_function (gnutls_session_t session
,
1304 gnutls_handshake_post_client_hello_func
1307 session
->internals
.user_hello_func
= func
;
1311 * gnutls_session_enable_compatibility_mode:
1312 * @session: is a #gnutls_session_t structure.
1314 * This function can be used to disable certain (security) features in
1315 * TLS in order to maintain maximum compatibility with buggy
1316 * clients. It is equivalent to calling:
1317 * gnutls_record_disable_padding()
1319 * Normally only servers that require maximum compatibility with
1320 * everything out there, need to call this function.
1323 gnutls_session_enable_compatibility_mode (gnutls_session_t session
)
1325 gnutls_record_disable_padding (session
);
1329 * gnutls_session_channel_binding:
1330 * @session: is a #gnutls_session_t structure.
1331 * @cbtype: an #gnutls_channel_binding_t enumeration type
1332 * @cb: output buffer array with data
1334 * Extract given channel binding data of the @cbtype (e.g.,
1335 * %GNUTLS_CB_TLS_UNIQUE) type.
1337 * Returns: %GNUTLS_E_SUCCESS on success,
1338 * %GNUTLS_E_UNIMPLEMENTED_FEATURE if the @cbtype is unsupported,
1339 * %GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE if the data is not
1340 * currently available, or an error code.
1345 gnutls_session_channel_binding (gnutls_session_t session
,
1346 gnutls_channel_binding_t cbtype
,
1347 gnutls_datum_t
* cb
)
1349 if (cbtype
!= GNUTLS_CB_TLS_UNIQUE
)
1350 return GNUTLS_E_UNIMPLEMENTED_FEATURE
;
1352 if (!session
->internals
.initial_negotiation_completed
)
1353 return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE
;
1355 cb
->size
= session
->internals
.cb_tls_unique_len
;
1356 cb
->data
= gnutls_malloc (cb
->size
);
1357 if (cb
->data
== NULL
)
1358 return GNUTLS_E_MEMORY_ERROR
;
1360 memcpy (cb
->data
, session
->internals
.cb_tls_unique
, cb
->size
);
1366 * gnutls_ecc_curve_get:
1367 * @session: is a #gnutls_session_t structure.
1369 * Returns the currently used elliptic curve. Only valid
1370 * when using an elliptic curve ciphersuite.
1372 * Returns: the currently used curve, a #gnutls_ecc_curve_t
1377 gnutls_ecc_curve_t
gnutls_ecc_curve_get(gnutls_session_t session
)
1379 return _gnutls_session_ecc_curve_get(session
);
1382 #undef gnutls_protocol_get_version
1384 * gnutls_protocol_get_version:
1385 * @session: is a #gnutls_session_t structure.
1387 * Get TLS version, a #gnutls_protocol_t value.
1389 * Returns: The version of the currently used protocol.
1392 gnutls_protocol_get_version (gnutls_session_t session
)
1394 return _gnutls_protocol_get_version(session
);
1398 * gnutls_session_get_random:
1399 * @session: is a #gnutls_session_t structure.
1400 * @client: the client part of the random
1401 * @server: the server part of the random
1403 * This function returns pointers to the client and server
1404 * random fields used in the TLS handshake. The pointers are
1405 * not to be modified or deallocated.
1407 * If a client random value has not yet been established, the output
1413 gnutls_session_get_random (gnutls_session_t session
, gnutls_datum_t
* client
, gnutls_datum_t
* server
)
1417 client
->data
= session
->security_parameters
.client_random
;
1418 client
->size
= sizeof(session
->security_parameters
.client_random
);
1423 server
->data
= session
->security_parameters
.server_random
;
1424 server
->size
= sizeof(session
->security_parameters
.server_random
);