Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* $OpenBSD: ssh-rsa.c,v 1.79 2023/03/05 05:34:09 dtucker Exp $ */  | 
2  |  | /*  | 
3  |  |  * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>  | 
4  |  |  *  | 
5  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
6  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
7  |  |  * copyright notice and this permission notice appear in all copies.  | 
8  |  |  *  | 
9  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
10  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
11  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
12  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
13  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
14  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
15  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
16  |  |  */  | 
17  |  |  | 
18  |  | #include "includes.h"  | 
19  |  |  | 
20  |  | #ifdef WITH_OPENSSL  | 
21  |  |  | 
22  |  | #include <sys/types.h>  | 
23  |  |  | 
24  |  | #include <openssl/evp.h>  | 
25  |  | #include <openssl/err.h>  | 
26  |  |  | 
27  |  | #include <stdarg.h>  | 
28  |  | #include <string.h>  | 
29  |  |  | 
30  |  | #include "sshbuf.h"  | 
31  |  | #include "ssherr.h"  | 
32  |  | #define SSHKEY_INTERNAL  | 
33  |  | #include "sshkey.h"  | 
34  |  | #include "digest.h"  | 
35  |  | #include "log.h"  | 
36  |  |  | 
37  |  | #include "openbsd-compat/openssl-compat.h"  | 
38  |  |  | 
39  |  | static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);  | 
40  |  |  | 
41  |  | static u_int  | 
42  |  | ssh_rsa_size(const struct sshkey *key)  | 
43  | 0  | { | 
44  | 0  |   const BIGNUM *rsa_n;  | 
45  |  | 
  | 
46  | 0  |   if (key->rsa == NULL)  | 
47  | 0  |     return 0;  | 
48  | 0  |   RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);  | 
49  | 0  |   return BN_num_bits(rsa_n);  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | static int  | 
53  |  | ssh_rsa_alloc(struct sshkey *k)  | 
54  | 7.67k  | { | 
55  | 7.67k  |   if ((k->rsa = RSA_new()) == NULL)  | 
56  | 0  |     return SSH_ERR_ALLOC_FAIL;  | 
57  | 7.67k  |   return 0;  | 
58  | 7.67k  | }  | 
59  |  |  | 
60  |  | static void  | 
61  |  | ssh_rsa_cleanup(struct sshkey *k)  | 
62  | 7.67k  | { | 
63  | 7.67k  |   RSA_free(k->rsa);  | 
64  | 7.67k  |   k->rsa = NULL;  | 
65  | 7.67k  | }  | 
66  |  |  | 
67  |  | static int  | 
68  |  | ssh_rsa_equal(const struct sshkey *a, const struct sshkey *b)  | 
69  | 3.06k  | { | 
70  | 3.06k  |   const BIGNUM *rsa_e_a, *rsa_n_a;  | 
71  | 3.06k  |   const BIGNUM *rsa_e_b, *rsa_n_b;  | 
72  |  |  | 
73  | 3.06k  |   if (a->rsa == NULL || b->rsa == NULL)  | 
74  | 0  |     return 0;  | 
75  | 3.06k  |   RSA_get0_key(a->rsa, &rsa_n_a, &rsa_e_a, NULL);  | 
76  | 3.06k  |   RSA_get0_key(b->rsa, &rsa_n_b, &rsa_e_b, NULL);  | 
77  | 3.06k  |   if (rsa_e_a == NULL || rsa_e_b == NULL)  | 
78  | 0  |     return 0;  | 
79  | 3.06k  |   if (rsa_n_a == NULL || rsa_n_b == NULL)  | 
80  | 0  |     return 0;  | 
81  | 3.06k  |   if (BN_cmp(rsa_e_a, rsa_e_b) != 0)  | 
82  | 0  |     return 0;  | 
83  | 3.06k  |   if (BN_cmp(rsa_n_a, rsa_n_b) != 0)  | 
84  | 0  |     return 0;  | 
85  | 3.06k  |   return 1;  | 
86  | 3.06k  | }  | 
87  |  |  | 
88  |  | static int  | 
89  |  | ssh_rsa_serialize_public(const struct sshkey *key, struct sshbuf *b,  | 
90  |  |     enum sshkey_serialize_rep opts)  | 
91  | 3.76k  | { | 
92  | 3.76k  |   int r;  | 
93  | 3.76k  |   const BIGNUM *rsa_n, *rsa_e;  | 
94  |  |  | 
95  | 3.76k  |   if (key->rsa == NULL)  | 
96  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
97  | 3.76k  |   RSA_get0_key(key->rsa, &rsa_n, &rsa_e, NULL);  | 
98  | 3.76k  |   if ((r = sshbuf_put_bignum2(b, rsa_e)) != 0 ||  | 
99  | 3.76k  |       (r = sshbuf_put_bignum2(b, rsa_n)) != 0)  | 
100  | 0  |     return r;  | 
101  |  |  | 
102  | 3.76k  |   return 0;  | 
103  | 3.76k  | }  | 
104  |  |  | 
105  |  | static int  | 
106  |  | ssh_rsa_serialize_private(const struct sshkey *key, struct sshbuf *b,  | 
107  |  |     enum sshkey_serialize_rep opts)  | 
108  | 0  | { | 
109  | 0  |   int r;  | 
110  | 0  |   const BIGNUM *rsa_n, *rsa_e, *rsa_d, *rsa_iqmp, *rsa_p, *rsa_q;  | 
111  |  | 
  | 
112  | 0  |   RSA_get0_key(key->rsa, &rsa_n, &rsa_e, &rsa_d);  | 
113  | 0  |   RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);  | 
114  | 0  |   RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp);  | 
115  |  | 
  | 
116  | 0  |   if (!sshkey_is_cert(key)) { | 
117  |  |     /* Note: can't reuse ssh_rsa_serialize_public: e, n vs. n, e */  | 
118  | 0  |     if ((r = sshbuf_put_bignum2(b, rsa_n)) != 0 ||  | 
119  | 0  |         (r = sshbuf_put_bignum2(b, rsa_e)) != 0)  | 
120  | 0  |       return r;  | 
121  | 0  |   }  | 
122  | 0  |   if ((r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||  | 
123  | 0  |       (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||  | 
124  | 0  |       (r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||  | 
125  | 0  |       (r = sshbuf_put_bignum2(b, rsa_q)) != 0)  | 
126  | 0  |     return r;  | 
127  |  |  | 
128  | 0  |   return 0;  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static int  | 
132  |  | ssh_rsa_generate(struct sshkey *k, int bits)  | 
133  | 0  | { | 
134  | 0  |   RSA *private = NULL;  | 
135  | 0  |   BIGNUM *f4 = NULL;  | 
136  | 0  |   int ret = SSH_ERR_INTERNAL_ERROR;  | 
137  |  | 
  | 
138  | 0  |   if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||  | 
139  | 0  |       bits > SSHBUF_MAX_BIGNUM * 8)  | 
140  | 0  |     return SSH_ERR_KEY_LENGTH;  | 
141  | 0  |   if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) { | 
142  | 0  |     ret = SSH_ERR_ALLOC_FAIL;  | 
143  | 0  |     goto out;  | 
144  | 0  |   }  | 
145  | 0  |   if (!BN_set_word(f4, RSA_F4) ||  | 
146  | 0  |       !RSA_generate_key_ex(private, bits, f4, NULL)) { | 
147  | 0  |     ret = SSH_ERR_LIBCRYPTO_ERROR;  | 
148  | 0  |     goto out;  | 
149  | 0  |   }  | 
150  | 0  |   k->rsa = private;  | 
151  | 0  |   private = NULL;  | 
152  | 0  |   ret = 0;  | 
153  | 0  |  out:  | 
154  | 0  |   RSA_free(private);  | 
155  | 0  |   BN_free(f4);  | 
156  | 0  |   return ret;  | 
157  | 0  | }  | 
158  |  |  | 
159  |  | static int  | 
160  |  | ssh_rsa_copy_public(const struct sshkey *from, struct sshkey *to)  | 
161  | 0  | { | 
162  | 0  |   const BIGNUM *rsa_n, *rsa_e;  | 
163  | 0  |   BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL;  | 
164  | 0  |   int r = SSH_ERR_INTERNAL_ERROR;  | 
165  |  | 
  | 
166  | 0  |   RSA_get0_key(from->rsa, &rsa_n, &rsa_e, NULL);  | 
167  | 0  |   if ((rsa_n_dup = BN_dup(rsa_n)) == NULL ||  | 
168  | 0  |       (rsa_e_dup = BN_dup(rsa_e)) == NULL) { | 
169  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
170  | 0  |     goto out;  | 
171  | 0  |   }  | 
172  | 0  |   if (!RSA_set0_key(to->rsa, rsa_n_dup, rsa_e_dup, NULL)) { | 
173  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
174  | 0  |     goto out;  | 
175  | 0  |   }  | 
176  | 0  |   rsa_n_dup = rsa_e_dup = NULL; /* transferred */  | 
177  |  |   /* success */  | 
178  | 0  |   r = 0;  | 
179  | 0  |  out:  | 
180  | 0  |   BN_clear_free(rsa_n_dup);  | 
181  | 0  |   BN_clear_free(rsa_e_dup);  | 
182  | 0  |   return r;  | 
183  | 0  | }  | 
184  |  |  | 
185  |  | static int  | 
186  |  | ssh_rsa_deserialize_public(const char *ktype, struct sshbuf *b,  | 
187  |  |     struct sshkey *key)  | 
188  | 4.59k  | { | 
189  | 4.59k  |   int ret = SSH_ERR_INTERNAL_ERROR;  | 
190  | 4.59k  |   BIGNUM *rsa_n = NULL, *rsa_e = NULL;  | 
191  |  |  | 
192  | 4.59k  |   if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||  | 
193  | 4.59k  |       sshbuf_get_bignum2(b, &rsa_n) != 0) { | 
194  | 0  |     ret = SSH_ERR_INVALID_FORMAT;  | 
195  | 0  |     goto out;  | 
196  | 0  |   }  | 
197  | 4.59k  |   if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, NULL)) { | 
198  | 0  |     ret = SSH_ERR_LIBCRYPTO_ERROR;  | 
199  | 0  |     goto out;  | 
200  | 0  |   }  | 
201  | 4.59k  |   rsa_n = rsa_e = NULL; /* transferred */  | 
202  | 4.59k  |   if ((ret = sshkey_check_rsa_length(key, 0)) != 0)  | 
203  | 0  |     goto out;  | 
204  |  | #ifdef DEBUG_PK  | 
205  |  |   RSA_print_fp(stderr, key->rsa, 8);  | 
206  |  | #endif  | 
207  |  |   /* success */  | 
208  | 4.59k  |   ret = 0;  | 
209  | 4.59k  |  out:  | 
210  | 4.59k  |   BN_clear_free(rsa_n);  | 
211  | 4.59k  |   BN_clear_free(rsa_e);  | 
212  | 4.59k  |   return ret;  | 
213  | 4.59k  | }  | 
214  |  |  | 
215  |  | static int  | 
216  |  | ssh_rsa_deserialize_private(const char *ktype, struct sshbuf *b,  | 
217  |  |     struct sshkey *key)  | 
218  | 3.07k  | { | 
219  | 3.07k  |   int r;  | 
220  | 3.07k  |   BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;  | 
221  | 3.07k  |   BIGNUM *rsa_iqmp = NULL, *rsa_p = NULL, *rsa_q = NULL;  | 
222  |  |  | 
223  |  |   /* Note: can't reuse ssh_rsa_deserialize_public: e, n vs. n, e */  | 
224  | 3.07k  |   if (!sshkey_is_cert(key)) { | 
225  | 3.07k  |     if ((r = sshbuf_get_bignum2(b, &rsa_n)) != 0 ||  | 
226  | 3.07k  |         (r = sshbuf_get_bignum2(b, &rsa_e)) != 0)  | 
227  | 4  |       goto out;  | 
228  | 3.07k  |     if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, NULL)) { | 
229  | 0  |       r = SSH_ERR_LIBCRYPTO_ERROR;  | 
230  | 0  |       goto out;  | 
231  | 0  |     }  | 
232  | 3.07k  |     rsa_n = rsa_e = NULL; /* transferred */  | 
233  | 3.07k  |   }  | 
234  | 3.07k  |   if ((r = sshbuf_get_bignum2(b, &rsa_d)) != 0 ||  | 
235  | 3.07k  |       (r = sshbuf_get_bignum2(b, &rsa_iqmp)) != 0 ||  | 
236  | 3.07k  |       (r = sshbuf_get_bignum2(b, &rsa_p)) != 0 ||  | 
237  | 3.07k  |       (r = sshbuf_get_bignum2(b, &rsa_q)) != 0)  | 
238  | 5  |     goto out;  | 
239  | 3.06k  |   if (!RSA_set0_key(key->rsa, NULL, NULL, rsa_d)) { | 
240  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
241  | 0  |     goto out;  | 
242  | 0  |   }  | 
243  | 3.06k  |   rsa_d = NULL; /* transferred */  | 
244  | 3.06k  |   if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q)) { | 
245  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
246  | 0  |     goto out;  | 
247  | 0  |   }  | 
248  | 3.06k  |   rsa_p = rsa_q = NULL; /* transferred */  | 
249  | 3.06k  |   if ((r = sshkey_check_rsa_length(key, 0)) != 0)  | 
250  | 2  |     goto out;  | 
251  | 3.06k  |   if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)  | 
252  | 0  |     goto out;  | 
253  | 3.06k  |   if (RSA_blinding_on(key->rsa, NULL) != 1) { | 
254  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
255  | 0  |     goto out;  | 
256  | 0  |   }  | 
257  |  |   /* success */  | 
258  | 3.06k  |   r = 0;  | 
259  | 3.07k  |  out:  | 
260  | 3.07k  |   BN_clear_free(rsa_n);  | 
261  | 3.07k  |   BN_clear_free(rsa_e);  | 
262  | 3.07k  |   BN_clear_free(rsa_d);  | 
263  | 3.07k  |   BN_clear_free(rsa_p);  | 
264  | 3.07k  |   BN_clear_free(rsa_q);  | 
265  | 3.07k  |   BN_clear_free(rsa_iqmp);  | 
266  | 3.07k  |   return r;  | 
267  | 3.06k  | }  | 
268  |  |  | 
269  |  | static const char *  | 
270  |  | rsa_hash_alg_ident(int hash_alg)  | 
271  | 0  | { | 
272  | 0  |   switch (hash_alg) { | 
273  | 0  |   case SSH_DIGEST_SHA1:  | 
274  | 0  |     return "ssh-rsa";  | 
275  | 0  |   case SSH_DIGEST_SHA256:  | 
276  | 0  |     return "rsa-sha2-256";  | 
277  | 0  |   case SSH_DIGEST_SHA512:  | 
278  | 0  |     return "rsa-sha2-512";  | 
279  | 0  |   }  | 
280  | 0  |   return NULL;  | 
281  | 0  | }  | 
282  |  |  | 
283  |  | /*  | 
284  |  |  * Returns the hash algorithm ID for a given algorithm identifier as used  | 
285  |  |  * inside the signature blob,  | 
286  |  |  */  | 
287  |  | static int  | 
288  |  | rsa_hash_id_from_ident(const char *ident)  | 
289  | 0  | { | 
290  | 0  |   if (strcmp(ident, "ssh-rsa") == 0)  | 
291  | 0  |     return SSH_DIGEST_SHA1;  | 
292  | 0  |   if (strcmp(ident, "rsa-sha2-256") == 0)  | 
293  | 0  |     return SSH_DIGEST_SHA256;  | 
294  | 0  |   if (strcmp(ident, "rsa-sha2-512") == 0)  | 
295  | 0  |     return SSH_DIGEST_SHA512;  | 
296  | 0  |   return -1;  | 
297  | 0  | }  | 
298  |  |  | 
299  |  | /*  | 
300  |  |  * Return the hash algorithm ID for the specified key name. This includes  | 
301  |  |  * all the cases of rsa_hash_id_from_ident() but also the certificate key  | 
302  |  |  * types.  | 
303  |  |  */  | 
304  |  | static int  | 
305  |  | rsa_hash_id_from_keyname(const char *alg)  | 
306  | 0  | { | 
307  | 0  |   int r;  | 
308  |  | 
  | 
309  | 0  |   if ((r = rsa_hash_id_from_ident(alg)) != -1)  | 
310  | 0  |     return r;  | 
311  | 0  |   if (strcmp(alg, "ssh-rsa-cert-v01@openssh.com") == 0)  | 
312  | 0  |     return SSH_DIGEST_SHA1;  | 
313  | 0  |   if (strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)  | 
314  | 0  |     return SSH_DIGEST_SHA256;  | 
315  | 0  |   if (strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)  | 
316  | 0  |     return SSH_DIGEST_SHA512;  | 
317  | 0  |   return -1;  | 
318  | 0  | }  | 
319  |  |  | 
320  |  | static int  | 
321  |  | rsa_hash_alg_nid(int type)  | 
322  | 0  | { | 
323  | 0  |   switch (type) { | 
324  | 0  |   case SSH_DIGEST_SHA1:  | 
325  | 0  |     return NID_sha1;  | 
326  | 0  |   case SSH_DIGEST_SHA256:  | 
327  | 0  |     return NID_sha256;  | 
328  | 0  |   case SSH_DIGEST_SHA512:  | 
329  | 0  |     return NID_sha512;  | 
330  | 0  |   default:  | 
331  | 0  |     return -1;  | 
332  | 0  |   }  | 
333  | 0  | }  | 
334  |  |  | 
335  |  | int  | 
336  |  | ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)  | 
337  | 3.06k  | { | 
338  | 3.06k  |   const BIGNUM *rsa_p, *rsa_q, *rsa_d;  | 
339  | 3.06k  |   BIGNUM *aux = NULL, *d_consttime = NULL;  | 
340  | 3.06k  |   BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;  | 
341  | 3.06k  |   BN_CTX *ctx = NULL;  | 
342  | 3.06k  |   int r;  | 
343  |  |  | 
344  | 3.06k  |   if (key == NULL || key->rsa == NULL ||  | 
345  | 3.06k  |       sshkey_type_plain(key->type) != KEY_RSA)  | 
346  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
347  |  |  | 
348  | 3.06k  |   RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);  | 
349  | 3.06k  |   RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);  | 
350  |  |  | 
351  | 3.06k  |   if ((ctx = BN_CTX_new()) == NULL)  | 
352  | 0  |     return SSH_ERR_ALLOC_FAIL;  | 
353  | 3.06k  |   if ((aux = BN_new()) == NULL ||  | 
354  | 3.06k  |       (rsa_dmq1 = BN_new()) == NULL ||  | 
355  | 3.06k  |       (rsa_dmp1 = BN_new()) == NULL)  | 
356  | 0  |     return SSH_ERR_ALLOC_FAIL;  | 
357  | 3.06k  |   if ((d_consttime = BN_dup(rsa_d)) == NULL ||  | 
358  | 3.06k  |       (rsa_iqmp = BN_dup(iqmp)) == NULL) { | 
359  | 0  |     r = SSH_ERR_ALLOC_FAIL;  | 
360  | 0  |     goto out;  | 
361  | 0  |   }  | 
362  | 3.06k  |   BN_set_flags(aux, BN_FLG_CONSTTIME);  | 
363  | 3.06k  |   BN_set_flags(d_consttime, BN_FLG_CONSTTIME);  | 
364  |  |  | 
365  | 3.06k  |   if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||  | 
366  | 3.06k  |       (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||  | 
367  | 3.06k  |       (BN_sub(aux, rsa_p, BN_value_one()) == 0) ||  | 
368  | 3.06k  |       (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) { | 
369  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
370  | 0  |     goto out;  | 
371  | 0  |   }  | 
372  | 3.06k  |   if (!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) { | 
373  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
374  | 0  |     goto out;  | 
375  | 0  |   }  | 
376  | 3.06k  |   rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */  | 
377  |  |   /* success */  | 
378  | 3.06k  |   r = 0;  | 
379  | 3.06k  |  out:  | 
380  | 3.06k  |   BN_clear_free(aux);  | 
381  | 3.06k  |   BN_clear_free(d_consttime);  | 
382  | 3.06k  |   BN_clear_free(rsa_dmp1);  | 
383  | 3.06k  |   BN_clear_free(rsa_dmq1);  | 
384  | 3.06k  |   BN_clear_free(rsa_iqmp);  | 
385  | 3.06k  |   BN_CTX_free(ctx);  | 
386  | 3.06k  |   return r;  | 
387  | 3.06k  | }  | 
388  |  |  | 
389  |  | /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */  | 
390  |  | static int  | 
391  |  | ssh_rsa_sign(struct sshkey *key,  | 
392  |  |     u_char **sigp, size_t *lenp,  | 
393  |  |     const u_char *data, size_t datalen,  | 
394  |  |     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)  | 
395  | 0  | { | 
396  | 0  |   const BIGNUM *rsa_n;  | 
397  | 0  |   u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;  | 
398  | 0  |   size_t slen = 0;  | 
399  | 0  |   u_int hlen, len;  | 
400  | 0  |   int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;  | 
401  | 0  |   struct sshbuf *b = NULL;  | 
402  |  | 
  | 
403  | 0  |   if (lenp != NULL)  | 
404  | 0  |     *lenp = 0;  | 
405  | 0  |   if (sigp != NULL)  | 
406  | 0  |     *sigp = NULL;  | 
407  |  | 
  | 
408  | 0  |   if (alg == NULL || strlen(alg) == 0)  | 
409  | 0  |     hash_alg = SSH_DIGEST_SHA1;  | 
410  | 0  |   else  | 
411  | 0  |     hash_alg = rsa_hash_id_from_keyname(alg);  | 
412  | 0  |   if (key == NULL || key->rsa == NULL || hash_alg == -1 ||  | 
413  | 0  |       sshkey_type_plain(key->type) != KEY_RSA)  | 
414  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
415  | 0  |   RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);  | 
416  | 0  |   if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)  | 
417  | 0  |     return SSH_ERR_KEY_LENGTH;  | 
418  | 0  |   slen = RSA_size(key->rsa);  | 
419  | 0  |   if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)  | 
420  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
421  |  |  | 
422  |  |   /* hash the data */  | 
423  | 0  |   nid = rsa_hash_alg_nid(hash_alg);  | 
424  | 0  |   if ((hlen = ssh_digest_bytes(hash_alg)) == 0)  | 
425  | 0  |     return SSH_ERR_INTERNAL_ERROR;  | 
426  | 0  |   if ((ret = ssh_digest_memory(hash_alg, data, datalen,  | 
427  | 0  |       digest, sizeof(digest))) != 0)  | 
428  | 0  |     goto out;  | 
429  |  |  | 
430  | 0  |   if ((sig = malloc(slen)) == NULL) { | 
431  | 0  |     ret = SSH_ERR_ALLOC_FAIL;  | 
432  | 0  |     goto out;  | 
433  | 0  |   }  | 
434  |  |  | 
435  | 0  |   if (RSA_sign(nid, digest, hlen, sig, &len, key->rsa) != 1) { | 
436  | 0  |     ret = SSH_ERR_LIBCRYPTO_ERROR;  | 
437  | 0  |     goto out;  | 
438  | 0  |   }  | 
439  | 0  |   if (len < slen) { | 
440  | 0  |     size_t diff = slen - len;  | 
441  | 0  |     memmove(sig + diff, sig, len);  | 
442  | 0  |     explicit_bzero(sig, diff);  | 
443  | 0  |   } else if (len > slen) { | 
444  | 0  |     ret = SSH_ERR_INTERNAL_ERROR;  | 
445  | 0  |     goto out;  | 
446  | 0  |   }  | 
447  |  |   /* encode signature */  | 
448  | 0  |   if ((b = sshbuf_new()) == NULL) { | 
449  | 0  |     ret = SSH_ERR_ALLOC_FAIL;  | 
450  | 0  |     goto out;  | 
451  | 0  |   }  | 
452  | 0  |   if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||  | 
453  | 0  |       (ret = sshbuf_put_string(b, sig, slen)) != 0)  | 
454  | 0  |     goto out;  | 
455  | 0  |   len = sshbuf_len(b);  | 
456  | 0  |   if (sigp != NULL) { | 
457  | 0  |     if ((*sigp = malloc(len)) == NULL) { | 
458  | 0  |       ret = SSH_ERR_ALLOC_FAIL;  | 
459  | 0  |       goto out;  | 
460  | 0  |     }  | 
461  | 0  |     memcpy(*sigp, sshbuf_ptr(b), len);  | 
462  | 0  |   }  | 
463  | 0  |   if (lenp != NULL)  | 
464  | 0  |     *lenp = len;  | 
465  | 0  |   ret = 0;  | 
466  | 0  |  out:  | 
467  | 0  |   explicit_bzero(digest, sizeof(digest));  | 
468  | 0  |   freezero(sig, slen);  | 
469  | 0  |   sshbuf_free(b);  | 
470  | 0  |   return ret;  | 
471  | 0  | }  | 
472  |  |  | 
473  |  | static int  | 
474  |  | ssh_rsa_verify(const struct sshkey *key,  | 
475  |  |     const u_char *sig, size_t siglen,  | 
476  |  |     const u_char *data, size_t dlen, const char *alg, u_int compat,  | 
477  |  |     struct sshkey_sig_details **detailsp)  | 
478  | 0  | { | 
479  | 0  |   const BIGNUM *rsa_n;  | 
480  | 0  |   char *sigtype = NULL;  | 
481  | 0  |   int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;  | 
482  | 0  |   size_t len = 0, diff, modlen, hlen;  | 
483  | 0  |   struct sshbuf *b = NULL;  | 
484  | 0  |   u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;  | 
485  |  | 
  | 
486  | 0  |   if (key == NULL || key->rsa == NULL ||  | 
487  | 0  |       sshkey_type_plain(key->type) != KEY_RSA ||  | 
488  | 0  |       sig == NULL || siglen == 0)  | 
489  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
490  | 0  |   RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);  | 
491  | 0  |   if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)  | 
492  | 0  |     return SSH_ERR_KEY_LENGTH;  | 
493  |  |  | 
494  | 0  |   if ((b = sshbuf_from(sig, siglen)) == NULL)  | 
495  | 0  |     return SSH_ERR_ALLOC_FAIL;  | 
496  | 0  |   if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) { | 
497  | 0  |     ret = SSH_ERR_INVALID_FORMAT;  | 
498  | 0  |     goto out;  | 
499  | 0  |   }  | 
500  | 0  |   if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) { | 
501  | 0  |     ret = SSH_ERR_KEY_TYPE_MISMATCH;  | 
502  | 0  |     goto out;  | 
503  | 0  |   }  | 
504  |  |   /*  | 
505  |  |    * Allow ssh-rsa-cert-v01 certs to generate SHA2 signatures for  | 
506  |  |    * legacy reasons, but otherwise the signature type should match.  | 
507  |  |    */  | 
508  | 0  |   if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) { | 
509  | 0  |     if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) { | 
510  | 0  |       ret = SSH_ERR_INVALID_ARGUMENT;  | 
511  | 0  |       goto out;  | 
512  | 0  |     }  | 
513  | 0  |     if (hash_alg != want_alg) { | 
514  | 0  |       ret = SSH_ERR_SIGNATURE_INVALID;  | 
515  | 0  |       goto out;  | 
516  | 0  |     }  | 
517  | 0  |   }  | 
518  | 0  |   if (sshbuf_get_string(b, &sigblob, &len) != 0) { | 
519  | 0  |     ret = SSH_ERR_INVALID_FORMAT;  | 
520  | 0  |     goto out;  | 
521  | 0  |   }  | 
522  | 0  |   if (sshbuf_len(b) != 0) { | 
523  | 0  |     ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;  | 
524  | 0  |     goto out;  | 
525  | 0  |   }  | 
526  |  |   /* RSA_verify expects a signature of RSA_size */  | 
527  | 0  |   modlen = RSA_size(key->rsa);  | 
528  | 0  |   if (len > modlen) { | 
529  | 0  |     ret = SSH_ERR_KEY_BITS_MISMATCH;  | 
530  | 0  |     goto out;  | 
531  | 0  |   } else if (len < modlen) { | 
532  | 0  |     diff = modlen - len;  | 
533  | 0  |     osigblob = sigblob;  | 
534  | 0  |     if ((sigblob = realloc(sigblob, modlen)) == NULL) { | 
535  | 0  |       sigblob = osigblob; /* put it back for clear/free */  | 
536  | 0  |       ret = SSH_ERR_ALLOC_FAIL;  | 
537  | 0  |       goto out;  | 
538  | 0  |     }  | 
539  | 0  |     memmove(sigblob + diff, sigblob, len);  | 
540  | 0  |     explicit_bzero(sigblob, diff);  | 
541  | 0  |     len = modlen;  | 
542  | 0  |   }  | 
543  | 0  |   if ((hlen = ssh_digest_bytes(hash_alg)) == 0) { | 
544  | 0  |     ret = SSH_ERR_INTERNAL_ERROR;  | 
545  | 0  |     goto out;  | 
546  | 0  |   }  | 
547  | 0  |   if ((ret = ssh_digest_memory(hash_alg, data, dlen,  | 
548  | 0  |       digest, sizeof(digest))) != 0)  | 
549  | 0  |     goto out;  | 
550  |  |  | 
551  | 0  |   ret = openssh_RSA_verify(hash_alg, digest, hlen, sigblob, len,  | 
552  | 0  |       key->rsa);  | 
553  | 0  |  out:  | 
554  | 0  |   freezero(sigblob, len);  | 
555  | 0  |   free(sigtype);  | 
556  | 0  |   sshbuf_free(b);  | 
557  | 0  |   explicit_bzero(digest, sizeof(digest));  | 
558  | 0  |   return ret;  | 
559  | 0  | }  | 
560  |  |  | 
561  |  | /*  | 
562  |  |  * See:  | 
563  |  |  * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/  | 
564  |  |  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn  | 
565  |  |  */  | 
566  |  |  | 
567  |  | /*  | 
568  |  |  * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3) | 
569  |  |  *  oiw(14) secsig(3) algorithms(2) 26 }  | 
570  |  |  */  | 
571  |  | static const u_char id_sha1[] = { | 
572  |  |   0x30, 0x21, /* type Sequence, length 0x21 (33) */  | 
573  |  |   0x30, 0x09, /* type Sequence, length 0x09 */  | 
574  |  |   0x06, 0x05, /* type OID, length 0x05 */  | 
575  |  |   0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */  | 
576  |  |   0x05, 0x00, /* NULL */  | 
577  |  |   0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */  | 
578  |  | };  | 
579  |  |  | 
580  |  | /*  | 
581  |  |  * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html  | 
582  |  |  * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) | 
583  |  |  *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)  | 
584  |  |  *      id-sha256(1) }  | 
585  |  |  */  | 
586  |  | static const u_char id_sha256[] = { | 
587  |  |   0x30, 0x31, /* type Sequence, length 0x31 (49) */  | 
588  |  |   0x30, 0x0d, /* type Sequence, length 0x0d (13) */  | 
589  |  |   0x06, 0x09, /* type OID, length 0x09 */  | 
590  |  |   0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */  | 
591  |  |   0x05, 0x00, /* NULL */  | 
592  |  |   0x04, 0x20  /* Octet string, length 0x20 (32), followed by sha256 hash */  | 
593  |  | };  | 
594  |  |  | 
595  |  | /*  | 
596  |  |  * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html  | 
597  |  |  * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840) | 
598  |  |  *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)  | 
599  |  |  *      id-sha256(3) }  | 
600  |  |  */  | 
601  |  | static const u_char id_sha512[] = { | 
602  |  |   0x30, 0x51, /* type Sequence, length 0x51 (81) */  | 
603  |  |   0x30, 0x0d, /* type Sequence, length 0x0d (13) */  | 
604  |  |   0x06, 0x09, /* type OID, length 0x09 */  | 
605  |  |   0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */  | 
606  |  |   0x05, 0x00, /* NULL */  | 
607  |  |   0x04, 0x40  /* Octet string, length 0x40 (64), followed by sha512 hash */  | 
608  |  | };  | 
609  |  |  | 
610  |  | static int  | 
611  |  | rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)  | 
612  | 0  | { | 
613  | 0  |   switch (hash_alg) { | 
614  | 0  |   case SSH_DIGEST_SHA1:  | 
615  | 0  |     *oidp = id_sha1;  | 
616  | 0  |     *oidlenp = sizeof(id_sha1);  | 
617  | 0  |     break;  | 
618  | 0  |   case SSH_DIGEST_SHA256:  | 
619  | 0  |     *oidp = id_sha256;  | 
620  | 0  |     *oidlenp = sizeof(id_sha256);  | 
621  | 0  |     break;  | 
622  | 0  |   case SSH_DIGEST_SHA512:  | 
623  | 0  |     *oidp = id_sha512;  | 
624  | 0  |     *oidlenp = sizeof(id_sha512);  | 
625  | 0  |     break;  | 
626  | 0  |   default:  | 
627  | 0  |     return SSH_ERR_INVALID_ARGUMENT;  | 
628  | 0  |   }  | 
629  | 0  |   return 0;  | 
630  | 0  | }  | 
631  |  |  | 
632  |  | static int  | 
633  |  | openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,  | 
634  |  |     u_char *sigbuf, size_t siglen, RSA *rsa)  | 
635  | 0  | { | 
636  | 0  |   size_t rsasize = 0, oidlen = 0, hlen = 0;  | 
637  | 0  |   int ret, len, oidmatch, hashmatch;  | 
638  | 0  |   const u_char *oid = NULL;  | 
639  | 0  |   u_char *decrypted = NULL;  | 
640  |  | 
  | 
641  | 0  |   if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)  | 
642  | 0  |     return ret;  | 
643  | 0  |   ret = SSH_ERR_INTERNAL_ERROR;  | 
644  | 0  |   hlen = ssh_digest_bytes(hash_alg);  | 
645  | 0  |   if (hashlen != hlen) { | 
646  | 0  |     ret = SSH_ERR_INVALID_ARGUMENT;  | 
647  | 0  |     goto done;  | 
648  | 0  |   }  | 
649  | 0  |   rsasize = RSA_size(rsa);  | 
650  | 0  |   if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||  | 
651  | 0  |       siglen == 0 || siglen > rsasize) { | 
652  | 0  |     ret = SSH_ERR_INVALID_ARGUMENT;  | 
653  | 0  |     goto done;  | 
654  | 0  |   }  | 
655  | 0  |   if ((decrypted = malloc(rsasize)) == NULL) { | 
656  | 0  |     ret = SSH_ERR_ALLOC_FAIL;  | 
657  | 0  |     goto done;  | 
658  | 0  |   }  | 
659  | 0  |   if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,  | 
660  | 0  |       RSA_PKCS1_PADDING)) < 0) { | 
661  | 0  |     ret = SSH_ERR_LIBCRYPTO_ERROR;  | 
662  | 0  |     goto done;  | 
663  | 0  |   }  | 
664  | 0  |   if (len < 0 || (size_t)len != hlen + oidlen) { | 
665  | 0  |     ret = SSH_ERR_INVALID_FORMAT;  | 
666  | 0  |     goto done;  | 
667  | 0  |   }  | 
668  | 0  |   oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;  | 
669  | 0  |   hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;  | 
670  | 0  |   if (!oidmatch || !hashmatch) { | 
671  | 0  |     ret = SSH_ERR_SIGNATURE_INVALID;  | 
672  | 0  |     goto done;  | 
673  | 0  |   }  | 
674  | 0  |   ret = 0;  | 
675  | 0  | done:  | 
676  | 0  |   freezero(decrypted, rsasize);  | 
677  | 0  |   return ret;  | 
678  | 0  | }  | 
679  |  |  | 
680  |  | static const struct sshkey_impl_funcs sshkey_rsa_funcs = { | 
681  |  |   /* .size = */   ssh_rsa_size,  | 
682  |  |   /* .alloc = */    ssh_rsa_alloc,  | 
683  |  |   /* .cleanup = */  ssh_rsa_cleanup,  | 
684  |  |   /* .equal = */    ssh_rsa_equal,  | 
685  |  |   /* .ssh_serialize_public = */ ssh_rsa_serialize_public,  | 
686  |  |   /* .ssh_deserialize_public = */ ssh_rsa_deserialize_public,  | 
687  |  |   /* .ssh_serialize_private = */ ssh_rsa_serialize_private,  | 
688  |  |   /* .ssh_deserialize_private = */ ssh_rsa_deserialize_private,  | 
689  |  |   /* .generate = */ ssh_rsa_generate,  | 
690  |  |   /* .copy_public = */  ssh_rsa_copy_public,  | 
691  |  |   /* .sign = */   ssh_rsa_sign,  | 
692  |  |   /* .verify = */   ssh_rsa_verify,  | 
693  |  | };  | 
694  |  |  | 
695  |  | const struct sshkey_impl sshkey_rsa_impl = { | 
696  |  |   /* .name = */   "ssh-rsa",  | 
697  |  |   /* .shortname = */  "RSA",  | 
698  |  |   /* .sigalg = */   NULL,  | 
699  |  |   /* .type = */   KEY_RSA,  | 
700  |  |   /* .nid = */    0,  | 
701  |  |   /* .cert = */   0,  | 
702  |  |   /* .sigonly = */  0,  | 
703  |  |   /* .keybits = */  0,  | 
704  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
705  |  | };  | 
706  |  |  | 
707  |  | const struct sshkey_impl sshkey_rsa_cert_impl = { | 
708  |  |   /* .name = */   "ssh-rsa-cert-v01@openssh.com",  | 
709  |  |   /* .shortname = */  "RSA-CERT",  | 
710  |  |   /* .sigalg = */   NULL,  | 
711  |  |   /* .type = */   KEY_RSA_CERT,  | 
712  |  |   /* .nid = */    0,  | 
713  |  |   /* .cert = */   1,  | 
714  |  |   /* .sigonly = */  0,  | 
715  |  |   /* .keybits = */  0,  | 
716  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
717  |  | };  | 
718  |  |  | 
719  |  | /* SHA2 signature algorithms */  | 
720  |  |  | 
721  |  | const struct sshkey_impl sshkey_rsa_sha256_impl = { | 
722  |  |   /* .name = */   "rsa-sha2-256",  | 
723  |  |   /* .shortname = */  "RSA",  | 
724  |  |   /* .sigalg = */   NULL,  | 
725  |  |   /* .type = */   KEY_RSA,  | 
726  |  |   /* .nid = */    0,  | 
727  |  |   /* .cert = */   0,  | 
728  |  |   /* .sigonly = */  1,  | 
729  |  |   /* .keybits = */  0,  | 
730  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
731  |  | };  | 
732  |  |  | 
733  |  | const struct sshkey_impl sshkey_rsa_sha512_impl = { | 
734  |  |   /* .name = */   "rsa-sha2-512",  | 
735  |  |   /* .shortname = */  "RSA",  | 
736  |  |   /* .sigalg = */   NULL,  | 
737  |  |   /* .type = */   KEY_RSA,  | 
738  |  |   /* .nid = */    0,  | 
739  |  |   /* .cert = */   0,  | 
740  |  |   /* .sigonly = */  1,  | 
741  |  |   /* .keybits = */  0,  | 
742  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
743  |  | };  | 
744  |  |  | 
745  |  | const struct sshkey_impl sshkey_rsa_sha256_cert_impl = { | 
746  |  |   /* .name = */   "rsa-sha2-256-cert-v01@openssh.com",  | 
747  |  |   /* .shortname = */  "RSA-CERT",  | 
748  |  |   /* .sigalg = */   "rsa-sha2-256",  | 
749  |  |   /* .type = */   KEY_RSA_CERT,  | 
750  |  |   /* .nid = */    0,  | 
751  |  |   /* .cert = */   1,  | 
752  |  |   /* .sigonly = */  1,  | 
753  |  |   /* .keybits = */  0,  | 
754  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
755  |  | };  | 
756  |  |  | 
757  |  | const struct sshkey_impl sshkey_rsa_sha512_cert_impl = { | 
758  |  |   /* .name = */   "rsa-sha2-512-cert-v01@openssh.com",  | 
759  |  |   /* .shortname = */  "RSA-CERT",  | 
760  |  |   /* .sigalg = */   "rsa-sha2-512",  | 
761  |  |   /* .type = */   KEY_RSA_CERT,  | 
762  |  |   /* .nid = */    0,  | 
763  |  |   /* .cert = */   1,  | 
764  |  |   /* .sigonly = */  1,  | 
765  |  |   /* .keybits = */  0,  | 
766  |  |   /* .funcs = */    &sshkey_rsa_funcs,  | 
767  |  | };  | 
768  |  | #endif /* WITH_OPENSSL */  |