]> andersk Git - openssh.git/blame - ssh-rsa.c
- (djm) Add --with-pam to RPM spec files
[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"
9616313f 26RCSID("$OpenBSD: ssh-rsa.c,v 1.3 2001/01/06 11:23:27 markus Exp $");
fa08c86b 27
28#include "ssh.h"
29#include "xmalloc.h"
30#include "buffer.h"
31#include "bufaux.h"
32
33#include <openssl/evp.h>
34#include <openssl/dsa.h>
35#include <openssl/rsa.h>
36#include <openssl/err.h>
37
38#include "key.h"
39
fa08c86b 40/* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
41int
42ssh_rsa_sign(
43 Key *key,
1e3b8b07 44 u_char **sigp, int *lenp,
45 u_char *data, int datalen)
fa08c86b 46{
47 EVP_MD *evp_md = EVP_sha1();
48 EVP_MD_CTX md;
1e3b8b07 49 u_char *digest, *sig, *ret;
50 u_int slen, dlen, len;
fa08c86b 51 int ok;
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 }
58 slen = RSA_size(key->rsa);
59 sig = xmalloc(slen);
60
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
67 ok = RSA_sign(NID_sha1, digest, dlen, sig, &len, key->rsa);
68 memset(digest, 'd', dlen);
69 xfree(digest);
70
71 if (ok != 1) {
72 int ecode = ERR_get_error();
73 error("ssh_rsa_sign: RSA_sign failed: %s", ERR_error_string(ecode, NULL));
74 xfree(sig);
75 return -1;
76 }
77 if (len < slen) {
78 int diff = slen - len;
79 debug("slen %d > len %d", slen, len);
80 memmove(sig + diff, sig, len);
81 memset(sig, 0, diff);
82 } else if (len > slen) {
83 error("ssh_rsa_sign: slen %d slen2 %d", slen, len);
84 xfree(sig);
85 return -1;
86 }
87 /* encode signature */
88 buffer_init(&b);
89 buffer_put_cstring(&b, "ssh-rsa");
90 buffer_put_string(&b, sig, slen);
91 len = buffer_len(&b);
92 ret = xmalloc(len);
93 memcpy(ret, buffer_ptr(&b), len);
94 buffer_free(&b);
95 memset(sig, 's', slen);
96 xfree(sig);
97
98 if (lenp != NULL)
99 *lenp = len;
100 if (sigp != NULL)
101 *sigp = ret;
102 debug2("ssh_rsa_sign: done");
103 return 0;
104}
105
106int
107ssh_rsa_verify(
108 Key *key,
1e3b8b07 109 u_char *signature, int signaturelen,
110 u_char *data, int datalen)
fa08c86b 111{
112 Buffer b;
113 EVP_MD *evp_md = EVP_sha1();
114 EVP_MD_CTX md;
115 char *ktype;
1e3b8b07 116 u_char *sigblob, *digest;
117 u_int len, dlen;
fa08c86b 118 int rlen;
119 int ret;
120
121 if (key == NULL || key->type != KEY_RSA || key->rsa == NULL) {
122 error("ssh_rsa_verify: no RSA key");
123 return -1;
124 }
125 buffer_init(&b);
126 buffer_append(&b, (char *) signature, signaturelen);
127 ktype = buffer_get_string(&b, NULL);
128 if (strcmp("ssh-rsa", ktype) != 0) {
129 error("ssh_rsa_verify: cannot handle type %s", ktype);
130 buffer_free(&b);
131 xfree(ktype);
132 return -1;
133 }
134 xfree(ktype);
1e3b8b07 135 sigblob = (u_char *)buffer_get_string(&b, &len);
fa08c86b 136 rlen = buffer_len(&b);
137 buffer_free(&b);
138 if(rlen != 0) {
139 error("ssh_rsa_verify: remaining bytes in signature %d", rlen);
140 return -1;
141 }
142
143 dlen = evp_md->md_size;
144 digest = xmalloc(dlen);
145 EVP_DigestInit(&md, evp_md);
146 EVP_DigestUpdate(&md, data, datalen);
147 EVP_DigestFinal(&md, digest, NULL);
148
149 ret = RSA_verify(NID_sha1, digest, dlen, sigblob, len, key->rsa);
150 memset(digest, 'd', dlen);
151 xfree(digest);
152 memset(sigblob, 's', len);
153 xfree(sigblob);
154 if (ret == 0) {
155 int ecode = ERR_get_error();
156 error("ssh_rsa_verify: RSA_verify failed: %s", ERR_error_string(ecode, NULL));
157 }
158 debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
159 return ret;
160}
This page took 0.062327 seconds and 5 git commands to generate.