a_mbstr.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/bytestring.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 
66 #include "internal.h"
67 #include "../bytestring/internal.h"
68 
69 /*
70  * These functions take a string in UTF8, ASCII or multibyte form and a mask
71  * of permissible ASN1 string types. It then works out the minimal type
72  * (using the order Printable < IA5 < T61 < BMP < Universal < UTF8) and
73  * creates a string of the correct type with the supplied data. Yes this is
74  * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
75  * size limits too.
76  */
77 
78 int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
79  int inform, unsigned long mask)
80 {
81  return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
82 }
83 
84 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_BMPSTRING)
85 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UNIVERSALSTRING)
86 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
87 
89  int inform, unsigned long mask,
90  long minsize, long maxsize)
91 {
92  int str_type;
93  char free_out;
95  size_t nchar = 0;
96  char strbuf[32];
97  if (len == -1)
98  len = strlen((const char *)in);
99  if (!mask)
100  mask = DIRSTRING_TYPE;
101 
102  int (*decode_func)(CBS *, uint32_t*);
103  int error;
104  switch (inform) {
105  case MBSTRING_BMP:
108  break;
109 
110  case MBSTRING_UNIV:
113  break;
114 
115  case MBSTRING_UTF8:
118  break;
119 
120  case MBSTRING_ASC:
122  error = ERR_R_INTERNAL_ERROR; // Latin-1 inputs are never invalid.
123  break;
124 
125  default:
127  return -1;
128  }
129 
130  /* Check |minsize| and |maxsize| and work out the minimal type, if any. */
131  CBS cbs;
132  CBS_init(&cbs, in, len);
133  size_t utf8_len = 0;
134  while (CBS_len(&cbs) != 0) {
135  uint32_t c;
136  if (!decode_func(&cbs, &c)) {
137  OPENSSL_PUT_ERROR(ASN1, error);
138  return -1;
139  }
140  if (nchar == 0 &&
141  (inform == MBSTRING_BMP || inform == MBSTRING_UNIV) &&
142  c == 0xfeff) {
143  /* Reject byte-order mark. We could drop it but that would mean
144  * adding ambiguity around whether a BOM was included or not when
145  * matching strings.
146  *
147  * For a little-endian UCS-2 string, the BOM will appear as 0xfffe
148  * and will be rejected as noncharacter, below. */
150  return -1;
151  }
152 
153  /* Update which output formats are still possible. */
154  if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
155  mask &= ~B_ASN1_PRINTABLESTRING;
156  }
157  if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
158  mask &= ~B_ASN1_IA5STRING;
159  }
160  if ((mask & B_ASN1_T61STRING) && (c > 0xff)) {
161  mask &= ~B_ASN1_T61STRING;
162  }
163  if ((mask & B_ASN1_BMPSTRING) && (c > 0xffff)) {
164  mask &= ~B_ASN1_BMPSTRING;
165  }
166  if (!mask) {
168  return -1;
169  }
170 
171  nchar++;
172  utf8_len += cbb_get_utf8_len(c);
173  }
174 
175  if (minsize > 0 && nchar < (size_t)minsize) {
177  BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
178  ERR_add_error_data(2, "minsize=", strbuf);
179  return -1;
180  }
181 
182  if (maxsize > 0 && nchar > (size_t)maxsize) {
184  BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
185  ERR_add_error_data(2, "maxsize=", strbuf);
186  return -1;
187  }
188 
189  /* Now work out output format and string type */
190  int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
191  size_t size_estimate = nchar;
192  int outform = MBSTRING_ASC;
193  if (mask & B_ASN1_PRINTABLESTRING) {
194  str_type = V_ASN1_PRINTABLESTRING;
195  } else if (mask & B_ASN1_IA5STRING) {
196  str_type = V_ASN1_IA5STRING;
197  } else if (mask & B_ASN1_T61STRING) {
198  str_type = V_ASN1_T61STRING;
199  } else if (mask & B_ASN1_BMPSTRING) {
200  str_type = V_ASN1_BMPSTRING;
201  outform = MBSTRING_BMP;
202  encode_func = cbb_add_ucs2_be;
203  size_estimate = 2 * nchar;
204  } else if (mask & B_ASN1_UNIVERSALSTRING) {
205  str_type = V_ASN1_UNIVERSALSTRING;
206  encode_func = cbb_add_utf32_be;
207  size_estimate = 4 * nchar;
208  outform = MBSTRING_UNIV;
209  } else if (mask & B_ASN1_UTF8STRING) {
210  str_type = V_ASN1_UTF8STRING;
211  outform = MBSTRING_UTF8;
212  encode_func = cbb_add_utf8;
213  size_estimate = utf8_len;
214  } else {
216  return -1;
217  }
218 
219  if (!out)
220  return str_type;
221  if (*out) {
222  free_out = 0;
223  dest = *out;
224  if (dest->data) {
225  dest->length = 0;
226  OPENSSL_free(dest->data);
227  dest->data = NULL;
228  }
229  dest->type = str_type;
230  } else {
231  free_out = 1;
232  dest = ASN1_STRING_type_new(str_type);
233  if (!dest) {
235  return -1;
236  }
237  *out = dest;
238  }
239 
240  /* If both the same type just copy across */
241  if (inform == outform) {
242  if (!ASN1_STRING_set(dest, in, len)) {
244  return -1;
245  }
246  return str_type;
247  }
248 
249  CBB cbb;
250  if (!CBB_init(&cbb, size_estimate + 1)) {
252  goto err;
253  }
254  CBS_init(&cbs, in, len);
255  while (CBS_len(&cbs) != 0) {
256  uint32_t c;
257  if (!decode_func(&cbs, &c) ||
258  !encode_func(&cbb, c)) {
260  goto err;
261  }
262  }
263  uint8_t *data = NULL;
264  size_t data_len;
265  if (/* OpenSSL historically NUL-terminated this value with a single byte,
266  * even for |MBSTRING_BMP| and |MBSTRING_UNIV|. */
267  !CBB_add_u8(&cbb, 0) ||
268  !CBB_finish(&cbb, &data, &data_len) ||
269  data_len < 1 ||
270  data_len > INT_MAX) {
273  goto err;
274  }
275  dest->length = (int)(data_len - 1);
276  dest->data = data;
277  return str_type;
278 
279  err:
280  if (free_out)
282  CBB_cleanup(&cbb);
283  return -1;
284 }
285 
287 {
288  if (value > 0x7f) {
289  return 0;
290  }
291  /* Note we cannot use |isalnum| because it is locale-dependent. */
292  return ('a' <= value && value <= 'z') || //
293  ('A' <= value && value <= 'Z') || //
294  ('0' <= value && value <= '9') || //
295  value == ' ' || value == '\'' || value == '(' || value == ')' ||
296  value == '+' || value == ',' || value == '-' || value == '.' ||
297  value == '/' || value == ':' || value == '=' || value == '?';
298 }
ASN1_mbstring_ncopy
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len, int inform, unsigned long mask, long minsize, long maxsize)
Definition: a_mbstr.c:88
DIRSTRING_TYPE
#define DIRSTRING_TYPE
Definition: asn1.h:728
cbs_get_latin1
#define cbs_get_latin1
Definition: boringssl_prefix_symbols.h:2929
CBB_init
#define CBB_init
Definition: boringssl_prefix_symbols.h:1047
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
cbb_get_utf8_len
#define cbb_get_utf8_len
Definition: boringssl_prefix_symbols.h:2928
cbs_st
Definition: bytestring.h:39
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
CBB_cleanup
#define CBB_cleanup
Definition: boringssl_prefix_symbols.h:1039
V_ASN1_PRINTABLESTRING
#define V_ASN1_PRINTABLESTRING
Definition: asn1.h:139
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
CBB_add_u8
#define CBB_add_u8
Definition: boringssl_prefix_symbols.h:1036
ASN1_STRING_type_new
#define ASN1_STRING_type_new
Definition: boringssl_prefix_symbols.h:695
error
grpc_error_handle error
Definition: retry_filter.cc:499
error_ref_leak.err
err
Definition: error_ref_leak.py:35
_gevent_test_main.maxsize
maxsize
Definition: _gevent_test_main.py:26
V_ASN1_UTF8STRING
#define V_ASN1_UTF8STRING
Definition: asn1.h:135
B_ASN1_IA5STRING
#define B_ASN1_IA5STRING
Definition: asn1.h:165
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
ASN1_STRING_free
#define ASN1_STRING_free
Definition: boringssl_prefix_symbols.h:680
MBSTRING_UNIV
#define MBSTRING_UNIV
Definition: asn1.h:725
B_ASN1_PRINTABLESTRING
#define B_ASN1_PRINTABLESTRING
Definition: asn1.h:161
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
ASN1_R_INVALID_UNIVERSALSTRING
#define ASN1_R_INVALID_UNIVERSALSTRING
Definition: asn1.h:1992
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
decode_func
int(* decode_func)(BIGNUM *bn, const char *in, int in_len)
Definition: boringssl-with-bazel/src/crypto/bn_extra/convert.c:187
ASN1_R_STRING_TOO_LONG
#define ASN1_R_STRING_TOO_LONG
Definition: asn1.h:2016
cbb_add_ucs2_be
#define cbb_add_ucs2_be
Definition: boringssl_prefix_symbols.h:2925
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
B_ASN1_T61STRING
#define B_ASN1_T61STRING
Definition: asn1.h:162
bytestring.h
B_ASN1_UNIVERSALSTRING
#define B_ASN1_UNIVERSALSTRING
Definition: asn1.h:170
ASN1_R_UNKNOWN_FORMAT
#define ASN1_R_UNKNOWN_FORMAT
Definition: asn1.h:2025
V_ASN1_BMPSTRING
#define V_ASN1_BMPSTRING
Definition: asn1.h:151
ASN1_STRING_set
#define ASN1_STRING_set
Definition: boringssl_prefix_symbols.h:688
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
internal.h
xds_interop_client.int
int
Definition: xds_interop_client.py:113
cbb_add_utf32_be
#define cbb_add_utf32_be
Definition: boringssl_prefix_symbols.h:2926
CBB_finish
#define CBB_finish
Definition: boringssl_prefix_symbols.h:1043
MBSTRING_BMP
#define MBSTRING_BMP
Definition: asn1.h:724
err.h
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
MBSTRING_UTF8
#define MBSTRING_UTF8
Definition: asn1.h:722
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
OPENSSL_DECLARE_ERROR_REASON
#define OPENSSL_DECLARE_ERROR_REASON(lib, reason)
Definition: err.h:459
ASN1_R_ILLEGAL_CHARACTERS
#define ASN1_R_ILLEGAL_CHARACTERS
Definition: asn1.h:1969
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
BIO_snprintf
#define BIO_snprintf
Definition: boringssl_prefix_symbols.h:864
ASN1_R_INVALID_BMPSTRING
#define ASN1_R_INVALID_BMPSTRING
Definition: asn1.h:1985
V_ASN1_IA5STRING
#define V_ASN1_IA5STRING
Definition: asn1.h:143
V_ASN1_T61STRING
#define V_ASN1_T61STRING
Definition: asn1.h:140
value
const char * value
Definition: hpack_parser_table.cc:165
ERR_add_error_data
#define ERR_add_error_data
Definition: boringssl_prefix_symbols.h:1411
MBSTRING_ASC
#define MBSTRING_ASC
Definition: asn1.h:723
ASN1_mbstring_copy
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len, int inform, unsigned long mask)
Definition: a_mbstr.c:78
cbb_add_latin1
#define cbb_add_latin1
Definition: boringssl_prefix_symbols.h:2924
cbb_add_utf8
#define cbb_add_utf8
Definition: boringssl_prefix_symbols.h:2927
B_ASN1_BMPSTRING
#define B_ASN1_BMPSTRING
Definition: asn1.h:173
ASN1_R_STRING_TOO_SHORT
#define ASN1_R_STRING_TOO_SHORT
Definition: asn1.h:2017
asn1_is_printable
int asn1_is_printable(uint32_t value)
Definition: a_mbstr.c:286
ASN1_R_INVALID_UTF8STRING
#define ASN1_R_INVALID_UTF8STRING
Definition: asn1.h:1993
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
cbs_get_utf8
#define cbs_get_utf8
Definition: boringssl_prefix_symbols.h:2932
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
cbs_get_utf32_be
#define cbs_get_utf32_be
Definition: boringssl_prefix_symbols.h:2931
V_ASN1_UNIVERSALSTRING
#define V_ASN1_UNIVERSALSTRING
Definition: asn1.h:150
asn1_string_st
Definition: asn1.h:543
asn1.h
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
B_ASN1_UTF8STRING
#define B_ASN1_UTF8STRING
Definition: asn1.h:175
cbs_get_ucs2_be
#define cbs_get_ucs2_be
Definition: boringssl_prefix_symbols.h:2930
cbb_st
Definition: bytestring.h:375


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:39