rsa_pk1.c 13 KB

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