OpenCoverage

sshbuf-getput-crypto.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/sshbuf-getput-crypto.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: sshbuf-getput-crypto.c,v 1.5 2016/01/12 23:42:54 djm Exp $ */-
2/*-
3 * Copyright (c) 2011 Damien Miller-
4 *-
5 * Permission to use, copy, modify, and distribute this software for any-
6 * purpose with or without fee is hereby granted, provided that the above-
7 * copyright notice and this permission notice appear in all copies.-
8 *-
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES-
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF-
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR-
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES-
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN-
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF-
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.-
16 */-
17-
18#define SSHBUF_INTERNAL-
19#include "includes.h"-
20-
21#include <sys/types.h>-
22#include <stdlib.h>-
23#include <stdio.h>-
24#include <string.h>-
25-
26#include <openssl/bn.h>-
27#ifdef OPENSSL_HAS_ECC-
28# include <openssl/ec.h>-
29#endif /* OPENSSL_HAS_ECC */-
30-
31#include "ssherr.h"-
32#include "sshbuf.h"-
33-
34int-
35sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v)-
36{-
37 const u_char *d;-
38 size_t len;-
39 int r;-
40-
41 if ((r = sshbuf_get_bignum2_bytes_direct(buf, &d, &len)) != 0)
(r = sshbuf_ge...d, &len)) != 0Description
TRUEevaluated 1242461 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
FALSEevaluated 64784 times by 6 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
64784-1242461
42 return r;
executed 1242461 times by 2 tests: return r;
Executed by:
  • test_sshbuf
  • test_sshkey
1242461
43 if (v != NULL && BN_bin2bn(d, len, v) == NULL)
v != ((void *)0)Description
TRUEevaluated 64784 times by 6 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
FALSEnever evaluated
BN_bin2bn(d, l...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 64784 times by 6 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
0-64784
44 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
45 return 0;
executed 64784 times by 6 tests: return 0;
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
64784
46}-
47-
48int-
49sshbuf_get_bignum1(struct sshbuf *buf, BIGNUM *v)-
50{-
51 const u_char *d = sshbuf_ptr(buf);-
52 u_int16_t len_bits;-
53 size_t len_bytes;-
54-
55 /* Length in bits */-
56 if (sshbuf_len(buf) < 2)
sshbuf_len(buf) < 2Description
TRUEnever evaluated
FALSEevaluated 1239382 times by 1 test
Evaluated by:
  • test_sshbuf
0-1239382
57 return SSH_ERR_MESSAGE_INCOMPLETE;
never executed: return -3;
0
58 len_bits = PEEK_U16(d);-
59 len_bytes = (len_bits + 7) >> 3;-
60 if (len_bytes > SSHBUF_MAX_BIGNUM)
len_bytes > (16384 / 8)Description
TRUEevaluated 1239378 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
4-1239378
61 return SSH_ERR_BIGNUM_TOO_LARGE;
executed 1239378 times by 1 test: return -7;
Executed by:
  • test_sshbuf
1239378
62 if (sshbuf_len(buf) < 2 + len_bytes)
sshbuf_len(buf... 2 + len_bytesDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
2
63 return SSH_ERR_MESSAGE_INCOMPLETE;
executed 2 times by 1 test: return -3;
Executed by:
  • test_sshbuf
2
64 if (v != NULL && BN_bin2bn(d + 2, len_bytes, v) == NULL)
v != ((void *)0)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEnever evaluated
BN_bin2bn(d + ...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
0-2
65 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
66 if (sshbuf_consume(buf, 2 + len_bytes) != 0) {
sshbuf_consume...en_bytes) != 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
0-2
67 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));-
68 SSHBUF_ABORT();-
69 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
70 }-
71 return 0;
executed 2 times by 1 test: return 0;
Executed by:
  • test_sshbuf
2
72}-
73-
74#ifdef OPENSSL_HAS_ECC-
75static int-
76get_ec(const u_char *d, size_t len, EC_POINT *v, const EC_GROUP *g)-
77{-
78 /* Refuse overlong bignums */-
79 if (len == 0 || len > SSHBUF_MAX_ECPOINT)
len == 0Description
TRUEnever evaluated
FALSEevaluated 6788 times by 5 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
len > ((528 * 2 / 8) + 1)Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 6785 times by 5 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
0-6788
80 return SSH_ERR_ECPOINT_TOO_LARGE;
executed 3 times by 1 test: return -8;
Executed by:
  • test_sshkey
3
81 /* Only handle uncompressed points */-
82 if (*d != POINT_CONVERSION_UNCOMPRESSED)
*d != POINT_CO...N_UNCOMPRESSEDDescription
TRUEevaluated 27 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 6758 times by 5 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
27-6758
83 return SSH_ERR_INVALID_FORMAT;
executed 27 times by 1 test: return -4;
Executed by:
  • test_sshkey
27
84 if (v != NULL && EC_POINT_oct2point(g, v, d, len, NULL) != 1)
v != ((void *)0)Description
TRUEevaluated 6758 times by 5 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
FALSEnever evaluated
EC_POINT_oct2p...id *)0) ) != 1Description
TRUEevaluated 1739 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 5019 times by 5 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
0-6758
85 return SSH_ERR_INVALID_FORMAT; /* XXX assumption */
executed 1739 times by 1 test: return -4;
Executed by:
  • test_sshkey
1739
86 return 0;
executed 5019 times by 5 tests: return 0;
Executed by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshbuf
  • test_sshkey
5019
87}-
88-
89int-
90sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g)-
91{-
92 const u_char *d;-
93 size_t len;-
94 int r;-
95-
96 if ((r = sshbuf_peek_string_direct(buf, &d, &len)) < 0)
(r = sshbuf_pe...&d, &len)) < 0Description
TRUEevaluated 235 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 6786 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshkey
235-6786
97 return r;
executed 235 times by 1 test: return r;
Executed by:
  • test_sshkey
235
98 if ((r = get_ec(d, len, v, g)) != 0)
(r = get_ec(d,...n, v, g)) != 0Description
TRUEevaluated 1769 times by 1 test
Evaluated by:
  • test_sshkey
FALSEevaluated 5017 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshkey
1769-5017
99 return r;
executed 1769 times by 1 test: return r;
Executed by:
  • test_sshkey
1769
100 /* Skip string */-
101 if (sshbuf_get_string_direct(buf, NULL, NULL) != 0) {
sshbuf_get_str...id *)0) ) != 0Description
TRUEnever evaluated
FALSEevaluated 5017 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshkey
0-5017
102 /* Shouldn't happen */-
103 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));-
104 SSHBUF_ABORT();-
105 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
106 }-
107 return 0;
executed 5017 times by 4 tests: return 0;
Executed by:
  • ssh-keygen
  • test_hostkeys
  • test_kex
  • test_sshkey
5017
108}-
109-
110int-
111sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v)-
112{-
113 EC_POINT *pt = EC_POINT_new(EC_KEY_get0_group(v));-
114 int r;-
115 const u_char *d;-
116 size_t len;-
117-
118 if (pt == NULL) {
pt == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 1239379 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
0-1239379
119 SSHBUF_DBG(("SSH_ERR_ALLOC_FAIL"));-
120 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
121 }-
122 if ((r = sshbuf_peek_string_direct(buf, &d, &len)) < 0) {
(r = sshbuf_pe...&d, &len)) < 0Description
TRUEevaluated 1239377 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 2 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
2-1239377
123 EC_POINT_free(pt);-
124 return r;
executed 1239377 times by 1 test: return r;
Executed by:
  • test_sshbuf
1239377
125 }-
126 if ((r = get_ec(d, len, pt, EC_KEY_get0_group(v))) != 0) {
(r = get_ec(d,...roup(v))) != 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
0-2
127 EC_POINT_free(pt);-
128 return r;
never executed: return r;
0
129 }-
130 if (EC_KEY_set_public_key(v, pt) != 1) {
EC_KEY_set_pub...ey(v, pt) != 1Description
TRUEnever evaluated
FALSEevaluated 2 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
0-2
131 EC_POINT_free(pt);-
132 return SSH_ERR_ALLOC_FAIL; /* XXX assumption */
never executed: return -2;
0
133 }-
134 EC_POINT_free(pt);-
135 /* Skip string */-
136 if (sshbuf_get_string_direct(buf, NULL, NULL) != 0) {
sshbuf_get_str...id *)0) ) != 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 2 tests
Evaluated by:
  • test_sshbuf
  • test_sshkey
0-2
137 /* Shouldn't happen */-
138 SSHBUF_DBG(("SSH_ERR_INTERNAL_ERROR"));-
139 SSHBUF_ABORT();-
140 return SSH_ERR_INTERNAL_ERROR;
never executed: return -1;
0
141 }-
142 return 0;
executed 2 times by 2 tests: return 0;
Executed by:
  • test_sshbuf
  • test_sshkey
2
143}-
144#endif /* OPENSSL_HAS_ECC */-
145-
146int-
147sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v)-
148{-
149 u_char d[SSHBUF_MAX_BIGNUM + 1];-
150 int len = BN_num_bytes(v), prepend = 0, r;-
151-
152 if (len < 0 || len > SSHBUF_MAX_BIGNUM)
len < 0Description
TRUEnever evaluated
FALSEevaluated 1778 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
len > (16384 / 8)Description
TRUEnever evaluated
FALSEevaluated 1778 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
0-1778
153 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
154 *d = '\0';-
155 if (BN_bn2bin(v, d + 1) != len)
BN_bn2bin(v, d + 1) != lenDescription
TRUEnever evaluated
FALSEevaluated 1778 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
0-1778
156 return SSH_ERR_INTERNAL_ERROR; /* Shouldn't happen */
never executed: return -1;
0
157 /* If MSB is set, prepend a \0 */-
158 if (len > 0 && (d[1] & 0x80) != 0)
len > 0Description
TRUEevaluated 1778 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
FALSEnever evaluated
(d[1] & 0x80) != 0Description
TRUEevaluated 957 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
FALSEevaluated 821 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
0-1778
159 prepend = 1;
executed 957 times by 5 tests: prepend = 1;
Executed by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
957
160 if ((r = sshbuf_put_string(buf, d + 1 - prepend, len + prepend)) < 0) {
(r = sshbuf_pu... prepend)) < 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 1776 times by 5 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
2-1776
161 explicit_bzero(d, sizeof(d));-
162 return r;
executed 2 times by 1 test: return r;
Executed by:
  • test_sshbuf
2
163 }-
164 explicit_bzero(d, sizeof(d));-
165 return 0;
executed 1776 times by 5 tests: return 0;
Executed by:
  • ssh-keygen
  • sshd
  • test_kex
  • test_sshbuf
  • test_sshkey
1776
166}-
167-
168int-
169sshbuf_put_bignum1(struct sshbuf *buf, const BIGNUM *v)-
170{-
171 int r, len_bits = BN_num_bits(v);-
172 size_t len_bytes = (len_bits + 7) / 8;-
173 u_char d[SSHBUF_MAX_BIGNUM], *dp;-
174-
175 if (len_bits < 0 || len_bytes > SSHBUF_MAX_BIGNUM)
len_bits < 0Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
len_bytes > (16384 / 8)Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
0-4
176 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
177 if (BN_bn2bin(v, d) != (int)len_bytes)
BN_bn2bin(v, d...(int)len_bytesDescription
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • test_sshbuf
0-4
178 return SSH_ERR_INTERNAL_ERROR; /* Shouldn't happen */
never executed: return -1;
0
179 if ((r = sshbuf_reserve(buf, len_bytes + 2, &dp)) < 0) {
(r = sshbuf_re...+ 2, &dp)) < 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
2
180 explicit_bzero(d, sizeof(d));-
181 return r;
executed 2 times by 1 test: return r;
Executed by:
  • test_sshbuf
2
182 }-
183 POKE_U16(dp, len_bits);-
184 if (len_bytes != 0)
len_bytes != 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • test_sshbuf
FALSEnever evaluated
0-2
185 memcpy(dp + 2, d, len_bytes);
executed 2 times by 1 test: memcpy(dp + 2, d, len_bytes);
Executed by:
  • test_sshbuf
2
186 explicit_bzero(d, sizeof(d));-
187 return 0;
executed 2 times by 1 test: return 0;
Executed by:
  • test_sshbuf
2
188}-
189-
190#ifdef OPENSSL_HAS_ECC-
191int-
192sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g)-
193{-
194 u_char d[SSHBUF_MAX_ECPOINT];-
195 BN_CTX *bn_ctx;-
196 size_t len;-
197 int ret;-
198-
199 if ((bn_ctx = BN_CTX_new()) == NULL)
(bn_ctx = BN_C...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 412 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
0-412
200 return SSH_ERR_ALLOC_FAIL;
never executed: return -2;
0
201 if ((len = EC_POINT_point2oct(g, v, POINT_CONVERSION_UNCOMPRESSED,
(len = EC_POIN... * 2 / 8) + 1)Description
TRUEnever evaluated
FALSEevaluated 412 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
0-412
202 NULL, 0, bn_ctx)) > SSHBUF_MAX_ECPOINT) {
(len = EC_POIN... * 2 / 8) + 1)Description
TRUEnever evaluated
FALSEevaluated 412 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
0-412
203 BN_CTX_free(bn_ctx);-
204 return SSH_ERR_INVALID_ARGUMENT;
never executed: return -10;
0
205 }-
206 if (EC_POINT_point2oct(g, v, POINT_CONVERSION_UNCOMPRESSED,
EC_POINT_point...bn_ctx) != lenDescription
TRUEnever evaluated
FALSEevaluated 412 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
0-412
207 d, len, bn_ctx) != len) {
EC_POINT_point...bn_ctx) != lenDescription
TRUEnever evaluated
FALSEevaluated 412 times by 4 tests
Evaluated by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
0-412
208 BN_CTX_free(bn_ctx);-
209 return SSH_ERR_INTERNAL_ERROR; /* Shouldn't happen */
never executed: return -1;
0
210 }-
211 BN_CTX_free(bn_ctx);-
212 ret = sshbuf_put_string(buf, d, len);-
213 explicit_bzero(d, len);-
214 return ret;
executed 412 times by 4 tests: return ret;
Executed by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
412
215}-
216-
217int-
218sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v)-
219{-
220 return sshbuf_put_ec(buf, EC_KEY_get0_public_key(v),
executed 52 times by 4 tests: return sshbuf_put_ec(buf, EC_KEY_get0_public_key(v), EC_KEY_get0_group(v));
Executed by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
52
221 EC_KEY_get0_group(v));
executed 52 times by 4 tests: return sshbuf_put_ec(buf, EC_KEY_get0_public_key(v), EC_KEY_get0_group(v));
Executed by:
  • ssh-keygen
  • test_kex
  • test_sshbuf
  • test_sshkey
52
222}-
223#endif /* OPENSSL_HAS_ECC */-
224-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2