asn_moid.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 {
  61. ln = value;
  62. ostr = p + 1;
  63. if (*ostr == '\0')
  64. return 0;
  65. while (ossl_isspace(*ostr))
  66. ostr++;
  67. while (ossl_isspace(*ln))
  68. ln++;
  69. p--;
  70. while (ossl_isspace(*p)) {
  71. if (p == ln)
  72. return 0;
  73. p--;
  74. }
  75. p++;
  76. if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL) {
  77. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  78. return 0;
  79. }
  80. memcpy(lntmp, ln, p - ln);
  81. lntmp[p - ln] = '\0';
  82. ln = lntmp;
  83. }
  84. nid = OBJ_create(ostr, name, ln);
  85. OPENSSL_free(lntmp);
  86. return nid != NID_undef;
  87. }