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 *pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t));
37 static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
43 static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
45 **(uint64_t **)pval = 0;
48 static int uint64_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
53 /* this exists to bypass broken gcc optimization */
54 char *cp = (char *)*pval;
56 /* use memcpy, because we may not be uint64_t aligned */
57 memcpy(&utmp, cp, sizeof(utmp));
59 if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
62 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
63 && (int64_t)utmp < 0) {
64 /* i2c_uint64_int() assumes positive values */
69 return i2c_uint64_int(cont, utmp, neg);
72 static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
73 int utype, char *free_cont, const ASN1_ITEM *it)
79 if (*pval == NULL && !uint64_new(pval, it))
83 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
85 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
86 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
89 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
90 && !neg && utmp > INT64_MAX) {
91 ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE);
95 /* c2i_uint64_int() returns positive values */
97 memcpy(cp, &utmp, sizeof(utmp));
101 static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
102 int indent, const ASN1_PCTX *pctx)
104 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
105 return BIO_printf(out, "%jd\n", **(int64_t **)pval);
106 return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
109 /* 32-bit variants */
111 static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
113 *pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t));
119 static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
125 static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
127 **(uint32_t **)pval = 0;
130 static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
135 /* this exists to bypass broken gcc optimization */
136 char *cp = (char *)*pval;
138 /* use memcpy, because we may not be uint32_t aligned */
139 memcpy(&utmp, cp, sizeof(utmp));
141 if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
144 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
145 && (int32_t)utmp < 0) {
146 /* i2c_uint64_int() assumes positive values */
151 return i2c_uint64_int(cont, (uint64_t)utmp, neg);
155 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
159 #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
161 static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
162 int utype, char *free_cont, const ASN1_ITEM *it)
169 if (*pval == NULL && !uint64_new(pval, it))
173 if (!c2i_uint64_int(&utmp, &neg, &cont, len))
175 if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
176 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
180 if (utmp > ABS_INT32_MIN) {
181 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL);
186 if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
187 || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
188 ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE);
192 utmp2 = (uint32_t)utmp;
193 memcpy(cp, &utmp2, sizeof(utmp2));
197 static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
198 int indent, const ASN1_PCTX *pctx)
200 if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
201 return BIO_printf(out, "%d\n", **(int32_t **)pval);
202 return BIO_printf(out, "%u\n", **(uint32_t **)pval);
206 /* Define the primitives themselves */
208 static ASN1_PRIMITIVE_FUNCS uint32_pf = {
218 static ASN1_PRIMITIVE_FUNCS uint64_pf = {
228 ASN1_ITEM_start(INT32)
229 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
230 INTxx_FLAG_SIGNED, "INT32"
233 ASN1_ITEM_start(UINT32)
234 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
235 ASN1_ITEM_end(UINT32)
237 ASN1_ITEM_start(INT64)
238 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
239 INTxx_FLAG_SIGNED, "INT64"
242 ASN1_ITEM_start(UINT64)
243 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
244 ASN1_ITEM_end(UINT64)
246 ASN1_ITEM_start(ZINT32)
247 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
248 INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
249 ASN1_ITEM_end(ZINT32)
251 ASN1_ITEM_start(ZUINT32)
252 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
253 INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
254 ASN1_ITEM_end(ZUINT32)
256 ASN1_ITEM_start(ZINT64)
257 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
258 INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
259 ASN1_ITEM_end(ZINT64)
261 ASN1_ITEM_start(ZUINT64)
262 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
263 INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
264 ASN1_ITEM_end(ZUINT64)