blob: a4ab3c51680eca03c842c84ca97bc43500620fff [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"
Ben Lindstromc66d4362001-06-09 01:36:21 +000026RCSID("$OpenBSD: ssh-rsa.c,v 1.9 2001/06/06 23:13:54 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;
Ben Lindstrom46c16222000-12-22 01:43:59 +000048 u_char *digest, *sig, *ret;
49 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 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000057 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000058 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
59 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
60 return -1;
61 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110062 dlen = evp_md->md_size;
63 digest = xmalloc(dlen);
64 EVP_DigestInit(&md, evp_md);
65 EVP_DigestUpdate(&md, data, datalen);
66 EVP_DigestFinal(&md, digest, NULL);
67
Ben Lindstrom425fb022001-03-29 00:31:20 +000068 slen = RSA_size(key->rsa);
69 sig = xmalloc(slen);
70
71 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +110072 memset(digest, 'd', dlen);
73 xfree(digest);
74
75 if (ok != 1) {
76 int ecode = ERR_get_error();
77 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
78 xfree(sig);
79 return -1;
80 }
81 if (len < slen) {
82 int diff = slen - len;
83 debug("slen %d > len %d", slen, len);
84 memmove(sig + diff, sig, len);
85 memset(sig, 0, diff);
86 } else if (len > slen) {
87 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
88 xfree(sig);
89 return -1;
90 }
91 /* encode signature */
92 buffer_init(&b);
93 buffer_put_cstring(&b, "ssh-rsa");
94 buffer_put_string(&b, sig, slen);
95 len = buffer_len(&b);
96 ret = xmalloc(len);
97 memcpy(ret, buffer_ptr(&b), len);
98 buffer_free(&b);
99 memset(sig, 's', slen);
100 xfree(sig);
101
102 if (lenp != NULL)
103 *lenp = len;
104 if (sigp != NULL)
105 *sigp = ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100106 return 0;
107}
108
109int
110ssh_rsa_verify(
111 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000112 u_char *signature, int signaturelen,
113 u_char *data, int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114{
115 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000116 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100117 EVP_MD_CTX md;
118 char *ktype;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000119 u_char *sigblob, *digest;
120 u_int len, dlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000121 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122
123 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
124 error("ssh_rsa_verify: no RSA key");
125 return -1;
126 }
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000127 if (BN_num_bits(key->rsa->n) < 768) {
128 error("ssh_rsa_verify: n too small: %d bits",
129 BN_num_bits(key->rsa->n));
130 return -1;
131 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100132 buffer_init(&b);
133 buffer_append(&b, (char *) 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);
Ben Lindstrom46c16222000-12-22 01:43:59 +0000142 sigblob = (u_char *)buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100143 rlen = buffer_len(&b);
144 buffer_free(&b);
145 if(rlen != 0) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000146 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
148 return -1;
149 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000150 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000151 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
152 xfree(sigblob);
153 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
154 return -1;
155 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100156 dlen = evp_md->md_size;
157 digest = xmalloc(dlen);
158 EVP_DigestInit(&md, evp_md);
159 EVP_DigestUpdate(&md, data, datalen);
160 EVP_DigestFinal(&md, digest, NULL);
161
Ben Lindstrom425fb022001-03-29 00:31:20 +0000162 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 memset(digest, 'd', dlen);
164 xfree(digest);
165 memset(sigblob, 's', len);
166 xfree(sigblob);
167 if (ret == 0) {
168 int ecode = ERR_get_error();
169 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
170 }
171 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
172 return ret;
173}