Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/rsa/rsa_ssl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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.h"
16
17
int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
18
                           const unsigned char *from, int flen)
19
0
{
20
0
    int i, j;
21
0
    unsigned char *p;
22
23
0
    if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
24
0
        RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,
25
0
               RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
26
0
        return 0;
27
0
    }
28
29
0
    p = (unsigned char *)to;
30
31
0
    *(p++) = 0;
32
0
    *(p++) = 2;                 /* Public Key BT (Block Type) */
33
34
    /* pad out with non-zero random data */
35
0
    j = tlen - 3 - 8 - flen;
36
37
0
    if (RAND_bytes(p, j) <= 0)
38
0
        return 0;
39
0
    for (i = 0; i < j; i++) {
40
0
        if (*p == '\0')
41
0
            do {
42
0
                if (RAND_bytes(p, 1) <= 0)
43
0
                    return 0;
44
0
            } while (*p == '\0');
45
0
        p++;
46
0
    }
47
48
0
    memset(p, 3, 8);
49
0
    p += 8;
50
0
    *(p++) = '\0';
51
52
0
    memcpy(p, from, (unsigned int)flen);
53
0
    return 1;
54
0
}
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
0
{
64
0
    int i;
65
    /* |em| is the encoded message, zero-padded to exactly |num| bytes */
66
0
    unsigned char *em = NULL;
67
0
    unsigned int good, found_zero_byte, mask, threes_in_row;
68
0
    int zero_index = 0, msg_index, mlen = -1, err;
69
70
0
    if (tlen <= 0 || flen <= 0)
71
0
        return -1;
72
73
0
    if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
74
0
        RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);
75
0
        return -1;
76
0
    }
77
78
0
    em = OPENSSL_malloc(num);
79
0
    if (em == NULL) {
80
0
        RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, ERR_R_MALLOC_FAILURE);
81
0
        return -1;
82
0
    }
83
    /*
84
     * Caller is encouraged to pass zero-padded message created with
85
     * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
86
     * bounds, it's impossible to have an invariant memory access pattern
87
     * in case |from| was not zero-padded in advance.
88
     */
89
0
    for (from += flen, em += num, i = 0; i < num; i++) {
90
0
        mask = ~constant_time_is_zero(flen);
91
0
        flen -= 1 & mask;
92
0
        from -= 1 & mask;
93
0
        *--em = *from & mask;
94
0
    }
95
96
0
    good = constant_time_is_zero(em[0]);
97
0
    good &= constant_time_eq(em[1], 2);
98
0
    err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02);
99
0
    mask = ~good;
100
101
    /* scan over padding data */
102
0
    found_zero_byte = 0;
103
0
    threes_in_row = 0;
104
0
    for (i = 2; i < num; i++) {
105
0
        unsigned int equals0 = constant_time_is_zero(em[i]);
106
107
0
        zero_index = constant_time_select_int(~found_zero_byte & equals0,
108
0
                                              i, zero_index);
109
0
        found_zero_byte |= equals0;
110
111
0
        threes_in_row += 1 & ~found_zero_byte;
112
0
        threes_in_row &= found_zero_byte | constant_time_eq(em[i], 3);
113
0
    }
114
115
    /*
116
     * PS must be at least 8 bytes long, and it starts two bytes into |em|.
117
     * If we never found a 0-byte, then |zero_index| is 0 and the check
118
     * also fails.
119
     */
120
0
    good &= constant_time_ge(zero_index, 2 + 8);
121
0
    err = constant_time_select_int(mask | good, err,
122
0
                                   RSA_R_NULL_BEFORE_BLOCK_MISSING);
123
0
    mask = ~good;
124
125
    /*
126
     * Reject if nul delimiter is preceded by 8 consecutive 0x03 bytes. Note
127
     * that RFC5246 incorrectly states this the other way around, i.e. reject
128
     * if it is not preceded by 8 consecutive 0x03 bytes. However this is
129
     * corrected in subsequent errata for that RFC.
130
     */
131
0
    good &= constant_time_lt(threes_in_row, 8);
132
0
    err = constant_time_select_int(mask | good, err,
133
0
                                   RSA_R_SSLV3_ROLLBACK_ATTACK);
134
0
    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
0
    msg_index = zero_index + 1;
141
0
    mlen = num - msg_index;
142
143
    /*
144
     * For good measure, do this check in constant time as well.
145
     */
146
0
    good &= constant_time_ge(tlen, mlen);
147
0
    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
0
    tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
160
0
                                    num - RSA_PKCS1_PADDING_SIZE, tlen);
161
0
    for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
162
0
        mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
163
0
        for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
164
0
            em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
165
0
    }
166
0
    for (i = 0; i < tlen; i++) {
167
0
        mask = good & constant_time_lt(i, mlen);
168
0
        to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
169
0
    }
170
171
0
    OPENSSL_clear_free(em, num);
172
0
    RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, err);
173
0
    err_clear_last_constant_time(1 & good);
174
175
0
    return constant_time_select_int(good, mlen, -1);
176
0
}