/src/FreeRDP/libfreerdp/crypto/crypto.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Cryptographic Abstraction Layer |
4 | | * |
5 | | * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2023 Armin Novak <anovak@thincast.com> |
7 | | * Copyright 2023 Thincast Technologies GmbH |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <errno.h> |
23 | | |
24 | | #include <openssl/objects.h> |
25 | | #include <openssl/bn.h> |
26 | | |
27 | | #include <freerdp/config.h> |
28 | | |
29 | | #include <winpr/crt.h> |
30 | | #include <winpr/assert.h> |
31 | | |
32 | | #include <freerdp/log.h> |
33 | | #include <freerdp/crypto/crypto.h> |
34 | | |
35 | | #include "crypto.h" |
36 | | #include "privatekey.h" |
37 | | |
38 | | #if !defined(_WIN32) |
39 | | #include <sys/stat.h> |
40 | | #endif |
41 | | |
42 | | #define TAG FREERDP_TAG("crypto") |
43 | | |
44 | | static SSIZE_T crypto_rsa_common(const BYTE* input, size_t length, UINT32 key_length, |
45 | | const BYTE* modulus, const BYTE* exponent, size_t exponent_size, |
46 | | BYTE* output, size_t out_length) |
47 | 0 | { |
48 | 0 | BN_CTX* ctx = nullptr; |
49 | 0 | int output_length = -1; |
50 | 0 | BYTE* input_reverse = nullptr; |
51 | 0 | BYTE* modulus_reverse = nullptr; |
52 | 0 | BYTE* exponent_reverse = nullptr; |
53 | 0 | BIGNUM* mod = nullptr; |
54 | 0 | BIGNUM* exp = nullptr; |
55 | 0 | BIGNUM* x = nullptr; |
56 | 0 | BIGNUM* y = nullptr; |
57 | 0 | size_t bufferSize = 0; |
58 | |
|
59 | 0 | if (!input || !modulus || !exponent || !output) |
60 | 0 | return -1; |
61 | | |
62 | 0 | if (exponent_size > INT_MAX / 2) |
63 | 0 | return -1; |
64 | | |
65 | 0 | if (key_length >= INT_MAX / 2 - exponent_size) |
66 | 0 | return -1; |
67 | | |
68 | 0 | bufferSize = 2ULL * key_length + exponent_size; |
69 | 0 | if (length > bufferSize) |
70 | 0 | bufferSize = length; |
71 | |
|
72 | 0 | input_reverse = (BYTE*)calloc(bufferSize, 1); |
73 | |
|
74 | 0 | if (!input_reverse) |
75 | 0 | return -1; |
76 | | |
77 | 0 | modulus_reverse = input_reverse + key_length; |
78 | 0 | exponent_reverse = modulus_reverse + key_length; |
79 | 0 | memmove(modulus_reverse, modulus, key_length); |
80 | 0 | crypto_reverse(modulus_reverse, key_length); |
81 | 0 | memmove(exponent_reverse, exponent, exponent_size); |
82 | 0 | crypto_reverse(exponent_reverse, exponent_size); |
83 | 0 | memmove(input_reverse, input, length); |
84 | 0 | crypto_reverse(input_reverse, length); |
85 | |
|
86 | 0 | if (!(ctx = BN_CTX_new())) |
87 | 0 | goto fail; |
88 | | |
89 | 0 | if (!(mod = BN_new())) |
90 | 0 | goto fail; |
91 | | |
92 | 0 | if (!(exp = BN_new())) |
93 | 0 | goto fail; |
94 | | |
95 | 0 | if (!(x = BN_new())) |
96 | 0 | goto fail; |
97 | | |
98 | 0 | if (!(y = BN_new())) |
99 | 0 | goto fail; |
100 | | |
101 | 0 | if (!BN_bin2bn(modulus_reverse, (int)key_length, mod)) |
102 | 0 | goto fail; |
103 | | |
104 | 0 | if (!BN_bin2bn(exponent_reverse, (int)exponent_size, exp)) |
105 | 0 | goto fail; |
106 | 0 | if (!BN_bin2bn(input_reverse, (int)length, x)) |
107 | 0 | goto fail; |
108 | 0 | if (BN_mod_exp(y, x, exp, mod, ctx) != 1) |
109 | 0 | goto fail; |
110 | 0 | { |
111 | 0 | const int len = BN_num_bytes(y); |
112 | 0 | if ((len < 0) || (WINPR_ASSERTING_INT_CAST(size_t, len) > out_length)) |
113 | 0 | goto fail; |
114 | 0 | output_length = BN_bn2bin(y, output); |
115 | 0 | } |
116 | 0 | if (output_length < 0) |
117 | 0 | goto fail; |
118 | 0 | crypto_reverse(output, WINPR_ASSERTING_INT_CAST(size_t, output_length)); |
119 | |
|
120 | 0 | if ((size_t)output_length < key_length) |
121 | 0 | { |
122 | 0 | size_t diff = key_length - WINPR_ASSERTING_INT_CAST(size_t, output_length); |
123 | 0 | if ((size_t)output_length + diff > out_length) |
124 | 0 | diff = out_length - (size_t)output_length; |
125 | 0 | memset(output + output_length, 0, diff); |
126 | 0 | } |
127 | |
|
128 | 0 | fail: |
129 | 0 | BN_free(y); |
130 | 0 | BN_clear_free(x); |
131 | 0 | BN_free(exp); |
132 | 0 | BN_free(mod); |
133 | 0 | BN_CTX_free(ctx); |
134 | 0 | free(input_reverse); |
135 | 0 | return output_length; |
136 | 0 | } |
137 | | |
138 | | static SSIZE_T crypto_rsa_public(const BYTE* input, size_t length, const rdpCertInfo* cert, |
139 | | BYTE* output, size_t output_length) |
140 | 0 | { |
141 | 0 | WINPR_ASSERT(cert); |
142 | 0 | return crypto_rsa_common(input, length, cert->ModulusLength, cert->Modulus, cert->exponent, |
143 | 0 | sizeof(cert->exponent), output, output_length); |
144 | 0 | } |
145 | | |
146 | | static SSIZE_T crypto_rsa_private(const BYTE* input, size_t length, const rdpPrivateKey* key, |
147 | | BYTE* output, size_t output_length) |
148 | 0 | { |
149 | 0 | WINPR_ASSERT(key); |
150 | 0 | const rdpCertInfo* info = freerdp_key_get_info(key); |
151 | 0 | WINPR_ASSERT(info); |
152 | |
|
153 | 0 | size_t PrivateExponentLength = 0; |
154 | 0 | const BYTE* PrivateExponent = freerdp_key_get_exponent(key, &PrivateExponentLength); |
155 | 0 | return crypto_rsa_common(input, length, info->ModulusLength, info->Modulus, PrivateExponent, |
156 | 0 | PrivateExponentLength, output, output_length); |
157 | 0 | } |
158 | | |
159 | | SSIZE_T crypto_rsa_public_encrypt(const BYTE* input, size_t length, const rdpCertInfo* cert, |
160 | | BYTE* output, size_t output_length) |
161 | 0 | { |
162 | 0 | return crypto_rsa_public(input, length, cert, output, output_length); |
163 | 0 | } |
164 | | |
165 | | SSIZE_T crypto_rsa_public_decrypt(const BYTE* input, size_t length, const rdpCertInfo* cert, |
166 | | BYTE* output, size_t output_length) |
167 | 0 | { |
168 | 0 | return crypto_rsa_public(input, length, cert, output, output_length); |
169 | 0 | } |
170 | | |
171 | | SSIZE_T crypto_rsa_private_encrypt(const BYTE* input, size_t length, const rdpPrivateKey* key, |
172 | | BYTE* output, size_t output_length) |
173 | 0 | { |
174 | 0 | return crypto_rsa_private(input, length, key, output, output_length); |
175 | 0 | } |
176 | | |
177 | | SSIZE_T crypto_rsa_private_decrypt(const BYTE* input, size_t length, const rdpPrivateKey* key, |
178 | | BYTE* output, size_t output_length) |
179 | 0 | { |
180 | 0 | return crypto_rsa_private(input, length, key, output, output_length); |
181 | 0 | } |
182 | | |
183 | | void crypto_reverse(BYTE* data, size_t length) |
184 | 0 | { |
185 | 0 | if (length < 1) |
186 | 0 | return; |
187 | | |
188 | 0 | for (size_t i = 0, j = length - 1; i < j; i++, j--) |
189 | 0 | { |
190 | 0 | const BYTE temp = data[i]; |
191 | 0 | data[i] = data[j]; |
192 | 0 | data[j] = temp; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | | char* crypto_read_pem(const char* WINPR_RESTRICT filename, size_t* WINPR_RESTRICT plength) |
197 | 0 | { |
198 | 0 | char* pem = nullptr; |
199 | 0 | FILE* fp = nullptr; |
200 | |
|
201 | 0 | WINPR_ASSERT(filename); |
202 | |
|
203 | 0 | if (plength) |
204 | 0 | *plength = 0; |
205 | | |
206 | | /* Binary mode: the size is taken from SEEK_END and then demanded in a single fread(). |
207 | | * In text mode on Windows CRLF collapses to LF, fewer than size bytes come back and the |
208 | | * read fails, which makes every CRLF-terminated PEM unreadable -- and that is what the |
209 | | * Windows tooling writes. */ |
210 | 0 | fp = winpr_fopen(filename, "rb"); |
211 | 0 | if (!fp) |
212 | 0 | goto fail; |
213 | | |
214 | 0 | { |
215 | 0 | const int rs = _fseeki64(fp, 0, SEEK_END); |
216 | 0 | if (rs < 0) |
217 | 0 | goto fail; |
218 | 0 | } |
219 | | |
220 | 0 | { |
221 | 0 | const int64_t size = _ftelli64(fp); |
222 | 0 | if (size < 0) |
223 | 0 | goto fail; |
224 | | |
225 | 0 | { |
226 | 0 | const int rc = _fseeki64(fp, 0, SEEK_SET); |
227 | 0 | if (rc < 0) |
228 | 0 | goto fail; |
229 | 0 | } |
230 | | |
231 | 0 | pem = calloc(WINPR_ASSERTING_INT_CAST(size_t, size) + 1, sizeof(char)); |
232 | 0 | if (!pem) |
233 | 0 | goto fail; |
234 | | |
235 | 0 | { |
236 | 0 | const size_t fr = fread(pem, (size_t)size, 1, fp); |
237 | 0 | if (fr != 1) |
238 | 0 | goto fail; |
239 | 0 | } |
240 | | |
241 | 0 | if (plength) |
242 | 0 | *plength = strnlen(pem, WINPR_ASSERTING_INT_CAST(size_t, size)); |
243 | 0 | } |
244 | 0 | (void)fclose(fp); |
245 | 0 | return pem; |
246 | | |
247 | 0 | fail: |
248 | 0 | { |
249 | 0 | char buffer[8192] = WINPR_C_ARRAY_INIT; |
250 | 0 | WLog_WARN(TAG, "Failed to read PEM from file '%s' [%s]", filename, |
251 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
252 | 0 | } |
253 | 0 | if (fp) |
254 | 0 | (void)fclose(fp); |
255 | 0 | free(pem); |
256 | 0 | return nullptr; |
257 | 0 | } |
258 | | |
259 | | BOOL crypto_write_pem(const char* WINPR_RESTRICT filename, const char* WINPR_RESTRICT pem, |
260 | | size_t length) |
261 | 0 | { |
262 | 0 | WINPR_ASSERT(filename); |
263 | 0 | WINPR_ASSERT(pem || (length == 0)); |
264 | |
|
265 | 0 | WINPR_ASSERT(filename); |
266 | 0 | WINPR_ASSERT(pem); |
267 | |
|
268 | 0 | const size_t size = strnlen(pem, length) + 1; |
269 | 0 | size_t rc = 0; |
270 | 0 | FILE* fp = winpr_fopen(filename, "w"); |
271 | 0 | if (!fp) |
272 | 0 | goto fail; |
273 | 0 | #if !defined(_WIN32) |
274 | 0 | const int res = fchmod(fileno(fp), S_IRUSR | S_IWUSR); |
275 | 0 | if (res != 0) |
276 | 0 | { |
277 | 0 | char buffer[128] = WINPR_C_ARRAY_INIT; |
278 | 0 | WLog_WARN(TAG, "Failed to chmod %s: %s", filename, |
279 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
280 | 0 | const int fres = fclose(fp); |
281 | 0 | if (fres != 0) |
282 | 0 | { |
283 | 0 | char buffer2[128] = WINPR_C_ARRAY_INIT; |
284 | 0 | WLog_WARN(TAG, "Failed to close PEM [%" PRIuz "] to file '%s' [%s]", length, filename, |
285 | 0 | winpr_strerror(errno, buffer2, sizeof(buffer2))); |
286 | 0 | } |
287 | 0 | goto fail; |
288 | 0 | } |
289 | 0 | #endif |
290 | 0 | rc = fwrite(pem, 1, size, fp); |
291 | 0 | const int fres = fclose(fp); |
292 | 0 | if (fres != 0) |
293 | 0 | { |
294 | 0 | char buffer[128] = WINPR_C_ARRAY_INIT; |
295 | 0 | WLog_WARN(TAG, "Failed to close PEM [%" PRIuz "] to file '%s' [%s]", length, filename, |
296 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
297 | 0 | } |
298 | 0 | fail: |
299 | 0 | if (rc == 0) |
300 | 0 | { |
301 | 0 | char buffer[128] = WINPR_C_ARRAY_INIT; |
302 | 0 | WLog_WARN(TAG, "Failed to write PEM [%" PRIuz "] to file '%s' [%s]", length, filename, |
303 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
304 | 0 | } |
305 | 0 | return rc == size; |
306 | 0 | } |