1 /* crypto/rsa/rsa_ssl.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/bn.h>
62 #include <openssl/rsa.h>
63 #include <openssl/rand.h>
64 #include "constant_time_locl.h"
65 
RSA_padding_add_SSLv23(unsigned char * to,int tlen,const unsigned char * from,int flen)66 int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
67                            const unsigned char *from, int flen)
68 {
69     int i, j;
70     unsigned char *p;
71 
72     if (flen > (tlen - 11)) {
73         RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,
74                RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
75         return (0);
76     }
77 
78     p = (unsigned char *)to;
79 
80     *(p++) = 0;
81     *(p++) = 2;                 /* Public Key BT (Block Type) */
82 
83     /* pad out with non-zero random data */
84     j = tlen - 3 - 8 - flen;
85 
86     if (RAND_bytes(p, j) <= 0)
87         return (0);
88     for (i = 0; i < j; i++) {
89         if (*p == '\0')
90             do {
91                 if (RAND_bytes(p, 1) <= 0)
92                     return (0);
93             } while (*p == '\0');
94         p++;
95     }
96 
97     memset(p, 3, 8);
98     p += 8;
99     *(p++) = '\0';
100 
101     memcpy(p, from, (unsigned int)flen);
102     return (1);
103 }
104 
105 /*
106  * Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding
107  * if nul delimiter is not preceded by 8 consecutive 0x03 bytes. It also
108  * preserves error code reporting for backward compatibility.
109  */
RSA_padding_check_SSLv23(unsigned char * to,int tlen,const unsigned char * from,int flen,int num)110 int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
111                              const unsigned char *from, int flen, int num)
112 {
113     int i;
114     /* |em| is the encoded message, zero-padded to exactly |num| bytes */
115     unsigned char *em = NULL;
116     unsigned int good, found_zero_byte, mask, threes_in_row;
117     int zero_index = 0, msg_index, mlen = -1, err;
118 
119     if (tlen <= 0 || flen <= 0)
120         return -1;
121 
122     if (flen > num || num < 11) {
123         RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);
124         return (-1);
125     }
126 
127     em = OPENSSL_malloc(num);
128     if (em == NULL) {
129         RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, ERR_R_MALLOC_FAILURE);
130         return -1;
131     }
132     /*
133      * Caller is encouraged to pass zero-padded message created with
134      * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
135      * bounds, it's impossible to have an invariant memory access pattern
136      * in case |from| was not zero-padded in advance.
137      */
138     for (from += flen, em += num, i = 0; i < num; i++) {
139         mask = ~constant_time_is_zero(flen);
140         flen -= 1 & mask;
141         from -= 1 & mask;
142         *--em = *from & mask;
143     }
144 
145     good = constant_time_is_zero(em[0]);
146     good &= constant_time_eq(em[1], 2);
147     err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02);
148     mask = ~good;
149 
150     /* scan over padding data */
151     found_zero_byte = 0;
152     threes_in_row = 0;
153     for (i = 2; i < num; i++) {
154         unsigned int equals0 = constant_time_is_zero(em[i]);
155 
156         zero_index = constant_time_select_int(~found_zero_byte & equals0,
157                                               i, zero_index);
158         found_zero_byte |= equals0;
159 
160         threes_in_row += 1 & ~found_zero_byte;
161         threes_in_row &= found_zero_byte | constant_time_eq(em[i], 3);
162     }
163 
164     /*
165      * PS must be at least 8 bytes long, and it starts two bytes into |em|.
166      * If we never found a 0-byte, then |zero_index| is 0 and the check
167      * also fails.
168      */
169     good &= constant_time_ge(zero_index, 2 + 8);
170     err = constant_time_select_int(mask | good, err,
171                                    RSA_R_NULL_BEFORE_BLOCK_MISSING);
172     mask = ~good;
173 
174     good &= constant_time_ge(threes_in_row, 8);
175     err = constant_time_select_int(mask | good, err,
176                                    RSA_R_SSLV3_ROLLBACK_ATTACK);
177     mask = ~good;
178 
179     /*
180      * Skip the zero byte. This is incorrect if we never found a zero-byte
181      * but in this case we also do not copy the message out.
182      */
183     msg_index = zero_index + 1;
184     mlen = num - msg_index;
185 
186     /*
187      * For good measure, do this check in constant time as well.
188      */
189     good &= constant_time_ge(tlen, mlen);
190     err = constant_time_select_int(mask | good, err, RSA_R_DATA_TOO_LARGE);
191 
192     /*
193      * Move the result in-place by |num|-11-|mlen| bytes to the left.
194      * Then if |good| move |mlen| bytes from |em|+11 to |to|.
195      * Otherwise leave |to| unchanged.
196      * Copy the memory back in a way that does not reveal the size of
197      * the data being copied via a timing side channel. This requires copying
198      * parts of the buffer multiple times based on the bits set in the real
199      * length. Clear bits do a non-copy with identical access pattern.
200      * The loop below has overall complexity of O(N*log(N)).
201      */
202     tlen = constant_time_select_int(constant_time_lt(num - 11, tlen),
203                                     num - 11, tlen);
204     for (msg_index = 1; msg_index < num - 11; msg_index <<= 1) {
205         mask = ~constant_time_eq(msg_index & (num - 11 - mlen), 0);
206         for (i = 11; i < num - msg_index; i++)
207             em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
208     }
209     for (i = 0; i < tlen; i++) {
210         mask = good & constant_time_lt(i, mlen);
211         to[i] = constant_time_select_8(mask, em[i + 11], to[i]);
212     }
213 
214     OPENSSL_cleanse(em, num);
215     OPENSSL_free(em);
216     RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, err);
217     err_clear_last_constant_time(1 & good);
218 
219     return constant_time_select_int(good, mlen, -1);
220 }
221