Coverage Report

Created: 2022-08-24 06:31

/src/libressl/crypto/asn1/a_strnid.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: a_strnid.c,v 1.25 2021/12/13 17:55:53 schwarze Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999 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 <errno.h>
60
#include <limits.h>
61
#include <stdlib.h>
62
#include <string.h>
63
64
#include <openssl/asn1.h>
65
#include <openssl/err.h>
66
#include <openssl/objects.h>
67
68
static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
69
70
static ASN1_STRING_TABLE *stable_get(int nid);
71
static void st_free(ASN1_STRING_TABLE *tbl);
72
static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
73
    const ASN1_STRING_TABLE * const *b);
74
75
76
/*
77
 * This is the global mask for the mbstring functions: this is used to
78
 * mask out certain types (such as BMPString and UTF8String) because
79
 * certain software (e.g. Netscape) has problems with them.
80
 */
81
82
static unsigned long global_mask = B_ASN1_UTF8STRING;
83
84
void
85
ASN1_STRING_set_default_mask(unsigned long mask)
86
0
{
87
0
  global_mask = mask;
88
0
}
89
90
unsigned long
91
ASN1_STRING_get_default_mask(void)
92
0
{
93
0
  return global_mask;
94
0
}
95
96
/*
97
 * This function sets the default to various "flavours" of configuration
98
 * based on an ASCII string. Currently this is:
99
 * MASK:XXXX : a numerical mask value.
100
 * nobmp : Don't use BMPStrings (just Printable, T61).
101
 * pkix : PKIX recommendation in RFC2459.
102
 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
103
 * default:   the default value, Printable, T61, BMP.
104
 */
105
106
int
107
ASN1_STRING_set_default_mask_asc(const char *p)
108
0
{
109
0
  unsigned long mask;
110
0
  char *end;
111
0
  int save_errno;
112
113
0
  if (strncmp(p, "MASK:", 5) == 0) {
114
0
    if (p[5] == '\0')
115
0
      return 0;
116
0
    save_errno = errno;
117
0
    errno = 0;
118
0
    mask = strtoul(p + 5, &end, 0);
119
0
    if (errno == ERANGE && mask == ULONG_MAX)
120
0
      return 0;
121
0
    errno = save_errno;
122
0
    if (*end != '\0')
123
0
      return 0;
124
0
  } else if (strcmp(p, "nombstr") == 0)
125
0
    mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
126
0
  else if (strcmp(p, "pkix") == 0)
127
0
    mask = ~((unsigned long)B_ASN1_T61STRING);
128
0
  else if (strcmp(p, "utf8only") == 0)
129
0
    mask = B_ASN1_UTF8STRING;
130
0
  else if (strcmp(p, "default") == 0)
131
0
    mask = 0xFFFFFFFFL;
132
0
  else
133
0
    return 0;
134
0
  ASN1_STRING_set_default_mask(mask);
135
0
  return 1;
136
0
}
137
138
/*
139
 * The following function generates an ASN1_STRING based on limits in a table.
140
 * Frequently the types and length of an ASN1_STRING are restricted by a
141
 * corresponding OID. For example certificates and certificate requests.
142
 */
143
144
ASN1_STRING *
145
ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, int inlen,
146
    int inform, int nid)
147
0
{
148
0
  ASN1_STRING_TABLE *tbl;
149
0
  ASN1_STRING *str = NULL;
150
0
  unsigned long mask;
151
0
  int ret;
152
153
0
  if (out == NULL)
154
0
    out = &str;
155
0
  tbl = ASN1_STRING_TABLE_get(nid);
156
0
  if (tbl != NULL) {
157
0
    mask = tbl->mask;
158
0
    if ((tbl->flags & STABLE_NO_MASK) == 0)
159
0
      mask &= global_mask;
160
0
    ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
161
0
        tbl->minsize, tbl->maxsize);
162
0
  } else
163
0
    ret = ASN1_mbstring_copy(out, in, inlen, inform,
164
0
        DIRSTRING_TYPE & global_mask);
165
0
  if (ret <= 0)
166
0
    return NULL;
167
0
  return *out;
168
0
}
169
170
/*
171
 * Now the tables and helper functions for the string table:
172
 */
173
174
/* size limits: this stuff is taken straight from RFC3280 */
175
176
#define ub_name       32768
177
#define ub_common_name      64
178
#define ub_locality_name    128
179
#define ub_state_name     128
180
#define ub_organization_name    64
181
#define ub_organization_unit_name 64
182
#define ub_title      64
183
#define ub_email_address    128
184
#define ub_serial_number    64
185
186
187
/* This table must be kept in NID order */
188
189
static const ASN1_STRING_TABLE tbl_standard[] = {
190
  {NID_commonName,    1, ub_common_name, DIRSTRING_TYPE, 0},
191
  {NID_countryName,   2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
192
  {NID_localityName,    1, ub_locality_name, DIRSTRING_TYPE, 0},
193
  {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
194
  {NID_organizationName,    1, ub_organization_name, DIRSTRING_TYPE, 0},
195
  {NID_organizationalUnitName,  1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
196
  {NID_pkcs9_emailAddress,  1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
197
  {NID_pkcs9_unstructuredName,  1, -1, PKCS9STRING_TYPE, 0},
198
  {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
199
  {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
200
  {NID_givenName,     1, ub_name, DIRSTRING_TYPE, 0},
201
  {NID_surname,     1, ub_name, DIRSTRING_TYPE, 0},
202
  {NID_initials,      1, ub_name, DIRSTRING_TYPE, 0},
203
  {NID_serialNumber,    1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
204
  {NID_friendlyName,    -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
205
  {NID_name,      1, ub_name, DIRSTRING_TYPE, 0},
206
  {NID_dnQualifier,   -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
207
  {NID_domainComponent,   1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
208
  {NID_ms_csp_name,   -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
209
};
210
211
static int
212
sk_table_cmp(const ASN1_STRING_TABLE * const *a,
213
    const ASN1_STRING_TABLE * const *b)
214
0
{
215
0
  return (*a)->nid - (*b)->nid;
216
0
}
217
218
static int table_cmp_BSEARCH_CMP_FN(const void *, const void *);
219
static int table_cmp(ASN1_STRING_TABLE const *, ASN1_STRING_TABLE const *);
220
static ASN1_STRING_TABLE *OBJ_bsearch_table(ASN1_STRING_TABLE *key, ASN1_STRING_TABLE const *base, int num);
221
222
static int
223
table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
224
0
{
225
0
  return a->nid - b->nid;
226
0
}
227
228
229
static int
230
table_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
231
0
{
232
0
  ASN1_STRING_TABLE const *a = a_;
233
0
  ASN1_STRING_TABLE const *b = b_;
234
0
  return table_cmp(a, b);
235
0
}
236
237
static ASN1_STRING_TABLE *
238
OBJ_bsearch_table(ASN1_STRING_TABLE *key, ASN1_STRING_TABLE const *base, int num)
239
0
{
240
0
  return (ASN1_STRING_TABLE *)OBJ_bsearch_(key, base, num, sizeof(ASN1_STRING_TABLE),
241
0
      table_cmp_BSEARCH_CMP_FN);
242
0
}
243
244
ASN1_STRING_TABLE *
245
ASN1_STRING_TABLE_get(int nid)
246
0
{
247
0
  int idx;
248
0
  ASN1_STRING_TABLE fnd;
249
250
0
  fnd.nid = nid;
251
0
  if (stable != NULL) {
252
0
    idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
253
0
    if (idx >= 0)
254
0
      return sk_ASN1_STRING_TABLE_value(stable, idx);
255
0
  }
256
0
  return OBJ_bsearch_table(&fnd, tbl_standard,
257
0
      sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE));
258
0
}
259
260
/*
261
 * Return a string table pointer which can be modified: either directly
262
 * from table or a copy of an internal value added to the table.
263
 */
264
265
static ASN1_STRING_TABLE *
266
stable_get(int nid)
267
0
{
268
0
  ASN1_STRING_TABLE *tmp, *rv;
269
270
  /* Always need a string table so allocate one if NULL */
271
0
  if (stable == NULL) {
272
0
    stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
273
0
    if (stable == NULL)
274
0
      return NULL;
275
0
  }
276
0
  tmp = ASN1_STRING_TABLE_get(nid);
277
0
  if (tmp != NULL && (tmp->flags & STABLE_FLAGS_MALLOC) != 0)
278
0
    return tmp;
279
280
0
  if ((rv = calloc(1, sizeof(*rv))) == NULL) {
281
0
    ASN1error(ERR_R_MALLOC_FAILURE);
282
0
    return NULL;
283
0
  }
284
0
  if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
285
0
    free(rv);
286
0
    return NULL;
287
0
  }
288
0
  if (tmp != NULL) {
289
0
    rv->nid = tmp->nid;
290
0
    rv->minsize = tmp->minsize;
291
0
    rv->maxsize = tmp->maxsize;
292
0
    rv->mask = tmp->mask;
293
0
    rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
294
0
  } else {
295
0
    rv->nid = nid;
296
0
    rv->minsize = -1;
297
0
    rv->maxsize = -1;
298
0
    rv->flags = STABLE_FLAGS_MALLOC;
299
0
  }
300
0
  return rv;
301
0
}
302
303
int
304
ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, unsigned long mask,
305
    unsigned long flags)
306
0
{
307
0
  ASN1_STRING_TABLE *tmp;
308
309
0
  if ((tmp = stable_get(nid)) == NULL) {
310
0
    ASN1error(ERR_R_MALLOC_FAILURE);
311
0
    return 0;
312
0
  }
313
0
  if (minsize >= 0)
314
0
    tmp->minsize = minsize;
315
0
  if (maxsize >= 0)
316
0
    tmp->maxsize = maxsize;
317
0
  if (mask != 0)
318
0
    tmp->mask = mask;
319
0
  if (flags != 0)
320
0
    tmp->flags = flags | STABLE_FLAGS_MALLOC;
321
322
0
  return 1;
323
0
}
324
325
void
326
ASN1_STRING_TABLE_cleanup(void)
327
0
{
328
0
  STACK_OF(ASN1_STRING_TABLE) *tmp;
329
330
0
  tmp = stable;
331
0
  if (tmp == NULL)
332
0
    return;
333
0
  stable = NULL;
334
0
  sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
335
0
}
336
337
static void
338
st_free(ASN1_STRING_TABLE *tbl)
339
0
{
340
0
  if (tbl->flags & STABLE_FLAGS_MALLOC)
341
0
    free(tbl);
342
0
}