You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
12 KiB

  1. --- a/sources/applications/applestreamingclient/include/protocols/aes/inboundaesprotocol.h
  2. +++ b/sources/applications/applestreamingclient/include/protocols/aes/inboundaesprotocol.h
  3. @@ -30,7 +30,7 @@ namespace app_applestreamingclient {
  4. private:
  5. IOBuffer _tempBuffer;
  6. IOBuffer _inputBuffer;
  7. - EVP_CIPHER_CTX _decContex;
  8. + EVP_CIPHER_CTX *_decContex;
  9. bool _lastChunk;
  10. uint8_t *_pIV;
  11. uint8_t *_pKey;
  12. --- a/sources/applications/applestreamingclient/src/protocols/aes/inboundaesprotocol.cpp
  13. +++ b/sources/applications/applestreamingclient/src/protocols/aes/inboundaesprotocol.cpp
  14. @@ -31,13 +31,12 @@ InboundAESProtocol::InboundAESProtocol()
  15. memset(_pIV, 0, 16);
  16. _pKey = new uint8_t[16];
  17. memset(_pKey, 0, 16);
  18. - memset(&_decContex, 0, sizeof (EVP_CIPHER_CTX));
  19. + _decContex = EVP_CIPHER_CTX_new();
  20. _totalDecrypted = 0;
  21. }
  22. InboundAESProtocol::~InboundAESProtocol() {
  23. - EVP_CIPHER_CTX_cleanup(&_decContex);
  24. - memset(&_decContex, 0, sizeof (EVP_CIPHER_CTX));
  25. + EVP_CIPHER_CTX_free(_decContex);
  26. delete[] _pIV;
  27. delete[] _pKey;
  28. }
  29. @@ -60,11 +59,9 @@ bool InboundAESProtocol::Initialize(Vari
  30. _inputBuffer.IgnoreAll();
  31. _tempBuffer.IgnoreAll();
  32. - EVP_CIPHER_CTX_cleanup(&_decContex);
  33. - memset(&_decContex, 0, sizeof (EVP_CIPHER_CTX));
  34. - EVP_CIPHER_CTX_init(&_decContex);
  35. - EVP_DecryptInit_ex(&_decContex, EVP_aes_128_cbc(), NULL, _pKey, _pIV);
  36. - EVP_CIPHER_CTX_set_padding(&_decContex, 0);
  37. + EVP_CIPHER_CTX_reset(_decContex);
  38. + EVP_DecryptInit_ex(_decContex, EVP_aes_128_cbc(), NULL, _pKey, _pIV);
  39. + EVP_CIPHER_CTX_set_padding(_decContex, 0);
  40. return true;
  41. }
  42. @@ -105,14 +102,14 @@ bool InboundAESProtocol::SignalInputData
  43. int decryptedFinalSize = 0;
  44. uint32_t padding = 0;
  45. - EVP_DecryptUpdate(&_decContex, pTempData, &decryptedSize, GETIBPOINTER(buffer), safeSize);
  46. + EVP_DecryptUpdate(_decContex, pTempData, &decryptedSize, GETIBPOINTER(buffer), safeSize);
  47. _totalDecrypted += decryptedSize;
  48. //6. Decrypt leftovers
  49. bool transferCompleted = false;
  50. if (((HTTPBufferProtocol *) GetFarProtocol())->TransferCompleted()) {
  51. transferCompleted = true;
  52. - EVP_DecryptFinal_ex(&_decContex,
  53. + EVP_DecryptFinal_ex(_decContex,
  54. pTempData + decryptedSize,
  55. &decryptedFinalSize);
  56. _totalDecrypted += decryptedFinalSize;
  57. --- a/sources/common/include/utils/misc/crypto.h
  58. +++ b/sources/common/include/utils/misc/crypto.h
  59. @@ -33,6 +33,7 @@
  60. #include <openssl/aes.h>
  61. #include <openssl/engine.h>
  62. #include <openssl/conf.h>
  63. +#include "utils/misc/libcrypto-compat.h"
  64. /*!
  65. @class DHWrapper
  66. @@ -83,7 +84,7 @@ public:
  67. bool CopySharedKey(uint8_t *pDst, int32_t dstLength);
  68. private:
  69. void Cleanup();
  70. - bool CopyKey(BIGNUM *pNum, uint8_t *pDst, int32_t dstLength);
  71. + bool CopyKey(const BIGNUM *pNum, uint8_t *pDst, int32_t dstLength);
  72. };
  73. DLLEXP void InitRC4Encryption(uint8_t *secretKey, uint8_t *pubKeyIn, uint8_t *pubKeyOut,
  74. --- a/sources/common/src/utils/misc/crypto.cpp
  75. +++ b/sources/common/src/utils/misc/crypto.cpp
  76. @@ -35,6 +35,7 @@ DHWrapper::~DHWrapper() {
  77. }
  78. bool DHWrapper::Initialize() {
  79. + BIGNUM *p = NULL, *g = NULL;
  80. Cleanup();
  81. //1. Create the DH
  82. @@ -46,42 +47,53 @@ bool DHWrapper::Initialize() {
  83. }
  84. //2. Create his internal p and g
  85. - _pDH->p = BN_new();
  86. - if (_pDH->p == NULL) {
  87. + p = BN_new();
  88. + if (p == NULL) {
  89. FATAL("Unable to create p");
  90. - Cleanup();
  91. - return false;
  92. + goto return_error;
  93. }
  94. - _pDH->g = BN_new();
  95. - if (_pDH->g == NULL) {
  96. + g = BN_new();
  97. + if (g == NULL) {
  98. FATAL("Unable to create g");
  99. - Cleanup();
  100. - return false;
  101. + goto return_error;
  102. }
  103. //3. initialize p, g and key length
  104. - if (BN_hex2bn(&_pDH->p, P1024) == 0) {
  105. + if (BN_hex2bn(&p, P1024) == 0) {
  106. FATAL("Unable to parse P1024");
  107. - Cleanup();
  108. - return false;
  109. + goto return_error;
  110. }
  111. - if (BN_set_word(_pDH->g, 2) != 1) {
  112. + if (BN_set_word(g, 2) != 1) {
  113. FATAL("Unable to set g");
  114. - Cleanup();
  115. - return false;
  116. + goto return_error;
  117. }
  118. - //4. Set the key length
  119. - _pDH->length = _bitsCount;
  120. + //4. Set internal p and g
  121. + if (DH_set0_pqg(_pDH, p, NULL, g) != 1) {
  122. + FATAL("Unable to set internal p and g");
  123. + goto return_error;
  124. + }
  125. + p = g = NULL;
  126. - //5. Generate private and public key
  127. + //5. Set the key length
  128. + if (DH_set_length(_pDH, _bitsCount) != 1) {
  129. + FATAL("Unable to set length");
  130. + goto return_error;
  131. + }
  132. +
  133. + //6. Generate private and public key
  134. if (DH_generate_key(_pDH) != 1) {
  135. FATAL("Unable to generate DH public/private keys");
  136. - Cleanup();
  137. - return false;
  138. + goto return_error;
  139. }
  140. return true;
  141. +
  142. +return_error:
  143. + if (p != NULL) BN_free(p);
  144. + if (g != NULL) BN_free(g);
  145. + Cleanup();
  146. + return false;
  147. }
  148. bool DHWrapper::CopyPublicKey(uint8_t *pDst, int32_t dstLength) {
  149. @@ -90,7 +102,9 @@ bool DHWrapper::CopyPublicKey(uint8_t *p
  150. return false;
  151. }
  152. - return CopyKey(_pDH->pub_key, pDst, dstLength);
  153. + const BIGNUM *pub_key;
  154. + DH_get0_key(_pDH, &pub_key, NULL);
  155. + return CopyKey(pub_key, pDst, dstLength);
  156. }
  157. bool DHWrapper::CopyPrivateKey(uint8_t *pDst, int32_t dstLength) {
  158. @@ -99,7 +113,9 @@ bool DHWrapper::CopyPrivateKey(uint8_t *
  159. return false;
  160. }
  161. - return CopyKey(_pDH->priv_key, pDst, dstLength);
  162. + const BIGNUM *priv_key;
  163. + DH_get0_key(_pDH, NULL, &priv_key);
  164. + return CopyKey(priv_key, pDst, dstLength);
  165. }
  166. bool DHWrapper::CreateSharedKey(uint8_t *pPeerPublicKey, int32_t length) {
  167. @@ -153,14 +169,6 @@ bool DHWrapper::CopySharedKey(uint8_t *p
  168. void DHWrapper::Cleanup() {
  169. if (_pDH != NULL) {
  170. - if (_pDH->p != NULL) {
  171. - BN_free(_pDH->p);
  172. - _pDH->p = NULL;
  173. - }
  174. - if (_pDH->g != NULL) {
  175. - BN_free(_pDH->g);
  176. - _pDH->g = NULL;
  177. - }
  178. DH_free(_pDH);
  179. _pDH = NULL;
  180. }
  181. @@ -177,7 +185,7 @@ void DHWrapper::Cleanup() {
  182. }
  183. }
  184. -bool DHWrapper::CopyKey(BIGNUM *pNum, uint8_t *pDst, int32_t dstLength) {
  185. +bool DHWrapper::CopyKey(const BIGNUM *pNum, uint8_t *pDst, int32_t dstLength) {
  186. int32_t keySize = BN_num_bytes(pNum);
  187. if ((keySize <= 0) || (dstLength <= 0) || (keySize > dstLength)) {
  188. FATAL("CopyPublicKey failed due to either invalid DH state or invalid call");
  189. @@ -197,20 +205,21 @@ void InitRC4Encryption(uint8_t *secretKe
  190. uint8_t digest[SHA256_DIGEST_LENGTH];
  191. unsigned int digestLen = 0;
  192. - HMAC_CTX ctx;
  193. - HMAC_CTX_init(&ctx);
  194. - HMAC_Init_ex(&ctx, secretKey, 128, EVP_sha256(), 0);
  195. - HMAC_Update(&ctx, pubKeyIn, 128);
  196. - HMAC_Final(&ctx, digest, &digestLen);
  197. - HMAC_CTX_cleanup(&ctx);
  198. + HMAC_CTX *ctx;
  199. + ctx = HMAC_CTX_new();
  200. + if (ctx == NULL)
  201. + return;
  202. + HMAC_Init_ex(ctx, secretKey, 128, EVP_sha256(), 0);
  203. + HMAC_Update(ctx, pubKeyIn, 128);
  204. + HMAC_Final(ctx, digest, &digestLen);
  205. + HMAC_CTX_reset(ctx);
  206. RC4_set_key(rc4keyOut, 16, digest);
  207. - HMAC_CTX_init(&ctx);
  208. - HMAC_Init_ex(&ctx, secretKey, 128, EVP_sha256(), 0);
  209. - HMAC_Update(&ctx, pubKeyOut, 128);
  210. - HMAC_Final(&ctx, digest, &digestLen);
  211. - HMAC_CTX_cleanup(&ctx);
  212. + HMAC_Init_ex(ctx, secretKey, 128, EVP_sha256(), 0);
  213. + HMAC_Update(ctx, pubKeyOut, 128);
  214. + HMAC_Final(ctx, digest, &digestLen);
  215. + HMAC_CTX_free(ctx);
  216. RC4_set_key(rc4keyIn, 16, digest);
  217. }
  218. @@ -220,14 +229,17 @@ string md5(string source, bool textResul
  219. }
  220. string md5(uint8_t *pBuffer, uint32_t length, bool textResult) {
  221. - EVP_MD_CTX mdctx;
  222. + EVP_MD_CTX *mdctx;
  223. unsigned char md_value[EVP_MAX_MD_SIZE];
  224. unsigned int md_len;
  225. - EVP_DigestInit(&mdctx, EVP_md5());
  226. - EVP_DigestUpdate(&mdctx, pBuffer, length);
  227. - EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
  228. - EVP_MD_CTX_cleanup(&mdctx);
  229. + mdctx = EVP_MD_CTX_new();
  230. + if (mdctx == NULL)
  231. + return "";
  232. + EVP_DigestInit(mdctx, EVP_md5());
  233. + EVP_DigestUpdate(mdctx, pBuffer, length);
  234. + EVP_DigestFinal_ex(mdctx, md_value, &md_len);
  235. + EVP_MD_CTX_free(mdctx);
  236. if (textResult) {
  237. string result = "";
  238. @@ -244,12 +256,12 @@ void HMACsha256(const void *pData, uint3
  239. const void *pKey, uint32_t keyLength, void *pResult) {
  240. unsigned int digestLen;
  241. - HMAC_CTX ctx;
  242. - HMAC_CTX_init(&ctx);
  243. - HMAC_Init_ex(&ctx, (unsigned char*) pKey, keyLength, EVP_sha256(), NULL);
  244. - HMAC_Update(&ctx, (unsigned char *) pData, dataLength);
  245. - HMAC_Final(&ctx, (unsigned char *) pResult, &digestLen);
  246. - HMAC_CTX_cleanup(&ctx);
  247. + HMAC_CTX *ctx;
  248. + ctx = HMAC_CTX_new();
  249. + HMAC_Init_ex(ctx, (unsigned char*) pKey, keyLength, EVP_sha256(), NULL);
  250. + HMAC_Update(ctx, (unsigned char *) pData, dataLength);
  251. + HMAC_Final(ctx, (unsigned char *) pResult, &digestLen);
  252. + HMAC_CTX_free(ctx);
  253. o_assert(digestLen == 32);
  254. }
  255. --- a/sources/thelib/src/protocols/ssl/basesslprotocol.cpp
  256. +++ b/sources/thelib/src/protocols/ssl/basesslprotocol.cpp
  257. @@ -211,6 +211,7 @@ string BaseSSLProtocol::GetSSLErrors() {
  258. string BaseSSLProtocol::DumpBIO(BIO *pBIO) {
  259. string formatString;
  260. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  261. formatString = "method: %p\n";
  262. formatString += "callback: %p\n";
  263. formatString += "cb_arg: %p\n";
  264. @@ -240,6 +241,39 @@ string BaseSSLProtocol::DumpBIO(BIO *pBI
  265. pBIO->references,
  266. (int64_t) pBIO->num_read,
  267. (int64_t) pBIO->num_write);
  268. +#else
  269. +// Some of these are problematic in openssl >= 1.1, since
  270. +// the BIO struct is opaque.
  271. + formatString = "method: %s\n";
  272. + formatString += "callback: %p\n";
  273. + formatString += "cb_arg: %p\n";
  274. + formatString += "init: %d\n";
  275. + formatString += "shutdown: %d\n";
  276. + formatString += "flags: %d\n";
  277. + formatString += "retry_reason: %d\n";
  278. + formatString += "num: %d\n";
  279. + formatString += "ptr: %p\n";
  280. + formatString += "next_bio: %p\n";
  281. + formatString += "prev_bio: %s\n";
  282. + formatString += "references: %s\n";
  283. + formatString += "num_read: %"PRId64"\n";
  284. + formatString += "num_write: %"PRId64;
  285. + return format(formatString,
  286. + BIO_method_name(pBIO),
  287. + BIO_get_callback(pBIO),
  288. + BIO_get_callback_arg(pBIO),
  289. + BIO_get_init(pBIO),
  290. + BIO_get_shutdown(pBIO),
  291. + BIO_get_flags(pBIO),
  292. + BIO_get_retry_reason(pBIO),
  293. + BIO_get_fd(pBIO, NULL),
  294. + BIO_get_data(pBIO),
  295. + BIO_next(pBIO),
  296. + "unknown", //prev_bio
  297. + "unknown", //references
  298. + BIO_number_read(pBIO),
  299. + BIO_number_written(pBIO));
  300. +#endif
  301. }
  302. void BaseSSLProtocol::InitRandGenerator() {
  303. --- /dev/null
  304. +++ b/sources/common/include/utils/misc/libcrypto-compat.h
  305. @@ -0,0 +1,25 @@
  306. +#ifndef LIBCRYPTO_COMPAT_H
  307. +#define LIBCRYPTO_COMPAT_H
  308. +
  309. +#include <openssl/opensslv.h>
  310. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  311. +
  312. +#include <openssl/dh.h>
  313. +#include <openssl/evp.h>
  314. +#include <openssl/hmac.h>
  315. +
  316. +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  317. +void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
  318. +int DH_set_length(DH *dh, long length);
  319. +
  320. +EVP_MD_CTX *EVP_MD_CTX_new(void);
  321. +void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  322. +#define EVP_MD_CTX_reset EVP_MD_CTX_cleanup
  323. +
  324. +HMAC_CTX *HMAC_CTX_new(void);
  325. +void HMAC_CTX_free(HMAC_CTX *ctx);
  326. +#define HMAC_CTX_reset HMAC_CTX_cleanup
  327. +
  328. +#endif /* OPENSSL_VERSION_NUMBER */
  329. +
  330. +#endif /* LIBCRYPTO_COMPAT_H */
  331. --- /dev/null
  332. +++ b/sources/common/src/utils/misc/libcrypto-compat.cpp
  333. @@ -0,0 +1,90 @@
  334. +/*
  335. + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  336. + *
  337. + * Licensed under the OpenSSL license (the "License"). You may not use
  338. + * this file except in compliance with the License. You can obtain a copy
  339. + * in the file LICENSE in the source distribution or at
  340. + * https://www.openssl.org/source/license.html
  341. + */
  342. +
  343. +#include "utils/misc/libcrypto-compat.h"
  344. +
  345. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  346. +
  347. +#include <string.h>
  348. +
  349. +static void *OPENSSL_zalloc(size_t num)
  350. +{
  351. + void *ret = OPENSSL_malloc(num);
  352. +
  353. + if (ret != NULL)
  354. + memset(ret, 0, num);
  355. + return ret;
  356. +}
  357. +
  358. +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  359. +{
  360. + /* If the fields p and g in d are NULL, the corresponding input
  361. + * parameters MUST be non-NULL. q may remain NULL.
  362. + */
  363. + if ((dh->p == NULL && p == NULL)
  364. + || (dh->g == NULL && g == NULL))
  365. + return 0;
  366. +
  367. + if (p != NULL) {
  368. + BN_free(dh->p);
  369. + dh->p = p;
  370. + }
  371. + if (q != NULL) {
  372. + BN_free(dh->q);
  373. + dh->q = q;
  374. + }
  375. + if (g != NULL) {
  376. + BN_free(dh->g);
  377. + dh->g = g;
  378. + }
  379. +
  380. + if (q != NULL) {
  381. + dh->length = BN_num_bits(q);
  382. + }
  383. +
  384. + return 1;
  385. +}
  386. +
  387. +void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  388. +{
  389. + if (pub_key != NULL)
  390. + *pub_key = dh->pub_key;
  391. + if (priv_key != NULL)
  392. + *priv_key = dh->priv_key;
  393. +}
  394. +
  395. +int DH_set_length(DH *dh, long length)
  396. +{
  397. + dh->length = length;
  398. + return 1;
  399. +}
  400. +
  401. +EVP_MD_CTX *EVP_MD_CTX_new(void)
  402. +{
  403. + return (EVP_MD_CTX *)OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  404. +}
  405. +
  406. +void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  407. +{
  408. + EVP_MD_CTX_cleanup(ctx);
  409. + OPENSSL_free(ctx);
  410. +}
  411. +
  412. +HMAC_CTX *HMAC_CTX_new(void)
  413. +{
  414. + return (HMAC_CTX *)OPENSSL_zalloc(sizeof(HMAC_CTX));
  415. +}
  416. +
  417. +void HMAC_CTX_free(HMAC_CTX *ctx)
  418. +{
  419. + HMAC_CTX_cleanup(ctx);
  420. + OPENSSL_free(ctx);
  421. +}
  422. +
  423. +#endif /* OPENSSL_VERSION_NUMBER */