| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/kexdhc.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: kexdhc.c,v 1.22 2018/02/07 02:06:51 jsing 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 <openssl/dh.h> | - | ||||||
| 33 | - | |||||||
| 34 | #include <stdarg.h> | - | ||||||
| 35 | #include <stdio.h> | - | ||||||
| 36 | #include <string.h> | - | ||||||
| 37 | #include <signal.h> | - | ||||||
| 38 | - | |||||||
| 39 | #include "openbsd-compat/openssl-compat.h" | - | ||||||
| 40 | - | |||||||
| 41 | #include "sshkey.h" | - | ||||||
| 42 | #include "cipher.h" | - | ||||||
| 43 | #include "digest.h" | - | ||||||
| 44 | #include "kex.h" | - | ||||||
| 45 | #include "log.h" | - | ||||||
| 46 | #include "packet.h" | - | ||||||
| 47 | #include "dh.h" | - | ||||||
| 48 | #include "ssh2.h" | - | ||||||
| 49 | #include "dispatch.h" | - | ||||||
| 50 | #include "compat.h" | - | ||||||
| 51 | #include "ssherr.h" | - | ||||||
| 52 | #include "sshbuf.h" | - | ||||||
| 53 | - | |||||||
| 54 | static int input_kex_dh(int, u_int32_t, struct ssh *); | - | ||||||
| 55 | - | |||||||
| 56 | int | - | ||||||
| 57 | kexdh_client(struct ssh *ssh) | - | ||||||
| 58 | { | - | ||||||
| 59 | struct kex *kex = ssh->kex; | - | ||||||
| 60 | int r; | - | ||||||
| 61 | const BIGNUM *pub_key; | - | ||||||
| 62 | - | |||||||
| 63 | /* generate and send 'e', client DH public key */ | - | ||||||
| 64 | switch (kex->kex_type) { | - | ||||||
| 65 | case KEX_DH_GRP1_SHA1: executed 20 times by 1 test: case KEX_DH_GRP1_SHA1:Executed by:
| 20 | ||||||
| 66 | kex->dh = dh_new_group1(); | - | ||||||
| 67 | break; executed 20 times by 1 test: break;Executed by:
| 20 | ||||||
| 68 | case KEX_DH_GRP14_SHA1: executed 20 times by 1 test: case KEX_DH_GRP14_SHA1:Executed by:
| 20 | ||||||
| 69 | case KEX_DH_GRP14_SHA256: never executed: case KEX_DH_GRP14_SHA256: | 0 | ||||||
| 70 | kex->dh = dh_new_group14(); | - | ||||||
| 71 | break; executed 20 times by 1 test: break;Executed by:
| 20 | ||||||
| 72 | case KEX_DH_GRP16_SHA512: never executed: case KEX_DH_GRP16_SHA512: | 0 | ||||||
| 73 | kex->dh = dh_new_group16(); | - | ||||||
| 74 | break; never executed: break; | 0 | ||||||
| 75 | case KEX_DH_GRP18_SHA512: never executed: case KEX_DH_GRP18_SHA512: | 0 | ||||||
| 76 | kex->dh = dh_new_group18(); | - | ||||||
| 77 | break; never executed: break; | 0 | ||||||
| 78 | default: never executed: default: | 0 | ||||||
| 79 | r = SSH_ERR_INVALID_ARGUMENT; | - | ||||||
| 80 | goto out; never executed: goto out; | 0 | ||||||
| 81 | } | - | ||||||
| 82 | if (kex->dh == NULL) {
| 0-40 | ||||||
| 83 | r = SSH_ERR_ALLOC_FAIL; | - | ||||||
| 84 | goto out; never executed: goto out; | 0 | ||||||
| 85 | } | - | ||||||
| 86 | debug("sending SSH2_MSG_KEXDH_INIT"); | - | ||||||
| 87 | if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
| 0-40 | ||||||
| 88 | goto out; never executed: goto out; | 0 | ||||||
| 89 | DH_get0_key(kex->dh, &pub_key, NULL); | - | ||||||
| 90 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
| 0-40 | ||||||
| 91 | (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
| 0-40 | ||||||
| 92 | (r = sshpkt_send(ssh)) != 0)
| 0-40 | ||||||
| 93 | goto out; never executed: goto out; | 0 | ||||||
| 94 | #ifdef DEBUG_KEXDH | - | ||||||
| 95 | DHparams_print_fp(stderr, kex->dh); | - | ||||||
| 96 | fprintf(stderr, "pub= "); | - | ||||||
| 97 | BN_print_fp(stderr, pub_key); | - | ||||||
| 98 | fprintf(stderr, "\n"); | - | ||||||
| 99 | #endif | - | ||||||
| 100 | debug("expecting SSH2_MSG_KEXDH_REPLY"); | - | ||||||
| 101 | ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh); | - | ||||||
| 102 | r = 0; | - | ||||||
| 103 | out: code before this statement executed 40 times by 1 test: out:Executed by:
| 40 | ||||||
| 104 | return r; executed 40 times by 1 test: return r;Executed by:
| 40 | ||||||
| 105 | } | - | ||||||
| 106 | - | |||||||
| 107 | static int | - | ||||||
| 108 | input_kex_dh(int type, u_int32_t seq, struct ssh *ssh) | - | ||||||
| 109 | { | - | ||||||
| 110 | struct kex *kex = ssh->kex; | - | ||||||
| 111 | BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; | - | ||||||
| 112 | const BIGNUM *pub_key; | - | ||||||
| 113 | struct sshkey *server_host_key = NULL; | - | ||||||
| 114 | u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL; | - | ||||||
| 115 | u_char hash[SSH_DIGEST_MAX_LENGTH]; | - | ||||||
| 116 | size_t klen = 0, slen, sbloblen, hashlen; | - | ||||||
| 117 | int kout, r; | - | ||||||
| 118 | - | |||||||
| 119 | if (kex->verify_host_key == NULL) {
| 0-40 | ||||||
| 120 | r = SSH_ERR_INVALID_ARGUMENT; | - | ||||||
| 121 | goto out; never executed: goto out; | 0 | ||||||
| 122 | } | - | ||||||
| 123 | /* key, cert */ | - | ||||||
| 124 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
| 0-40 | ||||||
| 125 | &sbloblen)) != 0 ||
| 0-40 | ||||||
| 126 | (r = sshkey_from_blob(server_host_key_blob, sbloblen,
| 0-40 | ||||||
| 127 | &server_host_key)) != 0)
| 0-40 | ||||||
| 128 | goto out; never executed: goto out; | 0 | ||||||
| 129 | if (server_host_key->type != kex->hostkey_type ||
| 0-40 | ||||||
| 130 | (kex->hostkey_type == KEY_ECDSA &&
| 10-30 | ||||||
| 131 | server_host_key->ecdsa_nid != kex->hostkey_nid)) {
| 0-10 | ||||||
| 132 | r = SSH_ERR_KEY_TYPE_MISMATCH; | - | ||||||
| 133 | goto out; never executed: goto out; | 0 | ||||||
| 134 | } | - | ||||||
| 135 | if (kex->verify_host_key(server_host_key, ssh) == -1) {
| 0-40 | ||||||
| 136 | r = SSH_ERR_SIGNATURE_INVALID; | - | ||||||
| 137 | goto out; never executed: goto out; | 0 | ||||||
| 138 | } | - | ||||||
| 139 | /* DH parameter f, server public DH key */ | - | ||||||
| 140 | if ((dh_server_pub = BN_new()) == NULL) {
| 0-40 | ||||||
| 141 | r = SSH_ERR_ALLOC_FAIL; | - | ||||||
| 142 | goto out; never executed: goto out; | 0 | ||||||
| 143 | } | - | ||||||
| 144 | /* signed H */ | - | ||||||
| 145 | if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
| 0-40 | ||||||
| 146 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
| 0-40 | ||||||
| 147 | (r = sshpkt_get_end(ssh)) != 0)
| 0-40 | ||||||
| 148 | goto out; never executed: goto out; | 0 | ||||||
| 149 | #ifdef DEBUG_KEXDH | - | ||||||
| 150 | fprintf(stderr, "dh_server_pub= "); | - | ||||||
| 151 | BN_print_fp(stderr, dh_server_pub); | - | ||||||
| 152 | fprintf(stderr, "\n"); | - | ||||||
| 153 | debug("bits %d", BN_num_bits(dh_server_pub)); | - | ||||||
| 154 | #endif | - | ||||||
| 155 | if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
| 0-40 | ||||||
| 156 | sshpkt_disconnect(ssh, "bad server public DH value"); | - | ||||||
| 157 | r = SSH_ERR_MESSAGE_INCOMPLETE; | - | ||||||
| 158 | goto out; never executed: goto out; | 0 | ||||||
| 159 | } | - | ||||||
| 160 | - | |||||||
| 161 | klen = DH_size(kex->dh); | - | ||||||
| 162 | if ((kbuf = malloc(klen)) == NULL ||
| 0-40 | ||||||
| 163 | (shared_secret = BN_new()) == NULL) {
| 0-40 | ||||||
| 164 | r = SSH_ERR_ALLOC_FAIL; | - | ||||||
| 165 | goto out; never executed: goto out; | 0 | ||||||
| 166 | } | - | ||||||
| 167 | if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
| 0-40 | ||||||
| 168 | BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
| 0-40 | ||||||
| 169 | r = SSH_ERR_LIBCRYPTO_ERROR; | - | ||||||
| 170 | goto out; never executed: goto out; | 0 | ||||||
| 171 | } | - | ||||||
| 172 | #ifdef DEBUG_KEXDH | - | ||||||
| 173 | dump_digest("shared secret", kbuf, kout); | - | ||||||
| 174 | #endif | - | ||||||
| 175 | - | |||||||
| 176 | /* calc and verify H */ | - | ||||||
| 177 | DH_get0_key(kex->dh, &pub_key, NULL); | - | ||||||
| 178 | hashlen = sizeof(hash); | - | ||||||
| 179 | if ((r = kex_dh_hash(
| 0-40 | ||||||
| 180 | kex->hash_alg,
| 0-40 | ||||||
| 181 | kex->client_version_string,
| 0-40 | ||||||
| 182 | kex->server_version_string,
| 0-40 | ||||||
| 183 | sshbuf_ptr(kex->my), sshbuf_len(kex->my),
| 0-40 | ||||||
| 184 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
| 0-40 | ||||||
| 185 | server_host_key_blob, sbloblen,
| 0-40 | ||||||
| 186 | pub_key,
| 0-40 | ||||||
| 187 | dh_server_pub,
| 0-40 | ||||||
| 188 | shared_secret,
| 0-40 | ||||||
| 189 | hash, &hashlen)) != 0)
| 0-40 | ||||||
| 190 | goto out; never executed: goto out; | 0 | ||||||
| 191 | - | |||||||
| 192 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
| 0-40 | ||||||
| 193 | kex->hostkey_alg, ssh->compat)) != 0)
| 0-40 | ||||||
| 194 | goto out; never executed: goto out; | 0 | ||||||
| 195 | - | |||||||
| 196 | /* save session id */ | - | ||||||
| 197 | if (kex->session_id == NULL) {
| 8-32 | ||||||
| 198 | kex->session_id_len = hashlen; | - | ||||||
| 199 | kex->session_id = malloc(kex->session_id_len); | - | ||||||
| 200 | if (kex->session_id == NULL) {
| 0-8 | ||||||
| 201 | r = SSH_ERR_ALLOC_FAIL; | - | ||||||
| 202 | goto out; never executed: goto out; | 0 | ||||||
| 203 | } | - | ||||||
| 204 | memcpy(kex->session_id, hash, kex->session_id_len); | - | ||||||
| 205 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||
| 206 | - | |||||||
| 207 | if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
| 0-40 | ||||||
| 208 | r = kex_send_newkeys(ssh); executed 40 times by 1 test: r = kex_send_newkeys(ssh);Executed by:
| 40 | ||||||
| 209 | out: code before this statement executed 40 times by 1 test: out:Executed by:
| 40 | ||||||
| 210 | explicit_bzero(hash, sizeof(hash)); | - | ||||||
| 211 | DH_free(kex->dh); | - | ||||||
| 212 | kex->dh = NULL; | - | ||||||
| 213 | BN_clear_free(dh_server_pub); | - | ||||||
| 214 | if (kbuf) {
| 0-40 | ||||||
| 215 | explicit_bzero(kbuf, klen); | - | ||||||
| 216 | free(kbuf); | - | ||||||
| 217 | } executed 40 times by 1 test: end of blockExecuted by:
| 40 | ||||||
| 218 | BN_clear_free(shared_secret); | - | ||||||
| 219 | sshkey_free(server_host_key); | - | ||||||
| 220 | free(server_host_key_blob); | - | ||||||
| 221 | free(signature); | - | ||||||
| 222 | return r; executed 40 times by 1 test: return r;Executed by:
| 40 | ||||||
| 223 | } | - | ||||||
| 224 | #endif /* WITH_OPENSSL */ | - | ||||||
| Source code | Switch to Preprocessed file |