xref: /openbsd-src/usr.bin/ssh/kex.h (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /* $OpenBSD: kex.h,v 1.52 2010/09/22 05:01:29 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 /* The following represents the family of ECDH methods */
41 #define	KEX_ECDH_SHA2_STEM	"ecdh-sha2-"
42 
43 #define COMP_NONE	0
44 #define COMP_ZLIB	1
45 #define COMP_DELAYED	2
46 
47 enum kex_init_proposals {
48 	PROPOSAL_KEX_ALGS,
49 	PROPOSAL_SERVER_HOST_KEY_ALGS,
50 	PROPOSAL_ENC_ALGS_CTOS,
51 	PROPOSAL_ENC_ALGS_STOC,
52 	PROPOSAL_MAC_ALGS_CTOS,
53 	PROPOSAL_MAC_ALGS_STOC,
54 	PROPOSAL_COMP_ALGS_CTOS,
55 	PROPOSAL_COMP_ALGS_STOC,
56 	PROPOSAL_LANG_CTOS,
57 	PROPOSAL_LANG_STOC,
58 	PROPOSAL_MAX
59 };
60 
61 enum kex_modes {
62 	MODE_IN,
63 	MODE_OUT,
64 	MODE_MAX
65 };
66 
67 enum kex_exchange {
68 	KEX_DH_GRP1_SHA1,
69 	KEX_DH_GRP14_SHA1,
70 	KEX_DH_GEX_SHA1,
71 	KEX_DH_GEX_SHA256,
72 	KEX_ECDH_SHA2,
73 	KEX_MAX
74 };
75 
76 #define KEX_INIT_SENT	0x0001
77 
78 typedef struct Kex Kex;
79 typedef struct Mac Mac;
80 typedef struct Comp Comp;
81 typedef struct Enc Enc;
82 typedef struct Newkeys Newkeys;
83 
84 struct Enc {
85 	char	*name;
86 	Cipher	*cipher;
87 	int	enabled;
88 	u_int	key_len;
89 	u_int	block_size;
90 	u_char	*key;
91 	u_char	*iv;
92 };
93 struct Mac {
94 	char	*name;
95 	int	enabled;
96 	u_int	mac_len;
97 	u_char	*key;
98 	u_int	key_len;
99 	int	type;
100 	const EVP_MD	*evp_md;
101 	HMAC_CTX	evp_ctx;
102 	struct umac_ctx *umac_ctx;
103 };
104 struct Comp {
105 	int	type;
106 	int	enabled;
107 	char	*name;
108 };
109 struct Newkeys {
110 	Enc	enc;
111 	Mac	mac;
112 	Comp	comp;
113 };
114 struct Kex {
115 	u_char	*session_id;
116 	u_int	session_id_len;
117 	Newkeys	*newkeys[MODE_MAX];
118 	u_int	we_need;
119 	int	server;
120 	char	*name;
121 	int	hostkey_type;
122 	int	kex_type;
123 	int	roaming;
124 	Buffer	my;
125 	Buffer	peer;
126 	sig_atomic_t done;
127 	int	flags;
128 	const EVP_MD *evp_md;
129 	char	*client_version_string;
130 	char	*server_version_string;
131 	int	(*verify_host_key)(Key *);
132 	Key	*(*load_host_public_key)(int);
133 	Key	*(*load_host_private_key)(int);
134 	int	(*host_key_index)(Key *);
135 	void	(*kex[KEX_MAX])(Kex *);
136 };
137 
138 int	 kex_names_valid(const char *);
139 
140 Kex	*kex_setup(char *[PROPOSAL_MAX]);
141 void	 kex_finish(Kex *);
142 
143 void	 kex_send_kexinit(Kex *);
144 void	 kex_input_kexinit(int, u_int32_t, void *);
145 void	 kex_derive_keys(Kex *, u_char *, u_int, BIGNUM *);
146 
147 Newkeys *kex_get_newkeys(int);
148 
149 void	 kexdh_client(Kex *);
150 void	 kexdh_server(Kex *);
151 void	 kexgex_client(Kex *);
152 void	 kexgex_server(Kex *);
153 void	 kexecdh_client(Kex *);
154 void	 kexecdh_server(Kex *);
155 
156 void
157 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
158     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
159 void
160 kexgex_hash(const EVP_MD *, char *, char *, char *, int, char *,
161     int, u_char *, int, int, int, int, BIGNUM *, BIGNUM *, BIGNUM *,
162     BIGNUM *, BIGNUM *, u_char **, u_int *);
163 void
164 kex_ecdh_hash(const EVP_MD *, const EC_GROUP *, char *, char *, char *, int,
165     char *, int, u_char *, int, const EC_POINT *, const EC_POINT *,
166     const BIGNUM *, u_char **, u_int *);
167 
168 int	kex_ecdh_name_to_nid(const char *);
169 const EVP_MD *kex_ecdh_name_to_evpmd(const char *);
170 
171 void
172 derive_ssh1_session_id(BIGNUM *, BIGNUM *, u_int8_t[8], u_int8_t[16]);
173 
174 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
175 void	dump_digest(char *, u_char *, int);
176 #endif
177 
178 #endif
179