dh_asn1.c 4.5 KB

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