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