Skip to content

Commit a497828

Browse files
committed
Allow RSA signatures with length less than the modulus size
PuTTY < 0.82 does not pad signature blobs with SHA2. See commit a5bcf3d384e [1]. Additionally, contrary to rsa-sha2-256 and rsa-sha2-512, the specification for the original ssh-rsa signatures seems to require no padding, even though openssh always pads with zeros: > The value for 'rsa_signature_blob' is encoded as a string containing > s (which is an integer, without lengths or padding, unsigned, and > in network byte order). For compatibility with these implementations, zero pad the signature if it is smaller than the modulus size. [1] https://git.tartarus.org/?p=simon/putty.git;a=commit;h=a5bcf3d384e1bf15a51a6923c3724cbbee022d8e
1 parent ed605a6 commit a497828

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

ssh-rsa.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,10 @@ ssh_rsa_verify(const struct sshkey *key,
510510
{
511511
char *sigtype = NULL;
512512
int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
513-
size_t len = 0, hlen;
513+
size_t len = 0, hlen, xlen;
514514
struct sshbuf *b = NULL;
515515
u_char digest[SSH_DIGEST_MAX_LENGTH], sigdigest[SSH_DIGEST_MAX_LENGTH];
516+
u_char x[(SSH_RSA_MAXIMUM_MODULUS_SIZE + 7) / 8];
516517
const u_char *sigblob, *oid;
517518

518519
if (key == NULL || key->rsa_pk == NULL ||
@@ -555,6 +556,19 @@ ssh_rsa_verify(const struct sshkey *key,
555556
ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
556557
goto out;
557558
}
559+
/* br_rsa_pkcs1_vrfy expects a signature of exactly nlen */
560+
xlen = key->rsa_pk->key.nlen;
561+
if (xlen > sizeof(x)) {
562+
ret = SSH_ERR_INTERNAL_ERROR;
563+
goto out;
564+
}
565+
if (len > xlen) {
566+
ret = SSH_ERR_KEY_BITS_MISMATCH;
567+
goto out;
568+
}
569+
memset(x, 0, xlen - len);
570+
memcpy(x + (xlen - len), sigblob, len);
571+
558572
if ((hlen = ssh_digest_bytes(hash_alg)) == 0) {
559573
ret = SSH_ERR_INTERNAL_ERROR;
560574
goto out;
@@ -563,7 +577,7 @@ ssh_rsa_verify(const struct sshkey *key,
563577
digest, sizeof(digest))) != 0)
564578
goto out;
565579

566-
if (br_rsa_pkcs1_vrfy_get_default()(sigblob, len, oid, hlen,
580+
if (br_rsa_pkcs1_vrfy_get_default()(x, xlen, oid, hlen,
567581
&key->rsa_pk->key, sigdigest) != 1 ||
568582
timingsafe_bcmp(digest, sigdigest, hlen) != 0) {
569583
ret = SSH_ERR_SIGNATURE_INVALID;
@@ -573,6 +587,7 @@ ssh_rsa_verify(const struct sshkey *key,
573587
out:
574588
free(sigtype);
575589
sshbuf_free(b);
590+
explicit_bzero(x, sizeof(x));
576591
explicit_bzero(digest, sizeof(digest));
577592
explicit_bzero(sigdigest, sizeof(sigdigest));
578593
return ret;

0 commit comments

Comments
 (0)