1 /*
   2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  * 1. Redistributions of source code must retain the above copyright
   8  *    notice, this list of conditions and the following disclaimer.
   9  * 2. Redistributions in binary form must reproduce the above copyright
  10  *    notice, this list of conditions and the following disclaimer in the
  11  *    documentation and/or other materials provided with the distribution.
  12  *
  13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23  */
  24 
  25 #include "includes.h"
  26 RCSID("$OpenBSD: ssh-rsa.c,v 1.26 2002/08/27 17:13:56 stevesk Exp $");
  27 
  28 #pragma ident   "%Z%%M% %I%     %E% SMI"
  29 
  30 #include <openssl/opensslconf.h>
  31 #include <openssl/evp.h>
  32 #include <openssl/err.h>
  33 
  34 #include "xmalloc.h"
  35 #include "log.h"
  36 #include "buffer.h"
  37 #include "bufaux.h"
  38 #include "key.h"
  39 #include "ssh-rsa.h"
  40 #include "compat.h"
  41 #include "ssh.h"
  42 
  43 static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int , RSA *);
  44 
  45 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
  46 int
  47 ssh_rsa_sign(Key *key, u_char **sigp, u_int *lenp,
  48     u_char *data, u_int datalen)
  49 {
  50         const EVP_MD *evp_md;
  51         EVP_MD_CTX md;
  52         u_char digest[EVP_MAX_MD_SIZE], *sig;
  53         u_int slen, dlen, len;
  54         int ok, nid;
  55         Buffer b;
  56 
  57         if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
  58                 error("ssh_rsa_sign: no RSA key");
  59                 return -1;
  60         }
  61         nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
  62         if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
  63                 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
  64                 return -1;
  65         }
  66         EVP_DigestInit(&md, evp_md);
  67         EVP_DigestUpdate(&md, data, datalen);
  68         EVP_DigestFinal(&md, digest, &dlen);
  69 
  70         slen = RSA_size(key->rsa);
  71         sig = xmalloc(slen);
  72 
  73         ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
  74         memset(digest, 'd', sizeof(digest));
  75 
  76         if (ok != 1) {
  77                 int ecode = ERR_get_error();
  78                 error("ssh_rsa_sign: RSA_sign failed: %s",
  79                     ERR_error_string(ecode, NULL));
  80                 xfree(sig);
  81                 return -1;
  82         }
  83         if (len < slen) {
  84                 u_int diff = slen - len;
  85                 debug("slen %u > len %u", slen, len);
  86                 memmove(sig + diff, sig, len);
  87                 memset(sig, 0, diff);
  88         } else if (len > slen) {
  89                 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
  90                 xfree(sig);
  91                 return -1;
  92         }
  93         /* encode signature */
  94         buffer_init(&b);
  95         buffer_put_cstring(&b, "ssh-rsa");
  96         buffer_put_string(&b, sig, slen);
  97         len = buffer_len(&b);
  98         if (lenp != NULL)
  99                 *lenp = len;
 100         if (sigp != NULL) {
 101                 *sigp = xmalloc(len);
 102                 memcpy(*sigp, buffer_ptr(&b), len);
 103         }
 104         buffer_free(&b);
 105         memset(sig, 's', slen);
 106         xfree(sig);
 107 
 108         return 0;
 109 }
 110 
 111 int
 112 ssh_rsa_verify(Key *key, u_char *signature, u_int signaturelen,
 113     u_char *data, u_int datalen)
 114 {
 115         Buffer b;
 116         const EVP_MD *evp_md;
 117         EVP_MD_CTX md;
 118         char *ktype;
 119         u_char digest[EVP_MAX_MD_SIZE], *sigblob;
 120         u_int len, dlen, modlen;
 121         int rlen, ret, nid;
 122 
 123         if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
 124                 error("ssh_rsa_verify: no RSA key");
 125                 return -1;
 126         }
 127         if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
 128                 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
 129                     BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
 130                 return -1;
 131         }
 132         buffer_init(&b);
 133         buffer_append(&b, signature, signaturelen);
 134         ktype = buffer_get_string(&b, NULL);
 135         if (strcmp("ssh-rsa", ktype) != 0) {
 136                 error("ssh_rsa_verify: cannot handle type %s", ktype);
 137                 buffer_free(&b);
 138                 xfree(ktype);
 139                 return -1;
 140         }
 141         xfree(ktype);
 142         sigblob = buffer_get_string(&b, &len);
 143         rlen = buffer_len(&b);
 144         buffer_free(&b);
 145         if (rlen != 0) {
 146                 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
 147                 xfree(sigblob);
 148                 return -1;
 149         }
 150         /* RSA_verify expects a signature of RSA_size */
 151         modlen = RSA_size(key->rsa);
 152         if (len > modlen) {
 153                 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
 154                 xfree(sigblob);
 155                 return -1;
 156         } else if (len < modlen) {
 157                 u_int diff = modlen - len;
 158                 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
 159                     modlen, len);
 160                 sigblob = xrealloc(sigblob, modlen);
 161                 memmove(sigblob + diff, sigblob, len);
 162                 memset(sigblob, 0, diff);
 163                 len = modlen;
 164         }
 165         nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
 166         if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
 167                 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
 168                 xfree(sigblob);
 169                 return -1;
 170         }
 171         EVP_DigestInit(&md, evp_md);
 172         EVP_DigestUpdate(&md, data, datalen);
 173         EVP_DigestFinal(&md, digest, &dlen);
 174 
 175         ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
 176         memset(digest, 'd', sizeof(digest));
 177         memset(sigblob, 's', len);
 178         xfree(sigblob);
 179         debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
 180         return ret;
 181 }
 182 
 183 /*
 184  * See:
 185  * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
 186  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
 187  */
 188 /*
 189  * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
 190  *      oiw(14) secsig(3) algorithms(2) 26 }
 191  */
 192 static const u_char id_sha1[] = {
 193         0x30, 0x21, /* type Sequence, length 0x21 (33) */
 194         0x30, 0x09, /* type Sequence, length 0x09 */
 195         0x06, 0x05, /* type OID, length 0x05 */
 196         0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
 197         0x05, 0x00, /* NULL */
 198         0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */
 199 };
 200 /*
 201  * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
 202  *      rsadsi(113549) digestAlgorithm(2) 5 }
 203  */
 204 static const u_char id_md5[] = {
 205         0x30, 0x20, /* type Sequence, length 0x20 (32) */
 206         0x30, 0x0c, /* type Sequence, length 0x09 */
 207         0x06, 0x08, /* type OID, length 0x05 */
 208         0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
 209         0x05, 0x00, /* NULL */
 210         0x04, 0x10  /* Octet string, length 0x10 (16), followed by md5 hash */
 211 };
 212 
 213 static int
 214 openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
 215     u_char *sigbuf, u_int siglen, RSA *rsa)
 216 {
 217         u_int ret, rsasize, oidlen = 0, hlen = 0;
 218         int len;
 219         const u_char *oid = NULL;
 220         u_char *decrypted = NULL;
 221 
 222         ret = 0;
 223         switch (type) {
 224         case NID_sha1:
 225                 oid = id_sha1;
 226                 oidlen = sizeof(id_sha1);
 227                 hlen = 20;
 228                 break;
 229         case NID_md5:
 230                 oid = id_md5;
 231                 oidlen = sizeof(id_md5);
 232                 hlen = 16;
 233                 break;
 234         default:
 235                 goto done;
 236                 break;
 237         }
 238         if (hashlen != hlen) {
 239                 error("bad hashlen");
 240                 goto done;
 241         }
 242         rsasize = RSA_size(rsa);
 243         if (siglen == 0 || siglen > rsasize) {
 244                 error("bad siglen");
 245                 goto done;
 246         }
 247         decrypted = xmalloc(rsasize);
 248         if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
 249             RSA_PKCS1_PADDING)) < 0) {
 250                 error("RSA_public_decrypt failed: %s",
 251                     ERR_error_string(ERR_get_error(), NULL));
 252                 goto done;
 253         }
 254         if (len != hlen + oidlen) {
 255                 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
 256                 goto done;
 257         }
 258         if (memcmp(decrypted, oid, oidlen) != 0) {
 259                 error("oid mismatch");
 260                 goto done;
 261         }
 262         if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
 263                 error("hash mismatch");
 264                 goto done;
 265         }
 266         ret = 1;
 267 done:
 268         if (decrypted)
 269                 xfree(decrypted);
 270         return ret;
 271 }