a_strnid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* a_strnid.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <ctype.h>
  61. #include "internal/cryptlib.h"
  62. #include <openssl/asn1.h>
  63. #include <openssl/objects.h>
  64. static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
  65. static void st_free(ASN1_STRING_TABLE *tbl);
  66. static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
  67. const ASN1_STRING_TABLE *const *b);
  68. /*
  69. * This is the global mask for the mbstring functions: this is use to mask
  70. * out certain types (such as BMPString and UTF8String) because certain
  71. * software (e.g. Netscape) has problems with them.
  72. */
  73. static unsigned long global_mask = B_ASN1_UTF8STRING;
  74. void ASN1_STRING_set_default_mask(unsigned long mask)
  75. {
  76. global_mask = mask;
  77. }
  78. unsigned long ASN1_STRING_get_default_mask(void)
  79. {
  80. return global_mask;
  81. }
  82. /*-
  83. * This function sets the default to various "flavours" of configuration.
  84. * based on an ASCII string. Currently this is:
  85. * MASK:XXXX : a numerical mask value.
  86. * nobmp : Don't use BMPStrings (just Printable, T61).
  87. * pkix : PKIX recommendation in RFC2459.
  88. * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
  89. * default: the default value, Printable, T61, BMP.
  90. */
  91. int ASN1_STRING_set_default_mask_asc(const char *p)
  92. {
  93. unsigned long mask;
  94. char *end;
  95. if (strncmp(p, "MASK:", 5) == 0) {
  96. if (!p[5])
  97. return 0;
  98. mask = strtoul(p + 5, &end, 0);
  99. if (*end)
  100. return 0;
  101. } else if (strcmp(p, "nombstr") == 0)
  102. mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
  103. else if (strcmp(p, "pkix") == 0)
  104. mask = ~((unsigned long)B_ASN1_T61STRING);
  105. else if (strcmp(p, "utf8only") == 0)
  106. mask = B_ASN1_UTF8STRING;
  107. else if (strcmp(p, "default") == 0)
  108. mask = 0xFFFFFFFFL;
  109. else
  110. return 0;
  111. ASN1_STRING_set_default_mask(mask);
  112. return 1;
  113. }
  114. /*
  115. * The following function generates an ASN1_STRING based on limits in a
  116. * table. Frequently the types and length of an ASN1_STRING are restricted by
  117. * a corresponding OID. For example certificates and certificate requests.
  118. */
  119. ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  120. const unsigned char *in, int inlen,
  121. int inform, int nid)
  122. {
  123. ASN1_STRING_TABLE *tbl;
  124. ASN1_STRING *str = NULL;
  125. unsigned long mask;
  126. int ret;
  127. if (!out)
  128. out = &str;
  129. tbl = ASN1_STRING_TABLE_get(nid);
  130. if (tbl) {
  131. mask = tbl->mask;
  132. if (!(tbl->flags & STABLE_NO_MASK))
  133. mask &= global_mask;
  134. ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
  135. tbl->minsize, tbl->maxsize);
  136. } else
  137. ret =
  138. ASN1_mbstring_copy(out, in, inlen, inform,
  139. DIRSTRING_TYPE & global_mask);
  140. if (ret <= 0)
  141. return NULL;
  142. return *out;
  143. }
  144. /*
  145. * Now the tables and helper functions for the string table:
  146. */
  147. /* size limits: this stuff is taken straight from RFC3280 */
  148. #define ub_name 32768
  149. #define ub_common_name 64
  150. #define ub_locality_name 128
  151. #define ub_state_name 128
  152. #define ub_organization_name 64
  153. #define ub_organization_unit_name 64
  154. #define ub_title 64
  155. #define ub_email_address 128
  156. #define ub_serial_number 64
  157. /* This table must be kept in NID order */
  158. static const ASN1_STRING_TABLE tbl_standard[] = {
  159. {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
  160. {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
  161. {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
  162. {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
  163. {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
  164. {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
  165. 0},
  166. {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
  167. STABLE_NO_MASK},
  168. {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
  169. {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
  170. {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
  171. {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
  172. {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
  173. {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
  174. {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
  175. STABLE_NO_MASK},
  176. {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
  177. {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
  178. {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
  179. {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
  180. {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
  181. };
  182. static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
  183. const ASN1_STRING_TABLE *const *b)
  184. {
  185. return (*a)->nid - (*b)->nid;
  186. }
  187. DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  188. static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
  189. {
  190. return a->nid - b->nid;
  191. }
  192. IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  193. ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
  194. {
  195. int idx;
  196. ASN1_STRING_TABLE fnd;
  197. fnd.nid = nid;
  198. if (stable) {
  199. idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
  200. if (idx >= 0)
  201. return sk_ASN1_STRING_TABLE_value(stable, idx);
  202. }
  203. return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
  204. }
  205. /*
  206. * Return a string table pointer which can be modified: either directly from
  207. * table or a copy of an internal value added to the table.
  208. */
  209. static ASN1_STRING_TABLE *stable_get(int nid)
  210. {
  211. ASN1_STRING_TABLE *tmp, *rv;
  212. /* Always need a string table so allocate one if NULL */
  213. if (!stable) {
  214. stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
  215. if (!stable)
  216. return NULL;
  217. }
  218. tmp = ASN1_STRING_TABLE_get(nid);
  219. if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
  220. return tmp;
  221. rv = OPENSSL_malloc(sizeof(*rv));
  222. if (!rv)
  223. return NULL;
  224. if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
  225. OPENSSL_free(rv);
  226. return NULL;
  227. }
  228. if (tmp) {
  229. rv->nid = tmp->nid;
  230. rv->minsize = tmp->minsize;
  231. rv->maxsize = tmp->maxsize;
  232. rv->mask = tmp->mask;
  233. rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
  234. } else {
  235. rv->minsize = -1;
  236. rv->maxsize = -1;
  237. rv->mask = 0;
  238. rv->flags = STABLE_FLAGS_MALLOC;
  239. }
  240. return rv;
  241. }
  242. int ASN1_STRING_TABLE_add(int nid,
  243. long minsize, long maxsize, unsigned long mask,
  244. unsigned long flags)
  245. {
  246. ASN1_STRING_TABLE *tmp;
  247. tmp = stable_get(nid);
  248. if (!tmp) {
  249. ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
  250. return 0;
  251. }
  252. if (minsize >= 0)
  253. tmp->minsize = minsize;
  254. if (maxsize >= 0)
  255. tmp->maxsize = maxsize;
  256. if (mask)
  257. tmp->mask = mask;
  258. if (flags)
  259. tmp->flags = STABLE_FLAGS_MALLOC | flags;
  260. return 1;
  261. }
  262. void ASN1_STRING_TABLE_cleanup(void)
  263. {
  264. STACK_OF(ASN1_STRING_TABLE) *tmp;
  265. tmp = stable;
  266. if (!tmp)
  267. return;
  268. stable = NULL;
  269. sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
  270. }
  271. static void st_free(ASN1_STRING_TABLE *tbl)
  272. {
  273. if (tbl->flags & STABLE_FLAGS_MALLOC)
  274. OPENSSL_free(tbl);
  275. }
  276. #ifdef STRING_TABLE_TEST
  277. main()
  278. {
  279. ASN1_STRING_TABLE *tmp;
  280. int i, last_nid = -1;
  281. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
  282. if (tmp->nid < last_nid) {
  283. last_nid = 0;
  284. break;
  285. }
  286. last_nid = tmp->nid;
  287. }
  288. if (last_nid != 0) {
  289. printf("Table order OK\n");
  290. exit(0);
  291. }
  292. for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
  293. printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
  294. OBJ_nid2ln(tmp->nid));
  295. }
  296. #endif