xref: /openbsd-src/usr.bin/ssh/kex.h (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: kex.h,v 1.64 2014/05/02 03:27:54 djm 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 <openssl/evp.h>
30 #include <openssl/hmac.h>
31 #include <openssl/ec.h>
32 
33 #define KEX_COOKIE_LEN	16
34 
35 #define	KEX_DH1			"diffie-hellman-group1-sha1"
36 #define	KEX_DH14		"diffie-hellman-group14-sha1"
37 #define	KEX_DHGEX_SHA1		"diffie-hellman-group-exchange-sha1"
38 #define	KEX_DHGEX_SHA256	"diffie-hellman-group-exchange-sha256"
39 #define	KEX_RESUME		"resume@appgate.com"
40 #define	KEX_ECDH_SHA2_NISTP256	"ecdh-sha2-nistp256"
41 #define	KEX_ECDH_SHA2_NISTP384	"ecdh-sha2-nistp384"
42 #define	KEX_ECDH_SHA2_NISTP521	"ecdh-sha2-nistp521"
43 #define	KEX_CURVE25519_SHA256	"curve25519-sha256@libssh.org"
44 
45 #define COMP_NONE	0
46 #define COMP_ZLIB	1
47 #define COMP_DELAYED	2
48 
49 enum kex_init_proposals {
50 	PROPOSAL_KEX_ALGS,
51 	PROPOSAL_SERVER_HOST_KEY_ALGS,
52 	PROPOSAL_ENC_ALGS_CTOS,
53 	PROPOSAL_ENC_ALGS_STOC,
54 	PROPOSAL_MAC_ALGS_CTOS,
55 	PROPOSAL_MAC_ALGS_STOC,
56 	PROPOSAL_COMP_ALGS_CTOS,
57 	PROPOSAL_COMP_ALGS_STOC,
58 	PROPOSAL_LANG_CTOS,
59 	PROPOSAL_LANG_STOC,
60 	PROPOSAL_MAX
61 };
62 
63 enum kex_modes {
64 	MODE_IN,
65 	MODE_OUT,
66 	MODE_MAX
67 };
68 
69 enum kex_exchange {
70 	KEX_DH_GRP1_SHA1,
71 	KEX_DH_GRP14_SHA1,
72 	KEX_DH_GEX_SHA1,
73 	KEX_DH_GEX_SHA256,
74 	KEX_ECDH_SHA2,
75 	KEX_C25519_SHA256,
76 	KEX_MAX
77 };
78 
79 #define KEX_INIT_SENT	0x0001
80 
81 typedef struct Kex Kex;
82 typedef struct Mac Mac;
83 typedef struct Comp Comp;
84 typedef struct Enc Enc;
85 typedef struct Newkeys Newkeys;
86 
87 struct Enc {
88 	char	*name;
89 	const Cipher *cipher;
90 	int	enabled;
91 	u_int	key_len;
92 	u_int	iv_len;
93 	u_int	block_size;
94 	u_char	*key;
95 	u_char	*iv;
96 };
97 struct Mac {
98 	char	*name;
99 	int	enabled;
100 	u_int	mac_len;
101 	u_char	*key;
102 	u_int	key_len;
103 	int	type;
104 	int	etm;		/* Encrypt-then-MAC */
105 	struct ssh_hmac_ctx	*hmac_ctx;
106 	struct umac_ctx		*umac_ctx;
107 };
108 struct Comp {
109 	int	type;
110 	int	enabled;
111 	char	*name;
112 };
113 struct Newkeys {
114 	Enc	enc;
115 	Mac	mac;
116 	Comp	comp;
117 };
118 struct Kex {
119 	u_char	*session_id;
120 	u_int	session_id_len;
121 	Newkeys	*newkeys[MODE_MAX];
122 	u_int	we_need;
123 	u_int	dh_need;
124 	int	server;
125 	char	*name;
126 	int	hostkey_type;
127 	int	kex_type;
128 	int	roaming;
129 	Buffer	my;
130 	Buffer	peer;
131 	sig_atomic_t done;
132 	int	flags;
133 	int	hash_alg;
134 	int	ec_nid;
135 	char	*client_version_string;
136 	char	*server_version_string;
137 	int	(*verify_host_key)(Key *);
138 	Key	*(*load_host_public_key)(int);
139 	Key	*(*load_host_private_key)(int);
140 	int	(*host_key_index)(Key *);
141 	void    (*sign)(Key *, Key *, u_char **, u_int *, u_char *, u_int);
142 	void	(*kex[KEX_MAX])(Kex *);
143 };
144 
145 int	 kex_names_valid(const char *);
146 char	*kex_alg_list(char);
147 
148 Kex	*kex_setup(char *[PROPOSAL_MAX]);
149 void	 kex_finish(Kex *);
150 
151 void	 kex_send_kexinit(Kex *);
152 void	 kex_input_kexinit(int, u_int32_t, void *);
153 void	 kex_derive_keys(Kex *, u_char *, u_int, const u_char *, u_int);
154 void	 kex_derive_keys_bn(Kex *, u_char *, u_int, const BIGNUM *);
155 
156 Newkeys *kex_get_newkeys(int);
157 
158 void	 kexdh_client(Kex *);
159 void	 kexdh_server(Kex *);
160 void	 kexgex_client(Kex *);
161 void	 kexgex_server(Kex *);
162 void	 kexecdh_client(Kex *);
163 void	 kexecdh_server(Kex *);
164 void	 kexc25519_client(Kex *);
165 void	 kexc25519_server(Kex *);
166 
167 void
168 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
169     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
170 void
171 kexgex_hash(int, char *, char *, char *, int, char *,
172     int, u_char *, int, int, int, int, BIGNUM *, BIGNUM *, BIGNUM *,
173     BIGNUM *, BIGNUM *, u_char **, u_int *);
174 void
175 kex_ecdh_hash(int, const EC_GROUP *, char *, char *, char *, int,
176     char *, int, u_char *, int, const EC_POINT *, const EC_POINT *,
177     const BIGNUM *, u_char **, u_int *);
178 void
179 kex_c25519_hash(int, char *, char *, char *, int,
180     char *, int, u_char *, int, const u_char *, const u_char *,
181     const u_char *, u_int, u_char **, u_int *);
182 
183 #define CURVE25519_SIZE 32
184 void	kexc25519_keygen(u_char[CURVE25519_SIZE], u_char[CURVE25519_SIZE])
185 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
186 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
187 void kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
188     const u_char pub[CURVE25519_SIZE], Buffer *out)
189 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
190 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
191 
192 void
193 derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
194 
195 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
196 void	dump_digest(char *, u_char *, int);
197 #endif
198 
199 #endif
200