1 /* $OpenBSD: x_long.c,v 1.18 2022/07/02 18:14:35 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <limits.h>
60 #include <string.h>
61
62 #include <openssl/asn1t.h>
63 #include <openssl/bn.h>
64 #include <openssl/err.h>
65
66 #include "asn1_locl.h"
67
68 /*
69 * Custom primitive type for long handling. This converts between an
70 * ASN1_INTEGER and a long directly.
71 */
72
73 static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
74 static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
75 static void long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
76
77 static int long_i2c(ASN1_VALUE **pval, unsigned char *content, int *putype,
78 const ASN1_ITEM *it);
79 static int long_c2i(ASN1_VALUE **pval, const unsigned char *content, int len,
80 int utype, char *free_content, const ASN1_ITEM *it);
81 static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
82 int indent, const ASN1_PCTX *pctx);
83
84 static ASN1_PRIMITIVE_FUNCS long_pf = {
85 .app_data = NULL,
86 .flags = 0,
87 .prim_new = long_new,
88 .prim_free = long_free,
89 .prim_clear = long_clear,
90 .prim_c2i = long_c2i,
91 .prim_i2c = long_i2c,
92 .prim_print = long_print,
93 };
94
95 const ASN1_ITEM LONG_it = {
96 .itype = ASN1_ITYPE_PRIMITIVE,
97 .utype = V_ASN1_INTEGER,
98 .templates = NULL,
99 .tcount = 0,
100 .funcs = &long_pf,
101 .size = ASN1_LONG_UNDEF,
102 .sname = "LONG",
103 };
104
105 const ASN1_ITEM ZLONG_it = {
106 .itype = ASN1_ITYPE_PRIMITIVE,
107 .utype = V_ASN1_INTEGER,
108 .templates = NULL,
109 .tcount = 0,
110 .funcs = &long_pf,
111 .size = 0,
112 .sname = "ZLONG",
113 };
114
115 static void
long_get(ASN1_VALUE ** pval,long * out_val)116 long_get(ASN1_VALUE **pval, long *out_val)
117 {
118 memcpy(out_val, pval, sizeof(long));
119 }
120
121 static void
long_set(ASN1_VALUE ** pval,long val)122 long_set(ASN1_VALUE **pval, long val)
123 {
124 memcpy(pval, &val, sizeof(long));
125 }
126
127 static int
long_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)128 long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
129 {
130 long_clear(pval, it);
131
132 return 1;
133 }
134
135 static void
long_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)136 long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
137 {
138 long_clear(pval, it);
139 }
140
141 static void
long_clear(ASN1_VALUE ** pval,const ASN1_ITEM * it)142 long_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
143 {
144 /* Zero value. */
145 long_set(pval, it->size);
146 }
147
148 static int
long_i2c(ASN1_VALUE ** pval,unsigned char * content,int * putype,const ASN1_ITEM * it)149 long_i2c(ASN1_VALUE **pval, unsigned char *content, int *putype,
150 const ASN1_ITEM *it)
151 {
152 ASN1_INTEGER *aint;
153 uint8_t **pp = NULL;
154 long val;
155 int ret = 0;
156
157 long_get(pval, &val);
158
159 /*
160 * The zero value for this type (stored in the overloaded it->size
161 * field) is considered to be invalid.
162 */
163 if (val == it->size)
164 return -1;
165
166 if ((aint = ASN1_INTEGER_new()) == NULL)
167 goto err;
168 if (!ASN1_INTEGER_set_int64(aint, (int64_t)val))
169 goto err;
170 if (content != NULL)
171 pp = &content;
172 ret = i2c_ASN1_INTEGER(aint, pp);
173
174 err:
175 ASN1_INTEGER_free(aint);
176
177 return ret;
178 }
179
180 static int
long_c2i(ASN1_VALUE ** pval,const unsigned char * content,int len,int utype,char * free_content,const ASN1_ITEM * it)181 long_c2i(ASN1_VALUE **pval, const unsigned char *content, int len, int utype,
182 char *free_content, const ASN1_ITEM *it)
183 {
184 ASN1_INTEGER *aint = NULL;
185 const uint8_t **pp = NULL;
186 int64_t val = 0;
187 int ret = 0;
188
189 /*
190 * The original long_i2c() mishandled 0 values and encoded them as
191 * content with zero length, rather than a single zero byte. Permit
192 * zero length content here for backwards compatibility.
193 */
194 if (len != 0) {
195 if (content != NULL)
196 pp = &content;
197 if (!c2i_ASN1_INTEGER(&aint, pp, len))
198 goto err;
199 if (!ASN1_INTEGER_get_int64(&val, aint))
200 goto err;
201 }
202
203 if (val < LONG_MIN || val > LONG_MAX) {
204 ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
205 goto err;
206 }
207
208 /*
209 * The zero value for this type (stored in the overloaded it->size
210 * field) is considered to be invalid.
211 */
212 if (val == (int64_t)it->size) {
213 ASN1error(ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
214 goto err;
215 }
216
217 long_set(pval, (long)val);
218
219 ret = 1;
220
221 err:
222 ASN1_INTEGER_free(aint);
223
224 return ret;
225 }
226
227 static int
long_print(BIO * out,ASN1_VALUE ** pval,const ASN1_ITEM * it,int indent,const ASN1_PCTX * pctx)228 long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent,
229 const ASN1_PCTX *pctx)
230 {
231 long val;
232
233 long_get(pval, &val);
234
235 if (BIO_printf(out, "%ld\n", val) <= 0)
236 return 0;
237
238 return 1;
239 }
240