2
0

ssl_mcnf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2015-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 <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 rv = 0;
  24. unsigned int flags;
  25. const SSL_METHOD *meth;
  26. const SSL_CONF_CMD *cmds;
  27. if (s == NULL && ctx == NULL) {
  28. SSLerr(SSL_F_SSL_DO_CONFIG, ERR_R_PASSED_NULL_PARAMETER);
  29. goto err;
  30. }
  31. if (name == NULL && system)
  32. name = "system_default";
  33. if (!conf_ssl_name_find(name, &idx)) {
  34. if (!system) {
  35. SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_INVALID_CONFIGURATION_NAME);
  36. ERR_add_error_data(2, "name=", name);
  37. }
  38. goto err;
  39. }
  40. cmds = conf_ssl_get(idx, &name, &cmd_count);
  41. cctx = SSL_CONF_CTX_new();
  42. if (cctx == NULL)
  43. goto err;
  44. flags = SSL_CONF_FLAG_FILE;
  45. if (!system)
  46. flags |= SSL_CONF_FLAG_CERTIFICATE | SSL_CONF_FLAG_REQUIRE_PRIVATE;
  47. if (s != NULL) {
  48. meth = s->method;
  49. SSL_CONF_CTX_set_ssl(cctx, s);
  50. } else {
  51. meth = ctx->method;
  52. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  53. }
  54. if (meth->ssl_accept != ssl_undefined_function)
  55. flags |= SSL_CONF_FLAG_SERVER;
  56. if (meth->ssl_connect != ssl_undefined_function)
  57. flags |= SSL_CONF_FLAG_CLIENT;
  58. SSL_CONF_CTX_set_flags(cctx, flags);
  59. for (i = 0; i < cmd_count; i++) {
  60. char *cmdstr, *arg;
  61. conf_ssl_get_cmd(cmds, i, &cmdstr, &arg);
  62. rv = SSL_CONF_cmd(cctx, cmdstr, arg);
  63. if (rv <= 0) {
  64. if (rv == -2)
  65. SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_UNKNOWN_COMMAND);
  66. else
  67. SSLerr(SSL_F_SSL_DO_CONFIG, SSL_R_BAD_VALUE);
  68. ERR_add_error_data(6, "section=", name, ", cmd=", cmdstr,
  69. ", arg=", arg);
  70. goto err;
  71. }
  72. }
  73. rv = SSL_CONF_CTX_finish(cctx);
  74. err:
  75. SSL_CONF_CTX_free(cctx);
  76. return rv <= 0 ? 0 : 1;
  77. }
  78. int SSL_config(SSL *s, const char *name)
  79. {
  80. return ssl_do_config(s, NULL, name, 0);
  81. }
  82. int SSL_CTX_config(SSL_CTX *ctx, const char *name)
  83. {
  84. return ssl_do_config(NULL, ctx, name, 0);
  85. }
  86. void ssl_ctx_system_config(SSL_CTX *ctx)
  87. {
  88. ssl_do_config(NULL, ctx, NULL, 1);
  89. }