asn_moid.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2002-2020 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 "crypto/ctype.h"
  11. #include <openssl/crypto.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/conf.h>
  14. #include <openssl/x509.h>
  15. #include "crypto/asn1.h"
  16. #include "crypto/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. ERR_raise(ERR_LIB_ASN1, 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. ERR_raise(ERR_LIB_ASN1, 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. const char *ln, *ostr, *p;
  55. char *lntmp = NULL;
  56. p = strrchr(value, ',');
  57. if (p == NULL) {
  58. ln = name;
  59. ostr = value;
  60. } else if (p == value) {
  61. /* we started with a leading comma */
  62. ln = name;
  63. ostr = p + 1;
  64. } else {
  65. ln = value;
  66. ostr = p + 1;
  67. if (*ostr == '\0')
  68. return 0;
  69. while (ossl_isspace(*ostr))
  70. ostr++;
  71. while (ossl_isspace(*ln))
  72. ln++;
  73. p--;
  74. while (ossl_isspace(*p)) {
  75. if (p == ln)
  76. return 0;
  77. p--;
  78. }
  79. p++;
  80. if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL)
  81. return 0;
  82. memcpy(lntmp, ln, p - ln);
  83. lntmp[p - ln] = '\0';
  84. ln = lntmp;
  85. }
  86. nid = OBJ_create(ostr, name, ln);
  87. OPENSSL_free(lntmp);
  88. return nid != NID_undef;
  89. }