summaryrefslogtreecommitdiff
path: root/kexkems.c
diff options
context:
space:
mode:
Diffstat (limited to 'kexkems.c')
-rw-r--r--kexkems.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/kexkems.c b/kexkems.c
new file mode 100644
index 000000000..43cf82018
--- /dev/null
+++ b/kexkems.c
@@ -0,0 +1,116 @@
1/* $OpenBSD: kexkems.c,v 1.1 2019/01/21 10:20:12 djm Exp $ */
2/*
3 * Copyright (c) 2019 Markus Friedl. All rights reserved.
4 *
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include <sys/types.h>
26#include <stdio.h>
27#include <string.h>
28#include <signal.h>
29
30#include "sshkey.h"
31#include "digest.h"
32#include "kex.h"
33#include "log.h"
34#include "packet.h"
35#include "ssh2.h"
36#include "sshbuf.h"
37#include "ssherr.h"
38
39static int input_kex_kem_init(int, u_int32_t, struct ssh *);
40
41int
42kex_kem_server(struct ssh *ssh)
43{
44 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
45 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_kem_init);
46 return 0;
47}
48
49static int
50input_kex_kem_init(int type, u_int32_t seq, struct ssh *ssh)
51{
52 struct kex *kex = ssh->kex;
53 struct sshkey *server_host_private, *server_host_public;
54 struct sshbuf *shared_secret = NULL;
55 struct sshbuf *server_pubkey = NULL;
56 u_char *server_host_key_blob = NULL, *signature = NULL;
57 u_char *client_pubkey = NULL;
58 u_char hash[SSH_DIGEST_MAX_LENGTH];
59 size_t slen, pklen, sbloblen, hashlen;
60 int r;
61
62 if ((r = kex_load_hostkey(ssh, &server_host_private,
63 &server_host_public)) != 0)
64 goto out;
65
66 if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
67 (r = sshpkt_get_end(ssh)) != 0)
68 goto out;
69
70 /* compute shared secret */
71 if ((r = kex_kem_sntrup4591761x25519_enc(kex, client_pubkey, pklen,
72 &server_pubkey, &shared_secret)) != 0)
73 goto out;
74
75 /* calc H */
76 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
77 &sbloblen)) != 0)
78 goto out;
79 hashlen = sizeof(hash);
80 if ((r = kex_c25519_hash(
81 kex->hash_alg,
82 kex->client_version,
83 kex->server_version,
84 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
85 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
86 server_host_key_blob, sbloblen,
87 client_pubkey, pklen,
88 sshbuf_ptr(server_pubkey), sshbuf_len(server_pubkey),
89 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
90 hash, &hashlen)) != 0)
91 goto out;
92
93 /* sign H */
94 if ((r = kex->sign(ssh, server_host_private, server_host_public,
95 &signature, &slen, hash, hashlen, kex->hostkey_alg)) != 0)
96 goto out;
97
98 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
99 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
100 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
101 (r = sshpkt_put_stringb(ssh, server_pubkey)) != 0 ||
102 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
103 (r = sshpkt_send(ssh)) != 0)
104 goto out;
105
106 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
107 r = kex_send_newkeys(ssh);
108out:
109 explicit_bzero(hash, sizeof(hash));
110 free(server_host_key_blob);
111 free(signature);
112 free(client_pubkey);
113 sshbuf_free(shared_secret);
114 sshbuf_free(server_pubkey);
115 return r;
116}