a_strnid.c 9.2 KB

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