xref: /openbsd-src/lib/libcrypto/asn1/a_string.c (revision acf644016ec1190723fc541ba590471e90a9ef53)
1 /* $OpenBSD: a_string.c,v 1.14 2023/07/05 21:23:36 beck 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_local.h"
67 
68 ASN1_STRING *
69 ASN1_STRING_new(void)
70 {
71 	return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
72 }
73 LCRYPTO_ALIAS(ASN1_STRING_new);
74 
75 ASN1_STRING *
76 ASN1_STRING_type_new(int type)
77 {
78 	ASN1_STRING *astr;
79 
80 	if ((astr = calloc(1, sizeof(ASN1_STRING))) == NULL) {
81 		ASN1error(ERR_R_MALLOC_FAILURE);
82 		return NULL;
83 	}
84 	astr->type = type;
85 
86 	return astr;
87 }
88 LCRYPTO_ALIAS(ASN1_STRING_type_new);
89 
90 static void
91 ASN1_STRING_clear(ASN1_STRING *astr)
92 {
93 	if (!(astr->flags & ASN1_STRING_FLAG_NDEF))
94 		freezero(astr->data, astr->length);
95 
96 	astr->flags &= ~ASN1_STRING_FLAG_NDEF;
97 	astr->data = NULL;
98 	astr->length = 0;
99 }
100 
101 void
102 ASN1_STRING_free(ASN1_STRING *astr)
103 {
104 	if (astr == NULL)
105 		return;
106 
107 	ASN1_STRING_clear(astr);
108 
109 	free(astr);
110 }
111 LCRYPTO_ALIAS(ASN1_STRING_free);
112 
113 int
114 ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
115 {
116 	int cmp;
117 
118 	if (a == NULL || b == NULL)
119 		return -1;
120 	if ((cmp = (a->length - b->length)) != 0)
121 		return cmp;
122 	if ((cmp = memcmp(a->data, b->data, a->length)) != 0)
123 		return cmp;
124 
125 	return (a->type - b->type);
126 }
127 LCRYPTO_ALIAS(ASN1_STRING_cmp);
128 
129 int
130 ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *src)
131 {
132 	if (src == NULL)
133 		return 0;
134 
135 	if (!ASN1_STRING_set(dst, src->data, src->length))
136 		return 0;
137 
138 	dst->type = src->type;
139 	dst->flags = src->flags & ~ASN1_STRING_FLAG_NDEF;
140 
141 	return 1;
142 }
143 LCRYPTO_ALIAS(ASN1_STRING_copy);
144 
145 ASN1_STRING *
146 ASN1_STRING_dup(const ASN1_STRING *src)
147 {
148 	ASN1_STRING *astr;
149 
150 	if (src == NULL)
151 		return NULL;
152 
153 	if ((astr = ASN1_STRING_new()) == NULL)
154 		return NULL;
155 	if (!ASN1_STRING_copy(astr, src)) {
156 		ASN1_STRING_free(astr);
157 		return NULL;
158 	}
159 	return astr;
160 }
161 LCRYPTO_ALIAS(ASN1_STRING_dup);
162 
163 int
164 ASN1_STRING_set(ASN1_STRING *astr, const void *_data, int len)
165 {
166 	const char *data = _data;
167 
168 	if (len == -1) {
169 		size_t slen;
170 
171 		if (data == NULL)
172 			return 0;
173 
174 		if ((slen = strlen(data)) > INT_MAX)
175 			return 0;
176 
177 		len = (int)slen;
178 	}
179 
180 	ASN1_STRING_clear(astr);
181 
182 	if (len < 0 || len >= INT_MAX)
183 		return 0;
184 
185 	if ((astr->data = calloc(1, len + 1)) == NULL) {
186 		ASN1error(ERR_R_MALLOC_FAILURE);
187 		return (0);
188 	}
189 	astr->length = len;
190 
191 	if (data != NULL) {
192 		memcpy(astr->data, data, len);
193 		astr->data[len] = '\0';
194 	}
195 
196 	return 1;
197 }
198 LCRYPTO_ALIAS(ASN1_STRING_set);
199 
200 void
201 ASN1_STRING_set0(ASN1_STRING *astr, void *data, int len)
202 {
203 	ASN1_STRING_clear(astr);
204 
205 	astr->data = data;
206 	astr->length = len;
207 }
208 LCRYPTO_ALIAS(ASN1_STRING_set0);
209 
210 int
211 ASN1_STRING_length(const ASN1_STRING *astr)
212 {
213 	return astr->length;
214 }
215 LCRYPTO_ALIAS(ASN1_STRING_length);
216 
217 void
218 ASN1_STRING_length_set(ASN1_STRING *astr, int len)
219 {
220 	/* This is dangerous and unfixable. */
221 	astr->length = len;
222 }
223 LCRYPTO_ALIAS(ASN1_STRING_length_set);
224 
225 int
226 ASN1_STRING_type(const ASN1_STRING *astr)
227 {
228 	return astr->type;
229 }
230 LCRYPTO_ALIAS(ASN1_STRING_type);
231 
232 unsigned char *
233 ASN1_STRING_data(ASN1_STRING *astr)
234 {
235 	return astr->data;
236 }
237 LCRYPTO_ALIAS(ASN1_STRING_data);
238 
239 const unsigned char *
240 ASN1_STRING_get0_data(const ASN1_STRING *astr)
241 {
242 	return astr->data;
243 }
244 LCRYPTO_ALIAS(ASN1_STRING_get0_data);
245 
246 int
247 ASN1_STRING_print(BIO *bp, const ASN1_STRING *astr)
248 {
249 	int i, n;
250 	char buf[80];
251 	const char *p;
252 
253 	if (astr == NULL)
254 		return 0;
255 
256 	n = 0;
257 	p = (const char *)astr->data;
258 	for (i = 0; i < astr->length; i++) {
259 		if ((p[i] > '~') || ((p[i] < ' ') &&
260 		    (p[i] != '\n') && (p[i] != '\r')))
261 			buf[n] = '.';
262 		else
263 			buf[n] = p[i];
264 		n++;
265 		if (n >= 80) {
266 			if (BIO_write(bp, buf, n) <= 0)
267 				return 0;
268 			n = 0;
269 		}
270 	}
271 	if (n > 0) {
272 		if (BIO_write(bp, buf, n) <= 0)
273 			return 0;
274 	}
275 
276 	return 1;
277 }
278 LCRYPTO_ALIAS(ASN1_STRING_print);
279 
280 /*
281  * Utility function: convert any string type to UTF8, returns number of bytes
282  * in output string or a negative error code
283  */
284 int
285 ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
286 {
287 	ASN1_STRING *astr = NULL;
288 	int mbflag;
289 	int ret = -1;
290 
291 	/*
292 	 * XXX We can't fail on *out != NULL here since things like haproxy and
293 	 * grpc pass in a pointer to an uninitialized pointer on the stack.
294 	 */
295 	if (out == NULL)
296 		goto err;
297 
298 	if (in == NULL)
299 		goto err;
300 
301 	if ((mbflag = asn1_tag2charwidth(in->type)) == -1)
302 		goto err;
303 
304 	mbflag |= MBSTRING_FLAG;
305 
306 	if ((ret = ASN1_mbstring_copy(&astr, in->data, in->length, mbflag,
307 	    B_ASN1_UTF8STRING)) < 0)
308 		goto err;
309 
310 	*out = astr->data;
311 	ret = astr->length;
312 
313 	astr->data = NULL;
314 	astr->length = 0;
315 
316  err:
317 	ASN1_STRING_free(astr);
318 
319 	return ret;
320 }
321 LCRYPTO_ALIAS(ASN1_STRING_to_UTF8);
322 
323 int
324 i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *astr, int type)
325 {
326 	int i, n = 0;
327 	static const char h[] = "0123456789ABCDEF";
328 	char buf[2];
329 
330 	if (astr == NULL)
331 		return 0;
332 
333 	if (astr->length == 0) {
334 		if (BIO_write(bp, "0", 1) != 1)
335 			goto err;
336 		n = 1;
337 	} else {
338 		for (i = 0; i < astr->length; i++) {
339 			if ((i != 0) && (i % 35 == 0)) {
340 				if (BIO_write(bp, "\\\n", 2) != 2)
341 					goto err;
342 				n += 2;
343 			}
344 			buf[0] = h[((unsigned char)astr->data[i] >> 4) & 0x0f];
345 			buf[1] = h[((unsigned char)astr->data[i]) & 0x0f];
346 			if (BIO_write(bp, buf, 2) != 2)
347 				goto err;
348 			n += 2;
349 		}
350 	}
351 	return n;
352 
353  err:
354 	return -1;
355 }
356 LCRYPTO_ALIAS(i2a_ASN1_STRING);
357 
358 int
359 a2i_ASN1_STRING(BIO *bp, ASN1_STRING *astr, char *buf, int size)
360 {
361 	int ret = 0;
362 	int i, j, k, m, n, again, bufsize;
363 	unsigned char *s = NULL, *sp;
364 	unsigned char *bufp;
365 	int first = 1;
366 	size_t num = 0, slen = 0;
367 
368 	bufsize = BIO_gets(bp, buf, size);
369 	for (;;) {
370 		if (bufsize < 1) {
371 			if (first)
372 				break;
373 			else
374 				goto err_sl;
375 		}
376 		first = 0;
377 
378 		i = bufsize;
379 		if (buf[i-1] == '\n')
380 			buf[--i] = '\0';
381 		if (i == 0)
382 			goto err_sl;
383 		if (buf[i-1] == '\r')
384 			buf[--i] = '\0';
385 		if (i == 0)
386 			goto err_sl;
387 		if (buf[i - 1] == '\\') {
388 			i--;
389 			again = 1;
390 		} else
391 			again = 0;
392 		buf[i] = '\0';
393 		if (i < 2)
394 			goto err_sl;
395 
396 		bufp = (unsigned char *)buf;
397 
398 		k = 0;
399 		if (i % 2 != 0) {
400 			ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS);
401 			goto err;
402 		}
403 		i /= 2;
404 		if (num + i > slen) {
405 			sp = realloc(s, num + i);
406 			if (sp == NULL) {
407 				ASN1error(ERR_R_MALLOC_FAILURE);
408 				goto err;
409 			}
410 			s = sp;
411 			slen = num + i;
412 		}
413 		for (j = 0; j < i; j++, k += 2) {
414 			for (n = 0; n < 2; n++) {
415 				m = bufp[k + n];
416 				if ((m >= '0') && (m <= '9'))
417 					m -= '0';
418 				else if ((m >= 'a') && (m <= 'f'))
419 					m = m - 'a' + 10;
420 				else if ((m >= 'A') && (m <= 'F'))
421 					m = m - 'A' + 10;
422 				else {
423 					ASN1error(ASN1_R_NON_HEX_CHARACTERS);
424 					goto err;
425 				}
426 				s[num + j] <<= 4;
427 				s[num + j] |= m;
428 			}
429 		}
430 		num += i;
431 		if (again)
432 			bufsize = BIO_gets(bp, buf, size);
433 		else
434 			break;
435 	}
436 	astr->length = num;
437 	astr->data = s;
438 
439 	return 1;
440 
441  err_sl:
442 	ASN1error(ASN1_R_SHORT_LINE);
443  err:
444 	free(s);
445 
446 	return ret;
447 }
448 LCRYPTO_ALIAS(a2i_ASN1_STRING);
449