asn_moid.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2002-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/ctype.h"
  11. #include <openssl/crypto.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/conf.h>
  14. #include <openssl/x509.h>
  15. #include "internal/asn1_int.h"
  16. #include "internal/objects.h"
  17. /* Simple ASN1 OID module: add all objects in a given section */
  18. static int do_create(const char *value, const char *name);
  19. static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
  20. {
  21. int i;
  22. const char *oid_section;
  23. STACK_OF(CONF_VALUE) *sktmp;
  24. CONF_VALUE *oval;
  25. oid_section = CONF_imodule_get_value(md);
  26. if ((sktmp = NCONF_get_section(cnf, oid_section)) == NULL) {
  27. ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
  28. return 0;
  29. }
  30. for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  31. oval = sk_CONF_VALUE_value(sktmp, i);
  32. if (!do_create(oval->value, oval->name)) {
  33. ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
  34. return 0;
  35. }
  36. }
  37. return 1;
  38. }
  39. static void oid_module_finish(CONF_IMODULE *md)
  40. {
  41. }
  42. void ASN1_add_oid_module(void)
  43. {
  44. CONF_module_add("oid_section", oid_module_init, oid_module_finish);
  45. }
  46. /*-
  47. * Create an OID based on a name value pair. Accept two formats.
  48. * shortname = 1.2.3.4
  49. * shortname = some long name, 1.2.3.4
  50. */
  51. static int do_create(const char *value, const char *name)
  52. {
  53. int nid;
  54. ASN1_OBJECT *oid;
  55. const char *ln, *ostr, *p;
  56. char *lntmp;
  57. p = strrchr(value, ',');
  58. if (!p) {
  59. ln = name;
  60. ostr = value;
  61. } else {
  62. ln = NULL;
  63. ostr = p + 1;
  64. if (!*ostr)
  65. return 0;
  66. while (ossl_isspace(*ostr))
  67. ostr++;
  68. }
  69. nid = OBJ_create(ostr, name, ln);
  70. if (nid == NID_undef)
  71. return 0;
  72. if (p) {
  73. ln = value;
  74. while (ossl_isspace(*ln))
  75. ln++;
  76. p--;
  77. while (ossl_isspace(*p)) {
  78. if (p == ln)
  79. return 0;
  80. p--;
  81. }
  82. p++;
  83. if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL) {
  84. ASN1err(ASN1_F_DO_CREATE, ERR_R_MALLOC_FAILURE);
  85. return 0;
  86. }
  87. memcpy(lntmp, ln, p - ln);
  88. lntmp[p - ln] = 0;
  89. oid = OBJ_nid2obj(nid);
  90. oid->ln = lntmp;
  91. }
  92. return 1;
  93. }