xref: /freebsd/crypto/openssh/ssh-dss.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /* $OpenBSD: ssh-dss.c,v 1.34 2015/12/11 04:21:12 mmcc 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
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 	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
55 	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
56 	struct sshbuf *b = NULL;
57 	int ret = SSH_ERR_INVALID_ARGUMENT;
58 
59 	if (lenp != NULL)
60 		*lenp = 0;
61 	if (sigp != NULL)
62 		*sigp = NULL;
63 
64 	if (key == NULL || key->dsa == NULL ||
65 	    sshkey_type_plain(key->type) != KEY_DSA)
66 		return SSH_ERR_INVALID_ARGUMENT;
67 	if (dlen == 0)
68 		return SSH_ERR_INTERNAL_ERROR;
69 
70 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
71 	    digest, sizeof(digest))) != 0)
72 		goto out;
73 
74 	if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
75 		ret = SSH_ERR_LIBCRYPTO_ERROR;
76 		goto out;
77 	}
78 
79 	rlen = BN_num_bytes(sig->r);
80 	slen = BN_num_bytes(sig->s);
81 	if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
82 		ret = SSH_ERR_INTERNAL_ERROR;
83 		goto out;
84 	}
85 	explicit_bzero(sigblob, SIGBLOB_LEN);
86 	BN_bn2bin(sig->r, sigblob + SIGBLOB_LEN - INTBLOB_LEN - rlen);
87 	BN_bn2bin(sig->s, sigblob + SIGBLOB_LEN - slen);
88 
89 	if (compat & SSH_BUG_SIGBLOB) {
90 		if (sigp != NULL) {
91 			if ((*sigp = malloc(SIGBLOB_LEN)) == NULL) {
92 				ret = SSH_ERR_ALLOC_FAIL;
93 				goto out;
94 			}
95 			memcpy(*sigp, sigblob, SIGBLOB_LEN);
96 		}
97 		if (lenp != NULL)
98 			*lenp = SIGBLOB_LEN;
99 		ret = 0;
100 	} else {
101 		/* ietf-drafts */
102 		if ((b = sshbuf_new()) == NULL) {
103 			ret = SSH_ERR_ALLOC_FAIL;
104 			goto out;
105 		}
106 		if ((ret = sshbuf_put_cstring(b, "ssh-dss")) != 0 ||
107 		    (ret = sshbuf_put_string(b, sigblob, SIGBLOB_LEN)) != 0)
108 			goto out;
109 		len = sshbuf_len(b);
110 		if (sigp != NULL) {
111 			if ((*sigp = malloc(len)) == NULL) {
112 				ret = SSH_ERR_ALLOC_FAIL;
113 				goto out;
114 			}
115 			memcpy(*sigp, sshbuf_ptr(b), len);
116 		}
117 		if (lenp != NULL)
118 			*lenp = len;
119 		ret = 0;
120 	}
121  out:
122 	explicit_bzero(digest, sizeof(digest));
123 	if (sig != NULL)
124 		DSA_SIG_free(sig);
125 	sshbuf_free(b);
126 	return ret;
127 }
128 
129 int
130 ssh_dss_verify(const struct sshkey *key,
131     const u_char *signature, size_t signaturelen,
132     const u_char *data, size_t datalen, u_int compat)
133 {
134 	DSA_SIG *sig = NULL;
135 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
136 	size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
137 	int ret = SSH_ERR_INTERNAL_ERROR;
138 	struct sshbuf *b = NULL;
139 	char *ktype = NULL;
140 
141 	if (key == NULL || key->dsa == NULL ||
142 	    sshkey_type_plain(key->type) != KEY_DSA)
143 		return SSH_ERR_INVALID_ARGUMENT;
144 	if (dlen == 0)
145 		return SSH_ERR_INTERNAL_ERROR;
146 
147 	/* fetch signature */
148 	if (compat & SSH_BUG_SIGBLOB) {
149 		if ((sigblob = malloc(signaturelen)) == NULL)
150 			return SSH_ERR_ALLOC_FAIL;
151 		memcpy(sigblob, signature, signaturelen);
152 		len = signaturelen;
153 	} else {
154 		/* ietf-drafts */
155 		if ((b = sshbuf_from(signature, signaturelen)) == NULL)
156 			return SSH_ERR_ALLOC_FAIL;
157 		if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
158 		    sshbuf_get_string(b, &sigblob, &len) != 0) {
159 			ret = SSH_ERR_INVALID_FORMAT;
160 			goto out;
161 		}
162 		if (strcmp("ssh-dss", ktype) != 0) {
163 			ret = SSH_ERR_KEY_TYPE_MISMATCH;
164 			goto out;
165 		}
166 		if (sshbuf_len(b) != 0) {
167 			ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
168 			goto out;
169 		}
170 	}
171 
172 	if (len != SIGBLOB_LEN) {
173 		ret = SSH_ERR_INVALID_FORMAT;
174 		goto out;
175 	}
176 
177 	/* parse signature */
178 	if ((sig = DSA_SIG_new()) == NULL ||
179 	    (sig->r = BN_new()) == NULL ||
180 	    (sig->s = BN_new()) == NULL) {
181 		ret = SSH_ERR_ALLOC_FAIL;
182 		goto out;
183 	}
184 	if ((BN_bin2bn(sigblob, INTBLOB_LEN, sig->r) == NULL) ||
185 	    (BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s) == NULL)) {
186 		ret = SSH_ERR_LIBCRYPTO_ERROR;
187 		goto out;
188 	}
189 
190 	/* sha1 the data */
191 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
192 	    digest, sizeof(digest))) != 0)
193 		goto out;
194 
195 	switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
196 	case 1:
197 		ret = 0;
198 		break;
199 	case 0:
200 		ret = SSH_ERR_SIGNATURE_INVALID;
201 		goto out;
202 	default:
203 		ret = SSH_ERR_LIBCRYPTO_ERROR;
204 		goto out;
205 	}
206 
207  out:
208 	explicit_bzero(digest, sizeof(digest));
209 	if (sig != NULL)
210 		DSA_SIG_free(sig);
211 	sshbuf_free(b);
212 	free(ktype);
213 	if (sigblob != NULL) {
214 		explicit_bzero(sigblob, len);
215 		free(sigblob);
216 	}
217 	return ret;
218 }
219 #endif /* WITH_OPENSSL */
220