]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_ssl.c
7cb743d219d8ed9cf969cabebeb9b4c879f9e51e
[thirdparty/openssl.git] / crypto / rsa / rsa_ssl.c
1 /*
2 * Copyright 1995-2021 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 /*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/bn.h>
19 #include <openssl/rsa.h>
20 #include <openssl/rand.h>
21 #include "internal/constant_time.h"
22 #include "rsa_local.h"
23
24 int ossl_rsa_padding_add_SSLv23_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
25 int tlen, const unsigned char *from,
26 int flen)
27 {
28 int i, j;
29 unsigned char *p;
30
31 if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
32 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
33 return 0;
34 }
35
36 p = (unsigned char *)to;
37
38 *(p++) = 0;
39 *(p++) = 2; /* Public Key BT (Block Type) */
40
41 /* pad out with non-zero random data */
42 j = tlen - 3 - 8 - flen;
43
44 if (RAND_bytes_ex(libctx, p, j) <= 0)
45 return 0;
46 for (i = 0; i < j; i++) {
47 if (*p == '\0')
48 do {
49 if (RAND_bytes_ex(libctx, p, 1) <= 0)
50 return 0;
51 } while (*p == '\0');
52 p++;
53 }
54
55 memset(p, 3, 8);
56 p += 8;
57 *(p++) = '\0';
58
59 memcpy(p, from, (unsigned int)flen);
60 return 1;
61 }
62
63 int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
64 const unsigned char *from, int flen)
65 {
66 return ossl_rsa_padding_add_SSLv23_ex(NULL, to, tlen, from, flen);
67 }
68
69 /*
70 * Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding
71 * if nul delimiter is preceded by 8 consecutive 0x03 bytes. It also
72 * preserves error code reporting for backward compatibility.
73 */
74 int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
75 const unsigned char *from, int flen, int num)
76 {
77 int i;
78 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
79 unsigned char *em = NULL;
80 unsigned int good, found_zero_byte, mask, threes_in_row;
81 int zero_index = 0, msg_index, mlen = -1, err;
82
83 if (tlen <= 0 || flen <= 0)
84 return -1;
85
86 if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
87 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_SMALL);
88 return -1;
89 }
90
91 em = OPENSSL_malloc(num);
92 if (em == NULL) {
93 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
94 return -1;
95 }
96 /*
97 * Caller is encouraged to pass zero-padded message created with
98 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
99 * bounds, it's impossible to have an invariant memory access pattern
100 * in case |from| was not zero-padded in advance.
101 */
102 for (from += flen, em += num, i = 0; i < num; i++) {
103 mask = ~constant_time_is_zero(flen);
104 flen -= 1 & mask;
105 from -= 1 & mask;
106 *--em = *from & mask;
107 }
108
109 good = constant_time_is_zero(em[0]);
110 good &= constant_time_eq(em[1], 2);
111 err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02);
112 mask = ~good;
113
114 /* scan over padding data */
115 found_zero_byte = 0;
116 threes_in_row = 0;
117 for (i = 2; i < num; i++) {
118 unsigned int equals0 = constant_time_is_zero(em[i]);
119
120 zero_index = constant_time_select_int(~found_zero_byte & equals0,
121 i, zero_index);
122 found_zero_byte |= equals0;
123
124 threes_in_row += 1 & ~found_zero_byte;
125 threes_in_row &= found_zero_byte | constant_time_eq(em[i], 3);
126 }
127
128 /*
129 * PS must be at least 8 bytes long, and it starts two bytes into |em|.
130 * If we never found a 0-byte, then |zero_index| is 0 and the check
131 * also fails.
132 */
133 good &= constant_time_ge(zero_index, 2 + 8);
134 err = constant_time_select_int(mask | good, err,
135 RSA_R_NULL_BEFORE_BLOCK_MISSING);
136 mask = ~good;
137
138 /*
139 * Reject if nul delimiter is preceded by 8 consecutive 0x03 bytes. Note
140 * that RFC5246 incorrectly states this the other way around, i.e. reject
141 * if it is not preceded by 8 consecutive 0x03 bytes. However this is
142 * corrected in subsequent errata for that RFC.
143 */
144 good &= constant_time_lt(threes_in_row, 8);
145 err = constant_time_select_int(mask | good, err,
146 RSA_R_SSLV3_ROLLBACK_ATTACK);
147 mask = ~good;
148
149 /*
150 * Skip the zero byte. This is incorrect if we never found a zero-byte
151 * but in this case we also do not copy the message out.
152 */
153 msg_index = zero_index + 1;
154 mlen = num - msg_index;
155
156 /*
157 * For good measure, do this check in constant time as well.
158 */
159 good &= constant_time_ge(tlen, mlen);
160 err = constant_time_select_int(mask | good, err, RSA_R_DATA_TOO_LARGE);
161
162 /*
163 * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
164 * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
165 * Otherwise leave |to| unchanged.
166 * Copy the memory back in a way that does not reveal the size of
167 * the data being copied via a timing side channel. This requires copying
168 * parts of the buffer multiple times based on the bits set in the real
169 * length. Clear bits do a non-copy with identical access pattern.
170 * The loop below has overall complexity of O(N*log(N)).
171 */
172 tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
173 num - RSA_PKCS1_PADDING_SIZE, tlen);
174 for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
175 mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
176 for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
177 em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
178 }
179 for (i = 0; i < tlen; i++) {
180 mask = good & constant_time_lt(i, mlen);
181 to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
182 }
183
184 OPENSSL_clear_free(em, num);
185 ERR_raise(ERR_LIB_RSA, err);
186 err_clear_last_constant_time(1 & good);
187
188 return constant_time_select_int(good, mlen, -1);
189 }