ASN.1: extend the possibilities to embed data instead of pointers
[openssl.git] / crypto / asn1 / x_int64.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
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"
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
29 static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
30 {
31     *(uint64_t *)pval = 0;
32     return 1;
33 }
34
35 static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
36 {
37     *(uint64_t *)pval = 0;
38 }
39
40 static int uint64_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
41                     const ASN1_ITEM *it)
42 {
43     uint64_t utmp;
44     int neg = 0;
45     /* this exists to bypass broken gcc optimization */
46     char *cp = (char *)pval;
47
48     /* use memcpy, because we may not be uint64_t aligned */
49     memcpy(&utmp, cp, sizeof(utmp));
50
51     if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
52         && utmp == 0)
53         return -1;
54     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
55         && (int64_t)utmp < 0) {
56         /* i2c_uint64_int() assumes positive values */
57         utmp = 0 - utmp;
58         neg = 1;
59     }
60
61     return i2c_uint64_int(cont, utmp, neg);
62 }
63
64 static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
65                     int utype, char *free_cont, const ASN1_ITEM *it)
66 {
67     uint64_t utmp = 0;
68     char *cp = (char *)pval;
69     int neg = 0;
70
71     if (!c2i_uint64_int(&utmp, &neg, &cont, len))
72         return 0;
73     if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
74         ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
75         return 0;
76     }
77     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
78             && !neg && utmp > INT64_MAX) {
79         ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE);
80         return 0;
81     }
82     if (neg)
83         /* c2i_uint64_int() returns positive values */
84         utmp = 0 - utmp;
85     memcpy(cp, &utmp, sizeof(utmp));
86     return 1;
87 }
88
89 static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
90                         int indent, const ASN1_PCTX *pctx)
91 {
92     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
93         return BIO_printf(out, "%jd\n", *(int64_t *)pval);
94     return BIO_printf(out, "%ju\n", *(uint64_t *)pval);
95 }
96
97 /* 32-bit variants */
98
99 static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
100 {
101     *(uint32_t *)pval = 0;
102     return 1;
103 }
104
105 static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
106 {
107     *(uint32_t *)pval = 0;
108 }
109
110 static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
111                     const ASN1_ITEM *it)
112 {
113     uint32_t utmp;
114     int neg = 0;
115     /* this exists to bypass broken gcc optimization */
116     char *cp = (char *)pval;
117
118     /* use memcpy, because we may not be uint32_t aligned */
119     memcpy(&utmp, cp, sizeof(utmp));
120
121     if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
122         && utmp == 0)
123         return -1;
124     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
125         && (int32_t)utmp < 0) {
126         /* i2c_uint64_int() assumes positive values */
127         utmp = 0 - utmp;
128         neg = 1;
129     }
130
131     return i2c_uint64_int(cont, (uint64_t)utmp, neg);
132 }
133
134 /*
135  * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
136  * overflow warnings.
137  */
138
139 #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
140
141 static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
142                     int utype, char *free_cont, const ASN1_ITEM *it)
143 {
144     uint64_t utmp = 0;
145     uint32_t utmp2 = 0;
146     char *cp = (char *)pval;
147     int neg = 0;
148
149     if (!c2i_uint64_int(&utmp, &neg, &cont, len))
150         return 0;
151     if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
152         ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
153         return 0;
154     }
155     if (neg) {
156         if (utmp > ABS_INT32_MIN) {
157             ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL);
158             return 0;
159         }
160         utmp = 0 - utmp;
161     } else {
162         if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
163             || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
164             ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE);
165             return 0;
166         }
167     }
168     utmp2 = (uint32_t)utmp;
169     memcpy(cp, &utmp2, sizeof(utmp2));
170     return 1;
171 }
172
173 static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
174                         int indent, const ASN1_PCTX *pctx)
175 {
176     if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
177         return BIO_printf(out, "%d\n", *(int32_t *)pval);
178     return BIO_printf(out, "%u\n", *(uint32_t *)pval);
179 }
180
181
182 /* Define the primitives themselves */
183
184 static ASN1_PRIMITIVE_FUNCS uint32_pf = {
185     NULL, 0,
186     uint32_new,
187     uint32_free,
188     uint32_free,                  /* Clear should set to initial value */
189     uint32_c2i,
190     uint32_i2c,
191     uint32_print
192 };
193
194 static ASN1_PRIMITIVE_FUNCS uint64_pf = {
195     NULL, 0,
196     uint64_new,
197     uint64_free,
198     uint64_free,                  /* Clear should set to initial value */
199     uint64_c2i,
200     uint64_i2c,
201     uint64_print
202 };
203
204 ASN1_ITEM_start(INT32)
205     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
206     INTxx_FLAG_SIGNED, "INT32"
207 ASN1_ITEM_end(INT32)
208
209 ASN1_ITEM_start(UINT32)
210     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
211 ASN1_ITEM_end(UINT32)
212
213 ASN1_ITEM_start(INT64)
214     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
215     INTxx_FLAG_SIGNED, "INT64"
216 ASN1_ITEM_end(INT64)
217
218 ASN1_ITEM_start(UINT64)
219     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
220 ASN1_ITEM_end(UINT64)
221
222 ASN1_ITEM_start(ZINT32)
223     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
224     INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
225 ASN1_ITEM_end(ZINT32)
226
227 ASN1_ITEM_start(ZUINT32)
228     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
229     INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
230 ASN1_ITEM_end(ZUINT32)
231
232 ASN1_ITEM_start(ZINT64)
233     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
234     INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
235 ASN1_ITEM_end(ZINT64)
236
237 ASN1_ITEM_start(ZUINT64)
238     ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
239     INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
240 ASN1_ITEM_end(ZUINT64)
241