rsa_ssl.c 6.1 KB

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