]> andersk Git - openssh.git/blame_incremental - ssh-rsa.c
- stevesk@cvs.openbsd.org 2006/07/05 02:42:09
[openssh.git] / ssh-rsa.c
... / ...
CommitLineData
1/* $OpenBSD: ssh-rsa.c,v 1.37 2006/03/25 13:17:02 djm Exp $ */
2/*
3 * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
4 *
5 * 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.
8 *
9 * 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.
16 */
17#include "includes.h"
18
19#include <openssl/evp.h>
20#include <openssl/err.h>
21
22#include "xmalloc.h"
23#include "log.h"
24#include "buffer.h"
25#include "bufaux.h"
26#include "key.h"
27#include "compat.h"
28#include "ssh.h"
29
30static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
31
32/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
33int
34ssh_rsa_sign(const Key *key, u_char **sigp, u_int *lenp,
35 const u_char *data, u_int datalen)
36{
37 const EVP_MD *evp_md;
38 EVP_MD_CTX md;
39 u_char digest[EVP_MAX_MD_SIZE], *sig;
40 u_int slen, dlen, len;
41 int ok, nid;
42 Buffer b;
43
44 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
45 error("ssh_rsa_sign: no RSA key");
46 return -1;
47 }
48 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
49 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
50 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
51 return -1;
52 }
53 EVP_DigestInit(&md, evp_md);
54 EVP_DigestUpdate(&md, data, datalen);
55 EVP_DigestFinal(&md, digest, &dlen);
56
57 slen = RSA_size(key->rsa);
58 sig = xmalloc(slen);
59
60 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
61 memset(digest, 'd', sizeof(digest));
62
63 if (ok != 1) {
64 int ecode = ERR_get_error();
65
66 error("ssh_rsa_sign: RSA_sign failed: %s",
67 ERR_error_string(ecode, NULL));
68 xfree(sig);
69 return -1;
70 }
71 if (len < slen) {
72 u_int diff = slen - len;
73 debug("slen %u > len %u", slen, len);
74 memmove(sig + diff, sig, len);
75 memset(sig, 0, diff);
76 } else if (len > slen) {
77 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
78 xfree(sig);
79 return -1;
80 }
81 /* encode signature */
82 buffer_init(&b);
83 buffer_put_cstring(&b, "ssh-rsa");
84 buffer_put_string(&b, sig, slen);
85 len = buffer_len(&b);
86 if (lenp != NULL)
87 *lenp = len;
88 if (sigp != NULL) {
89 *sigp = xmalloc(len);
90 memcpy(*sigp, buffer_ptr(&b), len);
91 }
92 buffer_free(&b);
93 memset(sig, 's', slen);
94 xfree(sig);
95
96 return 0;
97}
98
99int
100ssh_rsa_verify(const Key *key, const u_char *signature, u_int signaturelen,
101 const u_char *data, u_int datalen)
102{
103 Buffer b;
104 const EVP_MD *evp_md;
105 EVP_MD_CTX md;
106 char *ktype;
107 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
108 u_int len, dlen, modlen;
109 int rlen, ret, nid;
110
111 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
112 error("ssh_rsa_verify: no RSA key");
113 return -1;
114 }
115 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
116 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
117 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
118 return -1;
119 }
120 buffer_init(&b);
121 buffer_append(&b, signature, signaturelen);
122 ktype = buffer_get_string(&b, NULL);
123 if (strcmp("ssh-rsa", ktype) != 0) {
124 error("ssh_rsa_verify: cannot handle type %s", ktype);
125 buffer_free(&b);
126 xfree(ktype);
127 return -1;
128 }
129 xfree(ktype);
130 sigblob = buffer_get_string(&b, &len);
131 rlen = buffer_len(&b);
132 buffer_free(&b);
133 if (rlen != 0) {
134 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
135 xfree(sigblob);
136 return -1;
137 }
138 /* RSA_verify expects a signature of RSA_size */
139 modlen = RSA_size(key->rsa);
140 if (len > modlen) {
141 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
142 xfree(sigblob);
143 return -1;
144 } else if (len < modlen) {
145 u_int diff = modlen - len;
146 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
147 modlen, len);
148 sigblob = xrealloc(sigblob, 1, modlen);
149 memmove(sigblob + diff, sigblob, len);
150 memset(sigblob, 0, diff);
151 len = modlen;
152 }
153 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
154 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
155 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
156 xfree(sigblob);
157 return -1;
158 }
159 EVP_DigestInit(&md, evp_md);
160 EVP_DigestUpdate(&md, data, datalen);
161 EVP_DigestFinal(&md, digest, &dlen);
162
163 ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
164 memset(digest, 'd', sizeof(digest));
165 memset(sigblob, 's', len);
166 xfree(sigblob);
167 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
168 return ret;
169}
170
171/*
172 * See:
173 * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
174 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
175 */
176/*
177 * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
178 * oiw(14) secsig(3) algorithms(2) 26 }
179 */
180static const u_char id_sha1[] = {
181 0x30, 0x21, /* type Sequence, length 0x21 (33) */
182 0x30, 0x09, /* type Sequence, length 0x09 */
183 0x06, 0x05, /* type OID, length 0x05 */
184 0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
185 0x05, 0x00, /* NULL */
186 0x04, 0x14 /* Octet string, length 0x14 (20), followed by sha1 hash */
187};
188/*
189 * id-md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
190 * rsadsi(113549) digestAlgorithm(2) 5 }
191 */
192static const u_char id_md5[] = {
193 0x30, 0x20, /* type Sequence, length 0x20 (32) */
194 0x30, 0x0c, /* type Sequence, length 0x09 */
195 0x06, 0x08, /* type OID, length 0x05 */
196 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, /* id-md5 */
197 0x05, 0x00, /* NULL */
198 0x04, 0x10 /* Octet string, length 0x10 (16), followed by md5 hash */
199};
200
201static int
202openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
203 u_char *sigbuf, u_int siglen, RSA *rsa)
204{
205 u_int ret, rsasize, oidlen = 0, hlen = 0;
206 int len;
207 const u_char *oid = NULL;
208 u_char *decrypted = NULL;
209
210 ret = 0;
211 switch (type) {
212 case NID_sha1:
213 oid = id_sha1;
214 oidlen = sizeof(id_sha1);
215 hlen = 20;
216 break;
217 case NID_md5:
218 oid = id_md5;
219 oidlen = sizeof(id_md5);
220 hlen = 16;
221 break;
222 default:
223 goto done;
224 }
225 if (hashlen != hlen) {
226 error("bad hashlen");
227 goto done;
228 }
229 rsasize = RSA_size(rsa);
230 if (siglen == 0 || siglen > rsasize) {
231 error("bad siglen");
232 goto done;
233 }
234 decrypted = xmalloc(rsasize);
235 if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
236 RSA_PKCS1_PADDING)) < 0) {
237 error("RSA_public_decrypt failed: %s",
238 ERR_error_string(ERR_get_error(), NULL));
239 goto done;
240 }
241 if (len < 0 || (u_int)len != hlen + oidlen) {
242 error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
243 goto done;
244 }
245 if (memcmp(decrypted, oid, oidlen) != 0) {
246 error("oid mismatch");
247 goto done;
248 }
249 if (memcmp(decrypted + oidlen, hash, hlen) != 0) {
250 error("hash mismatch");
251 goto done;
252 }
253 ret = 1;
254done:
255 if (decrypted)
256 xfree(decrypted);
257 return ret;
258}
This page took 0.033377 seconds and 5 git commands to generate.