dh_asn1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2000-2016 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include "dh_local.h"
  13. #include <openssl/objects.h>
  14. #include <openssl/asn1t.h>
  15. /* Override the default free and new methods */
  16. static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  17. void *exarg)
  18. {
  19. if (operation == ASN1_OP_NEW_PRE) {
  20. *pval = (ASN1_VALUE *)DH_new();
  21. if (*pval != NULL)
  22. return 2;
  23. return 0;
  24. } else if (operation == ASN1_OP_FREE_PRE) {
  25. DH_free((DH *)*pval);
  26. *pval = NULL;
  27. return 2;
  28. } else if (operation == ASN1_OP_D2I_POST) {
  29. ((DH *)*pval)->dirty_cnt++;
  30. }
  31. return 1;
  32. }
  33. ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
  34. ASN1_SIMPLE(DH, p, BIGNUM),
  35. ASN1_SIMPLE(DH, g, BIGNUM),
  36. ASN1_OPT_EMBED(DH, length, ZINT32),
  37. } ASN1_SEQUENCE_END_cb(DH, DHparams)
  38. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
  39. /*
  40. * Internal only structures for handling X9.42 DH: this gets translated to or
  41. * from a DH structure straight away.
  42. */
  43. typedef struct {
  44. ASN1_BIT_STRING *seed;
  45. BIGNUM *counter;
  46. } int_dhvparams;
  47. typedef struct {
  48. BIGNUM *p;
  49. BIGNUM *q;
  50. BIGNUM *g;
  51. BIGNUM *j;
  52. int_dhvparams *vparams;
  53. } int_dhx942_dh;
  54. ASN1_SEQUENCE(DHvparams) = {
  55. ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
  56. ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
  57. } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
  58. ASN1_SEQUENCE(DHxparams) = {
  59. ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
  60. ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
  61. ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
  62. ASN1_OPT(int_dhx942_dh, j, BIGNUM),
  63. ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
  64. } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
  65. int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
  66. const unsigned char **pp, long length);
  67. int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
  68. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
  69. /* Application public function: read in X9.42 DH parameters into DH structure */
  70. DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
  71. {
  72. int_dhx942_dh *dhx = NULL;
  73. DH *dh = NULL;
  74. dh = DH_new();
  75. if (dh == NULL)
  76. return NULL;
  77. dhx = d2i_int_dhx(NULL, pp, length);
  78. if (dhx == NULL) {
  79. DH_free(dh);
  80. return NULL;
  81. }
  82. if (a) {
  83. DH_free(*a);
  84. *a = dh;
  85. }
  86. dh->p = dhx->p;
  87. dh->q = dhx->q;
  88. dh->g = dhx->g;
  89. dh->j = dhx->j;
  90. if (dhx->vparams) {
  91. dh->seed = dhx->vparams->seed->data;
  92. dh->seedlen = dhx->vparams->seed->length;
  93. dh->counter = dhx->vparams->counter;
  94. dhx->vparams->seed->data = NULL;
  95. ASN1_BIT_STRING_free(dhx->vparams->seed);
  96. OPENSSL_free(dhx->vparams);
  97. dhx->vparams = NULL;
  98. }
  99. OPENSSL_free(dhx);
  100. return dh;
  101. }
  102. int i2d_DHxparams(const DH *dh, unsigned char **pp)
  103. {
  104. int_dhx942_dh dhx;
  105. int_dhvparams dhv;
  106. ASN1_BIT_STRING bs;
  107. dhx.p = dh->p;
  108. dhx.g = dh->g;
  109. dhx.q = dh->q;
  110. dhx.j = dh->j;
  111. if (dh->counter && dh->seed && dh->seedlen > 0) {
  112. bs.flags = ASN1_STRING_FLAG_BITS_LEFT;
  113. bs.data = dh->seed;
  114. bs.length = dh->seedlen;
  115. dhv.seed = &bs;
  116. dhv.counter = dh->counter;
  117. dhx.vparams = &dhv;
  118. } else
  119. dhx.vparams = NULL;
  120. return i2d_int_dhx(&dhx, pp);
  121. }