2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
14 #include <openssl/objects.h>
15 #include <openssl/asn1t.h>
17 /* Override the default free and new methods */
18 static int dh_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
21 if (operation
== ASN1_OP_NEW_PRE
) {
22 *pval
= (ASN1_VALUE
*)DH_new();
26 } else if (operation
== ASN1_OP_FREE_PRE
) {
34 ASN1_SEQUENCE_cb(DHparams
, dh_cb
) = {
35 ASN1_SIMPLE(DH
, p
, BIGNUM
),
36 ASN1_SIMPLE(DH
, g
, BIGNUM
),
37 ASN1_OPT_EMBED(DH
, length
, ZINT32
),
38 } ASN1_SEQUENCE_END_cb(DH
, DHparams
)
40 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DH
, DHparams
, DHparams
)
43 * Internal only structures for handling X9.42 DH: this gets translated to or
44 * from a DH structure straight away.
48 ASN1_BIT_STRING
*seed
;
57 int_dhvparams
*vparams
;
60 ASN1_SEQUENCE(DHvparams
) = {
61 ASN1_SIMPLE(int_dhvparams
, seed
, ASN1_BIT_STRING
),
62 ASN1_SIMPLE(int_dhvparams
, counter
, BIGNUM
)
63 } static_ASN1_SEQUENCE_END_name(int_dhvparams
, DHvparams
)
65 ASN1_SEQUENCE(DHxparams
) = {
66 ASN1_SIMPLE(int_dhx942_dh
, p
, BIGNUM
),
67 ASN1_SIMPLE(int_dhx942_dh
, g
, BIGNUM
),
68 ASN1_SIMPLE(int_dhx942_dh
, q
, BIGNUM
),
69 ASN1_OPT(int_dhx942_dh
, j
, BIGNUM
),
70 ASN1_OPT(int_dhx942_dh
, vparams
, DHvparams
),
71 } static_ASN1_SEQUENCE_END_name(int_dhx942_dh
, DHxparams
)
73 int_dhx942_dh
*d2i_int_dhx(int_dhx942_dh
**a
,
74 const unsigned char **pp
, long length
);
75 int i2d_int_dhx(const int_dhx942_dh
*a
, unsigned char **pp
);
77 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(int_dhx942_dh
, DHxparams
, int_dhx
)
79 /* Application public function: read in X9.42 DH parameters into DH structure */
81 DH
*d2i_DHxparams(DH
**a
, const unsigned char **pp
, long length
)
83 int_dhx942_dh
*dhx
= NULL
;
88 dhx
= d2i_int_dhx(NULL
, pp
, length
);
105 dh
->seed
= dhx
->vparams
->seed
->data
;
106 dh
->seedlen
= dhx
->vparams
->seed
->length
;
107 dh
->counter
= dhx
->vparams
->counter
;
108 dhx
->vparams
->seed
->data
= NULL
;
109 ASN1_BIT_STRING_free(dhx
->vparams
->seed
);
110 OPENSSL_free(dhx
->vparams
);
118 int i2d_DHxparams(const DH
*dh
, unsigned char **pp
)
127 if (dh
->counter
&& dh
->seed
&& dh
->seedlen
> 0) {
128 bs
.flags
= ASN1_STRING_FLAG_BITS_LEFT
;
130 bs
.length
= dh
->seedlen
;
132 dhv
.counter
= dh
->counter
;
137 return i2d_int_dhx(&dhx
, pp
);