1 /* $OpenBSD: ssh-dss.c,v 1.32 2014/06/24 01:13:21 djm Exp $ */
2 /*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "includes.h"
27
28 #ifdef WITH_OPENSSL
29
30 #include <sys/types.h>
31
32 #include <openssl/bn.h>
33 #include <openssl/dsa.h>
34 #include <openssl/evp.h>
35
36 #include <stdarg.h>
37 #include <string.h>
38
39 #include "sshbuf.h"
40 #include "compat.h"
41 #include "ssherr.h"
42 #include "digest.h"
43 #define SSHKEY_INTERNAL
44 #include "sshkey.h"
45
46 #define INTBLOB_LEN 20
47 #define SIGBLOB_LEN (2*INTBLOB_LEN)
48
49 int
ssh_dss_sign(const struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,u_int compat)50 ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
51 const u_char *data, size_t datalen, u_int compat)
52 {
53 DSA_SIG *sig = NULL;
54 const BIGNUM *sig_r, *sig_s;
55
56 u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
57 size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
58 struct sshbuf *b = NULL;
59 int ret = SSH_ERR_INVALID_ARGUMENT;
60
61 if (lenp != NULL)
62 *lenp = 0;
63 if (sigp != NULL)
64 *sigp = NULL;
65
66 if (key == NULL || key->dsa == NULL ||
67 sshkey_type_plain(key->type) != KEY_DSA)
68 return SSH_ERR_INVALID_ARGUMENT;
69 if (dlen == 0)
70 return SSH_ERR_INTERNAL_ERROR;
71
72 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
73 digest, sizeof(digest))) != 0)
74 goto out;
75
76 if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
77 ret = SSH_ERR_LIBCRYPTO_ERROR;
78 goto out;
79 }
80
81 DSA_SIG_get0(sig, &sig_r, &sig_s);
82 rlen = BN_num_bytes(sig_r);
83 slen = BN_num_bytes(sig_s);
84 if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
85 ret = SSH_ERR_INTERNAL_ERROR;
86 goto out;
87 }
88 explicit_bzero(sigblob, SIGBLOB_LEN);
89 BN_bn2bin(sig_r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
90 BN_bn2bin(sig_s, sigblob + SIGBLOB_LEN - slen);
91
92 if (compat & SSH_BUG_SIGBLOB) {
93 if (sigp != NULL) {
94 if ((*sigp = malloc(SIGBLOB_LEN)) == NULL) {
95 ret = SSH_ERR_ALLOC_FAIL;
96 goto out;
97 }
98 memcpy(*sigp, sigblob, SIGBLOB_LEN);
99 }
100 if (lenp != NULL)
101 *lenp = SIGBLOB_LEN;
102 ret = 0;
103 } else {
104 /* ietf-drafts */
105 if ((b = sshbuf_new()) == NULL) {
106 ret = SSH_ERR_ALLOC_FAIL;
107 goto out;
108 }
109 if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
110 (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
111 goto out;
112 len = sshbuf_len(b);
113 if (sigp != NULL) {
114 if ((*sigp = malloc(len)) == NULL) {
115 ret = SSH_ERR_ALLOC_FAIL;
116 goto out;
117 }
118 memcpy(*sigp, sshbuf_ptr(b), len);
119 }
120 if (lenp != NULL)
121 *lenp = len;
122 ret = 0;
123 }
124 out:
125 explicit_bzero(digest, sizeof(digest));
126 if (sig != NULL)
127 DSA_SIG_free(sig);
128 if (b != NULL)
129 sshbuf_free(b);
130 return ret;
131 }
132
133 int
ssh_dss_verify(const struct sshkey * key,const u_char * signature,size_t signaturelen,const u_char * data,size_t datalen,u_int compat)134 ssh_dss_verify(const struct sshkey *key,
135 const u_char *signature, size_t signaturelen,
136 const u_char *data, size_t datalen, u_int compat)
137 {
138 DSA_SIG *sig = NULL;
139 const BIGNUM *sig_r, *sig_s;
140
141 u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
142 size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
143 int ret = SSH_ERR_INTERNAL_ERROR;
144 struct sshbuf *b = NULL;
145 char *ktype = NULL;
146
147 if (key == NULL || key->dsa == NULL ||
148 sshkey_type_plain(key->type) != KEY_DSA)
149 return SSH_ERR_INVALID_ARGUMENT;
150 if (dlen == 0)
151 return SSH_ERR_INTERNAL_ERROR;
152
153 /* fetch signature */
154 if (compat & SSH_BUG_SIGBLOB) {
155 if ((sigblob = malloc(signaturelen)) == NULL)
156 return SSH_ERR_ALLOC_FAIL;
157 memcpy(sigblob, signature, signaturelen);
158 len = signaturelen;
159 } else {
160 /* ietf-drafts */
161 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
162 return SSH_ERR_ALLOC_FAIL;
163 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
164 sshbuf_get_string(b, &sigblob, &len) != 0) {
165 ret = SSH_ERR_INVALID_FORMAT;
166 goto out;
167 }
168 if (strcmp("ssh-dss", ktype) != 0) {
169 ret = SSH_ERR_KEY_TYPE_MISMATCH;
170 goto out;
171 }
172 if (sshbuf_len(b) != 0) {
173 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
174 goto out;
175 }
176 }
177
178 if (len != SIGBLOB_LEN) {
179 ret = SSH_ERR_INVALID_FORMAT;
180 goto out;
181 }
182
183 if ((sig = DSA_SIG_new()) == NULL) {
184 ret = SSH_ERR_ALLOC_FAIL;
185 goto out;
186 }
187
188 DSA_SIG_get0(sig, &sig_r, &sig_s);
189
190 /* parse signature */
191 if ((sig_r = BN_new()) == NULL ||
192 (sig_s = BN_new()) == NULL) {
193 ret = SSH_ERR_ALLOC_FAIL;
194 goto out;
195 }
196 if ((BN_bin2bn(sigblob, INTBLOB_LEN, (BIGNUM*)sig_r) == NULL) ||
197 (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, (BIGNUM*)sig_s) == NULL)) {
198 ret = SSH_ERR_LIBCRYPTO_ERROR;
199 goto out;
200 }
201
202 /* sha1 the data */
203 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
204 digest, sizeof(digest))) != 0)
205 goto out;
206
207 switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
208 case 1:
209 ret = 0;
210 break;
211 case 0:
212 ret = SSH_ERR_SIGNATURE_INVALID;
213 goto out;
214 default:
215 ret = SSH_ERR_LIBCRYPTO_ERROR;
216 goto out;
217 }
218
219 out:
220 explicit_bzero(digest, sizeof(digest));
221 if (sig != NULL)
222 DSA_SIG_free(sig);
223 if (b != NULL)
224 sshbuf_free(b);
225 if (ktype != NULL)
226 free(ktype);
227 if (sigblob != NULL) {
228 explicit_bzero(sigblob, len);
229 free(sigblob);
230 }
231 return ret;
232 }
233 #endif /* WITH_OPENSSL */
234