Fix reading broken BER in gcrypt/rsa.c
[tinc] / src / connection.c
1 /*
2     connection.c -- connection list management
3     Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5                   2008      Max Rijevski <maksuf@gmail.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "list.h"
25 #include "cipher.h"
26 #include "conf.h"
27 #include "control_common.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "rsa.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 list_t connection_list = {
35         .head = NULL,
36         .tail = NULL,
37         .count = 0,
38         .delete = (list_action_t) free_connection,
39 };
40
41 connection_t *everyone;
42
43 void init_connections(void) {
44         everyone = new_connection();
45         everyone->name = xstrdup("everyone");
46         everyone->hostname = xstrdup("BROADCAST");
47 }
48
49 void exit_connections(void) {
50         list_empty_list(&connection_list);
51
52         free_connection(everyone);
53         everyone = NULL;
54 }
55
56 connection_t *new_connection(void) {
57         return xzalloc(sizeof(connection_t));
58 }
59
60 #ifndef DISABLE_LEGACY
61 bool init_crypto_by_nid(legacy_crypto_t *c, nid_t cipher, nid_t digest) {
62         if(!cipher_open_by_nid(&c->cipher, cipher)) {
63                 return false;
64         }
65
66         if(!digest_open_by_nid(&c->digest, digest, DIGEST_ALGO_SIZE)) {
67                 cipher_close(&c->cipher);
68                 return false;
69         }
70
71         c->budget = cipher_budget(&c->cipher);
72         return true;
73 }
74
75 bool init_crypto_by_name(legacy_crypto_t *c, const char *cipher, const char *digest) {
76         if(!cipher_open_by_name(&c->cipher, cipher)) {
77                 return false;
78         }
79
80         if(!digest_open_by_name(&c->digest, digest, DIGEST_ALGO_SIZE)) {
81                 cipher_close(&c->cipher);
82                 return false;
83         }
84
85         c->budget = cipher_budget(&c->cipher);
86         return true;
87 }
88
89 bool decrease_budget(legacy_crypto_t *c, size_t bytes) {
90         if(bytes > c->budget) {
91                 return false;
92         } else {
93                 c->budget -= bytes;
94                 return true;
95         }
96 }
97
98 static void close_legacy_crypto(legacy_crypto_t *c) {
99         cipher_close(&c->cipher);
100         digest_close(&c->digest);
101 }
102
103 legacy_ctx_t *new_legacy_ctx(rsa_t *rsa) {
104         legacy_ctx_t *ctx = xzalloc(sizeof(legacy_ctx_t));
105         ctx->rsa = rsa;
106         return ctx;
107 }
108
109 void free_legacy_ctx(legacy_ctx_t *ctx) {
110         if(ctx) {
111                 close_legacy_crypto(&ctx->in);
112                 close_legacy_crypto(&ctx->out);
113                 rsa_free(ctx->rsa);
114                 free(ctx);
115         }
116 }
117 #endif
118
119 void free_connection(connection_t *c) {
120         if(!c) {
121                 return;
122         }
123
124 #ifndef DISABLE_LEGACY
125         free_legacy_ctx(c->legacy);
126 #endif
127
128         sptps_stop(&c->sptps);
129         ecdsa_free(c->ecdsa);
130
131         free(c->hischallenge);
132         free(c->mychallenge);
133
134         buffer_clear(&c->inbuf);
135         buffer_clear(&c->outbuf);
136
137         io_del(&c->io);
138
139         if(c->socket > 0) {
140                 if(c->status.tarpit) {
141                         tarpit(c->socket);
142                 } else {
143                         closesocket(c->socket);
144                 }
145         }
146
147         free(c->name);
148         free(c->hostname);
149
150         if(c->config_tree) {
151                 exit_configuration(&c->config_tree);
152         }
153
154         free(c);
155 }
156
157 void connection_add(connection_t *c) {
158         list_insert_tail(&connection_list, c);
159 }
160
161 void connection_del(connection_t *c) {
162         list_delete(&connection_list, c);
163 }
164
165 bool dump_connections(connection_t *cdump) {
166         for list_each(connection_t, c, &connection_list) {
167                 send_request(cdump, "%d %d %s %s %x %d %x",
168                              CONTROL, REQ_DUMP_CONNECTIONS,
169                              c->name, c->hostname, c->options, c->socket,
170                              c->status.value);
171         }
172
173         return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
174 }