Coverage Report

Created: 2024-07-27 06:19

/src/openssh/ssh-rsa.c
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
29.5k
{
55
29.5k
  if ((k->rsa = RSA_new()) == NULL)
56
0
    return SSH_ERR_ALLOC_FAIL;
57
29.5k
  return 0;
58
29.5k
}
59
60
static void
61
ssh_rsa_cleanup(struct sshkey *k)
62
29.5k
{
63
29.5k
  RSA_free(k->rsa);
64
29.5k
  k->rsa = NULL;
65
29.5k
}
66
67
static int
68
ssh_rsa_equal(const struct sshkey *a, const struct sshkey *b)
69
167
{
70
167
  const BIGNUM *rsa_e_a, *rsa_n_a;
71
167
  const BIGNUM *rsa_e_b, *rsa_n_b;
72
73
167
  if (a->rsa == NULL || b->rsa == NULL)
74
0
    return 0;
75
167
  RSA_get0_key(a->rsa, &rsa_n_a, &rsa_e_a, NULL);
76
167
  RSA_get0_key(b->rsa, &rsa_n_b, &rsa_e_b, NULL);
77
167
  if (rsa_e_a == NULL || rsa_e_b == NULL)
78
0
    return 0;
79
167
  if (rsa_n_a == NULL || rsa_n_b == NULL)
80
0
    return 0;
81
167
  if (BN_cmp(rsa_e_a, rsa_e_b) != 0)
82
2
    return 0;
83
165
  if (BN_cmp(rsa_n_a, rsa_n_b) != 0)
84
4
    return 0;
85
161
  return 1;
86
165
}
87
88
static int
89
ssh_rsa_serialize_public(const struct sshkey *key, struct sshbuf *b,
90
    enum sshkey_serialize_rep opts)
91
511
{
92
511
  int r;
93
511
  const BIGNUM *rsa_n, *rsa_e;
94
95
511
  if (key->rsa == NULL)
96
0
    return SSH_ERR_INVALID_ARGUMENT;
97
511
  RSA_get0_key(key->rsa, &rsa_n, &rsa_e, NULL);
98
511
  if ((r = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
99
511
      (r = sshbuf_put_bignum2(b, rsa_n)) != 0)
100
0
    return r;
101
102
511
  return 0;
103
511
}
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
29.3k
{
162
29.3k
  const BIGNUM *rsa_n, *rsa_e;
163
29.3k
  BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL;
164
29.3k
  int r = SSH_ERR_INTERNAL_ERROR;
165
166
29.3k
  RSA_get0_key(from->rsa, &rsa_n, &rsa_e, NULL);
167
29.3k
  if ((rsa_n_dup = BN_dup(rsa_n)) == NULL ||
168
29.3k
      (rsa_e_dup = BN_dup(rsa_e)) == NULL) {
169
0
    r = SSH_ERR_ALLOC_FAIL;
170
0
    goto out;
171
0
  }
172
29.3k
  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
29.3k
  rsa_n_dup = rsa_e_dup = NULL; /* transferred */
177
  /* success */
178
29.3k
  r = 0;
179
29.3k
 out:
180
29.3k
  BN_clear_free(rsa_n_dup);
181
29.3k
  BN_clear_free(rsa_e_dup);
182
29.3k
  return r;
183
29.3k
}
184
185
static int
186
ssh_rsa_deserialize_public(const char *ktype, struct sshbuf *b,
187
    struct sshkey *key)
188
199
{
189
199
  int ret = SSH_ERR_INTERNAL_ERROR;
190
199
  BIGNUM *rsa_n = NULL, *rsa_e = NULL;
191
192
199
  if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||
193
199
      sshbuf_get_bignum2(b, &rsa_n) != 0) {
194
20
    ret = SSH_ERR_INVALID_FORMAT;
195
20
    goto out;
196
20
  }
197
179
  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
179
  rsa_n = rsa_e = NULL; /* transferred */
202
179
  if ((ret = sshkey_check_rsa_length(key, 0)) != 0)
203
9
    goto out;
204
#ifdef DEBUG_PK
205
  RSA_print_fp(stderr, key->rsa, 8);
206
#endif
207
  /* success */
208
170
  ret = 0;
209
199
 out:
210
199
  BN_clear_free(rsa_n);
211
199
  BN_clear_free(rsa_e);
212
199
  return ret;
213
170
}
214
215
static int
216
ssh_rsa_deserialize_private(const char *ktype, struct sshbuf *b,
217
    struct sshkey *key)
218
1
{
219
1
  int r;
220
1
  BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
221
1
  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
1
  if (!sshkey_is_cert(key)) {
225
1
    if ((r = sshbuf_get_bignum2(b, &rsa_n)) != 0 ||
226
1
        (r = sshbuf_get_bignum2(b, &rsa_e)) != 0)
227
0
      goto out;
228
1
    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
1
    rsa_n = rsa_e = NULL; /* transferred */
233
1
  }
234
1
  if ((r = sshbuf_get_bignum2(b, &rsa_d)) != 0 ||
235
1
      (r = sshbuf_get_bignum2(b, &rsa_iqmp)) != 0 ||
236
1
      (r = sshbuf_get_bignum2(b, &rsa_p)) != 0 ||
237
1
      (r = sshbuf_get_bignum2(b, &rsa_q)) != 0)
238
0
    goto out;
239
1
  if (!RSA_set0_key(key->rsa, NULL, NULL, rsa_d)) {
240
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
241
0
    goto out;
242
0
  }
243
1
  rsa_d = NULL; /* transferred */
244
1
  if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q)) {
245
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
246
0
    goto out;
247
0
  }
248
1
  rsa_p = rsa_q = NULL; /* transferred */
249
1
  if ((r = sshkey_check_rsa_length(key, 0)) != 0)
250
0
    goto out;
251
1
  if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)
252
0
    goto out;
253
1
  if (RSA_blinding_on(key->rsa, NULL) != 1) {
254
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
255
0
    goto out;
256
0
  }
257
  /* success */
258
1
  r = 0;
259
1
 out:
260
1
  BN_clear_free(rsa_n);
261
1
  BN_clear_free(rsa_e);
262
1
  BN_clear_free(rsa_d);
263
1
  BN_clear_free(rsa_p);
264
1
  BN_clear_free(rsa_q);
265
1
  BN_clear_free(rsa_iqmp);
266
1
  return r;
267
1
}
268
269
static const char *
270
rsa_hash_alg_ident(int hash_alg)
271
511
{
272
511
  switch (hash_alg) {
273
511
  case SSH_DIGEST_SHA1:
274
511
    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
511
  }
280
0
  return NULL;
281
511
}
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
719
{
290
719
  if (strcmp(ident, "ssh-rsa") == 0)
291
613
    return SSH_DIGEST_SHA1;
292
106
  if (strcmp(ident, "rsa-sha2-256") == 0)
293
0
    return SSH_DIGEST_SHA256;
294
106
  if (strcmp(ident, "rsa-sha2-512") == 0)
295
0
    return SSH_DIGEST_SHA512;
296
106
  return -1;
297
106
}
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
562
{
307
562
  int r;
308
309
562
  if ((r = rsa_hash_id_from_ident(alg)) != -1)
310
562
    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
511
{
323
511
  switch (type) {
324
511
  case SSH_DIGEST_SHA1:
325
511
    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
511
  }
333
511
}
334
335
int
336
ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
337
1
{
338
1
  const BIGNUM *rsa_p, *rsa_q, *rsa_d;
339
1
  BIGNUM *aux = NULL, *d_consttime = NULL;
340
1
  BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;
341
1
  BN_CTX *ctx = NULL;
342
1
  int r;
343
344
1
  if (key == NULL || key->rsa == NULL ||
345
1
      sshkey_type_plain(key->type) != KEY_RSA)
346
0
    return SSH_ERR_INVALID_ARGUMENT;
347
348
1
  RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
349
1
  RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
350
351
1
  if ((ctx = BN_CTX_new()) == NULL)
352
0
    return SSH_ERR_ALLOC_FAIL;
353
1
  if ((aux = BN_new()) == NULL ||
354
1
      (rsa_dmq1 = BN_new()) == NULL ||
355
1
      (rsa_dmp1 = BN_new()) == NULL)
356
0
    return SSH_ERR_ALLOC_FAIL;
357
1
  if ((d_consttime = BN_dup(rsa_d)) == NULL ||
358
1
      (rsa_iqmp = BN_dup(iqmp)) == NULL) {
359
0
    r = SSH_ERR_ALLOC_FAIL;
360
0
    goto out;
361
0
  }
362
1
  BN_set_flags(aux, BN_FLG_CONSTTIME);
363
1
  BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
364
365
1
  if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
366
1
      (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||
367
1
      (BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
368
1
      (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) {
369
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
370
0
    goto out;
371
0
  }
372
1
  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
1
  rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */
377
  /* success */
378
1
  r = 0;
379
1
 out:
380
1
  BN_clear_free(aux);
381
1
  BN_clear_free(d_consttime);
382
1
  BN_clear_free(rsa_dmp1);
383
1
  BN_clear_free(rsa_dmq1);
384
1
  BN_clear_free(rsa_iqmp);
385
1
  BN_CTX_free(ctx);
386
1
  return r;
387
1
}
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
511
{
396
511
  const BIGNUM *rsa_n;
397
511
  u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
398
511
  size_t slen = 0;
399
511
  u_int hlen, len;
400
511
  int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
401
511
  struct sshbuf *b = NULL;
402
403
511
  if (lenp != NULL)
404
511
    *lenp = 0;
405
511
  if (sigp != NULL)
406
511
    *sigp = NULL;
407
408
511
  if (alg == NULL || strlen(alg) == 0)
409
0
    hash_alg = SSH_DIGEST_SHA1;
410
511
  else
411
511
    hash_alg = rsa_hash_id_from_keyname(alg);
412
511
  if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
413
511
      sshkey_type_plain(key->type) != KEY_RSA)
414
0
    return SSH_ERR_INVALID_ARGUMENT;
415
511
  RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
416
511
  if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
417
0
    return SSH_ERR_KEY_LENGTH;
418
511
  slen = RSA_size(key->rsa);
419
511
  if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
420
0
    return SSH_ERR_INVALID_ARGUMENT;
421
422
  /* hash the data */
423
511
  nid = rsa_hash_alg_nid(hash_alg);
424
511
  if ((hlen = ssh_digest_bytes(hash_alg)) == 0)
425
0
    return SSH_ERR_INTERNAL_ERROR;
426
511
  if ((ret = ssh_digest_memory(hash_alg, data, datalen,
427
511
      digest, sizeof(digest))) != 0)
428
0
    goto out;
429
430
511
  if ((sig = malloc(slen)) == NULL) {
431
0
    ret = SSH_ERR_ALLOC_FAIL;
432
0
    goto out;
433
0
  }
434
435
511
  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
511
  if (len < slen) {
440
0
    size_t diff = slen - len;
441
0
    memmove(sig + diff, sig, len);
442
0
    explicit_bzero(sig, diff);
443
511
  } else if (len > slen) {
444
0
    ret = SSH_ERR_INTERNAL_ERROR;
445
0
    goto out;
446
0
  }
447
  /* encode signature */
448
511
  if ((b = sshbuf_new()) == NULL) {
449
0
    ret = SSH_ERR_ALLOC_FAIL;
450
0
    goto out;
451
0
  }
452
511
  if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
453
511
      (ret = sshbuf_put_string(b, sig, slen)) != 0)
454
0
    goto out;
455
511
  len = sshbuf_len(b);
456
511
  if (sigp != NULL) {
457
511
    if ((*sigp = malloc(len)) == NULL) {
458
0
      ret = SSH_ERR_ALLOC_FAIL;
459
0
      goto out;
460
0
    }
461
511
    memcpy(*sigp, sshbuf_ptr(b), len);
462
511
  }
463
511
  if (lenp != NULL)
464
511
    *lenp = len;
465
511
  ret = 0;
466
511
 out:
467
511
  explicit_bzero(digest, sizeof(digest));
468
511
  freezero(sig, slen);
469
511
  sshbuf_free(b);
470
511
  return ret;
471
511
}
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
159
{
479
159
  const BIGNUM *rsa_n;
480
159
  char *sigtype = NULL;
481
159
  int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
482
159
  size_t len = 0, diff, modlen, hlen;
483
159
  struct sshbuf *b = NULL;
484
159
  u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
485
486
159
  if (key == NULL || key->rsa == NULL ||
487
159
      sshkey_type_plain(key->type) != KEY_RSA ||
488
159
      sig == NULL || siglen == 0)
489
0
    return SSH_ERR_INVALID_ARGUMENT;
490
159
  RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
491
159
  if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
492
0
    return SSH_ERR_KEY_LENGTH;
493
494
159
  if ((b = sshbuf_from(sig, siglen)) == NULL)
495
0
    return SSH_ERR_ALLOC_FAIL;
496
159
  if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
497
2
    ret = SSH_ERR_INVALID_FORMAT;
498
2
    goto out;
499
2
  }
500
157
  if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
501
106
    ret = SSH_ERR_KEY_TYPE_MISMATCH;
502
106
    goto out;
503
106
  }
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
51
  if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
509
51
    if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) {
510
0
      ret = SSH_ERR_INVALID_ARGUMENT;
511
0
      goto out;
512
0
    }
513
51
    if (hash_alg != want_alg) {
514
0
      ret = SSH_ERR_SIGNATURE_INVALID;
515
0
      goto out;
516
0
    }
517
51
  }
518
51
  if (sshbuf_get_string(b, &sigblob, &len) != 0) {
519
2
    ret = SSH_ERR_INVALID_FORMAT;
520
2
    goto out;
521
2
  }
522
49
  if (sshbuf_len(b) != 0) {
523
2
    ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
524
2
    goto out;
525
2
  }
526
  /* RSA_verify expects a signature of RSA_size */
527
47
  modlen = RSA_size(key->rsa);
528
47
  if (len > modlen) {
529
0
    ret = SSH_ERR_KEY_BITS_MISMATCH;
530
0
    goto out;
531
47
  } else if (len < modlen) {
532
1
    diff = modlen - len;
533
1
    osigblob = sigblob;
534
1
    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
1
    memmove(sigblob + diff, sigblob, len);
540
1
    explicit_bzero(sigblob, diff);
541
1
    len = modlen;
542
1
  }
543
47
  if ((hlen = ssh_digest_bytes(hash_alg)) == 0) {
544
0
    ret = SSH_ERR_INTERNAL_ERROR;
545
0
    goto out;
546
0
  }
547
47
  if ((ret = ssh_digest_memory(hash_alg, data, dlen,
548
47
      digest, sizeof(digest))) != 0)
549
0
    goto out;
550
551
47
  ret = openssh_RSA_verify(hash_alg, digest, hlen, sigblob, len,
552
47
      key->rsa);
553
159
 out:
554
159
  freezero(sigblob, len);
555
159
  free(sigtype);
556
159
  sshbuf_free(b);
557
159
  explicit_bzero(digest, sizeof(digest));
558
159
  return ret;
559
47
}
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
47
{
613
47
  switch (hash_alg) {
614
47
  case SSH_DIGEST_SHA1:
615
47
    *oidp = id_sha1;
616
47
    *oidlenp = sizeof(id_sha1);
617
47
    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
47
  }
629
47
  return 0;
630
47
}
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
47
{
636
47
  size_t rsasize = 0, oidlen = 0, hlen = 0;
637
47
  int ret, len, oidmatch, hashmatch;
638
47
  const u_char *oid = NULL;
639
47
  u_char *decrypted = NULL;
640
641
47
  if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
642
0
    return ret;
643
47
  ret = SSH_ERR_INTERNAL_ERROR;
644
47
  hlen = ssh_digest_bytes(hash_alg);
645
47
  if (hashlen != hlen) {
646
0
    ret = SSH_ERR_INVALID_ARGUMENT;
647
0
    goto done;
648
0
  }
649
47
  rsasize = RSA_size(rsa);
650
47
  if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
651
47
      siglen == 0 || siglen > rsasize) {
652
0
    ret = SSH_ERR_INVALID_ARGUMENT;
653
0
    goto done;
654
0
  }
655
47
  if ((decrypted = malloc(rsasize)) == NULL) {
656
0
    ret = SSH_ERR_ALLOC_FAIL;
657
0
    goto done;
658
0
  }
659
47
  if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
660
47
      RSA_PKCS1_PADDING)) < 0) {
661
1
    ret = SSH_ERR_LIBCRYPTO_ERROR;
662
1
    goto done;
663
1
  }
664
46
  if (len < 0 || (size_t)len != hlen + oidlen) {
665
0
    ret = SSH_ERR_INVALID_FORMAT;
666
0
    goto done;
667
0
  }
668
46
  oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
669
46
  hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
670
46
  if (!oidmatch || !hashmatch) {
671
46
    ret = SSH_ERR_SIGNATURE_INVALID;
672
46
    goto done;
673
46
  }
674
0
  ret = 0;
675
47
done:
676
47
  freezero(decrypted, rsasize);
677
47
  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 */