xref: /openbsd-src/lib/libcrypto/asn1/a_string.c (revision 0cfc246570751410dbee27c7d28e7716aec91136)
1 /* $OpenBSD: a_string.c,v 1.8 2022/05/16 20:41:24 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <limits.h>
60 #include <stdlib.h>
61 #include <string.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/err.h>
65 
66 #include "asn1_locl.h"
67 
68 ASN1_STRING *
69 ASN1_STRING_new(void)
70 {
71 	return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
72 }
73 
74 ASN1_STRING *
75 ASN1_STRING_type_new(int type)
76 {
77 	ASN1_STRING *astr;
78 
79 	if ((astr = calloc(1, sizeof(ASN1_STRING))) == NULL) {
80 		ASN1error(ERR_R_MALLOC_FAILURE);
81 		return NULL;
82 	}
83 	astr->type = type;
84 
85 	return astr;
86 }
87 
88 static void
89 ASN1_STRING_clear(ASN1_STRING *astr)
90 {
91 	if (!(astr->flags & ASN1_STRING_FLAG_NDEF))
92 		freezero(astr->data, astr->length);
93 
94 	astr->flags &= ~ASN1_STRING_FLAG_NDEF;
95 	astr->data = NULL;
96 	astr->length = 0;
97 }
98 
99 void
100 ASN1_STRING_free(ASN1_STRING *astr)
101 {
102 	if (astr == NULL)
103 		return;
104 
105 	ASN1_STRING_clear(astr);
106 
107 	free(astr);
108 }
109 
110 int
111 ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
112 {
113 	int cmp;
114 
115 	if (a == NULL || b == NULL)
116 		return -1;
117 	if ((cmp = (a->length - b->length)) != 0)
118 		return cmp;
119 	if ((cmp = memcmp(a->data, b->data, a->length)) != 0)
120 		return cmp;
121 
122 	return (a->type - b->type);
123 }
124 
125 int
126 ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *src)
127 {
128 	if (src == NULL)
129 		return 0;
130 
131 	if (!ASN1_STRING_set(dst, src->data, src->length))
132 		return 0;
133 
134 	dst->type = src->type;
135 	dst->flags = src->flags & ~ASN1_STRING_FLAG_NDEF;
136 
137 	return 1;
138 }
139 
140 ASN1_STRING *
141 ASN1_STRING_dup(const ASN1_STRING *src)
142 {
143 	ASN1_STRING *astr;
144 
145 	if (src == NULL)
146 		return NULL;
147 
148 	if ((astr = ASN1_STRING_new()) == NULL)
149 		return NULL;
150 	if (!ASN1_STRING_copy(astr, src)) {
151 		ASN1_STRING_free(astr);
152 		return NULL;
153 	}
154 	return astr;
155 }
156 
157 int
158 ASN1_STRING_set(ASN1_STRING *astr, const void *_data, int len)
159 {
160 	const char *data = _data;
161 
162 	if (len == -1) {
163 		size_t slen;
164 
165 		if (data == NULL)
166 			return 0;
167 
168 		if ((slen = strlen(data)) > INT_MAX)
169 			return 0;
170 
171 		len = (int)slen;
172 	}
173 
174 	ASN1_STRING_clear(astr);
175 
176 	if (len < 0 || len >= INT_MAX)
177 		return 0;
178 
179 	if ((astr->data = calloc(1, len + 1)) == NULL) {
180 		ASN1error(ERR_R_MALLOC_FAILURE);
181 		return (0);
182 	}
183 	astr->length = len;
184 
185 	if (data != NULL) {
186 		memcpy(astr->data, data, len);
187 		astr->data[len] = '\0';
188 	}
189 
190 	return 1;
191 }
192 
193 void
194 ASN1_STRING_set0(ASN1_STRING *astr, void *data, int len)
195 {
196 	ASN1_STRING_clear(astr);
197 
198 	astr->data = data;
199 	astr->length = len;
200 }
201 
202 void
203 asn1_add_error(const unsigned char *address, int offset)
204 {
205 	ERR_asprintf_error_data("offset=%d", offset);
206 }
207 
208 int
209 ASN1_STRING_length(const ASN1_STRING *astr)
210 {
211 	return astr->length;
212 }
213 
214 void
215 ASN1_STRING_length_set(ASN1_STRING *astr, int len)
216 {
217 	/* This is dangerous and unfixable. */
218 	astr->length = len;
219 }
220 
221 int
222 ASN1_STRING_type(const ASN1_STRING *astr)
223 {
224 	return astr->type;
225 }
226 
227 unsigned char *
228 ASN1_STRING_data(ASN1_STRING *astr)
229 {
230 	return astr->data;
231 }
232 
233 const unsigned char *
234 ASN1_STRING_get0_data(const ASN1_STRING *astr)
235 {
236 	return astr->data;
237 }
238 
239 int
240 ASN1_STRING_print(BIO *bp, const ASN1_STRING *astr)
241 {
242 	int i, n;
243 	char buf[80];
244 	const char *p;
245 
246 	if (astr == NULL)
247 		return 0;
248 
249 	n = 0;
250 	p = (const char *)astr->data;
251 	for (i = 0; i < astr->length; i++) {
252 		if ((p[i] > '~') || ((p[i] < ' ') &&
253 		    (p[i] != '\n') && (p[i] != '\r')))
254 			buf[n] = '.';
255 		else
256 			buf[n] = p[i];
257 		n++;
258 		if (n >= 80) {
259 			if (BIO_write(bp, buf, n) <= 0)
260 				return 0;
261 			n = 0;
262 		}
263 	}
264 	if (n > 0) {
265 		if (BIO_write(bp, buf, n) <= 0)
266 			return 0;
267 	}
268 
269 	return 1;
270 }
271 
272 /*
273  * Utility function: convert any string type to UTF8, returns number of bytes
274  * in output string or a negative error code
275  */
276 int
277 ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
278 {
279 	ASN1_STRING stmp = { 0 };
280 	ASN1_STRING *str = &stmp;
281 	int mbflag, ret;
282 
283 	if (in == NULL)
284 		return -1;
285 
286 	if ((mbflag = asn1_tag2charwidth(in->type)) == -1)
287 		return -1;
288 
289 	mbflag |= MBSTRING_FLAG;
290 
291 	ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
292 	    B_ASN1_UTF8STRING);
293 	if (ret < 0)
294 		return ret;
295 	*out = stmp.data;
296 	return stmp.length;
297 }
298 
299 int
300 i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *astr, int type)
301 {
302 	int i, n = 0;
303 	static const char h[] = "0123456789ABCDEF";
304 	char buf[2];
305 
306 	if (astr == NULL)
307 		return 0;
308 
309 	if (astr->length == 0) {
310 		if (BIO_write(bp, "0", 1) != 1)
311 			goto err;
312 		n = 1;
313 	} else {
314 		for (i = 0; i < astr->length; i++) {
315 			if ((i != 0) && (i % 35 == 0)) {
316 				if (BIO_write(bp, "\\\n", 2) != 2)
317 					goto err;
318 				n += 2;
319 			}
320 			buf[0] = h[((unsigned char)astr->data[i] >> 4) & 0x0f];
321 			buf[1] = h[((unsigned char)astr->data[i]) & 0x0f];
322 			if (BIO_write(bp, buf, 2) != 2)
323 				goto err;
324 			n += 2;
325 		}
326 	}
327 	return n;
328 
329  err:
330 	return -1;
331 }
332 
333 int
334 a2i_ASN1_STRING(BIO *bp, ASN1_STRING *astr, char *buf, int size)
335 {
336 	int ret = 0;
337 	int i, j, k, m, n, again, bufsize;
338 	unsigned char *s = NULL, *sp;
339 	unsigned char *bufp;
340 	int first = 1;
341 	size_t num = 0, slen = 0;
342 
343 	bufsize = BIO_gets(bp, buf, size);
344 	for (;;) {
345 		if (bufsize < 1) {
346 			if (first)
347 				break;
348 			else
349 				goto err_sl;
350 		}
351 		first = 0;
352 
353 		i = bufsize;
354 		if (buf[i-1] == '\n')
355 			buf[--i] = '\0';
356 		if (i == 0)
357 			goto err_sl;
358 		if (buf[i-1] == '\r')
359 			buf[--i] = '\0';
360 		if (i == 0)
361 			goto err_sl;
362 		if (buf[i - 1] == '\\') {
363 			i--;
364 			again = 1;
365 		} else
366 			again = 0;
367 		buf[i] = '\0';
368 		if (i < 2)
369 			goto err_sl;
370 
371 		bufp = (unsigned char *)buf;
372 
373 		k = 0;
374 		if (i % 2 != 0) {
375 			ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS);
376 			goto err;
377 		}
378 		i /= 2;
379 		if (num + i > slen) {
380 			sp = realloc(s, num + i);
381 			if (sp == NULL) {
382 				ASN1error(ERR_R_MALLOC_FAILURE);
383 				goto err;
384 			}
385 			s = sp;
386 			slen = num + i;
387 		}
388 		for (j = 0; j < i; j++, k += 2) {
389 			for (n = 0; n < 2; n++) {
390 				m = bufp[k + n];
391 				if ((m >= '0') && (m <= '9'))
392 					m -= '0';
393 				else if ((m >= 'a') && (m <= 'f'))
394 					m = m - 'a' + 10;
395 				else if ((m >= 'A') && (m <= 'F'))
396 					m = m - 'A' + 10;
397 				else {
398 					ASN1error(ASN1_R_NON_HEX_CHARACTERS);
399 					goto err;
400 				}
401 				s[num + j] <<= 4;
402 				s[num + j] |= m;
403 			}
404 		}
405 		num += i;
406 		if (again)
407 			bufsize = BIO_gets(bp, buf, size);
408 		else
409 			break;
410 	}
411 	astr->length = num;
412 	astr->data = s;
413 
414 	return 1;
415 
416  err_sl:
417 	ASN1error(ASN1_R_SHORT_LINE);
418  err:
419 	free(s);
420 
421 	return ret;
422 }
423