2
0

asn_mstbl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2012-2017 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. ASN1err(ASN1_F_STBL_MODULE_INIT, 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. ASN1err(ASN1_F_STBL_MODULE_INIT, 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 (strcmp(cnf->name, "min") == 0) {
  67. tbl_min = strtoul(cnf->value, &eptr, 0);
  68. if (*eptr)
  69. goto err;
  70. } else if (strcmp(cnf->name, "max") == 0) {
  71. tbl_max = strtoul(cnf->value, &eptr, 0);
  72. if (*eptr)
  73. goto err;
  74. } else if (strcmp(cnf->name, "mask") == 0) {
  75. if (!ASN1_str2mask(cnf->value, &tbl_mask) || !tbl_mask)
  76. goto err;
  77. } else if (strcmp(cnf->name, "flags") == 0) {
  78. if (strcmp(cnf->value, "nomask") == 0)
  79. tbl_flags = STABLE_NO_MASK;
  80. else if (strcmp(cnf->value, "none") == 0)
  81. tbl_flags = STABLE_FLAGS_CLEAR;
  82. else
  83. goto err;
  84. } else
  85. goto err;
  86. }
  87. rv = 1;
  88. err:
  89. if (rv == 0) {
  90. ASN1err(ASN1_F_DO_TCREATE, ASN1_R_INVALID_STRING_TABLE_VALUE);
  91. if (cnf)
  92. ERR_add_error_data(4, "field=", cnf->name,
  93. ", value=", cnf->value);
  94. else
  95. ERR_add_error_data(4, "name=", name, ", value=", value);
  96. } else {
  97. rv = ASN1_STRING_TABLE_add(nid, tbl_min, tbl_max,
  98. tbl_mask, tbl_flags);
  99. if (!rv)
  100. ASN1err(ASN1_F_DO_TCREATE, ERR_R_MALLOC_FAILURE);
  101. }
  102. sk_CONF_VALUE_pop_free(lst, X509V3_conf_free);
  103. return rv;
  104. }