OpenCoverage

x_int64.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/asn1/x_int64.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
4 * Licensed under the OpenSSL license (the "License"). You may not use-
5 * this file except in compliance with the License. You can obtain a copy-
6 * in the file LICENSE in the source distribution or at-
7 * https://www.openssl.org/source/license.html-
8 */-
9-
10#include <stdio.h>-
11#include "internal/cryptlib.h"-
12#include "internal/numbers.h"-
13#include <openssl/asn1t.h>-
14#include <openssl/bn.h>-
15#include "asn1_locl.h"-
16-
17/*-
18 * Custom primitive types for handling int32_t, int64_t, uint32_t, uint64_t.-
19 * This converts between an ASN1_INTEGER and those types directly.-
20 * This is preferred to using the LONG / ZLONG primitives.-
21 */-
22-
23/*-
24 * We abuse the ASN1_ITEM fields |size| as a flags field-
25 */-
26#define INTxx_FLAG_ZERO_DEFAULT (1<<0)-
27#define INTxx_FLAG_SIGNED (1<<1)-
28-
29static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)-
30{-
31 if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL) {
(*pval = (ASN1...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 208 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-208
32 ASN1err(ASN1_F_UINT64_NEW, ERR_R_MALLOC_FAILURE);-
33 return 0;
never executed: return 0;
0
34 }-
35 return 1;
executed 208 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
208
36}-
37-
38static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)-
39{-
40 OPENSSL_free(*pval);-
41 *pval = NULL;-
42}
executed 13292 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
13292
43-
44static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)-
45{-
46 **(uint64_t **)pval = 0;-
47}
executed 43142 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
43142
48-
49static int uint64_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,-
50 const ASN1_ITEM *it)-
51{-
52 uint64_t utmp;-
53 int neg = 0;-
54 /* this exists to bypass broken gcc optimization */-
55 char *cp = (char *)*pval;-
56-
57 /* use memcpy, because we may not be uint64_t aligned */-
58 memcpy(&utmp, cp, sizeof(utmp));-
59-
60 if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
(it->size & (1<<0)) == (1<<0)Description
TRUEevaluated 62577 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 455 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
455-62577
61 && utmp == 0)
utmp == 0Description
TRUEevaluated 16576 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 46001 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
16576-46001
62 return -1;
executed 16576 times by 1 test: return -1;
Executed by:
  • libcrypto.so.1.1
16576
63 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
(it->size & (1<<1)) == (1<<1)Description
TRUEevaluated 37033 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 9423 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
9423-37033
64 && (int64_t)utmp < 0) {
(int64_t)utmp < 0Description
TRUEevaluated 128 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 36905 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
128-36905
65 /* i2c_uint64_int() assumes positive values */-
66 utmp = 0 - utmp;-
67 neg = 1;-
68 }
executed 128 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
128
69-
70 return i2c_uint64_int(cont, utmp, neg);
executed 46456 times by 1 test: return i2c_uint64_int(cont, utmp, neg);
Executed by:
  • libcrypto.so.1.1
46456
71}-
72-
73static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
74 int utype, char *free_cont, const ASN1_ITEM *it)-
75{-
76 uint64_t utmp = 0;-
77 char *cp;-
78 int neg = 0;-
79-
80 if (*pval == NULL && !uint64_new(pval, it))
*pval == ((void *)0)Description
TRUEevaluated 104 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 5157 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
!uint64_new(pval, it)Description
TRUEnever evaluated
FALSEevaluated 104 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-5157
81 return 0;
never executed: return 0;
0
82-
83 cp = (char *)*pval;-
84-
85 /*-
86 * Strictly speaking, zero length is malformed. However, long_c2i-
87 * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),-
88 * so for the sake of backward compatibility, we still decode zero-
89 * length INTEGERs as the number zero.-
90 */-
91 if (len == 0)
len == 0Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 5254 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
7-5254
92 goto long_compat;
executed 7 times by 1 test: goto long_compat;
Executed by:
  • libcrypto.so.1.1
7
93-
94 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
!c2i_uint64_in...g, &cont, len)Description
TRUEevaluated 69 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 5185 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
69-5185
95 return 0;
executed 69 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
69
96 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
(it->size & (1<<1)) == 0Description
TRUEevaluated 1134 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4051 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
negDescription
TRUEevaluated 22 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1112 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
22-4051
97 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);-
98 return 0;
executed 22 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
22
99 }-
100 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
(it->size & (1<<1)) == (1<<1)Description
TRUEevaluated 4051 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1112 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1112-4051
101 && !neg && utmp > INT64_MAX) {
!negDescription
TRUEevaluated 4015 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 36 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
utmp > (9223372036854775807L)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4010 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
5-4015
102 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE);-
103 return 0;
executed 5 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
5
104 }-
105 if (neg)
negDescription
TRUEevaluated 36 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 5122 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
36-5122
106 /* c2i_uint64_int() returns positive values */-
107 utmp = 0 - utmp;
executed 36 times by 1 test: utmp = 0 - utmp;
Executed by:
  • libcrypto.so.1.1
36
108-
109 long_compat:
code before this statement executed 5158 times by 1 test: long_compat:
Executed by:
  • libcrypto.so.1.1
5158
110 memcpy(cp, &utmp, sizeof(utmp));-
111 return 1;
executed 5165 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
5165
112}-
113-
114static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,-
115 int indent, const ASN1_PCTX *pctx)-
116{-
117 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
(it->size & (1<<1)) == (1<<1)Description
TRUEevaluated 28 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 18 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
18-28
118 return BIO_printf(out, "%jd\n", **(int64_t **)pval);
executed 28 times by 1 test: return BIO_printf(out, "%jd\n", **(int64_t **)pval);
Executed by:
  • libcrypto.so.1.1
28
119 return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
executed 18 times by 1 test: return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
Executed by:
  • libcrypto.so.1.1
18
120}-
121-
122/* 32-bit variants */-
123-
124static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)-
125{-
126 if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) {
(*pval = (ASN1...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
127 ASN1err(ASN1_F_UINT32_NEW, ERR_R_MALLOC_FAILURE);-
128 return 0;
never executed: return 0;
0
129 }-
130 return 1;
never executed: return 1;
0
131}-
132-
133static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)-
134{-
135 OPENSSL_free(*pval);-
136 *pval = NULL;-
137}
executed 13292 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
13292
138-
139static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)-
140{-
141 **(uint32_t **)pval = 0;-
142}
executed 130589 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
130589
143-
144static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,-
145 const ASN1_ITEM *it)-
146{-
147 uint32_t utmp;-
148 int neg = 0;-
149 /* this exists to bypass broken gcc optimization */-
150 char *cp = (char *)*pval;-
151-
152 /* use memcpy, because we may not be uint32_t aligned */-
153 memcpy(&utmp, cp, sizeof(utmp));-
154-
155 if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
(it->size & (1<<0)) == (1<<0)Description
TRUEevaluated 48258 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 72454 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
48258-72454
156 && utmp == 0)
utmp == 0Description
TRUEevaluated 38248 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 10010 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
10010-38248
157 return -1;
executed 38248 times by 1 test: return -1;
Executed by:
  • libcrypto.so.1.1
38248
158 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
(it->size & (1<<1)) == (1<<1)Description
TRUEevaluated 58073 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 24391 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
24391-58073
159 && (int32_t)utmp < 0) {
(int32_t)utmp < 0Description
TRUEevaluated 6464 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 51609 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6464-51609
160 /* i2c_uint64_int() assumes positive values */-
161 utmp = 0 - utmp;-
162 neg = 1;-
163 }
executed 6464 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
6464
164-
165 return i2c_uint64_int(cont, (uint64_t)utmp, neg);
executed 82464 times by 1 test: return i2c_uint64_int(cont, (uint64_t)utmp, neg);
Executed by:
  • libcrypto.so.1.1
82464
166}-
167-
168/*-
169 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces-
170 * overflow warnings.-
171 */-
172-
173#define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)-
174-
175static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,-
176 int utype, char *free_cont, const ASN1_ITEM *it)-
177{-
178 uint64_t utmp = 0;-
179 uint32_t utmp2 = 0;-
180 char *cp;-
181 int neg = 0;-
182-
183 if (*pval == NULL && !uint64_new(pval, it))
*pval == ((void *)0)Description
TRUEevaluated 104 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 37729 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
!uint64_new(pval, it)Description
TRUEnever evaluated
FALSEevaluated 104 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-37729
184 return 0;
never executed: return 0;
0
185-
186 cp = (char *)*pval;-
187-
188 /*-
189 * Strictly speaking, zero length is malformed. However, long_c2i-
190 * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),-
191 * so for the sake of backward compatibility, we still decode zero-
192 * length INTEGERs as the number zero.-
193 */-
194 if (len == 0)
len == 0Description
TRUEevaluated 249 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 37584 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
249-37584
195 goto long_compat;
executed 249 times by 1 test: goto long_compat;
Executed by:
  • libcrypto.so.1.1
249
196-
197 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
!c2i_uint64_in...g, &cont, len)Description
TRUEevaluated 401 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 37183 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
401-37183
198 return 0;
executed 401 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
401
199 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
(it->size & (1<<1)) == 0Description
TRUEevaluated 4295 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 32888 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
negDescription
TRUEevaluated 325 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 3970 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
325-32888
200 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);-
201 return 0;
executed 325 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
325
202 }-
203 if (neg) {
negDescription
TRUEevaluated 6266 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 30592 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6266-30592
204 if (utmp > ABS_INT32_MIN) {
utmp > ((uint3...47483647) + 1)Description
TRUEevaluated 70 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 6196 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
70-6196
205 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL);-
206 return 0;
executed 70 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
70
207 }-
208 utmp = 0 - utmp;-
209 } else {
executed 6196 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
6196
210 if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
(it->size & (1<<1)) != 0Description
TRUEevaluated 26622 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 3970 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
utmp > (2147483647)Description
TRUEevaluated 94 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 26528 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
94-26622
211 || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
(it->size & (1<<1)) == 0Description
TRUEevaluated 3970 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 26528 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
utmp > (4294967295U)Description
TRUEevaluated 20 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 3950 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
20-26528
212 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE);-
213 return 0;
executed 114 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
114
214 }-
215 }
executed 30478 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
30478
216-
217 long_compat:
code before this statement executed 36674 times by 1 test: long_compat:
Executed by:
  • libcrypto.so.1.1
36674
218 utmp2 = (uint32_t)utmp;-
219 memcpy(cp, &utmp2, sizeof(utmp2));-
220 return 1;
executed 36923 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
36923
221}-
222-
223static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,-
224 int indent, const ASN1_PCTX *pctx)-
225{-
226 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
(it->size & (1<<1)) == (1<<1)Description
TRUEevaluated 978 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12-978
227 return BIO_printf(out, "%d\n", **(int32_t **)pval);
executed 978 times by 1 test: return BIO_printf(out, "%d\n", **(int32_t **)pval);
Executed by:
  • libcrypto.so.1.1
978
228 return BIO_printf(out, "%u\n", **(uint32_t **)pval);
executed 12 times by 1 test: return BIO_printf(out, "%u\n", **(uint32_t **)pval);
Executed by:
  • libcrypto.so.1.1
12
229}-
230-
231-
232/* Define the primitives themselves */-
233-
234static ASN1_PRIMITIVE_FUNCS uint32_pf = {-
235 NULL, 0,-
236 uint32_new,-
237 uint32_free,-
238 uint32_clear,-
239 uint32_c2i,-
240 uint32_i2c,-
241 uint32_print-
242};-
243-
244static ASN1_PRIMITIVE_FUNCS uint64_pf = {-
245 NULL, 0,-
246 uint64_new,-
247 uint64_free,-
248 uint64_clear,-
249 uint64_c2i,-
250 uint64_i2c,-
251 uint64_print-
252};-
253-
254ASN1_ITEM_start(INT32)-
255 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,-
256 INTxx_FLAG_SIGNED, "INT32"-
257ASN1_ITEM_end(INT32)-
258-
259ASN1_ITEM_start(UINT32)-
260 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"-
261ASN1_ITEM_end(UINT32)-
262-
263ASN1_ITEM_start(INT64)-
264 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,-
265 INTxx_FLAG_SIGNED, "INT64"-
266ASN1_ITEM_end(INT64)-
267-
268ASN1_ITEM_start(UINT64)-
269 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"-
270ASN1_ITEM_end(UINT64)-
271-
272ASN1_ITEM_start(ZINT32)-
273 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,-
274 INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"-
275ASN1_ITEM_end(ZINT32)-
276-
277ASN1_ITEM_start(ZUINT32)-
278 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,-
279 INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"-
280ASN1_ITEM_end(ZUINT32)-
281-
282ASN1_ITEM_start(ZINT64)-
283 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,-
284 INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"-
285ASN1_ITEM_end(ZINT64)-
286-
287ASN1_ITEM_start(ZUINT64)-
288 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,-
289 INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"-
290ASN1_ITEM_end(ZUINT64)-
291-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2