2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
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,
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>
44 #include <gnutls_algorithms.h>
45 #include <gnutls_rsa_export.h>
47 #define CHECK_AUTH(auth, ret) if (gnutls_auth_get_type(session) != auth) { \
53 _gnutls_session_cert_type_set (gnutls_session_t session
,
54 gnutls_certificate_type_t ct
)
56 session
->security_parameters
.cert_type
= ct
;
60 * gnutls_cipher_get - Returns the currently used cipher.
61 * @session: is a #gnutls_session_t structure.
63 * Returns the currently used cipher.
65 gnutls_cipher_algorithm_t
66 gnutls_cipher_get (gnutls_session_t session
)
68 return session
->security_parameters
.read_bulk_cipher_algorithm
;
72 * gnutls_certificate_type_get - Returns the currently used certificate type.
73 * @session: is a #gnutls_session_t structure.
75 * Returns the currently used certificate type. The certificate type
76 * is by default X.509, unless it is negotiated as a TLS extension.
79 gnutls_certificate_type_t
80 gnutls_certificate_type_get (gnutls_session_t session
)
82 return session
->security_parameters
.cert_type
;
86 * gnutls_kx_get - Returns the key exchange algorithm.
87 * @session: is a #gnutls_session_t structure.
89 * Returns the key exchange algorithm used in the last handshake.
92 gnutls_kx_get (gnutls_session_t session
)
94 return session
->security_parameters
.kx_algorithm
;
98 * gnutls_mac_get - Returns the currently used mac algorithm.
99 * @session: is a #gnutls_session_t structure.
101 * Returns the currently used mac algorithm.
103 gnutls_mac_algorithm_t
104 gnutls_mac_get (gnutls_session_t session
)
106 return session
->security_parameters
.read_mac_algorithm
;
110 * gnutls_compression_get - Returns the currently used compression algorithm.
111 * @session: is a #gnutls_session_t structure.
113 * Returns the currently used compression method.
115 gnutls_compression_method_t
116 gnutls_compression_get (gnutls_session_t session
)
118 return session
->security_parameters
.read_compression_algorithm
;
121 /* Check if the given certificate type is supported.
122 * This means that it is enabled by the priority functions,
123 * and a matching certificate exists.
126 _gnutls_session_cert_type_supported (gnutls_session_t session
,
127 gnutls_certificate_type_t cert_type
)
130 unsigned cert_found
= 0;
131 gnutls_certificate_credentials_t cred
;
133 if (session
->security_parameters
.entity
== GNUTLS_SERVER
)
135 cred
= (gnutls_certificate_credentials_t
)
136 _gnutls_get_cred (session
->key
, GNUTLS_CRD_CERTIFICATE
, NULL
);
139 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
141 for (i
= 0; i
< cred
->ncerts
; i
++)
143 if (cred
->cert_list
[i
][0].cert_type
== cert_type
)
150 /* no certificate is of that type.
152 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
156 if (session
->internals
.cert_type_priority
.algorithms
== 0
157 && cert_type
== DEFAULT_CERT_TYPE
)
160 for (i
= 0; i
< session
->internals
.cert_type_priority
.algorithms
; i
++)
162 if (session
->internals
.cert_type_priority
.priority
[i
] == cert_type
)
168 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE
;
171 /* this function deinitializes all the internal parameters stored
172 * in a session struct.
175 deinit_internal_params (gnutls_session_t session
)
177 if (session
->internals
.params
.free_dh_params
)
178 gnutls_dh_params_deinit (session
->internals
.params
.dh_params
);
180 if (session
->internals
.params
.free_rsa_params
)
181 gnutls_rsa_params_deinit (session
->internals
.params
.rsa_params
);
183 memset (&session
->internals
.params
, 0, sizeof (session
->internals
.params
));
186 /* This function will clear all the variables in internals
187 * structure within the session, which depend on the current handshake.
188 * This is used to allow further handshakes.
191 _gnutls_handshake_internal_state_clear (gnutls_session_t session
)
193 session
->internals
.extensions_sent_size
= 0;
195 /* by default no selected certificate */
196 session
->internals
.proposed_record_size
= DEFAULT_MAX_RECORD_SIZE
;
197 session
->internals
.adv_version_major
= 0;
198 session
->internals
.adv_version_minor
= 0;
199 session
->internals
.v2_hello
= 0;
200 memset (&session
->internals
.handshake_header_buffer
, 0,
201 sizeof (handshake_header_buffer_st
));
202 session
->internals
.adv_version_minor
= 0;
203 session
->internals
.adv_version_minor
= 0;
204 session
->internals
.direction
= 0;
206 /* use out of band data for the last
207 * handshake messages received.
209 session
->internals
.last_handshake_in
= -1;
210 session
->internals
.last_handshake_out
= -1;
212 session
->internals
.handshake_restarted
= 0;
214 session
->internals
.resumable
= RESUME_TRUE
;
215 _gnutls_free_datum (&session
->internals
.recv_buffer
);
217 deinit_internal_params (session
);
221 #define MIN_DH_BITS 727
223 * gnutls_init - This function initializes the session to null (null encryption etc...).
224 * @con_end: is used to indicate if this session is to be used for server or
225 * client. Can be one of GNUTLS_CLIENT and GNUTLS_SERVER.
226 * @session: is a pointer to a #gnutls_session_t structure.
228 * This function initializes the current session to null. Every session
229 * must be initialized before use, so internal structures can be allocated.
230 * This function allocates structures which can only be free'd
231 * by calling gnutls_deinit(). Returns zero on success.
234 gnutls_init (gnutls_session_t
* session
, gnutls_connection_end_t con_end
)
236 *session
= gnutls_calloc (1, sizeof (struct gnutls_session_int
));
237 if (*session
== NULL
)
238 return GNUTLS_E_MEMORY_ERROR
;
240 (*session
)->security_parameters
.entity
= con_end
;
242 /* the default certificate type for TLS */
243 (*session
)->security_parameters
.cert_type
= DEFAULT_CERT_TYPE
;
245 /* Set the defaults for initial handshake */
246 (*session
)->security_parameters
.read_bulk_cipher_algorithm
=
247 (*session
)->security_parameters
.write_bulk_cipher_algorithm
=
250 (*session
)->security_parameters
.read_mac_algorithm
=
251 (*session
)->security_parameters
.write_mac_algorithm
= GNUTLS_MAC_NULL
;
253 (*session
)->security_parameters
.read_compression_algorithm
=
255 (*session
)->security_parameters
.write_compression_algorithm
=
258 (*session
)->internals
.enable_private
= 0;
260 /* Initialize buffers */
261 _gnutls_buffer_init (&(*session
)->internals
.application_data_buffer
);
262 _gnutls_buffer_init (&(*session
)->internals
.handshake_data_buffer
);
263 _gnutls_buffer_init (&(*session
)->internals
.handshake_hash_buffer
);
264 _gnutls_buffer_init (&(*session
)->internals
.ia_data_buffer
);
266 _gnutls_buffer_init (&(*session
)->internals
.record_send_buffer
);
267 _gnutls_buffer_init (&(*session
)->internals
.record_recv_buffer
);
269 _gnutls_buffer_init (&(*session
)->internals
.handshake_send_buffer
);
270 _gnutls_buffer_init (&(*session
)->internals
.handshake_recv_buffer
);
272 (*session
)->key
= gnutls_calloc (1, sizeof (struct gnutls_key_st
));
273 if ((*session
)->key
== NULL
)
276 gnutls_free (*session
);
278 return GNUTLS_E_MEMORY_ERROR
;
281 (*session
)->internals
.expire_time
= DEFAULT_EXPIRE_TIME
; /* one hour default */
283 gnutls_dh_set_prime_bits ((*session
), MIN_DH_BITS
);
285 gnutls_transport_set_lowat ((*session
), DEFAULT_LOWAT
); /* the default for tcp */
287 gnutls_handshake_set_max_packet_length ((*session
),
288 MAX_HANDSHAKE_PACKET_SIZE
);
290 /* Allocate a minimum size for recv_data
291 * This is allocated in order to avoid small messages, making
292 * the receive procedure slow.
294 (*session
)->internals
.record_recv_buffer
.data
=
295 gnutls_malloc (INITIAL_RECV_BUFFER_SIZE
);
296 if ((*session
)->internals
.record_recv_buffer
.data
== NULL
)
298 gnutls_free ((*session
)->key
);
299 goto cleanup_session
;
302 /* set the socket pointers to -1;
304 (*session
)->internals
.transport_recv_ptr
= (gnutls_transport_ptr_t
) - 1;
305 (*session
)->internals
.transport_send_ptr
= (gnutls_transport_ptr_t
) - 1;
307 /* set the default maximum record size for TLS
309 (*session
)->security_parameters
.max_record_recv_size
=
310 DEFAULT_MAX_RECORD_SIZE
;
311 (*session
)->security_parameters
.max_record_send_size
=
312 DEFAULT_MAX_RECORD_SIZE
;
314 /* everything else not initialized here is initialized
315 * as NULL or 0. This is why calloc is used.
318 _gnutls_handshake_internal_state_clear (*session
);
323 /* returns RESUME_FALSE or RESUME_TRUE.
326 _gnutls_session_is_resumable (gnutls_session_t session
)
328 return session
->internals
.resumable
;
333 * gnutls_deinit - This function clears all buffers associated with a session
334 * @session: is a #gnutls_session_t structure.
336 * This function clears all buffers associated with the @session.
337 * This function will also remove session data from the session database
338 * if the session was terminated abnormally.
342 gnutls_deinit (gnutls_session_t session
)
348 /* remove auth info firstly */
349 _gnutls_free_auth_info (session
);
351 _gnutls_handshake_internal_state_clear (session
);
352 _gnutls_handshake_io_buffer_clear (session
);
354 _gnutls_free_datum (&session
->connection_state
.read_mac_secret
);
355 _gnutls_free_datum (&session
->connection_state
.write_mac_secret
);
357 _gnutls_buffer_clear (&session
->internals
.ia_data_buffer
);
358 _gnutls_buffer_clear (&session
->internals
.handshake_hash_buffer
);
359 _gnutls_buffer_clear (&session
->internals
.handshake_data_buffer
);
360 _gnutls_buffer_clear (&session
->internals
.application_data_buffer
);
361 _gnutls_buffer_clear (&session
->internals
.record_recv_buffer
);
362 _gnutls_buffer_clear (&session
->internals
.record_send_buffer
);
364 gnutls_credentials_clear (session
);
365 _gnutls_selected_certs_deinit (session
);
367 if (session
->connection_state
.read_cipher_state
!= NULL
)
368 _gnutls_cipher_deinit (session
->connection_state
.read_cipher_state
);
369 if (session
->connection_state
.write_cipher_state
!= NULL
)
370 _gnutls_cipher_deinit (session
->connection_state
.write_cipher_state
);
372 if (session
->connection_state
.read_compression_state
!= NULL
)
373 _gnutls_comp_deinit (session
->connection_state
.read_compression_state
, 1);
374 if (session
->connection_state
.write_compression_state
!= NULL
)
375 _gnutls_comp_deinit (session
->connection_state
.
376 write_compression_state
, 0);
378 _gnutls_free_datum (&session
->cipher_specs
.server_write_mac_secret
);
379 _gnutls_free_datum (&session
->cipher_specs
.client_write_mac_secret
);
380 _gnutls_free_datum (&session
->cipher_specs
.server_write_IV
);
381 _gnutls_free_datum (&session
->cipher_specs
.client_write_IV
);
382 _gnutls_free_datum (&session
->cipher_specs
.server_write_key
);
383 _gnutls_free_datum (&session
->cipher_specs
.client_write_key
);
385 if (session
->key
!= NULL
)
387 _gnutls_mpi_release (&session
->key
->KEY
);
388 _gnutls_mpi_release (&session
->key
->client_Y
);
389 _gnutls_mpi_release (&session
->key
->client_p
);
390 _gnutls_mpi_release (&session
->key
->client_g
);
392 _gnutls_mpi_release (&session
->key
->u
);
393 _gnutls_mpi_release (&session
->key
->a
);
394 _gnutls_mpi_release (&session
->key
->x
);
395 _gnutls_mpi_release (&session
->key
->A
);
396 _gnutls_mpi_release (&session
->key
->B
);
397 _gnutls_mpi_release (&session
->key
->b
);
400 _gnutls_mpi_release (&session
->key
->rsa
[0]);
401 _gnutls_mpi_release (&session
->key
->rsa
[1]);
403 _gnutls_mpi_release (&session
->key
->dh_secret
);
404 gnutls_free (session
->key
);
409 gnutls_free (session
->internals
.srp_username
);
411 if (session
->internals
.srp_password
)
413 memset (session
->internals
.srp_password
, 0,
414 strlen (session
->internals
.srp_password
));
415 gnutls_free (session
->internals
.srp_password
);
418 memset (session
, 0, sizeof (struct gnutls_session_int
));
419 gnutls_free (session
);
422 /* Returns the minimum prime bits that are acceptable.
425 _gnutls_dh_get_allowed_prime_bits (gnutls_session_t session
)
427 return session
->internals
.dh_prime_bits
;
431 _gnutls_dh_set_peer_public (gnutls_session_t session
, mpi_t
public)
436 switch (gnutls_auth_get_type (session
))
438 case GNUTLS_CRD_ANON
:
440 anon_auth_info_t info
;
441 info
= _gnutls_get_auth_info (session
);
443 return GNUTLS_E_INTERNAL_ERROR
;
450 psk_auth_info_t info
;
451 info
= _gnutls_get_auth_info (session
);
453 return GNUTLS_E_INTERNAL_ERROR
;
458 case GNUTLS_CRD_CERTIFICATE
:
460 cert_auth_info_t info
;
462 info
= _gnutls_get_auth_info (session
);
464 return GNUTLS_E_INTERNAL_ERROR
;
471 return GNUTLS_E_INTERNAL_ERROR
;
474 ret
= _gnutls_mpi_dprint_lz (&dh
->public_key
, public);
485 _gnutls_dh_set_secret_bits (gnutls_session_t session
, unsigned bits
)
487 switch (gnutls_auth_get_type (session
))
489 case GNUTLS_CRD_ANON
:
491 anon_auth_info_t info
;
492 info
= _gnutls_get_auth_info (session
);
494 return GNUTLS_E_INTERNAL_ERROR
;
495 info
->dh
.secret_bits
= bits
;
500 psk_auth_info_t info
;
501 info
= _gnutls_get_auth_info (session
);
503 return GNUTLS_E_INTERNAL_ERROR
;
504 info
->dh
.secret_bits
= bits
;
507 case GNUTLS_CRD_CERTIFICATE
:
509 cert_auth_info_t info
;
511 info
= _gnutls_get_auth_info (session
);
513 return GNUTLS_E_INTERNAL_ERROR
;
515 info
->dh
.secret_bits
= bits
;
519 return GNUTLS_E_INTERNAL_ERROR
;
526 /* This function will set in the auth info structure the
527 * RSA exponent and the modulus.
530 _gnutls_rsa_export_set_pubkey (gnutls_session_t session
,
531 mpi_t exponent
, mpi_t modulus
)
533 cert_auth_info_t info
;
536 info
= _gnutls_get_auth_info (session
);
538 return GNUTLS_E_INTERNAL_ERROR
;
540 ret
= _gnutls_mpi_dprint_lz (&info
->rsa_export
.modulus
, modulus
);
547 ret
= _gnutls_mpi_dprint_lz (&info
->rsa_export
.exponent
, exponent
);
551 _gnutls_free_datum (&info
->rsa_export
.modulus
);
559 /* Sets the prime and the generator in the auth info structure.
562 _gnutls_dh_set_group (gnutls_session_t session
, mpi_t gen
, mpi_t prime
)
567 switch (gnutls_auth_get_type (session
))
569 case GNUTLS_CRD_ANON
:
571 anon_auth_info_t info
;
572 info
= _gnutls_get_auth_info (session
);
574 return GNUTLS_E_INTERNAL_ERROR
;
581 psk_auth_info_t info
;
582 info
= _gnutls_get_auth_info (session
);
584 return GNUTLS_E_INTERNAL_ERROR
;
589 case GNUTLS_CRD_CERTIFICATE
:
591 cert_auth_info_t info
;
593 info
= _gnutls_get_auth_info (session
);
595 return GNUTLS_E_INTERNAL_ERROR
;
602 return GNUTLS_E_INTERNAL_ERROR
;
607 ret
= _gnutls_mpi_dprint_lz (&dh
->prime
, prime
);
616 ret
= _gnutls_mpi_dprint_lz (&dh
->generator
, gen
);
620 _gnutls_free_datum (&dh
->prime
);
628 * gnutls_openpgp_send_key - This function will order gnutls to send the openpgp fingerprint instead of the key
629 * @session: is a pointer to a #gnutls_session_t structure.
630 * @status: is one of OPENPGP_KEY, or OPENPGP_KEY_FINGERPRINT
632 * This function will order gnutls to send the key fingerprint instead
633 * of the key in the initial handshake procedure. This should be used
634 * with care and only when there is indication or knowledge that the
635 * server can obtain the client's key.
639 gnutls_openpgp_send_key (gnutls_session_t session
,
640 gnutls_openpgp_key_status_t status
)
642 session
->internals
.pgp_fingerprint
= status
;
646 * gnutls_certificate_send_x509_rdn_sequence - This function will order gnutls to send or not the x.509 rdn sequence
647 * @session: is a pointer to a #gnutls_session_t structure.
650 * If status is non zero, this function will order gnutls not to send the rdnSequence
651 * in the certificate request message. That is the server will not advertize
652 * it's trusted CAs to the peer. If status is zero then the default behaviour will
653 * take effect, which is to advertize the server's trusted CAs.
655 * This function has no effect in clients, and in authentication methods other than
656 * certificate with X.509 certificates.
660 gnutls_certificate_send_x509_rdn_sequence (gnutls_session_t session
,
663 session
->internals
.ignore_rdn_sequence
= status
;
667 _gnutls_openpgp_send_fingerprint (gnutls_session_t session
)
669 return session
->internals
.pgp_fingerprint
;
673 * _gnutls_record_set_default_version - Used to set the default version for the first record packet
674 * @session: is a #gnutls_session_t structure.
675 * @major: is a tls major version
676 * @minor: is a tls minor version
678 * This function sets the default version that we will use in the first
679 * record packet (client hello). This function is only useful to people
680 * that know TLS internals and want to debug other implementations.
684 _gnutls_record_set_default_version (gnutls_session_t session
,
685 unsigned char major
, unsigned char minor
)
687 session
->internals
.default_record_version
[0] = major
;
688 session
->internals
.default_record_version
[1] = minor
;
692 * gnutls_handshake_set_private_extensions - Used to enable the private cipher suites
693 * @session: is a #gnutls_session_t structure.
694 * @allow: is an integer (0 or 1)
696 * This function will enable or disable the use of private
697 * cipher suites (the ones that start with 0xFF). By default
698 * or if @allow is 0 then these cipher suites will not be
699 * advertized nor used.
701 * Unless this function is called with the option to allow (1), then
702 * no compression algorithms, like LZO. That is because these algorithms
703 * are not yet defined in any RFC or even internet draft.
705 * Enabling the private ciphersuites when talking to other than gnutls
706 * servers and clients may cause interoperability problems.
710 gnutls_handshake_set_private_extensions (gnutls_session_t session
, int allow
)
712 session
->internals
.enable_private
= allow
;
716 _gnutls_cal_PRF_A (gnutls_mac_algorithm_t algorithm
,
717 const void *secret
, int secret_size
,
718 const void *seed
, int seed_size
, void *result
)
722 td1
= _gnutls_hmac_init (algorithm
, secret
, secret_size
);
723 if (td1
== GNUTLS_MAC_FAILED
)
726 return GNUTLS_E_INTERNAL_ERROR
;
729 _gnutls_hmac (td1
, seed
, seed_size
);
730 _gnutls_hmac_deinit (td1
, result
);
735 #define MAX_SEED_SIZE 200
737 /* Produces "total_bytes" bytes using the hash algorithm specified.
738 * (used in the PRF function)
741 _gnutls_P_hash (gnutls_mac_algorithm_t algorithm
,
742 const opaque
* secret
, int secret_size
,
743 const opaque
* seed
, int seed_size
,
744 int total_bytes
, opaque
* ret
)
748 int i
, times
, how
, blocksize
, A_size
;
749 opaque final
[20], Atmp
[MAX_SEED_SIZE
];
750 int output_bytes
, result
;
752 if (seed_size
> MAX_SEED_SIZE
|| total_bytes
<= 0)
755 return GNUTLS_E_INTERNAL_ERROR
;
758 blocksize
= _gnutls_hmac_get_algo_len (algorithm
);
763 output_bytes
+= blocksize
;
765 while (output_bytes
< total_bytes
);
769 memcpy (Atmp
, seed
, seed_size
);
772 times
= output_bytes
/ blocksize
;
774 for (i
= 0; i
< times
; i
++)
776 td2
= _gnutls_hmac_init (algorithm
, secret
, secret_size
);
777 if (td2
== GNUTLS_MAC_FAILED
)
780 return GNUTLS_E_INTERNAL_ERROR
;
783 /* here we calculate A(i+1) */
785 _gnutls_cal_PRF_A (algorithm
, secret
, secret_size
, Atmp
,
789 _gnutls_hmac_deinit (td2
, final
);
795 _gnutls_hmac (td2
, Atmp
, A_size
);
796 _gnutls_hmac (td2
, seed
, seed_size
);
797 _gnutls_hmac_deinit (td2
, final
);
799 if ((1 + i
) * blocksize
< total_bytes
)
805 how
= total_bytes
- (i
) * blocksize
;
810 memcpy (&ret
[i
* blocksize
], final
, how
);
817 /* Xor's two buffers and puts the output in the first one.
820 _gnutls_xor (opaque
* o1
, opaque
* o2
, int length
)
823 for (i
= 0; i
< length
; i
++)
831 #define MAX_PRF_BYTES 200
833 /* The PRF function expands a given secret
834 * needed by the TLS specification. ret must have a least total_bytes
838 _gnutls_PRF (gnutls_session_t session
,
839 const opaque
* secret
, int secret_size
, const char *label
,
840 int label_size
, const opaque
* seed
, int seed_size
,
841 int total_bytes
, void *ret
)
843 int l_s
, s_seed_size
;
844 const opaque
*s1
, *s2
;
845 opaque s_seed
[MAX_SEED_SIZE
];
846 opaque o1
[MAX_PRF_BYTES
], o2
[MAX_PRF_BYTES
];
848 gnutls_protocol_t ver
= gnutls_protocol_get_version (session
);
850 if (total_bytes
> MAX_PRF_BYTES
)
853 return GNUTLS_E_INTERNAL_ERROR
;
855 /* label+seed = s_seed */
856 s_seed_size
= seed_size
+ label_size
;
858 if (s_seed_size
> MAX_SEED_SIZE
)
861 return GNUTLS_E_INTERNAL_ERROR
;
864 memcpy (s_seed
, label
, label_size
);
865 memcpy (&s_seed
[label_size
], seed
, seed_size
);
867 if (ver
>= GNUTLS_TLS1_2
)
870 _gnutls_P_hash (GNUTLS_MAC_SHA1
, secret
, secret_size
,
881 l_s
= secret_size
/ 2;
886 if (secret_size
% 2 != 0)
892 _gnutls_P_hash (GNUTLS_MAC_MD5
, s1
, l_s
, s_seed
, s_seed_size
,
901 _gnutls_P_hash (GNUTLS_MAC_SHA1
, s2
, l_s
, s_seed
, s_seed_size
,
909 _gnutls_xor (o1
, o2
, total_bytes
);
911 memcpy (ret
, o1
, total_bytes
);
919 * gnutls_prf_raw - access the TLS PRF directly
920 * @session: is a #gnutls_session_t structure.
921 * @label_size: length of the @label variable.
922 * @label: label used in PRF computation, typically a short string.
923 * @seed_size: length of the @seed variable.
924 * @seed: optional extra data to seed the PRF with.
925 * @outsize: size of pre-allocated output buffer to hold the output.
926 * @out: pre-allocate buffer to hold the generated data.
928 * Apply the TLS Pseudo-Random-Function (PRF) using the master secret
931 * The @label variable usually contain a string denoting the purpose
932 * for the generated data. The @seed usually contain data such as the
933 * client and server random, perhaps together with some additional
934 * data that is added to guarantee uniqueness of the output for a
935 * particular purpose.
937 * Because the output is not guaranteed to be unique for a particular
938 * session unless @seed include the client random and server random
939 * fields (the PRF would output the same data on another connection
940 * resumed from the first one), it is not recommended to use this
941 * function directly. The gnutls_prf() function seed the PRF with the
942 * client and server random fields directly, and is recommended if you
943 * want to generate pseudo random data unique for each session.
945 * Return value: Return 0 on success, or an error code.
948 gnutls_prf_raw (gnutls_session_t session
,
951 size_t seed_size
, const char *seed
, size_t outsize
, char *out
)
955 ret
= _gnutls_PRF (session
,
956 session
->security_parameters
.master_secret
,
959 label_size
, (opaque
*) seed
, seed_size
, outsize
, out
);
965 * gnutls_prf - derive pseudo-random data using the TLS PRF
966 * @session: is a #gnutls_session_t structure.
967 * @label_size: length of the @label variable.
968 * @label: label used in PRF computation, typically a short string.
969 * @server_random_first: non-0 if server random field should be first in seed
970 * @extra_size: length of the @extra variable.
971 * @extra: optional extra data to seed the PRF with.
972 * @outsize: size of pre-allocated output buffer to hold the output.
973 * @out: pre-allocate buffer to hold the generated data.
975 * Apply the TLS Pseudo-Random-Function (PRF) using the master secret
976 * on some data, seeded with the client and server random fields.
978 * The @label variable usually contain a string denoting the purpose
979 * for the generated data. The @server_random_first indicate whether
980 * the client random field or the server random field should be first
981 * in the seed. Non-0 indicate that the server random field is first,
982 * 0 that the client random field is first.
984 * The @extra variable can be used to add more data to the seed, after
985 * the random variables. It can be used to tie make sure the
986 * generated output is strongly connected to some additional data
987 * (e.g., a string used in user authentication).
989 * The output is placed in *@OUT, which must be pre-allocated.
991 * Return value: Return 0 on success, or an error code.
994 gnutls_prf (gnutls_session_t session
,
997 int server_random_first
,
998 size_t extra_size
, const char *extra
, size_t outsize
, char *out
)
1002 size_t seedsize
= 2 * TLS_RANDOM_SIZE
+ extra_size
;
1004 seed
= gnutls_malloc (seedsize
);
1008 return GNUTLS_E_MEMORY_ERROR
;
1011 memcpy (seed
, server_random_first
?
1012 session
->security_parameters
.server_random
:
1013 session
->security_parameters
.client_random
, TLS_RANDOM_SIZE
);
1014 memcpy (seed
+ TLS_RANDOM_SIZE
, server_random_first
?
1015 session
->security_parameters
.client_random
:
1016 session
->security_parameters
.server_random
, TLS_RANDOM_SIZE
);
1018 memcpy (seed
+ 2 * TLS_RANDOM_SIZE
, extra
, extra_size
);
1020 ret
= _gnutls_PRF (session
, session
->security_parameters
.master_secret
,
1022 label
, label_size
, seed
, seedsize
, outsize
, out
);
1030 * gnutls_session_get_client_random - get the session's client random value
1031 * @session: is a #gnutls_session_t structure.
1033 * Return a pointer to the 32-byte client random field used in the
1034 * session. The pointer must not be modified or deallocated.
1036 * If a client random value has not yet been established, the output
1037 * will be garbage; in particular, a %NULL return value should not be
1040 * Return value: pointer to client random.
1043 gnutls_session_get_client_random (gnutls_session_t session
)
1045 return (char *) session
->security_parameters
.client_random
;
1049 * gnutls_session_get_server_random - get the session's server random value
1050 * @session: is a #gnutls_session_t structure.
1052 * Return a pointer to the 32-byte server random field used in the
1053 * session. The pointer must not be modified or deallocated.
1055 * If a server random value has not yet been established, the output
1056 * will be garbage; in particular, a %NULL return value should not be
1059 * Return value: pointer to server random.
1062 gnutls_session_get_server_random (gnutls_session_t session
)
1064 return (char *) session
->security_parameters
.server_random
;
1068 * gnutls_session_get_master_secret - get the session's master secret value
1069 * @session: is a #gnutls_session_t structure.
1071 * Return a pointer to the 48-byte master secret in the session. The
1072 * pointer must not be modified or deallocated.
1074 * If a master secret value has not yet been established, the output
1075 * will be garbage; in particular, a %NULL return value should not be
1078 * Consider using gnutls_prf() rather than extracting the master
1079 * secret and use it to derive further data.
1081 * Return value: pointer to master secret.
1084 gnutls_session_get_master_secret (gnutls_session_t session
)
1086 return (char *) session
->security_parameters
.master_secret
;
1090 * gnutls_session_is_resumed - Used to check whether this session is a resumed one
1091 * @session: is a #gnutls_session_t structure.
1093 * This function will return non zero if this session is a resumed one,
1094 * or a zero if this is a new session.
1098 gnutls_session_is_resumed (gnutls_session_t session
)
1100 if (session
->security_parameters
.entity
== GNUTLS_CLIENT
)
1102 if (session
->security_parameters
.session_id_size
> 0 &&
1103 session
->security_parameters
.session_id_size
==
1104 session
->internals
.resumed_security_parameters
.session_id_size
1105 && memcmp (session
->security_parameters
.session_id
,
1106 session
->internals
.resumed_security_parameters
.
1108 session
->security_parameters
.session_id_size
) == 0)
1113 if (session
->internals
.resumed
== RESUME_TRUE
)
1121 * _gnutls_session_is_export - Used to check whether this session is of export grade
1122 * @session: is a #gnutls_session_t structure.
1124 * This function will return non zero if this session is of export grade.
1128 _gnutls_session_is_export (gnutls_session_t session
)
1130 gnutls_cipher_algorithm_t cipher
;
1133 _gnutls_cipher_suite_get_cipher_algo (&session
->security_parameters
.
1134 current_cipher_suite
);
1136 if (_gnutls_cipher_get_export_flag (cipher
) != 0)
1143 * gnutls_session_get_ptr - Used to get the user pointer from the session structure
1144 * @session: is a #gnutls_session_t structure.
1146 * This function will return the user given pointer from the session structure.
1147 * This is the pointer set with gnutls_session_set_ptr().
1151 gnutls_session_get_ptr (gnutls_session_t session
)
1153 return session
->internals
.user_ptr
;
1157 * gnutls_session_set_ptr - Used to set the user pointer to the session structure
1158 * @session: is a #gnutls_session_t structure.
1159 * @ptr: is the user pointer
1161 * This function will set (associate) the user given pointer to the
1162 * session structure. This is pointer can be accessed with
1163 * gnutls_session_get_ptr().
1167 gnutls_session_set_ptr (gnutls_session_t session
, void *ptr
)
1169 session
->internals
.user_ptr
= ptr
;
1174 * gnutls_record_get_direction - This function will return the direction of the last interrupted function call
1175 * @session: is a #gnutls_session_t structure.
1177 * This function provides information about the internals of the record
1178 * protocol and is only useful if a prior gnutls function call (e.g.
1179 * gnutls_handshake()) was interrupted for some reason, that is, if a function
1180 * returned GNUTLS_E_INTERRUPTED or GNUTLS_E_AGAIN. In such a case, you might
1181 * want to call select() or poll() before calling the interrupted gnutls
1182 * function again. To tell you whether a file descriptor should be selected
1183 * for either reading or writing, gnutls_record_get_direction() returns 0 if
1184 * the interrupted function was trying to read data, and 1 if it was trying to
1189 gnutls_record_get_direction (gnutls_session_t session
)
1191 return session
->internals
.direction
;
1195 * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS
1196 * @session: is a #gnutls_session_t structure.
1197 * @major: is the major version to use
1198 * @minor: is the minor version to use
1200 * This function will set the given version number to be used at the
1201 * RSA PMS secret. This is only useful to clients, which want to
1202 * test server's capabilities.
1206 _gnutls_rsa_pms_set_version (gnutls_session_t session
,
1207 unsigned char major
, unsigned char minor
)
1209 session
->internals
.rsa_pms_version
[0] = major
;
1210 session
->internals
.rsa_pms_version
[1] = minor
;