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 ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
  109. int tlen, const unsigned char *from,
  110. int flen)
  111. {
  112. int i, j;
  113. unsigned char *p;
  114. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  115. RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  116. return 0;
  117. }
  118. p = (unsigned char *)to;
  119. *(p++) = 0;
  120. *(p++) = 2; /* Public Key BT (Block Type) */
  121. /* pad out with non-zero random data */
  122. j = tlen - 3 - flen;
  123. if (RAND_bytes_ex(libctx, p, j) <= 0)
  124. return 0;
  125. for (i = 0; i < j; i++) {
  126. if (*p == '\0')
  127. do {
  128. if (RAND_bytes_ex(libctx, p, 1) <= 0)
  129. return 0;
  130. } while (*p == '\0');
  131. p++;
  132. }
  133. *(p++) = '\0';
  134. memcpy(p, from, (unsigned int)flen);
  135. return 1;
  136. }
  137. int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
  138. const unsigned char *from, int flen)
  139. {
  140. return ossl_rsa_padding_add_PKCS1_type_2_ex(NULL, to, tlen, from, flen);
  141. }
  142. int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
  143. const unsigned char *from, int flen,
  144. int num)
  145. {
  146. int i;
  147. /* |em| is the encoded message, zero-padded to exactly |num| bytes */
  148. unsigned char *em = NULL;
  149. unsigned int good, found_zero_byte, mask;
  150. int zero_index = 0, msg_index, mlen = -1;
  151. if (tlen <= 0 || flen <= 0)
  152. return -1;
  153. /*
  154. * PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography Standard",
  155. * section 7.2.2.
  156. */
  157. if (flen > num || num < RSA_PKCS1_PADDING_SIZE) {
  158. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
  159. RSA_R_PKCS_DECODING_ERROR);
  160. return -1;
  161. }
  162. em = OPENSSL_malloc(num);
  163. if (em == NULL) {
  164. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, ERR_R_MALLOC_FAILURE);
  165. return -1;
  166. }
  167. /*
  168. * Caller is encouraged to pass zero-padded message created with
  169. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  170. * bounds, it's impossible to have an invariant memory access pattern
  171. * in case |from| was not zero-padded in advance.
  172. */
  173. for (from += flen, em += num, i = 0; i < num; i++) {
  174. mask = ~constant_time_is_zero(flen);
  175. flen -= 1 & mask;
  176. from -= 1 & mask;
  177. *--em = *from & mask;
  178. }
  179. good = constant_time_is_zero(em[0]);
  180. good &= constant_time_eq(em[1], 2);
  181. /* scan over padding data */
  182. found_zero_byte = 0;
  183. for (i = 2; i < num; i++) {
  184. unsigned int equals0 = constant_time_is_zero(em[i]);
  185. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  186. i, zero_index);
  187. found_zero_byte |= equals0;
  188. }
  189. /*
  190. * PS must be at least 8 bytes long, and it starts two bytes into |em|.
  191. * If we never found a 0-byte, then |zero_index| is 0 and the check
  192. * also fails.
  193. */
  194. good &= constant_time_ge(zero_index, 2 + 8);
  195. /*
  196. * Skip the zero byte. This is incorrect if we never found a zero-byte
  197. * but in this case we also do not copy the message out.
  198. */
  199. msg_index = zero_index + 1;
  200. mlen = num - msg_index;
  201. /*
  202. * For good measure, do this check in constant time as well.
  203. */
  204. good &= constant_time_ge(tlen, mlen);
  205. /*
  206. * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
  207. * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
  208. * Otherwise leave |to| unchanged.
  209. * Copy the memory back in a way that does not reveal the size of
  210. * the data being copied via a timing side channel. This requires copying
  211. * parts of the buffer multiple times based on the bits set in the real
  212. * length. Clear bits do a non-copy with identical access pattern.
  213. * The loop below has overall complexity of O(N*log(N)).
  214. */
  215. tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
  216. num - RSA_PKCS1_PADDING_SIZE, tlen);
  217. for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
  218. mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
  219. for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
  220. em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
  221. }
  222. for (i = 0; i < tlen; i++) {
  223. mask = good & constant_time_lt(i, mlen);
  224. to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
  225. }
  226. OPENSSL_clear_free(em, num);
  227. #ifndef FIPS_MODULE
  228. /*
  229. * This trick doesn't work in the FIPS provider because libcrypto manages
  230. * the error stack. Instead we opt not to put an error on the stack at all
  231. * in case of padding failure in the FIPS provider.
  232. */
  233. RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_PKCS_DECODING_ERROR);
  234. err_clear_last_constant_time(1 & good);
  235. #endif
  236. return constant_time_select_int(good, mlen, -1);
  237. }
  238. /*
  239. * ossl_rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
  240. * padding from a decrypted RSA message in a TLS signature. The result is stored
  241. * in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
  242. * must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
  243. * should be stored in |from| which must be |flen| bytes in length and padded
  244. * such that |flen == RSA_size()|. The TLS protocol version that the client
  245. * originally requested should be passed in |client_version|. Some buggy clients
  246. * can exist which use the negotiated version instead of the originally
  247. * requested protocol version. If it is necessary to work around this bug then
  248. * the negotiated protocol version can be passed in |alt_version|, otherwise 0
  249. * should be passed.
  250. *
  251. * If the passed message is publicly invalid or some other error that can be
  252. * treated in non-constant time occurs then -1 is returned. On success the
  253. * length of the decrypted data is returned. This will always be
  254. * SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
  255. * constant time then this function will appear to return successfully, but the
  256. * decrypted data will be randomly generated (as per
  257. * https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
  258. */
  259. int ossl_rsa_padding_check_PKCS1_type_2_TLS(OSSL_LIB_CTX *libctx,
  260. unsigned char *to, size_t tlen,
  261. 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. }