-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcrypto.c
262 lines (214 loc) · 4.98 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (c) 2014 Zhao, Gang <[email protected]>
* This is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*/
#include <string.h>
#include <netdb.h>
#include <openssl/bio.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "common.h"
#include "crypto.h"
int iv_len;
static const EVP_CIPHER *evp_cipher;
static const EVP_MD *md;
static char key[EVP_MAX_KEY_LENGTH];
static int key_len;
static const char supported_method[][MAX_METHOD_NAME_LEN] = {
"aes-128-cfb",
"aes-192-cfb",
"aes-256-cfb",
"bf-cfb",
/* "camellia-128-cfb", */
/* "camellia-192-cfb", */
/* "camellia-256-cfb", */
"cast5-cfb",
"des-cfb",
/* "idea-cfb", */
"rc2-cfb",
"rc4",
"seed-cfb",
/* "salsa20-ctr", */
};
int get_method(char *password, char *method)
{
int ret;
md = EVP_get_digestbyname("MD5");
if (md == NULL)
goto err;
evp_cipher = EVP_get_cipherbyname(ss_opt.method);
if (evp_cipher == NULL)
goto err;
key_len = EVP_CIPHER_key_length(evp_cipher);
iv_len = EVP_CIPHER_iv_length(evp_cipher);
ret = EVP_BytesToKey(evp_cipher, md, NULL,
(void *)password, strlen(password), 1,
(void *)key, NULL);
if (ret == 0)
goto err;
key[key_len] = '\0';
/* pr_data(stdout, "password", password, strlen(password)); */
/* pr_data(stdout, "key", key, key_len); */
return 0;
err:
ERR_print_errors_fp(stderr);
pr_exit("%s: failed\n", __func__);
}
int crypto_init(char *password, char *method)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
#endif
if (get_method(password, method) == -1)
return -1;
return 0;
}
void crypto_exit(void)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_cleanup();
ERR_free_strings();
#endif
}
int add_iv(int sockfd, struct link *ln)
{
int ret;
char *iv_p;
if (sockfd == ln->local_sockfd)
iv_p = ln->local_iv;
else if (sockfd == ln->server_sockfd)
iv_p = ln->server_iv;
else
goto err;
ret = add_data(sockfd, ln, "cipher", iv_p, iv_len);
if (ret != 0)
goto err;
ln->state |= SS_IV_SENT;
return 0;
err:
sock_warn(sockfd, "%s failed", __func__);
return -1;
}
/* iv is in the first iv_len byptes of ss tcp/udp header */
int receive_iv(int sockfd, struct link *ln)
{
int ret;
char *iv_p;
if (sockfd == ln->local_sockfd)
iv_p = ln->local_iv;
else if (sockfd == ln->server_sockfd)
iv_p = ln->server_iv;
else
goto err;
memcpy(iv_p, ln->cipher, iv_len);
ret = rm_data(sockfd, ln, "cipher", iv_len);
if (ret != 0)
goto err;
ln->state |= SS_IV_RECEIVED;
return 0;
err:
sock_warn(sockfd, "%s failed", __func__);
return -1;
}
static int check_cipher(int sockfd, struct link *ln, const char *type)
{
int ret;
char *iv_p;
EVP_CIPHER_CTX *ctx_p;
if (sockfd == ln->local_sockfd) {
iv_p = ln->local_iv;
ctx_p = ln->local_ctx;
} else if (sockfd == ln->server_sockfd) {
iv_p = ln->server_iv;
ctx_p = ln->server_ctx;
} else {
goto err;
}
if (strcmp(type, "encrypt") == 0 &&
!(ln->state & SS_IV_SENT)) {
if (RAND_bytes((void *)iv_p, iv_len) == -1)
goto err;
iv_p[iv_len] = '\0';
ret = EVP_EncryptInit_ex(ctx_p, evp_cipher,
NULL, (void *)key,
(void *)iv_p);
if (ret != 1)
goto err;
} else if (strcmp(type, "decrypt") == 0 &&
!(ln->state & SS_IV_RECEIVED)) {
if (receive_iv(sockfd, ln) == -1)
goto err;
ret = EVP_DecryptInit_ex(ctx_p, evp_cipher,
NULL, (void *)key,
(void *)iv_p);
if (ret != 1)
goto err;
}
return 0;
err:
sock_warn(sockfd, "%s failed", __func__);
return -1;
}
int crypto_encrypt(int sockfd, struct link *ln)
{
int len, cipher_len;
EVP_CIPHER_CTX *ctx_p;
if (check_cipher(sockfd, ln, "encrypt") == -1)
goto err;
if (sockfd == ln->local_sockfd) {
ctx_p = ln->local_ctx;
} else if (sockfd == ln->server_sockfd) {
ctx_p = ln->server_ctx;
} else {
goto err;
}
if (EVP_EncryptUpdate(ctx_p, ln->cipher, &len,
ln->text, ln->text_len) != 1)
goto err;
cipher_len = len;
ln->cipher_len = cipher_len;
if (!(ln->state & SS_IV_SENT))
if (add_iv(sockfd, ln) == -1)
goto err;
/* encryption succeeded, so text buffer is not needed */
ln->text_len = 0;
return ln->cipher_len;
err:
ERR_print_errors_fp(stderr);
pr_link_warn(ln);
sock_warn(sockfd, "%s failed", __func__);
return -1;
}
int crypto_decrypt(int sockfd, struct link *ln)
{
int len, text_len;
EVP_CIPHER_CTX *ctx_p;
if (check_cipher(sockfd, ln, "decrypt") == -1)
goto err;
if (sockfd == ln->local_sockfd) {
ctx_p = ln->local_ctx;
} else if (sockfd == ln->server_sockfd) {
ctx_p = ln->server_ctx;
} else {
goto err;
}
if (EVP_DecryptUpdate(ctx_p, ln->text, &len,
ln->cipher, ln->cipher_len) != 1) {
goto err;
}
text_len = len;
ln->text_len = text_len;
/* decryption succeeded, so cipher buffer is not needed */
ln->cipher_len = 0;
return text_len;
err:
ERR_print_errors_fp(stderr);
pr_link_warn(ln);
sock_warn(sockfd, "%s failed\n", __func__);
return -1;
}