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.

1159 lines
34 KiB

  1. --- a/src/libcrypto.c
  2. +++ b/src/libcrypto.c
  3. @@ -43,10 +43,12 @@
  4. #include <openssl/hmac.h>
  5. #include <openssl/opensslv.h>
  6. #include <openssl/rand.h>
  7. +#include "libcrypto-compat.h"
  8. #ifdef HAVE_OPENSSL_AES_H
  9. #define HAS_AES
  10. #include <openssl/aes.h>
  11. +#include <openssl/modes.h>
  12. #endif
  13. #ifdef HAVE_OPENSSL_BLOWFISH_H
  14. #define HAS_BLOWFISH
  15. @@ -133,18 +135,20 @@ static const EVP_MD *nid_to_evpmd(int ni
  16. void evp(int nid, unsigned char *digest, int len, unsigned char *hash, unsigned int *hlen)
  17. {
  18. const EVP_MD *evp_md = nid_to_evpmd(nid);
  19. - EVP_MD_CTX md;
  20. + EVP_MD_CTX *md;
  21. - EVP_DigestInit(&md, evp_md);
  22. - EVP_DigestUpdate(&md, digest, len);
  23. - EVP_DigestFinal(&md, hash, hlen);
  24. + md = EVP_MD_CTX_new();
  25. + EVP_DigestInit(md, evp_md);
  26. + EVP_DigestUpdate(md, digest, len);
  27. + EVP_DigestFinal(md, hash, hlen);
  28. + EVP_MD_CTX_free(md);
  29. }
  30. EVPCTX evp_init(int nid)
  31. {
  32. const EVP_MD *evp_md = nid_to_evpmd(nid);
  33. - EVPCTX ctx = malloc(sizeof(EVP_MD_CTX));
  34. + EVPCTX ctx = EVP_MD_CTX_new();
  35. if (ctx == NULL) {
  36. return NULL;
  37. }
  38. @@ -322,32 +326,33 @@ void ssh_mac_final(unsigned char *md, ss
  39. HMACCTX hmac_init(const void *key, int len, enum ssh_hmac_e type) {
  40. HMACCTX ctx = NULL;
  41. - ctx = malloc(sizeof(*ctx));
  42. + ctx = HMAC_CTX_new();
  43. if (ctx == NULL) {
  44. return NULL;
  45. }
  46. #ifndef OLD_CRYPTO
  47. - HMAC_CTX_init(ctx); // openssl 0.9.7 requires it.
  48. + HMAC_CTX_reset(ctx); // openssl 0.9.7 requires it.
  49. #endif
  50. switch(type) {
  51. case SSH_HMAC_SHA1:
  52. - HMAC_Init(ctx, key, len, EVP_sha1());
  53. + HMAC_Init_ex(ctx, key, len, EVP_sha1(), NULL);
  54. break;
  55. case SSH_HMAC_SHA256:
  56. - HMAC_Init(ctx, key, len, EVP_sha256());
  57. + HMAC_Init_ex(ctx, key, len, EVP_sha256(), NULL);
  58. break;
  59. case SSH_HMAC_SHA384:
  60. - HMAC_Init(ctx, key, len, EVP_sha384());
  61. + HMAC_Init_ex(ctx, key, len, EVP_sha384(), NULL);
  62. break;
  63. case SSH_HMAC_SHA512:
  64. - HMAC_Init(ctx, key, len, EVP_sha512());
  65. + HMAC_Init_ex(ctx, key, len, EVP_sha512(), NULL);
  66. break;
  67. case SSH_HMAC_MD5:
  68. - HMAC_Init(ctx, key, len, EVP_md5());
  69. + HMAC_Init_ex(ctx, key, len, EVP_md5(), NULL);
  70. break;
  71. default:
  72. + HMAC_CTX_free(ctx);
  73. SAFE_FREE(ctx);
  74. ctx = NULL;
  75. }
  76. @@ -363,7 +368,8 @@ void hmac_final(HMACCTX ctx, unsigned ch
  77. HMAC_Final(ctx,hashmacbuf,len);
  78. #ifndef OLD_CRYPTO
  79. - HMAC_CTX_cleanup(ctx);
  80. + HMAC_CTX_free(ctx);
  81. + ctx = NULL;
  82. #else
  83. HMAC_cleanup(ctx);
  84. #endif
  85. @@ -455,7 +461,11 @@ static void aes_ctr128_encrypt(struct ss
  86. * Same for num, which is being used to store the current offset in blocksize in CTR
  87. * function.
  88. */
  89. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  90. AES_ctr128_encrypt(in, out, len, cipher->key, cipher->IV, tmp_buffer, &num);
  91. +#else
  92. + CRYPTO_ctr128_encrypt(in, out, len, cipher->key, cipher->IV, tmp_buffer, &num, (block128_f)AES_encrypt);
  93. +#endif
  94. }
  95. #endif /* BROKEN_AES_CTR */
  96. #endif /* HAS_AES */
  97. --- a/src/pki_crypto.c
  98. +++ b/src/pki_crypto.c
  99. @@ -31,6 +31,7 @@
  100. #include <openssl/dsa.h>
  101. #include <openssl/err.h>
  102. #include <openssl/rsa.h>
  103. +#include "libcrypto-compat.h"
  104. #ifdef HAVE_OPENSSL_EC_H
  105. #include <openssl/ec.h>
  106. @@ -230,7 +231,10 @@ ssh_key pki_key_dup(const ssh_key key, i
  107. }
  108. switch (key->type) {
  109. - case SSH_KEYTYPE_DSS:
  110. + case SSH_KEYTYPE_DSS: {
  111. + const BIGNUM *p = NULL, *q = NULL, *g = NULL,
  112. + *pub_key = NULL, *priv_key = NULL;
  113. + BIGNUM *np, *nq, *ng, *npub_key, *npriv_key;
  114. new->dsa = DSA_new();
  115. if (new->dsa == NULL) {
  116. goto fail;
  117. @@ -243,36 +247,54 @@ ssh_key pki_key_dup(const ssh_key key, i
  118. * pub_key = public key y = g^x
  119. * priv_key = private key x
  120. */
  121. - new->dsa->p = BN_dup(key->dsa->p);
  122. - if (new->dsa->p == NULL) {
  123. + DSA_get0_pqg(key->dsa, &p, &q, &g);
  124. + np = BN_dup(p);
  125. + nq = BN_dup(q);
  126. + ng = BN_dup(g);
  127. + if (np == NULL || nq == NULL || ng == NULL) {
  128. + BN_free(np);
  129. + BN_free(nq);
  130. + BN_free(ng);
  131. goto fail;
  132. }
  133. - new->dsa->q = BN_dup(key->dsa->q);
  134. - if (new->dsa->q == NULL) {
  135. + rc = DSA_set0_pqg(new->dsa, np, nq, ng);
  136. + if (rc == 0) {
  137. + BN_free(np);
  138. + BN_free(nq);
  139. + BN_free(ng);
  140. goto fail;
  141. }
  142. - new->dsa->g = BN_dup(key->dsa->g);
  143. - if (new->dsa->g == NULL) {
  144. + DSA_get0_key(key->dsa, &pub_key, &priv_key);
  145. + npub_key = BN_dup(pub_key);
  146. + if (npub_key == NULL) {
  147. goto fail;
  148. }
  149. - new->dsa->pub_key = BN_dup(key->dsa->pub_key);
  150. - if (new->dsa->pub_key == NULL) {
  151. + rc = DSA_set0_key(new->dsa, npub_key, NULL);
  152. + if (rc == 0) {
  153. goto fail;
  154. }
  155. if (!demote && (key->flags & SSH_KEY_FLAG_PRIVATE)) {
  156. - new->dsa->priv_key = BN_dup(key->dsa->priv_key);
  157. - if (new->dsa->priv_key == NULL) {
  158. + npriv_key = BN_dup(priv_key);
  159. + if (npriv_key == NULL) {
  160. + goto fail;
  161. + }
  162. +
  163. + rc = DSA_set0_key(new->dsa, NULL, npriv_key);
  164. + if (rc == 0) {
  165. goto fail;
  166. }
  167. }
  168. break;
  169. + }
  170. case SSH_KEYTYPE_RSA:
  171. - case SSH_KEYTYPE_RSA1:
  172. + case SSH_KEYTYPE_RSA1: {
  173. + const BIGNUM *n = NULL, *e = NULL, *d = NULL;
  174. + BIGNUM *nn, *ne, *nd;
  175. new->rsa = RSA_new();
  176. if (new->rsa == NULL) {
  177. goto fail;
  178. @@ -288,62 +310,82 @@ ssh_key pki_key_dup(const ssh_key key, i
  179. * dmq1 = d mod (q-1)
  180. * iqmp = q^-1 mod p
  181. */
  182. - new->rsa->n = BN_dup(key->rsa->n);
  183. - if (new->rsa->n == NULL) {
  184. + RSA_get0_key(key->rsa, &n, &e, &d);
  185. + nn = BN_dup(n);
  186. + ne = BN_dup(e);
  187. + if (nn == NULL || ne == NULL) {
  188. + BN_free(nn);
  189. + BN_free(ne);
  190. goto fail;
  191. }
  192. - new->rsa->e = BN_dup(key->rsa->e);
  193. - if (new->rsa->e == NULL) {
  194. + rc = RSA_set0_key(new->rsa, nn, ne, NULL);
  195. + if (rc == 0) {
  196. + BN_free(nn);
  197. + BN_free(ne);
  198. goto fail;
  199. }
  200. if (!demote && (key->flags & SSH_KEY_FLAG_PRIVATE)) {
  201. - new->rsa->d = BN_dup(key->rsa->d);
  202. - if (new->rsa->d == NULL) {
  203. + const BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL,
  204. + *dmq1 = NULL, *iqmp = NULL;
  205. + BIGNUM *np, *nq, *ndmp1, *ndmq1, *niqmp;
  206. +
  207. + nd = BN_dup(d);
  208. + if (nd == NULL) {
  209. + goto fail;
  210. + }
  211. +
  212. + rc = RSA_set0_key(new->rsa, NULL, NULL, nd);
  213. + if (rc == 0) {
  214. goto fail;
  215. }
  216. /* p, q, dmp1, dmq1 and iqmp may be NULL in private keys, but the
  217. * RSA operations are much faster when these values are available.
  218. */
  219. - if (key->rsa->p != NULL) {
  220. - new->rsa->p = BN_dup(key->rsa->p);
  221. - if (new->rsa->p == NULL) {
  222. + RSA_get0_factors(key->rsa, &p, &q);
  223. + if (p != NULL && q != NULL) { /* need to set both of them */
  224. + np = BN_dup(p);
  225. + nq = BN_dup(q);
  226. + if (np == NULL || nq == NULL) {
  227. + BN_free(np);
  228. + BN_free(nq);
  229. goto fail;
  230. }
  231. - }
  232. - if (key->rsa->q != NULL) {
  233. - new->rsa->q = BN_dup(key->rsa->q);
  234. - if (new->rsa->q == NULL) {
  235. + rc = RSA_set0_factors(new->rsa, np, nq);
  236. + if (rc == 0) {
  237. + BN_free(np);
  238. + BN_free(nq);
  239. goto fail;
  240. }
  241. }
  242. - if (key->rsa->dmp1 != NULL) {
  243. - new->rsa->dmp1 = BN_dup(key->rsa->dmp1);
  244. - if (new->rsa->dmp1 == NULL) {
  245. + RSA_get0_crt_params(key->rsa, &dmp1, &dmq1, &iqmp);
  246. + if (dmp1 != NULL || dmq1 != NULL || iqmp != NULL) {
  247. + ndmp1 = BN_dup(dmp1);
  248. + ndmq1 = BN_dup(dmq1);
  249. + niqmp = BN_dup(iqmp);
  250. + if (ndmp1 == NULL || ndmq1 == NULL || niqmp == NULL) {
  251. + BN_free(ndmp1);
  252. + BN_free(ndmq1);
  253. + BN_free(niqmp);
  254. goto fail;
  255. }
  256. - }
  257. - if (key->rsa->dmq1 != NULL) {
  258. - new->rsa->dmq1 = BN_dup(key->rsa->dmq1);
  259. - if (new->rsa->dmq1 == NULL) {
  260. - goto fail;
  261. - }
  262. - }
  263. -
  264. - if (key->rsa->iqmp != NULL) {
  265. - new->rsa->iqmp = BN_dup(key->rsa->iqmp);
  266. - if (new->rsa->iqmp == NULL) {
  267. + rc = RSA_set0_crt_params(new->rsa, ndmp1, ndmq1, niqmp);
  268. + if (rc == 0) {
  269. + BN_free(ndmp1);
  270. + BN_free(ndmq1);
  271. + BN_free(niqmp);
  272. goto fail;
  273. }
  274. }
  275. }
  276. break;
  277. + }
  278. case SSH_KEYTYPE_ECDSA:
  279. #ifdef HAVE_OPENSSL_ECC
  280. new->ecdsa_nid = key->ecdsa_nid;
  281. @@ -409,11 +451,30 @@ int pki_key_generate_rsa(ssh_key key, in
  282. int pki_key_generate_dss(ssh_key key, int parameter){
  283. int rc;
  284. +#if OPENSSL_VERSION_NUMBER > 0x10100000L
  285. + key->dsa = DSA_new();
  286. + if (key->dsa == NULL) {
  287. + return SSH_ERROR;
  288. + }
  289. + rc = DSA_generate_parameters_ex(key->dsa,
  290. + parameter,
  291. + NULL, /* seed */
  292. + 0, /* seed_len */
  293. + NULL, /* counter_ret */
  294. + NULL, /* h_ret */
  295. + NULL); /* cb */
  296. + if (rc != 1) {
  297. + DSA_free(key->dsa);
  298. + key->dsa = NULL;
  299. + return SSH_ERROR;
  300. + }
  301. +#else
  302. key->dsa = DSA_generate_parameters(parameter, NULL, 0, NULL, NULL,
  303. NULL, NULL);
  304. if(key->dsa == NULL){
  305. return SSH_ERROR;
  306. }
  307. +#endif
  308. rc = DSA_generate_key(key->dsa);
  309. if (rc != 1){
  310. DSA_free(key->dsa);
  311. @@ -466,51 +527,64 @@ int pki_key_compare(const ssh_key k1,
  312. enum ssh_keycmp_e what)
  313. {
  314. switch (k1->type) {
  315. - case SSH_KEYTYPE_DSS:
  316. + case SSH_KEYTYPE_DSS: {
  317. + const BIGNUM *p1, *p2, *q1, *q2, *g1, *g2,
  318. + *pub_key1, *pub_key2, *priv_key1, *priv_key2;
  319. if (DSA_size(k1->dsa) != DSA_size(k2->dsa)) {
  320. return 1;
  321. }
  322. - if (bignum_cmp(k1->dsa->p, k2->dsa->p) != 0) {
  323. + DSA_get0_pqg(k1->dsa, &p1, &q1, &g1);
  324. + DSA_get0_pqg(k2->dsa, &p2, &q2, &g2);
  325. + if (bignum_cmp(p1, p2) != 0) {
  326. return 1;
  327. }
  328. - if (bignum_cmp(k1->dsa->q, k2->dsa->q) != 0) {
  329. + if (bignum_cmp(q1, q2) != 0) {
  330. return 1;
  331. }
  332. - if (bignum_cmp(k1->dsa->g, k2->dsa->g) != 0) {
  333. + if (bignum_cmp(g1, g2) != 0) {
  334. return 1;
  335. }
  336. - if (bignum_cmp(k1->dsa->pub_key, k2->dsa->pub_key) != 0) {
  337. + DSA_get0_key(k1->dsa, &pub_key1, &priv_key1);
  338. + DSA_get0_key(k2->dsa, &pub_key2, &priv_key2);
  339. + if (bignum_cmp(pub_key1, pub_key2) != 0) {
  340. return 1;
  341. }
  342. if (what == SSH_KEY_CMP_PRIVATE) {
  343. - if (bignum_cmp(k1->dsa->priv_key, k2->dsa->priv_key) != 0) {
  344. + if (bignum_cmp(priv_key1, priv_key2) != 0) {
  345. return 1;
  346. }
  347. }
  348. break;
  349. + }
  350. case SSH_KEYTYPE_RSA:
  351. - case SSH_KEYTYPE_RSA1:
  352. + case SSH_KEYTYPE_RSA1: {
  353. + const BIGNUM *e1, *e2, *n1, *n2, *p1, *p2, *q1, *q2;
  354. if (RSA_size(k1->rsa) != RSA_size(k2->rsa)) {
  355. return 1;
  356. }
  357. - if (bignum_cmp(k1->rsa->e, k2->rsa->e) != 0) {
  358. + RSA_get0_key(k1->rsa, &n1, &e1, NULL);
  359. + RSA_get0_key(k2->rsa, &n2, &e2, NULL);
  360. + if (bignum_cmp(e1, e2) != 0) {
  361. return 1;
  362. }
  363. - if (bignum_cmp(k1->rsa->n, k2->rsa->n) != 0) {
  364. + if (bignum_cmp(n1, n2) != 0) {
  365. return 1;
  366. }
  367. if (what == SSH_KEY_CMP_PRIVATE) {
  368. - if (bignum_cmp(k1->rsa->p, k2->rsa->p) != 0) {
  369. + RSA_get0_factors(k1->rsa, &p1, &q1);
  370. + RSA_get0_factors(k2->rsa, &p2, &q2);
  371. + if (bignum_cmp(p1, p2) != 0) {
  372. return 1;
  373. }
  374. - if (bignum_cmp(k1->rsa->q, k2->rsa->q) != 0) {
  375. + if (bignum_cmp(q1, q2) != 0) {
  376. return 1;
  377. }
  378. }
  379. break;
  380. + }
  381. case SSH_KEYTYPE_ECDSA:
  382. #ifdef HAVE_OPENSSL_ECC
  383. {
  384. @@ -586,7 +660,7 @@ ssh_string pki_private_key_to_pem(const
  385. } else {
  386. rc = PEM_write_bio_DSAPrivateKey(mem,
  387. key->dsa,
  388. - NULL, /* cipher */
  389. + EVP_aes_128_cbc(),
  390. NULL, /* kstr */
  391. 0, /* klen */
  392. NULL, /* auth_fn */
  393. @@ -611,7 +685,7 @@ ssh_string pki_private_key_to_pem(const
  394. } else {
  395. rc = PEM_write_bio_RSAPrivateKey(mem,
  396. key->rsa,
  397. - NULL, /* cipher */
  398. + EVP_aes_128_cbc(),
  399. NULL, /* kstr */
  400. 0, /* klen */
  401. NULL, /* auth_fn */
  402. @@ -621,8 +695,8 @@ ssh_string pki_private_key_to_pem(const
  403. goto err;
  404. }
  405. break;
  406. - case SSH_KEYTYPE_ECDSA:
  407. #ifdef HAVE_ECC
  408. + case SSH_KEYTYPE_ECDSA:
  409. if (passphrase == NULL) {
  410. struct pem_get_password_struct pgp = { auth_fn, auth_data };
  411. @@ -636,7 +710,7 @@ ssh_string pki_private_key_to_pem(const
  412. } else {
  413. rc = PEM_write_bio_ECPrivateKey(mem,
  414. key->ecdsa,
  415. - NULL, /* cipher */
  416. + EVP_aes_128_cbc(),
  417. NULL, /* kstr */
  418. 0, /* klen */
  419. NULL, /* auth_fn */
  420. @@ -819,43 +893,65 @@ int pki_pubkey_build_dss(ssh_key key,
  421. ssh_string q,
  422. ssh_string g,
  423. ssh_string pubkey) {
  424. + int rc;
  425. + BIGNUM *bp, *bq, *bg, *bpub_key;
  426. +
  427. key->dsa = DSA_new();
  428. if (key->dsa == NULL) {
  429. return SSH_ERROR;
  430. }
  431. - key->dsa->p = make_string_bn(p);
  432. - key->dsa->q = make_string_bn(q);
  433. - key->dsa->g = make_string_bn(g);
  434. - key->dsa->pub_key = make_string_bn(pubkey);
  435. - if (key->dsa->p == NULL ||
  436. - key->dsa->q == NULL ||
  437. - key->dsa->g == NULL ||
  438. - key->dsa->pub_key == NULL) {
  439. - DSA_free(key->dsa);
  440. - return SSH_ERROR;
  441. + bp = make_string_bn(p);
  442. + bq = make_string_bn(q);
  443. + bg = make_string_bn(g);
  444. + bpub_key = make_string_bn(pubkey);
  445. + if (bp == NULL || bq == NULL ||
  446. + bg == NULL || bpub_key == NULL) {
  447. + goto fail;
  448. + }
  449. +
  450. + rc = DSA_set0_pqg(key->dsa, bp, bq, bg);
  451. + if (rc == 0) {
  452. + goto fail;
  453. + }
  454. +
  455. + rc = DSA_set0_key(key->dsa, bpub_key, NULL);
  456. + if (rc == 0) {
  457. + goto fail;
  458. }
  459. return SSH_OK;
  460. +fail:
  461. + DSA_free(key->dsa);
  462. + return SSH_ERROR;
  463. }
  464. int pki_pubkey_build_rsa(ssh_key key,
  465. ssh_string e,
  466. ssh_string n) {
  467. + int rc;
  468. + BIGNUM *be, *bn;
  469. +
  470. key->rsa = RSA_new();
  471. if (key->rsa == NULL) {
  472. return SSH_ERROR;
  473. }
  474. - key->rsa->e = make_string_bn(e);
  475. - key->rsa->n = make_string_bn(n);
  476. - if (key->rsa->e == NULL ||
  477. - key->rsa->n == NULL) {
  478. - RSA_free(key->rsa);
  479. - return SSH_ERROR;
  480. + be = make_string_bn(e);
  481. + bn = make_string_bn(n);
  482. + if (be == NULL || bn == NULL) {
  483. + goto fail;
  484. + }
  485. +
  486. + rc = RSA_set0_key(key->rsa, bn, be, NULL);
  487. + if (rc == 0) {
  488. + goto fail;
  489. }
  490. return SSH_OK;
  491. +fail:
  492. + RSA_free(key->rsa);
  493. + return SSH_ERROR;
  494. }
  495. ssh_string pki_publickey_to_blob(const ssh_key key)
  496. @@ -889,23 +985,26 @@ ssh_string pki_publickey_to_blob(const s
  497. }
  498. switch (key->type) {
  499. - case SSH_KEYTYPE_DSS:
  500. - p = make_bignum_string(key->dsa->p);
  501. + case SSH_KEYTYPE_DSS: {
  502. + const BIGNUM *bp, *bq, *bg, *bpub_key;
  503. + DSA_get0_pqg(key->dsa, &bp, &bq, &bg);
  504. + p = make_bignum_string((BIGNUM *)bp);
  505. if (p == NULL) {
  506. goto fail;
  507. }
  508. - q = make_bignum_string(key->dsa->q);
  509. + q = make_bignum_string((BIGNUM *)bq);
  510. if (q == NULL) {
  511. goto fail;
  512. }
  513. - g = make_bignum_string(key->dsa->g);
  514. + g = make_bignum_string((BIGNUM *)bg);
  515. if (g == NULL) {
  516. goto fail;
  517. }
  518. - n = make_bignum_string(key->dsa->pub_key);
  519. + DSA_get0_key(key->dsa, &bpub_key, NULL);
  520. + n = make_bignum_string((BIGNUM *)bpub_key);
  521. if (n == NULL) {
  522. goto fail;
  523. }
  524. @@ -937,14 +1036,17 @@ ssh_string pki_publickey_to_blob(const s
  525. n = NULL;
  526. break;
  527. + }
  528. case SSH_KEYTYPE_RSA:
  529. - case SSH_KEYTYPE_RSA1:
  530. - e = make_bignum_string(key->rsa->e);
  531. + case SSH_KEYTYPE_RSA1: {
  532. + const BIGNUM *be, *bn;
  533. + RSA_get0_key(key->rsa, &bn, &be, NULL);
  534. + e = make_bignum_string((BIGNUM *)be);
  535. if (e == NULL) {
  536. goto fail;
  537. }
  538. - n = make_bignum_string(key->rsa->n);
  539. + n = make_bignum_string((BIGNUM *)bn);
  540. if (n == NULL) {
  541. goto fail;
  542. }
  543. @@ -964,6 +1066,7 @@ ssh_string pki_publickey_to_blob(const s
  544. n = NULL;
  545. break;
  546. + }
  547. case SSH_KEYTYPE_ECDSA:
  548. #ifdef HAVE_OPENSSL_ECC
  549. rc = ssh_buffer_reinit(buffer);
  550. @@ -1065,13 +1168,15 @@ int pki_export_pubkey_rsa1(const ssh_key
  551. char *e;
  552. char *n;
  553. int rsa_size = RSA_size(key->rsa);
  554. + const BIGNUM *be, *bn;
  555. - e = bignum_bn2dec(key->rsa->e);
  556. + RSA_get0_key(key->rsa, &bn, &be, NULL);
  557. + e = bignum_bn2dec(be);
  558. if (e == NULL) {
  559. return SSH_ERROR;
  560. }
  561. - n = bignum_bn2dec(key->rsa->n);
  562. + n = bignum_bn2dec(bn);
  563. if (n == NULL) {
  564. OPENSSL_free(e);
  565. return SSH_ERROR;
  566. @@ -1136,6 +1241,7 @@ static ssh_string pki_dsa_signature_to_b
  567. {
  568. char buffer[40] = { 0 };
  569. ssh_string sig_blob = NULL;
  570. + const BIGNUM *pr, *ps;
  571. ssh_string r;
  572. int r_len, r_offset_in, r_offset_out;
  573. @@ -1143,12 +1249,13 @@ static ssh_string pki_dsa_signature_to_b
  574. ssh_string s;
  575. int s_len, s_offset_in, s_offset_out;
  576. - r = make_bignum_string(sig->dsa_sig->r);
  577. + DSA_SIG_get0(sig->dsa_sig, &pr, &ps);
  578. + r = make_bignum_string((BIGNUM *)pr);
  579. if (r == NULL) {
  580. return NULL;
  581. }
  582. - s = make_bignum_string(sig->dsa_sig->s);
  583. + s = make_bignum_string((BIGNUM *)ps);
  584. if (s == NULL) {
  585. ssh_string_free(r);
  586. return NULL;
  587. @@ -1201,13 +1308,15 @@ ssh_string pki_signature_to_blob(const s
  588. ssh_string s;
  589. ssh_buffer b;
  590. int rc;
  591. + const BIGNUM *pr, *ps;
  592. b = ssh_buffer_new();
  593. if (b == NULL) {
  594. return NULL;
  595. }
  596. - r = make_bignum_string(sig->ecdsa_sig->r);
  597. + ECDSA_SIG_get0(sig->ecdsa_sig, &pr, &ps);
  598. + r = make_bignum_string((BIGNUM *)pr);
  599. if (r == NULL) {
  600. ssh_buffer_free(b);
  601. return NULL;
  602. @@ -1219,7 +1328,7 @@ ssh_string pki_signature_to_blob(const s
  603. return NULL;
  604. }
  605. - s = make_bignum_string(sig->ecdsa_sig->s);
  606. + s = make_bignum_string((BIGNUM *)ps);
  607. if (s == NULL) {
  608. ssh_buffer_free(b);
  609. return NULL;
  610. @@ -1324,6 +1433,7 @@ ssh_signature pki_signature_from_blob(co
  611. ssh_string s;
  612. size_t len;
  613. int rc;
  614. + BIGNUM *pr = NULL, *ps = NULL;
  615. sig = ssh_signature_new();
  616. if (sig == NULL) {
  617. @@ -1363,9 +1473,9 @@ ssh_signature pki_signature_from_blob(co
  618. }
  619. ssh_string_fill(r, ssh_string_data(sig_blob), 20);
  620. - sig->dsa_sig->r = make_string_bn(r);
  621. + pr = make_string_bn(r);
  622. ssh_string_free(r);
  623. - if (sig->dsa_sig->r == NULL) {
  624. + if (pr == NULL) {
  625. ssh_signature_free(sig);
  626. return NULL;
  627. }
  628. @@ -1377,9 +1487,15 @@ ssh_signature pki_signature_from_blob(co
  629. }
  630. ssh_string_fill(s, (char *)ssh_string_data(sig_blob) + 20, 20);
  631. - sig->dsa_sig->s = make_string_bn(s);
  632. + ps = make_string_bn(s);
  633. ssh_string_free(s);
  634. - if (sig->dsa_sig->s == NULL) {
  635. + if (ps == NULL) {
  636. + ssh_signature_free(sig);
  637. + return NULL;
  638. + }
  639. +
  640. + rc = DSA_SIG_set0(sig->dsa_sig, pr, ps);
  641. + if (rc == 0) {
  642. ssh_signature_free(sig);
  643. return NULL;
  644. }
  645. @@ -1427,17 +1543,17 @@ ssh_signature pki_signature_from_blob(co
  646. ssh_print_hexa("r", ssh_string_data(r), ssh_string_len(r));
  647. #endif
  648. - make_string_bn_inplace(r, sig->ecdsa_sig->r);
  649. + pr = make_string_bn(r);
  650. ssh_string_burn(r);
  651. ssh_string_free(r);
  652. - if (sig->ecdsa_sig->r == NULL) {
  653. + if (pr == NULL) {
  654. ssh_buffer_free(b);
  655. ssh_signature_free(sig);
  656. return NULL;
  657. }
  658. s = buffer_get_ssh_string(b);
  659. - rlen = buffer_get_rest_len(b);
  660. + rlen = buffer_get_len(b);
  661. ssh_buffer_free(b);
  662. if (s == NULL) {
  663. ssh_signature_free(sig);
  664. @@ -1448,10 +1564,16 @@ ssh_signature pki_signature_from_blob(co
  665. ssh_print_hexa("s", ssh_string_data(s), ssh_string_len(s));
  666. #endif
  667. - make_string_bn_inplace(s, sig->ecdsa_sig->s);
  668. + ps = make_string_bn(s);
  669. ssh_string_burn(s);
  670. ssh_string_free(s);
  671. - if (sig->ecdsa_sig->s == NULL) {
  672. + if (ps == NULL) {
  673. + ssh_signature_free(sig);
  674. + return NULL;
  675. + }
  676. +
  677. + rc = ECDSA_SIG_set0(sig->ecdsa_sig, pr, ps);
  678. + if (rc == 0) {
  679. ssh_signature_free(sig);
  680. return NULL;
  681. }
  682. @@ -1578,8 +1700,12 @@ ssh_signature pki_do_sign(const ssh_key
  683. }
  684. #ifdef DEBUG_CRYPTO
  685. - ssh_print_bignum("r", sig->dsa_sig->r);
  686. - ssh_print_bignum("s", sig->dsa_sig->s);
  687. + {
  688. + const BIGNUM *pr, *ps;
  689. + DSA_SIG_get0(sig->dsa_sig, &pr, &ps);
  690. + ssh_print_bignum("r", (BIGNUM *) pr);
  691. + ssh_print_bignum("s", (BIGNUM *) ps);
  692. + }
  693. #endif
  694. break;
  695. @@ -1601,8 +1727,12 @@ ssh_signature pki_do_sign(const ssh_key
  696. }
  697. # ifdef DEBUG_CRYPTO
  698. - ssh_print_bignum("r", sig->ecdsa_sig->r);
  699. - ssh_print_bignum("s", sig->ecdsa_sig->s);
  700. + {
  701. + const BIGNUM *pr, *ps;
  702. + ECDSA_SIG_get0(sig->ecdsa_sig, &pr, &ps);
  703. + ssh_print_bignum("r", (BIGNUM *) pr);
  704. + ssh_print_bignum("s", (BIGNUM *) ps);
  705. + }
  706. # endif /* DEBUG_CRYPTO */
  707. break;
  708. --- a/src/CMakeLists.txt
  709. +++ b/src/CMakeLists.txt
  710. @@ -164,6 +164,9 @@ else (WITH_GCRYPT)
  711. ${libssh_SRCS}
  712. pki_crypto.c
  713. )
  714. + if(OPENSSL_VERSION VERSION_LESS "1.1.0")
  715. + set(libssh_SRCS ${libssh_SRCS} libcrypto-compat.c)
  716. + endif()
  717. endif (WITH_GCRYPT)
  718. if (WITH_SFTP)
  719. --- /dev/null
  720. +++ b/src/libcrypto-compat.c
  721. @@ -0,0 +1,334 @@
  722. +/*
  723. + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  724. + *
  725. + * Licensed under the OpenSSL license (the "License"). You may not use
  726. + * this file except in compliance with the License. You can obtain a copy
  727. + * in the file LICENSE in the source distribution or at
  728. + * https://www.openssl.org/source/license.html
  729. + */
  730. +
  731. +#include "config.h"
  732. +
  733. +#include <string.h>
  734. +#include <openssl/engine.h>
  735. +#include "libcrypto-compat.h"
  736. +
  737. +static void *OPENSSL_zalloc(size_t num)
  738. +{
  739. + void *ret = OPENSSL_malloc(num);
  740. +
  741. + if (ret != NULL)
  742. + memset(ret, 0, num);
  743. + return ret;
  744. +}
  745. +
  746. +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  747. +{
  748. + /* If the fields n and e in r are NULL, the corresponding input
  749. + * parameters MUST be non-NULL for n and e. d may be
  750. + * left NULL (in case only the public key is used).
  751. + */
  752. + if ((r->n == NULL && n == NULL)
  753. + || (r->e == NULL && e == NULL))
  754. + return 0;
  755. +
  756. + if (n != NULL) {
  757. + BN_free(r->n);
  758. + r->n = n;
  759. + }
  760. + if (e != NULL) {
  761. + BN_free(r->e);
  762. + r->e = e;
  763. + }
  764. + if (d != NULL) {
  765. + BN_free(r->d);
  766. + r->d = d;
  767. + }
  768. +
  769. + return 1;
  770. +}
  771. +
  772. +int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  773. +{
  774. + /* If the fields p and q in r are NULL, the corresponding input
  775. + * parameters MUST be non-NULL.
  776. + */
  777. + if ((r->p == NULL && p == NULL)
  778. + || (r->q == NULL && q == NULL))
  779. + return 0;
  780. +
  781. + if (p != NULL) {
  782. + BN_free(r->p);
  783. + r->p = p;
  784. + }
  785. + if (q != NULL) {
  786. + BN_free(r->q);
  787. + r->q = q;
  788. + }
  789. +
  790. + return 1;
  791. +}
  792. +
  793. +int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  794. +{
  795. + /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  796. + * parameters MUST be non-NULL.
  797. + */
  798. + if ((r->dmp1 == NULL && dmp1 == NULL)
  799. + || (r->dmq1 == NULL && dmq1 == NULL)
  800. + || (r->iqmp == NULL && iqmp == NULL))
  801. + return 0;
  802. +
  803. + if (dmp1 != NULL) {
  804. + BN_free(r->dmp1);
  805. + r->dmp1 = dmp1;
  806. + }
  807. + if (dmq1 != NULL) {
  808. + BN_free(r->dmq1);
  809. + r->dmq1 = dmq1;
  810. + }
  811. + if (iqmp != NULL) {
  812. + BN_free(r->iqmp);
  813. + r->iqmp = iqmp;
  814. + }
  815. +
  816. + return 1;
  817. +}
  818. +
  819. +void RSA_get0_key(const RSA *r,
  820. + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  821. +{
  822. + if (n != NULL)
  823. + *n = r->n;
  824. + if (e != NULL)
  825. + *e = r->e;
  826. + if (d != NULL)
  827. + *d = r->d;
  828. +}
  829. +
  830. +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  831. +{
  832. + if (p != NULL)
  833. + *p = r->p;
  834. + if (q != NULL)
  835. + *q = r->q;
  836. +}
  837. +
  838. +void RSA_get0_crt_params(const RSA *r,
  839. + const BIGNUM **dmp1, const BIGNUM **dmq1,
  840. + const BIGNUM **iqmp)
  841. +{
  842. + if (dmp1 != NULL)
  843. + *dmp1 = r->dmp1;
  844. + if (dmq1 != NULL)
  845. + *dmq1 = r->dmq1;
  846. + if (iqmp != NULL)
  847. + *iqmp = r->iqmp;
  848. +}
  849. +
  850. +void DSA_get0_pqg(const DSA *d,
  851. + const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  852. +{
  853. + if (p != NULL)
  854. + *p = d->p;
  855. + if (q != NULL)
  856. + *q = d->q;
  857. + if (g != NULL)
  858. + *g = d->g;
  859. +}
  860. +
  861. +int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  862. +{
  863. + /* If the fields p, q and g in d are NULL, the corresponding input
  864. + * parameters MUST be non-NULL.
  865. + */
  866. + if ((d->p == NULL && p == NULL)
  867. + || (d->q == NULL && q == NULL)
  868. + || (d->g == NULL && g == NULL))
  869. + return 0;
  870. +
  871. + if (p != NULL) {
  872. + BN_free(d->p);
  873. + d->p = p;
  874. + }
  875. + if (q != NULL) {
  876. + BN_free(d->q);
  877. + d->q = q;
  878. + }
  879. + if (g != NULL) {
  880. + BN_free(d->g);
  881. + d->g = g;
  882. + }
  883. +
  884. + return 1;
  885. +}
  886. +
  887. +void DSA_get0_key(const DSA *d,
  888. + const BIGNUM **pub_key, const BIGNUM **priv_key)
  889. +{
  890. + if (pub_key != NULL)
  891. + *pub_key = d->pub_key;
  892. + if (priv_key != NULL)
  893. + *priv_key = d->priv_key;
  894. +}
  895. +
  896. +int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  897. +{
  898. + /* If the field pub_key in d is NULL, the corresponding input
  899. + * parameters MUST be non-NULL. The priv_key field may
  900. + * be left NULL.
  901. + */
  902. + if (d->pub_key == NULL && pub_key == NULL)
  903. + return 0;
  904. +
  905. + if (pub_key != NULL) {
  906. + BN_free(d->pub_key);
  907. + d->pub_key = pub_key;
  908. + }
  909. + if (priv_key != NULL) {
  910. + BN_free(d->priv_key);
  911. + d->priv_key = priv_key;
  912. + }
  913. +
  914. + return 1;
  915. +}
  916. +
  917. +void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  918. +{
  919. + if (pr != NULL)
  920. + *pr = sig->r;
  921. + if (ps != NULL)
  922. + *ps = sig->s;
  923. +}
  924. +
  925. +int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  926. +{
  927. + if (r == NULL || s == NULL)
  928. + return 0;
  929. + BN_clear_free(sig->r);
  930. + BN_clear_free(sig->s);
  931. + sig->r = r;
  932. + sig->s = s;
  933. + return 1;
  934. +}
  935. +
  936. +void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  937. +{
  938. + if (pr != NULL)
  939. + *pr = sig->r;
  940. + if (ps != NULL)
  941. + *ps = sig->s;
  942. +}
  943. +
  944. +int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  945. +{
  946. + if (r == NULL || s == NULL)
  947. + return 0;
  948. + BN_clear_free(sig->r);
  949. + BN_clear_free(sig->s);
  950. + sig->r = r;
  951. + sig->s = s;
  952. + return 1;
  953. +}
  954. +
  955. +EVP_MD_CTX *EVP_MD_CTX_new(void)
  956. +{
  957. + return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  958. +}
  959. +
  960. +static void OPENSSL_clear_free(void *str, size_t num)
  961. +{
  962. + if (str == NULL)
  963. + return;
  964. + if (num)
  965. + OPENSSL_cleanse(str, num);
  966. + OPENSSL_free(str);
  967. +}
  968. +
  969. +/* This call frees resources associated with the context */
  970. +int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
  971. +{
  972. + if (ctx == NULL)
  973. + return 1;
  974. +
  975. + /*
  976. + * Don't assume ctx->md_data was cleaned in EVP_Digest_Final, because
  977. + * sometimes only copies of the context are ever finalised.
  978. + */
  979. + if (ctx->digest && ctx->digest->cleanup
  980. + && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
  981. + ctx->digest->cleanup(ctx);
  982. + if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
  983. + && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
  984. + OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
  985. + }
  986. + EVP_PKEY_CTX_free(ctx->pctx);
  987. +#ifndef OPENSSL_NO_ENGINE
  988. + ENGINE_finish(ctx->engine);
  989. +#endif
  990. + OPENSSL_cleanse(ctx, sizeof(*ctx));
  991. +
  992. + return 1;
  993. +}
  994. +
  995. +void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  996. +{
  997. + EVP_MD_CTX_reset(ctx);
  998. + OPENSSL_free(ctx);
  999. +}
  1000. +
  1001. +HMAC_CTX *HMAC_CTX_new(void)
  1002. +{
  1003. + HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));
  1004. +
  1005. + if (ctx != NULL) {
  1006. + if (!HMAC_CTX_reset(ctx)) {
  1007. + HMAC_CTX_free(ctx);
  1008. + return NULL;
  1009. + }
  1010. + }
  1011. + return ctx;
  1012. +}
  1013. +
  1014. +static void hmac_ctx_cleanup(HMAC_CTX *ctx)
  1015. +{
  1016. + EVP_MD_CTX_reset(&ctx->i_ctx);
  1017. + EVP_MD_CTX_reset(&ctx->o_ctx);
  1018. + EVP_MD_CTX_reset(&ctx->md_ctx);
  1019. + ctx->md = NULL;
  1020. + ctx->key_length = 0;
  1021. + OPENSSL_cleanse(ctx->key, sizeof(ctx->key));
  1022. +}
  1023. +
  1024. +void HMAC_CTX_free(HMAC_CTX *ctx)
  1025. +{
  1026. + if (ctx != NULL) {
  1027. + hmac_ctx_cleanup(ctx);
  1028. +#if OPENSSL_VERSION_NUMBER > 0x10100000L
  1029. + EVP_MD_CTX_free(&ctx->i_ctx);
  1030. + EVP_MD_CTX_free(&ctx->o_ctx);
  1031. + EVP_MD_CTX_free(&ctx->md_ctx);
  1032. +#endif
  1033. + OPENSSL_free(ctx);
  1034. + }
  1035. +}
  1036. +
  1037. +int HMAC_CTX_reset(HMAC_CTX *ctx)
  1038. +{
  1039. + HMAC_CTX_init(ctx);
  1040. + return 1;
  1041. +}
  1042. +
  1043. +#ifndef HAVE_OPENSSL_EVP_CIPHER_CTX_NEW
  1044. +EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
  1045. +{
  1046. + return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
  1047. +}
  1048. +
  1049. +void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
  1050. +{
  1051. + /* EVP_CIPHER_CTX_reset(ctx); alias */
  1052. + EVP_CIPHER_CTX_init(ctx);
  1053. + OPENSSL_free(ctx);
  1054. +}
  1055. +#endif
  1056. --- /dev/null
  1057. +++ b/src/libcrypto-compat.h
  1058. @@ -0,0 +1,42 @@
  1059. +#ifndef LIBCRYPTO_COMPAT_H
  1060. +#define LIBCRYPTO_COMPAT_H
  1061. +
  1062. +#include <openssl/opensslv.h>
  1063. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  1064. +
  1065. +#include <openssl/rsa.h>
  1066. +#include <openssl/dsa.h>
  1067. +#include <openssl/ecdsa.h>
  1068. +#include <openssl/dh.h>
  1069. +#include <openssl/evp.h>
  1070. +#include <openssl/hmac.h>
  1071. +
  1072. +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
  1073. +int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q);
  1074. +int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp);
  1075. +void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
  1076. +void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);
  1077. +void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp);
  1078. +
  1079. +void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
  1080. +int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  1081. +void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key);
  1082. +int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key);
  1083. +
  1084. +void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  1085. +int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  1086. +
  1087. +void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  1088. +int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  1089. +
  1090. +int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
  1091. +EVP_MD_CTX *EVP_MD_CTX_new(void);
  1092. +void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  1093. +
  1094. +HMAC_CTX *HMAC_CTX_new(void);
  1095. +int HMAC_CTX_reset(HMAC_CTX *ctx);
  1096. +void HMAC_CTX_free(HMAC_CTX *ctx);
  1097. +
  1098. +#endif /* OPENSSL_VERSION_NUMBER */
  1099. +
  1100. +#endif /* LIBCRYPTO_COMPAT_H */