OpenCoverage

kexdhs.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/kexdhs.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: kexdhs.c,v 1.27 2018/04/10 00:10:49 djm Exp $ */-
2/*-
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.-
4 *-
5 * Redistribution and use in source and binary forms, with or without-
6 * modification, are permitted provided that the following conditions-
7 * are met:-
8 * 1. Redistributions of source code must retain the above copyright-
9 * notice, this list of conditions and the following disclaimer.-
10 * 2. Redistributions in binary form must reproduce the above copyright-
11 * notice, this list of conditions and the following disclaimer in the-
12 * documentation and/or other materials provided with the distribution.-
13 *-
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR-
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES-
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.-
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,-
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT-
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,-
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY-
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT-
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF-
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.-
24 */-
25-
26#include "includes.h"-
27-
28#ifdef WITH_OPENSSL-
29-
30#include <sys/types.h>-
31-
32#include <stdarg.h>-
33#include <string.h>-
34#include <signal.h>-
35-
36#include <openssl/dh.h>-
37-
38#include "openbsd-compat/openssl-compat.h"-
39-
40#include "sshkey.h"-
41#include "cipher.h"-
42#include "digest.h"-
43#include "kex.h"-
44#include "log.h"-
45#include "packet.h"-
46#include "dh.h"-
47#include "ssh2.h"-
48-
49#include "dispatch.h"-
50#include "compat.h"-
51#include "ssherr.h"-
52#include "sshbuf.h"-
53-
54static int input_kex_dh_init(int, u_int32_t, struct ssh *);-
55-
56int-
57kexdh_server(struct ssh *ssh)-
58{-
59 struct kex *kex = ssh->kex;-
60 int r;-
61-
62 /* generate server DH public key */-
63 switch (kex->kex_type) {-
64 case KEX_DH_GRP1_SHA1:
executed 20 times by 1 test: case KEX_DH_GRP1_SHA1:
Executed by:
  • test_kex
20
65 kex->dh = dh_new_group1();-
66 break;
executed 20 times by 1 test: break;
Executed by:
  • test_kex
20
67 case KEX_DH_GRP14_SHA1:
executed 20 times by 1 test: case KEX_DH_GRP14_SHA1:
Executed by:
  • test_kex
20
68 case KEX_DH_GRP14_SHA256:
never executed: case KEX_DH_GRP14_SHA256:
0
69 kex->dh = dh_new_group14();-
70 break;
executed 20 times by 1 test: break;
Executed by:
  • test_kex
20
71 case KEX_DH_GRP16_SHA512:
never executed: case KEX_DH_GRP16_SHA512:
0
72 kex->dh = dh_new_group16();-
73 break;
never executed: break;
0
74 case KEX_DH_GRP18_SHA512:
never executed: case KEX_DH_GRP18_SHA512:
0
75 kex->dh = dh_new_group18();-
76 break;
never executed: break;
0
77 default:
never executed: default:
0
78 r = SSH_ERR_INVALID_ARGUMENT;-
79 goto out;
never executed: goto out;
0
80 }-
81 if (kex->dh == NULL) {
kex->dh == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
82 r = SSH_ERR_ALLOC_FAIL;-
83 goto out;
never executed: goto out;
0
84 }-
85 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
(r = dh_gen_ke...eed * 8)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
86 goto out;
never executed: goto out;
0
87-
88 debug("expecting SSH2_MSG_KEXDH_INIT");-
89 ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);-
90 r = 0;-
91 out:
code before this statement executed 40 times by 1 test: out:
Executed by:
  • test_kex
40
92 return r;
executed 40 times by 1 test: return r;
Executed by:
  • test_kex
40
93}-
94-
95int-
96input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)-
97{-
98 struct kex *kex = ssh->kex;-
99 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;-
100 const BIGNUM *pub_key;-
101 struct sshkey *server_host_public, *server_host_private;-
102 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;-
103 u_char hash[SSH_DIGEST_MAX_LENGTH];-
104 size_t sbloblen, slen;-
105 size_t klen = 0, hashlen;-
106 int kout, r;-
107-
108 if (kex->load_host_public_key == NULL ||
kex->load_host...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
109 kex->load_host_private_key == NULL) {
kex->load_host...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
110 r = SSH_ERR_INVALID_ARGUMENT;-
111 goto out;
never executed: goto out;
0
112 }-
113 server_host_public = kex->load_host_public_key(kex->hostkey_type,-
114 kex->hostkey_nid, ssh);-
115 server_host_private = kex->load_host_private_key(kex->hostkey_type,-
116 kex->hostkey_nid, ssh);-
117 if (server_host_public == NULL) {
server_host_pu...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
118 r = SSH_ERR_NO_HOSTKEY_LOADED;-
119 goto out;
never executed: goto out;
0
120 }-
121-
122 /* key, cert */-
123 if ((dh_client_pub = BN_new()) == NULL) {
(dh_client_pub...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
124 r = SSH_ERR_ALLOC_FAIL;-
125 goto out;
never executed: goto out;
0
126 }-
127 DH_get0_key(kex->dh, &pub_key, NULL);-
128 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
(r = sshpkt_ge...ent_pub)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
129 (r = sshpkt_get_end(ssh)) != 0)
(r = sshpkt_get_end(ssh)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
130 goto out;
never executed: goto out;
0
131-
132#ifdef DEBUG_KEXDH-
133 fprintf(stderr, "dh_client_pub= ");-
134 BN_print_fp(stderr, dh_client_pub);-
135 fprintf(stderr, "\n");-
136 debug("bits %d", BN_num_bits(dh_client_pub));-
137 DHparams_print_fp(stderr, kex->dh);-
138 fprintf(stderr, "pub= ");-
139 BN_print_fp(stderr, pub_key);-
140 fprintf(stderr, "\n");-
141#endif-
142 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
!dh_pub_is_val...dh_client_pub)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
143 sshpkt_disconnect(ssh, "bad client public DH value");-
144 r = SSH_ERR_MESSAGE_INCOMPLETE;-
145 goto out;
never executed: goto out;
0
146 }-
147-
148 klen = DH_size(kex->dh);-
149 if ((kbuf = malloc(klen)) == NULL ||
(kbuf = malloc...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
150 (shared_secret = BN_new()) == NULL) {
(shared_secret...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
151 r = SSH_ERR_ALLOC_FAIL;-
152 goto out;
never executed: goto out;
0
153 }-
154 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
(kout = DH_com... kex->dh)) < 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
155 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
BN_bin2bn(kbuf...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
156 r = SSH_ERR_LIBCRYPTO_ERROR;-
157 goto out;
never executed: goto out;
0
158 }-
159#ifdef DEBUG_KEXDH-
160 dump_digest("shared secret", kbuf, kout);-
161#endif-
162 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
(r = sshkey_to...bloblen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
163 &sbloblen)) != 0)
(r = sshkey_to...bloblen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
164 goto out;
never executed: goto out;
0
165 /* calc H */-
166 hashlen = sizeof(hash);-
167 if ((r = kex_dh_hash(
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
168 kex->hash_alg,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
169 kex->client_version_string,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
170 kex->server_version_string,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
171 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
172 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
173 server_host_key_blob, sbloblen,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
174 dh_client_pub,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
175 pub_key,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
176 shared_secret,
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
177 hash, &hashlen)) != 0)
(r = kex_dh_ha...hashlen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
178 goto out;
never executed: goto out;
0
179-
180 /* save session id := H */-
181 if (kex->session_id == NULL) {
kex->session_id == ((void *)0)Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • test_kex
FALSEevaluated 32 times by 1 test
Evaluated by:
  • test_kex
8-32
182 kex->session_id_len = hashlen;-
183 kex->session_id = malloc(kex->session_id_len);-
184 if (kex->session_id == NULL) {
kex->session_id == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • test_kex
0-8
185 r = SSH_ERR_ALLOC_FAIL;-
186 goto out;
never executed: goto out;
0
187 }-
188 memcpy(kex->session_id, hash, kex->session_id_len);-
189 }
executed 8 times by 1 test: end of block
Executed by:
  • test_kex
8
190-
191 /* sign H */-
192 if ((r = kex->sign(server_host_private, server_host_public, &signature,
(r = kex->sign...->compat)) < 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
193 &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
(r = kex->sign...->compat)) < 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
194 goto out;
never executed: goto out;
0
195-
196 /* destroy_sensitive_data(); */-
197-
198 /* send server hostkey, DH pubkey 'f' and signed H */-
199 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
(r = sshpkt_st...ssh, 31)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
200 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
(r = sshpkt_pu...bloblen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
201 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
(r = sshpkt_pu...pub_key)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
202 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
(r = sshpkt_pu...e, slen)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
203 (r = sshpkt_send(ssh)) != 0)
(r = sshpkt_send(ssh)) != 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
0-40
204 goto out;
never executed: goto out;
0
205-
206 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
(r = kex_deriv..._secret)) == 0Description
TRUEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
0-40
207 r = kex_send_newkeys(ssh);
executed 40 times by 1 test: r = kex_send_newkeys(ssh);
Executed by:
  • test_kex
40
208 out:
code before this statement executed 40 times by 1 test: out:
Executed by:
  • test_kex
40
209 explicit_bzero(hash, sizeof(hash));-
210 DH_free(kex->dh);-
211 kex->dh = NULL;-
212 BN_clear_free(dh_client_pub);-
213 if (kbuf) {
kbufDescription
TRUEevaluated 40 times by 1 test
Evaluated by:
  • test_kex
FALSEnever evaluated
0-40
214 explicit_bzero(kbuf, klen);-
215 free(kbuf);-
216 }
executed 40 times by 1 test: end of block
Executed by:
  • test_kex
40
217 BN_clear_free(shared_secret);-
218 free(server_host_key_blob);-
219 free(signature);-
220 return r;
executed 40 times by 1 test: return r;
Executed by:
  • test_kex
40
221}-
222#endif /* WITH_OPENSSL */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2