1/*
2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include "internal/constant_time.h"
11
12#include <stdio.h>
13#include <openssl/bn.h>
14#include <openssl/rsa.h>
15#include <openssl/rand.h>
16/* Just for the SSL_MAX_MASTER_KEY_LENGTH value */
17#include <openssl/ssl.h>
18#include "internal/cryptlib.h"
19#include "crypto/rsa.h"
20
21int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
22 const unsigned char *from, int flen)
23{
24 int j;
25 unsigned char *p;
26
27 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
28 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
29 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
30 return 0;
31 }
32
33 p = (unsigned char *)to;
34
35 *(p++) = 0;
36 *(p++) = 1; /* Private Key BT (Block Type) */
37
38 /* pad out with 0xff data */
39 j = tlen - 3 - flen;
40 memset(p, 0xff, j);
41 p += j;
42 *(p++) = '\0';
43 memcpy(p, from, (unsigned int)flen);
44 return 1;
45}
46
47int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
48 const unsigned char *from, int flen,
49 int num)
50{
51 int i, j;
52 const unsigned char *p;
53
54 p = from;
55
56 /*
57 * The format is
58 * 00 || 01 || PS || 00 || D
59 * PS - padding string, at least 8 bytes of FF
60 * D - data.
61 */
62
63 if (num < RSA_PKCS1_PADDING_SIZE)
64 return -1;
65
66 /* Accept inputs with and without the leading 0-byte. */
67 if (num == flen) {
68 if ((*p++) != 0x00) {
69 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
70 RSA_R_INVALID_PADDING);
71 return -1;
72 }
73 flen--;
74 }
75
76 if ((num != (flen + 1)) || (*(p++) != 0x01)) {
77 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
78 RSA_R_BLOCK_TYPE_IS_NOT_01);
79 return -1;
80 }
81
82 /* scan over padding data */
83 j = flen - 1; /* one for type. */
84 for (i = 0; i < j; i++) {
85 if (*p != 0xff) { /* should decrypt to 0xff */
86 if (*p == 0) {
87 p++;
88 break;
89 } else {
90 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
91 RSA_R_BAD_FIXED_HEADER_DECRYPT);
92 return -1;
93 }
94 }
95 p++;
96 }
97
98 if (i == j) {
99 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
100 RSA_R_NULL_BEFORE_BLOCK_MISSING);
101 return -1;
102 }
103
104 if (i < 8) {
105 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
106 RSA_R_BAD_PAD_BYTE_COUNT);
107 return -1;
108 }
109 i++; /* Skip over the '\0' */
110 j -= i;
111 if (j > tlen) {
112 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
113 return -1;
114 }
115 memcpy(to, p, (unsigned int)j);
116
117 return j;
118}
119
120int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
121 const unsigned char *from, int flen)
122{
123 int i, j;
124 unsigned char *p;
125
126 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
127 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
128 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
129 return 0;
130 }
131
132 p = (unsigned char *)to;
133
134 *(p++) = 0;
135 *(p++) = 2; /* Public Key BT (Block Type) */
136
137 /* pad out with non-zero random data */
138 j = tlen - 3 - flen;
139
140 if (RAND_bytes(p, j) <= 0)
141 return 0;
142 for (i = 0; i < j; i++) {
143 if (*p == '\0')
144 do {
145 if (RAND_bytes(p, 1) <= 0)
146 return 0;
147 } while (*p == '\0');
148 p++;
149 }
150
151 *(p++) = '\0';
152
153 memcpy(p, from, (unsigned int)flen);
154 return 1;
155}
156
157int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
158 const unsigned char *from, int flen,
159 int num)
160{
161 int i;
162 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
163 unsigned char *em = NULL;
164 unsigned int good, found_zero_byte, mask;
165 int zero_index = 0, msg_index, mlen = -1;
166
167 if (tlen <= 0 || flen <= 0)
168 return -1;
169
170 /*
171 * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
172 * section 7.2.2.
173 */
174
175 if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
176 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
177 RSA_R_PKCS_DECODING_ERROR);
178 return -1;
179 }
180
181 em = OPENSSL_malloc(num);
182 if (em == NULL) {
183 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
184 return -1;
185 }
186 /*
187 * Caller is encouraged to pass zero-padded message created with
188 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
189 * bounds, it's impossible to have an invariant memory access pattern
190 * in case |from| was not zero-padded in advance.
191 */
192 for (from += flen, em += num, i = 0; i < num; i++) {
193 mask = ~constant_time_is_zero(flen);
194 flen -= 1 & mask;
195 from -= 1 & mask;
196 *--em = *from & mask;
197 }
198
199 good = constant_time_is_zero(em[0]);
200 good &= constant_time_eq(em[1], 2);
201
202 /* scan over padding data */
203 found_zero_byte = 0;
204 for (i = 2; i < num; i++) {
205 unsigned int equals0 = constant_time_is_zero(em[i]);
206
207 zero_index = constant_time_select_int(~found_zero_byte & equals0,
208 i, zero_index);
209 found_zero_byte |= equals0;
210 }
211
212 /*
213 * PS must be at least 8 bytes long, and it starts two bytes into |em|.
214 * If we never found a 0-byte, then |zero_index| is 0 and the check
215 * also fails.
216 */
217 good &= constant_time_ge(zero_index, 2 + 8);
218
219 /*
220 * Skip the zero byte. This is incorrect if we never found a zero-byte
221 * but in this case we also do not copy the message out.
222 */
223 msg_index = zero_index + 1;
224 mlen = num - msg_index;
225
226 /*
227 * For good measure, do this check in constant time as well.
228 */
229 good &= constant_time_ge(tlen, mlen);
230
231 /*
232 * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
233 * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
234 * Otherwise leave |to| unchanged.
235 * Copy the memory back in a way that does not reveal the size of
236 * the data being copied via a timing side channel. This requires copying
237 * parts of the buffer multiple times based on the bits set in the real
238 * length. Clear bits do a non-copy with identical access pattern.
239 * The loop below has overall complexity of O(N*log(N)).
240 */
241 tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
242 num - RSA_PKCS1_PADDING_SIZE, tlen);
243 for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
244 mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
245 for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
246 em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
247 }
248 for (i = 0; i < tlen; i++) {
249 mask = good & constant_time_lt(i, mlen);
250 to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
251 }
252
253 OPENSSL_clear_free(em, num);
254 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_PKCS_DECODING_ERROR);
255 err_clear_last_constant_time(1 & good);
256
257 return constant_time_select_int(good, mlen, -1);
258}
259
260/*
261 * rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
262 * padding from a decrypted RSA message in a TLS signature. The result is stored
263 * in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
264 * must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
265 * should be stored in |from| which must be |flen| bytes in length and padded
266 * such that |flen == RSA_size()|. The TLS protocol version that the client
267 * originally requested should be passed in |client_version|. Some buggy clients
268 * can exist which use the negotiated version instead of the originally
269 * requested protocol version. If it is necessary to work around this bug then
270 * the negotiated protocol version can be passed in |alt_version|, otherwise 0
271 * should be passed.
272 *
273 * If the passed message is publicly invalid or some other error that can be
274 * treated in non-constant time occurs then -1 is returned. On success the
275 * length of the decrypted data is returned. This will always be
276 * SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
277 * constant time then this function will appear to return successfully, but the
278 * decrypted data will be randomly generated (as per
279 * https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
280 */
281int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen,
282 const unsigned char *from, size_t flen,
283 int client_version, int alt_version)
284{
285 unsigned int i, good, version_good;
286 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
287
288 /*
289 * If these checks fail then either the message in publicly invalid, or
290 * we've been called incorrectly. We can fail immediately.
291 */
292 if (flen < RSA_PKCS1_PADDING_SIZE + SSL_MAX_MASTER_KEY_LENGTH
293 || tlen < SSL_MAX_MASTER_KEY_LENGTH) {
294 ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
295 return -1;
296 }
297
298 /*
299 * Generate a random premaster secret to use in the event that we fail
300 * to decrypt.
301 */
302 if (RAND_priv_bytes(rand_premaster_secret,
303 sizeof(rand_premaster_secret)) <= 0) {
304 ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
305 return -1;
306 }
307
308 good = constant_time_is_zero(from[0]);
309 good &= constant_time_eq(from[1], 2);
310
311 /* Check we have the expected padding data */
312 for (i = 2; i < flen - SSL_MAX_MASTER_KEY_LENGTH - 1; i++)
313 good &= ~constant_time_is_zero_8(from[i]);
314 good &= constant_time_is_zero_8(from[flen - SSL_MAX_MASTER_KEY_LENGTH - 1]);
315
316
317 /*
318 * If the version in the decrypted pre-master secret is correct then
319 * version_good will be 0xff, otherwise it'll be zero. The
320 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
321 * (http://eprint.iacr.org/2003/052/) exploits the version number
322 * check as a "bad version oracle". Thus version checks are done in
323 * constant time and are treated like any other decryption error.
324 */
325 version_good =
326 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
327 (client_version >> 8) & 0xff);
328 version_good &=
329 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
330 client_version & 0xff);
331
332 /*
333 * The premaster secret must contain the same version number as the
334 * ClientHello to detect version rollback attacks (strangely, the
335 * protocol does not offer such protection for DH ciphersuites).
336 * However, buggy clients exist that send the negotiated protocol
337 * version instead if the server does not support the requested
338 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set then we tolerate
339 * such clients. In that case alt_version will be non-zero and set to
340 * the negotiated version.
341 */
342 if (alt_version > 0) {
343 unsigned int workaround_good;
344
345 workaround_good =
346 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
347 (alt_version >> 8) & 0xff);
348 workaround_good &=
349 constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
350 alt_version & 0xff);
351 version_good |= workaround_good;
352 }
353
354 good &= version_good;
355
356
357 /*
358 * Now copy the result over to the to buffer if good, or random data if
359 * not good.
360 */
361 for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) {
362 to[i] =
363 constant_time_select_8(good,
364 from[flen - SSL_MAX_MASTER_KEY_LENGTH + i],
365 rand_premaster_secret[i]);
366 }
367
368 /*
369 * We must not leak whether a decryption failure occurs because of
370 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
371 * section 7.4.7.1). The code follows that advice of the TLS RFC and
372 * generates a random premaster secret for the case that the decrypt
373 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
374 * So, whether we actually succeeded or not, return success.
375 */
376
377 return SSL_MAX_MASTER_KEY_LENGTH;
378}
379