blob: fe4dc1f8dfffbd8da198e126ef63ab636bdec07c [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 Lindstrom07739fe2002-04-03 03:03:04 +000026RCSID("$OpenBSD: ssh-rsa.c,v 1.18 2002/04/02 20:11:38 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"
Ben Lindstrom03f39322002-04-02 20:43:11 +000038#include "ssh.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110039
Damien Miller0bc1bd82000-11-13 22:57:25 +110040/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
41int
42ssh_rsa_sign(
43 Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +000044 u_char **sigp, u_int *lenp,
45 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +110046{
Ben Lindstrom425fb022001-03-29 00:31:20 +000047 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +110048 EVP_MD_CTX md;
Damien Millerc516e922002-02-05 11:53:43 +110049 u_char digest[EVP_MAX_MD_SIZE], *sig, *ret;
Ben Lindstrom46c16222000-12-22 01:43:59 +000050 u_int slen, dlen, len;
Ben Lindstrom425fb022001-03-29 00:31:20 +000051 int ok, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +110052 Buffer b;
53
54 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
55 error("ssh_rsa_sign: no RSA key");
56 return -1;
57 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000058 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000059 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
60 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
61 return -1;
62 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110063 EVP_DigestInit(&md, evp_md);
64 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110065 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110066
Ben Lindstrom425fb022001-03-29 00:31:20 +000067 slen = RSA_size(key->rsa);
68 sig = xmalloc(slen);
69
70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110071 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110072
73 if (ok != 1) {
74 int ecode = ERR_get_error();
75 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
76 xfree(sig);
77 return -1;
78 }
79 if (len < slen) {
80 int diff = slen - len;
81 debug("slen %d > len %d", slen, len);
82 memmove(sig + diff, sig, len);
83 memset(sig, 0, diff);
84 } else if (len > slen) {
85 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
86 xfree(sig);
87 return -1;
88 }
89 /* encode signature */
90 buffer_init(&b);
91 buffer_put_cstring(&b, "ssh-rsa");
92 buffer_put_string(&b, sig, slen);
93 len = buffer_len(&b);
94 ret = xmalloc(len);
95 memcpy(ret, buffer_ptr(&b), len);
96 buffer_free(&b);
97 memset(sig, 's', slen);
98 xfree(sig);
99
100 if (lenp != NULL)
101 *lenp = len;
102 if (sigp != NULL)
103 *sigp = ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100104 return 0;
105}
106
107int
108ssh_rsa_verify(
109 Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000110 u_char *signature, u_int signaturelen,
111 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100112{
113 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000114 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 EVP_MD_CTX md;
116 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100117 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000118 u_int len, dlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000119 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100120
121 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
122 error("ssh_rsa_verify: no RSA key");
123 return -1;
124 }
Ben Lindstrom03f39322002-04-02 20:43:11 +0000125 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000126 error("ssh_rsa_verify: n too small: %d bits",
127 BN_num_bits(key->rsa->n));
128 return -1;
129 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100130 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000131 buffer_append(&b, signature, signaturelen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100132 ktype = buffer_get_string(&b, NULL);
133 if (strcmp("ssh-rsa", ktype) != 0) {
134 error("ssh_rsa_verify: cannot handle type %s", ktype);
135 buffer_free(&b);
136 xfree(ktype);
137 return -1;
138 }
139 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000140 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100141 rlen = buffer_len(&b);
142 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000143 if (rlen != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
Damien Miller36e603d2001-11-12 11:03:35 +1100145 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100146 return -1;
147 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000148 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000149 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000150 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
Damien Miller36e603d2001-11-12 11:03:35 +1100151 xfree(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000152 return -1;
153 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 EVP_DigestInit(&md, evp_md);
155 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100156 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157
Ben Lindstrom425fb022001-03-29 00:31:20 +0000158 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100159 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 memset(sigblob, 's', len);
161 xfree(sigblob);
162 if (ret == 0) {
163 int ecode = ERR_get_error();
164 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
165 }
166 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
167 return ret;
168}