1 /* $OpenBSD: a_strnid.c,v 1.16 2014/07/10 13:58:22 jsing 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 <ctype.h> 60 #include <stdio.h> 61 #include <string.h> 62 63 #include "cryptlib.h" 64 #include <openssl/asn1.h> 65 #include <openssl/objects.h> 66 67 68 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL; 69 static void st_free(ASN1_STRING_TABLE *tbl); 70 static int sk_table_cmp(const ASN1_STRING_TABLE * const *a, 71 const ASN1_STRING_TABLE * const *b); 72 73 74 /* This is the global mask for the mbstring functions: this is use to 75 * mask out certain types (such as BMPString and UTF8String) because 76 * certain software (e.g. Netscape) has problems with them. 77 */ 78 79 static unsigned long global_mask = B_ASN1_UTF8STRING; 80 81 void 82 ASN1_STRING_set_default_mask(unsigned long mask) 83 { 84 global_mask = mask; 85 } 86 87 unsigned long 88 ASN1_STRING_get_default_mask(void) 89 { 90 return global_mask; 91 } 92 93 /* This function sets the default to various "flavours" of configuration. 94 * based on an ASCII string. Currently this is: 95 * MASK:XXXX : a numerical mask value. 96 * nobmp : Don't use BMPStrings (just Printable, T61). 97 * pkix : PKIX recommendation in RFC2459. 98 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004). 99 * default: the default value, Printable, T61, BMP. 100 */ 101 102 int 103 ASN1_STRING_set_default_mask_asc(const char *p) 104 { 105 unsigned long mask; 106 char *end; 107 108 if (!strncmp(p, "MASK:", 5)) { 109 if (!p[5]) 110 return 0; 111 mask = strtoul(p + 5, &end, 0); 112 if (*end) 113 return 0; 114 } else if (!strcmp(p, "nombstr")) 115 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING)); 116 else if (!strcmp(p, "pkix")) 117 mask = ~((unsigned long)B_ASN1_T61STRING); 118 else if (!strcmp(p, "utf8only")) 119 mask = B_ASN1_UTF8STRING; 120 else if (!strcmp(p, "default")) 121 mask = 0xFFFFFFFFL; 122 else 123 return 0; 124 ASN1_STRING_set_default_mask(mask); 125 return 1; 126 } 127 128 /* The following function generates an ASN1_STRING based on limits in a table. 129 * Frequently the types and length of an ASN1_STRING are restricted by a 130 * corresponding OID. For example certificates and certificate requests. 131 */ 132 133 ASN1_STRING * 134 ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, int inlen, 135 int inform, int nid) 136 { 137 ASN1_STRING_TABLE *tbl; 138 ASN1_STRING *str = NULL; 139 unsigned long mask; 140 int ret; 141 if (!out) 142 out = &str; 143 tbl = ASN1_STRING_TABLE_get(nid); 144 if (tbl) { 145 mask = tbl->mask; 146 if (!(tbl->flags & STABLE_NO_MASK)) 147 mask &= global_mask; 148 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask, 149 tbl->minsize, tbl->maxsize); 150 } else 151 ret = ASN1_mbstring_copy(out, in, inlen, inform, 152 DIRSTRING_TYPE & global_mask); 153 if (ret <= 0) 154 return NULL; 155 return *out; 156 } 157 158 /* Now the tables and helper functions for the string table: 159 */ 160 161 /* size limits: this stuff is taken straight from RFC3280 */ 162 163 #define ub_name 32768 164 #define ub_common_name 64 165 #define ub_locality_name 128 166 #define ub_state_name 128 167 #define ub_organization_name 64 168 #define ub_organization_unit_name 64 169 #define ub_title 64 170 #define ub_email_address 128 171 #define ub_serial_number 64 172 173 174 /* This table must be kept in NID order */ 175 176 static const ASN1_STRING_TABLE tbl_standard[] = { 177 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0}, 178 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 179 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0}, 180 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0}, 181 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0}, 182 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE, 0}, 183 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK}, 184 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0}, 185 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0}, 186 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0}, 187 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0}, 188 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0}, 189 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0}, 190 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 191 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}, 192 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0}, 193 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 194 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK}, 195 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK} 196 }; 197 198 static int 199 sk_table_cmp(const ASN1_STRING_TABLE * const *a, 200 const ASN1_STRING_TABLE * const *b) 201 { 202 return (*a)->nid - (*b)->nid; 203 } 204 205 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table); 206 207 static int 208 table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) 209 { 210 return a->nid - b->nid; 211 } 212 213 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table); 214 215 ASN1_STRING_TABLE * 216 ASN1_STRING_TABLE_get(int nid) 217 { 218 int idx; 219 ASN1_STRING_TABLE *ttmp; 220 ASN1_STRING_TABLE fnd; 221 222 fnd.nid = nid; 223 ttmp = OBJ_bsearch_table(&fnd, tbl_standard, 224 sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE)); 225 if (ttmp) 226 return ttmp; 227 if (!stable) 228 return NULL; 229 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd); 230 if (idx < 0) 231 return NULL; 232 return sk_ASN1_STRING_TABLE_value(stable, idx); 233 } 234 235 int 236 ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, unsigned long mask, 237 unsigned long flags) 238 { 239 ASN1_STRING_TABLE *tmp; 240 char new_nid = 0; 241 242 flags &= ~STABLE_FLAGS_MALLOC; 243 if (!stable) 244 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp); 245 if (!stable) { 246 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE); 247 return 0; 248 } 249 if (!(tmp = ASN1_STRING_TABLE_get(nid))) { 250 tmp = malloc(sizeof(ASN1_STRING_TABLE)); 251 if (!tmp) { 252 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, 253 ERR_R_MALLOC_FAILURE); 254 return 0; 255 } 256 tmp->flags = flags | STABLE_FLAGS_MALLOC; 257 tmp->nid = nid; 258 new_nid = 1; 259 } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags; 260 if (minsize != -1) 261 tmp->minsize = minsize; 262 if (maxsize != -1) 263 tmp->maxsize = maxsize; 264 tmp->mask = mask; 265 if (new_nid) 266 sk_ASN1_STRING_TABLE_push(stable, tmp); 267 return 1; 268 } 269 270 void 271 ASN1_STRING_TABLE_cleanup(void) 272 { 273 STACK_OF(ASN1_STRING_TABLE) *tmp; 274 275 tmp = stable; 276 if (!tmp) 277 return; 278 stable = NULL; 279 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free); 280 } 281 282 static void 283 st_free(ASN1_STRING_TABLE *tbl) 284 { 285 if (tbl->flags & STABLE_FLAGS_MALLOC) 286 free(tbl); 287 } 288 289 290 IMPLEMENT_STACK_OF(ASN1_STRING_TABLE) 291