]> andersk Git - openssh.git/blame - ssh-rsa.c
- deraadt@cvs.openbsd.org 2006/03/20 18:17:20
[openssh.git] / ssh-rsa.c
CommitLineData
fa08c86b 1/*
c1ffd4bd 2 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
fa08c86b 3 *
c1ffd4bd 4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
fa08c86b 7 *
c1ffd4bd 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
fa08c86b 15 */
fa08c86b 16#include "includes.h"
fa08c86b 17
18#include <openssl/evp.h>
fa08c86b 19#include <openssl/err.h>
20
42f11eb2 21#include "xmalloc.h"
22#include "log.h"
23#include "buffer.h"
24#include "bufaux.h"
fa08c86b 25#include "key.h"
a4da628b 26#include "compat.h"
82b00264 27#include "ssh.h"
fa08c86b 28
b77a87e5 29static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
0caf874a 30
fa08c86b 31/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
32int
b6c7b7b7 33ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
34 const u_char *data, u_int datalen)
fa08c86b 35{
1a92bd7e 36 const EVP_MD *evp_md;
fa08c86b 37 EVP_MD_CTX md;
eb9f2fab 38 u_char digest[EVP_MAX_MD_SIZE], *sig;
1e3b8b07 39 u_int slen, dlen, len;
1a92bd7e 40 int ok, nid;
fa08c86b 41 Buffer b;
42
43 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
44 error("ssh_rsa_sign: no RSA key");
45 return -1;
46 }
a4da628b 47 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 48 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
49 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
50 return -1;
51 }
fa08c86b 52 EVP_DigestInit(&md, evp_md);
53 EVP_DigestUpdate(&md, data, datalen);
087dea86 54 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 55
1a92bd7e 56 slen = RSA_size(key->rsa);
57 sig = xmalloc(slen);
58
59 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
087dea86 60 memset(digest, 'd', sizeof(digest));
fa08c86b 61
62 if (ok != 1) {
63 int ecode = ERR_get_error();
d6133f43 64 error("ssh_rsa_sign: RSA_sign failed: %s",
65 ERR_error_string(ecode, NULL));
fa08c86b 66 xfree(sig);
67 return -1;
68 }
69 if (len < slen) {
4004c2ac 70 u_int diff = slen - len;
d6133f43 71 debug("slen %u > len %u", slen, len);
fa08c86b 72 memmove(sig + diff, sig, len);
73 memset(sig, 0, diff);
74 } else if (len > slen) {
d6133f43 75 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
fa08c86b 76 xfree(sig);
77 return -1;
78 }
79 /* encode signature */
80 buffer_init(&b);
81 buffer_put_cstring(&b, "ssh-rsa");
82 buffer_put_string(&b, sig, slen);
83 len = buffer_len(&b);
eb9f2fab 84 if (lenp != NULL)
85 *lenp = len;
86 if (sigp != NULL) {
87 *sigp = xmalloc(len);
88 memcpy(*sigp, buffer_ptr(&b), len);
89 }
fa08c86b 90 buffer_free(&b);
91 memset(sig, 's', slen);
92 xfree(sig);
93
fa08c86b 94 return 0;
95}
96
97int
b6c7b7b7 98ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
99 const u_char *data, u_int datalen)
fa08c86b 100{
101 Buffer b;
1a92bd7e 102 const EVP_MD *evp_md;
fa08c86b 103 EVP_MD_CTX md;
104 char *ktype;
087dea86 105 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
88f24e9d 106 u_int len, dlen, modlen;
1a92bd7e 107 int rlen, ret, nid;
fa08c86b 108
109 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
110 error("ssh_rsa_verify: no RSA key");
111 return -1;
112 }
82b00264 113 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
b7ced7d1 114 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
115 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
b5c334cc 116 return -1;
117 }
fa08c86b 118 buffer_init(&b);
cf54363d 119 buffer_append(&b, signature, signaturelen);
fa08c86b 120 ktype = buffer_get_string(&b, NULL);
121 if (strcmp("ssh-rsa", ktype) != 0) {
122 error("ssh_rsa_verify: cannot handle type %s", ktype);
123 buffer_free(&b);
124 xfree(ktype);
125 return -1;
126 }
127 xfree(ktype);
cf54363d 128 sigblob = buffer_get_string(&b, &len);
fa08c86b 129 rlen = buffer_len(&b);
130 buffer_free(&b);
6aacefa7 131 if (rlen != 0) {
fa08c86b 132 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
abc4e9a7 133 xfree(sigblob);
fa08c86b 134 return -1;
135 }
88f24e9d 136 /* RSA_verify expects a signature of RSA_size */
137 modlen = RSA_size(key->rsa);
138 if (len > modlen) {
d6133f43 139 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
88f24e9d 140 xfree(sigblob);
141 return -1;
142 } else if (len < modlen) {
4004c2ac 143 u_int diff = modlen - len;
d6133f43 144 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
88f24e9d 145 modlen, len);
146 sigblob = xrealloc(sigblob, modlen);
147 memmove(sigblob + diff, sigblob, len);
148 memset(sigblob, 0, diff);
149 len = modlen;
150 }
a4da628b 151 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 152 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
1a92bd7e 153 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
abc4e9a7 154 xfree(sigblob);
1a92bd7e 155 return -1;
156 }
fa08c86b 157 EVP_DigestInit(&md, evp_md);
158 EVP_DigestUpdate(&md, data, datalen);
087dea86 159 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 160
0caf874a 161 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
087dea86 162 memset(digest, 'd', sizeof(digest));
fa08c86b 163 memset(sigblob, 's', len);
164 xfree(sigblob);
fa08c86b 165 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
166 return ret;
167}
0caf874a 168
169/*
170 * See:
171 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
172 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
173 */
174/*
175 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
176 * oiw(14) secsig(3) algorithms(2) 26 }
177 */
178static const u_char id_sha1[] = {
179 0x30, 0x21, /* type Sequence, length 0x21 (33) */
180 0x30, 0x09, /* type Sequence, length 0x09 */
181 0x06, 0x05, /* type OID, length 0x05 */
182 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
183 0x05, 0x00, /* NULL */
184 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
185};
186/*
187 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
188 * rsadsi(113549) digestAlgorithm(2) 5 }
189 */
190static const u_char id_md5[] = {
191 0x30, 0x20, /* type Sequence, length 0x20 (32) */
192 0x30, 0x0c, /* type Sequence, length 0x09 */
193 0x06, 0x08, /* type OID, length 0x05 */
194 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
195 0x05, 0x00, /* NULL */
196 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
197};
198
199static int
200openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
201 u_char *sigbuf, u_int siglen, RSA *rsa)
202{
b41baf4d 203 u_int ret, rsasize, oidlen = 0, hlen = 0;
204 int len;
0caf874a 205 const u_char *oid = NULL;
206 u_char *decrypted = NULL;
207
208 ret = 0;
209 switch (type) {
210 case NID_sha1:
211 oid = id_sha1;
212 oidlen = sizeof(id_sha1);
213 hlen = 20;
214 break;
215 case NID_md5:
216 oid = id_md5;
217 oidlen = sizeof(id_md5);
218 hlen = 16;
219 break;
220 default:
221 goto done;
0caf874a 222 }
223 if (hashlen != hlen) {
224 error("bad hashlen");
225 goto done;
226 }
227 rsasize = RSA_size(rsa);
228 if (siglen == 0 || siglen > rsasize) {
229 error("bad siglen");
230 goto done;
231 }
232 decrypted = xmalloc(rsasize);
233 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
234 RSA_PKCS1_PADDING)) < 0) {
235 error("RSA_public_decrypt failed: %s",
236 ERR_error_string(ERR_get_error(), NULL));
237 goto done;
238 }
2ceb8101 239 if (len < 0 || (u_int)len != hlen + oidlen) {
c1ffd4bd 240 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
0caf874a 241 goto done;
242 }
243 if (memcmp(decrypted, oid, oidlen) != 0) {
244 error("oid mismatch");
245 goto done;
246 }
247 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
248 error("hash mismatch");
249 goto done;
250 }
251 ret = 1;
252done:
253 if (decrypted)
254 xfree(decrypted);
255 return ret;
256}
This page took 0.261985 seconds and 5 git commands to generate.