a_strnid.c 6.1 KB

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