195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * All rights reserved.
395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This package is an SSL implementation written
595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * by Eric Young (eay@cryptsoft.com).
695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The implementation was written so as to conform with Netscapes SSL.
795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This library is free for commercial and non-commercial use as long as
995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the following conditions are aheared to.  The following conditions
1095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * apply to all code found in this distribution, be it the RC4, RSA,
1195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * included with this distribution is covered by the same copyright terms
1395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
1595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Copyright remains Eric Young's, and as such any Copyright notices in
1695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the code are not to be removed.
1795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * If this package is used in a product, Eric Young should be given attribution
1895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * as the author of the parts of the library used.
1995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This can be in the form of a textual message at program startup or
2095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * in documentation (online or textual) provided with the package.
2195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
2295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Redistribution and use in source and binary forms, with or without
2395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * modification, are permitted provided that the following conditions
2495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * are met:
2595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 1. Redistributions of source code must retain the copyright
2695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer.
2795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 2. Redistributions in binary form must reproduce the above copyright
2895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    notice, this list of conditions and the following disclaimer in the
2995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    documentation and/or other materials provided with the distribution.
3095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 3. All advertising materials mentioning features or use of this software
3195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    must display the following acknowledgement:
3295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes cryptographic software written by
3395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *     Eric Young (eay@cryptsoft.com)"
3495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    The word 'cryptographic' can be left out if the rouines from the library
3595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    being used are not cryptographic related :-).
3695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 4. If you include any Windows specific code (or a derivative thereof) from
3795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    the apps directory (application code) you must include an acknowledgement:
3895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
3995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
4095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * SUCH DAMAGE.
5195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley *
5295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The licence and distribution terms for any publically available version or
5395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * derivative of this code cannot be changed.  i.e. this code cannot simply be
5495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * copied and put under another distribution licence
5595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * [including the GNU Public Licence.] */
5695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
5795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/asn1.h>
5895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
5995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <stdlib.h>  /* For bsearch */
6095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
6195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/err.h>
6295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/mem.h>
6395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/obj.h>
6495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
6595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
6695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
6795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void st_free(ASN1_STRING_TABLE *tbl);
6895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
6995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* This is the global mask for the mbstring functions: this is use to
7095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * mask out certain types (such as BMPString and UTF8String) because
7195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * certain software (e.g. Netscape) has problems with them.
7295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley */
7395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
74a09d2127eaeaab15d520c7e5dd964b72dba9597fAdam Langleystatic unsigned long global_mask = B_ASN1_UTF8STRING;
7595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
7695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ASN1_STRING_set_default_mask(unsigned long mask)
7795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
7895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	global_mask = mask;
7995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
8095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
8195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyunsigned long ASN1_STRING_get_default_mask(void)
8295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
8395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return global_mask;
8495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
8595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
8695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* This function sets the default to various "flavours" of configuration.
8795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * based on an ASCII string. Currently this is:
8895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * MASK:XXXX : a numerical mask value.
8995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * nobmp : Don't use BMPStrings (just Printable, T61).
9095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * pkix : PKIX recommendation in RFC2459.
9195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
9295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * default:   the default value, Printable, T61, BMP.
9395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley */
9495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
9595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyint ASN1_STRING_set_default_mask_asc(const char *p)
9695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
9795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	unsigned long mask;
9895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	char *end;
9995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!strncmp(p, "MASK:", 5)) {
10095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		if(!p[5]) return 0;
10195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		mask = strtoul(p + 5, &end, 0);
10295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		if(*end) return 0;
10395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	} else if(!strcmp(p, "nombstr"))
10495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
10595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	else if(!strcmp(p, "pkix"))
10695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			mask = ~((unsigned long)B_ASN1_T61STRING);
10795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING;
10895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	else if(!strcmp(p, "default"))
10995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	    mask = 0xFFFFFFFFL;
11095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	else return 0;
11195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_set_default_mask(mask);
11295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return 1;
11395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
11495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
11595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* The following function generates an ASN1_STRING based on limits in a table.
11695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Frequently the types and length of an ASN1_STRING are restricted by a
11795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * corresponding OID. For example certificates and certificate requests.
11895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley */
11995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
12095c29f3cd1f6c08c6c0927868683392eea727ccAdam LangleyASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
12195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley					int inlen, int inform, int nid)
12295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
12395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_TABLE *tbl;
12495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING *str = NULL;
12595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	unsigned long mask;
12695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	int ret;
12795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!out) out = &str;
12895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	tbl = ASN1_STRING_TABLE_get(nid);
12995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(tbl) {
13095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		mask = tbl->mask;
13195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask;
13295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
13395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley					tbl->minsize, tbl->maxsize);
13495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	} else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask);
13595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(ret <= 0) return NULL;
13695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return *out;
13795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
13895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
13995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* Now the tables and helper functions for the string table:
14095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley */
14195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
14295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* size limits: this stuff is taken straight from RFC3280 */
14395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
14495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_name				32768
14595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_common_name			64
14695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_locality_name		128
14795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_state_name			128
14895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_organization_name		64
14995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_organization_unit_name	64
15095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_title			64
15195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_email_address		128
15295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define ub_serial_number		64
15395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
15495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
15595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* This table must be kept in NID order */
15695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
15795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const ASN1_STRING_TABLE tbl_standard[] = {
15895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_commonName,		1, ub_common_name, DIRSTRING_TYPE, 0},
15995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_countryName,		2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
16095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_localityName,		1, ub_locality_name, DIRSTRING_TYPE, 0},
16195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_stateOrProvinceName,	1, ub_state_name, DIRSTRING_TYPE, 0},
16295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_organizationName,		1, ub_organization_name, DIRSTRING_TYPE, 0},
16395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_organizationalUnitName,	1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
16495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_pkcs9_emailAddress,	1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
16595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_pkcs9_unstructuredName,	1, -1, PKCS9STRING_TYPE, 0},
16695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_pkcs9_challengePassword,	1, -1, PKCS9STRING_TYPE, 0},
16795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_pkcs9_unstructuredAddress,	1, -1, DIRSTRING_TYPE, 0},
16895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_givenName,			1, ub_name, DIRSTRING_TYPE, 0},
16995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_surname,			1, ub_name, DIRSTRING_TYPE, 0},
17095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_initials,			1, ub_name, DIRSTRING_TYPE, 0},
17195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_serialNumber,		1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
17295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_friendlyName,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
17395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_name,			1, ub_name, DIRSTRING_TYPE, 0},
17495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_dnQualifier,		-1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
17595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_domainComponent,		1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
17695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{NID_ms_csp_name,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
17795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley};
17895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
17995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int sk_table_cmp(const ASN1_STRING_TABLE **a,
18095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			const ASN1_STRING_TABLE **b)
18195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
18295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return (*a)->nid - (*b)->nid;
18395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
18495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
18595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int table_cmp(const void *in_a, const void *in_b)
18695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
18795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	const ASN1_STRING_TABLE *a = in_a;
18895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	const ASN1_STRING_TABLE *b = in_b;
18995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return a->nid - b->nid;
19095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
19195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
19295c29f3cd1f6c08c6c0927868683392eea727ccAdam LangleyASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
19395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
19495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	int found;
19595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	size_t idx;
19695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_TABLE *ttmp;
19795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_TABLE fnd;
19895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	fnd.nid = nid;
19995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
20095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ttmp = bsearch(&fnd, tbl_standard, sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE), sizeof(ASN1_STRING_TABLE), table_cmp);
20195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(ttmp) return ttmp;
20295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!stable) return NULL;
20395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	found = sk_ASN1_STRING_TABLE_find(stable, &idx, &fnd);
20495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if (!found) return NULL;
20595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return sk_ASN1_STRING_TABLE_value(stable, idx);
20695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
20795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
20895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyint ASN1_STRING_TABLE_add(int nid,
20995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		 long minsize, long maxsize, unsigned long mask,
21095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley				unsigned long flags)
21195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
21295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_TABLE *tmp;
21395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	char new_nid = 0;
21495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	flags &= ~STABLE_FLAGS_MALLOC;
21595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
21695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!stable) {
21795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_TABLE_add,  ERR_R_MALLOC_FAILURE);
21895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		return 0;
21995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	}
22095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
22195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
22295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		if(!tmp) {
22395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_TABLE_add,  ERR_R_MALLOC_FAILURE);
22495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			return 0;
22595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		}
22695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		tmp->flags = flags | STABLE_FLAGS_MALLOC;
22795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		tmp->nid = nid;
22895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		new_nid = 1;
22995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	} else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
23095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(minsize != -1) tmp->minsize = minsize;
23195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(maxsize != -1) tmp->maxsize = maxsize;
23295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	tmp->mask = mask;
23395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
23495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	return 1;
23595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
23695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
23795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyvoid ASN1_STRING_TABLE_cleanup(void)
23895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
23995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	STACK_OF(ASN1_STRING_TABLE) *tmp;
24095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	tmp = stable;
24195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(!tmp) return;
24295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	stable = NULL;
24395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
24495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
24595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
24695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic void st_free(ASN1_STRING_TABLE *tbl)
24795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
24895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if(tbl->flags & STABLE_FLAGS_MALLOC) OPENSSL_free(tbl);
24995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
25095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
25195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
25295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#ifdef STRING_TABLE_TEST
25395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
254c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminint
255c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjaminmain(void)
25695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley{
25795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	ASN1_STRING_TABLE *tmp;
25895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	int i, last_nid = -1;
25995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
26095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	for (tmp = tbl_standard, i = 0;
26195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
26295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		{
26395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			if (tmp->nid < last_nid)
26495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley				{
26595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley				last_nid = 0;
26695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley				break;
26795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley				}
26895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			last_nid = tmp->nid;
26995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		}
27095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
27195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	if (last_nid != 0)
27295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		{
27395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		printf("Table order OK\n");
27495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		exit(0);
27595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		}
27695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
27795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley	for (tmp = tbl_standard, i = 0;
27895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
27995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley			printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
28095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley							OBJ_nid2ln(tmp->nid));
28195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
282c44d2f4cb8a892a603edbbe710fa82bcd30f9cb5David Benjamin	return 0;
28395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley}
28495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley
28595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#endif
286