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