2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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(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))
84 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
86 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
87 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
90 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
91 && !neg && utmp > INT64_MAX) {
92 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE);
96 /* c2i_uint64_int() returns positive values */
98 memcpy(cp, &utmp, sizeof(utmp));
102 static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
103 int indent, const ASN1_PCTX *pctx)
105 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
106 return BIO_printf(out, "%jd\n", **(int64_t **)pval);
107 return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
110 /* 32-bit variants */
112 static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
114 if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) {
115 ASN1err(ASN1_F_UINT32_NEW, ERR_R_MALLOC_FAILURE);
121 static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
127 static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
129 **(uint32_t **)pval = 0;
132 static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
137 /* this exists to bypass broken gcc optimization */
138 char *cp = (char *)*pval;
140 /* use memcpy, because we may not be uint32_t aligned */
141 memcpy(&utmp, cp, sizeof(utmp));
143 if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
146 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
147 && (int32_t)utmp < 0) {
148 /* i2c_uint64_int() assumes positive values */
153 return i2c_uint64_int(cont, (uint64_t)utmp, neg);
157 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
161 #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
163 static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
164 int utype, char *free_cont, const ASN1_ITEM *it)
171 if (*pval == NULL && !uint64_new(pval, it))
175 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
177 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
178 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
182 if (utmp > ABS_INT32_MIN) {
183 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL);
188 if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
189 || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
190 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE);
194 utmp2 = (uint32_t)utmp;
195 memcpy(cp, &utmp2, sizeof(utmp2));
199 static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
200 int indent, const ASN1_PCTX *pctx)
202 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
203 return BIO_printf(out, "%d\n", **(int32_t **)pval);
204 return BIO_printf(out, "%u\n", **(uint32_t **)pval);
208 /* Define the primitives themselves */
210 static ASN1_PRIMITIVE_FUNCS uint32_pf = {
220 static ASN1_PRIMITIVE_FUNCS uint64_pf = {
230 ASN1_ITEM_start(INT32)
231 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
232 INTxx_FLAG_SIGNED, "INT32"
235 ASN1_ITEM_start(UINT32)
236 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
237 ASN1_ITEM_end(UINT32)
239 ASN1_ITEM_start(INT64)
240 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
241 INTxx_FLAG_SIGNED, "INT64"
244 ASN1_ITEM_start(UINT64)
245 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
246 ASN1_ITEM_end(UINT64)
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)
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)
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)
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)