]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_ssl.c
563c8ba513bf6479f5ac056446655d7b21183beb
[thirdparty/openssl.git] / crypto / rsa / rsa_ssl.c
1 /*
2 * Copyright 1995-2018 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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/rand.h>
15 #include "internal/constant_time_locl.h"
16
17 int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
18 const unsigned char *from, int flen)
19 {
20 int i, j;
21 unsigned char *p;
22
23 if (flen > (tlen - 11)) {
24 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,
25 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
26 return 0;
27 }
28
29 p = (unsigned char *)to;
30
31 *(p++) = 0;
32 *(p++) = 2; /* Public Key BT (Block Type) */
33
34 /* pad out with non-zero random data */
35 j = tlen - 3 - 8 - flen;
36
37 if (RAND_bytes(p, j) <= 0)
38 return 0;
39 for (i = 0; i < j; i++) {
40 if (*p == '\0')
41 do {
42 if (RAND_bytes(p, 1) <= 0)
43 return 0;
44 } while (*p == '\0');
45 p++;
46 }
47
48 memset(p, 3, 8);
49 p += 8;
50 *(p++) = '\0';
51
52 memcpy(p, from, (unsigned int)flen);
53 return 1;
54 }
55
56 /*
57 * Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding
58 * if nul delimiter is preceded by 8 consecutive 0x03 bytes. It also
59 * preserves error code reporting for backward compatibility.
60 */
61 int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
62 const unsigned char *from, int flen, int num)
63 {
64 int i;
65 /* |em| is the encoded message, zero-padded to exactly |num| bytes */
66 unsigned char *em = NULL;
67 unsigned int good, found_zero_byte, mask, threes_in_row;
68 int zero_index = 0, msg_index, mlen = -1, err;
69
70 if (flen < 10) {
71 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);
72 return -1;
73 }
74
75 em = OPENSSL_malloc(num);
76 if (em == NULL) {
77 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, ERR_R_MALLOC_FAILURE);
78 return -1;
79 }
80 /*
81 * Caller is encouraged to pass zero-padded message created with
82 * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
83 * bounds, it's impossible to have an invariant memory access pattern
84 * in case |from| was not zero-padded in advance.
85 */
86 for (from += flen, em += num, i = 0; i < num; i++) {
87 mask = ~constant_time_is_zero(flen);
88 flen -= 1 & mask;
89 from -= 1 & mask;
90 *--em = *from & mask;
91 }
92 from = em;
93
94 good = constant_time_is_zero(from[0]);
95 good &= constant_time_eq(from[1], 2);
96 err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02);
97 mask = ~good;
98
99 /* scan over padding data */
100 found_zero_byte = 0;
101 threes_in_row = 0;
102 for (i = 2; i < num; i++) {
103 unsigned int equals0 = constant_time_is_zero(from[i]);
104
105 zero_index = constant_time_select_int(~found_zero_byte & equals0,
106 i, zero_index);
107 found_zero_byte |= equals0;
108
109 threes_in_row += 1 & ~found_zero_byte;
110 threes_in_row &= found_zero_byte | constant_time_eq(from[i], 3);
111 }
112
113 /*
114 * PS must be at least 8 bytes long, and it starts two bytes into |from|.
115 * If we never found a 0-byte, then |zero_index| is 0 and the check
116 * also fails.
117 */
118 good &= constant_time_ge(zero_index, 2 + 8);
119 err = constant_time_select_int(mask | good, err,
120 RSA_R_NULL_BEFORE_BLOCK_MISSING);
121 mask = ~good;
122
123 good &= constant_time_lt(threes_in_row, 8);
124 err = constant_time_select_int(mask | good, err,
125 RSA_R_SSLV3_ROLLBACK_ATTACK);
126 mask = ~good;
127
128 /*
129 * Skip the zero byte. This is incorrect if we never found a zero-byte
130 * but in this case we also do not copy the message out.
131 */
132 msg_index = zero_index + 1;
133 mlen = num - msg_index;
134
135 /*
136 * For good measure, do this check in constant time as well.
137 */
138 good &= constant_time_ge(tlen, mlen);
139 err = constant_time_select_int(mask | good, err, RSA_R_DATA_TOO_LARGE);
140
141 /*
142 * Even though we can't fake result's length, we can pretend copying
143 * |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |num|
144 * bytes are viewed as circular buffer with start at |tlen|-|mlen'|,
145 * where |mlen'| is "saturated" |mlen| value. Deducing information
146 * about failure or |mlen| would take attacker's ability to observe
147 * memory access pattern with byte granularity *as it occurs*. It
148 * should be noted that failure is indistinguishable from normal
149 * operation if |tlen| is fixed by protocol.
150 */
151 tlen = constant_time_select_int(constant_time_lt(num, tlen), num, tlen);
152 msg_index = constant_time_select_int(good, msg_index, num - tlen);
153 mlen = num - msg_index;
154 for (from += msg_index, mask = good, i = 0; i < tlen; i++) {
155 unsigned int equals = constant_time_eq(i, mlen);
156
157 from -= tlen & equals; /* if (i == mlen) rewind */
158 mask &= mask ^ equals; /* if (i == mlen) mask = 0 */
159 to[i] = constant_time_select_8(mask, from[i], to[i]);
160 }
161
162 OPENSSL_clear_free(em, num);
163 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, err);
164 err_clear_last_constant_time(1 & good);
165
166 return constant_time_select_int(good, mlen, -1);
167 }