2
0

rsa_pk1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright 1995-2019 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. #include "internal/constant_time.h"
  10. #include <stdio.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/rand.h>
  14. /* Just for the SSL_MAX_MASTER_KEY_LENGTH value */
  15. #include <openssl/ssl.h>
  16. #include "internal/cryptlib.h"
  17. #include "crypto/rsa.h"
  18. int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
  19. const unsigned char *from, int flen)
  20. {
  21. int j;
  22. unsigned char *p;
  23. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  24. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,
  25. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  26. return 0;
  27. }
  28. p = (unsigned char *)to;
  29. *(p++) = 0;
  30. *(p++) = 1; /* Private Key BT (Block Type) */
  31. /* pad out with 0xff data */
  32. j = tlen - 3 - flen;
  33. memset(p, 0xff, j);
  34. p += j;
  35. *(p++) = '\0';
  36. memcpy(p, from, (unsigned int)flen);
  37. return 1;
  38. }
  39. int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
  40. const unsigned char *from, int flen,
  41. int num)
  42. {
  43. int i, j;
  44. const unsigned char *p;
  45. p = from;
  46. /*
  47. * The format is
  48. * 00 || 01 || PS || 00 || D
  49. * PS - padding string, at least 8 bytes of FF
  50. * D - data.
  51. */
  52. if (num < RSA_PKCS1_PADDING_SIZE)
  53. return -1;
  54. /* Accept inputs with and without the leading 0-byte. */
  55. if (num == flen) {
  56. if ((*p++) != 0x00) {
  57. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  58. RSA_R_INVALID_PADDING);
  59. return -1;
  60. }
  61. flen--;
  62. }
  63. if ((num != (flen + 1)) || (*(p++) != 0x01)) {
  64. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  65. RSA_R_BLOCK_TYPE_IS_NOT_01);
  66. return -1;
  67. }
  68. /* scan over padding data */
  69. j = flen - 1; /* one for type. */
  70. for (i = 0; i < j; i++) {
  71. if (*p != 0xff) { /* should decrypt to 0xff */
  72. if (*p == 0) {
  73. p++;
  74. break;
  75. } else {
  76. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  77. RSA_R_BAD_FIXED_HEADER_DECRYPT);
  78. return -1;
  79. }
  80. }
  81. p++;
  82. }
  83. if (i == j) {
  84. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  85. RSA_R_NULL_BEFORE_BLOCK_MISSING);
  86. return -1;
  87. }
  88. if (i < 8) {
  89. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
  90. RSA_R_BAD_PAD_BYTE_COUNT);
  91. return -1;
  92. }
  93. i++; /* Skip over the '\0' */
  94. j -= i;
  95. if (j > tlen) {
  96. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
  97. return -1;
  98. }
  99. memcpy(to, p, (unsigned int)j);
  100. return j;
  101. }
  102. int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
  103. const unsigned char *from, int flen)
  104. {
  105. int i, j;
  106. unsigned char *p;
  107. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  108. RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
  109. RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  110. return 0;
  111. }
  112. p = (unsigned char *)to;
  113. *(p++) = 0;
  114. *(p++) = 2; /* Public Key BT (Block Type) */
  115. /* pad out with non-zero random data */
  116. j = tlen - 3 - flen;
  117. if (RAND_bytes(p, j) <= 0)
  118. return 0;
  119. for (i = 0; i < j; i++) {
  120. if (*p == '\0')
  121. do {
  122. if (RAND_bytes(p, 1) <= 0)
  123. return 0;
  124. } while (*p == '\0');
  125. p++;
  126. }
  127. *(p++) = '\0';
  128. memcpy(p, from, (unsigned int)flen);
  129. return 1;
  130. }
  131. int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
  132. const unsigned char *from, int flen,
  133. int num)
  134. {
  135. int i;
  136. /* |em| is the encoded message, zero-padded to exactly |num| bytes */
  137. unsigned char *em = NULL;
  138. unsigned int good, found_zero_byte, mask;
  139. int zero_index = 0, msg_index, mlen = -1;
  140. if (tlen <= 0 || flen <= 0)
  141. return -1;
  142. /*
  143. * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
  144. * section 7.2.2.
  145. */
  146. if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
  147. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
  148. RSA_R_PKCS_DECODING_ERROR);
  149. return -1;
  150. }
  151. em = OPENSSL_malloc(num);
  152. if (em == NULL) {
  153. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
  154. return -1;
  155. }
  156. /*
  157. * Caller is encouraged to pass zero-padded message created with
  158. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  159. * bounds, it's impossible to have an invariant memory access pattern
  160. * in case |from| was not zero-padded in advance.
  161. */
  162. for (from += flen, em += num, i = 0; i < num; i++) {
  163. mask = ~constant_time_is_zero(flen);
  164. flen -= 1 & mask;
  165. from -= 1 & mask;
  166. *--em = *from & mask;
  167. }
  168. good = constant_time_is_zero(em[0]);
  169. good &= constant_time_eq(em[1], 2);
  170. /* scan over padding data */
  171. found_zero_byte = 0;
  172. for (i = 2; i < num; i++) {
  173. unsigned int equals0 = constant_time_is_zero(em[i]);
  174. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  175. i, zero_index);
  176. found_zero_byte |= equals0;
  177. }
  178. /*
  179. * PS must be at least 8 bytes long, and it starts two bytes into |em|.
  180. * If we never found a 0-byte, then |zero_index| is 0 and the check
  181. * also fails.
  182. */
  183. good &= constant_time_ge(zero_index, 2 + 8);
  184. /*
  185. * Skip the zero byte. This is incorrect if we never found a zero-byte
  186. * but in this case we also do not copy the message out.
  187. */
  188. msg_index = zero_index + 1;
  189. mlen = num - msg_index;
  190. /*
  191. * For good measure, do this check in constant time as well.
  192. */
  193. good &= constant_time_ge(tlen, mlen);
  194. /*
  195. * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
  196. * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
  197. * Otherwise leave |to| unchanged.
  198. * Copy the memory back in a way that does not reveal the size of
  199. * the data being copied via a timing side channel. This requires copying
  200. * parts of the buffer multiple times based on the bits set in the real
  201. * length. Clear bits do a non-copy with identical access pattern.
  202. * The loop below has overall complexity of O(N*log(N)).
  203. */
  204. tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
  205. num - RSA_PKCS1_PADDING_SIZE, tlen);
  206. for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
  207. mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
  208. for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
  209. em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
  210. }
  211. for (i = 0; i < tlen; i++) {
  212. mask = good & constant_time_lt(i, mlen);
  213. to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
  214. }
  215. OPENSSL_clear_free(em, num);
  216. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_PKCS_DECODING_ERROR);
  217. err_clear_last_constant_time(1 & good);
  218. return constant_time_select_int(good, mlen, -1);
  219. }
  220. /*
  221. * rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
  222. * padding from a decrypted RSA message in a TLS signature. The result is stored
  223. * in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
  224. * must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
  225. * should be stored in |from| which must be |flen| bytes in length and padded
  226. * such that |flen == RSA_size()|. The TLS protocol version that the client
  227. * originally requested should be passed in |client_version|. Some buggy clients
  228. * can exist which use the negotiated version instead of the originally
  229. * requested protocol version. If it is necessary to work around this bug then
  230. * the negotiated protocol version can be passed in |alt_version|, otherwise 0
  231. * should be passed.
  232. *
  233. * If the passed message is publicly invalid or some other error that can be
  234. * treated in non-constant time occurs then -1 is returned. On success the
  235. * length of the decrypted data is returned. This will always be
  236. * SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
  237. * constant time then this function will appear to return successfully, but the
  238. * decrypted data will be randomly generated (as per
  239. * https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
  240. */
  241. int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen,
  242. const unsigned char *from, size_t flen,
  243. int client_version, int alt_version)
  244. {
  245. unsigned int i, good, version_good;
  246. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  247. /*
  248. * If these checks fail then either the message in publicly invalid, or
  249. * we've been called incorrectly. We can fail immediately.
  250. */
  251. if (flen < RSA_PKCS1_PADDING_SIZE + SSL_MAX_MASTER_KEY_LENGTH
  252. || tlen < SSL_MAX_MASTER_KEY_LENGTH) {
  253. ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
  254. return -1;
  255. }
  256. /*
  257. * Generate a random premaster secret to use in the event that we fail
  258. * to decrypt.
  259. */
  260. if (RAND_priv_bytes(rand_premaster_secret,
  261. sizeof(rand_premaster_secret)) <= 0) {
  262. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  263. return -1;
  264. }
  265. good = constant_time_is_zero(from[0]);
  266. good &= constant_time_eq(from[1], 2);
  267. /* Check we have the expected padding data */
  268. for (i = 2; i < flen - SSL_MAX_MASTER_KEY_LENGTH - 1; i++)
  269. good &= ~constant_time_is_zero_8(from[i]);
  270. good &= constant_time_is_zero_8(from[flen - SSL_MAX_MASTER_KEY_LENGTH - 1]);
  271. /*
  272. * If the version in the decrypted pre-master secret is correct then
  273. * version_good will be 0xff, otherwise it'll be zero. The
  274. * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  275. * (http://eprint.iacr.org/2003/052/) exploits the version number
  276. * check as a "bad version oracle". Thus version checks are done in
  277. * constant time and are treated like any other decryption error.
  278. */
  279. version_good =
  280. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
  281. (client_version >> 8) & 0xff);
  282. version_good &=
  283. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
  284. client_version & 0xff);
  285. /*
  286. * The premaster secret must contain the same version number as the
  287. * ClientHello to detect version rollback attacks (strangely, the
  288. * protocol does not offer such protection for DH ciphersuites).
  289. * However, buggy clients exist that send the negotiated protocol
  290. * version instead if the server does not support the requested
  291. * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set then we tolerate
  292. * such clients. In that case alt_version will be non-zero and set to
  293. * the negotiated version.
  294. */
  295. if (alt_version > 0) {
  296. unsigned int workaround_good;
  297. workaround_good =
  298. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
  299. (alt_version >> 8) & 0xff);
  300. workaround_good &=
  301. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
  302. alt_version & 0xff);
  303. version_good |= workaround_good;
  304. }
  305. good &= version_good;
  306. /*
  307. * Now copy the result over to the to buffer if good, or random data if
  308. * not good.
  309. */
  310. for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) {
  311. to[i] =
  312. constant_time_select_8(good,
  313. from[flen - SSL_MAX_MASTER_KEY_LENGTH + i],
  314. rand_premaster_secret[i]);
  315. }
  316. /*
  317. * We must not leak whether a decryption failure occurs because of
  318. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  319. * section 7.4.7.1). The code follows that advice of the TLS RFC and
  320. * generates a random premaster secret for the case that the decrypt
  321. * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  322. * So, whether we actually succeeded or not, return success.
  323. */
  324. return SSL_MAX_MASTER_KEY_LENGTH;
  325. }