a_strnid.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1.h>
  12. #include <openssl/objects.h>
  13. static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
  14. static void st_free(ASN1_STRING_TABLE *tbl);
  15. static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
  16. const ASN1_STRING_TABLE *const *b);
  17. /*
  18. * This is the global mask for the mbstring functions: this is use to mask
  19. * out certain types (such as BMPString and UTF8String) because certain
  20. * software (e.g. Netscape) has problems with them.
  21. */
  22. static unsigned long global_mask = B_ASN1_UTF8STRING;
  23. void ASN1_STRING_set_default_mask(unsigned long mask)
  24. {
  25. global_mask = mask;
  26. }
  27. unsigned long ASN1_STRING_get_default_mask(void)
  28. {
  29. return global_mask;
  30. }
  31. /*-
  32. * This function sets the default to various "flavours" of configuration.
  33. * based on an ASCII string. Currently this is:
  34. * MASK:XXXX : a numerical mask value.
  35. * nobmp : Don't use BMPStrings (just Printable, T61).
  36. * pkix : PKIX recommendation in RFC2459.
  37. * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
  38. * default: the default value, Printable, T61, BMP.
  39. */
  40. int ASN1_STRING_set_default_mask_asc(const char *p)
  41. {
  42. unsigned long mask;
  43. char *end;
  44. if (strncmp(p, "MASK:", 5) == 0) {
  45. if (!p[5])
  46. return 0;
  47. mask = strtoul(p + 5, &end, 0);
  48. if (*end)
  49. return 0;
  50. } else if (strcmp(p, "nombstr") == 0)
  51. mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
  52. else if (strcmp(p, "pkix") == 0)
  53. mask = ~((unsigned long)B_ASN1_T61STRING);
  54. else if (strcmp(p, "utf8only") == 0)
  55. mask = B_ASN1_UTF8STRING;
  56. else if (strcmp(p, "default") == 0)
  57. mask = 0xFFFFFFFFL;
  58. else
  59. return 0;
  60. ASN1_STRING_set_default_mask(mask);
  61. return 1;
  62. }
  63. /*
  64. * The following function generates an ASN1_STRING based on limits in a
  65. * table. Frequently the types and length of an ASN1_STRING are restricted by
  66. * a corresponding OID. For example certificates and certificate requests.
  67. */
  68. ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  69. const unsigned char *in, int inlen,
  70. int inform, int nid)
  71. {
  72. ASN1_STRING_TABLE *tbl;
  73. ASN1_STRING *str = NULL;
  74. unsigned long mask;
  75. int ret;
  76. if (out == NULL)
  77. out = &str;
  78. tbl = ASN1_STRING_TABLE_get(nid);
  79. if (tbl != NULL) {
  80. mask = tbl->mask;
  81. if (!(tbl->flags & STABLE_NO_MASK))
  82. mask &= global_mask;
  83. ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
  84. tbl->minsize, tbl->maxsize);
  85. } else {
  86. ret = ASN1_mbstring_copy(out, in, inlen, inform,
  87. DIRSTRING_TYPE & global_mask);
  88. }
  89. if (ret <= 0)
  90. return NULL;
  91. return *out;
  92. }
  93. /*
  94. * Now the tables and helper functions for the string table:
  95. */
  96. #include "tbl_standard.h"
  97. static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
  98. const ASN1_STRING_TABLE *const *b)
  99. {
  100. return (*a)->nid - (*b)->nid;
  101. }
  102. DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  103. static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
  104. {
  105. return a->nid - b->nid;
  106. }
  107. IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
  108. ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
  109. {
  110. int idx;
  111. ASN1_STRING_TABLE fnd;
  112. fnd.nid = nid;
  113. if (stable) {
  114. idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
  115. if (idx >= 0)
  116. return sk_ASN1_STRING_TABLE_value(stable, idx);
  117. }
  118. return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
  119. }
  120. /*
  121. * Return a string table pointer which can be modified: either directly from
  122. * table or a copy of an internal value added to the table.
  123. */
  124. static ASN1_STRING_TABLE *stable_get(int nid)
  125. {
  126. ASN1_STRING_TABLE *tmp, *rv;
  127. /* Always need a string table so allocate one if NULL */
  128. if (stable == NULL) {
  129. stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
  130. if (stable == NULL)
  131. return NULL;
  132. }
  133. tmp = ASN1_STRING_TABLE_get(nid);
  134. if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
  135. return tmp;
  136. if ((rv = OPENSSL_zalloc(sizeof(*rv))) == NULL) {
  137. ASN1err(ASN1_F_STABLE_GET, ERR_R_MALLOC_FAILURE);
  138. return NULL;
  139. }
  140. if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
  141. OPENSSL_free(rv);
  142. return NULL;
  143. }
  144. if (tmp != NULL) {
  145. rv->nid = tmp->nid;
  146. rv->minsize = tmp->minsize;
  147. rv->maxsize = tmp->maxsize;
  148. rv->mask = tmp->mask;
  149. rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
  150. } else {
  151. rv->nid = nid;
  152. rv->minsize = -1;
  153. rv->maxsize = -1;
  154. rv->flags = STABLE_FLAGS_MALLOC;
  155. }
  156. return rv;
  157. }
  158. int ASN1_STRING_TABLE_add(int nid,
  159. long minsize, long maxsize, unsigned long mask,
  160. unsigned long flags)
  161. {
  162. ASN1_STRING_TABLE *tmp;
  163. tmp = stable_get(nid);
  164. if (tmp == NULL) {
  165. ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
  166. return 0;
  167. }
  168. if (minsize >= 0)
  169. tmp->minsize = minsize;
  170. if (maxsize >= 0)
  171. tmp->maxsize = maxsize;
  172. if (mask)
  173. tmp->mask = mask;
  174. if (flags)
  175. tmp->flags = STABLE_FLAGS_MALLOC | flags;
  176. return 1;
  177. }
  178. void ASN1_STRING_TABLE_cleanup(void)
  179. {
  180. STACK_OF(ASN1_STRING_TABLE) *tmp;
  181. tmp = stable;
  182. if (tmp == NULL)
  183. return;
  184. stable = NULL;
  185. sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
  186. }
  187. static void st_free(ASN1_STRING_TABLE *tbl)
  188. {
  189. if (tbl->flags & STABLE_FLAGS_MALLOC)
  190. OPENSSL_free(tbl);
  191. }