asn_mstbl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2012-2024 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 <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. /* Multi string module: add table entries from a given section */
  15. static int do_tcreate(const char *value, const char *name);
  16. static int stbl_module_init(CONF_IMODULE *md, const CONF *cnf)
  17. {
  18. int i;
  19. const char *stbl_section;
  20. STACK_OF(CONF_VALUE) *sktmp;
  21. CONF_VALUE *mval;
  22. stbl_section = CONF_imodule_get_value(md);
  23. if ((sktmp = NCONF_get_section(cnf, stbl_section)) == NULL) {
  24. ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_LOADING_SECTION);
  25. return 0;
  26. }
  27. for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  28. mval = sk_CONF_VALUE_value(sktmp, i);
  29. if (!do_tcreate(mval->value, mval->name)) {
  30. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_VALUE);
  31. return 0;
  32. }
  33. }
  34. return 1;
  35. }
  36. static void stbl_module_finish(CONF_IMODULE *md)
  37. {
  38. ASN1_STRING_TABLE_cleanup();
  39. }
  40. void ASN1_add_stable_module(void)
  41. {
  42. CONF_module_add("stbl_section", stbl_module_init, stbl_module_finish);
  43. }
  44. /*
  45. * Create an table entry based on a name value pair. format is oid_name =
  46. * n1:v1, n2:v2,... where name is "min", "max", "mask" or "flags".
  47. */
  48. static int do_tcreate(const char *value, const char *name)
  49. {
  50. char *eptr;
  51. int nid, i, rv = 0;
  52. long tbl_min = -1, tbl_max = -1;
  53. unsigned long tbl_mask = 0, tbl_flags = 0;
  54. STACK_OF(CONF_VALUE) *lst = NULL;
  55. CONF_VALUE *cnf = NULL;
  56. nid = OBJ_sn2nid(name);
  57. if (nid == NID_undef)
  58. nid = OBJ_ln2nid(name);
  59. if (nid == NID_undef)
  60. goto err;
  61. lst = X509V3_parse_list(value);
  62. if (!lst)
  63. goto err;
  64. for (i = 0; i < sk_CONF_VALUE_num(lst); i++) {
  65. cnf = sk_CONF_VALUE_value(lst, i);
  66. if (cnf->value == NULL)
  67. goto err;
  68. if (strcmp(cnf->name, "min") == 0) {
  69. tbl_min = strtoul(cnf->value, &eptr, 0);
  70. if (*eptr)
  71. goto err;
  72. } else if (strcmp(cnf->name, "max") == 0) {
  73. tbl_max = strtoul(cnf->value, &eptr, 0);
  74. if (*eptr)
  75. goto err;
  76. } else if (strcmp(cnf->name, "mask") == 0) {
  77. if (!ASN1_str2mask(cnf->value, &tbl_mask) || !tbl_mask)
  78. goto err;
  79. } else if (strcmp(cnf->name, "flags") == 0) {
  80. if (strcmp(cnf->value, "nomask") == 0)
  81. tbl_flags = STABLE_NO_MASK;
  82. else if (strcmp(cnf->value, "none") == 0)
  83. tbl_flags = STABLE_FLAGS_CLEAR;
  84. else
  85. goto err;
  86. } else
  87. goto err;
  88. }
  89. rv = 1;
  90. err:
  91. if (rv == 0) {
  92. if (cnf)
  93. ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_STRING_TABLE_VALUE,
  94. "field=%s, value=%s", cnf->name,
  95. cnf->value != NULL ? cnf->value
  96. : value);
  97. else
  98. ERR_raise_data(ERR_LIB_ASN1, ASN1_R_INVALID_STRING_TABLE_VALUE,
  99. "name=%s, value=%s", name, value);
  100. } else {
  101. rv = ASN1_STRING_TABLE_add(nid, tbl_min, tbl_max,
  102. tbl_mask, tbl_flags);
  103. if (!rv)
  104. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  105. }
  106. sk_CONF_VALUE_pop_free(lst, X509V3_conf_free);
  107. return rv;
  108. }