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