kex.h revision 294693
1/* $OpenBSD: kex.h,v 1.62 2014/01/27 18:58:14 markus Exp $ */
2
3/*
4 * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#ifndef KEX_H
27#define KEX_H
28
29#include <signal.h>
30#include <openssl/evp.h>
31#include <openssl/hmac.h>
32#ifdef OPENSSL_HAS_ECC
33#include <openssl/ec.h>
34#endif
35
36#define KEX_COOKIE_LEN	16
37
38#define	KEX_DH1			"diffie-hellman-group1-sha1"
39#define	KEX_DH14		"diffie-hellman-group14-sha1"
40#define	KEX_DHGEX_SHA1		"diffie-hellman-group-exchange-sha1"
41#define	KEX_DHGEX_SHA256	"diffie-hellman-group-exchange-sha256"
42#define	KEX_RESUME		"resume@appgate.com"
43#define	KEX_ECDH_SHA2_NISTP256	"ecdh-sha2-nistp256"
44#define	KEX_ECDH_SHA2_NISTP384	"ecdh-sha2-nistp384"
45#define	KEX_ECDH_SHA2_NISTP521	"ecdh-sha2-nistp521"
46#define	KEX_CURVE25519_SHA256	"curve25519-sha256@libssh.org"
47
48#define COMP_NONE	0
49#define COMP_ZLIB	1
50#define COMP_DELAYED	2
51
52enum kex_init_proposals {
53	PROPOSAL_KEX_ALGS,
54	PROPOSAL_SERVER_HOST_KEY_ALGS,
55	PROPOSAL_ENC_ALGS_CTOS,
56	PROPOSAL_ENC_ALGS_STOC,
57	PROPOSAL_MAC_ALGS_CTOS,
58	PROPOSAL_MAC_ALGS_STOC,
59	PROPOSAL_COMP_ALGS_CTOS,
60	PROPOSAL_COMP_ALGS_STOC,
61	PROPOSAL_LANG_CTOS,
62	PROPOSAL_LANG_STOC,
63	PROPOSAL_MAX
64};
65
66enum kex_modes {
67	MODE_IN,
68	MODE_OUT,
69	MODE_MAX
70};
71
72enum kex_exchange {
73	KEX_DH_GRP1_SHA1,
74	KEX_DH_GRP14_SHA1,
75	KEX_DH_GEX_SHA1,
76	KEX_DH_GEX_SHA256,
77	KEX_ECDH_SHA2,
78	KEX_C25519_SHA256,
79	KEX_MAX
80};
81
82#define KEX_INIT_SENT	0x0001
83
84typedef struct Kex Kex;
85typedef struct Mac Mac;
86typedef struct Comp Comp;
87typedef struct Enc Enc;
88typedef struct Newkeys Newkeys;
89
90struct Enc {
91	char	*name;
92	const Cipher *cipher;
93	int	enabled;
94	u_int	key_len;
95	u_int	iv_len;
96	u_int	block_size;
97	u_char	*key;
98	u_char	*iv;
99};
100struct Mac {
101	char	*name;
102	int	enabled;
103	u_int	mac_len;
104	u_char	*key;
105	u_int	key_len;
106	int	type;
107	int	etm;		/* Encrypt-then-MAC */
108	struct ssh_hmac_ctx	*hmac_ctx;
109	struct umac_ctx		*umac_ctx;
110};
111struct Comp {
112	int	type;
113	int	enabled;
114	char	*name;
115};
116struct Newkeys {
117	Enc	enc;
118	Mac	mac;
119	Comp	comp;
120};
121struct Kex {
122	u_char	*session_id;
123	u_int	session_id_len;
124	Newkeys	*newkeys[MODE_MAX];
125	u_int	we_need;
126	u_int	dh_need;
127	int	server;
128	char	*name;
129	int	hostkey_type;
130	int	kex_type;
131	int	roaming;
132	Buffer	my;
133	Buffer	peer;
134	sig_atomic_t done;
135	int	flags;
136	int	hash_alg;
137	int	ec_nid;
138	char	*client_version_string;
139	char	*server_version_string;
140	int	(*verify_host_key)(Key *);
141	Key	*(*load_host_public_key)(int);
142	Key	*(*load_host_private_key)(int);
143	int	(*host_key_index)(Key *);
144	void    (*sign)(Key *, Key *, u_char **, u_int *, u_char *, u_int);
145	void	(*kex[KEX_MAX])(Kex *);
146};
147
148int	 kex_names_valid(const char *);
149char	*kex_alg_list(char);
150
151Kex	*kex_setup(char *[PROPOSAL_MAX]);
152void	 kex_finish(Kex *);
153
154void	 kex_send_kexinit(Kex *);
155void	 kex_input_kexinit(int, u_int32_t, void *);
156void	 kex_derive_keys(Kex *, u_char *, u_int, const u_char *, u_int);
157void	 kex_derive_keys_bn(Kex *, u_char *, u_int, const BIGNUM *);
158
159Newkeys *kex_get_newkeys(int);
160
161void	 kexdh_client(Kex *);
162void	 kexdh_server(Kex *);
163void	 kexgex_client(Kex *);
164void	 kexgex_server(Kex *);
165void	 kexecdh_client(Kex *);
166void	 kexecdh_server(Kex *);
167void	 kexc25519_client(Kex *);
168void	 kexc25519_server(Kex *);
169
170void
171kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
172    BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
173void
174kexgex_hash(int, char *, char *, char *, int, char *,
175    int, u_char *, int, int, int, int, BIGNUM *, BIGNUM *, BIGNUM *,
176    BIGNUM *, BIGNUM *, u_char **, u_int *);
177#ifdef OPENSSL_HAS_ECC
178void
179kex_ecdh_hash(int, const EC_GROUP *, char *, char *, char *, int,
180    char *, int, u_char *, int, const EC_POINT *, const EC_POINT *,
181    const BIGNUM *, u_char **, u_int *);
182#endif
183void
184kex_c25519_hash(int, char *, char *, char *, int,
185    char *, int, u_char *, int, const u_char *, const u_char *,
186    const u_char *, u_int, u_char **, u_int *);
187
188#define CURVE25519_SIZE 32
189void	kexc25519_keygen(u_char[CURVE25519_SIZE], u_char[CURVE25519_SIZE])
190	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
191	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
192void kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
193    const u_char pub[CURVE25519_SIZE], Buffer *out)
194	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
195	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
196
197void
198derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
199
200#if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
201void	dump_digest(char *, u_char *, int);
202#endif
203
204#endif
205