2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/bn.h>
15 #include "asn1_local.h"
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.
24 * We abuse the ASN1_ITEM fields |size| as a flags field
26 #define INTxx_FLAG_ZERO_DEFAULT (1<<0)
27 #define INTxx_FLAG_SIGNED (1<<1)
29 static int uint64_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
31 if ((*pval
= (ASN1_VALUE
*)OPENSSL_zalloc(sizeof(uint64_t))) == NULL
) {
32 ASN1err(ASN1_F_UINT64_NEW
, ERR_R_MALLOC_FAILURE
);
38 static void uint64_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
44 static void uint64_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
46 **(uint64_t **)pval
= 0;
49 static int uint64_i2c(const ASN1_VALUE
**pval
, unsigned char *cont
, int *putype
,
54 /* this exists to bypass broken gcc optimization */
55 char *cp
= (char *)*pval
;
57 /* use memcpy, because we may not be uint64_t aligned */
58 memcpy(&utmp
, cp
, sizeof(utmp
));
60 if ((it
->size
& INTxx_FLAG_ZERO_DEFAULT
) == INTxx_FLAG_ZERO_DEFAULT
63 if ((it
->size
& INTxx_FLAG_SIGNED
) == INTxx_FLAG_SIGNED
64 && (int64_t)utmp
< 0) {
65 /* i2c_uint64_int() assumes positive values */
70 return i2c_uint64_int(cont
, utmp
, neg
);
73 static int uint64_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
74 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
80 if (*pval
== NULL
&& !uint64_new(pval
, it
))
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.
94 if (!c2i_uint64_int(&utmp
, &neg
, &cont
, len
))
96 if ((it
->size
& INTxx_FLAG_SIGNED
) == 0 && neg
) {
97 ASN1err(ASN1_F_UINT64_C2I
, ASN1_R_ILLEGAL_NEGATIVE_VALUE
);
100 if ((it
->size
& INTxx_FLAG_SIGNED
) == INTxx_FLAG_SIGNED
101 && !neg
&& utmp
> INT64_MAX
) {
102 ASN1err(ASN1_F_UINT64_C2I
, ASN1_R_TOO_LARGE
);
106 /* c2i_uint64_int() returns positive values */
110 memcpy(cp
, &utmp
, sizeof(utmp
));
114 static int uint64_print(BIO
*out
, const ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
115 int indent
, const ASN1_PCTX
*pctx
)
117 if ((it
->size
& INTxx_FLAG_SIGNED
) == INTxx_FLAG_SIGNED
)
118 return BIO_printf(out
, "%jd\n", **(int64_t **)pval
);
119 return BIO_printf(out
, "%ju\n", **(uint64_t **)pval
);
122 /* 32-bit variants */
124 static int uint32_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
126 if ((*pval
= (ASN1_VALUE
*)OPENSSL_zalloc(sizeof(uint32_t))) == NULL
) {
127 ASN1err(ASN1_F_UINT32_NEW
, ERR_R_MALLOC_FAILURE
);
133 static void uint32_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
139 static void uint32_clear(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
141 **(uint32_t **)pval
= 0;
144 static int uint32_i2c(const ASN1_VALUE
**pval
, unsigned char *cont
, int *putype
,
149 /* this exists to bypass broken gcc optimization */
150 char *cp
= (char *)*pval
;
152 /* use memcpy, because we may not be uint32_t aligned */
153 memcpy(&utmp
, cp
, sizeof(utmp
));
155 if ((it
->size
& INTxx_FLAG_ZERO_DEFAULT
) == INTxx_FLAG_ZERO_DEFAULT
158 if ((it
->size
& INTxx_FLAG_SIGNED
) == INTxx_FLAG_SIGNED
159 && (int32_t)utmp
< 0) {
160 /* i2c_uint64_int() assumes positive values */
165 return i2c_uint64_int(cont
, (uint64_t)utmp
, neg
);
169 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
173 #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
175 static int uint32_c2i(ASN1_VALUE
**pval
, const unsigned char *cont
, int len
,
176 int utype
, char *free_cont
, const ASN1_ITEM
*it
)
183 if (*pval
== NULL
&& !uint64_new(pval
, it
))
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.
197 if (!c2i_uint64_int(&utmp
, &neg
, &cont
, len
))
199 if ((it
->size
& INTxx_FLAG_SIGNED
) == 0 && neg
) {
200 ASN1err(ASN1_F_UINT32_C2I
, ASN1_R_ILLEGAL_NEGATIVE_VALUE
);
204 if (utmp
> ABS_INT32_MIN
) {
205 ASN1err(ASN1_F_UINT32_C2I
, ASN1_R_TOO_SMALL
);
210 if (((it
->size
& INTxx_FLAG_SIGNED
) != 0 && utmp
> INT32_MAX
)
211 || ((it
->size
& INTxx_FLAG_SIGNED
) == 0 && utmp
> UINT32_MAX
)) {
212 ASN1err(ASN1_F_UINT32_C2I
, ASN1_R_TOO_LARGE
);
218 utmp2
= (uint32_t)utmp
;
219 memcpy(cp
, &utmp2
, sizeof(utmp2
));
223 static int uint32_print(BIO
*out
, const ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
224 int indent
, const ASN1_PCTX
*pctx
)
226 if ((it
->size
& INTxx_FLAG_SIGNED
) == INTxx_FLAG_SIGNED
)
227 return BIO_printf(out
, "%d\n", **(int32_t **)pval
);
228 return BIO_printf(out
, "%u\n", **(uint32_t **)pval
);
232 /* Define the primitives themselves */
234 static ASN1_PRIMITIVE_FUNCS uint32_pf
= {
244 static ASN1_PRIMITIVE_FUNCS uint64_pf
= {
254 ASN1_ITEM_start(INT32
)
255 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint32_pf
,
256 INTxx_FLAG_SIGNED
, "INT32"
259 ASN1_ITEM_start(UINT32
)
260 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint32_pf
, 0, "UINT32"
261 ASN1_ITEM_end(UINT32
)
263 ASN1_ITEM_start(INT64
)
264 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint64_pf
,
265 INTxx_FLAG_SIGNED
, "INT64"
268 ASN1_ITEM_start(UINT64
)
269 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint64_pf
, 0, "UINT64"
270 ASN1_ITEM_end(UINT64
)
272 ASN1_ITEM_start(ZINT32
)
273 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint32_pf
,
274 INTxx_FLAG_ZERO_DEFAULT
|INTxx_FLAG_SIGNED
, "ZINT32"
275 ASN1_ITEM_end(ZINT32
)
277 ASN1_ITEM_start(ZUINT32
)
278 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint32_pf
,
279 INTxx_FLAG_ZERO_DEFAULT
, "ZUINT32"
280 ASN1_ITEM_end(ZUINT32
)
282 ASN1_ITEM_start(ZINT64
)
283 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint64_pf
,
284 INTxx_FLAG_ZERO_DEFAULT
|INTxx_FLAG_SIGNED
, "ZINT64"
285 ASN1_ITEM_end(ZINT64
)
287 ASN1_ITEM_start(ZUINT64
)
288 ASN1_ITYPE_PRIMITIVE
, V_ASN1_INTEGER
, NULL
, 0, &uint64_pf
,
289 INTxx_FLAG_ZERO_DEFAULT
, "ZUINT64"
290 ASN1_ITEM_end(ZUINT64
)