encoder_dh.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2019-2020 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. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/err.h>
  15. #include "prov/bio.h" /* ossl_prov_bio_printf() */
  16. #include "prov/implementations.h" /* rsa_keymgmt_functions */
  17. #include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
  18. #include "internal/ffc.h"
  19. #include "crypto/dh.h"
  20. #include "encoder_local.h"
  21. OSSL_FUNC_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
  22. {
  23. return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
  24. }
  25. OSSL_FUNC_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
  26. {
  27. return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
  28. }
  29. OSSL_FUNC_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
  30. {
  31. return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
  32. }
  33. int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
  34. {
  35. const char *type_label = NULL;
  36. const BIGNUM *priv_key = NULL, *pub_key = NULL;
  37. const BIGNUM *p = NULL;
  38. switch (type) {
  39. case dh_print_priv:
  40. type_label = "DH Private-Key";
  41. break;
  42. case dh_print_pub:
  43. type_label = "DH Public-Key";
  44. break;
  45. case dh_print_params:
  46. type_label = "DH Parameters";
  47. break;
  48. }
  49. if (type == dh_print_priv) {
  50. priv_key = DH_get0_priv_key(dh);
  51. if (priv_key == NULL)
  52. goto null_err;
  53. }
  54. if (type == dh_print_priv || type == dh_print_pub) {
  55. pub_key = DH_get0_pub_key(dh);
  56. if (pub_key == NULL)
  57. goto null_err;
  58. }
  59. p = DH_get0_p(dh);
  60. if (p == NULL)
  61. goto null_err;
  62. if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
  63. <= 0)
  64. goto err;
  65. if (priv_key != NULL
  66. && !ossl_prov_print_labeled_bignum(out, "private-key:", priv_key))
  67. goto err;
  68. if (pub_key != NULL
  69. && !ossl_prov_print_labeled_bignum(out, "public-key:", pub_key))
  70. goto err;
  71. if (!ffc_params_prov_print(out, dh_get0_params(dh)))
  72. goto err;
  73. return 1;
  74. err:
  75. return 0;
  76. null_err:
  77. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  78. goto err;
  79. }
  80. int ossl_prov_prepare_dh_params(const void *dh, int nid,
  81. void **pstr, int *pstrtype)
  82. {
  83. ASN1_STRING *params = ASN1_STRING_new();
  84. if (params == NULL) {
  85. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  86. return 0;
  87. }
  88. if (nid == EVP_PKEY_DHX)
  89. params->length = i2d_DHxparams(dh, &params->data);
  90. else
  91. params->length = i2d_DHparams(dh, &params->data);
  92. if (params->length <= 0) {
  93. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  94. ASN1_STRING_free(params);
  95. return 0;
  96. }
  97. params->type = V_ASN1_SEQUENCE;
  98. *pstr = params;
  99. *pstrtype = V_ASN1_SEQUENCE;
  100. return 1;
  101. }
  102. int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
  103. {
  104. const BIGNUM *bn = NULL;
  105. ASN1_INTEGER *pub_key = NULL;
  106. int ret;
  107. if ((bn = DH_get0_pub_key(dh)) == NULL) {
  108. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  109. return 0;
  110. }
  111. if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  112. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  113. return 0;
  114. }
  115. ret = i2d_ASN1_INTEGER(pub_key, pder);
  116. ASN1_STRING_clear_free(pub_key);
  117. return ret;
  118. }
  119. int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
  120. {
  121. const BIGNUM *bn = NULL;
  122. ASN1_INTEGER *priv_key = NULL;
  123. int ret;
  124. if ((bn = DH_get0_priv_key(dh)) == NULL) {
  125. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  126. return 0;
  127. }
  128. if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  129. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  130. return 0;
  131. }
  132. ret = i2d_ASN1_INTEGER(priv_key, pder);
  133. ASN1_STRING_clear_free(priv_key);
  134. return ret;
  135. }
  136. int ossl_prov_dh_type_to_evp(const DH *dh)
  137. {
  138. return DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
  139. }