asn_moid.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return 0;
  78. memcpy(lntmp, ln, p - ln);
  79. lntmp[p - ln] = '\0';
  80. ln = lntmp;
  81. }
  82. nid = OBJ_create(ostr, name, ln);
  83. OPENSSL_free(lntmp);
  84. return nid != NID_undef;
  85. }