xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-dss.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ssh-dss.c,v 1.17 2020/05/28 17:05:49 christos Exp $	*/
2 /* $OpenBSD: ssh-dss.c,v 1.39 2020/02/26 13:40:09 jsg 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.17 2020/05/28 17:05:49 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 	const BIGNUM *sig_r, *sig_s;
52 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
53 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
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, &sig_r, &sig_s);
78 	rlen = BN_num_bytes(sig_r);
79 	slen = BN_num_bytes(sig_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(sig_r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
86 	BN_bn2bin(sig_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 	BIGNUM *sig_r = NULL, *sig_s = NULL;
121 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
122 	size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
123 	int ret = SSH_ERR_INTERNAL_ERROR;
124 	struct sshbuf *b = NULL;
125 	char *ktype = NULL;
126 
127 	if (key == NULL || key->dsa == NULL ||
128 	    sshkey_type_plain(key->type) != KEY_DSA ||
129 	    signature == NULL || signaturelen == 0)
130 		return SSH_ERR_INVALID_ARGUMENT;
131 	if (dlen == 0)
132 		return SSH_ERR_INTERNAL_ERROR;
133 
134 	/* fetch signature */
135 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
136 		return SSH_ERR_ALLOC_FAIL;
137 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
138 	    sshbuf_get_string(b, &sigblob, &len) != 0) {
139 		ret = SSH_ERR_INVALID_FORMAT;
140 		goto out;
141 	}
142 	if (strcmp("ssh-dss", ktype) != 0) {
143 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
144 		goto out;
145 	}
146 	if (sshbuf_len(b) != 0) {
147 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
148 		goto out;
149 	}
150 
151 	if (len != SIGBLOB_LEN) {
152 		ret = SSH_ERR_INVALID_FORMAT;
153 		goto out;
154 	}
155 
156 	/* parse signature */
157 	if ((sig = DSA_SIG_new()) == NULL ||
158 	    (sig_r = BN_new()) == NULL ||
159 	    (sig_s = BN_new()) == NULL) {
160 		ret = SSH_ERR_ALLOC_FAIL;
161 		goto out;
162 	}
163 	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig_r) == NULL) ||
164 	    (BN_bin2bn(sigblob + INTBLOB_LEN, INTBLOB_LEN, sig_s) == NULL)) {
165 		ret = SSH_ERR_LIBCRYPTO_ERROR;
166 		goto out;
167 	}
168 	if (!DSA_SIG_set0(sig, sig_r, sig_s)) {
169 		ret = SSH_ERR_LIBCRYPTO_ERROR;
170 		goto out;
171 	}
172 	sig_r = sig_s = NULL; /* transferred */
173 
174 	/* sha1 the data */
175 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
176 	    digest, sizeof(digest))) != 0)
177 		goto out;
178 
179 	switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
180 	case 1:
181 		ret = 0;
182 		break;
183 	case 0:
184 		ret = SSH_ERR_SIGNATURE_INVALID;
185 		goto out;
186 	default:
187 		ret = SSH_ERR_LIBCRYPTO_ERROR;
188 		goto out;
189 	}
190 
191  out:
192 	explicit_bzero(digest, sizeof(digest));
193 	DSA_SIG_free(sig);
194 	BN_clear_free(sig_r);
195 	BN_clear_free(sig_s);
196 	sshbuf_free(b);
197 	free(ktype);
198 	if (sigblob != NULL)
199 		freezero(sigblob, len);
200 	return ret;
201 }
202