/src/openssl/crypto/rsa/rsa_ssl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2017 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 | | |
16 | | int RSA_padding_add_SSLv23(unsigned char *to, int tlen, |
17 | | const unsigned char *from, int flen) |
18 | 0 | { |
19 | 0 | int i, j; |
20 | 0 | unsigned char *p; |
21 | 0 |
|
22 | 0 | if (flen > (tlen - 11)) { |
23 | 0 | RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23, |
24 | 0 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); |
25 | 0 | return 0; |
26 | 0 | } |
27 | 0 |
|
28 | 0 | p = (unsigned char *)to; |
29 | 0 |
|
30 | 0 | *(p++) = 0; |
31 | 0 | *(p++) = 2; /* Public Key BT (Block Type) */ |
32 | 0 |
|
33 | 0 | /* pad out with non-zero random data */ |
34 | 0 | j = tlen - 3 - 8 - flen; |
35 | 0 |
|
36 | 0 | if (RAND_bytes(p, j) <= 0) |
37 | 0 | return 0; |
38 | 0 | for (i = 0; i < j; i++) { |
39 | 0 | if (*p == '\0') |
40 | 0 | do { |
41 | 0 | if (RAND_bytes(p, 1) <= 0) |
42 | 0 | return 0; |
43 | 0 | } while (*p == '\0'); |
44 | 0 | p++; |
45 | 0 | } |
46 | 0 |
|
47 | 0 | memset(p, 3, 8); |
48 | 0 | p += 8; |
49 | 0 | *(p++) = '\0'; |
50 | 0 |
|
51 | 0 | memcpy(p, from, (unsigned int)flen); |
52 | 0 | return 1; |
53 | 0 | } |
54 | | |
55 | | int RSA_padding_check_SSLv23(unsigned char *to, int tlen, |
56 | | const unsigned char *from, int flen, int num) |
57 | 0 | { |
58 | 0 | int i, j, k; |
59 | 0 | const unsigned char *p; |
60 | 0 |
|
61 | 0 | p = from; |
62 | 0 | if (flen < 10) { |
63 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL); |
64 | 0 | return -1; |
65 | 0 | } |
66 | 0 | /* Accept even zero-padded input */ |
67 | 0 | if (flen == num) { |
68 | 0 | if (*(p++) != 0) { |
69 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_BLOCK_TYPE_IS_NOT_02); |
70 | 0 | return -1; |
71 | 0 | } |
72 | 0 | flen--; |
73 | 0 | } |
74 | 0 | if ((num != (flen + 1)) || (*(p++) != 02)) { |
75 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_BLOCK_TYPE_IS_NOT_02); |
76 | 0 | return -1; |
77 | 0 | } |
78 | 0 |
|
79 | 0 | /* scan over padding data */ |
80 | 0 | j = flen - 1; /* one for type */ |
81 | 0 | for (i = 0; i < j; i++) |
82 | 0 | if (*(p++) == 0) |
83 | 0 | break; |
84 | 0 |
|
85 | 0 | if ((i == j) || (i < 8)) { |
86 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, |
87 | 0 | RSA_R_NULL_BEFORE_BLOCK_MISSING); |
88 | 0 | return -1; |
89 | 0 | } |
90 | 0 | for (k = -9; k < -1; k++) { |
91 | 0 | if (p[k] != 0x03) |
92 | 0 | break; |
93 | 0 | } |
94 | 0 | if (k == -1) { |
95 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_SSLV3_ROLLBACK_ATTACK); |
96 | 0 | return -1; |
97 | 0 | } |
98 | 0 |
|
99 | 0 | i++; /* Skip over the '\0' */ |
100 | 0 | j -= i; |
101 | 0 | if (j > tlen) { |
102 | 0 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_LARGE); |
103 | 0 | return -1; |
104 | 0 | } |
105 | 0 | memcpy(to, p, (unsigned int)j); |
106 | 0 |
|
107 | 0 | return j; |
108 | 0 | } |