2
0

rsa_pk1.c 13 KB

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