Annotation of src/usr.bin/ssh/kexkemc.c, Revision 1.1
1.1 ! djm 1: /* $OpenBSD: $ */
! 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:
! 50: if ((r = kex_kem_sntrup4591761x25519_keypair(kex)) != 0)
! 51: return r;
! 52: if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
! 53: (r = sshpkt_put_stringb(ssh, kex->kem_client_pub)) != 0 ||
! 54: (r = sshpkt_send(ssh)) != 0)
! 55: return r;
! 56: debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
! 57: ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_kem_reply);
! 58: return 0;
! 59: }
! 60:
! 61: static int
! 62: input_kex_kem_reply(int type, u_int32_t seq, struct ssh *ssh)
! 63: {
! 64: struct kex *kex = ssh->kex;
! 65: struct sshkey *server_host_key = NULL;
! 66: struct sshbuf *shared_secret = NULL;
! 67: u_char *server_pubkey = NULL;
! 68: u_char *server_host_key_blob = NULL, *signature = NULL;
! 69: u_char hash[SSH_DIGEST_MAX_LENGTH];
! 70: size_t slen, pklen, sbloblen, hashlen;
! 71: int r;
! 72:
! 73: /* hostkey */
! 74: if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
! 75: &sbloblen)) != 0 ||
! 76: (r = sshkey_from_blob(server_host_key_blob, sbloblen,
! 77: &server_host_key)) != 0)
! 78: goto out;
! 79: if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
! 80: goto out;
! 81:
! 82: /* Q_S, server public key */
! 83: /* signed H */
! 84: if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
! 85: (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
! 86: (r = sshpkt_get_end(ssh)) != 0)
! 87: goto out;
! 88:
! 89: /* compute shared secret */
! 90: if ((r = kex_kem_sntrup4591761x25519_dec(kex, server_pubkey, pklen,
! 91: &shared_secret)) != 0)
! 92: goto out;
! 93:
! 94: /* calc and verify H */
! 95: hashlen = sizeof(hash);
! 96: if ((r = kex_c25519_hash(
! 97: kex->hash_alg,
! 98: kex->client_version,
! 99: kex->server_version,
! 100: sshbuf_ptr(kex->my), sshbuf_len(kex->my),
! 101: sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
! 102: server_host_key_blob, sbloblen,
! 103: sshbuf_ptr(kex->kem_client_pub), sshbuf_len(kex->kem_client_pub),
! 104: server_pubkey, pklen,
! 105: sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
! 106: hash, &hashlen)) != 0)
! 107: goto out;
! 108:
! 109: if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
! 110: kex->hostkey_alg, ssh->compat)) != 0)
! 111: goto out;
! 112:
! 113: if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
! 114: r = kex_send_newkeys(ssh);
! 115: out:
! 116: explicit_bzero(hash, sizeof(hash));
! 117: explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
! 118: explicit_bzero(kex->sntrup4591761_client_key,
! 119: sizeof(kex->sntrup4591761_client_key));
! 120: free(server_host_key_blob);
! 121: free(server_pubkey);
! 122: free(signature);
! 123: sshkey_free(server_host_key);
! 124: sshbuf_free(shared_secret);
! 125: sshbuf_free(kex->kem_client_pub);
! 126: kex->kem_client_pub = NULL;
! 127: return r;
! 128: }