a_strnid.c 11 KB

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