xref: /dflybsd-src/crypto/openssh/ssh-dss.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
1*ba1276acSMatthew Dillon /* $OpenBSD: ssh-dss.c,v 1.50 2024/01/11 01:45:36 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
418de8d7fSPeter Avalos  *
518de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
618de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
718de8d7fSPeter Avalos  * are met:
818de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
918de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1018de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1118de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1218de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
1318de8d7fSPeter Avalos  *
1418de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1518de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1618de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1718de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1818de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1918de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2018de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2118de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2218de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2318de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2418de8d7fSPeter Avalos  */
2518de8d7fSPeter Avalos 
2618de8d7fSPeter Avalos #include "includes.h"
2718de8d7fSPeter Avalos 
28*ba1276acSMatthew Dillon #if defined(WITH_OPENSSL) && defined(WITH_DSA)
29e9778795SPeter Avalos 
3018de8d7fSPeter Avalos #include <sys/types.h>
3118de8d7fSPeter Avalos 
3218de8d7fSPeter Avalos #include <openssl/bn.h>
3336e94dc5SPeter Avalos #include <openssl/dsa.h>
3418de8d7fSPeter Avalos #include <openssl/evp.h>
3518de8d7fSPeter Avalos 
3618de8d7fSPeter Avalos #include <stdarg.h>
3718de8d7fSPeter Avalos #include <string.h>
3818de8d7fSPeter Avalos 
3936e94dc5SPeter Avalos #include "sshbuf.h"
4036e94dc5SPeter Avalos #include "ssherr.h"
4136e94dc5SPeter Avalos #include "digest.h"
4236e94dc5SPeter Avalos #define SSHKEY_INTERNAL
4336e94dc5SPeter Avalos #include "sshkey.h"
4418de8d7fSPeter Avalos 
45664f4763Szrj #include "openbsd-compat/openssl-compat.h"
46664f4763Szrj 
4718de8d7fSPeter Avalos #define INTBLOB_LEN	20
4818de8d7fSPeter Avalos #define SIGBLOB_LEN	(2*INTBLOB_LEN)
4918de8d7fSPeter Avalos 
50*ba1276acSMatthew Dillon static u_int
ssh_dss_size(const struct sshkey * key)51*ba1276acSMatthew Dillon ssh_dss_size(const struct sshkey *key)
52*ba1276acSMatthew Dillon {
53*ba1276acSMatthew Dillon 	const BIGNUM *dsa_p;
54*ba1276acSMatthew Dillon 
55*ba1276acSMatthew Dillon 	if (key->dsa == NULL)
56*ba1276acSMatthew Dillon 		return 0;
57*ba1276acSMatthew Dillon 	DSA_get0_pqg(key->dsa, &dsa_p, NULL, NULL);
58*ba1276acSMatthew Dillon 	return BN_num_bits(dsa_p);
59*ba1276acSMatthew Dillon }
60*ba1276acSMatthew Dillon 
61*ba1276acSMatthew Dillon static int
ssh_dss_alloc(struct sshkey * k)62*ba1276acSMatthew Dillon ssh_dss_alloc(struct sshkey *k)
63*ba1276acSMatthew Dillon {
64*ba1276acSMatthew Dillon 	if ((k->dsa = DSA_new()) == NULL)
65*ba1276acSMatthew Dillon 		return SSH_ERR_ALLOC_FAIL;
66*ba1276acSMatthew Dillon 	return 0;
67*ba1276acSMatthew Dillon }
68*ba1276acSMatthew Dillon 
69*ba1276acSMatthew Dillon static void
ssh_dss_cleanup(struct sshkey * k)70*ba1276acSMatthew Dillon ssh_dss_cleanup(struct sshkey *k)
71*ba1276acSMatthew Dillon {
72*ba1276acSMatthew Dillon 	DSA_free(k->dsa);
73*ba1276acSMatthew Dillon 	k->dsa = NULL;
74*ba1276acSMatthew Dillon }
75*ba1276acSMatthew Dillon 
76*ba1276acSMatthew Dillon static int
ssh_dss_equal(const struct sshkey * a,const struct sshkey * b)77*ba1276acSMatthew Dillon ssh_dss_equal(const struct sshkey *a, const struct sshkey *b)
78*ba1276acSMatthew Dillon {
79*ba1276acSMatthew Dillon 	const BIGNUM *dsa_p_a, *dsa_q_a, *dsa_g_a, *dsa_pub_key_a;
80*ba1276acSMatthew Dillon 	const BIGNUM *dsa_p_b, *dsa_q_b, *dsa_g_b, *dsa_pub_key_b;
81*ba1276acSMatthew Dillon 
82*ba1276acSMatthew Dillon 	if (a->dsa == NULL || b->dsa == NULL)
83*ba1276acSMatthew Dillon 		return 0;
84*ba1276acSMatthew Dillon 	DSA_get0_pqg(a->dsa, &dsa_p_a, &dsa_q_a, &dsa_g_a);
85*ba1276acSMatthew Dillon 	DSA_get0_pqg(b->dsa, &dsa_p_b, &dsa_q_b, &dsa_g_b);
86*ba1276acSMatthew Dillon 	DSA_get0_key(a->dsa, &dsa_pub_key_a, NULL);
87*ba1276acSMatthew Dillon 	DSA_get0_key(b->dsa, &dsa_pub_key_b, NULL);
88*ba1276acSMatthew Dillon 	if (dsa_p_a == NULL || dsa_p_b == NULL ||
89*ba1276acSMatthew Dillon 	    dsa_q_a == NULL || dsa_q_b == NULL ||
90*ba1276acSMatthew Dillon 	    dsa_g_a == NULL || dsa_g_b == NULL ||
91*ba1276acSMatthew Dillon 	    dsa_pub_key_a == NULL || dsa_pub_key_b == NULL)
92*ba1276acSMatthew Dillon 		return 0;
93*ba1276acSMatthew Dillon 	if (BN_cmp(dsa_p_a, dsa_p_b) != 0)
94*ba1276acSMatthew Dillon 		return 0;
95*ba1276acSMatthew Dillon 	if (BN_cmp(dsa_q_a, dsa_q_b) != 0)
96*ba1276acSMatthew Dillon 		return 0;
97*ba1276acSMatthew Dillon 	if (BN_cmp(dsa_g_a, dsa_g_b) != 0)
98*ba1276acSMatthew Dillon 		return 0;
99*ba1276acSMatthew Dillon 	if (BN_cmp(dsa_pub_key_a, dsa_pub_key_b) != 0)
100*ba1276acSMatthew Dillon 		return 0;
101*ba1276acSMatthew Dillon 	return 1;
102*ba1276acSMatthew Dillon }
103*ba1276acSMatthew Dillon 
104*ba1276acSMatthew Dillon static int
ssh_dss_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)105*ba1276acSMatthew Dillon ssh_dss_serialize_public(const struct sshkey *key, struct sshbuf *b,
106*ba1276acSMatthew Dillon     enum sshkey_serialize_rep opts)
107*ba1276acSMatthew Dillon {
108*ba1276acSMatthew Dillon 	int r;
109*ba1276acSMatthew Dillon 	const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
110*ba1276acSMatthew Dillon 
111*ba1276acSMatthew Dillon 	if (key->dsa == NULL)
112*ba1276acSMatthew Dillon 		return SSH_ERR_INVALID_ARGUMENT;
113*ba1276acSMatthew Dillon 	DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g);
114*ba1276acSMatthew Dillon 	DSA_get0_key(key->dsa, &dsa_pub_key, NULL);
115*ba1276acSMatthew Dillon 	if (dsa_p == NULL || dsa_q == NULL ||
116*ba1276acSMatthew Dillon 	    dsa_g == NULL || dsa_pub_key == NULL)
117*ba1276acSMatthew Dillon 		return SSH_ERR_INTERNAL_ERROR;
118*ba1276acSMatthew Dillon 	if ((r = sshbuf_put_bignum2(b, dsa_p)) != 0 ||
119*ba1276acSMatthew Dillon 	    (r = sshbuf_put_bignum2(b, dsa_q)) != 0 ||
120*ba1276acSMatthew Dillon 	    (r = sshbuf_put_bignum2(b, dsa_g)) != 0 ||
121*ba1276acSMatthew Dillon 	    (r = sshbuf_put_bignum2(b, dsa_pub_key)) != 0)
122*ba1276acSMatthew Dillon 		return r;
123*ba1276acSMatthew Dillon 
124*ba1276acSMatthew Dillon 	return 0;
125*ba1276acSMatthew Dillon }
126*ba1276acSMatthew Dillon 
127*ba1276acSMatthew Dillon static int
ssh_dss_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)128*ba1276acSMatthew Dillon ssh_dss_serialize_private(const struct sshkey *key, struct sshbuf *b,
129*ba1276acSMatthew Dillon     enum sshkey_serialize_rep opts)
130*ba1276acSMatthew Dillon {
131*ba1276acSMatthew Dillon 	int r;
132*ba1276acSMatthew Dillon 	const BIGNUM *dsa_priv_key;
133*ba1276acSMatthew Dillon 
134*ba1276acSMatthew Dillon 	DSA_get0_key(key->dsa, NULL, &dsa_priv_key);
135*ba1276acSMatthew Dillon 	if (!sshkey_is_cert(key)) {
136*ba1276acSMatthew Dillon 		if ((r = ssh_dss_serialize_public(key, b, opts)) != 0)
137*ba1276acSMatthew Dillon 			return r;
138*ba1276acSMatthew Dillon 	}
139*ba1276acSMatthew Dillon 	if ((r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0)
140*ba1276acSMatthew Dillon 		return r;
141*ba1276acSMatthew Dillon 
142*ba1276acSMatthew Dillon 	return 0;
143*ba1276acSMatthew Dillon }
144*ba1276acSMatthew Dillon 
145*ba1276acSMatthew Dillon static int
ssh_dss_generate(struct sshkey * k,int bits)146*ba1276acSMatthew Dillon ssh_dss_generate(struct sshkey *k, int bits)
147*ba1276acSMatthew Dillon {
148*ba1276acSMatthew Dillon 	DSA *private;
149*ba1276acSMatthew Dillon 
150*ba1276acSMatthew Dillon 	if (bits != 1024)
151*ba1276acSMatthew Dillon 		return SSH_ERR_KEY_LENGTH;
152*ba1276acSMatthew Dillon 	if ((private = DSA_new()) == NULL)
153*ba1276acSMatthew Dillon 		return SSH_ERR_ALLOC_FAIL;
154*ba1276acSMatthew Dillon 	if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
155*ba1276acSMatthew Dillon 	    NULL, NULL) || !DSA_generate_key(private)) {
156*ba1276acSMatthew Dillon 		DSA_free(private);
157*ba1276acSMatthew Dillon 		return SSH_ERR_LIBCRYPTO_ERROR;
158*ba1276acSMatthew Dillon 	}
159*ba1276acSMatthew Dillon 	k->dsa = private;
160*ba1276acSMatthew Dillon 	return 0;
161*ba1276acSMatthew Dillon }
162*ba1276acSMatthew Dillon 
163*ba1276acSMatthew Dillon static int
ssh_dss_copy_public(const struct sshkey * from,struct sshkey * to)164*ba1276acSMatthew Dillon ssh_dss_copy_public(const struct sshkey *from, struct sshkey *to)
165*ba1276acSMatthew Dillon {
166*ba1276acSMatthew Dillon 	const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
167*ba1276acSMatthew Dillon 	BIGNUM *dsa_p_dup = NULL, *dsa_q_dup = NULL, *dsa_g_dup = NULL;
168*ba1276acSMatthew Dillon 	BIGNUM *dsa_pub_key_dup = NULL;
169*ba1276acSMatthew Dillon 	int r = SSH_ERR_INTERNAL_ERROR;
170*ba1276acSMatthew Dillon 
171*ba1276acSMatthew Dillon 	DSA_get0_pqg(from->dsa, &dsa_p, &dsa_q, &dsa_g);
172*ba1276acSMatthew Dillon 	DSA_get0_key(from->dsa, &dsa_pub_key, NULL);
173*ba1276acSMatthew Dillon 	if ((dsa_p_dup = BN_dup(dsa_p)) == NULL ||
174*ba1276acSMatthew Dillon 	    (dsa_q_dup = BN_dup(dsa_q)) == NULL ||
175*ba1276acSMatthew Dillon 	    (dsa_g_dup = BN_dup(dsa_g)) == NULL ||
176*ba1276acSMatthew Dillon 	    (dsa_pub_key_dup = BN_dup(dsa_pub_key)) == NULL) {
177*ba1276acSMatthew Dillon 		r = SSH_ERR_ALLOC_FAIL;
178*ba1276acSMatthew Dillon 		goto out;
179*ba1276acSMatthew Dillon 	}
180*ba1276acSMatthew Dillon 	if (!DSA_set0_pqg(to->dsa, dsa_p_dup, dsa_q_dup, dsa_g_dup)) {
181*ba1276acSMatthew Dillon 		r = SSH_ERR_LIBCRYPTO_ERROR;
182*ba1276acSMatthew Dillon 		goto out;
183*ba1276acSMatthew Dillon 	}
184*ba1276acSMatthew Dillon 	dsa_p_dup = dsa_q_dup = dsa_g_dup = NULL; /* transferred */
185*ba1276acSMatthew Dillon 	if (!DSA_set0_key(to->dsa, dsa_pub_key_dup, NULL)) {
186*ba1276acSMatthew Dillon 		r = SSH_ERR_LIBCRYPTO_ERROR;
187*ba1276acSMatthew Dillon 		goto out;
188*ba1276acSMatthew Dillon 	}
189*ba1276acSMatthew Dillon 	dsa_pub_key_dup = NULL; /* transferred */
190*ba1276acSMatthew Dillon 	/* success */
191*ba1276acSMatthew Dillon 	r = 0;
192*ba1276acSMatthew Dillon  out:
193*ba1276acSMatthew Dillon 	BN_clear_free(dsa_p_dup);
194*ba1276acSMatthew Dillon 	BN_clear_free(dsa_q_dup);
195*ba1276acSMatthew Dillon 	BN_clear_free(dsa_g_dup);
196*ba1276acSMatthew Dillon 	BN_clear_free(dsa_pub_key_dup);
197*ba1276acSMatthew Dillon 	return r;
198*ba1276acSMatthew Dillon }
199*ba1276acSMatthew Dillon 
200*ba1276acSMatthew Dillon static int
ssh_dss_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)201*ba1276acSMatthew Dillon ssh_dss_deserialize_public(const char *ktype, struct sshbuf *b,
202*ba1276acSMatthew Dillon     struct sshkey *key)
203*ba1276acSMatthew Dillon {
204*ba1276acSMatthew Dillon 	int ret = SSH_ERR_INTERNAL_ERROR;
205*ba1276acSMatthew Dillon 	BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL, *dsa_pub_key = NULL;
206*ba1276acSMatthew Dillon 
207*ba1276acSMatthew Dillon 	if (sshbuf_get_bignum2(b, &dsa_p) != 0 ||
208*ba1276acSMatthew Dillon 	    sshbuf_get_bignum2(b, &dsa_q) != 0 ||
209*ba1276acSMatthew Dillon 	    sshbuf_get_bignum2(b, &dsa_g) != 0 ||
210*ba1276acSMatthew Dillon 	    sshbuf_get_bignum2(b, &dsa_pub_key) != 0) {
211*ba1276acSMatthew Dillon 		ret = SSH_ERR_INVALID_FORMAT;
212*ba1276acSMatthew Dillon 		goto out;
213*ba1276acSMatthew Dillon 	}
214*ba1276acSMatthew Dillon 	if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g)) {
215*ba1276acSMatthew Dillon 		ret = SSH_ERR_LIBCRYPTO_ERROR;
216*ba1276acSMatthew Dillon 		goto out;
217*ba1276acSMatthew Dillon 	}
218*ba1276acSMatthew Dillon 	dsa_p = dsa_q = dsa_g = NULL; /* transferred */
219*ba1276acSMatthew Dillon 	if (!DSA_set0_key(key->dsa, dsa_pub_key, NULL)) {
220*ba1276acSMatthew Dillon 		ret = SSH_ERR_LIBCRYPTO_ERROR;
221*ba1276acSMatthew Dillon 		goto out;
222*ba1276acSMatthew Dillon 	}
223*ba1276acSMatthew Dillon 	dsa_pub_key = NULL; /* transferred */
224*ba1276acSMatthew Dillon #ifdef DEBUG_PK
225*ba1276acSMatthew Dillon 	DSA_print_fp(stderr, key->dsa, 8);
226*ba1276acSMatthew Dillon #endif
227*ba1276acSMatthew Dillon 	/* success */
228*ba1276acSMatthew Dillon 	ret = 0;
229*ba1276acSMatthew Dillon  out:
230*ba1276acSMatthew Dillon 	BN_clear_free(dsa_p);
231*ba1276acSMatthew Dillon 	BN_clear_free(dsa_q);
232*ba1276acSMatthew Dillon 	BN_clear_free(dsa_g);
233*ba1276acSMatthew Dillon 	BN_clear_free(dsa_pub_key);
234*ba1276acSMatthew Dillon 	return ret;
235*ba1276acSMatthew Dillon }
236*ba1276acSMatthew Dillon 
237*ba1276acSMatthew Dillon static int
ssh_dss_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)238*ba1276acSMatthew Dillon ssh_dss_deserialize_private(const char *ktype, struct sshbuf *b,
239*ba1276acSMatthew Dillon     struct sshkey *key)
240*ba1276acSMatthew Dillon {
241*ba1276acSMatthew Dillon 	int r;
242*ba1276acSMatthew Dillon 	BIGNUM *dsa_priv_key = NULL;
243*ba1276acSMatthew Dillon 
244*ba1276acSMatthew Dillon 	if (!sshkey_is_cert(key)) {
245*ba1276acSMatthew Dillon 		if ((r = ssh_dss_deserialize_public(ktype, b, key)) != 0)
246*ba1276acSMatthew Dillon 			return r;
247*ba1276acSMatthew Dillon 	}
248*ba1276acSMatthew Dillon 
249*ba1276acSMatthew Dillon 	if ((r = sshbuf_get_bignum2(b, &dsa_priv_key)) != 0)
250*ba1276acSMatthew Dillon 		return r;
251*ba1276acSMatthew Dillon 	if (!DSA_set0_key(key->dsa, NULL, dsa_priv_key)) {
252*ba1276acSMatthew Dillon 		BN_clear_free(dsa_priv_key);
253*ba1276acSMatthew Dillon 		return SSH_ERR_LIBCRYPTO_ERROR;
254*ba1276acSMatthew Dillon 	}
255*ba1276acSMatthew Dillon 	return 0;
256*ba1276acSMatthew Dillon }
257*ba1276acSMatthew Dillon 
258*ba1276acSMatthew Dillon static int
ssh_dss_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat)259*ba1276acSMatthew Dillon ssh_dss_sign(struct sshkey *key,
260*ba1276acSMatthew Dillon     u_char **sigp, size_t *lenp,
261*ba1276acSMatthew Dillon     const u_char *data, size_t datalen,
262*ba1276acSMatthew Dillon     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
26318de8d7fSPeter Avalos {
26436e94dc5SPeter Avalos 	DSA_SIG *sig = NULL;
265664f4763Szrj 	const BIGNUM *sig_r, *sig_s;
26636e94dc5SPeter Avalos 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
26736e94dc5SPeter Avalos 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
26836e94dc5SPeter Avalos 	struct sshbuf *b = NULL;
26936e94dc5SPeter Avalos 	int ret = SSH_ERR_INVALID_ARGUMENT;
27018de8d7fSPeter Avalos 
27136e94dc5SPeter Avalos 	if (lenp != NULL)
27236e94dc5SPeter Avalos 		*lenp = 0;
27336e94dc5SPeter Avalos 	if (sigp != NULL)
27436e94dc5SPeter Avalos 		*sigp = NULL;
27518de8d7fSPeter Avalos 
27636e94dc5SPeter Avalos 	if (key == NULL || key->dsa == NULL ||
27736e94dc5SPeter Avalos 	    sshkey_type_plain(key->type) != KEY_DSA)
27836e94dc5SPeter Avalos 		return SSH_ERR_INVALID_ARGUMENT;
27936e94dc5SPeter Avalos 	if (dlen == 0)
28036e94dc5SPeter Avalos 		return SSH_ERR_INTERNAL_ERROR;
28118de8d7fSPeter Avalos 
28236e94dc5SPeter Avalos 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
28336e94dc5SPeter Avalos 	    digest, sizeof(digest))) != 0)
28436e94dc5SPeter Avalos 		goto out;
28536e94dc5SPeter Avalos 
28636e94dc5SPeter Avalos 	if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
28736e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
28836e94dc5SPeter Avalos 		goto out;
28918de8d7fSPeter Avalos 	}
29018de8d7fSPeter Avalos 
291664f4763Szrj 	DSA_SIG_get0(sig, &sig_r, &sig_s);
292664f4763Szrj 	rlen = BN_num_bytes(sig_r);
293664f4763Szrj 	slen = BN_num_bytes(sig_s);
29418de8d7fSPeter Avalos 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
29536e94dc5SPeter Avalos 		ret = SSH_ERR_INTERNAL_ERROR;
29636e94dc5SPeter Avalos 		goto out;
29718de8d7fSPeter Avalos 	}
29836e94dc5SPeter Avalos 	explicit_bzero(sigblob, SIGBLOB_LEN);
299664f4763Szrj 	BN_bn2bin(sig_r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
300664f4763Szrj 	BN_bn2bin(sig_s, sigblob + SIGBLOB_LEN - slen);
30118de8d7fSPeter Avalos 
30236e94dc5SPeter Avalos 	if ((b = sshbuf_new()) == NULL) {
30336e94dc5SPeter Avalos 		ret = SSH_ERR_ALLOC_FAIL;
30436e94dc5SPeter Avalos 		goto out;
30536e94dc5SPeter Avalos 	}
30636e94dc5SPeter Avalos 	if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
30736e94dc5SPeter Avalos 	    (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
30836e94dc5SPeter Avalos 		goto out;
309664f4763Szrj 
31036e94dc5SPeter Avalos 	len = sshbuf_len(b);
31136e94dc5SPeter Avalos 	if (sigp != NULL) {
31236e94dc5SPeter Avalos 		if ((*sigp = malloc(len)) == NULL) {
31336e94dc5SPeter Avalos 			ret = SSH_ERR_ALLOC_FAIL;
31436e94dc5SPeter Avalos 			goto out;
31536e94dc5SPeter Avalos 		}
31636e94dc5SPeter Avalos 		memcpy(*sigp, sshbuf_ptr(b), len);
31736e94dc5SPeter Avalos 	}
31818de8d7fSPeter Avalos 	if (lenp != NULL)
31918de8d7fSPeter Avalos 		*lenp = len;
32036e94dc5SPeter Avalos 	ret = 0;
32136e94dc5SPeter Avalos  out:
32236e94dc5SPeter Avalos 	explicit_bzero(digest, sizeof(digest));
32336e94dc5SPeter Avalos 	DSA_SIG_free(sig);
32436e94dc5SPeter Avalos 	sshbuf_free(b);
32536e94dc5SPeter Avalos 	return ret;
32618de8d7fSPeter Avalos }
32718de8d7fSPeter Avalos 
328*ba1276acSMatthew Dillon static int
ssh_dss_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t dlen,const char * alg,u_int compat,struct sshkey_sig_details ** detailsp)32936e94dc5SPeter Avalos ssh_dss_verify(const struct sshkey *key,
330*ba1276acSMatthew Dillon     const u_char *sig, size_t siglen,
331*ba1276acSMatthew Dillon     const u_char *data, size_t dlen, const char *alg, u_int compat,
332*ba1276acSMatthew Dillon     struct sshkey_sig_details **detailsp)
33336e94dc5SPeter Avalos {
334*ba1276acSMatthew Dillon 	DSA_SIG *dsig = NULL;
335664f4763Szrj 	BIGNUM *sig_r = NULL, *sig_s = NULL;
33636e94dc5SPeter Avalos 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
337*ba1276acSMatthew Dillon 	size_t len, hlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
33836e94dc5SPeter Avalos 	int ret = SSH_ERR_INTERNAL_ERROR;
33936e94dc5SPeter Avalos 	struct sshbuf *b = NULL;
34036e94dc5SPeter Avalos 	char *ktype = NULL;
34136e94dc5SPeter Avalos 
34236e94dc5SPeter Avalos 	if (key == NULL || key->dsa == NULL ||
343e9778795SPeter Avalos 	    sshkey_type_plain(key->type) != KEY_DSA ||
344*ba1276acSMatthew Dillon 	    sig == NULL || siglen == 0)
34536e94dc5SPeter Avalos 		return SSH_ERR_INVALID_ARGUMENT;
346*ba1276acSMatthew Dillon 	if (hlen == 0)
34736e94dc5SPeter Avalos 		return SSH_ERR_INTERNAL_ERROR;
34818de8d7fSPeter Avalos 
34918de8d7fSPeter Avalos 	/* fetch signature */
350*ba1276acSMatthew Dillon 	if ((b = sshbuf_from(sig, siglen)) == NULL)
35136e94dc5SPeter Avalos 		return SSH_ERR_ALLOC_FAIL;
35236e94dc5SPeter Avalos 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
35336e94dc5SPeter Avalos 	    sshbuf_get_string(b, &sigblob, &len) != 0) {
35436e94dc5SPeter Avalos 		ret = SSH_ERR_INVALID_FORMAT;
35536e94dc5SPeter Avalos 		goto out;
35618de8d7fSPeter Avalos 	}
35736e94dc5SPeter Avalos 	if (strcmp("ssh-dss", ktype) != 0) {
35836e94dc5SPeter Avalos 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
35936e94dc5SPeter Avalos 		goto out;
36036e94dc5SPeter Avalos 	}
36136e94dc5SPeter Avalos 	if (sshbuf_len(b) != 0) {
36236e94dc5SPeter Avalos 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
36336e94dc5SPeter Avalos 		goto out;
36418de8d7fSPeter Avalos 	}
36518de8d7fSPeter Avalos 
36618de8d7fSPeter Avalos 	if (len != SIGBLOB_LEN) {
36736e94dc5SPeter Avalos 		ret = SSH_ERR_INVALID_FORMAT;
36836e94dc5SPeter Avalos 		goto out;
36918de8d7fSPeter Avalos 	}
37018de8d7fSPeter Avalos 
37118de8d7fSPeter Avalos 	/* parse signature */
372*ba1276acSMatthew Dillon 	if ((dsig = DSA_SIG_new()) == NULL ||
373664f4763Szrj 	    (sig_r = BN_new()) == NULL ||
374664f4763Szrj 	    (sig_s = BN_new()) == NULL) {
37536e94dc5SPeter Avalos 		ret = SSH_ERR_ALLOC_FAIL;
37636e94dc5SPeter Avalos 		goto out;
37736e94dc5SPeter Avalos 	}
378664f4763Szrj 	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig_r) == NULL) ||
379664f4763Szrj 	    (BN_bin2bn(sigblob + INTBLOB_LEN, INTBLOB_LEN, sig_s) == NULL)) {
38036e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
38136e94dc5SPeter Avalos 		goto out;
38236e94dc5SPeter Avalos 	}
383*ba1276acSMatthew Dillon 	if (!DSA_SIG_set0(dsig, sig_r, sig_s)) {
384664f4763Szrj 		ret = SSH_ERR_LIBCRYPTO_ERROR;
385664f4763Szrj 		goto out;
386664f4763Szrj 	}
387664f4763Szrj 	sig_r = sig_s = NULL; /* transferred */
38818de8d7fSPeter Avalos 
38918de8d7fSPeter Avalos 	/* sha1 the data */
390*ba1276acSMatthew Dillon 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, dlen,
39136e94dc5SPeter Avalos 	    digest, sizeof(digest))) != 0)
39236e94dc5SPeter Avalos 		goto out;
39318de8d7fSPeter Avalos 
394*ba1276acSMatthew Dillon 	switch (DSA_do_verify(digest, hlen, dsig, key->dsa)) {
39536e94dc5SPeter Avalos 	case 1:
39636e94dc5SPeter Avalos 		ret = 0;
39736e94dc5SPeter Avalos 		break;
39836e94dc5SPeter Avalos 	case 0:
39936e94dc5SPeter Avalos 		ret = SSH_ERR_SIGNATURE_INVALID;
40036e94dc5SPeter Avalos 		goto out;
40136e94dc5SPeter Avalos 	default:
40236e94dc5SPeter Avalos 		ret = SSH_ERR_LIBCRYPTO_ERROR;
40336e94dc5SPeter Avalos 		goto out;
40436e94dc5SPeter Avalos 	}
40518de8d7fSPeter Avalos 
40636e94dc5SPeter Avalos  out:
40736e94dc5SPeter Avalos 	explicit_bzero(digest, sizeof(digest));
408*ba1276acSMatthew Dillon 	DSA_SIG_free(dsig);
409664f4763Szrj 	BN_clear_free(sig_r);
410664f4763Szrj 	BN_clear_free(sig_s);
41136e94dc5SPeter Avalos 	sshbuf_free(b);
41236e94dc5SPeter Avalos 	free(ktype);
4130cbfa66cSDaniel Fojt 	if (sigblob != NULL)
4140cbfa66cSDaniel Fojt 		freezero(sigblob, len);
41518de8d7fSPeter Avalos 	return ret;
41618de8d7fSPeter Avalos }
417*ba1276acSMatthew Dillon 
418*ba1276acSMatthew Dillon static const struct sshkey_impl_funcs sshkey_dss_funcs = {
419*ba1276acSMatthew Dillon 	/* .size = */		ssh_dss_size,
420*ba1276acSMatthew Dillon 	/* .alloc = */		ssh_dss_alloc,
421*ba1276acSMatthew Dillon 	/* .cleanup = */	ssh_dss_cleanup,
422*ba1276acSMatthew Dillon 	/* .equal = */		ssh_dss_equal,
423*ba1276acSMatthew Dillon 	/* .ssh_serialize_public = */ ssh_dss_serialize_public,
424*ba1276acSMatthew Dillon 	/* .ssh_deserialize_public = */ ssh_dss_deserialize_public,
425*ba1276acSMatthew Dillon 	/* .ssh_serialize_private = */ ssh_dss_serialize_private,
426*ba1276acSMatthew Dillon 	/* .ssh_deserialize_private = */ ssh_dss_deserialize_private,
427*ba1276acSMatthew Dillon 	/* .generate = */	ssh_dss_generate,
428*ba1276acSMatthew Dillon 	/* .copy_public = */	ssh_dss_copy_public,
429*ba1276acSMatthew Dillon 	/* .sign = */		ssh_dss_sign,
430*ba1276acSMatthew Dillon 	/* .verify = */		ssh_dss_verify,
431*ba1276acSMatthew Dillon };
432*ba1276acSMatthew Dillon 
433*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_dss_impl = {
434*ba1276acSMatthew Dillon 	/* .name = */		"ssh-dss",
435*ba1276acSMatthew Dillon 	/* .shortname = */	"DSA",
436*ba1276acSMatthew Dillon 	/* .sigalg = */		NULL,
437*ba1276acSMatthew Dillon 	/* .type = */		KEY_DSA,
438*ba1276acSMatthew Dillon 	/* .nid = */		0,
439*ba1276acSMatthew Dillon 	/* .cert = */		0,
440*ba1276acSMatthew Dillon 	/* .sigonly = */	0,
441*ba1276acSMatthew Dillon 	/* .keybits = */	0,
442*ba1276acSMatthew Dillon 	/* .funcs = */		&sshkey_dss_funcs,
443*ba1276acSMatthew Dillon };
444*ba1276acSMatthew Dillon 
445*ba1276acSMatthew Dillon const struct sshkey_impl sshkey_dsa_cert_impl = {
446*ba1276acSMatthew Dillon 	/* .name = */		"ssh-dss-cert-v01@openssh.com",
447*ba1276acSMatthew Dillon 	/* .shortname = */	"DSA-CERT",
448*ba1276acSMatthew Dillon 	/* .sigalg = */		NULL,
449*ba1276acSMatthew Dillon 	/* .type = */		KEY_DSA_CERT,
450*ba1276acSMatthew Dillon 	/* .nid = */		0,
451*ba1276acSMatthew Dillon 	/* .cert = */		1,
452*ba1276acSMatthew Dillon 	/* .sigonly = */	0,
453*ba1276acSMatthew Dillon 	/* .keybits = */	0,
454*ba1276acSMatthew Dillon 	/* .funcs = */		&sshkey_dss_funcs,
455*ba1276acSMatthew Dillon };
456*ba1276acSMatthew Dillon 
457*ba1276acSMatthew Dillon #endif /* WITH_OPENSSL && WITH_DSA */
458