You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

314 lines
11 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <stdlib.h> /* For bsearch */
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. #include <openssl/stack.h>
  63. DEFINE_STACK_OF(ASN1_STRING_TABLE)
  64. static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
  65. static void st_free(ASN1_STRING_TABLE *tbl);
  66. /*
  67. * This is the global mask for the mbstring functions: this is use to mask
  68. * out certain types (such as BMPString and UTF8String) because certain
  69. * software (e.g. Netscape) has problems with them.
  70. */
  71. static unsigned long global_mask = B_ASN1_UTF8STRING;
  72. void ASN1_STRING_set_default_mask(unsigned long mask)
  73. {
  74. global_mask = mask;
  75. }
  76. unsigned long ASN1_STRING_get_default_mask(void)
  77. {
  78. return global_mask;
  79. }
  80. /*
  81. * This function sets the default to various "flavours" of configuration.
  82. * based on an ASCII string. Currently this is: MASK:XXXX : a numerical mask
  83. * value. nobmp : Don't use BMPStrings (just Printable, T61). pkix : PKIX
  84. * recommendation in RFC2459. utf8only : only use UTF8Strings (RFC2459
  85. * recommendation for 2004). default: the default value, Printable, T61, BMP.
  86. */
  87. int ASN1_STRING_set_default_mask_asc(const char *p)
  88. {
  89. unsigned long mask;
  90. char *end;
  91. if (!strncmp(p, "MASK:", 5)) {
  92. if (!p[5])
  93. return 0;
  94. mask = strtoul(p + 5, &end, 0);
  95. if (*end)
  96. return 0;
  97. } else if (!strcmp(p, "nombstr"))
  98. mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
  99. else if (!strcmp(p, "pkix"))
  100. mask = ~((unsigned long)B_ASN1_T61STRING);
  101. else if (!strcmp(p, "utf8only"))
  102. mask = B_ASN1_UTF8STRING;
  103. else if (!strcmp(p, "default"))
  104. mask = 0xFFFFFFFFL;
  105. else
  106. return 0;
  107. ASN1_STRING_set_default_mask(mask);
  108. return 1;
  109. }
  110. /*
  111. * The following function generates an ASN1_STRING based on limits in a
  112. * table. Frequently the types and length of an ASN1_STRING are restricted by
  113. * a corresponding OID. For example certificates and certificate requests.
  114. */
  115. ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  116. const unsigned char *in, int inlen,
  117. int inform, int nid)
  118. {
  119. ASN1_STRING_TABLE *tbl;
  120. ASN1_STRING *str = NULL;
  121. unsigned long mask;
  122. int ret;
  123. if (!out)
  124. out = &str;
  125. tbl = ASN1_STRING_TABLE_get(nid);
  126. if (tbl) {
  127. mask = tbl->mask;
  128. if (!(tbl->flags & STABLE_NO_MASK))
  129. mask &= global_mask;
  130. ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
  131. tbl->minsize, tbl->maxsize);
  132. } else
  133. ret =
  134. ASN1_mbstring_copy(out, in, inlen, inform,
  135. DIRSTRING_TYPE & global_mask);
  136. if (ret <= 0)
  137. return NULL;
  138. return *out;
  139. }
  140. /*
  141. * Now the tables and helper functions for the string table:
  142. */
  143. /* size limits: this stuff is taken straight from RFC3280 */
  144. #define ub_name 32768
  145. #define ub_common_name 64
  146. #define ub_locality_name 128
  147. #define ub_state_name 128
  148. #define ub_organization_name 64
  149. #define ub_organization_unit_name 64
  150. #define ub_title 64
  151. #define ub_email_address 128
  152. #define ub_serial_number 64
  153. /* This table must be kept in NID order */
  154. static const ASN1_STRING_TABLE tbl_standard[] = {
  155. {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
  156. {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
  157. {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
  158. {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
  159. {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
  160. {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
  161. 0},
  162. {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
  163. STABLE_NO_MASK},
  164. {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
  165. {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
  166. {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
  167. {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
  168. {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
  169. {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
  170. {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
  171. STABLE_NO_MASK},
  172. {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
  173. {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
  174. {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
  175. {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
  176. {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
  177. };
  178. static int sk_table_cmp(const ASN1_STRING_TABLE **a,
  179. const ASN1_STRING_TABLE **b)
  180. {
  181. return (*a)->nid - (*b)->nid;
  182. }
  183. static int table_cmp(const void *in_a, const void *in_b)
  184. {
  185. const ASN1_STRING_TABLE *a = in_a;
  186. const ASN1_STRING_TABLE *b = in_b;
  187. return a->nid - b->nid;
  188. }
  189. ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
  190. {
  191. int found;
  192. size_t idx;
  193. ASN1_STRING_TABLE *ttmp;
  194. ASN1_STRING_TABLE fnd;
  195. fnd.nid = nid;
  196. ttmp =
  197. bsearch(&fnd, tbl_standard,
  198. sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE),
  199. sizeof(ASN1_STRING_TABLE), table_cmp);
  200. if (ttmp)
  201. return ttmp;
  202. if (!stable)
  203. return NULL;
  204. sk_ASN1_STRING_TABLE_sort(stable);
  205. found = sk_ASN1_STRING_TABLE_find(stable, &idx, &fnd);
  206. if (!found)
  207. return NULL;
  208. return sk_ASN1_STRING_TABLE_value(stable, idx);
  209. }
  210. int ASN1_STRING_TABLE_add(int nid,
  211. long minsize, long maxsize, unsigned long mask,
  212. unsigned long flags)
  213. {
  214. ASN1_STRING_TABLE *tmp;
  215. char new_nid = 0;
  216. flags &= ~STABLE_FLAGS_MALLOC;
  217. if (!stable)
  218. stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
  219. if (!stable) {
  220. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  221. return 0;
  222. }
  223. if (!(tmp = ASN1_STRING_TABLE_get(nid))) {
  224. tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
  225. if (!tmp) {
  226. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  227. return 0;
  228. }
  229. tmp->flags = flags | STABLE_FLAGS_MALLOC;
  230. tmp->nid = nid;
  231. tmp->minsize = tmp->maxsize = -1;
  232. new_nid = 1;
  233. } else
  234. tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
  235. if (minsize != -1)
  236. tmp->minsize = minsize;
  237. if (maxsize != -1)
  238. tmp->maxsize = maxsize;
  239. tmp->mask = mask;
  240. if (new_nid)
  241. sk_ASN1_STRING_TABLE_push(stable, tmp);
  242. return 1;
  243. }
  244. void ASN1_STRING_TABLE_cleanup(void)
  245. {
  246. STACK_OF(ASN1_STRING_TABLE) *tmp;
  247. tmp = stable;
  248. if (!tmp)
  249. return;
  250. stable = NULL;
  251. sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
  252. }
  253. static void st_free(ASN1_STRING_TABLE *tbl)
  254. {
  255. if (tbl->flags & STABLE_FLAGS_MALLOC)
  256. OPENSSL_free(tbl);
  257. }
  258. #ifdef STRING_TABLE_TEST
  259. int main(void)
  260. {
  261. ASN1_STRING_TABLE *tmp;
  262. int i, last_nid = -1;
  263. for (tmp = tbl_standard, i = 0;
  264. i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
  265. if (tmp->nid < last_nid) {
  266. last_nid = 0;
  267. break;
  268. }
  269. last_nid = tmp->nid;
  270. }
  271. if (last_nid != 0) {
  272. printf("Table order OK\n");
  273. exit(0);
  274. }
  275. for (tmp = tbl_standard, i = 0;
  276. i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++)
  277. printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
  278. OBJ_nid2ln(tmp->nid));
  279. return 0;
  280. }
  281. #endif