ssl_mcnf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2015-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 <openssl/conf.h>
  11. #include <openssl/ssl.h>
  12. #include "ssl_local.h"
  13. #include "internal/sslconf.h"
  14. /* SSL library configuration module. */
  15. void SSL_add_ssl_module(void)
  16. {
  17. /* Do nothing. This will be added automatically by libcrypto */
  18. }
  19. static int ssl_do_config(SSL *s, SSL_CTX *ctx, const char *name, int system)
  20. {
  21. SSL_CONF_CTX *cctx = NULL;
  22. size_t i, idx, cmd_count;
  23. int err = 1;
  24. unsigned int flags;
  25. unsigned int conf_diagnostics = 0;
  26. const SSL_METHOD *meth;
  27. const SSL_CONF_CMD *cmds;
  28. OSSL_LIB_CTX *prev_libctx = NULL;
  29. OSSL_LIB_CTX *libctx = NULL;
  30. if (s == NULL && ctx == NULL) {
  31. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  32. goto err;
  33. }
  34. if (name == NULL && system)
  35. name = "system_default";
  36. if (!conf_ssl_name_find(name, &idx)) {
  37. if (!system)
  38. ERR_raise_data(ERR_LIB_SSL, SSL_R_INVALID_CONFIGURATION_NAME,
  39. "name=%s", name);
  40. goto err;
  41. }
  42. cmds = conf_ssl_get(idx, &name, &cmd_count);
  43. cctx = SSL_CONF_CTX_new();
  44. if (cctx == NULL) {
  45. /* this is a fatal error, always report */
  46. system = 0;
  47. goto err;
  48. }
  49. flags = SSL_CONF_FLAG_FILE;
  50. if (!system)
  51. flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE;
  52. if (s != NULL) {
  53. meth = s->method;
  54. SSL_CONF_CTX_set_ssl(cctx, s);
  55. libctx = s->ctx->libctx;
  56. } else {
  57. meth = ctx->method;
  58. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  59. libctx = ctx->libctx;
  60. }
  61. conf_diagnostics = OSSL_LIB_CTX_get_conf_diagnostics(libctx);
  62. if (conf_diagnostics)
  63. flags |= SSL_CONF_FLAG_SHOW_ERRORS;
  64. if (meth->ssl_accept != ssl_undefined_function)
  65. flags |= SSL_CONF_FLAG_SERVER;
  66. if (meth->ssl_connect != ssl_undefined_function)
  67. flags |= SSL_CONF_FLAG_CLIENT;
  68. SSL_CONF_CTX_set_flags(cctx, flags);
  69. prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
  70. err = 0;
  71. for (i = 0; i < cmd_count; i++) {
  72. char *cmdstr, *arg;
  73. int rv;
  74. conf_ssl_get_cmd(cmds, i, &cmdstr, &arg);
  75. rv = SSL_CONF_cmd(cctx, cmdstr, arg);
  76. if (rv <= 0)
  77. ++err;
  78. }
  79. if (!SSL_CONF_CTX_finish(cctx))
  80. ++err;
  81. err:
  82. OSSL_LIB_CTX_set0_default(prev_libctx);
  83. SSL_CONF_CTX_free(cctx);
  84. return err == 0 || (system && !conf_diagnostics);
  85. }
  86. int SSL_config(SSL *s, const char *name)
  87. {
  88. return ssl_do_config(s, NULL, name, 0);
  89. }
  90. int SSL_CTX_config(SSL_CTX *ctx, const char *name)
  91. {
  92. return ssl_do_config(NULL, ctx, name, 0);
  93. }
  94. int ssl_ctx_system_config(SSL_CTX *ctx)
  95. {
  96. return ssl_do_config(NULL, ctx, NULL, 1);
  97. }