dh_asn1.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_locl.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. }
  29. return 1;
  30. }
  31. ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
  32. ASN1_SIMPLE(DH, p, BIGNUM),
  33. ASN1_SIMPLE(DH, g, BIGNUM),
  34. ASN1_OPT(DH, length, ZLONG),
  35. } ASN1_SEQUENCE_END_cb(DH, DHparams)
  36. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DH, DHparams, DHparams)
  37. /*
  38. * Internal only structures for handling X9.42 DH: this gets translated to or
  39. * from a DH structure straight away.
  40. */
  41. typedef struct {
  42. ASN1_BIT_STRING *seed;
  43. BIGNUM *counter;
  44. } int_dhvparams;
  45. typedef struct {
  46. BIGNUM *p;
  47. BIGNUM *q;
  48. BIGNUM *g;
  49. BIGNUM *j;
  50. int_dhvparams *vparams;
  51. } int_dhx942_dh;
  52. ASN1_SEQUENCE(DHvparams) = {
  53. ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
  54. ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
  55. } static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
  56. ASN1_SEQUENCE(DHxparams) = {
  57. ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
  58. ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
  59. ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
  60. ASN1_OPT(int_dhx942_dh, j, BIGNUM),
  61. ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
  62. } static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
  63. int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
  64. const unsigned char **pp, long length);
  65. int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
  66. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(int_dhx942_dh, DHxparams, int_dhx)
  67. /* Application public function: read in X9.42 DH parameters into DH structure */
  68. DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
  69. {
  70. int_dhx942_dh *dhx = NULL;
  71. DH *dh = NULL;
  72. dh = DH_new();
  73. if (dh == NULL)
  74. return NULL;
  75. dhx = d2i_int_dhx(NULL, pp, length);
  76. if (dhx == NULL) {
  77. DH_free(dh);
  78. return NULL;
  79. }
  80. if (a) {
  81. DH_free(*a);
  82. *a = dh;
  83. }
  84. dh->p = dhx->p;
  85. dh->q = dhx->q;
  86. dh->g = dhx->g;
  87. dh->j = dhx->j;
  88. if (dhx->vparams) {
  89. dh->seed = dhx->vparams->seed->data;
  90. dh->seedlen = dhx->vparams->seed->length;
  91. dh->counter = dhx->vparams->counter;
  92. dhx->vparams->seed->data = NULL;
  93. ASN1_BIT_STRING_free(dhx->vparams->seed);
  94. OPENSSL_free(dhx->vparams);
  95. dhx->vparams = NULL;
  96. }
  97. OPENSSL_free(dhx);
  98. return dh;
  99. }
  100. int i2d_DHxparams(const DH *dh, unsigned char **pp)
  101. {
  102. int_dhx942_dh dhx;
  103. int_dhvparams dhv;
  104. ASN1_BIT_STRING bs;
  105. dhx.p = dh->p;
  106. dhx.g = dh->g;
  107. dhx.q = dh->q;
  108. dhx.j = dh->j;
  109. if (dh->counter && dh->seed && dh->seedlen > 0) {
  110. bs.flags = ASN1_STRING_FLAG_BITS_LEFT;
  111. bs.data = dh->seed;
  112. bs.length = dh->seedlen;
  113. dhv.seed = &bs;
  114. dhv.counter = dh->counter;
  115. dhx.vparams = &dhv;
  116. } else
  117. dhx.vparams = NULL;
  118. return i2d_int_dhx(&dhx, pp);
  119. }