dh.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2019 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. #include <openssl/crypto.h>
  10. #include <openssl/core_numbers.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/dh.h>
  13. #include <openssl/params.h>
  14. #include "internal/provider_algs.h"
  15. static OSSL_OP_keyexch_newctx_fn dh_newctx;
  16. static OSSL_OP_keyexch_init_fn dh_init;
  17. static OSSL_OP_keyexch_set_peer_fn dh_set_peer;
  18. static OSSL_OP_keyexch_derive_fn dh_derive;
  19. static OSSL_OP_keyexch_freectx_fn dh_freectx;
  20. static OSSL_OP_keyexch_dupctx_fn dh_dupctx;
  21. typedef struct {
  22. DH *dh;
  23. DH *dhpeer;
  24. int pad;
  25. } PROV_DH_CTX;
  26. static void *dh_newctx(void *provctx)
  27. {
  28. return OPENSSL_zalloc(sizeof(PROV_DH_CTX));
  29. }
  30. static DH *param_to_dh(OSSL_PARAM params[], int priv)
  31. {
  32. DH *dh = DH_new();
  33. OSSL_PARAM *paramptr;
  34. BIGNUM *p = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;
  35. if (dh == NULL)
  36. return NULL;
  37. paramptr = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DH_P);
  38. if (paramptr == NULL
  39. || !OSSL_PARAM_get_BN(paramptr, &p))
  40. goto err;
  41. paramptr = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DH_G);
  42. if (paramptr == NULL || !OSSL_PARAM_get_BN(paramptr, &g))
  43. goto err;
  44. if (!DH_set0_pqg(dh, p, NULL, g))
  45. goto err;
  46. p = g = NULL;
  47. paramptr = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DH_PUB_KEY);
  48. if (paramptr == NULL || !OSSL_PARAM_get_BN(paramptr, &pub_key))
  49. goto err;
  50. /* Private key is optional */
  51. if (priv) {
  52. paramptr = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DH_PRIV_KEY);
  53. if (paramptr == NULL
  54. || (priv_key = BN_secure_new()) == NULL
  55. || !OSSL_PARAM_get_BN(paramptr, &priv_key))
  56. goto err;
  57. }
  58. if (!DH_set0_key(dh, pub_key, priv_key))
  59. goto err;
  60. return dh;
  61. err:
  62. BN_free(p);
  63. BN_free(g);
  64. BN_free(pub_key);
  65. BN_free(priv_key);
  66. DH_free(dh);
  67. return NULL;
  68. }
  69. static int dh_init(void *vpdhctx, OSSL_PARAM params[])
  70. {
  71. PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
  72. DH_free(pdhctx->dh);
  73. pdhctx->dh = param_to_dh(params, 1);
  74. return pdhctx->dh != NULL;
  75. }
  76. static int dh_set_peer(void *vpdhctx, OSSL_PARAM params[])
  77. {
  78. PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
  79. DH_free(pdhctx->dhpeer);
  80. pdhctx->dhpeer = param_to_dh(params, 0);
  81. return pdhctx->dhpeer != NULL;
  82. }
  83. static int dh_derive(void *vpdhctx, unsigned char *key, size_t *keylen,
  84. size_t outlen)
  85. {
  86. PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
  87. int ret;
  88. size_t dhsize;
  89. const BIGNUM *pub_key = NULL;
  90. /* TODO(3.0): Add errors to stack */
  91. if (pdhctx->dh == NULL || pdhctx->dhpeer == NULL)
  92. return 0;
  93. dhsize = (size_t)DH_size(pdhctx->dh);
  94. if (key == NULL) {
  95. *keylen = dhsize;
  96. return 1;
  97. }
  98. if (outlen < dhsize)
  99. return 0;
  100. DH_get0_key(pdhctx->dhpeer, &pub_key, NULL);
  101. ret = (pdhctx->pad) ? DH_compute_key_padded(key, pub_key, pdhctx->dh)
  102. : DH_compute_key(key, pub_key, pdhctx->dh);
  103. if (ret <= 0)
  104. return 0;
  105. *keylen = ret;
  106. return 1;
  107. }
  108. static void dh_freectx(void *vpdhctx)
  109. {
  110. PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
  111. DH_free(pdhctx->dh);
  112. DH_free(pdhctx->dhpeer);
  113. OPENSSL_free(pdhctx);
  114. }
  115. static void *dh_dupctx(void *vpdhctx)
  116. {
  117. PROV_DH_CTX *srcctx = (PROV_DH_CTX *)vpdhctx;
  118. PROV_DH_CTX *dstctx;
  119. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  120. *dstctx = *srcctx;
  121. if (dstctx->dh != NULL && !DH_up_ref(dstctx->dh)) {
  122. OPENSSL_free(dstctx);
  123. return NULL;
  124. }
  125. if (dstctx->dhpeer != NULL && !DH_up_ref(dstctx->dhpeer)) {
  126. DH_free(dstctx->dh);
  127. OPENSSL_free(dstctx);
  128. return NULL;
  129. }
  130. return dstctx;
  131. }
  132. static int dh_set_params(void *vpdhctx, OSSL_PARAM params[])
  133. {
  134. PROV_DH_CTX *pdhctx = (PROV_DH_CTX *)vpdhctx;
  135. const OSSL_PARAM *p;
  136. int pad;
  137. if (pdhctx == NULL || params == NULL)
  138. return 0;
  139. p = OSSL_PARAM_locate(params, OSSL_EXCHANGE_PARAM_PAD);
  140. if (p == NULL || !OSSL_PARAM_get_int(p, &pad))
  141. return 0;
  142. pdhctx->pad = pad;
  143. return 1;
  144. }
  145. const OSSL_DISPATCH dh_functions[] = {
  146. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))dh_newctx },
  147. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))dh_init },
  148. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))dh_derive },
  149. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))dh_set_peer },
  150. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))dh_freectx },
  151. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))dh_dupctx },
  152. { OSSL_FUNC_KEYEXCH_SET_PARAMS, (void (*)(void))dh_set_params },
  153. { 0, NULL }
  154. };