Annotation of src/usr.bin/ssh/kexdhs.c, Revision 1.33
1.33 ! djm 1: /* $OpenBSD: kexdhs.c,v 1.32 2019/01/21 09:55:52 djm Exp $ */
1.1 markus 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:
1.6 stevesk 26:
1.7 deraadt 27: #include <sys/types.h>
1.6 stevesk 28: #include <string.h>
1.7 deraadt 29: #include <signal.h>
1.12 djm 30:
31: #include <openssl/dh.h>
1.1 markus 32:
1.20 markus 33: #include "sshkey.h"
1.7 deraadt 34: #include "cipher.h"
1.20 markus 35: #include "digest.h"
1.33 ! djm 36: #include "dh.h"
1.1 markus 37: #include "kex.h"
38: #include "log.h"
39: #include "packet.h"
40: #include "ssh2.h"
41:
1.20 markus 42: #include "dispatch.h"
43: #include "compat.h"
44: #include "ssherr.h"
45: #include "sshbuf.h"
46:
1.25 markus 47: static int input_kex_dh_init(int, u_int32_t, struct ssh *);
1.20 markus 48:
49: int
50: kexdh_server(struct ssh *ssh)
1.1 markus 51: {
1.20 markus 52: struct kex *kex = ssh->kex;
53: int r;
1.1 markus 54:
55: /* generate server DH public key */
1.33 ! djm 56: if ((r = kex_dh_keygen(kex)) != 0)
! 57: return r;
1.1 markus 58: debug("expecting SSH2_MSG_KEXDH_INIT");
1.20 markus 59: ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
1.33 ! djm 60: return 0;
1.20 markus 61: }
62:
63: int
1.25 markus 64: input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 65: {
66: struct kex *kex = ssh->kex;
67: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
1.28 djm 68: const BIGNUM *pub_key;
1.20 markus 69: struct sshkey *server_host_public, *server_host_private;
70: u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
71: u_char hash[SSH_DIGEST_MAX_LENGTH];
72: size_t sbloblen, slen;
73: size_t klen = 0, hashlen;
74: int kout, r;
1.1 markus 75:
1.11 djm 76: if (kex->load_host_public_key == NULL ||
1.20 markus 77: kex->load_host_private_key == NULL) {
78: r = SSH_ERR_INVALID_ARGUMENT;
79: goto out;
80: }
1.22 djm 81: server_host_public = kex->load_host_public_key(kex->hostkey_type,
82: kex->hostkey_nid, ssh);
83: server_host_private = kex->load_host_private_key(kex->hostkey_type,
84: kex->hostkey_nid, ssh);
1.21 djm 85: if (server_host_public == NULL) {
1.20 markus 86: r = SSH_ERR_NO_HOSTKEY_LOADED;
87: goto out;
88: }
1.1 markus 89:
90: /* key, cert */
1.31 djm 91: if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
92: (r = sshpkt_get_end(ssh)) != 0)
1.20 markus 93: goto out;
1.28 djm 94: DH_get0_key(kex->dh, &pub_key, NULL);
1.1 markus 95:
96: #ifdef DEBUG_KEXDH
97: fprintf(stderr, "dh_client_pub= ");
98: BN_print_fp(stderr, dh_client_pub);
99: fprintf(stderr, "\n");
100: debug("bits %d", BN_num_bits(dh_client_pub));
1.20 markus 101: DHparams_print_fp(stderr, kex->dh);
1.1 markus 102: fprintf(stderr, "pub= ");
1.28 djm 103: BN_print_fp(stderr, pub_key);
1.1 markus 104: fprintf(stderr, "\n");
105: #endif
1.20 markus 106: if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
107: sshpkt_disconnect(ssh, "bad client public DH value");
108: r = SSH_ERR_MESSAGE_INCOMPLETE;
109: goto out;
110: }
1.1 markus 111:
1.20 markus 112: klen = DH_size(kex->dh);
113: if ((kbuf = malloc(klen)) == NULL ||
114: (shared_secret = BN_new()) == NULL) {
115: r = SSH_ERR_ALLOC_FAIL;
116: goto out;
117: }
118: if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
119: BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
120: r = SSH_ERR_LIBCRYPTO_ERROR;
121: goto out;
122: }
1.1 markus 123: #ifdef DEBUG_KEXDH
124: dump_digest("shared secret", kbuf, kout);
125: #endif
1.20 markus 126: if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
127: &sbloblen)) != 0)
128: goto out;
1.1 markus 129: /* calc H */
1.20 markus 130: hashlen = sizeof(hash);
131: if ((r = kex_dh_hash(
1.24 djm 132: kex->hash_alg,
1.29 djm 133: kex->client_version,
134: kex->server_version,
1.20 markus 135: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
136: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1 markus 137: server_host_key_blob, sbloblen,
138: dh_client_pub,
1.28 djm 139: pub_key,
1.3 djm 140: shared_secret,
1.20 markus 141: hash, &hashlen)) != 0)
142: goto out;
1.1 markus 143:
144: /* sign H */
1.30 djm 145: if ((r = kex->sign(ssh, server_host_private, server_host_public,
146: &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
1.20 markus 147: goto out;
1.1 markus 148:
149: /* destroy_sensitive_data(); */
150:
1.27 djm 151: /* send server hostkey, DH pubkey 'f' and signed H */
1.20 markus 152: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
153: (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
1.28 djm 154: (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
1.20 markus 155: (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
156: (r = sshpkt_send(ssh)) != 0)
157: goto out;
158:
159: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
160: r = kex_send_newkeys(ssh);
161: out:
162: explicit_bzero(hash, sizeof(hash));
163: DH_free(kex->dh);
164: kex->dh = NULL;
1.26 jsing 165: BN_clear_free(dh_client_pub);
1.20 markus 166: if (kbuf) {
167: explicit_bzero(kbuf, klen);
168: free(kbuf);
169: }
1.26 jsing 170: BN_clear_free(shared_secret);
1.20 markus 171: free(server_host_key_blob);
1.13 djm 172: free(signature);
1.20 markus 173: return r;
1.1 markus 174: }