rsa_ssl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 1995-2018 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/rand.h>
  14. #include "internal/constant_time_locl.h"
  15. int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
  16. const unsigned char *from, int flen)
  17. {
  18. int i, j;
  19. unsigned char *p;
  20. if (flen > (tlen - 11)) {
  21. RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,
  22. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  23. return 0;
  24. }
  25. p = (unsigned char *)to;
  26. *(p++) = 0;
  27. *(p++) = 2; /* Public Key BT (Block Type) */
  28. /* pad out with non-zero random data */
  29. j = tlen - 3 - 8 - flen;
  30. if (RAND_bytes(p, j) <= 0)
  31. return 0;
  32. for (i = 0; i < j; i++) {
  33. if (*p == '\0')
  34. do {
  35. if (RAND_bytes(p, 1) <= 0)
  36. return 0;
  37. } while (*p == '\0');
  38. p++;
  39. }
  40. memset(p, 3, 8);
  41. p += 8;
  42. *(p++) = '\0';
  43. memcpy(p, from, (unsigned int)flen);
  44. return 1;
  45. }
  46. /*
  47. * Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding
  48. * if nul delimiter is preceded by 8 consecutive 0x03 bytes. It also
  49. * preserves error code reporting for backward compatibility.
  50. */
  51. int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
  52. const unsigned char *from, int flen, int num)
  53. {
  54. int i;
  55. /* |em| is the encoded message, zero-padded to exactly |num| bytes */
  56. unsigned char *em = NULL;
  57. unsigned int good, found_zero_byte, mask, threes_in_row;
  58. int zero_index = 0, msg_index, mlen = -1, err;
  59. if (flen < 10) {
  60. RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);
  61. return -1;
  62. }
  63. em = OPENSSL_malloc(num);
  64. if (em == NULL) {
  65. RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, ERR_R_MALLOC_FAILURE);
  66. return -1;
  67. }
  68. /*
  69. * Caller is encouraged to pass zero-padded message created with
  70. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  71. * bounds, it's impossible to have an invariant memory access pattern
  72. * in case |from| was not zero-padded in advance.
  73. */
  74. for (from += flen, em += num, i = 0; i < num; i++) {
  75. mask = ~constant_time_is_zero(flen);
  76. flen -= 1 & mask;
  77. from -= 1 & mask;
  78. *--em = *from & mask;
  79. }
  80. from = em;
  81. good = constant_time_is_zero(from[0]);
  82. good &= constant_time_eq(from[1], 2);
  83. err = constant_time_select_int(good, 0, RSA_R_BLOCK_TYPE_IS_NOT_02);
  84. mask = ~good;
  85. /* scan over padding data */
  86. found_zero_byte = 0;
  87. threes_in_row = 0;
  88. for (i = 2; i < num; i++) {
  89. unsigned int equals0 = constant_time_is_zero(from[i]);
  90. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  91. i, zero_index);
  92. found_zero_byte |= equals0;
  93. threes_in_row += 1 & ~found_zero_byte;
  94. threes_in_row &= found_zero_byte | constant_time_eq(from[i], 3);
  95. }
  96. /*
  97. * PS must be at least 8 bytes long, and it starts two bytes into |from|.
  98. * If we never found a 0-byte, then |zero_index| is 0 and the check
  99. * also fails.
  100. */
  101. good &= constant_time_ge(zero_index, 2 + 8);
  102. err = constant_time_select_int(mask | good, err,
  103. RSA_R_NULL_BEFORE_BLOCK_MISSING);
  104. mask = ~good;
  105. good &= constant_time_lt(threes_in_row, 8);
  106. err = constant_time_select_int(mask | good, err,
  107. RSA_R_SSLV3_ROLLBACK_ATTACK);
  108. mask = ~good;
  109. /*
  110. * Skip the zero byte. This is incorrect if we never found a zero-byte
  111. * but in this case we also do not copy the message out.
  112. */
  113. msg_index = zero_index + 1;
  114. mlen = num - msg_index;
  115. /*
  116. * For good measure, do this check in constant time as well.
  117. */
  118. good &= constant_time_ge(tlen, mlen);
  119. err = constant_time_select_int(mask | good, err, RSA_R_DATA_TOO_LARGE);
  120. /*
  121. * Even though we can't fake result's length, we can pretend copying
  122. * |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |num|
  123. * bytes are viewed as circular buffer with start at |tlen|-|mlen'|,
  124. * where |mlen'| is "saturated" |mlen| value. Deducing information
  125. * about failure or |mlen| would take attacker's ability to observe
  126. * memory access pattern with byte granularity *as it occurs*. It
  127. * should be noted that failure is indistinguishable from normal
  128. * operation if |tlen| is fixed by protocol.
  129. */
  130. tlen = constant_time_select_int(constant_time_lt(num, tlen), num, tlen);
  131. msg_index = constant_time_select_int(good, msg_index, num - tlen);
  132. mlen = num - msg_index;
  133. for (from += msg_index, mask = good, i = 0; i < tlen; i++) {
  134. unsigned int equals = constant_time_eq(i, mlen);
  135. from -= tlen & equals; /* if (i == mlen) rewind */
  136. mask &= mask ^ equals; /* if (i == mlen) mask = 0 */
  137. to[i] = constant_time_select_8(mask, from[i], to[i]);
  138. }
  139. OPENSSL_clear_free(em, num);
  140. RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, err);
  141. err_clear_last_constant_time(1 & good);
  142. return constant_time_select_int(good, mlen, -1);
  143. }