Annotation of src/usr.bin/ssh/kexdhs.c, Revision 1.25
1.25 ! markus 1: /* $OpenBSD: kexdhs.c,v 1.24 2016/05/02 10:26:04 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.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:
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.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
1.25 ! markus 89: input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 90: {
91: struct kex *kex = ssh->kex;
92: BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
93: struct sshkey *server_host_public, *server_host_private;
94: u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
95: u_char hash[SSH_DIGEST_MAX_LENGTH];
96: size_t sbloblen, slen;
97: size_t klen = 0, hashlen;
98: int kout, r;
1.1 markus 99:
1.11 djm 100: if (kex->load_host_public_key == NULL ||
1.20 markus 101: kex->load_host_private_key == NULL) {
102: r = SSH_ERR_INVALID_ARGUMENT;
103: goto out;
104: }
1.22 djm 105: server_host_public = kex->load_host_public_key(kex->hostkey_type,
106: kex->hostkey_nid, ssh);
107: server_host_private = kex->load_host_private_key(kex->hostkey_type,
108: kex->hostkey_nid, ssh);
1.21 djm 109: if (server_host_public == NULL) {
1.20 markus 110: r = SSH_ERR_NO_HOSTKEY_LOADED;
111: goto out;
112: }
1.1 markus 113:
114: /* key, cert */
1.20 markus 115: if ((dh_client_pub = BN_new()) == NULL) {
116: r = SSH_ERR_ALLOC_FAIL;
117: goto out;
118: }
119: if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
120: (r = sshpkt_get_end(ssh)) != 0)
121: goto out;
1.1 markus 122:
123: #ifdef DEBUG_KEXDH
124: fprintf(stderr, "dh_client_pub= ");
125: BN_print_fp(stderr, dh_client_pub);
126: fprintf(stderr, "\n");
127: debug("bits %d", BN_num_bits(dh_client_pub));
128: #endif
129:
130: #ifdef DEBUG_KEXDH
1.20 markus 131: DHparams_print_fp(stderr, kex->dh);
1.1 markus 132: fprintf(stderr, "pub= ");
1.20 markus 133: BN_print_fp(stderr, kex->dh->pub_key);
1.1 markus 134: fprintf(stderr, "\n");
135: #endif
1.20 markus 136: if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
137: sshpkt_disconnect(ssh, "bad client public DH value");
138: r = SSH_ERR_MESSAGE_INCOMPLETE;
139: goto out;
140: }
1.1 markus 141:
1.20 markus 142: klen = DH_size(kex->dh);
143: if ((kbuf = malloc(klen)) == NULL ||
144: (shared_secret = BN_new()) == NULL) {
145: r = SSH_ERR_ALLOC_FAIL;
146: goto out;
147: }
148: if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
149: BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
150: r = SSH_ERR_LIBCRYPTO_ERROR;
151: goto out;
152: }
1.1 markus 153: #ifdef DEBUG_KEXDH
154: dump_digest("shared secret", kbuf, kout);
155: #endif
1.20 markus 156: if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
157: &sbloblen)) != 0)
158: goto out;
1.1 markus 159: /* calc H */
1.20 markus 160: hashlen = sizeof(hash);
161: if ((r = kex_dh_hash(
1.24 djm 162: kex->hash_alg,
1.1 markus 163: kex->client_version_string,
164: kex->server_version_string,
1.20 markus 165: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
166: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
1.1 markus 167: server_host_key_blob, sbloblen,
168: dh_client_pub,
1.20 markus 169: kex->dh->pub_key,
1.3 djm 170: shared_secret,
1.20 markus 171: hash, &hashlen)) != 0)
172: goto out;
1.1 markus 173:
174: /* save session id := H */
175: if (kex->session_id == NULL) {
1.3 djm 176: kex->session_id_len = hashlen;
1.20 markus 177: kex->session_id = malloc(kex->session_id_len);
178: if (kex->session_id == NULL) {
179: r = SSH_ERR_ALLOC_FAIL;
180: goto out;
181: }
1.1 markus 182: memcpy(kex->session_id, hash, kex->session_id_len);
183: }
184:
185: /* sign H */
1.23 markus 186: if ((r = kex->sign(server_host_private, server_host_public, &signature,
187: &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
1.20 markus 188: goto out;
1.1 markus 189:
190: /* destroy_sensitive_data(); */
191:
192: /* send server hostkey, DH pubkey 'f' and singed H */
1.20 markus 193: if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
194: (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
195: (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
196: (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
197: (r = sshpkt_send(ssh)) != 0)
198: goto out;
199:
200: if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
201: r = kex_send_newkeys(ssh);
202: out:
203: explicit_bzero(hash, sizeof(hash));
204: DH_free(kex->dh);
205: kex->dh = NULL;
206: if (dh_client_pub)
207: BN_clear_free(dh_client_pub);
208: if (kbuf) {
209: explicit_bzero(kbuf, klen);
210: free(kbuf);
211: }
212: if (shared_secret)
213: BN_clear_free(shared_secret);
214: free(server_host_key_blob);
1.13 djm 215: free(signature);
1.20 markus 216: return r;
1.1 markus 217: }