2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: ssh-rsa.c,v 1.2 2000/12/19 23:17:58 markus Exp $");
33 #include <openssl/evp.h>
34 #include <openssl/dsa.h>
35 #include <openssl/rsa.h>
36 #include <openssl/err.h>
40 #define INTBLOB_LEN 20
41 #define SIGBLOB_LEN (2*INTBLOB_LEN)
43 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
47 u_char **sigp, int *lenp,
48 u_char *data, int datalen)
50 EVP_MD *evp_md = EVP_sha1();
52 u_char *digest, *sig, *ret;
53 u_int slen, dlen, len;
57 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
58 error("ssh_rsa_sign: no RSA key");
61 slen = RSA_size(key->rsa);
64 dlen = evp_md->md_size;
65 digest = xmalloc(dlen);
66 EVP_DigestInit(&md, evp_md);
67 EVP_DigestUpdate(&md, data, datalen);
68 EVP_DigestFinal(&md, digest, NULL);
70 ok = RSA_sign(NID_sha1, digest, dlen, sig, &len, key->rsa);
71 memset(digest, 'd', dlen);
75 int ecode = ERR_get_error();
76 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
81 int diff = slen - len;
82 debug("slen %d > len %d", slen, len);
83 memmove(sig + diff, sig, len);
85 } else if (len > slen) {
86 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
90 /* encode signature */
92 buffer_put_cstring(&b, "ssh-rsa");
93 buffer_put_string(&b, sig, slen);
96 memcpy(ret, buffer_ptr(&b), len);
98 memset(sig, 's', slen);
105 debug2("ssh_rsa_sign: done");
112 u_char *signature, int signaturelen,
113 u_char *data, int datalen)
116 EVP_MD *evp_md = EVP_sha1();
119 u_char *sigblob, *digest;
124 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
125 error("ssh_rsa_verify: no RSA key");
129 buffer_append(&b, (char *) signature, signaturelen);
130 ktype = buffer_get_string(&b, NULL);
131 if (strcmp("ssh-rsa", ktype) != 0) {
132 error("ssh_rsa_verify: cannot handle type %s", ktype);
138 sigblob = (u_char *)buffer_get_string(&b, &len);
139 rlen = buffer_len(&b);
142 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
146 dlen = evp_md->md_size;
147 digest = xmalloc(dlen);
148 EVP_DigestInit(&md, evp_md);
149 EVP_DigestUpdate(&md, data, datalen);
150 EVP_DigestFinal(&md, digest, NULL);
152 ret = RSA_verify(NID_sha1, digest, dlen, sigblob, len, key->rsa);
153 memset(digest, 'd', dlen);
155 memset(sigblob, 's', len);
158 int ecode = ERR_get_error();
159 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
161 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");