Annotation of src/usr.bin/ssh/kexc25519c.c, Revision 1.13
1.13 ! djm 1: /* $OpenBSD: kexc25519c.c,v 1.12 2019/01/21 10:07:22 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
4: * Copyright (c) 2010 Damien Miller. All rights reserved.
5: * Copyright (c) 2013 Aris Adamantiadis. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
28: #include <sys/types.h>
29:
30: #include <stdio.h>
31: #include <string.h>
32: #include <signal.h>
33:
1.6 markus 34: #include "sshkey.h"
1.1 markus 35: #include "cipher.h"
36: #include "kex.h"
37: #include "log.h"
38: #include "packet.h"
39: #include "ssh2.h"
1.6 markus 40: #include "sshbuf.h"
41: #include "digest.h"
42: #include "ssherr.h"
1.1 markus 43:
1.6 markus 44: static int
1.8 djm 45: input_kex_c25519_reply(int type, u_int32_t seq, struct ssh *ssh);
1.6 markus 46:
47: int
48: kexc25519_client(struct ssh *ssh)
1.1 markus 49: {
1.6 markus 50: struct kex *kex = ssh->kex;
51: int r;
1.1 markus 52:
1.6 markus 53: kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey);
1.1 markus 54: #ifdef DEBUG_KEXECDH
1.6 markus 55: dump_digest("client private key:", kex->c25519_client_key,
56: sizeof(kex->c25519_client_key));
1.1 markus 57: #endif
1.6 markus 58: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
59: (r = sshpkt_put_string(ssh, kex->c25519_client_pubkey,
60: sizeof(kex->c25519_client_pubkey))) != 0 ||
61: (r = sshpkt_send(ssh)) != 0)
62: return r;
1.1 markus 63:
64: debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
1.6 markus 65: ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply);
66: return 0;
67: }
68:
69: static int
1.8 djm 70: input_kex_c25519_reply(int type, u_int32_t seq, struct ssh *ssh)
1.6 markus 71: {
72: struct kex *kex = ssh->kex;
73: struct sshkey *server_host_key = NULL;
74: struct sshbuf *shared_secret = NULL;
75: u_char *server_pubkey = NULL;
76: u_char *server_host_key_blob = NULL, *signature = NULL;
77: u_char hash[SSH_DIGEST_MAX_LENGTH];
78: size_t slen, pklen, sbloblen, hashlen;
79: int r;
80:
1.1 markus 81: /* hostkey */
1.6 markus 82: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
83: &sbloblen)) != 0 ||
84: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
85: &server_host_key)) != 0)
86: goto out;
1.12 djm 87: if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
1.6 markus 88: goto out;
1.1 markus 89:
90: /* Q_S, server public key */
1.6 markus 91: /* signed H */
92: if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
93: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
94: (r = sshpkt_get_end(ssh)) != 0)
95: goto out;
96: if (pklen != CURVE25519_SIZE) {
97: r = SSH_ERR_SIGNATURE_INVALID;
98: goto out;
99: }
1.1 markus 100:
101: #ifdef DEBUG_KEXECDH
102: dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
103: #endif
104:
1.6 markus 105: if ((shared_secret = sshbuf_new()) == NULL) {
106: r = SSH_ERR_ALLOC_FAIL;
107: goto out;
108: }
109: if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey,
1.13 ! djm 110: shared_secret)) != 0)
1.6 markus 111: goto out;
1.1 markus 112:
113: /* calc and verify H */
1.6 markus 114: hashlen = sizeof(hash);
115: if ((r = kex_c25519_hash(
1.3 djm 116: kex->hash_alg,
1.10 djm 117: kex->client_version,
118: kex->server_version,
1.6 markus 119: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
120: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
1.1 markus 121: server_host_key_blob, sbloblen,
1.13 ! djm 122: kex->c25519_client_pubkey, sizeof(kex->c25519_client_pubkey),
! 123: server_pubkey, pklen,
1.6 markus 124: sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
1.13 ! djm 125: hash, &hashlen)) != 0)
1.6 markus 126: goto out;
127:
128: if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
1.9 djm 129: kex->hostkey_alg, ssh->compat)) != 0)
1.6 markus 130: goto out;
131:
132: if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
133: r = kex_send_newkeys(ssh);
134: out:
135: explicit_bzero(hash, sizeof(hash));
136: explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
137: free(server_host_key_blob);
138: free(server_pubkey);
139: free(signature);
140: sshkey_free(server_host_key);
141: sshbuf_free(shared_secret);
142: return r;
1.1 markus 143: }