a_strnid.c 10 KB

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