Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / lib / gnutls_constate.h
1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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 2.1 of
11  * the License, or (at your option) any later version.
12  *
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.
17  *
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/>
20  *
21  */
22
23 #ifndef GNUTLS_CONSTATE_H
24 #define GNUTLS_CONSTATE_H
25
26 int _gnutls_epoch_set_cipher_suite(gnutls_session_t session, int epoch_rel,
27                                    const uint8_t suite[2]);
28 int _gnutls_epoch_set_compression(gnutls_session_t session, int epoch_rel,
29                                   gnutls_compression_method_t comp_algo);
30 int _gnutls_epoch_get_compression(gnutls_session_t session, int epoch_rel);
31 void _gnutls_epoch_set_null_algos(gnutls_session_t session,
32                                   record_parameters_st * params);
33 int _gnutls_epoch_set_keys(gnutls_session_t session, uint16_t epoch);
34 int _gnutls_connection_state_init(gnutls_session_t session);
35 int _gnutls_read_connection_state_init(gnutls_session_t session);
36 int _gnutls_write_connection_state_init(gnutls_session_t session);
37
38 int _gnutls_epoch_get(gnutls_session_t session, unsigned int epoch_rel,
39                       record_parameters_st ** params_out);
40 int _gnutls_epoch_alloc(gnutls_session_t session, uint16_t epoch,
41                         record_parameters_st ** out);
42 void _gnutls_epoch_gc(gnutls_session_t session);
43 void _gnutls_epoch_free(gnutls_session_t session,
44                         record_parameters_st * state);
45
46 static inline int _gnutls_epoch_is_valid(gnutls_session_t session,
47                                          int epoch)
48 {
49         record_parameters_st *params;
50         int ret;
51
52         ret = _gnutls_epoch_get(session, epoch, &params);
53         if (ret < 0)
54                 return 0;
55
56         return 1;
57 }
58
59
60 static inline int _gnutls_epoch_refcount_inc(gnutls_session_t session,
61                                              int epoch)
62 {
63         record_parameters_st *params;
64         int ret;
65
66         ret = _gnutls_epoch_get(session, epoch, &params);
67         if (ret < 0)
68                 return ret;
69
70         params->usage_cnt++;
71
72         return params->epoch;
73 }
74
75 static inline int _gnutls_epoch_refcount_dec(gnutls_session_t session,
76                                              uint16_t epoch)
77 {
78         record_parameters_st *params;
79         int ret;
80
81         ret = _gnutls_epoch_get(session, epoch, &params);
82         if (ret < 0)
83                 return ret;
84
85         params->usage_cnt--;
86         if (params->usage_cnt < 0)
87                 return GNUTLS_E_INTERNAL_ERROR;
88
89         return 0;
90 }
91
92 #endif