summaryrefslogtreecommitdiff
path: root/kexc25519c.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2015-08-19 14:23:50 +0100
committerColin Watson <cjwatson@debian.org>2015-08-19 14:23:50 +0100
commitbaccdb349b31c47cd76fb63211f754ed33a9707e (patch)
treed03653f975fd4eb8bf71bb0c9d168614401202fa /kexc25519c.c
parent487bdb3a5ef6075887b830ccb8a0b14f6da78e93 (diff)
parent9f82e5a9042f2d872e98f48a876fcab3e25dd9bb (diff)
Import openssh_6.8p1.orig.tar.gz
Diffstat (limited to 'kexc25519c.c')
-rw-r--r--kexc25519c.c161
1 files changed, 101 insertions, 60 deletions
diff --git a/kexc25519c.c b/kexc25519c.c
index a80678af6..b7ef65dc3 100644
--- a/kexc25519c.c
+++ b/kexc25519c.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: kexc25519c.c,v 1.4 2014/01/12 08:13:13 djm Exp $ */ 1/* $OpenBSD: kexc25519c.c,v 1.7 2015/01/26 06:10:03 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved. 4 * Copyright (c) 2010 Damien Miller. All rights reserved.
@@ -33,97 +33,138 @@
33#include <string.h> 33#include <string.h>
34#include <signal.h> 34#include <signal.h>
35 35
36#include "xmalloc.h" 36#include "sshkey.h"
37#include "buffer.h"
38#include "key.h"
39#include "cipher.h" 37#include "cipher.h"
40#include "kex.h" 38#include "kex.h"
41#include "log.h" 39#include "log.h"
42#include "packet.h" 40#include "packet.h"
43#include "ssh2.h" 41#include "ssh2.h"
42#include "sshbuf.h"
43#include "digest.h"
44#include "ssherr.h"
44 45
45void 46static int
46kexc25519_client(Kex *kex) 47input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt);
47{
48 Key *server_host_key;
49 u_char client_key[CURVE25519_SIZE];
50 u_char client_pubkey[CURVE25519_SIZE];
51 u_char *server_pubkey = NULL;
52 u_char *server_host_key_blob = NULL, *signature = NULL;
53 u_char *hash;
54 u_int slen, sbloblen, hashlen;
55 Buffer shared_secret;
56
57 kexc25519_keygen(client_key, client_pubkey);
58 48
59 packet_start(SSH2_MSG_KEX_ECDH_INIT); 49int
60 packet_put_string(client_pubkey, sizeof(client_pubkey)); 50kexc25519_client(struct ssh *ssh)
61 packet_send(); 51{
62 debug("sending SSH2_MSG_KEX_ECDH_INIT"); 52 struct kex *kex = ssh->kex;
53 int r;
63 54
55 kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey);
64#ifdef DEBUG_KEXECDH 56#ifdef DEBUG_KEXECDH
65 dump_digest("client private key:", client_key, sizeof(client_key)); 57 dump_digest("client private key:", kex->c25519_client_key,
58 sizeof(kex->c25519_client_key));
66#endif 59#endif
60 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 ||
61 (r = sshpkt_put_string(ssh, kex->c25519_client_pubkey,
62 sizeof(kex->c25519_client_pubkey))) != 0 ||
63 (r = sshpkt_send(ssh)) != 0)
64 return r;
67 65
68 debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); 66 debug("expecting SSH2_MSG_KEX_ECDH_REPLY");
69 packet_read_expect(SSH2_MSG_KEX_ECDH_REPLY); 67 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply);
68 return 0;
69}
70
71static int
72input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt)
73{
74 struct ssh *ssh = ctxt;
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 if (kex->verify_host_key == NULL) {
85 r = SSH_ERR_INVALID_ARGUMENT;
86 goto out;
87 }
70 88
71 /* hostkey */ 89 /* hostkey */
72 server_host_key_blob = packet_get_string(&sbloblen); 90 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
73 server_host_key = key_from_blob(server_host_key_blob, sbloblen); 91 &sbloblen)) != 0 ||
74 if (server_host_key == NULL) 92 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
75 fatal("cannot decode server_host_key_blob"); 93 &server_host_key)) != 0)
76 if (server_host_key->type != kex->hostkey_type) 94 goto out;
77 fatal("type mismatch for decoded server_host_key_blob"); 95 if (server_host_key->type != kex->hostkey_type ||
78 if (kex->verify_host_key == NULL) 96 (kex->hostkey_type == KEY_ECDSA &&
79 fatal("cannot verify server_host_key"); 97 server_host_key->ecdsa_nid != kex->hostkey_nid)) {
80 if (kex->verify_host_key(server_host_key) == -1) 98 r = SSH_ERR_KEY_TYPE_MISMATCH;
81 fatal("server_host_key verification failed"); 99 goto out;
100 }
101 if (kex->verify_host_key(server_host_key, ssh) == -1) {
102 r = SSH_ERR_SIGNATURE_INVALID;
103 goto out;
104 }
82 105
83 /* Q_S, server public key */ 106 /* Q_S, server public key */
84 server_pubkey = packet_get_string(&slen); 107 /* signed H */
85 if (slen != CURVE25519_SIZE) 108 if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 ||
86 fatal("Incorrect size for server Curve25519 pubkey: %d", slen); 109 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
110 (r = sshpkt_get_end(ssh)) != 0)
111 goto out;
112 if (pklen != CURVE25519_SIZE) {
113 r = SSH_ERR_SIGNATURE_INVALID;
114 goto out;
115 }
87 116
88#ifdef DEBUG_KEXECDH 117#ifdef DEBUG_KEXECDH
89 dump_digest("server public key:", server_pubkey, CURVE25519_SIZE); 118 dump_digest("server public key:", server_pubkey, CURVE25519_SIZE);
90#endif 119#endif
91 120
92 /* signed H */ 121 if ((shared_secret = sshbuf_new()) == NULL) {
93 signature = packet_get_string(&slen); 122 r = SSH_ERR_ALLOC_FAIL;
94 packet_check_eom(); 123 goto out;
95 124 }
96 buffer_init(&shared_secret); 125 if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey,
97 kexc25519_shared_key(client_key, server_pubkey, &shared_secret); 126 shared_secret)) < 0)
127 goto out;
98 128
99 /* calc and verify H */ 129 /* calc and verify H */
100 kex_c25519_hash( 130 hashlen = sizeof(hash);
131 if ((r = kex_c25519_hash(
101 kex->hash_alg, 132 kex->hash_alg,
102 kex->client_version_string, 133 kex->client_version_string,
103 kex->server_version_string, 134 kex->server_version_string,
104 buffer_ptr(&kex->my), buffer_len(&kex->my), 135 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
105 buffer_ptr(&kex->peer), buffer_len(&kex->peer), 136 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
106 server_host_key_blob, sbloblen, 137 server_host_key_blob, sbloblen,
107 client_pubkey, 138 kex->c25519_client_pubkey,
108 server_pubkey, 139 server_pubkey,
109 buffer_ptr(&shared_secret), buffer_len(&shared_secret), 140 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
110 &hash, &hashlen 141 hash, &hashlen)) < 0)
111 ); 142 goto out;
112 free(server_host_key_blob); 143
113 free(server_pubkey); 144 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
114 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) 145 ssh->compat)) != 0)
115 fatal("key_verify failed for server_host_key"); 146 goto out;
116 key_free(server_host_key);
117 free(signature);
118 147
119 /* save session id */ 148 /* save session id */
120 if (kex->session_id == NULL) { 149 if (kex->session_id == NULL) {
121 kex->session_id_len = hashlen; 150 kex->session_id_len = hashlen;
122 kex->session_id = xmalloc(kex->session_id_len); 151 kex->session_id = malloc(kex->session_id_len);
152 if (kex->session_id == NULL) {
153 r = SSH_ERR_ALLOC_FAIL;
154 goto out;
155 }
123 memcpy(kex->session_id, hash, kex->session_id_len); 156 memcpy(kex->session_id, hash, kex->session_id_len);
124 } 157 }
125 kex_derive_keys(kex, hash, hashlen, 158
126 buffer_ptr(&shared_secret), buffer_len(&shared_secret)); 159 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
127 buffer_free(&shared_secret); 160 r = kex_send_newkeys(ssh);
128 kex_finish(kex); 161out:
162 explicit_bzero(hash, sizeof(hash));
163 explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key));
164 free(server_host_key_blob);
165 free(server_pubkey);
166 free(signature);
167 sshkey_free(server_host_key);
168 sshbuf_free(shared_secret);
169 return r;
129} 170}