OpenCoverage

ssh-ecdsa.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/ssh-ecdsa.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: ssh-ecdsa.c,v 1.14 2018/02/07 02:06:51 jsing Exp $ */-
2/*-
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.-
4 * Copyright (c) 2010 Damien Miller. 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-
29#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)-
30-
31#include <sys/types.h>-
32-
33#include <openssl/bn.h>-
34#include <openssl/ec.h>-
35#include <openssl/ecdsa.h>-
36#include <openssl/evp.h>-
37-
38#include <string.h>-
39-
40#include "sshbuf.h"-
41#include "ssherr.h"-
42#include "digest.h"-
43#define SSHKEY_INTERNAL-
44#include "sshkey.h"-
45-
46#include "openbsd-compat/openssl-compat.h"-
47-
48/* ARGSUSED */-
49int-
50ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,-
51 const u_char *data, size_t datalen, u_int compat)-
52{-
53 ECDSA_SIG *sig = NULL;-
54 const BIGNUM *sig_r, *sig_s;-
55 int hash_alg;-
56 u_char digest[SSH_DIGEST_MAX_LENGTH];-
57 size_t len, dlen;-
58 struct sshbuf *b = NULL, *bb = NULL;-
59 int ret = SSH_ERR_INTERNAL_ERROR;-
60-
61 if (lenp != NULL)
lenp != ((void *)0)Description
TRUEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
FALSEnever evaluated
0-64
62 *lenp = 0;
executed 64 times by 2 tests: *lenp = 0;
Executed by:
  • test_kex
  • test_sshkey
64
63 if (sigp != NULL)
sigp != ((void *)0)Description
TRUEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
FALSEnever evaluated
0-64
64 *sigp = NULL;
executed 64 times by 2 tests: *sigp = ((void *)0) ;
Executed by:
  • test_kex
  • test_sshkey
64
65-
66 if (key == NULL || key->ecdsa == NULL ||
key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
key->ecdsa == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
67 sshkey_type_plain(key->type) != KEY_ECDSA)
sshkey_type_pl...) != KEY_ECDSADescription
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
68 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
69-
70 if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
(hash_alg = ss...sa_nid)) == -1Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
71 (dlen = ssh_digest_bytes(hash_alg)) == 0)
(dlen = ssh_di...ash_alg)) == 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
72 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
73 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
(ret = ssh_dig...digest))) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
74 digest, sizeof(digest))) != 0)
(ret = ssh_dig...digest))) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
75 goto out;
never executed: goto out;
0
76-
77 if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
(sig = ECDSA_d...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
78 ret = SSH_ERR_LIBCRYPTO_ERROR;-
79 goto out;
never executed: goto out;
0
80 }-
81-
82 if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
(bb = sshbuf_n...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
(b = sshbuf_ne...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
83 ret = SSH_ERR_ALLOC_FAIL;-
84 goto out;
never executed: goto out;
0
85 }-
86 ECDSA_SIG_get0(sig, &sig_r, &sig_s);-
87 if ((ret = sshbuf_put_bignum2(bb, sig_r)) != 0 ||
(ret = sshbuf_..., sig_r)) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
88 (ret = sshbuf_put_bignum2(bb, sig_s)) != 0)
(ret = sshbuf_..., sig_s)) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
89 goto out;
never executed: goto out;
0
90 if ((ret = sshbuf_put_cstring(b, sshkey_ssh_name_plain(key))) != 0 ||
(ret = sshbuf_...in(key))) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
91 (ret = sshbuf_put_stringb(b, bb)) != 0)
(ret = sshbuf_...b(b, bb)) != 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
92 goto out;
never executed: goto out;
0
93 len = sshbuf_len(b);-
94 if (sigp != NULL) {
sigp != ((void *)0)Description
TRUEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
FALSEnever evaluated
0-64
95 if ((*sigp = malloc(len)) == NULL) {
(*sigp = mallo...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-64
96 ret = SSH_ERR_ALLOC_FAIL;-
97 goto out;
never executed: goto out;
0
98 }-
99 memcpy(*sigp, sshbuf_ptr(b), len);-
100 }
executed 64 times by 2 tests: end of block
Executed by:
  • test_kex
  • test_sshkey
64
101 if (lenp != NULL)
lenp != ((void *)0)Description
TRUEevaluated 64 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
FALSEnever evaluated
0-64
102 *lenp = len;
executed 64 times by 2 tests: *lenp = len;
Executed by:
  • test_kex
  • test_sshkey
64
103 ret = 0;-
104 out:
code before this statement executed 64 times by 2 tests: out:
Executed by:
  • test_kex
  • test_sshkey
64
105 explicit_bzero(digest, sizeof(digest));-
106 sshbuf_free(b);-
107 sshbuf_free(bb);-
108 ECDSA_SIG_free(sig);-
109 return ret;
executed 64 times by 2 tests: return ret;
Executed by:
  • test_kex
  • test_sshkey
64
110}-
111-
112/* ARGSUSED */-
113int-
114ssh_ecdsa_verify(const struct sshkey *key,-
115 const u_char *signature, size_t signaturelen,-
116 const u_char *data, size_t datalen, u_int compat)-
117{-
118 ECDSA_SIG *sig = NULL;-
119 BIGNUM *sig_r = NULL, *sig_s = NULL;-
120 int hash_alg;-
121 u_char digest[SSH_DIGEST_MAX_LENGTH];-
122 size_t dlen;-
123 int ret = SSH_ERR_INTERNAL_ERROR;-
124 struct sshbuf *b = NULL, *sigbuf = NULL;-
125 char *ktype = NULL;-
126-
127 if (key == NULL || key->ecdsa == NULL ||
key == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
key->ecdsa == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
128 sshkey_type_plain(key->type) != KEY_ECDSA ||
sshkey_type_pl...) != KEY_ECDSADescription
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
129 signature == NULL || signaturelen == 0)
signature == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
signaturelen == 0Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
130 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
131-
132 if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
(hash_alg = ss...sa_nid)) == -1Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
133 (dlen = ssh_digest_bytes(hash_alg)) == 0)
(dlen = ssh_di...ash_alg)) == 0Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
134 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
135-
136 /* fetch signature */-
137 if ((b = sshbuf_from(signature, signaturelen)) == NULL)
(b = sshbuf_fr...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 12662 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-12662
138 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
139 if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
sshbuf_get_cst...id *)0) ) != 0Description
TRUEevaluated 966 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 11696 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
966-11696
140 sshbuf_froms(b, &sigbuf) != 0) {
sshbuf_froms(b, &sigbuf) != 0Description
TRUEevaluated 901 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 10795 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
901-10795
141 ret = SSH_ERR_INVALID_FORMAT;-
142 goto out;
executed 1867 times by 1 test: goto out;
Executed by:
  • test_sshkey
1867
143 }-
144 if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
never executed: __result = (((const unsigned char *) (const char *) ( sshkey_ssh_name_plain(key) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( ktype ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
__extension__ ... )))); }) != 0Description
TRUEevaluated 3481 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 7314 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-7314
145 ret = SSH_ERR_KEY_TYPE_MISMATCH;-
146 goto out;
executed 3481 times by 1 test: goto out;
Executed by:
  • test_sshkey
3481
147 }-
148 if (sshbuf_len(b) != 0) {
sshbuf_len(b) != 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 7308 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
6-7308
149 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;-
150 goto out;
executed 6 times by 1 test: goto out;
Executed by:
  • test_sshkey
6
151 }-
152-
153 /* parse signature */-
154 if ((sig = ECDSA_SIG_new()) == NULL ||
(sig = ECDSA_S...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 7308 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-7308
155 (sig_r = BN_new()) == NULL ||
(sig_r = BN_ne...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 7308 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-7308
156 (sig_s = BN_new()) == NULL) {
(sig_s = BN_ne...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 7308 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-7308
157 ret = SSH_ERR_ALLOC_FAIL;-
158 goto out;
never executed: goto out;
0
159 }-
160 if (sshbuf_get_bignum2(sigbuf, sig_r) != 0 ||
sshbuf_get_big...f, sig_r) != 0Description
TRUEevaluated 764 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 6544 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
764-6544
161 sshbuf_get_bignum2(sigbuf, sig_s) != 0) {
sshbuf_get_big...f, sig_s) != 0Description
TRUEevaluated 735 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 5809 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
735-5809
162 ret = SSH_ERR_INVALID_FORMAT;-
163 goto out;
executed 1499 times by 1 test: goto out;
Executed by:
  • test_sshkey
1499
164 }-
165 if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
!ECDSA_SIG_set... sig_r, sig_s)Description
TRUEnever evaluated
FALSEevaluated 5809 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-5809
166 ret = SSH_ERR_LIBCRYPTO_ERROR;-
167 goto out;
never executed: goto out;
0
168 }-
169 sig_r = sig_s = NULL; /* transferred */-
170-
171 if (sshbuf_len(sigbuf) != 0) {
sshbuf_len(sigbuf) != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 5806 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
3-5806
172 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;-
173 goto out;
executed 3 times by 1 test: goto out;
Executed by:
  • test_sshkey
3
174 }-
175 if ((ret = ssh_digest_memory(hash_alg, data, datalen,
(ret = ssh_dig...digest))) != 0Description
TRUEnever evaluated
FALSEevaluated 5806 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-5806
176 digest, sizeof(digest))) != 0)
(ret = ssh_dig...digest))) != 0Description
TRUEnever evaluated
FALSEevaluated 5806 times by 2 tests
Evaluated by:
  • test_kex
  • test_sshkey
0-5806
177 goto out;
never executed: goto out;
0
178-
179 switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {-
180 case 1:
executed 67 times by 2 tests: case 1:
Executed by:
  • test_kex
  • test_sshkey
67
181 ret = 0;-
182 break;
executed 67 times by 2 tests: break;
Executed by:
  • test_kex
  • test_sshkey
67
183 case 0:
executed 5739 times by 1 test: case 0:
Executed by:
  • test_sshkey
5739
184 ret = SSH_ERR_SIGNATURE_INVALID;-
185 goto out;
executed 5739 times by 1 test: goto out;
Executed by:
  • test_sshkey
5739
186 default:
never executed: default:
0
187 ret = SSH_ERR_LIBCRYPTO_ERROR;-
188 goto out;
never executed: goto out;
0
189 }-
190-
191 out:
code before this statement executed 67 times by 2 tests: out:
Executed by:
  • test_kex
  • test_sshkey
67
192 explicit_bzero(digest, sizeof(digest));-
193 sshbuf_free(sigbuf);-
194 sshbuf_free(b);-
195 ECDSA_SIG_free(sig);-
196 BN_clear_free(sig_r);-
197 BN_clear_free(sig_s);-
198 free(ktype);-
199 return ret;
executed 12662 times by 2 tests: return ret;
Executed by:
  • test_kex
  • test_sshkey
12662
200}-
201-
202#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2