blob: c4339b95bd12d3c5cc66436203afbfbc921cfcdf [file] [log] [blame]
Damien Miller0bc1bd82000-11-13 22:57:25 +11001/*
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"
Damien Millerc516e922002-02-05 11:53:43 +110026RCSID("$OpenBSD: ssh-rsa.c,v 1.15 2002/01/25 21:42:11 markus Exp $");
Damien Miller0bc1bd82000-11-13 22:57:25 +110027
28#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110029#include <openssl/err.h>
30
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "xmalloc.h"
32#include "log.h"
33#include "buffer.h"
34#include "bufaux.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110035#include "key.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000036#include "ssh-rsa.h"
Ben Lindstrom60a43812001-03-29 00:32:56 +000037#include "compat.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110038
Damien Miller0bc1bd82000-11-13 22:57:25 +110039/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
40int
41ssh_rsa_sign(
42 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +000043 u_char **sigp, int *lenp,
44 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110045{
Ben Lindstrom425fb022001-03-29 00:31:20 +000046 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +110047 EVP_MD_CTX md;
Damien Millerc516e922002-02-05 11:53:43 +110048 u_char digest[EVP_MAX_MD_SIZE], *sig, *ret;
Ben Lindstrom46c16222000-12-22 01:43:59 +000049 u_int slen, dlen, len;
Ben Lindstrom425fb022001-03-29 00:31:20 +000050 int ok, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +110051 Buffer b;
52
53 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
54 error("ssh_rsa_sign: no RSA key");
55 return -1;
56 }
Damien Millereacff852001-11-12 11:07:35 +110057 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Miller9af8c3c2001-11-12 11:03:16 +110058 error("ssh_rsa_sign: SSH_BUG_SIGBLOB not supported");
59 return -1;
60 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000061 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000062 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
63 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
64 return -1;
65 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110066 EVP_DigestInit(&md, evp_md);
67 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110068 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110069
Ben Lindstrom425fb022001-03-29 00:31:20 +000070 slen = RSA_size(key->rsa);
71 sig = xmalloc(slen);
72
73 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110074 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110075
76 if (ok != 1) {
77 int ecode = ERR_get_error();
78 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
79 xfree(sig);
80 return -1;
81 }
82 if (len < slen) {
83 int diff = slen - len;
84 debug("slen %d > len %d", slen, len);
85 memmove(sig + diff, sig, len);
86 memset(sig, 0, diff);
87 } else if (len > slen) {
88 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
89 xfree(sig);
90 return -1;
91 }
92 /* encode signature */
93 buffer_init(&b);
94 buffer_put_cstring(&b, "ssh-rsa");
95 buffer_put_string(&b, sig, slen);
96 len = buffer_len(&b);
97 ret = xmalloc(len);
98 memcpy(ret, buffer_ptr(&b), len);
99 buffer_free(&b);
100 memset(sig, 's', slen);
101 xfree(sig);
102
103 if (lenp != NULL)
104 *lenp = len;
105 if (sigp != NULL)
106 *sigp = ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100107 return 0;
108}
109
110int
111ssh_rsa_verify(
112 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000113 u_char *signature, int signaturelen,
114 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115{
116 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000117 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 EVP_MD_CTX md;
119 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100120 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121 u_int len, dlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000122 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100123
124 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
125 error("ssh_rsa_verify: no RSA key");
126 return -1;
127 }
Damien Millereacff852001-11-12 11:07:35 +1100128 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Miller9af8c3c2001-11-12 11:03:16 +1100129 error("ssh_rsa_verify: SSH_BUG_SIGBLOB not supported");
130 return -1;
131 }
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000132 if (BN_num_bits(key->rsa->n) < 768) {
133 error("ssh_rsa_verify: n too small: %d bits",
134 BN_num_bits(key->rsa->n));
135 return -1;
136 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000138 buffer_append(&b, signature, signaturelen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139 ktype = buffer_get_string(&b, NULL);
140 if (strcmp("ssh-rsa", ktype) != 0) {
141 error("ssh_rsa_verify: cannot handle type %s", ktype);
142 buffer_free(&b);
143 xfree(ktype);
144 return -1;
145 }
146 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000147 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100148 rlen = buffer_len(&b);
149 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000150 if (rlen != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100151 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
Damien Miller36e603d2001-11-12 11:03:35 +1100152 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100153 return -1;
154 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000155 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000156 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000157 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
Damien Miller36e603d2001-11-12 11:03:35 +1100158 xfree(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000159 return -1;
160 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100161 EVP_DigestInit(&md, evp_md);
162 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100163 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100164
Ben Lindstrom425fb022001-03-29 00:31:20 +0000165 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100166 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100167 memset(sigblob, 's', len);
168 xfree(sigblob);
169 if (ret == 0) {
170 int ecode = ERR_get_error();
171 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
172 }
173 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
174 return ret;
175}