2010-08-03 06:04:03 +00:00
|
|
|
/* $OpenBSD: ssh-rsa.c,v 1.44 2010/07/16 14:07:35 djm Exp $ */
|
2000-11-13 11:57:25 +00:00
|
|
|
/*
|
2003-06-22 10:45:15 +00:00
|
|
|
* Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
|
2000-11-13 11:57:25 +00:00
|
|
|
*
|
2003-06-22 10:45:15 +00: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 11:57:25 +00:00
|
|
|
*
|
2003-06-22 10:45:15 +00: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 11:57:25 +00:00
|
|
|
*/
|
2006-08-05 02:39:39 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2006-09-01 05:38:36 +00:00
|
|
|
#include <stdarg.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
2000-11-13 11:57:25 +00:00
|
|
|
#include "key.h"
|
2001-03-29 00:32:56 +00:00
|
|
|
#include "compat.h"
|
2010-07-16 03:57:51 +00:00
|
|
|
#include "misc.h"
|
2002-04-02 20:43:11 +00:00
|
|
|
#include "ssh.h"
|
2000-11-13 11:57:25 +00:00
|
|
|
|
2002-12-23 02:06:19 +00:00
|
|
|
static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
|
2002-08-20 18:40:03 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
|
|
|
|
int
|
2003-11-17 10:18:23 +00:00
|
|
|
ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
|
|
|
|
const u_char *data, u_int datalen)
|
2000-11-13 11:57:25 +00:00
|
|
|
{
|
2001-03-29 00:31:20 +00:00
|
|
|
const EVP_MD *evp_md;
|
2000-11-13 11:57:25 +00:00
|
|
|
EVP_MD_CTX md;
|
2002-07-07 22:13:31 +00:00
|
|
|
u_char digest[EVP_MAX_MD_SIZE], *sig;
|
2000-12-22 01:43:59 +00:00
|
|
|
u_int slen, dlen, len;
|
2001-03-29 00:31:20 +00:00
|
|
|
int ok, nid;
|
2000-11-13 11:57:25 +00:00
|
|
|
Buffer b;
|
|
|
|
|
2010-04-16 05:56:21 +00:00
|
|
|
if (key == NULL || key->rsa == NULL || (key->type != KEY_RSA &&
|
|
|
|
key->type != KEY_RSA_CERT && key->type != KEY_RSA_CERT_V00)) {
|
2000-11-13 11:57:25 +00:00
|
|
|
error("ssh_rsa_sign: no RSA key");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-29 00:32:56 +00:00
|
|
|
nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
|
2001-03-29 00:31:20 +00:00
|
|
|
if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
|
|
|
|
error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-11-13 11:57:25 +00:00
|
|
|
EVP_DigestInit(&md, evp_md);
|
|
|
|
EVP_DigestUpdate(&md, data, datalen);
|
2002-02-05 00:53:43 +00:00
|
|
|
EVP_DigestFinal(&md, digest, &dlen);
|
2000-11-13 11:57:25 +00:00
|
|
|
|
2001-03-29 00:31:20 +00:00
|
|
|
slen = RSA_size(key->rsa);
|
|
|
|
sig = xmalloc(slen);
|
|
|
|
|
|
|
|
ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
|
2002-02-05 00:53:43 +00:00
|
|
|
memset(digest, 'd', sizeof(digest));
|
2000-11-13 11:57:25 +00:00
|
|
|
|
|
|
|
if (ok != 1) {
|
|
|
|
int ecode = ERR_get_error();
|
2006-03-26 03:07:26 +00:00
|
|
|
|
2002-06-23 21:23:20 +00:00
|
|
|
error("ssh_rsa_sign: RSA_sign failed: %s",
|
|
|
|
ERR_error_string(ecode, NULL));
|
2000-11-13 11:57:25 +00:00
|
|
|
xfree(sig);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (len < slen) {
|
2002-08-20 18:39:14 +00:00
|
|
|
u_int diff = slen - len;
|
2002-06-23 21:23:20 +00:00
|
|
|
debug("slen %u > len %u", slen, len);
|
2000-11-13 11:57:25 +00:00
|
|
|
memmove(sig + diff, sig, len);
|
|
|
|
memset(sig, 0, diff);
|
|
|
|
} else if (len > slen) {
|
2002-06-23 21:23:20 +00:00
|
|
|
error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
|
2000-11-13 11:57:25 +00:00
|
|
|
xfree(sig);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* encode signature */
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_put_cstring(&b, "ssh-rsa");
|
|
|
|
buffer_put_string(&b, sig, slen);
|
|
|
|
len = buffer_len(&b);
|
2002-07-07 22:13:31 +00:00
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = len;
|
|
|
|
if (sigp != NULL) {
|
|
|
|
*sigp = xmalloc(len);
|
|
|
|
memcpy(*sigp, buffer_ptr(&b), len);
|
|
|
|
}
|
2000-11-13 11:57:25 +00:00
|
|
|
buffer_free(&b);
|
|
|
|
memset(sig, 's', slen);
|
|
|
|
xfree(sig);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-11-17 10:18:23 +00:00
|
|
|
ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
|
|
|
|
const u_char *data, u_int datalen)
|
2000-11-13 11:57:25 +00:00
|
|
|
{
|
|
|
|
Buffer b;
|
2001-03-29 00:31:20 +00:00
|
|
|
const EVP_MD *evp_md;
|
2000-11-13 11:57:25 +00:00
|
|
|
EVP_MD_CTX md;
|
|
|
|
char *ktype;
|
2002-02-05 00:53:43 +00:00
|
|
|
u_char digest[EVP_MAX_MD_SIZE], *sigblob;
|
2002-06-06 20:55:04 +00:00
|
|
|
u_int len, dlen, modlen;
|
2001-03-29 00:31:20 +00:00
|
|
|
int rlen, ret, nid;
|
2000-11-13 11:57:25 +00:00
|
|
|
|
2010-04-16 05:56:21 +00:00
|
|
|
if (key == NULL || key->rsa == NULL || (key->type != KEY_RSA &&
|
|
|
|
key->type != KEY_RSA_CERT && key->type != KEY_RSA_CERT_V00)) {
|
2000-11-13 11:57:25 +00:00
|
|
|
error("ssh_rsa_verify: no RSA key");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-02 20:43:11 +00:00
|
|
|
if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
|
2002-06-11 15:47:42 +00:00
|
|
|
error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
|
|
|
|
BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
|
2001-01-18 02:04:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-11-13 11:57:25 +00:00
|
|
|
buffer_init(&b);
|
2001-09-18 05:41:19 +00:00
|
|
|
buffer_append(&b, signature, signaturelen);
|
2000-11-13 11:57:25 +00:00
|
|
|
ktype = buffer_get_string(&b, NULL);
|
|
|
|
if (strcmp("ssh-rsa", ktype) != 0) {
|
|
|
|
error("ssh_rsa_verify: cannot handle type %s", ktype);
|
|
|
|
buffer_free(&b);
|
|
|
|
xfree(ktype);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
xfree(ktype);
|
2001-09-18 05:41:19 +00:00
|
|
|
sigblob = buffer_get_string(&b, &len);
|
2000-11-13 11:57:25 +00:00
|
|
|
rlen = buffer_len(&b);
|
|
|
|
buffer_free(&b);
|
2001-12-06 18:00:18 +00:00
|
|
|
if (rlen != 0) {
|
2000-11-13 11:57:25 +00:00
|
|
|
error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
|
2001-11-12 00:03:35 +00:00
|
|
|
xfree(sigblob);
|
2000-11-13 11:57:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-06-06 20:55:04 +00:00
|
|
|
/* RSA_verify expects a signature of RSA_size */
|
|
|
|
modlen = RSA_size(key->rsa);
|
|
|
|
if (len > modlen) {
|
2002-06-23 21:23:20 +00:00
|
|
|
error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
|
2002-06-06 20:55:04 +00:00
|
|
|
xfree(sigblob);
|
|
|
|
return -1;
|
|
|
|
} else if (len < modlen) {
|
2002-08-20 18:39:14 +00:00
|
|
|
u_int diff = modlen - len;
|
2002-06-23 21:23:20 +00:00
|
|
|
debug("ssh_rsa_verify: add padding: modlen %u > len %u",
|
2002-06-06 20:55:04 +00:00
|
|
|
modlen, len);
|
2006-03-26 03:22:47 +00:00
|
|
|
sigblob = xrealloc(sigblob, 1, modlen);
|
2002-06-06 20:55:04 +00:00
|
|
|
memmove(sigblob + diff, sigblob, len);
|
|
|
|
memset(sigblob, 0, diff);
|
|
|
|
len = modlen;
|
|
|
|
}
|
2001-03-29 00:32:56 +00:00
|
|
|
nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
|
2001-03-29 00:31:20 +00:00
|
|
|
if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
|
|
|
|
error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
|
2001-11-12 00:03:35 +00:00
|
|
|
xfree(sigblob);
|
2001-03-29 00:31:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2000-11-13 11:57:25 +00:00
|
|
|
EVP_DigestInit(&md, evp_md);
|
|
|
|
EVP_DigestUpdate(&md, data, datalen);
|
2002-02-05 00:53:43 +00:00
|
|
|
EVP_DigestFinal(&md, digest, &dlen);
|
2000-11-13 11:57:25 +00:00
|
|
|
|
2002-08-20 18:40:03 +00:00
|
|
|
ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
|
2002-02-05 00:53:43 +00:00
|
|
|
memset(digest, 'd', sizeof(digest));
|
2000-11-13 11:57:25 +00:00
|
|
|
memset(sigblob, 's', len);
|
|
|
|
xfree(sigblob);
|
|
|
|
debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
|
|
|
|
return ret;
|
|
|
|
}
|
2002-08-20 18:40:03 +00: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 */
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
|
|
|
|
* rsadsi(113549) digestAlgorithm(2) 5 }
|
|
|
|
*/
|
|
|
|
static const u_char id_md5[] = {
|
|
|
|
0x30, 0x20, /* type Sequence, length 0x20 (32) */
|
|
|
|
0x30, 0x0c, /* type Sequence, length 0x09 */
|
|
|
|
0x06, 0x08, /* type OID, length 0x05 */
|
|
|
|
0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
|
|
|
|
0x05, 0x00, /* NULL */
|
|
|
|
0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
|
|
|
|
u_char *sigbuf, u_int siglen, RSA *rsa)
|
|
|
|
{
|
2002-09-04 06:39:48 +00:00
|
|
|
u_int ret, rsasize, oidlen = 0, hlen = 0;
|
2010-08-03 06:04:03 +00:00
|
|
|
int len, oidmatch, hashmatch;
|
2002-08-20 18:40:03 +00:00
|
|
|
const u_char *oid = NULL;
|
|
|
|
u_char *decrypted = NULL;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
switch (type) {
|
|
|
|
case NID_sha1:
|
|
|
|
oid = id_sha1;
|
|
|
|
oidlen = sizeof(id_sha1);
|
|
|
|
hlen = 20;
|
|
|
|
break;
|
|
|
|
case NID_md5:
|
|
|
|
oid = id_md5;
|
|
|
|
oidlen = sizeof(id_md5);
|
|
|
|
hlen = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (hashlen != hlen) {
|
|
|
|
error("bad hashlen");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
rsasize = RSA_size(rsa);
|
|
|
|
if (siglen == 0 || siglen > rsasize) {
|
|
|
|
error("bad siglen");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
decrypted = xmalloc(rsasize);
|
|
|
|
if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
|
|
|
|
RSA_PKCS1_PADDING)) < 0) {
|
|
|
|
error("RSA_public_decrypt failed: %s",
|
|
|
|
ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
goto done;
|
|
|
|
}
|
2005-06-17 02:59:34 +00:00
|
|
|
if (len < 0 || (u_int)len != hlen + oidlen) {
|
2003-06-22 10:45:15 +00:00
|
|
|
error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
|
2002-08-20 18:40:03 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2010-08-03 06:04:03 +00:00
|
|
|
oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
|
|
|
|
hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
|
|
|
|
if (!oidmatch) {
|
2002-08-20 18:40:03 +00:00
|
|
|
error("oid mismatch");
|
|
|
|
goto done;
|
|
|
|
}
|
2010-08-03 06:04:03 +00:00
|
|
|
if (!hashmatch) {
|
2002-08-20 18:40:03 +00:00
|
|
|
error("hash mismatch");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
done:
|
|
|
|
if (decrypted)
|
|
|
|
xfree(decrypted);
|
|
|
|
return ret;
|
|
|
|
}
|