ssl/libcrypto/src/crypto/asn1/a_strnid.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* a_strnid.c */
       
     2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
    60  */
       
    61 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
    62 #include "libcrypto_wsd.h"
       
    63 #include "libcrypto_wsd_macros.h"
       
    64 #endif
       
    65 
       
    66 
       
    67 #include <stdio.h>
       
    68 #include <ctype.h>
       
    69 #include "cryptlib.h"
       
    70 #include <openssl/asn1.h>
       
    71 #include <openssl/objects.h>
       
    72 
       
    73 
       
    74 #ifndef EMULATOR
       
    75 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
       
    76 #else //EMULATOR
       
    77 GET_STATIC_VAR_FROM_TLS(stable,a_strnid,STACK_OF(ASN1_STRING_TABLE)*)
       
    78 #define stable (*GET_WSD_VAR_NAME(stable,a_strnid, s)())
       
    79 #endif //EMULATOR
       
    80 
       
    81 static void st_free(ASN1_STRING_TABLE *tbl);
       
    82 static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
       
    83 			const ASN1_STRING_TABLE * const *b);
       
    84 static int table_cmp(const void *a, const void *b);
       
    85 
       
    86 
       
    87 /* This is the global mask for the mbstring functions: this is use to
       
    88  * mask out certain types (such as BMPString and UTF8String) because
       
    89  * certain software (e.g. Netscape) has problems with them.
       
    90  */
       
    91 
       
    92 #ifndef EMULATOR
       
    93 static unsigned long global_mask = 0xFFFFFFFFL;
       
    94 #else
       
    95 GET_STATIC_VAR_FROM_TLS(global_mask,a_strnid, unsigned long)
       
    96 #define global_mask (*GET_WSD_VAR_NAME(global_mask,a_strnid, s)())
       
    97 #endif
       
    98 
       
    99 EXPORT_C void ASN1_STRING_set_default_mask(unsigned long mask)
       
   100 {
       
   101 	global_mask = mask;
       
   102 }
       
   103 
       
   104 EXPORT_C unsigned long ASN1_STRING_get_default_mask(void)
       
   105 {
       
   106 	return global_mask;
       
   107 }
       
   108 
       
   109 /* This function sets the default to various "flavours" of configuration.
       
   110  * based on an ASCII string. Currently this is:
       
   111  * MASK:XXXX : a numerical mask value.
       
   112  * nobmp : Don't use BMPStrings (just Printable, T61).
       
   113  * pkix : PKIX recommendation in RFC2459.
       
   114  * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
       
   115  * default:   the default value, Printable, T61, BMP.
       
   116  */
       
   117 
       
   118 EXPORT_C int ASN1_STRING_set_default_mask_asc(char *p)
       
   119 {
       
   120 	unsigned long mask;
       
   121 	char *end;
       
   122 	if(!strncmp(p, "MASK:", 5)) {
       
   123 		if(!p[5]) return 0;
       
   124 		mask = strtoul(p + 5, &end, 0);
       
   125 		if(*end) return 0;
       
   126 	} else if(!strcmp(p, "nombstr"))
       
   127 			 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
       
   128 	else if(!strcmp(p, "pkix"))
       
   129 			mask = ~((unsigned long)B_ASN1_T61STRING);
       
   130 	else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING;
       
   131 	else if(!strcmp(p, "default"))
       
   132 	    mask = 0xFFFFFFFFL;
       
   133 	else return 0;
       
   134 	ASN1_STRING_set_default_mask(mask);
       
   135 	return 1;
       
   136 }
       
   137 
       
   138 /* The following function generates an ASN1_STRING based on limits in a table.
       
   139  * Frequently the types and length of an ASN1_STRING are restricted by a 
       
   140  * corresponding OID. For example certificates and certificate requests.
       
   141  */
       
   142 
       
   143 EXPORT_C ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
       
   144 					int inlen, int inform, int nid)
       
   145 {
       
   146 	ASN1_STRING_TABLE *tbl;
       
   147 	ASN1_STRING *str = NULL;
       
   148 	unsigned long mask;
       
   149 	int ret;
       
   150 	if(!out) out = &str;
       
   151 	tbl = ASN1_STRING_TABLE_get(nid);
       
   152 	if(tbl) {
       
   153 		mask = tbl->mask;
       
   154 		if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask;
       
   155 		ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
       
   156 					tbl->minsize, tbl->maxsize);
       
   157 	} else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask);
       
   158 	if(ret <= 0) return NULL;
       
   159 	return *out;
       
   160 }
       
   161 
       
   162 /* Now the tables and helper functions for the string table:
       
   163  */
       
   164 
       
   165 /* size limits: this stuff is taken straight from RFC3280 */
       
   166 
       
   167 #define ub_name				32768
       
   168 #define ub_common_name			64
       
   169 #define ub_locality_name		128
       
   170 #define ub_state_name			128
       
   171 #define ub_organization_name		64
       
   172 #define ub_organization_unit_name	64
       
   173 #define ub_title			64
       
   174 #define ub_email_address		128
       
   175 #define ub_serial_number		64
       
   176 
       
   177 
       
   178 /* This table must be kept in NID order */
       
   179 #ifndef EMULATOR
       
   180 static ASN1_STRING_TABLE tbl_standard[] = {
       
   181 {NID_commonName,		1, ub_common_name, DIRSTRING_TYPE, 0},
       
   182 {NID_countryName,		2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   183 {NID_localityName,		1, ub_locality_name, DIRSTRING_TYPE, 0},
       
   184 {NID_stateOrProvinceName,	1, ub_state_name, DIRSTRING_TYPE, 0},
       
   185 {NID_organizationName,		1, ub_organization_name, DIRSTRING_TYPE, 0},
       
   186 {NID_organizationalUnitName,	1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
       
   187 {NID_pkcs9_emailAddress,	1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
       
   188 {NID_pkcs9_unstructuredName,	1, -1, PKCS9STRING_TYPE, 0},
       
   189 {NID_pkcs9_challengePassword,	1, -1, PKCS9STRING_TYPE, 0},
       
   190 {NID_pkcs9_unstructuredAddress,	1, -1, DIRSTRING_TYPE, 0},
       
   191 {NID_givenName,			1, ub_name, DIRSTRING_TYPE, 0},
       
   192 {NID_surname,			1, ub_name, DIRSTRING_TYPE, 0},
       
   193 {NID_initials,			1, ub_name, DIRSTRING_TYPE, 0},
       
   194 {NID_serialNumber,		1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   195 {NID_friendlyName,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
       
   196 {NID_name,			1, ub_name, DIRSTRING_TYPE, 0},
       
   197 {NID_dnQualifier,		-1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   198 {NID_domainComponent,		1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
       
   199 {NID_ms_csp_name,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
       
   200 };
       
   201 #else  //EMULATOR
       
   202 const ASN1_STRING_TABLE tbl_standard[] = {
       
   203 {NID_commonName,		1, ub_common_name, DIRSTRING_TYPE, 0},
       
   204 {NID_countryName,		2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   205 {NID_localityName,		1, ub_locality_name, DIRSTRING_TYPE, 0},
       
   206 {NID_stateOrProvinceName,	1, ub_state_name, DIRSTRING_TYPE, 0},
       
   207 {NID_organizationName,		1, ub_organization_name, DIRSTRING_TYPE, 0},
       
   208 {NID_organizationalUnitName,	1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
       
   209 {NID_pkcs9_emailAddress,	1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
       
   210 {NID_pkcs9_unstructuredName,	1, -1, PKCS9STRING_TYPE, 0},
       
   211 {NID_pkcs9_challengePassword,	1, -1, PKCS9STRING_TYPE, 0},
       
   212 {NID_pkcs9_unstructuredAddress,	1, -1, DIRSTRING_TYPE, 0},
       
   213 {NID_givenName,			1, ub_name, DIRSTRING_TYPE, 0},
       
   214 {NID_surname,			1, ub_name, DIRSTRING_TYPE, 0},
       
   215 {NID_initials,			1, ub_name, DIRSTRING_TYPE, 0},
       
   216 {NID_serialNumber,		1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   217 {NID_friendlyName,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
       
   218 {NID_name,			1, ub_name, DIRSTRING_TYPE, 0},
       
   219 {NID_dnQualifier,		-1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
       
   220 {NID_domainComponent,		1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
       
   221 {NID_ms_csp_name,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
       
   222 };
       
   223 #endif //EMULATOR
       
   224 
       
   225 static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
       
   226 			const ASN1_STRING_TABLE * const *b)
       
   227 {
       
   228 	return (*a)->nid - (*b)->nid;
       
   229 }
       
   230 
       
   231 static int table_cmp(const void *a, const void *b)
       
   232 {
       
   233 	const ASN1_STRING_TABLE *sa = a, *sb = b;
       
   234 	return sa->nid - sb->nid;
       
   235 }
       
   236 
       
   237 EXPORT_C ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
       
   238 {
       
   239 	int idx;
       
   240 	ASN1_STRING_TABLE *ttmp;
       
   241 	ASN1_STRING_TABLE fnd;
       
   242 	fnd.nid = nid;
       
   243 	ttmp = (ASN1_STRING_TABLE *) OBJ_bsearch((char *)&fnd,
       
   244 					(char *)tbl_standard, 
       
   245 			sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE),
       
   246 			sizeof(ASN1_STRING_TABLE), table_cmp);
       
   247 	if(ttmp) return ttmp;
       
   248 	if(!stable) return NULL;
       
   249 	idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
       
   250 	if(idx < 0) return NULL;
       
   251 	return sk_ASN1_STRING_TABLE_value(stable, idx);
       
   252 }
       
   253 	
       
   254 EXPORT_C int ASN1_STRING_TABLE_add(int nid,
       
   255 		 long minsize, long maxsize, unsigned long mask,
       
   256 				unsigned long flags)
       
   257 {
       
   258 	ASN1_STRING_TABLE *tmp;
       
   259 	char new_nid = 0;
       
   260 	flags &= ~STABLE_FLAGS_MALLOC;
       
   261 	if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
       
   262 	if(!stable) {
       
   263 		ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
       
   264 		return 0;
       
   265 	}
       
   266 	if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
       
   267 		tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
       
   268 		if(!tmp) {
       
   269 			ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
       
   270 							ERR_R_MALLOC_FAILURE);
       
   271 			return 0;
       
   272 		}
       
   273 		tmp->flags = flags | STABLE_FLAGS_MALLOC;
       
   274 		tmp->nid = nid;
       
   275 		new_nid = 1;
       
   276 	} else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
       
   277 	if(minsize != -1) tmp->minsize = minsize;
       
   278 	if(maxsize != -1) tmp->maxsize = maxsize;
       
   279 	tmp->mask = mask;
       
   280 	if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
       
   281 	return 1;
       
   282 }
       
   283 
       
   284 EXPORT_C void ASN1_STRING_TABLE_cleanup(void)
       
   285 {
       
   286 	STACK_OF(ASN1_STRING_TABLE) *tmp;
       
   287 	tmp = stable;
       
   288 	if(!tmp) return;
       
   289 	stable = NULL;
       
   290 	sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
       
   291 }
       
   292 
       
   293 static void st_free(ASN1_STRING_TABLE *tbl)
       
   294 {
       
   295 	if(tbl->flags & STABLE_FLAGS_MALLOC) OPENSSL_free(tbl);
       
   296 }
       
   297 
       
   298 
       
   299 IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)
       
   300 
       
   301 #ifdef STRING_TABLE_TEST
       
   302 
       
   303 main()
       
   304 {
       
   305 	ASN1_STRING_TABLE *tmp;
       
   306 	int i, last_nid = -1;
       
   307 
       
   308 	for (tmp = tbl_standard, i = 0;
       
   309 		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
       
   310 		{
       
   311 			if (tmp->nid < last_nid)
       
   312 				{
       
   313 				last_nid = 0;
       
   314 				break;
       
   315 				}
       
   316 			last_nid = tmp->nid;
       
   317 		}
       
   318 
       
   319 	if (last_nid != 0)
       
   320 		{
       
   321 		printf("Table order OK\n");
       
   322 		exit(0);
       
   323 		}
       
   324 
       
   325 	for (tmp = tbl_standard, i = 0;
       
   326 		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
       
   327 			printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
       
   328 							OBJ_nid2ln(tmp->nid));
       
   329 
       
   330 }
       
   331 
       
   332 #endif