hiredis_ssl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2019, Redis Labs
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HIREDIS_SSL_H
  31. #define __HIREDIS_SSL_H
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* This is the underlying struct for SSL in ssl.h, which is not included to
  36. * keep build dependencies short here.
  37. */
  38. struct ssl_st;
  39. /* A wrapper around OpenSSL SSL_CTX to allow easy SSL use without directly
  40. * calling OpenSSL.
  41. */
  42. typedef struct redisSSLContext redisSSLContext;
  43. /**
  44. * Initialization errors that redisCreateSSLContext() may return.
  45. */
  46. typedef enum {
  47. REDIS_SSL_CTX_NONE = 0, /* No Error */
  48. REDIS_SSL_CTX_CREATE_FAILED, /* Failed to create OpenSSL SSL_CTX */
  49. REDIS_SSL_CTX_CERT_KEY_REQUIRED, /* Client cert and key must both be specified or skipped */
  50. REDIS_SSL_CTX_CA_CERT_LOAD_FAILED, /* Failed to load CA Certificate or CA Path */
  51. REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED, /* Failed to load client certificate */
  52. REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED /* Failed to load private key */
  53. } redisSSLContextError;
  54. /**
  55. * Return the error message corresponding with the specified error code.
  56. */
  57. const char *redisSSLContextGetError(redisSSLContextError error);
  58. /**
  59. * Helper function to initialize the OpenSSL library.
  60. *
  61. * OpenSSL requires one-time initialization before it can be used. Callers should
  62. * call this function only once, and only if OpenSSL is not directly initialized
  63. * elsewhere.
  64. */
  65. int redisInitOpenSSL(void);
  66. /**
  67. * Helper function to initialize an OpenSSL context that can be used
  68. * to initiate SSL connections.
  69. *
  70. * cacert_filename is an optional name of a CA certificate/bundle file to load
  71. * and use for validation.
  72. *
  73. * capath is an optional directory path where trusted CA certificate files are
  74. * stored in an OpenSSL-compatible structure.
  75. *
  76. * cert_filename and private_key_filename are optional names of a client side
  77. * certificate and private key files to use for authentication. They need to
  78. * be both specified or omitted.
  79. *
  80. * server_name is an optional and will be used as a server name indication
  81. * (SNI) TLS extension.
  82. *
  83. * If error is non-null, it will be populated in case the context creation fails
  84. * (returning a NULL).
  85. */
  86. redisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *capath,
  87. const char *cert_filename, const char *private_key_filename,
  88. const char *server_name, redisSSLContextError *error);
  89. /**
  90. * Free a previously created OpenSSL context.
  91. */
  92. void redisFreeSSLContext(redisSSLContext *redis_ssl_ctx);
  93. /**
  94. * Initiate SSL on an existing redisContext.
  95. *
  96. * This is similar to redisInitiateSSL() but does not require the caller
  97. * to directly interact with OpenSSL, and instead uses a redisSSLContext
  98. * previously created using redisCreateSSLContext().
  99. */
  100. int redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx);
  101. /**
  102. * Initiate SSL/TLS negotiation on a provided OpenSSL SSL object.
  103. */
  104. int redisInitiateSSL(redisContext *c, struct ssl_st *ssl);
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* __HIREDIS_SSL_H */