]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/x_int64.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / x_int64.c
CommitLineData
93f7d6fc 1/*
3c2bdd7d 2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
93f7d6fc 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
93f7d6fc
RL
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"
93f7d6fc 12#include "internal/numbers.h"
64f11ee8 13#include <openssl/asn1t.h>
93f7d6fc 14#include <openssl/bn.h>
706457b7 15#include "asn1_local.h"
93f7d6fc
RL
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{
cdb10bae 31 if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL) {
9311d0c4 32 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
da26ff30 33 return 0;
cdb10bae 34 }
93f7d6fc
RL
35 return 1;
36}
37
38static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
39{
da26ff30
RL
40 OPENSSL_free(*pval);
41 *pval = NULL;
42}
43
44static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
45{
46 **(uint64_t **)pval = 0;
93f7d6fc
RL
47}
48
9fdcc21f
DO
49static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
50 const ASN1_ITEM *it)
93f7d6fc
RL
51{
52 uint64_t utmp;
53 int neg = 0;
54 /* this exists to bypass broken gcc optimization */
da26ff30 55 char *cp = (char *)*pval;
93f7d6fc
RL
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
61 && utmp == 0)
62 return -1;
63 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
429223d1 64 && (int64_t)utmp < 0) {
adf7e6d1 65 /* ossl_i2c_uint64_int() assumes positive values */
429223d1 66 utmp = 0 - utmp;
93f7d6fc 67 neg = 1;
429223d1 68 }
93f7d6fc 69
adf7e6d1 70 return ossl_i2c_uint64_int(cont, utmp, neg);
93f7d6fc
RL
71}
72
73static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
9fdcc21f 74 int utype, char *free_cont, const ASN1_ITEM *it)
93f7d6fc
RL
75{
76 uint64_t utmp = 0;
da26ff30 77 char *cp;
93f7d6fc
RL
78 int neg = 0;
79
da26ff30
RL
80 if (*pval == NULL && !uint64_new(pval, it))
81 return 0;
82
83 cp = (char *)*pval;
ca89174b
RL
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)
92 goto long_compat;
93
adf7e6d1 94 if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
93f7d6fc
RL
95 return 0;
96 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
9311d0c4 97 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
93f7d6fc
RL
98 return 0;
99 }
0856e3f1
MC
100 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
101 && !neg && utmp > INT64_MAX) {
9311d0c4 102 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
0856e3f1
MC
103 return 0;
104 }
429223d1 105 if (neg)
adf7e6d1 106 /* ossl_c2i_uint64_int() returns positive values */
429223d1 107 utmp = 0 - utmp;
ca89174b
RL
108
109 long_compat:
93f7d6fc
RL
110 memcpy(cp, &utmp, sizeof(utmp));
111 return 1;
112}
113
9fdcc21f 114static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
93f7d6fc
RL
115 int indent, const ASN1_PCTX *pctx)
116{
117 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
12997aa9
RS
118 return BIO_printf(out, "%jd\n", **(int64_t **)pval);
119 return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
93f7d6fc
RL
120}
121
122/* 32-bit variants */
123
124static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
125{
cdb10bae 126 if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) {
9311d0c4 127 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
da26ff30 128 return 0;
cdb10bae 129 }
93f7d6fc
RL
130 return 1;
131}
132
133static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
134{
da26ff30
RL
135 OPENSSL_free(*pval);
136 *pval = NULL;
137}
138
139static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
140{
141 **(uint32_t **)pval = 0;
93f7d6fc
RL
142}
143
9fdcc21f
DO
144static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
145 const ASN1_ITEM *it)
93f7d6fc
RL
146{
147 uint32_t utmp;
148 int neg = 0;
149 /* this exists to bypass broken gcc optimization */
da26ff30 150 char *cp = (char *)*pval;
93f7d6fc
RL
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
156 && utmp == 0)
157 return -1;
158 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
429223d1 159 && (int32_t)utmp < 0) {
adf7e6d1 160 /* ossl_i2c_uint64_int() assumes positive values */
429223d1 161 utmp = 0 - utmp;
93f7d6fc 162 neg = 1;
429223d1 163 }
93f7d6fc 164
adf7e6d1 165 return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
93f7d6fc
RL
166}
167
429223d1
RL
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
93f7d6fc 175static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
9fdcc21f 176 int utype, char *free_cont, const ASN1_ITEM *it)
93f7d6fc
RL
177{
178 uint64_t utmp = 0;
179 uint32_t utmp2 = 0;
da26ff30 180 char *cp;
93f7d6fc
RL
181 int neg = 0;
182
da26ff30
RL
183 if (*pval == NULL && !uint64_new(pval, it))
184 return 0;
185
186 cp = (char *)*pval;
ca89174b
RL
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)
195 goto long_compat;
196
adf7e6d1 197 if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
93f7d6fc
RL
198 return 0;
199 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
9311d0c4 200 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
93f7d6fc
RL
201 return 0;
202 }
429223d1
RL
203 if (neg) {
204 if (utmp > ABS_INT32_MIN) {
9311d0c4 205 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
429223d1
RL
206 return 0;
207 }
208 utmp = 0 - utmp;
209 } else {
210 if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
211 || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
9311d0c4 212 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
429223d1
RL
213 return 0;
214 }
93f7d6fc 215 }
ca89174b
RL
216
217 long_compat:
429223d1 218 utmp2 = (uint32_t)utmp;
93f7d6fc
RL
219 memcpy(cp, &utmp2, sizeof(utmp2));
220 return 1;
221}
222
9fdcc21f 223static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
93f7d6fc
RL
224 int indent, const ASN1_PCTX *pctx)
225{
226 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
da26ff30
RL
227 return BIO_printf(out, "%d\n", **(int32_t **)pval);
228 return BIO_printf(out, "%u\n", **(uint32_t **)pval);
93f7d6fc
RL
229}
230
231
232/* Define the primitives themselves */
233
234static ASN1_PRIMITIVE_FUNCS uint32_pf = {
235 NULL, 0,
236 uint32_new,
237 uint32_free,
da26ff30 238 uint32_clear,
93f7d6fc
RL
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,
da26ff30 248 uint64_clear,
93f7d6fc
RL
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