Codebase list openssl / 9ba9d81
test/dhtest.c: fix resource leak Reported by Coverity Scan (CID 1439136) [extended tests] Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/7155) Dr. Matthias St. Pierre authored 5 years ago Matt Caswell committed 5 years ago
1 changed file(s) with 40 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
2525
2626 static int dh_test(void)
2727 {
28 DH *dh;
29 BIGNUM *p, *q, *g;
28 DH *dh = NULL;
29 BIGNUM *p = NULL, *q = NULL, *g = NULL;
3030 const BIGNUM *p2, *q2, *g2;
31 BIGNUM *priv_key;
31 BIGNUM *priv_key = NULL;
3232 const BIGNUM *pub_key2, *priv_key2;
3333 BN_GENCB *_cb = NULL;
3434 DH *a = NULL;
4848 || !TEST_ptr(q = BN_new())
4949 || !TEST_ptr(g = BN_new())
5050 || !TEST_ptr(priv_key = BN_new()))
51 goto err;
51 goto err1;
5252
5353 /*
5454 * I) basic tests
5959 || !TEST_true(BN_set_word(q, 2039L))
6060 || !TEST_true(BN_set_word(g, 3L))
6161 || !TEST_true(DH_set0_pqg(dh, p, q, g)))
62 goto err;
62 goto err1;
6363
6464 /* test the combined getter for p, q, and g */
6565 DH_get0_pqg(dh, &p2, &q2, &g2);
6666 if (!TEST_ptr_eq(p2, p)
6767 || !TEST_ptr_eq(q2, q)
6868 || !TEST_ptr_eq(g2, g))
69 goto err;
69 goto err2;
7070
7171 /* test the simple getters for p, q, and g */
7272 if (!TEST_ptr_eq(DH_get0_p(dh), p2)
7373 || !TEST_ptr_eq(DH_get0_q(dh), q2)
7474 || !TEST_ptr_eq(DH_get0_g(dh), g2))
75 goto err;
75 goto err2;
7676
7777 /* set the private key only*/
7878 if (!TEST_true(BN_set_word(priv_key, 1234L))
7979 || !TEST_true(DH_set0_key(dh, NULL, priv_key)))
80 goto err;
80 goto err2;
8181
8282 /* test the combined getter for pub_key and priv_key */
8383 DH_get0_key(dh, &pub_key2, &priv_key2);
8484 if (!TEST_ptr_eq(pub_key2, NULL)
8585 || !TEST_ptr_eq(priv_key2, priv_key))
86 goto err;
86 goto err3;
8787
8888 /* test the simple getters for pub_key and priv_key */
8989 if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)
9090 || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))
91 goto err;
91 goto err3;
9292
9393 /* now generate a key pair ... */
9494 if (!DH_generate_key(dh))
95 goto err;
95 goto err3;
9696
9797 /* ... and check whether the private key was reused: */
9898
100100 DH_get0_key(dh, &pub_key2, &priv_key2);
101101 if (!TEST_ptr(pub_key2)
102102 || !TEST_ptr_eq(priv_key2, priv_key))
103 goto err;
103 goto err3;
104104
105105 /* test it the simple getters for pub_key and priv_key */
106106 if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)
107107 || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))
108 goto err;
109
110 /* check whether the public key was calculated correclty */
108 goto err3;
109
110 /* check whether the public key was calculated correctly */
111111 TEST_uint_eq(BN_get_word(pub_key2), 3331L);
112112
113113 /*
116116
117117 /* generate a DH group ... */
118118 if (!TEST_ptr(_cb = BN_GENCB_new()))
119 goto err;
119 goto err3;
120120 BN_GENCB_set(_cb, &cb, NULL);
121121 if (!TEST_ptr(a = DH_new())
122122 || !TEST_true(DH_generate_parameters_ex(a, 64,
123123 DH_GENERATOR_5, _cb)))
124 goto err;
124 goto err3;
125125
126126 /* ... and check whether it is valid */
127127 if (!DH_check(a, &i))
128 goto err;
128 goto err3;
129129 if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
130130 || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
131131 || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
132132 || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR))
133 goto err;
133 goto err3;
134134
135135 DH_get0_pqg(a, &ap, NULL, &ag);
136136
137137 /* now create another copy of the DH group for the peer */
138138 if (!TEST_ptr(b = DH_new()))
139 goto err;
139 goto err3;
140140
141141 if (!TEST_ptr(bp = BN_dup(ap))
142142 || !TEST_ptr(bg = BN_dup(ag))
143143 || !TEST_true(DH_set0_pqg(b, bp, NULL, bg)))
144 goto err;
144 goto err3;
145145 bp = bg = NULL;
146146
147147 /*
149149 */
150150
151151 if (!DH_generate_key(a))
152 goto err;
152 goto err3;
153153 DH_get0_key(a, &apub_key, NULL);
154154
155155 if (!DH_generate_key(b))
156 goto err;
156 goto err3;
157157 DH_get0_key(b, &bpub_key, &bpriv_key);
158158
159159 /* Also test with a private-key-only copy of |b|. */
160160 if (!TEST_ptr(c = DHparams_dup(b))
161161 || !TEST_ptr(cpriv_key = BN_dup(bpriv_key))
162162 || !TEST_true(DH_set0_key(c, NULL, cpriv_key)))
163 goto err;
163 goto err3;
164164 cpriv_key = NULL;
165165
166166 alen = DH_size(a);
167167 if (!TEST_ptr(abuf = OPENSSL_malloc(alen))
168168 || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))
169 goto err;
169 goto err3;
170170
171171 blen = DH_size(b);
172172 if (!TEST_ptr(bbuf = OPENSSL_malloc(blen))
173173 || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))
174 goto err;
174 goto err3;
175175
176176 clen = DH_size(c);
177177 if (!TEST_ptr(cbuf = OPENSSL_malloc(clen))
178178 || !TEST_true((cout = DH_compute_key(cbuf, apub_key, c)) != -1))
179 goto err;
179 goto err3;
180180
181181 if (!TEST_true(aout >= 4)
182182 || !TEST_mem_eq(abuf, aout, bbuf, bout)
183183 || !TEST_mem_eq(abuf, aout, cbuf, cout))
184 goto err;
184 goto err3;
185185
186186 ret = 1;
187
188 err:
187 goto success;
188
189 err1:
190 /* an error occurred before p,q,g were assigned to dh */
191 BN_free(p);
192 BN_free(q);
193 BN_free(g);
194 err2:
195 /* an error occured before priv_key was assigned to dh */
196 BN_free(priv_key);
197 err3:
198 success:
189199 OPENSSL_free(abuf);
190200 OPENSSL_free(bbuf);
191201 OPENSSL_free(cbuf);