Line  | Count  | Source  | 
1  |  | /* $OpenBSD: kexecdh.c,v 1.11 2025/10/03 00:08:02 djm Exp $ */  | 
2  |  | /*  | 
3  |  |  * Copyright (c) 2010 Damien Miller.  All rights reserved.  | 
4  |  |  * Copyright (c) 2019 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  |  |  | 
27  |  | #include "includes.h"  | 
28  |  |  | 
29  |  | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)  | 
30  |  |  | 
31  |  | #include <sys/types.h>  | 
32  |  |  | 
33  |  | #include <stdio.h>  | 
34  |  | #include <string.h>  | 
35  |  | #include <signal.h>  | 
36  |  |  | 
37  |  | #include <openssl/bn.h>  | 
38  |  | #include <openssl/ecdh.h>  | 
39  |  |  | 
40  |  | #include "sshkey.h"  | 
41  |  | #include "kex.h"  | 
42  |  | #include "sshbuf.h"  | 
43  |  | #include "digest.h"  | 
44  |  | #include "ssherr.h"  | 
45  |  |  | 
46  |  | static int  | 
47  |  | kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key,  | 
48  |  |     const EC_GROUP *, struct sshbuf **);  | 
49  |  |  | 
50  |  | int  | 
51  |  | kex_ecdh_keypair(struct kex *kex)  | 
52  | 0  | { | 
53  | 0  |   EC_KEY *client_key = NULL;  | 
54  | 0  |   const EC_GROUP *group;  | 
55  | 0  |   const EC_POINT *public_key;  | 
56  | 0  |   struct sshbuf *buf = NULL;  | 
57  | 0  |   int r;  | 
58  |  | 
  | 
59  | 0  |   if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { | 
60  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
61  | 0  |     goto out;  | 
62  | 0  |   }  | 
63  | 0  |   if (EC_KEY_generate_key(client_key) != 1) { | 
64  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
65  | 0  |     goto out;  | 
66  | 0  |   }  | 
67  | 0  |   group = EC_KEY_get0_group(client_key);  | 
68  | 0  |   public_key = EC_KEY_get0_public_key(client_key);  | 
69  |  | 
  | 
70  | 0  |   if ((buf = sshbuf_new()) == NULL) { | 
71  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
72  | 0  |     goto out;  | 
73  | 0  |   }  | 
74  | 0  |   if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 ||  | 
75  | 0  |       (r = sshbuf_get_u32(buf, NULL)) != 0)  | 
76  | 0  |     goto out;  | 
77  |  | #ifdef DEBUG_KEXECDH  | 
78  |  |   fputs("client private key:\n", stderr); | 
79  |  |   sshkey_dump_ec_key(client_key);  | 
80  |  | #endif  | 
81  | 0  |   kex->ec_client_key = client_key;  | 
82  | 0  |   kex->ec_group = group;  | 
83  | 0  |   client_key = NULL; /* owned by the kex */  | 
84  | 0  |   kex->client_pub = buf;  | 
85  | 0  |   buf = NULL;  | 
86  | 0  |  out:  | 
87  | 0  |   EC_KEY_free(client_key);  | 
88  | 0  |   sshbuf_free(buf);  | 
89  | 0  |   return r;  | 
90  | 0  | }  | 
91  |  |  | 
92  |  | int  | 
93  |  | kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,  | 
94  |  |     struct sshbuf **server_blobp, struct sshbuf **shared_secretp)  | 
95  | 0  | { | 
96  | 0  |   const EC_GROUP *group;  | 
97  | 0  |   const EC_POINT *pub_key;  | 
98  | 0  |   EC_KEY *server_key = NULL;  | 
99  | 0  |   struct sshbuf *server_blob = NULL;  | 
100  | 0  |   int r;  | 
101  |  | 
  | 
102  | 0  |   *server_blobp = NULL;  | 
103  | 0  |   *shared_secretp = NULL;  | 
104  |  | 
  | 
105  | 0  |   if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { | 
106  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
107  | 0  |     goto out;  | 
108  | 0  |   }  | 
109  | 0  |   if (EC_KEY_generate_key(server_key) != 1) { | 
110  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
111  | 0  |     goto out;  | 
112  | 0  |   }  | 
113  | 0  |   group = EC_KEY_get0_group(server_key);  | 
114  |  | 
  | 
115  |  | #ifdef DEBUG_KEXECDH  | 
116  |  |   fputs("server private key:\n", stderr); | 
117  |  |   sshkey_dump_ec_key(server_key);  | 
118  |  | #endif  | 
119  | 0  |   pub_key = EC_KEY_get0_public_key(server_key);  | 
120  | 0  |   if ((server_blob = sshbuf_new()) == NULL) { | 
121  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
122  | 0  |     goto out;  | 
123  | 0  |   }  | 
124  | 0  |   if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 ||  | 
125  | 0  |       (r = sshbuf_get_u32(server_blob, NULL)) != 0)  | 
126  | 0  |     goto out;  | 
127  | 0  |   if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group,  | 
128  | 0  |       shared_secretp)) != 0)  | 
129  | 0  |     goto out;  | 
130  | 0  |   *server_blobp = server_blob;  | 
131  | 0  |   server_blob = NULL;  | 
132  | 0  |  out:  | 
133  | 0  |   EC_KEY_free(server_key);  | 
134  | 0  |   sshbuf_free(server_blob);  | 
135  | 0  |   return r;  | 
136  | 0  | }  | 
137  |  |  | 
138  |  | static int  | 
139  |  | kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,  | 
140  |  |     EC_KEY *key, const EC_GROUP *group, struct sshbuf **shared_secretp)  | 
141  | 0  | { | 
142  | 0  |   struct sshbuf *buf = NULL;  | 
143  | 0  |   BIGNUM *shared_secret = NULL;  | 
144  | 0  |   EC_POINT *dh_pub = NULL;  | 
145  | 0  |   u_char *kbuf = NULL;  | 
146  | 0  |   size_t klen = 0;  | 
147  | 0  |   int r;  | 
148  |  | 
  | 
149  | 0  |   *shared_secretp = NULL;  | 
150  |  | 
  | 
151  | 0  |   if ((buf = sshbuf_new()) == NULL) { | 
152  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
153  | 0  |     goto out;  | 
154  | 0  |   }  | 
155  | 0  |   if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)  | 
156  | 0  |     goto out;  | 
157  | 0  |   if ((dh_pub = EC_POINT_new(group)) == NULL) { | 
158  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
159  | 0  |     goto out;  | 
160  | 0  |   }  | 
161  | 0  |   if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) { | 
162  | 0  |     goto out;  | 
163  | 0  |   }  | 
164  | 0  |   sshbuf_reset(buf);  | 
165  |  | 
  | 
166  |  | #ifdef DEBUG_KEXECDH  | 
167  |  |   fputs("public key:\n", stderr); | 
168  |  |   sshkey_dump_ec_point(group, dh_pub);  | 
169  |  | #endif  | 
170  | 0  |   if (sshkey_ec_validate_public(group, dh_pub) != 0) { | 
171  | 0  |     r = SSH_ERR_MESSAGE_INCOMPLETE;  | 
172  | 0  |     goto out;  | 
173  | 0  |   }  | 
174  | 0  |   klen = (EC_GROUP_get_degree(group) + 7) / 8;  | 
175  | 0  |   if ((kbuf = malloc(klen)) == NULL ||  | 
176  | 0  |       (shared_secret = BN_new()) == NULL) { | 
177  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
178  | 0  |     goto out;  | 
179  | 0  |   }  | 
180  | 0  |   if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen ||  | 
181  | 0  |       BN_bin2bn(kbuf, klen, shared_secret) == NULL) { | 
182  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
183  | 0  |     goto out;  | 
184  | 0  |   }  | 
185  |  | #ifdef DEBUG_KEXECDH  | 
186  |  |   dump_digest("shared secret", kbuf, klen); | 
187  |  | #endif  | 
188  | 0  |   if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)  | 
189  | 0  |     goto out;  | 
190  | 0  |   *shared_secretp = buf;  | 
191  | 0  |   buf = NULL;  | 
192  | 0  |  out:  | 
193  | 0  |   EC_POINT_clear_free(dh_pub);  | 
194  | 0  |   BN_clear_free(shared_secret);  | 
195  | 0  |   freezero(kbuf, klen);  | 
196  | 0  |   sshbuf_free(buf);  | 
197  | 0  |   return r;  | 
198  | 0  | }  | 
199  |  |  | 
200  |  | int  | 
201  |  | kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,  | 
202  |  |     struct sshbuf **shared_secretp)  | 
203  | 0  | { | 
204  | 0  |   int r;  | 
205  |  | 
  | 
206  | 0  |   r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key,  | 
207  | 0  |       kex->ec_group, shared_secretp);  | 
208  | 0  |   EC_KEY_free(kex->ec_client_key);  | 
209  |  |   kex->ec_client_key = NULL;  | 
210  | 0  |   return r;  | 
211  | 0  | }  | 
212  |  |  | 
213  |  | #else  | 
214  |  |  | 
215  |  | #include "ssherr.h"  | 
216  |  |  | 
217  |  | struct kex;  | 
218  |  | struct sshbuf;  | 
219  |  | struct sshkey;  | 
220  |  |  | 
221  |  | int  | 
222  |  | kex_ecdh_keypair(struct kex *kex)  | 
223  |  | { | 
224  |  |   return SSH_ERR_SIGN_ALG_UNSUPPORTED;  | 
225  |  | }  | 
226  |  |  | 
227  |  | int  | 
228  |  | kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,  | 
229  |  |     struct sshbuf **server_blobp, struct sshbuf **shared_secretp)  | 
230  |  | { | 
231  |  |   return SSH_ERR_SIGN_ALG_UNSUPPORTED;  | 
232  |  | }  | 
233  |  |  | 
234  |  | int  | 
235  |  | kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,  | 
236  |  |     struct sshbuf **shared_secretp)  | 
237  |  | { | 
238  |  |   return SSH_ERR_SIGN_ALG_UNSUPPORTED;  | 
239  |  | }  | 
240  |  | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */  |