2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/rand.h>
16 int RSA_padding_add_SSLv23(unsigned char *to
, int tlen
,
17 const unsigned char *from
, int flen
)
22 if (flen
> (tlen
- 11)) {
23 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23
,
24 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
28 p
= (unsigned char *)to
;
31 *(p
++) = 2; /* Public Key BT (Block Type) */
33 /* pad out with non-zero random data */
34 j
= tlen
- 3 - 8 - flen
;
36 if (RAND_bytes(p
, j
) <= 0)
38 for (i
= 0; i
< j
; i
++) {
41 if (RAND_bytes(p
, 1) <= 0)
51 memcpy(p
, from
, (unsigned int)flen
);
55 int RSA_padding_check_SSLv23(unsigned char *to
, int tlen
,
56 const unsigned char *from
, int flen
, int num
)
59 const unsigned char *p
;
63 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
, RSA_R_DATA_TOO_SMALL
);
66 /* Accept even zero-padded input */
69 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
, RSA_R_BLOCK_TYPE_IS_NOT_02
);
74 if ((num
!= (flen
+ 1)) || (*(p
++) != 02)) {
75 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
, RSA_R_BLOCK_TYPE_IS_NOT_02
);
79 /* scan over padding data */
80 j
= flen
- 1; /* one for type */
81 for (i
= 0; i
< j
; i
++)
85 if ((i
== j
) || (i
< 8)) {
86 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
,
87 RSA_R_NULL_BEFORE_BLOCK_MISSING
);
90 for (k
= -9; k
< -1; k
++) {
95 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
, RSA_R_SSLV3_ROLLBACK_ATTACK
);
99 i
++; /* Skip over the '\0' */
102 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23
, RSA_R_DATA_TOO_LARGE
);
105 memcpy(to
, p
, (unsigned int)j
);