2014-07-02 07:28:02 +02:00
|
|
|
/* $OpenBSD: ssh-rsa.c,v 1.52 2014/06/24 01:13:21 djm Exp $ */
|
2000-11-13 12:57:25 +01:00
|
|
|
/*
|
2003-06-22 12:45:15 +02:00
|
|
|
* Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
|
2000-11-13 12:57:25 +01:00
|
|
|
*
|
2003-06-22 12:45:15 +02:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-11-13 12:57:25 +01:00
|
|
|
*
|
2003-06-22 12:45:15 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2000-11-13 12:57:25 +01:00
|
|
|
*/
|
2006-08-05 04:39:39 +02:00
|
|
|
|
2000-11-13 12:57:25 +01:00
|
|
|
#include "includes.h"
|
|
|
|
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-11-13 12:57:25 +01:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2006-09-01 07:38:36 +02:00
|
|
|
#include <stdarg.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
#include "sshbuf.h"
|
2001-03-29 02:32:56 +02:00
|
|
|
#include "compat.h"
|
2014-07-02 07:28:02 +02:00
|
|
|
#include "ssherr.h"
|
|
|
|
#define SSHKEY_INTERNAL
|
|
|
|
#include "sshkey.h"
|
2014-01-10 00:58:53 +01:00
|
|
|
#include "digest.h"
|
2000-11-13 12:57:25 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
|
2002-08-20 20:40:03 +02:00
|
|
|
|
2000-11-13 12:57:25 +01:00
|
|
|
/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
|
|
|
|
int
|
2014-07-02 07:28:02 +02:00
|
|
|
ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
|
|
|
|
const u_char *data, size_t datalen, u_int compat)
|
2000-11-13 12:57:25 +01:00
|
|
|
{
|
2014-01-10 00:58:53 +01:00
|
|
|
int hash_alg;
|
2014-07-02 07:28:02 +02:00
|
|
|
u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
|
|
|
|
size_t slen;
|
|
|
|
u_int dlen, len;
|
|
|
|
int nid, ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *b = NULL;
|
2000-11-13 12:57:25 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
|
|
|
|
if (key == NULL || key->rsa == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_RSA)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
slen = RSA_size(key->rsa);
|
|
|
|
if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2013-12-29 07:47:50 +01:00
|
|
|
|
2014-01-10 00:58:53 +01:00
|
|
|
/* hash the data */
|
|
|
|
hash_alg = SSH_DIGEST_SHA1;
|
2013-12-31 02:25:40 +01:00
|
|
|
nid = NID_sha1;
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
|
|
|
|
return SSH_ERR_INTERNAL_ERROR;
|
|
|
|
if ((ret = ssh_digest_memory(hash_alg, data, datalen,
|
|
|
|
digest, sizeof(digest))) != 0)
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((sig = malloc(slen)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-03-26 05:07:26 +02:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
|
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
}
|
|
|
|
if (len < slen) {
|
2014-07-02 07:28:02 +02:00
|
|
|
size_t diff = slen - len;
|
2000-11-13 12:57:25 +01:00
|
|
|
memmove(sig + diff, sig, len);
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(sig, diff);
|
2000-11-13 12:57:25 +01:00
|
|
|
} else if (len > slen) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
}
|
|
|
|
/* encode signature */
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((b = sshbuf_new()) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((ret = sshbuf_put_cstring(b, "ssh-rsa")) != 0 ||
|
|
|
|
(ret = sshbuf_put_string(b, sig, slen)) != 0)
|
|
|
|
goto out;
|
|
|
|
len = sshbuf_len(b);
|
|
|
|
if (sigp != NULL) {
|
|
|
|
if ((*sigp = malloc(len)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(*sigp, sshbuf_ptr(b), len);
|
|
|
|
}
|
2002-07-08 00:13:31 +02:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
explicit_bzero(digest, sizeof(digest));
|
|
|
|
if (sig != NULL) {
|
|
|
|
explicit_bzero(sig, slen);
|
|
|
|
free(sig);
|
2002-07-08 00:13:31 +02:00
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
if (b != NULL)
|
|
|
|
sshbuf_free(b);
|
2000-11-13 12:57:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-07-02 07:28:02 +02:00
|
|
|
ssh_rsa_verify(const struct sshkey *key,
|
|
|
|
const u_char *signature, size_t signaturelen,
|
|
|
|
const u_char *data, size_t datalen, u_int compat)
|
2000-11-13 12:57:25 +01:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
char *ktype = NULL;
|
|
|
|
int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
size_t len, diff, modlen, dlen;
|
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
|
2000-11-13 12:57:25 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if (key == NULL || key->rsa == NULL ||
|
|
|
|
sshkey_type_plain(key->type) != KEY_RSA ||
|
|
|
|
BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
2013-12-29 07:47:50 +01:00
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((b = sshbuf_from(signature, signaturelen)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
2001-01-18 03:04:35 +01:00
|
|
|
}
|
2000-11-13 12:57:25 +01:00
|
|
|
if (strcmp("ssh-rsa", ktype) != 0) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_KEY_TYPE_MISMATCH;
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
if (sshbuf_get_string(b, &sigblob, &len) != 0) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(b) != 0) {
|
|
|
|
ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
}
|
2002-06-06 22:55:04 +02:00
|
|
|
/* RSA_verify expects a signature of RSA_size */
|
|
|
|
modlen = RSA_size(key->rsa);
|
|
|
|
if (len > modlen) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_KEY_BITS_MISMATCH;
|
|
|
|
goto out;
|
2002-06-06 22:55:04 +02:00
|
|
|
} else if (len < modlen) {
|
2014-07-02 07:28:02 +02:00
|
|
|
diff = modlen - len;
|
|
|
|
osigblob = sigblob;
|
|
|
|
if ((sigblob = realloc(sigblob, modlen)) == NULL) {
|
|
|
|
sigblob = osigblob; /* put it back for clear/free */
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-06-06 22:55:04 +02:00
|
|
|
memmove(sigblob + diff, sigblob, len);
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(sigblob, diff);
|
2002-06-06 22:55:04 +02:00
|
|
|
len = modlen;
|
|
|
|
}
|
2014-01-10 00:58:53 +01:00
|
|
|
hash_alg = SSH_DIGEST_SHA1;
|
|
|
|
if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2001-03-29 02:31:20 +02:00
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
if ((ret = ssh_digest_memory(hash_alg, data, datalen,
|
|
|
|
digest, sizeof(digest))) != 0)
|
|
|
|
goto out;
|
2000-11-13 12:57:25 +01:00
|
|
|
|
2014-01-10 00:58:53 +01:00
|
|
|
ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
|
|
|
|
key->rsa);
|
2014-07-02 07:28:02 +02:00
|
|
|
out:
|
|
|
|
if (sigblob != NULL) {
|
|
|
|
explicit_bzero(sigblob, len);
|
|
|
|
free(sigblob);
|
|
|
|
}
|
|
|
|
if (ktype != NULL)
|
|
|
|
free(ktype);
|
|
|
|
if (b != NULL)
|
|
|
|
sshbuf_free(b);
|
2014-02-04 01:20:14 +01:00
|
|
|
explicit_bzero(digest, sizeof(digest));
|
2000-11-13 12:57:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2002-08-20 20:40:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See:
|
|
|
|
* http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
|
|
|
|
* ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
|
|
|
|
* oiw(14) secsig(3) algorithms(2) 26 }
|
|
|
|
*/
|
|
|
|
static const u_char id_sha1[] = {
|
|
|
|
0x30, 0x21, /* type Sequence, length 0x21 (33) */
|
|
|
|
0x30, 0x09, /* type Sequence, length 0x09 */
|
|
|
|
0x06, 0x05, /* type OID, length 0x05 */
|
|
|
|
0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
|
|
|
|
0x05, 0x00, /* NULL */
|
|
|
|
0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2014-07-02 07:28:02 +02:00
|
|
|
openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
|
|
|
|
u_char *sigbuf, size_t siglen, RSA *rsa)
|
2002-08-20 20:40:03 +02:00
|
|
|
{
|
2014-07-02 07:28:02 +02:00
|
|
|
size_t ret, rsasize = 0, oidlen = 0, hlen = 0;
|
2010-08-03 08:04:03 +02:00
|
|
|
int len, oidmatch, hashmatch;
|
2002-08-20 20:40:03 +02:00
|
|
|
const u_char *oid = NULL;
|
|
|
|
u_char *decrypted = NULL;
|
|
|
|
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_INTERNAL_ERROR;
|
2014-01-10 00:58:53 +01:00
|
|
|
switch (hash_alg) {
|
|
|
|
case SSH_DIGEST_SHA1:
|
2002-08-20 20:40:03 +02:00
|
|
|
oid = id_sha1;
|
|
|
|
oidlen = sizeof(id_sha1);
|
|
|
|
hlen = 20;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (hashlen != hlen) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
2002-08-20 20:40:03 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
rsasize = RSA_size(rsa);
|
2014-07-02 07:28:02 +02:00
|
|
|
if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
|
|
|
|
siglen == 0 || siglen > rsasize) {
|
|
|
|
ret = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((decrypted = malloc(rsasize)) == NULL) {
|
|
|
|
ret = SSH_ERR_ALLOC_FAIL;
|
2002-08-20 20:40:03 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
|
|
|
|
RSA_PKCS1_PADDING)) < 0) {
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = SSH_ERR_LIBCRYPTO_ERROR;
|
2002-08-20 20:40:03 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
if (len < 0 || (size_t)len != hlen + oidlen) {
|
|
|
|
ret = SSH_ERR_INVALID_FORMAT;
|
2002-08-20 20:40:03 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2010-08-03 08:04:03 +02:00
|
|
|
oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
|
|
|
|
hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
|
2014-07-02 07:28:02 +02:00
|
|
|
if (!oidmatch || !hashmatch) {
|
|
|
|
ret = SSH_ERR_SIGNATURE_INVALID;
|
2002-08-20 20:40:03 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2014-07-02 07:28:02 +02:00
|
|
|
ret = 0;
|
2002-08-20 20:40:03 +02:00
|
|
|
done:
|
2014-07-02 07:28:02 +02:00
|
|
|
if (decrypted) {
|
|
|
|
explicit_bzero(decrypted, rsasize);
|
|
|
|
free(decrypted);
|
|
|
|
}
|
2002-08-20 20:40:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|