a_strnid.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 (CHECK_AND_SKIP_PREFIX(p, "MASK:")) {
  45. if (*p == '\0')
  46. return 0;
  47. mask = strtoul(p, &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. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  113. /* "stable" can be impacted by config, so load the config file first */
  114. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  115. #endif
  116. fnd.nid = nid;
  117. if (stable != NULL) {
  118. /* Ideally, this would be done under lock */
  119. sk_ASN1_STRING_TABLE_sort(stable);
  120. idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
  121. if (idx >= 0)
  122. return sk_ASN1_STRING_TABLE_value(stable, idx);
  123. }
  124. return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
  125. }
  126. /*
  127. * Return a string table pointer which can be modified: either directly from
  128. * table or a copy of an internal value added to the table.
  129. */
  130. static ASN1_STRING_TABLE *stable_get(int nid)
  131. {
  132. ASN1_STRING_TABLE *tmp, *rv;
  133. /* Always need a string table so allocate one if NULL */
  134. if (stable == NULL) {
  135. stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
  136. if (stable == NULL)
  137. return NULL;
  138. }
  139. tmp = ASN1_STRING_TABLE_get(nid);
  140. if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
  141. return tmp;
  142. if ((rv = OPENSSL_zalloc(sizeof(*rv))) == NULL)
  143. return NULL;
  144. if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
  145. OPENSSL_free(rv);
  146. return NULL;
  147. }
  148. if (tmp != NULL) {
  149. rv->nid = tmp->nid;
  150. rv->minsize = tmp->minsize;
  151. rv->maxsize = tmp->maxsize;
  152. rv->mask = tmp->mask;
  153. rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
  154. } else {
  155. rv->nid = nid;
  156. rv->minsize = -1;
  157. rv->maxsize = -1;
  158. rv->flags = STABLE_FLAGS_MALLOC;
  159. }
  160. return rv;
  161. }
  162. int ASN1_STRING_TABLE_add(int nid,
  163. long minsize, long maxsize, unsigned long mask,
  164. unsigned long flags)
  165. {
  166. ASN1_STRING_TABLE *tmp;
  167. tmp = stable_get(nid);
  168. if (tmp == NULL) {
  169. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  170. return 0;
  171. }
  172. if (minsize >= 0)
  173. tmp->minsize = minsize;
  174. if (maxsize >= 0)
  175. tmp->maxsize = maxsize;
  176. if (mask)
  177. tmp->mask = mask;
  178. if (flags)
  179. tmp->flags = STABLE_FLAGS_MALLOC | flags;
  180. return 1;
  181. }
  182. void ASN1_STRING_TABLE_cleanup(void)
  183. {
  184. STACK_OF(ASN1_STRING_TABLE) *tmp;
  185. tmp = stable;
  186. if (tmp == NULL)
  187. return;
  188. stable = NULL;
  189. sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
  190. }
  191. static void st_free(ASN1_STRING_TABLE *tbl)
  192. {
  193. if (tbl->flags & STABLE_FLAGS_MALLOC)
  194. OPENSSL_free(tbl);
  195. }