]>
Commit | Line | Data |
---|---|---|
4f8c6159 | 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. | |
4f8c6159 | 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" | |
5ca51e19 | 26 | RCSID("$OpenBSD: ssh-dss.c,v 1.6 2001/02/08 19:30:52 itojun Exp $"); |
42f11eb2 | 27 | |
28 | #include <openssl/bn.h> | |
29 | #include <openssl/evp.h> | |
4f8c6159 | 30 | |
4f8c6159 | 31 | #include "xmalloc.h" |
32 | #include "buffer.h" | |
33 | #include "bufaux.h" | |
34 | #include "compat.h" | |
42f11eb2 | 35 | #include "log.h" |
4f8c6159 | 36 | #include "key.h" |
5ca51e19 | 37 | #include "ssh-dss.h" |
4f8c6159 | 38 | |
39 | #define INTBLOB_LEN 20 | |
40 | #define SIGBLOB_LEN (2*INTBLOB_LEN) | |
41 | ||
4f8c6159 | 42 | int |
fa08c86b | 43 | ssh_dss_sign( |
4f8c6159 | 44 | Key *key, |
1e3b8b07 | 45 | u_char **sigp, int *lenp, |
46 | u_char *data, int datalen) | |
4f8c6159 | 47 | { |
1e3b8b07 | 48 | u_char *digest; |
49 | u_char *ret; | |
4f8c6159 | 50 | DSA_SIG *sig; |
51 | EVP_MD *evp_md = EVP_sha1(); | |
52 | EVP_MD_CTX md; | |
1e3b8b07 | 53 | u_int rlen; |
54 | u_int slen; | |
42f11eb2 | 55 | u_int len, dlen; |
1e3b8b07 | 56 | u_char sigblob[SIGBLOB_LEN]; |
4f8c6159 | 57 | Buffer b; |
58 | ||
59 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { | |
fa08c86b | 60 | error("ssh_dss_sign: no DSA key"); |
4f8c6159 | 61 | return -1; |
62 | } | |
42f11eb2 | 63 | dlen = evp_md->md_size; |
64 | digest = xmalloc(dlen); | |
4f8c6159 | 65 | EVP_DigestInit(&md, evp_md); |
a306f2dd | 66 | EVP_DigestUpdate(&md, data, datalen); |
4f8c6159 | 67 | EVP_DigestFinal(&md, digest, NULL); |
68 | ||
42f11eb2 | 69 | sig = DSA_do_sign(digest, dlen, key->dsa); |
a306f2dd | 70 | if (sig == NULL) { |
fa08c86b | 71 | fatal("ssh_dss_sign: cannot sign"); |
a306f2dd | 72 | } |
42f11eb2 | 73 | memset(digest, 0, dlen); |
74 | xfree(digest); | |
4f8c6159 | 75 | |
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 | ||
d0c832f3 | 90 | if (datafellows & SSH_BUG_SIGBLOB) { |
4f8c6159 | 91 | 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); | |
fa08c86b | 101 | buffer_put_cstring(&b, "ssh-dss"); |
4f8c6159 | 102 | 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 | } | |
114 | int | |
fa08c86b | 115 | ssh_dss_verify( |
4f8c6159 | 116 | Key *key, |
1e3b8b07 | 117 | u_char *signature, int signaturelen, |
118 | u_char *data, int datalen) | |
4f8c6159 | 119 | { |
120 | Buffer b; | |
1e3b8b07 | 121 | u_char *digest; |
4f8c6159 | 122 | DSA_SIG *sig; |
123 | EVP_MD *evp_md = EVP_sha1(); | |
124 | EVP_MD_CTX md; | |
1e3b8b07 | 125 | u_char *sigblob; |
4f8c6159 | 126 | char *txt; |
1e3b8b07 | 127 | u_int len, dlen; |
4f8c6159 | 128 | int rlen; |
129 | int ret; | |
130 | ||
131 | if (key == NULL || key->type != KEY_DSA || key->dsa == NULL) { | |
fa08c86b | 132 | error("ssh_dss_verify: no DSA key"); |
4f8c6159 | 133 | return -1; |
134 | } | |
135 | ||
d0c832f3 | 136 | 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; | |
4f8c6159 | 144 | } |
145 | ||
146 | debug("len %d datafellows %d", signaturelen, datafellows); | |
147 | ||
148 | /* fetch signature */ | |
d0c832f3 | 149 | if (datafellows & SSH_BUG_SIGBLOB) { |
4f8c6159 | 150 | sigblob = signature; |
151 | len = signaturelen; | |
152 | } else { | |
153 | /* ietf-drafts */ | |
74fc9186 | 154 | char *ktype; |
4f8c6159 | 155 | buffer_init(&b); |
156 | buffer_append(&b, (char *) signature, signaturelen); | |
157 | ktype = buffer_get_string(&b, NULL); | |
fa08c86b | 158 | if (strcmp("ssh-dss", ktype) != 0) { |
159 | error("ssh_dss_verify: cannot handle type %s", ktype); | |
74fc9186 | 160 | buffer_free(&b); |
161 | return -1; | |
162 | } | |
1e3b8b07 | 163 | sigblob = (u_char *)buffer_get_string(&b, &len); |
4f8c6159 | 164 | rlen = buffer_len(&b); |
74fc9186 | 165 | if(rlen != 0) { |
4f8c6159 | 166 | error("remaining bytes in signature %d", rlen); |
74fc9186 | 167 | buffer_free(&b); |
168 | return -1; | |
169 | } | |
4f8c6159 | 170 | buffer_free(&b); |
74fc9186 | 171 | xfree(ktype); |
4f8c6159 | 172 | } |
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); | |
d0c832f3 | 184 | |
185 | if (!(datafellows & SSH_BUG_SIGBLOB)) { | |
4f8c6159 | 186 | memset(sigblob, 0, len); |
187 | xfree(sigblob); | |
188 | } | |
2b87da3b | 189 | |
a306f2dd | 190 | /* sha1 the data */ |
fa08c86b | 191 | dlen = evp_md->md_size; |
192 | digest = xmalloc(dlen); | |
4f8c6159 | 193 | EVP_DigestInit(&md, evp_md); |
a306f2dd | 194 | EVP_DigestUpdate(&md, data, datalen); |
4f8c6159 | 195 | EVP_DigestFinal(&md, digest, NULL); |
196 | ||
fa08c86b | 197 | ret = DSA_do_verify(digest, dlen, sig, key->dsa); |
4f8c6159 | 198 | |
fa08c86b | 199 | memset(digest, 0, dlen); |
4f8c6159 | 200 | 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 | } | |
fa08c86b | 215 | debug("ssh_dss_verify: signature %s", txt); |
4f8c6159 | 216 | return ret; |
217 | } |