blob: b2fcc3408ad03a204b725271f2c97e4fb3d04bf0 [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 Lindstrom03f39322002-04-02 20:43:11 +000026RCSID("$OpenBSD: ssh-rsa.c,v 1.17 2002/03/29 19:18:33 stevesk 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 }
Damien Millereacff852001-11-12 11:07:35 +110058 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Miller9af8c3c2001-11-12 11:03:16 +110059 error("ssh_rsa_sign: SSH_BUG_SIGBLOB not supported");
60 return -1;
61 }
Ben Lindstrom60a43812001-03-29 00:32:56 +000062 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +000063 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
64 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
65 return -1;
66 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110067 EVP_DigestInit(&md, evp_md);
68 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +110069 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +110070
Ben Lindstrom425fb022001-03-29 00:31:20 +000071 slen = RSA_size(key->rsa);
72 sig = xmalloc(slen);
73
74 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +110075 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +110076
77 if (ok != 1) {
78 int ecode = ERR_get_error();
79 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
80 xfree(sig);
81 return -1;
82 }
83 if (len < slen) {
84 int diff = slen - len;
85 debug("slen %d > len %d", slen, len);
86 memmove(sig + diff, sig, len);
87 memset(sig, 0, diff);
88 } else if (len > slen) {
89 error("ssh_rsa_sign: slen %d slen2 %d", 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 ret = xmalloc(len);
99 memcpy(ret, buffer_ptr(&b), len);
100 buffer_free(&b);
101 memset(sig, 's', slen);
102 xfree(sig);
103
104 if (lenp != NULL)
105 *lenp = len;
106 if (sigp != NULL)
107 *sigp = ret;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100108 return 0;
109}
110
111int
112ssh_rsa_verify(
113 Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000114 u_char *signature, u_int signaturelen,
115 u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116{
117 Buffer b;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000118 const EVP_MD *evp_md;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119 EVP_MD_CTX md;
120 char *ktype;
Damien Millerc516e922002-02-05 11:53:43 +1100121 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000122 u_int len, dlen;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000123 int rlen, ret, nid;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100124
125 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
126 error("ssh_rsa_verify: no RSA key");
127 return -1;
128 }
Damien Millereacff852001-11-12 11:07:35 +1100129 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Miller9af8c3c2001-11-12 11:03:16 +1100130 error("ssh_rsa_verify: SSH_BUG_SIGBLOB not supported");
131 return -1;
132 }
Ben Lindstrom03f39322002-04-02 20:43:11 +0000133 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000134 error("ssh_rsa_verify: n too small: %d bits",
135 BN_num_bits(key->rsa->n));
136 return -1;
137 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100138 buffer_init(&b);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000139 buffer_append(&b, signature, signaturelen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100140 ktype = buffer_get_string(&b, NULL);
141 if (strcmp("ssh-rsa", ktype) != 0) {
142 error("ssh_rsa_verify: cannot handle type %s", ktype);
143 buffer_free(&b);
144 xfree(ktype);
145 return -1;
146 }
147 xfree(ktype);
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000148 sigblob = buffer_get_string(&b, &len);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100149 rlen = buffer_len(&b);
150 buffer_free(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000151 if (rlen != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100152 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
Damien Miller36e603d2001-11-12 11:03:35 +1100153 xfree(sigblob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100154 return -1;
155 }
Ben Lindstrom60a43812001-03-29 00:32:56 +0000156 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000157 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
Ben Lindstrom425fb022001-03-29 00:31:20 +0000158 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
Damien Miller36e603d2001-11-12 11:03:35 +1100159 xfree(sigblob);
Ben Lindstrom425fb022001-03-29 00:31:20 +0000160 return -1;
161 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100162 EVP_DigestInit(&md, evp_md);
163 EVP_DigestUpdate(&md, data, datalen);
Damien Millerc516e922002-02-05 11:53:43 +1100164 EVP_DigestFinal(&md, digest, &dlen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100165
Ben Lindstrom425fb022001-03-29 00:31:20 +0000166 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
Damien Millerc516e922002-02-05 11:53:43 +1100167 memset(digest, 'd', sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100168 memset(sigblob, 's', len);
169 xfree(sigblob);
170 if (ret == 0) {
171 int ecode = ERR_get_error();
172 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
173 }
174 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
175 return ret;
176}