blob: adc8f983e0927085aeb116a6bf8b8a02122c0844 [file] [log] [blame]
Damien Millera664e872000-04-16 11:52:47 +10001/*
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.
Damien Millera664e872000-04-16 11:52:47 +100012 *
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"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000026RCSID("$OpenBSD: ssh-dss.c,v 1.6 2001/02/08 19:30:52 itojun Exp $");
Damien Millera664e872000-04-16 11:52:47 +100027
Ben Lindstrom226cfa02001-01-22 05:34:40 +000028#include <openssl/bn.h>
29#include <openssl/evp.h>
30
Damien Millera664e872000-04-16 11:52:47 +100031#include "xmalloc.h"
32#include "buffer.h"
33#include "bufaux.h"
34#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "log.h"
Damien Millera664e872000-04-16 11:52:47 +100036#include "key.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000037#include "ssh-dss.h"
Damien Millera664e872000-04-16 11:52:47 +100038
39#define INTBLOB_LEN 20
40#define SIGBLOB_LEN (2*INTBLOB_LEN)
41
Damien Millera664e872000-04-16 11:52:47 +100042int
Damien Miller0bc1bd82000-11-13 22:57:25 +110043ssh_dss_sign(
Damien Millera664e872000-04-16 11:52:47 +100044 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +000045 u_char **sigp, int *lenp,
46 u_char *data, int datalen)
Damien Millera664e872000-04-16 11:52:47 +100047{
Ben Lindstrom46c16222000-12-22 01:43:59 +000048 u_char *digest;
49 u_char *ret;
Damien Millera664e872000-04-16 11:52:47 +100050 DSA_SIG *sig;
51 EVP_MD *evp_md = EVP_sha1();
52 EVP_MD_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +000053 u_int rlen;
54 u_int slen;
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055 u_int len, dlen;
Ben Lindstrom46c16222000-12-22 01:43:59 +000056 u_char sigblob[SIGBLOB_LEN];
Damien Millera664e872000-04-16 11:52:47 +100057 Buffer b;
58
59 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110060 error("ssh_dss_sign: no DSA key");
Damien Millera664e872000-04-16 11:52:47 +100061 return -1;
62 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063 dlen = evp_md->md_size;
64 digest = xmalloc(dlen);
Damien Millera664e872000-04-16 11:52:47 +100065 EVP_DigestInit(&md, evp_md);
Damien Millereba71ba2000-04-29 23:57:08 +100066 EVP_DigestUpdate(&md, data, datalen);
Damien Millera664e872000-04-16 11:52:47 +100067 EVP_DigestFinal(&md, digest, NULL);
68
Ben Lindstrom226cfa02001-01-22 05:34:40 +000069 sig = DSA_do_sign(digest, dlen, key->dsa);
Damien Millereba71ba2000-04-29 23:57:08 +100070 if (sig == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110071 fatal("ssh_dss_sign: cannot sign");
Damien Millereba71ba2000-04-29 23:57:08 +100072 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073 memset(digest, 0, dlen);
74 xfree(digest);
Damien Millera664e872000-04-16 11:52:47 +100075
76 rlen = BN_num_bytes(sig->r);
77 slen = BN_num_bytes(sig->s);
78 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
79 error("bad sig size %d %d", rlen, slen);
80 DSA_SIG_free(sig);
81 return -1;
82 }
83 debug("sig size %d %d", rlen, slen);
84
85 memset(sigblob, 0, SIGBLOB_LEN);
86 BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
87 BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
88 DSA_SIG_free(sig);
89
Damien Miller30c3d422000-05-09 11:02:59 +100090 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millera664e872000-04-16 11:52:47 +100091 debug("datafellows");
92 ret = xmalloc(SIGBLOB_LEN);
93 memcpy(ret, sigblob, SIGBLOB_LEN);
94 if (lenp != NULL)
95 *lenp = SIGBLOB_LEN;
96 if (sigp != NULL)
97 *sigp = ret;
98 } else {
99 /* ietf-drafts */
100 buffer_init(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100101 buffer_put_cstring(&b, "ssh-dss");
Damien Millera664e872000-04-16 11:52:47 +1000102 buffer_put_string(&b, sigblob, SIGBLOB_LEN);
103 len = buffer_len(&b);
104 ret = xmalloc(len);
105 memcpy(ret, buffer_ptr(&b), len);
106 buffer_free(&b);
107 if (lenp != NULL)
108 *lenp = len;
109 if (sigp != NULL)
110 *sigp = ret;
111 }
112 return 0;
113}
114int
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115ssh_dss_verify(
Damien Millera664e872000-04-16 11:52:47 +1000116 Key *key,
Ben Lindstrom46c16222000-12-22 01:43:59 +0000117 u_char *signature, int signaturelen,
118 u_char *data, int datalen)
Damien Millera664e872000-04-16 11:52:47 +1000119{
120 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121 u_char *digest;
Damien Millera664e872000-04-16 11:52:47 +1000122 DSA_SIG *sig;
123 EVP_MD *evp_md = EVP_sha1();
124 EVP_MD_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000125 u_char *sigblob;
Damien Millera664e872000-04-16 11:52:47 +1000126 char *txt;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000127 u_int len, dlen;
Damien Millera664e872000-04-16 11:52:47 +1000128 int rlen;
129 int ret;
130
131 if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100132 error("ssh_dss_verify: no DSA key");
Damien Millera664e872000-04-16 11:52:47 +1000133 return -1;
134 }
135
Damien Miller30c3d422000-05-09 11:02:59 +1000136 if (!(datafellows & SSH_BUG_SIGBLOB) &&
137 signaturelen == SIGBLOB_LEN) {
138 datafellows |= ~SSH_BUG_SIGBLOB;
139 log("autodetect SSH_BUG_SIGBLOB");
140 } else if ((datafellows & SSH_BUG_SIGBLOB) &&
141 signaturelen != SIGBLOB_LEN) {
142 log("autoremove SSH_BUG_SIGBLOB");
143 datafellows &= ~SSH_BUG_SIGBLOB;
Damien Millera664e872000-04-16 11:52:47 +1000144 }
145
146 debug("len %d datafellows %d", signaturelen, datafellows);
147
148 /* fetch signature */
Damien Miller30c3d422000-05-09 11:02:59 +1000149 if (datafellows & SSH_BUG_SIGBLOB) {
Damien Millera664e872000-04-16 11:52:47 +1000150 sigblob = signature;
151 len = signaturelen;
152 } else {
153 /* ietf-drafts */
Damien Miller6536c7d2000-06-22 21:32:31 +1000154 char *ktype;
Damien Millera664e872000-04-16 11:52:47 +1000155 buffer_init(&b);
156 buffer_append(&b, (char *) signature, signaturelen);
157 ktype = buffer_get_string(&b, NULL);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158 if (strcmp("ssh-dss", ktype) != 0) {
159 error("ssh_dss_verify: cannot handle type %s", ktype);
Damien Miller6536c7d2000-06-22 21:32:31 +1000160 buffer_free(&b);
161 return -1;
162 }
Ben Lindstrom46c16222000-12-22 01:43:59 +0000163 sigblob = (u_char *)buffer_get_string(&b, &len);
Damien Millera664e872000-04-16 11:52:47 +1000164 rlen = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000165 if(rlen != 0) {
Damien Millera664e872000-04-16 11:52:47 +1000166 error("remaining bytes in signature %d", rlen);
Damien Miller6536c7d2000-06-22 21:32:31 +1000167 buffer_free(&b);
168 return -1;
169 }
Damien Millera664e872000-04-16 11:52:47 +1000170 buffer_free(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000171 xfree(ktype);
Damien Millera664e872000-04-16 11:52:47 +1000172 }
173
174 if (len != SIGBLOB_LEN) {
175 fatal("bad sigbloblen %d != SIGBLOB_LEN", len);
176 }
177
178 /* parse signature */
179 sig = DSA_SIG_new();
180 sig->r = BN_new();
181 sig->s = BN_new();
182 BN_bin2bn(sigblob, INTBLOB_LEN, sig->r);
183 BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s);
Damien Miller30c3d422000-05-09 11:02:59 +1000184
185 if (!(datafellows & SSH_BUG_SIGBLOB)) {
Damien Millera664e872000-04-16 11:52:47 +1000186 memset(sigblob, 0, len);
187 xfree(sigblob);
188 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000189
Damien Millereba71ba2000-04-29 23:57:08 +1000190 /* sha1 the data */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100191 dlen = evp_md->md_size;
192 digest = xmalloc(dlen);
Damien Millera664e872000-04-16 11:52:47 +1000193 EVP_DigestInit(&md, evp_md);
Damien Millereba71ba2000-04-29 23:57:08 +1000194 EVP_DigestUpdate(&md, data, datalen);
Damien Millera664e872000-04-16 11:52:47 +1000195 EVP_DigestFinal(&md, digest, NULL);
196
Damien Miller0bc1bd82000-11-13 22:57:25 +1100197 ret = DSA_do_verify(digest, dlen, sig, key->dsa);
Damien Millera664e872000-04-16 11:52:47 +1000198
Damien Miller0bc1bd82000-11-13 22:57:25 +1100199 memset(digest, 0, dlen);
Damien Millera664e872000-04-16 11:52:47 +1000200 xfree(digest);
201 DSA_SIG_free(sig);
202
203 switch (ret) {
204 case 1:
205 txt = "correct";
206 break;
207 case 0:
208 txt = "incorrect";
209 break;
210 case -1:
211 default:
212 txt = "error";
213 break;
214 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100215 debug("ssh_dss_verify: signature %s", txt);
Damien Millera664e872000-04-16 11:52:47 +1000216 return ret;
217}