Annotation of src/usr.bin/ssh/kexkemc.c, Revision 1.2
1.2 ! djm 1: /* $OpenBSD: kexkemc.c,v 1.1 2019/01/21 10:20:12 djm Exp $ */
1.1 djm 2: /*
3: * Copyright (c) 2019 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 <sys/types.h>
27:
28: #include <stdio.h>
29: #include <string.h>
30: #include <signal.h>
31:
32: #include "sshkey.h"
33: #include "kex.h"
34: #include "log.h"
35: #include "packet.h"
36: #include "ssh2.h"
37: #include "sshbuf.h"
38: #include "digest.h"
39: #include "ssherr.h"
40:
41: static int
42: input_kex_kem_reply(int type, u_int32_t seq, struct ssh *ssh);
43:
44: int
45: kex_kem_client(struct ssh *ssh)
46: {
47: struct kex *kex = ssh->kex;
48: int r;
49:
1.2 ! djm 50: switch (kex->kex_type) {
! 51: case KEX_C25519_SHA256:
! 52: r = kex_c25519_keypair(kex);
! 53: break;
! 54: case KEX_KEM_SNTRUP4591761X25519_SHA512:
! 55: r = kex_kem_sntrup4591761x25519_keypair(kex);
! 56: break;
! 57: default:
! 58: r = SSH_ERR_INVALID_ARGUMENT;
! 59: break;
! 60: }
! 61: if (r != 0)
1.1 djm 62: return r;
63: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
64: (r = sshpkt_put_stringb(ssh, kex->kem_client_pub)) != 0 ||
65: (r = sshpkt_send(ssh)) != 0)
66: return r;
67: debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
68: ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_kem_reply);
69: return 0;
70: }
71:
72: static int
73: input_kex_kem_reply(int type, u_int32_t seq, struct ssh *ssh)
74: {
75: struct kex *kex = ssh->kex;
76: struct sshkey *server_host_key = NULL;
77: struct sshbuf *shared_secret = NULL;
78: u_char *server_pubkey = NULL;
79: u_char *server_host_key_blob = NULL, *signature = NULL;
80: u_char hash[SSH_DIGEST_MAX_LENGTH];
81: size_t slen, pklen, sbloblen, hashlen;
82: int r;
83:
84: /* hostkey */
85: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
86: &sbloblen)) != 0 ||
87: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
88: &server_host_key)) != 0)
89: goto out;
90: if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
91: goto out;
92:
93: /* Q_S, server public key */
94: /* signed H */
95: if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
96: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
97: (r = sshpkt_get_end(ssh)) != 0)
98: goto out;
99:
100: /* compute shared secret */
1.2 ! djm 101: switch (kex->kex_type) {
! 102: case KEX_C25519_SHA256:
! 103: r = kex_c25519_dec(kex, server_pubkey, pklen, &shared_secret);
! 104: break;
! 105: case KEX_KEM_SNTRUP4591761X25519_SHA512:
! 106: r = kex_kem_sntrup4591761x25519_dec(kex, server_pubkey, pklen,
! 107: &shared_secret);
! 108: break;
! 109: default:
! 110: r = SSH_ERR_INVALID_ARGUMENT;
! 111: break;
! 112: }
! 113: if (r !=0 )
1.1 djm 114: goto out;
115:
116: /* calc and verify H */
117: hashlen = sizeof(hash);
118: if ((r = kex_c25519_hash(
119: kex->hash_alg,
120: kex->client_version,
121: kex->server_version,
122: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
123: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
124: server_host_key_blob, sbloblen,
125: sshbuf_ptr(kex->kem_client_pub), sshbuf_len(kex->kem_client_pub),
126: server_pubkey, pklen,
127: sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
128: hash, &hashlen)) != 0)
129: goto out;
130:
131: if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
132: kex->hostkey_alg, ssh->compat)) != 0)
133: goto out;
134:
135: if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
136: r = kex_send_newkeys(ssh);
137: out:
138: explicit_bzero(hash, sizeof(hash));
139: explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
140: explicit_bzero(kex->sntrup4591761_client_key,
141: sizeof(kex->sntrup4591761_client_key));
142: free(server_host_key_blob);
143: free(server_pubkey);
144: free(signature);
145: sshkey_free(server_host_key);
146: sshbuf_free(shared_secret);
147: sshbuf_free(kex->kem_client_pub);
148: kex->kem_client_pub = NULL;
149: return r;
150: }