]> andersk Git - openssh.git/blame - ssh-rsa.c
- (bal) Missed msg.[ch] in merge. Required for ssh-keysign.
[openssh.git] / ssh-rsa.c
CommitLineData
fa08c86b 1/*
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"
88f24e9d 26RCSID("$OpenBSD: ssh-rsa.c,v 1.19 2002/05/31 13:20:50 markus Exp $");
fa08c86b 27
28#include <openssl/evp.h>
fa08c86b 29#include <openssl/err.h>
30
42f11eb2 31#include "xmalloc.h"
32#include "log.h"
33#include "buffer.h"
34#include "bufaux.h"
fa08c86b 35#include "key.h"
5ca51e19 36#include "ssh-rsa.h"
a4da628b 37#include "compat.h"
82b00264 38#include "ssh.h"
fa08c86b 39
fa08c86b 40/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
41int
42ssh_rsa_sign(
43 Key *key,
c66f9d0e 44 u_char **sigp, u_int *lenp,
45 u_char *data, u_int datalen)
fa08c86b 46{
1a92bd7e 47 const EVP_MD *evp_md;
fa08c86b 48 EVP_MD_CTX md;
087dea86 49 u_char digest[EVP_MAX_MD_SIZE], *sig, *ret;
1e3b8b07 50 u_int slen, dlen, len;
1a92bd7e 51 int ok, nid;
fa08c86b 52 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 }
a4da628b 58 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 59 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
60 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
61 return -1;
62 }
fa08c86b 63 EVP_DigestInit(&md, evp_md);
64 EVP_DigestUpdate(&md, data, datalen);
087dea86 65 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 66
1a92bd7e 67 slen = RSA_size(key->rsa);
68 sig = xmalloc(slen);
69
70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
087dea86 71 memset(digest, 'd', sizeof(digest));
fa08c86b 72
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;
fa08c86b 104 return 0;
105}
106
107int
108ssh_rsa_verify(
109 Key *key,
c66f9d0e 110 u_char *signature, u_int signaturelen,
111 u_char *data, u_int datalen)
fa08c86b 112{
113 Buffer b;
1a92bd7e 114 const EVP_MD *evp_md;
fa08c86b 115 EVP_MD_CTX md;
116 char *ktype;
087dea86 117 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
88f24e9d 118 u_int len, dlen, modlen;
1a92bd7e 119 int rlen, ret, nid;
fa08c86b 120
121 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
122 error("ssh_rsa_verify: no RSA key");
123 return -1;
124 }
82b00264 125 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
b5c334cc 126 error("ssh_rsa_verify: n too small: %d bits",
127 BN_num_bits(key->rsa->n));
128 return -1;
129 }
fa08c86b 130 buffer_init(&b);
cf54363d 131 buffer_append(&b, signature, signaturelen);
fa08c86b 132 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);
cf54363d 140 sigblob = buffer_get_string(&b, &len);
fa08c86b 141 rlen = buffer_len(&b);
142 buffer_free(&b);
6aacefa7 143 if (rlen != 0) {
fa08c86b 144 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
abc4e9a7 145 xfree(sigblob);
fa08c86b 146 return -1;
147 }
88f24e9d 148 /* RSA_verify expects a signature of RSA_size */
149 modlen = RSA_size(key->rsa);
150 if (len > modlen) {
151 error("ssh_rsa_verify: len %d > modlen %d", len, modlen);
152 xfree(sigblob);
153 return -1;
154 } else if (len < modlen) {
155 int diff = modlen - len;
156 debug("ssh_rsa_verify: add padding: modlen %d > len %d",
157 modlen, len);
158 sigblob = xrealloc(sigblob, modlen);
159 memmove(sigblob + diff, sigblob, len);
160 memset(sigblob, 0, diff);
161 len = modlen;
162 }
a4da628b 163 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 164 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
1a92bd7e 165 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
abc4e9a7 166 xfree(sigblob);
1a92bd7e 167 return -1;
168 }
fa08c86b 169 EVP_DigestInit(&md, evp_md);
170 EVP_DigestUpdate(&md, data, datalen);
087dea86 171 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 172
1a92bd7e 173 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
087dea86 174 memset(digest, 'd', sizeof(digest));
fa08c86b 175 memset(sigblob, 's', len);
176 xfree(sigblob);
177 if (ret == 0) {
178 int ecode = ERR_get_error();
179 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
180 }
181 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
182 return ret;
183}
This page took 0.150671 seconds and 5 git commands to generate.