2 * Copyright 1995-2016 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 if ((num != (flen + 1)) || (*(p++) != 02)) {
67 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_BLOCK_TYPE_IS_NOT_02);
71 /* scan over padding data */
72 j = flen - 1; /* one for type */
73 for (i = 0; i < j; i++)
77 if ((i == j) || (i < 8)) {
78 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,
79 RSA_R_NULL_BEFORE_BLOCK_MISSING);
82 for (k = -9; k < -1; k++) {
87 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_SSLV3_ROLLBACK_ATTACK);
91 i++; /* Skip over the '\0' */
94 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_LARGE);
97 memcpy(to, p, (unsigned int)j);