2
0

a_strnid.c 9.2 KB

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