dh_meth.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2016-2018 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 "dh_local.h"
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. DH_METHOD *DH_meth_new(const char *name, int flags)
  13. {
  14. DH_METHOD *dhm = OPENSSL_zalloc(sizeof(*dhm));
  15. if (dhm != NULL) {
  16. dhm->flags = flags;
  17. dhm->name = OPENSSL_strdup(name);
  18. if (dhm->name != NULL)
  19. return dhm;
  20. OPENSSL_free(dhm);
  21. }
  22. DHerr(DH_F_DH_METH_NEW, ERR_R_MALLOC_FAILURE);
  23. return NULL;
  24. }
  25. void DH_meth_free(DH_METHOD *dhm)
  26. {
  27. if (dhm != NULL) {
  28. OPENSSL_free(dhm->name);
  29. OPENSSL_free(dhm);
  30. }
  31. }
  32. DH_METHOD *DH_meth_dup(const DH_METHOD *dhm)
  33. {
  34. DH_METHOD *ret = OPENSSL_malloc(sizeof(*ret));
  35. if (ret != NULL) {
  36. memcpy(ret, dhm, sizeof(*dhm));
  37. ret->name = OPENSSL_strdup(dhm->name);
  38. if (ret->name != NULL)
  39. return ret;
  40. OPENSSL_free(ret);
  41. }
  42. DHerr(DH_F_DH_METH_DUP, ERR_R_MALLOC_FAILURE);
  43. return NULL;
  44. }
  45. const char *DH_meth_get0_name(const DH_METHOD *dhm)
  46. {
  47. return dhm->name;
  48. }
  49. int DH_meth_set1_name(DH_METHOD *dhm, const char *name)
  50. {
  51. char *tmpname = OPENSSL_strdup(name);
  52. if (tmpname == NULL) {
  53. DHerr(DH_F_DH_METH_SET1_NAME, ERR_R_MALLOC_FAILURE);
  54. return 0;
  55. }
  56. OPENSSL_free(dhm->name);
  57. dhm->name = tmpname;
  58. return 1;
  59. }
  60. int DH_meth_get_flags(const DH_METHOD *dhm)
  61. {
  62. return dhm->flags;
  63. }
  64. int DH_meth_set_flags(DH_METHOD *dhm, int flags)
  65. {
  66. dhm->flags = flags;
  67. return 1;
  68. }
  69. void *DH_meth_get0_app_data(const DH_METHOD *dhm)
  70. {
  71. return dhm->app_data;
  72. }
  73. int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data)
  74. {
  75. dhm->app_data = app_data;
  76. return 1;
  77. }
  78. int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *)
  79. {
  80. return dhm->generate_key;
  81. }
  82. int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *))
  83. {
  84. dhm->generate_key = generate_key;
  85. return 1;
  86. }
  87. int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
  88. (unsigned char *key, const BIGNUM *pub_key, DH *dh)
  89. {
  90. return dhm->compute_key;
  91. }
  92. int DH_meth_set_compute_key(DH_METHOD *dhm,
  93. int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh))
  94. {
  95. dhm->compute_key = compute_key;
  96. return 1;
  97. }
  98. int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
  99. (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
  100. BN_CTX *, BN_MONT_CTX *)
  101. {
  102. return dhm->bn_mod_exp;
  103. }
  104. int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
  105. int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  106. const BIGNUM *, BN_CTX *, BN_MONT_CTX *))
  107. {
  108. dhm->bn_mod_exp = bn_mod_exp;
  109. return 1;
  110. }
  111. int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *)
  112. {
  113. return dhm->init;
  114. }
  115. int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *))
  116. {
  117. dhm->init = init;
  118. return 1;
  119. }
  120. int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *)
  121. {
  122. return dhm->finish;
  123. }
  124. int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *))
  125. {
  126. dhm->finish = finish;
  127. return 1;
  128. }
  129. int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
  130. (DH *, int, int, BN_GENCB *)
  131. {
  132. return dhm->generate_params;
  133. }
  134. int DH_meth_set_generate_params(DH_METHOD *dhm,
  135. int (*generate_params) (DH *, int, int, BN_GENCB *))
  136. {
  137. dhm->generate_params = generate_params;
  138. return 1;
  139. }