1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include "internal/numbers.h"
13e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
14e1051a39Sopenharmony_ci#include <openssl/bn.h>
15e1051a39Sopenharmony_ci#include "asn1_local.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci/*
18e1051a39Sopenharmony_ci * Custom primitive types for handling int32_t, int64_t, uint32_t, uint64_t.
19e1051a39Sopenharmony_ci * This converts between an ASN1_INTEGER and those types directly.
20e1051a39Sopenharmony_ci * This is preferred to using the LONG / ZLONG primitives.
21e1051a39Sopenharmony_ci */
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci/*
24e1051a39Sopenharmony_ci * We abuse the ASN1_ITEM fields |size| as a flags field
25e1051a39Sopenharmony_ci */
26e1051a39Sopenharmony_ci#define INTxx_FLAG_ZERO_DEFAULT (1<<0)
27e1051a39Sopenharmony_ci#define INTxx_FLAG_SIGNED       (1<<1)
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_cistatic int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
30e1051a39Sopenharmony_ci{
31e1051a39Sopenharmony_ci    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL) {
32e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
33e1051a39Sopenharmony_ci        return 0;
34e1051a39Sopenharmony_ci    }
35e1051a39Sopenharmony_ci    return 1;
36e1051a39Sopenharmony_ci}
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_cistatic void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
39e1051a39Sopenharmony_ci{
40e1051a39Sopenharmony_ci    OPENSSL_free(*pval);
41e1051a39Sopenharmony_ci    *pval = NULL;
42e1051a39Sopenharmony_ci}
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_cistatic void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
45e1051a39Sopenharmony_ci{
46e1051a39Sopenharmony_ci    **(uint64_t **)pval = 0;
47e1051a39Sopenharmony_ci}
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_cistatic int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
50e1051a39Sopenharmony_ci                      const ASN1_ITEM *it)
51e1051a39Sopenharmony_ci{
52e1051a39Sopenharmony_ci    uint64_t utmp;
53e1051a39Sopenharmony_ci    int neg = 0;
54e1051a39Sopenharmony_ci    /* this exists to bypass broken gcc optimization */
55e1051a39Sopenharmony_ci    char *cp = (char *)*pval;
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci    /* use memcpy, because we may not be uint64_t aligned */
58e1051a39Sopenharmony_ci    memcpy(&utmp, cp, sizeof(utmp));
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
61e1051a39Sopenharmony_ci        && utmp == 0)
62e1051a39Sopenharmony_ci        return -1;
63e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
64e1051a39Sopenharmony_ci        && (int64_t)utmp < 0) {
65e1051a39Sopenharmony_ci        /* ossl_i2c_uint64_int() assumes positive values */
66e1051a39Sopenharmony_ci        utmp = 0 - utmp;
67e1051a39Sopenharmony_ci        neg = 1;
68e1051a39Sopenharmony_ci    }
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ci    return ossl_i2c_uint64_int(cont, utmp, neg);
71e1051a39Sopenharmony_ci}
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_cistatic int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
74e1051a39Sopenharmony_ci                      int utype, char *free_cont, const ASN1_ITEM *it)
75e1051a39Sopenharmony_ci{
76e1051a39Sopenharmony_ci    uint64_t utmp = 0;
77e1051a39Sopenharmony_ci    char *cp;
78e1051a39Sopenharmony_ci    int neg = 0;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    if (*pval == NULL && !uint64_new(pval, it))
81e1051a39Sopenharmony_ci        return 0;
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci    cp = (char *)*pval;
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    /*
86e1051a39Sopenharmony_ci     * Strictly speaking, zero length is malformed.  However, long_c2i
87e1051a39Sopenharmony_ci     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
88e1051a39Sopenharmony_ci     * so for the sake of backward compatibility, we still decode zero
89e1051a39Sopenharmony_ci     * length INTEGERs as the number zero.
90e1051a39Sopenharmony_ci     */
91e1051a39Sopenharmony_ci    if (len == 0)
92e1051a39Sopenharmony_ci        goto long_compat;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
95e1051a39Sopenharmony_ci        return 0;
96e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
97e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
98e1051a39Sopenharmony_ci        return 0;
99e1051a39Sopenharmony_ci    }
100e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
101e1051a39Sopenharmony_ci            && !neg && utmp > INT64_MAX) {
102e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
103e1051a39Sopenharmony_ci        return 0;
104e1051a39Sopenharmony_ci    }
105e1051a39Sopenharmony_ci    if (neg)
106e1051a39Sopenharmony_ci        /* ossl_c2i_uint64_int() returns positive values */
107e1051a39Sopenharmony_ci        utmp = 0 - utmp;
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ci long_compat:
110e1051a39Sopenharmony_ci    memcpy(cp, &utmp, sizeof(utmp));
111e1051a39Sopenharmony_ci    return 1;
112e1051a39Sopenharmony_ci}
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_cistatic int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
115e1051a39Sopenharmony_ci                        int indent, const ASN1_PCTX *pctx)
116e1051a39Sopenharmony_ci{
117e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
118e1051a39Sopenharmony_ci        return BIO_printf(out, "%jd\n", **(int64_t **)pval);
119e1051a39Sopenharmony_ci    return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
120e1051a39Sopenharmony_ci}
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ci/* 32-bit variants */
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_cistatic int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
125e1051a39Sopenharmony_ci{
126e1051a39Sopenharmony_ci    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) {
127e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
128e1051a39Sopenharmony_ci        return 0;
129e1051a39Sopenharmony_ci    }
130e1051a39Sopenharmony_ci    return 1;
131e1051a39Sopenharmony_ci}
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_cistatic void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
134e1051a39Sopenharmony_ci{
135e1051a39Sopenharmony_ci    OPENSSL_free(*pval);
136e1051a39Sopenharmony_ci    *pval = NULL;
137e1051a39Sopenharmony_ci}
138e1051a39Sopenharmony_ci
139e1051a39Sopenharmony_cistatic void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
140e1051a39Sopenharmony_ci{
141e1051a39Sopenharmony_ci    **(uint32_t **)pval = 0;
142e1051a39Sopenharmony_ci}
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_cistatic int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
145e1051a39Sopenharmony_ci                      const ASN1_ITEM *it)
146e1051a39Sopenharmony_ci{
147e1051a39Sopenharmony_ci    uint32_t utmp;
148e1051a39Sopenharmony_ci    int neg = 0;
149e1051a39Sopenharmony_ci    /* this exists to bypass broken gcc optimization */
150e1051a39Sopenharmony_ci    char *cp = (char *)*pval;
151e1051a39Sopenharmony_ci
152e1051a39Sopenharmony_ci    /* use memcpy, because we may not be uint32_t aligned */
153e1051a39Sopenharmony_ci    memcpy(&utmp, cp, sizeof(utmp));
154e1051a39Sopenharmony_ci
155e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
156e1051a39Sopenharmony_ci        && utmp == 0)
157e1051a39Sopenharmony_ci        return -1;
158e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
159e1051a39Sopenharmony_ci        && (int32_t)utmp < 0) {
160e1051a39Sopenharmony_ci        /* ossl_i2c_uint64_int() assumes positive values */
161e1051a39Sopenharmony_ci        utmp = 0 - utmp;
162e1051a39Sopenharmony_ci        neg = 1;
163e1051a39Sopenharmony_ci    }
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_ci    return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
166e1051a39Sopenharmony_ci}
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci/*
169e1051a39Sopenharmony_ci * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
170e1051a39Sopenharmony_ci * overflow warnings.
171e1051a39Sopenharmony_ci */
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_ci#define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
174e1051a39Sopenharmony_ci
175e1051a39Sopenharmony_cistatic int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
176e1051a39Sopenharmony_ci                      int utype, char *free_cont, const ASN1_ITEM *it)
177e1051a39Sopenharmony_ci{
178e1051a39Sopenharmony_ci    uint64_t utmp = 0;
179e1051a39Sopenharmony_ci    uint32_t utmp2 = 0;
180e1051a39Sopenharmony_ci    char *cp;
181e1051a39Sopenharmony_ci    int neg = 0;
182e1051a39Sopenharmony_ci
183e1051a39Sopenharmony_ci    if (*pval == NULL && !uint64_new(pval, it))
184e1051a39Sopenharmony_ci        return 0;
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci    cp = (char *)*pval;
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci    /*
189e1051a39Sopenharmony_ci     * Strictly speaking, zero length is malformed.  However, long_c2i
190e1051a39Sopenharmony_ci     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
191e1051a39Sopenharmony_ci     * so for the sake of backward compatibility, we still decode zero
192e1051a39Sopenharmony_ci     * length INTEGERs as the number zero.
193e1051a39Sopenharmony_ci     */
194e1051a39Sopenharmony_ci    if (len == 0)
195e1051a39Sopenharmony_ci        goto long_compat;
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
198e1051a39Sopenharmony_ci        return 0;
199e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
200e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
201e1051a39Sopenharmony_ci        return 0;
202e1051a39Sopenharmony_ci    }
203e1051a39Sopenharmony_ci    if (neg) {
204e1051a39Sopenharmony_ci        if (utmp > ABS_INT32_MIN) {
205e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
206e1051a39Sopenharmony_ci            return 0;
207e1051a39Sopenharmony_ci        }
208e1051a39Sopenharmony_ci        utmp = 0 - utmp;
209e1051a39Sopenharmony_ci    } else {
210e1051a39Sopenharmony_ci        if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
211e1051a39Sopenharmony_ci            || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
212e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
213e1051a39Sopenharmony_ci            return 0;
214e1051a39Sopenharmony_ci        }
215e1051a39Sopenharmony_ci    }
216e1051a39Sopenharmony_ci
217e1051a39Sopenharmony_ci long_compat:
218e1051a39Sopenharmony_ci    utmp2 = (uint32_t)utmp;
219e1051a39Sopenharmony_ci    memcpy(cp, &utmp2, sizeof(utmp2));
220e1051a39Sopenharmony_ci    return 1;
221e1051a39Sopenharmony_ci}
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_cistatic int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
224e1051a39Sopenharmony_ci                        int indent, const ASN1_PCTX *pctx)
225e1051a39Sopenharmony_ci{
226e1051a39Sopenharmony_ci    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
227e1051a39Sopenharmony_ci        return BIO_printf(out, "%d\n", **(int32_t **)pval);
228e1051a39Sopenharmony_ci    return BIO_printf(out, "%u\n", **(uint32_t **)pval);
229e1051a39Sopenharmony_ci}
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci/* Define the primitives themselves */
233e1051a39Sopenharmony_ci
234e1051a39Sopenharmony_cistatic ASN1_PRIMITIVE_FUNCS uint32_pf = {
235e1051a39Sopenharmony_ci    NULL, 0,
236e1051a39Sopenharmony_ci    uint32_new,
237e1051a39Sopenharmony_ci    uint32_free,
238e1051a39Sopenharmony_ci    uint32_clear,
239e1051a39Sopenharmony_ci    uint32_c2i,
240e1051a39Sopenharmony_ci    uint32_i2c,
241e1051a39Sopenharmony_ci    uint32_print
242e1051a39Sopenharmony_ci};
243e1051a39Sopenharmony_ci
244e1051a39Sopenharmony_cistatic ASN1_PRIMITIVE_FUNCS uint64_pf = {
245e1051a39Sopenharmony_ci    NULL, 0,
246e1051a39Sopenharmony_ci    uint64_new,
247e1051a39Sopenharmony_ci    uint64_free,
248e1051a39Sopenharmony_ci    uint64_clear,
249e1051a39Sopenharmony_ci    uint64_c2i,
250e1051a39Sopenharmony_ci    uint64_i2c,
251e1051a39Sopenharmony_ci    uint64_print
252e1051a39Sopenharmony_ci};
253e1051a39Sopenharmony_ci
254e1051a39Sopenharmony_ciASN1_ITEM_start(INT32)
255e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
256e1051a39Sopenharmony_ci    INTxx_FLAG_SIGNED, "INT32"
257e1051a39Sopenharmony_ciASN1_ITEM_end(INT32)
258e1051a39Sopenharmony_ci
259e1051a39Sopenharmony_ciASN1_ITEM_start(UINT32)
260e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
261e1051a39Sopenharmony_ciASN1_ITEM_end(UINT32)
262e1051a39Sopenharmony_ci
263e1051a39Sopenharmony_ciASN1_ITEM_start(INT64)
264e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
265e1051a39Sopenharmony_ci    INTxx_FLAG_SIGNED, "INT64"
266e1051a39Sopenharmony_ciASN1_ITEM_end(INT64)
267e1051a39Sopenharmony_ci
268e1051a39Sopenharmony_ciASN1_ITEM_start(UINT64)
269e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
270e1051a39Sopenharmony_ciASN1_ITEM_end(UINT64)
271e1051a39Sopenharmony_ci
272e1051a39Sopenharmony_ciASN1_ITEM_start(ZINT32)
273e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
274e1051a39Sopenharmony_ci    INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
275e1051a39Sopenharmony_ciASN1_ITEM_end(ZINT32)
276e1051a39Sopenharmony_ci
277e1051a39Sopenharmony_ciASN1_ITEM_start(ZUINT32)
278e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
279e1051a39Sopenharmony_ci    INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
280e1051a39Sopenharmony_ciASN1_ITEM_end(ZUINT32)
281e1051a39Sopenharmony_ci
282e1051a39Sopenharmony_ciASN1_ITEM_start(ZINT64)
283e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
284e1051a39Sopenharmony_ci    INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
285e1051a39Sopenharmony_ciASN1_ITEM_end(ZINT64)
286e1051a39Sopenharmony_ci
287e1051a39Sopenharmony_ciASN1_ITEM_start(ZUINT64)
288e1051a39Sopenharmony_ci    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
289e1051a39Sopenharmony_ci    INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
290e1051a39Sopenharmony_ciASN1_ITEM_end(ZUINT64)
291e1051a39Sopenharmony_ci
292