rsa_pk1.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright 1995-2023 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 <openssl/evp.h>
  22. #include <openssl/sha.h>
  23. #include <openssl/hmac.h>
  24. #include "internal/cryptlib.h"
  25. #include "crypto/rsa.h"
  26. #include "rsa_local.h"
  27. int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
  28. const unsigned char *from, int flen)
  29. {
  30. int j;
  31. unsigned char *p;
  32. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  33. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  34. return 0;
  35. }
  36. p = (unsigned char *)to;
  37. *(p++) = 0;
  38. *(p++) = 1; /* Private Key BT (Block Type) */
  39. /* pad out with 0xff data */
  40. j = tlen - 3 - flen;
  41. memset(p, 0xff, j);
  42. p += j;
  43. *(p++) = '\0';
  44. memcpy(p, from, (unsigned int)flen);
  45. return 1;
  46. }
  47. int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
  48. const unsigned char *from, int flen,
  49. int num)
  50. {
  51. int i, j;
  52. const unsigned char *p;
  53. p = from;
  54. /*
  55. * The format is
  56. * 00 || 01 || PS || 00 || D
  57. * PS - padding string, at least 8 bytes of FF
  58. * D - data.
  59. */
  60. if (num < RSA_PKCS1_PADDING_SIZE)
  61. return -1;
  62. /* Accept inputs with and without the leading 0-byte. */
  63. if (num == flen) {
  64. if ((*p++) != 0x00) {
  65. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING);
  66. return -1;
  67. }
  68. flen--;
  69. }
  70. if ((num != (flen + 1)) || (*(p++) != 0x01)) {
  71. ERR_raise(ERR_LIB_RSA, 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. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT);
  83. return -1;
  84. }
  85. }
  86. p++;
  87. }
  88. if (i == j) {
  89. ERR_raise(ERR_LIB_RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING);
  90. return -1;
  91. }
  92. if (i < 8) {
  93. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_PAD_BYTE_COUNT);
  94. return -1;
  95. }
  96. i++; /* Skip over the '\0' */
  97. j -= i;
  98. if (j > tlen) {
  99. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
  100. return -1;
  101. }
  102. memcpy(to, p, (unsigned int)j);
  103. return j;
  104. }
  105. int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
  106. int tlen, const unsigned char *from,
  107. int flen)
  108. {
  109. int i, j;
  110. unsigned char *p;
  111. if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
  112. ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  113. return 0;
  114. } else if (flen < 0) {
  115. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH);
  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) <= 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) <= 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. ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
  159. return -1;
  160. }
  161. em = OPENSSL_malloc(num);
  162. if (em == NULL)
  163. return -1;
  164. /*
  165. * Caller is encouraged to pass zero-padded message created with
  166. * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
  167. * bounds, it's impossible to have an invariant memory access pattern
  168. * in case |from| was not zero-padded in advance.
  169. */
  170. for (from += flen, em += num, i = 0; i < num; i++) {
  171. mask = ~constant_time_is_zero(flen);
  172. flen -= 1 & mask;
  173. from -= 1 & mask;
  174. *--em = *from & mask;
  175. }
  176. good = constant_time_is_zero(em[0]);
  177. good &= constant_time_eq(em[1], 2);
  178. /* scan over padding data */
  179. found_zero_byte = 0;
  180. for (i = 2; i < num; i++) {
  181. unsigned int equals0 = constant_time_is_zero(em[i]);
  182. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  183. i, zero_index);
  184. found_zero_byte |= equals0;
  185. }
  186. /*
  187. * PS must be at least 8 bytes long, and it starts two bytes into |em|.
  188. * If we never found a 0-byte, then |zero_index| is 0 and the check
  189. * also fails.
  190. */
  191. good &= constant_time_ge(zero_index, 2 + 8);
  192. /*
  193. * Skip the zero byte. This is incorrect if we never found a zero-byte
  194. * but in this case we also do not copy the message out.
  195. */
  196. msg_index = zero_index + 1;
  197. mlen = num - msg_index;
  198. /*
  199. * For good measure, do this check in constant time as well.
  200. */
  201. good &= constant_time_ge(tlen, mlen);
  202. /*
  203. * Move the result in-place by |num|-RSA_PKCS1_PADDING_SIZE-|mlen| bytes to the left.
  204. * Then if |good| move |mlen| bytes from |em|+RSA_PKCS1_PADDING_SIZE to |to|.
  205. * Otherwise leave |to| unchanged.
  206. * Copy the memory back in a way that does not reveal the size of
  207. * the data being copied via a timing side channel. This requires copying
  208. * parts of the buffer multiple times based on the bits set in the real
  209. * length. Clear bits do a non-copy with identical access pattern.
  210. * The loop below has overall complexity of O(N*log(N)).
  211. */
  212. tlen = constant_time_select_int(constant_time_lt(num - RSA_PKCS1_PADDING_SIZE, tlen),
  213. num - RSA_PKCS1_PADDING_SIZE, tlen);
  214. for (msg_index = 1; msg_index < num - RSA_PKCS1_PADDING_SIZE; msg_index <<= 1) {
  215. mask = ~constant_time_eq(msg_index & (num - RSA_PKCS1_PADDING_SIZE - mlen), 0);
  216. for (i = RSA_PKCS1_PADDING_SIZE; i < num - msg_index; i++)
  217. em[i] = constant_time_select_8(mask, em[i + msg_index], em[i]);
  218. }
  219. for (i = 0; i < tlen; i++) {
  220. mask = good & constant_time_lt(i, mlen);
  221. to[i] = constant_time_select_8(mask, em[i + RSA_PKCS1_PADDING_SIZE], to[i]);
  222. }
  223. OPENSSL_clear_free(em, num);
  224. #ifndef FIPS_MODULE
  225. /*
  226. * This trick doesn't work in the FIPS provider because libcrypto manages
  227. * the error stack. Instead we opt not to put an error on the stack at all
  228. * in case of padding failure in the FIPS provider.
  229. */
  230. ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
  231. err_clear_last_constant_time(1 & good);
  232. #endif
  233. return constant_time_select_int(good, mlen, -1);
  234. }
  235. static int ossl_rsa_prf(OSSL_LIB_CTX *ctx,
  236. unsigned char *to, int tlen,
  237. const char *label, int llen,
  238. const unsigned char *kdk,
  239. uint16_t bitlen)
  240. {
  241. int pos;
  242. int ret = -1;
  243. uint16_t iter = 0;
  244. unsigned char be_iter[sizeof(iter)];
  245. unsigned char be_bitlen[sizeof(bitlen)];
  246. HMAC_CTX *hmac = NULL;
  247. EVP_MD *md = NULL;
  248. unsigned char hmac_out[SHA256_DIGEST_LENGTH];
  249. unsigned int md_len;
  250. if (tlen * 8 != bitlen) {
  251. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  252. return ret;
  253. }
  254. be_bitlen[0] = (bitlen >> 8) & 0xff;
  255. be_bitlen[1] = bitlen & 0xff;
  256. hmac = HMAC_CTX_new();
  257. if (hmac == NULL) {
  258. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  259. goto err;
  260. }
  261. /*
  262. * we use hardcoded hash so that migrating between versions that use
  263. * different hash doesn't provide a Bleichenbacher oracle:
  264. * if the attacker can see that different versions return different
  265. * messages for the same ciphertext, they'll know that the message is
  266. * synthetically generated, which means that the padding check failed
  267. */
  268. md = EVP_MD_fetch(ctx, "sha256", NULL);
  269. if (md == NULL) {
  270. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  271. goto err;
  272. }
  273. if (HMAC_Init_ex(hmac, kdk, SHA256_DIGEST_LENGTH, md, NULL) <= 0) {
  274. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  275. goto err;
  276. }
  277. for (pos = 0; pos < tlen; pos += SHA256_DIGEST_LENGTH, iter++) {
  278. if (HMAC_Init_ex(hmac, NULL, 0, NULL, NULL) <= 0) {
  279. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  280. goto err;
  281. }
  282. be_iter[0] = (iter >> 8) & 0xff;
  283. be_iter[1] = iter & 0xff;
  284. if (HMAC_Update(hmac, be_iter, sizeof(be_iter)) <= 0) {
  285. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  286. goto err;
  287. }
  288. if (HMAC_Update(hmac, (unsigned char *)label, llen) <= 0) {
  289. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  290. goto err;
  291. }
  292. if (HMAC_Update(hmac, be_bitlen, sizeof(be_bitlen)) <= 0) {
  293. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  294. goto err;
  295. }
  296. /*
  297. * HMAC_Final requires the output buffer to fit the whole MAC
  298. * value, so we need to use the intermediate buffer for the last
  299. * unaligned block
  300. */
  301. md_len = SHA256_DIGEST_LENGTH;
  302. if (pos + SHA256_DIGEST_LENGTH > tlen) {
  303. if (HMAC_Final(hmac, hmac_out, &md_len) <= 0) {
  304. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  305. goto err;
  306. }
  307. memcpy(to + pos, hmac_out, tlen - pos);
  308. } else {
  309. if (HMAC_Final(hmac, to + pos, &md_len) <= 0) {
  310. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  311. goto err;
  312. }
  313. }
  314. }
  315. ret = 0;
  316. err:
  317. HMAC_CTX_free(hmac);
  318. EVP_MD_free(md);
  319. return ret;
  320. }
  321. /*
  322. * ossl_rsa_padding_check_PKCS1_type_2() checks and removes the PKCS#1 type 2
  323. * padding from a decrypted RSA message. Unlike the
  324. * RSA_padding_check_PKCS1_type_2() it will not return an error in case it
  325. * detects a padding error, rather it will return a deterministically generated
  326. * random message. In other words it will perform an implicit rejection
  327. * of an invalid padding. This means that the returned value does not indicate
  328. * if the padding of the encrypted message was correct or not, making
  329. * side channel attacks like the ones described by Bleichenbacher impossible
  330. * without access to the full decrypted value and a brute-force search of
  331. * remaining padding bytes
  332. */
  333. int ossl_rsa_padding_check_PKCS1_type_2(OSSL_LIB_CTX *ctx,
  334. unsigned char *to, int tlen,
  335. const unsigned char *from, int flen,
  336. int num, unsigned char *kdk)
  337. {
  338. /*
  339. * We need to generate a random length for the synthetic message, to avoid
  340. * bias towards zero and avoid non-constant timeness of DIV, we prepare
  341. * 128 values to check if they are not too large for the used key size,
  342. * and use 0 in case none of them are small enough, as 2^-128 is a good enough
  343. * safety margin
  344. */
  345. #define MAX_LEN_GEN_TRIES 128
  346. unsigned char *synthetic = NULL;
  347. int synthetic_length;
  348. uint16_t len_candidate;
  349. unsigned char candidate_lengths[MAX_LEN_GEN_TRIES * sizeof(len_candidate)];
  350. uint16_t len_mask;
  351. uint16_t max_sep_offset;
  352. int synth_msg_index = 0;
  353. int ret = -1;
  354. int i, j;
  355. unsigned int good, found_zero_byte;
  356. int zero_index = 0, msg_index;
  357. /*
  358. * If these checks fail then either the message in publicly invalid, or
  359. * we've been called incorrectly. We can fail immediately.
  360. * Since this code is called only internally by openssl, those are just
  361. * sanity checks
  362. */
  363. if (num != flen || tlen <= 0 || flen <= 0) {
  364. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  365. return -1;
  366. }
  367. /* Generate a random message to return in case the padding checks fail */
  368. synthetic = OPENSSL_malloc(flen);
  369. if (synthetic == NULL) {
  370. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  371. return -1;
  372. }
  373. if (ossl_rsa_prf(ctx, synthetic, flen, "message", 7, kdk, flen * 8) < 0)
  374. goto err;
  375. /* decide how long the random message should be */
  376. if (ossl_rsa_prf(ctx, candidate_lengths, sizeof(candidate_lengths),
  377. "length", 6, kdk,
  378. MAX_LEN_GEN_TRIES * sizeof(len_candidate) * 8) < 0)
  379. goto err;
  380. /*
  381. * max message size is the size of the modulus size less 2 bytes for
  382. * version and padding type and a minimum of 8 bytes padding
  383. */
  384. len_mask = max_sep_offset = flen - 2 - 8;
  385. /*
  386. * we want a mask so lets propagate the high bit to all positions less
  387. * significant than it
  388. */
  389. len_mask |= len_mask >> 1;
  390. len_mask |= len_mask >> 2;
  391. len_mask |= len_mask >> 4;
  392. len_mask |= len_mask >> 8;
  393. synthetic_length = 0;
  394. for (i = 0; i < MAX_LEN_GEN_TRIES * (int)sizeof(len_candidate);
  395. i += sizeof(len_candidate)) {
  396. len_candidate = (candidate_lengths[i] << 8) | candidate_lengths[i + 1];
  397. len_candidate &= len_mask;
  398. synthetic_length = constant_time_select_int(
  399. constant_time_lt(len_candidate, max_sep_offset),
  400. len_candidate, synthetic_length);
  401. }
  402. synth_msg_index = flen - synthetic_length;
  403. /* we have alternative message ready, check the real one */
  404. good = constant_time_is_zero(from[0]);
  405. good &= constant_time_eq(from[1], 2);
  406. /* then look for the padding|message separator (the first zero byte) */
  407. found_zero_byte = 0;
  408. for (i = 2; i < flen; i++) {
  409. unsigned int equals0 = constant_time_is_zero(from[i]);
  410. zero_index = constant_time_select_int(~found_zero_byte & equals0,
  411. i, zero_index);
  412. found_zero_byte |= equals0;
  413. }
  414. /*
  415. * padding must be at least 8 bytes long, and it starts two bytes into
  416. * |from|. If we never found a 0-byte, then |zero_index| is 0 and the check
  417. * also fails.
  418. */
  419. good &= constant_time_ge(zero_index, 2 + 8);
  420. /*
  421. * Skip the zero byte. This is incorrect if we never found a zero-byte
  422. * but in this case we also do not copy the message out.
  423. */
  424. msg_index = zero_index + 1;
  425. /*
  426. * old code returned an error in case the decrypted message wouldn't fit
  427. * into the |to|, since that would leak information, return the synthetic
  428. * message instead
  429. */
  430. good &= constant_time_ge(tlen, num - msg_index);
  431. msg_index = constant_time_select_int(good, msg_index, synth_msg_index);
  432. /*
  433. * since at this point the |msg_index| does not provide the signal
  434. * indicating if the padding check failed or not, we don't have to worry
  435. * about leaking the length of returned message, we still need to ensure
  436. * that we read contents of both buffers so that cache accesses don't leak
  437. * the value of |good|
  438. */
  439. for (i = msg_index, j = 0; i < flen && j < tlen; i++, j++)
  440. to[j] = constant_time_select_8(good, from[i], synthetic[i]);
  441. ret = j;
  442. err:
  443. /*
  444. * the only time ret < 0 is when the ciphertext is publicly invalid
  445. * or we were called with invalid parameters, so we don't have to perform
  446. * a side-channel secure raising of the error
  447. */
  448. if (ret < 0)
  449. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  450. OPENSSL_free(synthetic);
  451. return ret;
  452. }
  453. /*
  454. * ossl_rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
  455. * padding from a decrypted RSA message in a TLS signature. The result is stored
  456. * in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
  457. * must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
  458. * should be stored in |from| which must be |flen| bytes in length and padded
  459. * such that |flen == RSA_size()|. The TLS protocol version that the client
  460. * originally requested should be passed in |client_version|. Some buggy clients
  461. * can exist which use the negotiated version instead of the originally
  462. * requested protocol version. If it is necessary to work around this bug then
  463. * the negotiated protocol version can be passed in |alt_version|, otherwise 0
  464. * should be passed.
  465. *
  466. * If the passed message is publicly invalid or some other error that can be
  467. * treated in non-constant time occurs then -1 is returned. On success the
  468. * length of the decrypted data is returned. This will always be
  469. * SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
  470. * constant time then this function will appear to return successfully, but the
  471. * decrypted data will be randomly generated (as per
  472. * https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
  473. */
  474. int ossl_rsa_padding_check_PKCS1_type_2_TLS(OSSL_LIB_CTX *libctx,
  475. unsigned char *to, size_t tlen,
  476. const unsigned char *from,
  477. size_t flen, int client_version,
  478. int alt_version)
  479. {
  480. unsigned int i, good, version_good;
  481. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  482. /*
  483. * If these checks fail then either the message in publicly invalid, or
  484. * we've been called incorrectly. We can fail immediately.
  485. */
  486. if (flen < RSA_PKCS1_PADDING_SIZE + SSL_MAX_MASTER_KEY_LENGTH
  487. || tlen < SSL_MAX_MASTER_KEY_LENGTH) {
  488. ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
  489. return -1;
  490. }
  491. /*
  492. * Generate a random premaster secret to use in the event that we fail
  493. * to decrypt.
  494. */
  495. if (RAND_priv_bytes_ex(libctx, rand_premaster_secret,
  496. sizeof(rand_premaster_secret), 0) <= 0) {
  497. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  498. return -1;
  499. }
  500. good = constant_time_is_zero(from[0]);
  501. good &= constant_time_eq(from[1], 2);
  502. /* Check we have the expected padding data */
  503. for (i = 2; i < flen - SSL_MAX_MASTER_KEY_LENGTH - 1; i++)
  504. good &= ~constant_time_is_zero_8(from[i]);
  505. good &= constant_time_is_zero_8(from[flen - SSL_MAX_MASTER_KEY_LENGTH - 1]);
  506. /*
  507. * If the version in the decrypted pre-master secret is correct then
  508. * version_good will be 0xff, otherwise it'll be zero. The
  509. * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  510. * (http://eprint.iacr.org/2003/052/) exploits the version number
  511. * check as a "bad version oracle". Thus version checks are done in
  512. * constant time and are treated like any other decryption error.
  513. */
  514. version_good =
  515. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
  516. (client_version >> 8) & 0xff);
  517. version_good &=
  518. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
  519. client_version & 0xff);
  520. /*
  521. * The premaster secret must contain the same version number as the
  522. * ClientHello to detect version rollback attacks (strangely, the
  523. * protocol does not offer such protection for DH ciphersuites).
  524. * However, buggy clients exist that send the negotiated protocol
  525. * version instead if the server does not support the requested
  526. * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set then we tolerate
  527. * such clients. In that case alt_version will be non-zero and set to
  528. * the negotiated version.
  529. */
  530. if (alt_version > 0) {
  531. unsigned int workaround_good;
  532. workaround_good =
  533. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
  534. (alt_version >> 8) & 0xff);
  535. workaround_good &=
  536. constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
  537. alt_version & 0xff);
  538. version_good |= workaround_good;
  539. }
  540. good &= version_good;
  541. /*
  542. * Now copy the result over to the to buffer if good, or random data if
  543. * not good.
  544. */
  545. for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) {
  546. to[i] =
  547. constant_time_select_8(good,
  548. from[flen - SSL_MAX_MASTER_KEY_LENGTH + i],
  549. rand_premaster_secret[i]);
  550. }
  551. /*
  552. * We must not leak whether a decryption failure occurs because of
  553. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  554. * section 7.4.7.1). The code follows that advice of the TLS RFC and
  555. * generates a random premaster secret for the case that the decrypt
  556. * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  557. * So, whether we actually succeeded or not, return success.
  558. */
  559. return SSL_MAX_MASTER_KEY_LENGTH;
  560. }