]> andersk Git - openssh.git/blame - ssh-rsa.c
- markus@cvs.openbsd.org 2001/03/27 10:34:08
[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"
1a92bd7e 26RCSID("$OpenBSD: ssh-rsa.c,v 1.7 2001/03/27 10:34:08 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"
fa08c86b 37
fa08c86b 38/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
39int
40ssh_rsa_sign(
41 Key *key,
1e3b8b07 42 u_char **sigp, int *lenp,
43 u_char *data, int datalen)
fa08c86b 44{
1a92bd7e 45 const EVP_MD *evp_md;
fa08c86b 46 EVP_MD_CTX md;
1e3b8b07 47 u_char *digest, *sig, *ret;
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 }
1a92bd7e 56 nid = NID_sha1;
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 dlen = evp_md->md_size;
62 digest = xmalloc(dlen);
63 EVP_DigestInit(&md, evp_md);
64 EVP_DigestUpdate(&md, data, datalen);
65 EVP_DigestFinal(&md, digest, NULL);
66
1a92bd7e 67 slen = RSA_size(key->rsa);
68 sig = xmalloc(slen);
69
70 ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
fa08c86b 71 memset(digest, 'd', dlen);
72 xfree(digest);
73
74 if (ok != 1) {
75 int ecode = ERR_get_error();
76 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
77 xfree(sig);
78 return -1;
79 }
80 if (len < slen) {
81 int diff = slen - len;
82 debug("slen %d > len %d", slen, len);
83 memmove(sig + diff, sig, len);
84 memset(sig, 0, diff);
85 } else if (len > slen) {
86 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
87 xfree(sig);
88 return -1;
89 }
90 /* encode signature */
91 buffer_init(&b);
92 buffer_put_cstring(&b, "ssh-rsa");
93 buffer_put_string(&b, sig, slen);
94 len = buffer_len(&b);
95 ret = xmalloc(len);
96 memcpy(ret, buffer_ptr(&b), len);
97 buffer_free(&b);
98 memset(sig, 's', slen);
99 xfree(sig);
100
101 if (lenp != NULL)
102 *lenp = len;
103 if (sigp != NULL)
104 *sigp = ret;
105 debug2("ssh_rsa_sign: done");
106 return 0;
107}
108
109int
110ssh_rsa_verify(
111 Key *key,
1e3b8b07 112 u_char *signature, int signaturelen,
113 u_char *data, int datalen)
fa08c86b 114{
115 Buffer b;
1a92bd7e 116 const EVP_MD *evp_md;
fa08c86b 117 EVP_MD_CTX md;
118 char *ktype;
1e3b8b07 119 u_char *sigblob, *digest;
120 u_int len, dlen;
1a92bd7e 121 int rlen, ret, nid;
fa08c86b 122
123 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
124 error("ssh_rsa_verify: no RSA key");
125 return -1;
126 }
b5c334cc 127 if (BN_num_bits(key->rsa->n) < 768) {
128 error("ssh_rsa_verify: n too small: %d bits",
129 BN_num_bits(key->rsa->n));
130 return -1;
131 }
fa08c86b 132 buffer_init(&b);
133 buffer_append(&b, (char *) signature, signaturelen);
134 ktype = buffer_get_string(&b, NULL);
135 if (strcmp("ssh-rsa", ktype) != 0) {
136 error("ssh_rsa_verify: cannot handle type %s", ktype);
137 buffer_free(&b);
138 xfree(ktype);
139 return -1;
140 }
141 xfree(ktype);
1e3b8b07 142 sigblob = (u_char *)buffer_get_string(&b, &len);
fa08c86b 143 rlen = buffer_len(&b);
144 buffer_free(&b);
145 if(rlen != 0) {
1a92bd7e 146 xfree(sigblob);
fa08c86b 147 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
148 return -1;
149 }
1a92bd7e 150 nid = NID_sha1;
151 if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
152 xfree(sigblob);
153 error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
154 return -1;
155 }
fa08c86b 156 dlen = evp_md->md_size;
157 digest = xmalloc(dlen);
158 EVP_DigestInit(&md, evp_md);
159 EVP_DigestUpdate(&md, data, datalen);
160 EVP_DigestFinal(&md, digest, NULL);
161
1a92bd7e 162 ret = RSA_verify(nid, digest, dlen, sigblob, len, key->rsa);
fa08c86b 163 memset(digest, 'd', dlen);
164 xfree(digest);
165 memset(sigblob, 's', len);
166 xfree(sigblob);
167 if (ret == 0) {
168 int ecode = ERR_get_error();
169 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
170 }
171 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
172 return ret;
173}
This page took 0.131228 seconds and 5 git commands to generate.