dh_asn1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2000-2021 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 <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include "dh_local.h"
  18. #include <openssl/objects.h>
  19. #include <openssl/asn1t.h>
  20. #include "crypto/dh.h"
  21. /* Override the default free and new methods */
  22. static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  23. void *exarg)
  24. {
  25. if (operation == ASN1_OP_NEW_PRE) {
  26. *pval = (ASN1_VALUE *)DH_new();
  27. if (*pval != NULL)
  28. return 2;
  29. return 0;
  30. } else if (operation == ASN1_OP_FREE_PRE) {
  31. DH_free((DH *)*pval);
  32. *pval = NULL;
  33. return 2;
  34. } else if (operation == ASN1_OP_D2I_POST) {
  35. DH *dh = (DH *)*pval;
  36. DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
  37. DH_set_flags(dh, DH_FLAG_TYPE_DH);
  38. ossl_dh_cache_named_group(dh);
  39. dh->dirty_cnt++;
  40. }
  41. return 1;
  42. }
  43. ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
  44. ASN1_SIMPLE(DH, params.p, BIGNUM),
  45. ASN1_SIMPLE(DH, params.g, BIGNUM),
  46. ASN1_OPT_EMBED(DH, length, ZINT32),
  47. } ASN1_SEQUENCE_END_cb(DH, DHparams)
  48. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
  49. /*
  50. * Internal only structures for handling X9.42 DH: this gets translated to or
  51. * from a DH structure straight away.
  52. */
  53. typedef struct {
  54. ASN1_BIT_STRING *seed;
  55. BIGNUM *counter;
  56. } int_dhvparams;
  57. typedef struct {
  58. BIGNUM *p;
  59. BIGNUM *q;
  60. BIGNUM *g;
  61. BIGNUM *j;
  62. int_dhvparams *vparams;
  63. } int_dhx942_dh;
  64. ASN1_SEQUENCE(DHvparams) = {
  65. ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
  66. ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
  67. } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
  68. ASN1_SEQUENCE(DHxparams) = {
  69. ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
  70. ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
  71. ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
  72. ASN1_OPT(int_dhx942_dh, j, BIGNUM),
  73. ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
  74. } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
  75. int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
  76. const unsigned char **pp, long length);
  77. int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
  78. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
  79. DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
  80. {
  81. FFC_PARAMS *params;
  82. int_dhx942_dh *dhx = NULL;
  83. DH *dh = NULL;
  84. dh = DH_new();
  85. if (dh == NULL)
  86. return NULL;
  87. dhx = d2i_int_dhx(NULL, pp, length);
  88. if (dhx == NULL) {
  89. DH_free(dh);
  90. return NULL;
  91. }
  92. if (a != NULL) {
  93. DH_free(*a);
  94. *a = dh;
  95. }
  96. params = &dh->params;
  97. DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
  98. ossl_ffc_params_set0_j(params, dhx->j);
  99. if (dhx->vparams != NULL) {
  100. /* The counter has a maximum value of 4 * numbits(p) - 1 */
  101. size_t counter = (size_t)BN_get_word(dhx->vparams->counter);
  102. ossl_ffc_params_set_validate_params(params, dhx->vparams->seed->data,
  103. dhx->vparams->seed->length,
  104. counter);
  105. ASN1_BIT_STRING_free(dhx->vparams->seed);
  106. BN_free(dhx->vparams->counter);
  107. OPENSSL_free(dhx->vparams);
  108. dhx->vparams = NULL;
  109. }
  110. OPENSSL_free(dhx);
  111. DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
  112. DH_set_flags(dh, DH_FLAG_TYPE_DHX);
  113. return dh;
  114. }
  115. int i2d_DHxparams(const DH *dh, unsigned char **pp)
  116. {
  117. int ret = 0;
  118. int_dhx942_dh dhx;
  119. int_dhvparams dhv = { NULL, NULL };
  120. ASN1_BIT_STRING seed;
  121. size_t seedlen = 0;
  122. const FFC_PARAMS *params = &dh->params;
  123. int counter;
  124. ossl_ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
  125. (const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
  126. dhx.j = params->j;
  127. ossl_ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
  128. seed.length = (int)seedlen;
  129. if (counter != -1 && seed.data != NULL && seed.length > 0) {
  130. seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
  131. dhv.seed = &seed;
  132. dhv.counter = BN_new();
  133. if (dhv.counter == NULL)
  134. return 0;
  135. if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
  136. goto err;
  137. dhx.vparams = &dhv;
  138. } else {
  139. dhx.vparams = NULL;
  140. }
  141. ret = i2d_int_dhx(&dhx, pp);
  142. err:
  143. BN_free(dhv.counter);
  144. return ret;
  145. }