Annotation of src/usr.bin/ssh/kexdhs.c, Revision 1.24
1.24 ! djm 1: /* $OpenBSD: kexdhs.c,v 1.23 2015/12/04 16:41:28 markus 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.1 markus 36: #include "kex.h"
37: #include "log.h"
38: #include "packet.h"
39: #include "dh.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:
47: static int input_kex_dh_init(int, u_int32_t, void *);
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.2 djm 56: switch (kex->kex_type) {
57: case KEX_DH_GRP1_SHA1:
1.20 markus 58: kex->dh = dh_new_group1();
1.2 djm 59: break;
60: case KEX_DH_GRP14_SHA1:
1.24 ! djm 61: case KEX_DH_GRP14_SHA256:
1.20 markus 62: kex->dh = dh_new_group14();
1.2 djm 63: break;
1.24 ! djm 64: case KEX_DH_GRP16_SHA512:
! 65: kex->dh = dh_new_group16();
! 66: break;
! 67: case KEX_DH_GRP18_SHA512:
! 68: kex->dh = dh_new_group18();
! 69: break;
1.2 djm 70: default:
1.20 markus 71: r = SSH_ERR_INVALID_ARGUMENT;
72: goto out;
1.2 djm 73: }
1.20 markus 74: if (kex->dh == NULL) {
75: r = SSH_ERR_ALLOC_FAIL;
76: goto out;
77: }
78: if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
79: goto out;
1.1 markus 80:
81: debug("expecting SSH2_MSG_KEXDH_INIT");
1.20 markus 82: ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
83: r = 0;
84: out:
85: return r;
86: }
87:
88: int
89: input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
90: {
91: struct ssh *ssh = ctxt;
92: struct kex *kex = ssh->kex;
93: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
94: struct sshkey *server_host_public, *server_host_private;
95: u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
96: u_char hash[SSH_DIGEST_MAX_LENGTH];
97: size_t sbloblen, slen;
98: size_t klen = 0, hashlen;
99: int kout, r;
1.1 markus 100:
1.11 djm 101: if (kex->load_host_public_key == NULL ||
1.20 markus 102: kex->load_host_private_key == NULL) {
103: r = SSH_ERR_INVALID_ARGUMENT;
104: goto out;
105: }
1.22 djm 106: server_host_public = kex->load_host_public_key(kex->hostkey_type,
107: kex->hostkey_nid, ssh);
108: server_host_private = kex->load_host_private_key(kex->hostkey_type,
109: kex->hostkey_nid, ssh);
1.21 djm 110: if (server_host_public == NULL) {
1.20 markus 111: r = SSH_ERR_NO_HOSTKEY_LOADED;
112: goto out;
113: }
1.1 markus 114:
115: /* key, cert */
1.20 markus 116: if ((dh_client_pub = BN_new()) == NULL) {
117: r = SSH_ERR_ALLOC_FAIL;
118: goto out;
119: }
120: if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
121: (r = sshpkt_get_end(ssh)) != 0)
122: goto out;
1.1 markus 123:
124: #ifdef DEBUG_KEXDH
125: fprintf(stderr, "dh_client_pub= ");
126: BN_print_fp(stderr, dh_client_pub);
127: fprintf(stderr, "\n");
128: debug("bits %d", BN_num_bits(dh_client_pub));
129: #endif
130:
131: #ifdef DEBUG_KEXDH
1.20 markus 132: DHparams_print_fp(stderr, kex->dh);
1.1 markus 133: fprintf(stderr, "pub= ");
1.20 markus 134: BN_print_fp(stderr, kex->dh->pub_key);
1.1 markus 135: fprintf(stderr, "\n");
136: #endif
1.20 markus 137: if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
138: sshpkt_disconnect(ssh, "bad client public DH value");
139: r = SSH_ERR_MESSAGE_INCOMPLETE;
140: goto out;
141: }
1.1 markus 142:
1.20 markus 143: klen = DH_size(kex->dh);
144: if ((kbuf = malloc(klen)) == NULL ||
145: (shared_secret = BN_new()) == NULL) {
146: r = SSH_ERR_ALLOC_FAIL;
147: goto out;
148: }
149: if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
150: BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
151: r = SSH_ERR_LIBCRYPTO_ERROR;
152: goto out;
153: }
1.1 markus 154: #ifdef DEBUG_KEXDH
155: dump_digest("shared secret", kbuf, kout);
156: #endif
1.20 markus 157: if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
158: &sbloblen)) != 0)
159: goto out;
1.1 markus 160: /* calc H */
1.20 markus 161: hashlen = sizeof(hash);
162: if ((r = kex_dh_hash(
1.24 ! djm 163: kex->hash_alg,
1.1 markus 164: kex->client_version_string,
165: kex->server_version_string,
1.20 markus 166: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
167: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1 markus 168: server_host_key_blob, sbloblen,
169: dh_client_pub,
1.20 markus 170: kex->dh->pub_key,
1.3 djm 171: shared_secret,
1.20 markus 172: hash, &hashlen)) != 0)
173: goto out;
1.1 markus 174:
175: /* save session id := H */
176: if (kex->session_id == NULL) {
1.3 djm 177: kex->session_id_len = hashlen;
1.20 markus 178: kex->session_id = malloc(kex->session_id_len);
179: if (kex->session_id == NULL) {
180: r = SSH_ERR_ALLOC_FAIL;
181: goto out;
182: }
1.1 markus 183: memcpy(kex->session_id, hash, kex->session_id_len);
184: }
185:
186: /* sign H */
1.23 markus 187: if ((r = kex->sign(server_host_private, server_host_public, &signature,
188: &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
1.20 markus 189: goto out;
1.1 markus 190:
191: /* destroy_sensitive_data(); */
192:
193: /* send server hostkey, DH pubkey 'f' and singed H */
1.20 markus 194: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
195: (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
196: (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
197: (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
198: (r = sshpkt_send(ssh)) != 0)
199: goto out;
200:
201: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
202: r = kex_send_newkeys(ssh);
203: out:
204: explicit_bzero(hash, sizeof(hash));
205: DH_free(kex->dh);
206: kex->dh = NULL;
207: if (dh_client_pub)
208: BN_clear_free(dh_client_pub);
209: if (kbuf) {
210: explicit_bzero(kbuf, klen);
211: free(kbuf);
212: }
213: if (shared_secret)
214: BN_clear_free(shared_secret);
215: free(server_host_key_blob);
1.13 djm 216: free(signature);
1.20 markus 217: return r;
1.1 markus 218: }