/src/openssl/crypto/dh/dh_asn1.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/bn.h> |
13 | | #include "dh_locl.h" |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/asn1t.h> |
16 | | |
17 | | /* Override the default free and new methods */ |
18 | | static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
19 | | void *exarg) |
20 | 0 | { |
21 | 0 | if (operation == ASN1_OP_NEW_PRE) { |
22 | 0 | *pval = (ASN1_VALUE *)DH_new(); |
23 | 0 | if (*pval != NULL) |
24 | 0 | return 2; |
25 | 0 | return 0; |
26 | 0 | } else if (operation == ASN1_OP_FREE_PRE) { |
27 | 0 | DH_free((DH *)*pval); |
28 | 0 | *pval = NULL; |
29 | 0 | return 2; |
30 | 0 | } |
31 | 0 | return 1; |
32 | 0 | } |
33 | | |
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) |
39 | | |
40 | | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DH, DHparams, DHparams) |
41 | | |
42 | | /* |
43 | | * Internal only structures for handling X9.42 DH: this gets translated to or |
44 | | * from a DH structure straight away. |
45 | | */ |
46 | | |
47 | | typedef struct { |
48 | | ASN1_BIT_STRING *seed; |
49 | | BIGNUM *counter; |
50 | | } int_dhvparams; |
51 | | |
52 | | typedef struct { |
53 | | BIGNUM *p; |
54 | | BIGNUM *q; |
55 | | BIGNUM *g; |
56 | | BIGNUM *j; |
57 | | int_dhvparams *vparams; |
58 | | } int_dhx942_dh; |
59 | | |
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) |
64 | | |
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) |
72 | | |
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); |
76 | | |
77 | | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(int_dhx942_dh, DHxparams, int_dhx) |
78 | | |
79 | | /* Application public function: read in X9.42 DH parameters into DH structure */ |
80 | | |
81 | | DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length) |
82 | 0 | { |
83 | 0 | int_dhx942_dh *dhx = NULL; |
84 | 0 | DH *dh = NULL; |
85 | 0 | dh = DH_new(); |
86 | 0 | if (dh == NULL) |
87 | 0 | return NULL; |
88 | 0 | dhx = d2i_int_dhx(NULL, pp, length); |
89 | 0 | if (dhx == NULL) { |
90 | 0 | DH_free(dh); |
91 | 0 | return NULL; |
92 | 0 | } |
93 | 0 | |
94 | 0 | if (a) { |
95 | 0 | DH_free(*a); |
96 | 0 | *a = dh; |
97 | 0 | } |
98 | 0 |
|
99 | 0 | dh->p = dhx->p; |
100 | 0 | dh->q = dhx->q; |
101 | 0 | dh->g = dhx->g; |
102 | 0 | dh->j = dhx->j; |
103 | 0 |
|
104 | 0 | if (dhx->vparams) { |
105 | 0 | dh->seed = dhx->vparams->seed->data; |
106 | 0 | dh->seedlen = dhx->vparams->seed->length; |
107 | 0 | dh->counter = dhx->vparams->counter; |
108 | 0 | dhx->vparams->seed->data = NULL; |
109 | 0 | ASN1_BIT_STRING_free(dhx->vparams->seed); |
110 | 0 | OPENSSL_free(dhx->vparams); |
111 | 0 | dhx->vparams = NULL; |
112 | 0 | } |
113 | 0 |
|
114 | 0 | OPENSSL_free(dhx); |
115 | 0 | return dh; |
116 | 0 | } |
117 | | |
118 | | int i2d_DHxparams(const DH *dh, unsigned char **pp) |
119 | 0 | { |
120 | 0 | int_dhx942_dh dhx; |
121 | 0 | int_dhvparams dhv; |
122 | 0 | ASN1_BIT_STRING bs; |
123 | 0 | dhx.p = dh->p; |
124 | 0 | dhx.g = dh->g; |
125 | 0 | dhx.q = dh->q; |
126 | 0 | dhx.j = dh->j; |
127 | 0 | if (dh->counter && dh->seed && dh->seedlen > 0) { |
128 | 0 | bs.flags = ASN1_STRING_FLAG_BITS_LEFT; |
129 | 0 | bs.data = dh->seed; |
130 | 0 | bs.length = dh->seedlen; |
131 | 0 | dhv.seed = &bs; |
132 | 0 | dhv.counter = dh->counter; |
133 | 0 | dhx.vparams = &dhv; |
134 | 0 | } else |
135 | 0 | dhx.vparams = NULL; |
136 | 0 |
|
137 | 0 | return i2d_int_dhx(&dhx, pp); |
138 | 0 | } |