OpenCoverage

a_mbstr.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/asn1/a_mbstr.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1999-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/ctype.h"-
12#include "internal/cryptlib.h"-
13#include <openssl/asn1.h>-
14-
15static int traverse_string(const unsigned char *p, int len, int inform,-
16 int (*rfunc) (unsigned long value, void *in),-
17 void *arg);-
18static int in_utf8(unsigned long value, void *arg);-
19static int out_utf8(unsigned long value, void *arg);-
20static int type_str(unsigned long value, void *arg);-
21static int cpy_asc(unsigned long value, void *arg);-
22static int cpy_bmp(unsigned long value, void *arg);-
23static int cpy_univ(unsigned long value, void *arg);-
24static int cpy_utf8(unsigned long value, void *arg);-
25-
26/*-
27 * These functions take a string in UTF8, ASCII or multibyte form and a mask-
28 * of permissible ASN1 string types. It then works out the minimal type-
29 * (using the order Numeric < Printable < IA5 < T61 < BMP < Universal < UTF8)-
30 * and creates a string of the correct type with the supplied data. Yes this is-
31 * horrible: it has to be :-( The 'ncopy' form checks minimum and maximum-
32 * size limits too.-
33 */-
34-
35int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,-
36 int inform, unsigned long mask)-
37{-
38 return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
executed 154154 times by 1 test: return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
Executed by:
  • libcrypto.so.1.1
154154
39}-
40-
41int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,-
42 int inform, unsigned long mask,-
43 long minsize, long maxsize)-
44{-
45 int str_type;-
46 int ret;-
47 char free_out;-
48 int outform, outlen = 0;-
49 ASN1_STRING *dest;-
50 unsigned char *p;-
51 int nchar;-
52 char strbuf[32];-
53 int (*cpyfunc) (unsigned long, void *) = NULL;-
54 if (len == -1)
len == -1Description
TRUEevaluated 765 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 154154 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
765-154154
55 len = strlen((const char *)in);
executed 765 times by 1 test: len = strlen((const char *)in);
Executed by:
  • libcrypto.so.1.1
765
56 if (!mask)
!maskDescription
TRUEnever evaluated
FALSEevaluated 154919 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-154919
57 mask = DIRSTRING_TYPE;
never executed: mask = (0x0002|0x0004|0x0800|0x2000);
0
58-
59 /* First do a string check and work out the number of characters */-
60 switch (inform) {-
61-
62 case MBSTRING_BMP:
executed 7094 times by 1 test: case (0x1000|2):
Executed by:
  • libcrypto.so.1.1
7094
63 if (len & 1) {
len & 1Description
TRUEnever evaluated
FALSEevaluated 7094 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-7094
64 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,-
65 ASN1_R_INVALID_BMPSTRING_LENGTH);-
66 return -1;
never executed: return -1;
0
67 }-
68 nchar = len >> 1;-
69 break;
executed 7094 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
7094
70-
71 case MBSTRING_UNIV:
executed 7520 times by 1 test: case (0x1000|4):
Executed by:
  • libcrypto.so.1.1
7520
72 if (len & 3) {
len & 3Description
TRUEnever evaluated
FALSEevaluated 7520 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-7520
73 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,-
74 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);-
75 return -1;
never executed: return -1;
0
76 }-
77 nchar = len >> 2;-
78 break;
executed 7520 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
7520
79-
80 case MBSTRING_UTF8:
executed 85293 times by 1 test: case (0x1000):
Executed by:
  • libcrypto.so.1.1
85293
81 nchar = 0;-
82 /* This counts the characters and does utf8 syntax checking */-
83 ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);-
84 if (ret < 0) {
ret < 0Description
TRUEevaluated 4656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 80637 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
4656-80637
85 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_UTF8STRING);-
86 return -1;
executed 4656 times by 1 test: return -1;
Executed by:
  • libcrypto.so.1.1
4656
87 }-
88 break;
executed 80637 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
80637
89-
90 case MBSTRING_ASC:
executed 55012 times by 1 test: case (0x1000|1):
Executed by:
  • libcrypto.so.1.1
55012
91 nchar = len;-
92 break;
executed 55012 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
55012
93-
94 default:
never executed: default:
0
95 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);-
96 return -1;
never executed: return -1;
0
97 }-
98-
99 if ((minsize > 0) && (nchar < minsize)) {
(minsize > 0)Description
TRUEevaluated 765 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 149498 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(nchar < minsize)Description
TRUEnever evaluated
FALSEevaluated 765 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-149498
100 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);-
101 BIO_snprintf(strbuf, sizeof(strbuf), "%ld", minsize);-
102 ERR_add_error_data(2, "minsize=", strbuf);-
103 return -1;
never executed: return -1;
0
104 }-
105-
106 if ((maxsize > 0) && (nchar > maxsize)) {
(maxsize > 0)Description
TRUEevaluated 765 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 149498 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(nchar > maxsize)Description
TRUEnever evaluated
FALSEevaluated 765 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-149498
107 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);-
108 BIO_snprintf(strbuf, sizeof(strbuf), "%ld", maxsize);-
109 ERR_add_error_data(2, "maxsize=", strbuf);-
110 return -1;
never executed: return -1;
0
111 }-
112-
113 /* Now work out minimal type (if any) */-
114 if (traverse_string(in, len, inform, type_str, &mask) < 0) {
traverse_strin...tr, &mask) < 0Description
TRUEnever evaluated
FALSEevaluated 150263 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-150263
115 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);-
116 return -1;
never executed: return -1;
0
117 }-
118-
119 /* Now work out output format and string type */-
120 outform = MBSTRING_ASC;-
121 if (mask & B_ASN1_NUMERICSTRING)
mask & 0x0001Description
TRUEnever evaluated
FALSEevaluated 150263 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-150263
122 str_type = V_ASN1_NUMERICSTRING;
never executed: str_type = 18;
0
123 else if (mask & B_ASN1_PRINTABLESTRING)
mask & 0x0002Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 150252 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
11-150252
124 str_type = V_ASN1_PRINTABLESTRING;
executed 11 times by 1 test: str_type = 19;
Executed by:
  • libcrypto.so.1.1
11
125 else if (mask & B_ASN1_IA5STRING)
mask & 0x0010Description
TRUEevaluated 359 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 149893 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
359-149893
126 str_type = V_ASN1_IA5STRING;
executed 359 times by 1 test: str_type = 22;
Executed by:
  • libcrypto.so.1.1
359
127 else if (mask & B_ASN1_T61STRING)
mask & 0x0004Description
TRUEnever evaluated
FALSEevaluated 149893 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-149893
128 str_type = V_ASN1_T61STRING;
never executed: str_type = 20;
0
129 else if (mask & B_ASN1_BMPSTRING) {
mask & 0x0800Description
TRUEnever evaluated
FALSEevaluated 149893 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-149893
130 str_type = V_ASN1_BMPSTRING;-
131 outform = MBSTRING_BMP;-
132 } else if (mask & B_ASN1_UNIVERSALSTRING) {
never executed: end of block
mask & 0x0100Description
TRUEnever evaluated
FALSEevaluated 149893 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-149893
133 str_type = V_ASN1_UNIVERSALSTRING;-
134 outform = MBSTRING_UNIV;-
135 } else {
never executed: end of block
0
136 str_type = V_ASN1_UTF8STRING;-
137 outform = MBSTRING_UTF8;-
138 }
executed 149893 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
149893
139 if (!out)
!outDescription
TRUEnever evaluated
FALSEevaluated 150263 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-150263
140 return str_type;
never executed: return str_type;
0
141 if (*out) {
*outDescription
TRUEevaluated 150263 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-150263
142 free_out = 0;-
143 dest = *out;-
144 OPENSSL_free(dest->data);-
145 dest->data = NULL;-
146 dest->length = 0;-
147 dest->type = str_type;-
148 } else {
executed 150263 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
150263
149 free_out = 1;-
150 dest = ASN1_STRING_type_new(str_type);-
151 if (dest == NULL) {
dest == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
152 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);-
153 return -1;
never executed: return -1;
0
154 }-
155 *out = dest;-
156 }
never executed: end of block
0
157 /* If both the same type just copy across */-
158 if (inform == outform) {
inform == outformDescription
TRUEevaluated 81007 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 69256 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
69256-81007
159 if (!ASN1_STRING_set(dest, in, len)) {
!ASN1_STRING_s...dest, in, len)Description
TRUEnever evaluated
FALSEevaluated 81007 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-81007
160 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);-
161 return -1;
never executed: return -1;
0
162 }-
163 return str_type;
executed 81007 times by 1 test: return str_type;
Executed by:
  • libcrypto.so.1.1
81007
164 }-
165-
166 /* Work out how much space the destination will need */-
167 switch (outform) {-
168 case MBSTRING_ASC:
never executed: case (0x1000|1):
0
169 outlen = nchar;-
170 cpyfunc = cpy_asc;-
171 break;
never executed: break;
0
172-
173 case MBSTRING_BMP:
never executed: case (0x1000|2):
0
174 outlen = nchar << 1;-
175 cpyfunc = cpy_bmp;-
176 break;
never executed: break;
0
177-
178 case MBSTRING_UNIV:
never executed: case (0x1000|4):
0
179 outlen = nchar << 2;-
180 cpyfunc = cpy_univ;-
181 break;
never executed: break;
0
182-
183 case MBSTRING_UTF8:
executed 69256 times by 1 test: case (0x1000):
Executed by:
  • libcrypto.so.1.1
69256
184 outlen = 0;-
185 traverse_string(in, len, inform, out_utf8, &outlen);-
186 cpyfunc = cpy_utf8;-
187 break;
executed 69256 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
69256
188 }-
189 if ((p = OPENSSL_malloc(outlen + 1)) == NULL) {
(p = CRYPTO_ma...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 69256 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-69256
190 if (free_out)
free_outDescription
TRUEnever evaluated
FALSEnever evaluated
0
191 ASN1_STRING_free(dest);
never executed: ASN1_STRING_free(dest);
0
192 ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE);-
193 return -1;
never executed: return -1;
0
194 }-
195 dest->length = outlen;-
196 dest->data = p;-
197 p[outlen] = 0;-
198 traverse_string(in, len, inform, cpyfunc, &p);-
199 return str_type;
executed 69256 times by 1 test: return str_type;
Executed by:
  • libcrypto.so.1.1
69256
200}-
201-
202/*-
203 * This function traverses a string and passes the value of each character to-
204 * an optional function along with a void * argument.-
205 */-
206-
207static int traverse_string(const unsigned char *p, int len, int inform,-
208 int (*rfunc) (unsigned long value, void *in),-
209 void *arg)-
210{-
211 unsigned long value;-
212 int ret;-
213 while (len) {
lenDescription
TRUEevaluated 3369336 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 369412 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
369412-3369336
214 if (inform == MBSTRING_ASC) {
inform == (0x1000|1)Description
TRUEevaluated 1638770 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1730566 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1638770-1730566
215 value = *p++;-
216 len--;-
217 } else if (inform == MBSTRING_BMP) {
executed 1638770 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
inform == (0x1000|2)Description
TRUEevaluated 54267 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1676299 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
54267-1676299
218 value = *p++ << 8;-
219 value |= *p++;-
220 len -= 2;-
221 } else if (inform == MBSTRING_UNIV) {
executed 54267 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
inform == (0x1000|4)Description
TRUEevaluated 22413 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1653886 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
22413-1653886
222 value = ((unsigned long)*p++) << 24;-
223 value |= ((unsigned long)*p++) << 16;-
224 value |= *p++ << 8;-
225 value |= *p++;-
226 len -= 4;-
227 } else {
executed 22413 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
22413
228 ret = UTF8_getc(p, len, &value);-
229 if (ret < 0)
ret < 0Description
TRUEevaluated 4656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1649230 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
4656-1649230
230 return -1;
executed 4656 times by 1 test: return -1;
Executed by:
  • libcrypto.so.1.1
4656
231 len -= ret;-
232 p += ret;-
233 }
executed 1649230 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1649230
234 if (rfunc) {
rfuncDescription
TRUEevaluated 3364680 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-3364680
235 ret = rfunc(value, arg);-
236 if (ret <= 0)
ret <= 0Description
TRUEnever evaluated
FALSEevaluated 3364680 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-3364680
237 return ret;
never executed: return ret;
0
238 }
executed 3364680 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
3364680
239 }
executed 3364680 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
3364680
240 return 1;
executed 369412 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
369412
241}-
242-
243/* Various utility functions for traverse_string */-
244-
245/* Just count number of characters */-
246-
247static int in_utf8(unsigned long value, void *arg)-
248{-
249 int *nchar;-
250 nchar = arg;-
251 (*nchar)++;-
252 return 1;
executed 833749 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
833749
253}-
254-
255/* Determine size of output as a UTF8 String */-
256-
257static int out_utf8(unsigned long value, void *arg)-
258{-
259 int *outlen;-
260 outlen = arg;-
261 *outlen += UTF8_putc(NULL, -1, value);-
262 return 1;
executed 570249 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
570249
263}-
264-
265/*-
266 * Determine the "type" of a string: check each character against a supplied-
267 * "mask".-
268 */-
269-
270static int type_str(unsigned long value, void *arg)-
271{-
272 unsigned long types = *((unsigned long *)arg);-
273 const int native = value > INT_MAX ? INT_MAX : ossl_fromascii(value);
value > 0x7fffffffDescription
TRUEevaluated 1460 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1388973 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1460-1388973
274-
275 if ((types & B_ASN1_NUMERICSTRING) && !(ossl_isdigit(native)
(types & 0x0001)Description
TRUEnever evaluated
FALSEevaluated 1390433 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(ossl_ctype_ch...native), 0x4))Description
TRUEnever evaluated
FALSEnever evaluated
0-1390433
276 || native == ' '))
native == ' 'Description
TRUEnever evaluated
FALSEnever evaluated
0
277 types &= ~B_ASN1_NUMERICSTRING;
never executed: types &= ~0x0001;
0
278 if ((types & B_ASN1_PRINTABLESTRING) && !ossl_isasn1print(native))
(types & 0x0002)Description
TRUEevaluated 22 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1390411 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
!(ossl_ctype_c...tive), 0x800))Description
TRUEnever evaluated
FALSEevaluated 22 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1390411
279 types &= ~B_ASN1_PRINTABLESTRING;
never executed: types &= ~0x0002;
0
280 if ((types & B_ASN1_IA5STRING) && !ossl_isascii(native))
(types & 0x0010)Description
TRUEevaluated 4681 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1385752 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
!(((native) & ~127) == 0)Description
TRUEnever evaluated
FALSEevaluated 4681 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1385752
281 types &= ~B_ASN1_IA5STRING;
never executed: types &= ~0x0010;
0
282 if ((types & B_ASN1_T61STRING) && (value > 0xff))
(types & 0x0004)Description
TRUEnever evaluated
FALSEevaluated 1390433 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(value > 0xff)Description
TRUEnever evaluated
FALSEnever evaluated
0-1390433
283 types &= ~B_ASN1_T61STRING;
never executed: types &= ~0x0004;
0
284 if ((types & B_ASN1_BMPSTRING) && (value > 0xffff))
(types & 0x0800)Description
TRUEnever evaluated
FALSEevaluated 1390433 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(value > 0xffff)Description
TRUEnever evaluated
FALSEnever evaluated
0-1390433
285 types &= ~B_ASN1_BMPSTRING;
never executed: types &= ~0x0800;
0
286 if (!types)
!typesDescription
TRUEnever evaluated
FALSEevaluated 1390433 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1390433
287 return -1;
never executed: return -1;
0
288 *((unsigned long *)arg) = types;-
289 return 1;
executed 1390433 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
1390433
290}-
291-
292/* Copy one byte per character ASCII like strings */-
293-
294static int cpy_asc(unsigned long value, void *arg)-
295{-
296 unsigned char **p, *q;-
297 p = arg;-
298 q = *p;-
299 *q = (unsigned char)value;-
300 (*p)++;-
301 return 1;
never executed: return 1;
0
302}-
303-
304/* Copy two byte per character BMPStrings */-
305-
306static int cpy_bmp(unsigned long value, void *arg)-
307{-
308 unsigned char **p, *q;-
309 p = arg;-
310 q = *p;-
311 *q++ = (unsigned char)((value >> 8) & 0xff);-
312 *q = (unsigned char)(value & 0xff);-
313 *p += 2;-
314 return 1;
never executed: return 1;
0
315}-
316-
317/* Copy four byte per character UniversalStrings */-
318-
319static int cpy_univ(unsigned long value, void *arg)-
320{-
321 unsigned char **p, *q;-
322 p = arg;-
323 q = *p;-
324 *q++ = (unsigned char)((value >> 24) & 0xff);-
325 *q++ = (unsigned char)((value >> 16) & 0xff);-
326 *q++ = (unsigned char)((value >> 8) & 0xff);-
327 *q = (unsigned char)(value & 0xff);-
328 *p += 4;-
329 return 1;
never executed: return 1;
0
330}-
331-
332/* Copy to a UTF8String */-
333-
334static int cpy_utf8(unsigned long value, void *arg)-
335{-
336 unsigned char **p;-
337 int ret;-
338 p = arg;-
339 /* We already know there is enough room so pass 0xff as the length */-
340 ret = UTF8_putc(*p, 0xff, value);-
341 *p += ret;-
342 return 1;
executed 570249 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
570249
343}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2