a_strnid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "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)) {
  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"))
  102. mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
  103. else if (!strcmp(p, "pkix"))
  104. mask = ~((unsigned long)B_ASN1_T61STRING);
  105. else if (!strcmp(p, "utf8only"))
  106. mask = B_ASN1_UTF8STRING;
  107. else if (!strcmp(p, "default"))
  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. {NID_jurisdictionCountryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}
  182. };
  183. static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
  184. const ASN1_STRING_TABLE *const *b)
  185. {
  186. return (*a)->nid - (*b)->nid;
  187. }
  188. DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  189. static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
  190. {
  191. return a->nid - b->nid;
  192. }
  193. IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  194. ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
  195. {
  196. int idx;
  197. ASN1_STRING_TABLE *ttmp;
  198. ASN1_STRING_TABLE fnd;
  199. fnd.nid = nid;
  200. ttmp = OBJ_bsearch_table(&fnd, tbl_standard,
  201. sizeof(tbl_standard) /
  202. sizeof(ASN1_STRING_TABLE));
  203. if (ttmp)
  204. return ttmp;
  205. if (!stable)
  206. return NULL;
  207. idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
  208. if (idx < 0)
  209. return NULL;
  210. return sk_ASN1_STRING_TABLE_value(stable, idx);
  211. }
  212. int ASN1_STRING_TABLE_add(int nid,
  213. long minsize, long maxsize, unsigned long mask,
  214. unsigned long flags)
  215. {
  216. ASN1_STRING_TABLE *tmp;
  217. char new_nid = 0;
  218. flags &= ~STABLE_FLAGS_MALLOC;
  219. if (!stable)
  220. stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
  221. if (!stable) {
  222. ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
  223. return 0;
  224. }
  225. if (!(tmp = ASN1_STRING_TABLE_get(nid))) {
  226. tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
  227. if (!tmp) {
  228. ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
  229. return 0;
  230. }
  231. tmp->flags = flags | STABLE_FLAGS_MALLOC;
  232. tmp->nid = nid;
  233. tmp->minsize = tmp->maxsize = -1;
  234. new_nid = 1;
  235. } else
  236. tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
  237. if (minsize != -1)
  238. tmp->minsize = minsize;
  239. if (maxsize != -1)
  240. tmp->maxsize = maxsize;
  241. tmp->mask = mask;
  242. if (new_nid)
  243. sk_ASN1_STRING_TABLE_push(stable, tmp);
  244. return 1;
  245. }
  246. void ASN1_STRING_TABLE_cleanup(void)
  247. {
  248. STACK_OF(ASN1_STRING_TABLE) *tmp;
  249. tmp = stable;
  250. if (!tmp)
  251. return;
  252. stable = NULL;
  253. sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
  254. }
  255. static void st_free(ASN1_STRING_TABLE *tbl)
  256. {
  257. if (tbl->flags & STABLE_FLAGS_MALLOC)
  258. OPENSSL_free(tbl);
  259. }
  260. IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)
  261. #ifdef STRING_TABLE_TEST
  262. main()
  263. {
  264. ASN1_STRING_TABLE *tmp;
  265. int i, last_nid = -1;
  266. for (tmp = tbl_standard, i = 0;
  267. i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
  268. if (tmp->nid < last_nid) {
  269. last_nid = 0;
  270. break;
  271. }
  272. last_nid = tmp->nid;
  273. }
  274. if (last_nid != 0) {
  275. printf("Table order OK\n");
  276. exit(0);
  277. }
  278. for (tmp = tbl_standard, i = 0;
  279. i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++)
  280. printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
  281. OBJ_nid2ln(tmp->nid));
  282. }
  283. #endif