/src/openssl/crypto/asn1/x_int64.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define INTxx_FLAG_ZERO_DEFAULT (1<<0) |
27 | 402k | #define INTxx_FLAG_SIGNED (1<<1) |
28 | | |
29 | | static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
30 | 0 | { |
31 | 0 | if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL) { |
32 | 0 | ASN1err(ASN1_F_UINT64_NEW, ERR_R_MALLOC_FAILURE); |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | return 1; |
36 | 0 | } |
37 | | |
38 | | static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
39 | 0 | { |
40 | 0 | OPENSSL_free(*pval); |
41 | 0 | *pval = NULL; |
42 | 0 | } |
43 | | |
44 | | static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
45 | 0 | { |
46 | 0 | **(uint64_t **)pval = 0; |
47 | 0 | } |
48 | | |
49 | | static int uint64_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
50 | | const ASN1_ITEM *it) |
51 | 0 | { |
52 | 0 | uint64_t utmp; |
53 | 0 | int neg = 0; |
54 | 0 | /* this exists to bypass broken gcc optimization */ |
55 | 0 | char *cp = (char *)*pval; |
56 | 0 |
|
57 | 0 | /* use memcpy, because we may not be uint64_t aligned */ |
58 | 0 | memcpy(&utmp, cp, sizeof(utmp)); |
59 | 0 |
|
60 | 0 | if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT |
61 | 0 | && utmp == 0) |
62 | 0 | return -1; |
63 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED |
64 | 0 | && (int64_t)utmp < 0) { |
65 | 0 | /* i2c_uint64_int() assumes positive values */ |
66 | 0 | utmp = 0 - utmp; |
67 | 0 | neg = 1; |
68 | 0 | } |
69 | 0 |
|
70 | 0 | return i2c_uint64_int(cont, utmp, neg); |
71 | 0 | } |
72 | | |
73 | | static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
74 | | int utype, char *free_cont, const ASN1_ITEM *it) |
75 | 0 | { |
76 | 0 | uint64_t utmp = 0; |
77 | 0 | char *cp; |
78 | 0 | int neg = 0; |
79 | 0 |
|
80 | 0 | if (*pval == NULL && !uint64_new(pval, it)) |
81 | 0 | return 0; |
82 | 0 | |
83 | 0 | cp = (char *)*pval; |
84 | 0 | if (!c2i_uint64_int(&utmp, &neg, &cont, len)) |
85 | 0 | return 0; |
86 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) { |
87 | 0 | ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE); |
88 | 0 | return 0; |
89 | 0 | } |
90 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED |
91 | 0 | && !neg && utmp > INT64_MAX) { |
92 | 0 | ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE); |
93 | 0 | return 0; |
94 | 0 | } |
95 | 0 | if (neg) |
96 | 0 | /* c2i_uint64_int() returns positive values */ |
97 | 0 | utmp = 0 - utmp; |
98 | 0 | memcpy(cp, &utmp, sizeof(utmp)); |
99 | 0 | return 1; |
100 | 0 | } |
101 | | |
102 | | static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, |
103 | | int indent, const ASN1_PCTX *pctx) |
104 | 0 | { |
105 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED) |
106 | 0 | return BIO_printf(out, "%jd\n", **(int64_t **)pval); |
107 | 0 | return BIO_printf(out, "%ju\n", **(uint64_t **)pval); |
108 | 0 | } |
109 | | |
110 | | /* 32-bit variants */ |
111 | | |
112 | | static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
113 | 0 | { |
114 | 0 | if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) { |
115 | 0 | ASN1err(ASN1_F_UINT32_NEW, ERR_R_MALLOC_FAILURE); |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | return 1; |
119 | 0 | } |
120 | | |
121 | | static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
122 | 0 | { |
123 | 0 | OPENSSL_free(*pval); |
124 | 0 | *pval = NULL; |
125 | 0 | } |
126 | | |
127 | | static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
128 | 0 | { |
129 | 0 | **(uint32_t **)pval = 0; |
130 | 0 | } |
131 | | |
132 | | static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
133 | | const ASN1_ITEM *it) |
134 | 0 | { |
135 | 0 | uint32_t utmp; |
136 | 0 | int neg = 0; |
137 | 0 | /* this exists to bypass broken gcc optimization */ |
138 | 0 | char *cp = (char *)*pval; |
139 | 0 |
|
140 | 0 | /* use memcpy, because we may not be uint32_t aligned */ |
141 | 0 | memcpy(&utmp, cp, sizeof(utmp)); |
142 | 0 |
|
143 | 0 | if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT |
144 | 0 | && utmp == 0) |
145 | 0 | return -1; |
146 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED |
147 | 0 | && (int32_t)utmp < 0) { |
148 | 0 | /* i2c_uint64_int() assumes positive values */ |
149 | 0 | utmp = 0 - utmp; |
150 | 0 | neg = 1; |
151 | 0 | } |
152 | 0 |
|
153 | 0 | return i2c_uint64_int(cont, (uint64_t)utmp, neg); |
154 | 0 | } |
155 | | |
156 | | /* |
157 | | * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces |
158 | | * overflow warnings. |
159 | | */ |
160 | | |
161 | 52.9k | #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1) |
162 | | |
163 | | static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
164 | | int utype, char *free_cont, const ASN1_ITEM *it) |
165 | 176k | { |
166 | 176k | uint64_t utmp = 0; |
167 | 176k | uint32_t utmp2 = 0; |
168 | 176k | char *cp; |
169 | 176k | int neg = 0; |
170 | 176k | |
171 | 176k | if (*pval == NULL && !uint64_new(pval, it)) |
172 | 0 | return 0; |
173 | 176k | |
174 | 176k | cp = (char *)*pval; |
175 | 176k | if (!c2i_uint64_int(&utmp, &neg, &cont, len)) |
176 | 2.60k | return 0; |
177 | 173k | if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) { |
178 | 0 | ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE); |
179 | 0 | return 0; |
180 | 0 | } |
181 | 173k | if (neg) { |
182 | 52.9k | if (utmp > ABS_INT32_MIN) { |
183 | 4.42k | ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL); |
184 | 4.42k | return 0; |
185 | 4.42k | } |
186 | 48.5k | utmp = 0 - utmp; |
187 | 120k | } else { |
188 | 120k | if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX) |
189 | 120k | || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) { |
190 | 13.2k | ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE); |
191 | 13.2k | return 0; |
192 | 13.2k | } |
193 | 156k | } |
194 | 156k | utmp2 = (uint32_t)utmp; |
195 | 156k | memcpy(cp, &utmp2, sizeof(utmp2)); |
196 | 156k | return 1; |
197 | 156k | } |
198 | | |
199 | | static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, |
200 | | int indent, const ASN1_PCTX *pctx) |
201 | 0 | { |
202 | 0 | if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED) |
203 | 0 | return BIO_printf(out, "%d\n", **(int32_t **)pval); |
204 | 0 | return BIO_printf(out, "%u\n", **(uint32_t **)pval); |
205 | 0 | } |
206 | | |
207 | | |
208 | | /* Define the primitives themselves */ |
209 | | |
210 | | static ASN1_PRIMITIVE_FUNCS uint32_pf = { |
211 | | NULL, 0, |
212 | | uint32_new, |
213 | | uint32_free, |
214 | | uint32_clear, |
215 | | uint32_c2i, |
216 | | uint32_i2c, |
217 | | uint32_print |
218 | | }; |
219 | | |
220 | | static ASN1_PRIMITIVE_FUNCS uint64_pf = { |
221 | | NULL, 0, |
222 | | uint64_new, |
223 | | uint64_free, |
224 | | uint64_clear, |
225 | | uint64_c2i, |
226 | | uint64_i2c, |
227 | | uint64_print |
228 | | }; |
229 | | |
230 | | ASN1_ITEM_start(INT32) |
231 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, |
232 | | INTxx_FLAG_SIGNED, "INT32" |
233 | | ASN1_ITEM_end(INT32) |
234 | | |
235 | | ASN1_ITEM_start(UINT32) |
236 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32" |
237 | | ASN1_ITEM_end(UINT32) |
238 | | |
239 | | ASN1_ITEM_start(INT64) |
240 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, |
241 | | INTxx_FLAG_SIGNED, "INT64" |
242 | | ASN1_ITEM_end(INT64) |
243 | | |
244 | | ASN1_ITEM_start(UINT64) |
245 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64" |
246 | | ASN1_ITEM_end(UINT64) |
247 | | |
248 | | ASN1_ITEM_start(ZINT32) |
249 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, |
250 | | INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32" |
251 | | ASN1_ITEM_end(ZINT32) |
252 | | |
253 | | ASN1_ITEM_start(ZUINT32) |
254 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, |
255 | | INTxx_FLAG_ZERO_DEFAULT, "ZUINT32" |
256 | | ASN1_ITEM_end(ZUINT32) |
257 | | |
258 | | ASN1_ITEM_start(ZINT64) |
259 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, |
260 | | INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64" |
261 | | ASN1_ITEM_end(ZINT64) |
262 | | |
263 | | ASN1_ITEM_start(ZUINT64) |
264 | | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, |
265 | | INTxx_FLAG_ZERO_DEFAULT, "ZUINT64" |
266 | | ASN1_ITEM_end(ZUINT64) |
267 | | |