您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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