]> andersk Git - openssh.git/blame - ssh-rsa.c
- deraadt@cvs.openbsd.org 2002/07/04 08:12:15
[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"
da2499f5 26RCSID("$OpenBSD: ssh-rsa.c,v 1.22 2002/07/04 04:15:33 deraadt 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
d6133f43 42ssh_rsa_sign(Key *key, u_char **sigp, u_int *lenp,
c66f9d0e 43 u_char *data, u_int datalen)
fa08c86b 44{
1a92bd7e 45 const EVP_MD *evp_md;
fa08c86b 46 EVP_MD_CTX md;
087dea86 47 u_char digest[EVP_MAX_MD_SIZE], *sig, *ret;
1e3b8b07 48 u_int slen, dlen, len;
1a92bd7e 49 int ok, nid;
fa08c86b 50 Buffer b;
51
52 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
53 error("ssh_rsa_sign: no RSA key");
54 return -1;
55 }
a4da628b 56 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 57 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
58 error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
59 return -1;
60 }
fa08c86b 61 EVP_DigestInit(&md, evp_md);
62 EVP_DigestUpdate(&md, data, datalen);
087dea86 63 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 64
1a92bd7e 65 slen = RSA_size(key->rsa);
66 sig = xmalloc(slen);
67
68 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
087dea86 69 memset(digest, 'd', sizeof(digest));
fa08c86b 70
71 if (ok != 1) {
72 int ecode = ERR_get_error();
d6133f43 73 error("ssh_rsa_sign: RSA_sign failed: %s",
74 ERR_error_string(ecode, NULL));
fa08c86b 75 xfree(sig);
76 return -1;
77 }
78 if (len < slen) {
79 int diff = slen - len;
d6133f43 80 debug("slen %u > len %u", slen, len);
fa08c86b 81 memmove(sig + diff, sig, len);
82 memset(sig, 0, diff);
83 } else if (len > slen) {
d6133f43 84 error("ssh_rsa_sign: slen %u slen2 %u", slen, len);
fa08c86b 85 xfree(sig);
86 return -1;
87 }
88 /* encode signature */
89 buffer_init(&b);
90 buffer_put_cstring(&b, "ssh-rsa");
91 buffer_put_string(&b, sig, slen);
92 len = buffer_len(&b);
93 ret = xmalloc(len);
94 memcpy(ret, buffer_ptr(&b), len);
95 buffer_free(&b);
96 memset(sig, 's', slen);
97 xfree(sig);
98
99 if (lenp != NULL)
100 *lenp = len;
101 if (sigp != NULL)
102 *sigp = ret;
da2499f5 103 else
104 xfree(ret);
fa08c86b 105 return 0;
106}
107
108int
d6133f43 109ssh_rsa_verify(Key *key, u_char *signature, u_int signaturelen,
c66f9d0e 110 u_char *data, u_int datalen)
fa08c86b 111{
112 Buffer b;
1a92bd7e 113 const EVP_MD *evp_md;
fa08c86b 114 EVP_MD_CTX md;
115 char *ktype;
087dea86 116 u_char digest[EVP_MAX_MD_SIZE], *sigblob;
88f24e9d 117 u_int len, dlen, modlen;
1a92bd7e 118 int rlen, ret, nid;
fa08c86b 119
120 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
121 error("ssh_rsa_verify: no RSA key");
122 return -1;
123 }
82b00264 124 if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
b7ced7d1 125 error("ssh_rsa_verify: RSA modulus too small: %d < minimum %d bits",
126 BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
b5c334cc 127 return -1;
128 }
fa08c86b 129 buffer_init(&b);
cf54363d 130 buffer_append(&b, signature, signaturelen);
fa08c86b 131 ktype = buffer_get_string(&b, NULL);
132 if (strcmp("ssh-rsa", ktype) != 0) {
133 error("ssh_rsa_verify: cannot handle type %s", ktype);
134 buffer_free(&b);
135 xfree(ktype);
136 return -1;
137 }
138 xfree(ktype);
cf54363d 139 sigblob = buffer_get_string(&b, &len);
fa08c86b 140 rlen = buffer_len(&b);
141 buffer_free(&b);
6aacefa7 142 if (rlen != 0) {
fa08c86b 143 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
abc4e9a7 144 xfree(sigblob);
fa08c86b 145 return -1;
146 }
88f24e9d 147 /* RSA_verify expects a signature of RSA_size */
148 modlen = RSA_size(key->rsa);
149 if (len > modlen) {
d6133f43 150 error("ssh_rsa_verify: len %u > modlen %u", len, modlen);
88f24e9d 151 xfree(sigblob);
152 return -1;
153 } else if (len < modlen) {
154 int diff = modlen - len;
d6133f43 155 debug("ssh_rsa_verify: add padding: modlen %u > len %u",
88f24e9d 156 modlen, len);
157 sigblob = xrealloc(sigblob, modlen);
158 memmove(sigblob + diff, sigblob, len);
159 memset(sigblob, 0, diff);
160 len = modlen;
161 }
a4da628b 162 nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
1a92bd7e 163 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
1a92bd7e 164 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
abc4e9a7 165 xfree(sigblob);
1a92bd7e 166 return -1;
167 }
fa08c86b 168 EVP_DigestInit(&md, evp_md);
169 EVP_DigestUpdate(&md, data, datalen);
087dea86 170 EVP_DigestFinal(&md, digest, &dlen);
fa08c86b 171
1a92bd7e 172 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
087dea86 173 memset(digest, 'd', sizeof(digest));
fa08c86b 174 memset(sigblob, 's', len);
175 xfree(sigblob);
176 if (ret == 0) {
177 int ecode = ERR_get_error();
d6133f43 178 error("ssh_rsa_verify: RSA_verify failed: %s",
179 ERR_error_string(ecode, NULL));
fa08c86b 180 }
181 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
182 return ret;
183}
This page took 4.434043 seconds and 5 git commands to generate.