blob: fec1953b49a15e2c67dea9f5c8f39346bbc8603d [file] [log] [blame]
Damien Miller86687062014-07-02 15:28:02 +10001/* $OpenBSD: ssh-rsa.c,v 1.52 2014/06/24 01:13:21 djm Exp $ */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002/*
Darren Tuckera251f802003-06-22 20:45:15 +10003 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
Damien Miller0bc1bd82000-11-13 22:57:25 +11004 *
Darren Tuckera251f802003-06-22 20:45:15 +10005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller0bc1bd82000-11-13 22:57:25 +11008 *
Darren Tuckera251f802003-06-22 20:45:15 +10009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller0bc1bd82000-11-13 22:57:25 +110016 */
Damien Millerd7834352006-08-05 12:39:39 +100017
Damien Miller0bc1bd82000-11-13 22:57:25 +110018#include "includes.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110019
Damien Millerd7834352006-08-05 12:39:39 +100020#include <sys/types.h>
21
Damien Miller0bc1bd82000-11-13 22:57:25 +110022#include <openssl/evp.h>
Damien Miller0bc1bd82000-11-13 22:57:25 +110023#include <openssl/err.h>
24
Damien Millerded319c2006-09-01 15:38:36 +100025#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100026#include <string.h>
27
Damien Miller86687062014-07-02 15:28:02 +100028#include "sshbuf.h"
Ben Lindstrom60a43812001-03-29 00:32:56 +000029#include "compat.h"
Damien Miller86687062014-07-02 15:28:02 +100030#include "ssherr.h"
31#define SSHKEY_INTERNAL
32#include "sshkey.h"
Damien Millerb3051d02014-01-10 10:58:53 +110033#include "digest.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110034
Damien Miller86687062014-07-02 15:28:02 +100035static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
Ben Lindstrom0deb5d92002-08-20 18:40:03 +000036
Damien Miller0bc1bd82000-11-13 22:57:25 +110037/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
38int
Damien Miller86687062014-07-02 15:28:02 +100039ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
40 const u_char *data, size_t datalen, u_int compat)
Damien Miller0bc1bd82000-11-13 22:57:25 +110041{
Damien Millerb3051d02014-01-10 10:58:53 +110042 int hash_alg;
Damien Miller86687062014-07-02 15:28:02 +100043 u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
44 size_t slen;
45 u_int dlen, len;
46 int nid, ret = SSH_ERR_INTERNAL_ERROR;
47 struct sshbuf *b = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +110048
Damien Miller86687062014-07-02 15:28:02 +100049 if (lenp != NULL)
50 *lenp = 0;
51 if (sigp != NULL)
52 *sigp = NULL;
53
54 if (key == NULL || key->rsa == NULL ||
55 sshkey_type_plain(key->type) != KEY_RSA)
56 return SSH_ERR_INVALID_ARGUMENT;
57 slen = RSA_size(key->rsa);
58 if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
59 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +110060
Damien Millerb3051d02014-01-10 10:58:53 +110061 /* hash the data */
62 hash_alg = SSH_DIGEST_SHA1;
Damien Miller324541e2013-12-31 12:25:40 +110063 nid = NID_sha1;
Damien Miller86687062014-07-02 15:28:02 +100064 if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
65 return SSH_ERR_INTERNAL_ERROR;
66 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
67 digest, sizeof(digest))) != 0)
68 goto out;
69
70 if ((sig = malloc(slen)) == NULL) {
71 ret = SSH_ERR_ALLOC_FAIL;
72 goto out;
Damien Millerb3051d02014-01-10 10:58:53 +110073 }
Damien Miller0bc1bd82000-11-13 22:57:25 +110074
Damien Miller86687062014-07-02 15:28:02 +100075 if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
76 ret = SSH_ERR_LIBCRYPTO_ERROR;
77 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +110078 }
79 if (len < slen) {
Damien Miller86687062014-07-02 15:28:02 +100080 size_t diff = slen - len;
Damien Miller0bc1bd82000-11-13 22:57:25 +110081 memmove(sig + diff, sig, len);
Damien Millera5103f42014-02-04 11:20:14 +110082 explicit_bzero(sig, diff);
Damien Miller0bc1bd82000-11-13 22:57:25 +110083 } else if (len > slen) {
Damien Miller86687062014-07-02 15:28:02 +100084 ret = SSH_ERR_INTERNAL_ERROR;
85 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +110086 }
87 /* encode signature */
Damien Miller86687062014-07-02 15:28:02 +100088 if ((b = sshbuf_new()) == NULL) {
89 ret = SSH_ERR_ALLOC_FAIL;
90 goto out;
91 }
92 if ((ret = sshbuf_put_cstring(b, "ssh-rsa")) != 0 ||
93 (ret = sshbuf_put_string(b, sig, slen)) != 0)
94 goto out;
95 len = sshbuf_len(b);
96 if (sigp != NULL) {
97 if ((*sigp = malloc(len)) == NULL) {
98 ret = SSH_ERR_ALLOC_FAIL;
99 goto out;
100 }
101 memcpy(*sigp, sshbuf_ptr(b), len);
102 }
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000103 if (lenp != NULL)
104 *lenp = len;
Damien Miller86687062014-07-02 15:28:02 +1000105 ret = 0;
106 out:
107 explicit_bzero(digest, sizeof(digest));
108 if (sig != NULL) {
109 explicit_bzero(sig, slen);
110 free(sig);
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000111 }
Damien Miller86687062014-07-02 15:28:02 +1000112 if (b != NULL)
113 sshbuf_free(b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114 return 0;
115}
116
117int
Damien Miller86687062014-07-02 15:28:02 +1000118ssh_rsa_verify(const struct sshkey *key,
119 const u_char *signature, size_t signaturelen,
120 const u_char *data, size_t datalen, u_int compat)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100121{
Damien Miller86687062014-07-02 15:28:02 +1000122 char *ktype = NULL;
123 int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
124 size_t len, diff, modlen, dlen;
125 struct sshbuf *b = NULL;
126 u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100127
Damien Miller86687062014-07-02 15:28:02 +1000128 if (key == NULL || key->rsa == NULL ||
129 sshkey_type_plain(key->type) != KEY_RSA ||
130 BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
131 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3e192952013-12-29 17:47:50 +1100132
Damien Miller86687062014-07-02 15:28:02 +1000133 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
134 return SSH_ERR_ALLOC_FAIL;
135 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
136 ret = SSH_ERR_INVALID_FORMAT;
137 goto out;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000138 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139 if (strcmp("ssh-rsa", ktype) != 0) {
Damien Miller86687062014-07-02 15:28:02 +1000140 ret = SSH_ERR_KEY_TYPE_MISMATCH;
141 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100142 }
Damien Miller86687062014-07-02 15:28:02 +1000143 if (sshbuf_get_string(b, &sigblob, &len) != 0) {
144 ret = SSH_ERR_INVALID_FORMAT;
145 goto out;
146 }
147 if (sshbuf_len(b) != 0) {
148 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
149 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100150 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000151 /* RSA_verify expects a signature of RSA_size */
152 modlen = RSA_size(key->rsa);
153 if (len > modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000154 ret = SSH_ERR_KEY_BITS_MISMATCH;
155 goto out;
Ben Lindstromceae9d12002-06-06 20:55:04 +0000156 } else if (len < modlen) {
Damien Miller86687062014-07-02 15:28:02 +1000157 diff = modlen - len;
158 osigblob = sigblob;
159 if ((sigblob = realloc(sigblob, modlen)) == NULL) {
160 sigblob = osigblob; /* put it back for clear/free */
161 ret = SSH_ERR_ALLOC_FAIL;
162 goto out;
163 }
Ben Lindstromceae9d12002-06-06 20:55:04 +0000164 memmove(sigblob + diff, sigblob, len);
Damien Millera5103f42014-02-04 11:20:14 +1100165 explicit_bzero(sigblob, diff);
Ben Lindstromceae9d12002-06-06 20:55:04 +0000166 len = modlen;
167 }
Damien Millerb3051d02014-01-10 10:58:53 +1100168 hash_alg = SSH_DIGEST_SHA1;
169 if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000170 ret = SSH_ERR_INTERNAL_ERROR;
171 goto out;
Ben Lindstrom425fb022001-03-29 00:31:20 +0000172 }
Damien Miller86687062014-07-02 15:28:02 +1000173 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
174 digest, sizeof(digest))) != 0)
175 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100176
Damien Millerb3051d02014-01-10 10:58:53 +1100177 ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
178 key->rsa);
Damien Miller86687062014-07-02 15:28:02 +1000179 out:
180 if (sigblob != NULL) {
181 explicit_bzero(sigblob, len);
182 free(sigblob);
183 }
184 if (ktype != NULL)
185 free(ktype);
186 if (b != NULL)
187 sshbuf_free(b);
Damien Millera5103f42014-02-04 11:20:14 +1100188 explicit_bzero(digest, sizeof(digest));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100189 return ret;
190}
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000191
192/*
193 * See:
194 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
195 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
196 */
197/*
198 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
199 * oiw(14) secsig(3) algorithms(2) 26 }
200 */
201static const u_char id_sha1[] = {
202 0x30, 0x21, /* type Sequence, length 0x21 (33) */
203 0x30, 0x09, /* type Sequence, length 0x09 */
204 0x06, 0x05, /* type OID, length 0x05 */
205 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
206 0x05, 0x00, /* NULL */
207 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
208};
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000209
210static int
Damien Miller86687062014-07-02 15:28:02 +1000211openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
212 u_char *sigbuf, size_t siglen, RSA *rsa)
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000213{
Damien Miller86687062014-07-02 15:28:02 +1000214 size_t ret, rsasize = 0, oidlen = 0, hlen = 0;
Damien Miller4e8285e2010-08-03 16:04:03 +1000215 int len, oidmatch, hashmatch;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000216 const u_char *oid = NULL;
217 u_char *decrypted = NULL;
218
Damien Miller86687062014-07-02 15:28:02 +1000219 ret = SSH_ERR_INTERNAL_ERROR;
Damien Millerb3051d02014-01-10 10:58:53 +1100220 switch (hash_alg) {
221 case SSH_DIGEST_SHA1:
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000222 oid = id_sha1;
223 oidlen = sizeof(id_sha1);
224 hlen = 20;
225 break;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000226 default:
227 goto done;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000228 }
229 if (hashlen != hlen) {
Damien Miller86687062014-07-02 15:28:02 +1000230 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000231 goto done;
232 }
233 rsasize = RSA_size(rsa);
Damien Miller86687062014-07-02 15:28:02 +1000234 if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
235 siglen == 0 || siglen > rsasize) {
236 ret = SSH_ERR_INVALID_ARGUMENT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000237 goto done;
238 }
Damien Miller86687062014-07-02 15:28:02 +1000239 if ((decrypted = malloc(rsasize)) == NULL) {
240 ret = SSH_ERR_ALLOC_FAIL;
241 goto done;
242 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000243 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
244 RSA_PKCS1_PADDING)) < 0) {
Damien Miller86687062014-07-02 15:28:02 +1000245 ret = SSH_ERR_LIBCRYPTO_ERROR;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000246 goto done;
247 }
Damien Miller86687062014-07-02 15:28:02 +1000248 if (len < 0 || (size_t)len != hlen + oidlen) {
249 ret = SSH_ERR_INVALID_FORMAT;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000250 goto done;
251 }
Damien Miller4e8285e2010-08-03 16:04:03 +1000252 oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
253 hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000254 if (!oidmatch || !hashmatch) {
255 ret = SSH_ERR_SIGNATURE_INVALID;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000256 goto done;
257 }
Damien Miller86687062014-07-02 15:28:02 +1000258 ret = 0;
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000259done:
Damien Miller86687062014-07-02 15:28:02 +1000260 if (decrypted) {
261 explicit_bzero(decrypted, rsasize);
262 free(decrypted);
263 }
Ben Lindstrom0deb5d92002-08-20 18:40:03 +0000264 return ret;
265}