security_connector.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #ifndef GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H
  34. #define GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H
  35. #include <grpc/grpc_security.h>
  36. #include "src/core/iomgr/endpoint.h"
  37. #include "src/core/tsi/transport_security_interface.h"
  38. /* --- status enum. --- */
  39. typedef enum {
  40. GRPC_SECURITY_OK = 0,
  41. GRPC_SECURITY_PENDING,
  42. GRPC_SECURITY_ERROR
  43. } grpc_security_status;
  44. /* --- URL schemes. --- */
  45. #define GRPC_SSL_URL_SCHEME "https"
  46. #define GRPC_FAKE_SECURITY_URL_SCHEME "http+fake_security"
  47. /* --- security_connector object. ---
  48. A security connector object represents away to configure the underlying
  49. transport security mechanism and check the resulting trusted peer. */
  50. typedef struct grpc_security_connector grpc_security_connector;
  51. #define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector"
  52. typedef void (*grpc_security_check_cb)(void *user_data,
  53. grpc_security_status status);
  54. /* Ownership of the secure_endpoint is transfered. */
  55. typedef void (*grpc_security_handshake_done_cb)(
  56. void *user_data, grpc_security_status status,
  57. grpc_endpoint *wrapped_endpoint, grpc_endpoint *secure_endpoint);
  58. typedef struct {
  59. void (*destroy)(grpc_security_connector *sc);
  60. void (*do_handshake)(grpc_security_connector *sc,
  61. grpc_endpoint *nonsecure_endpoint,
  62. grpc_security_handshake_done_cb cb, void *user_data);
  63. grpc_security_status (*check_peer)(grpc_security_connector *sc, tsi_peer peer,
  64. grpc_security_check_cb cb,
  65. void *user_data);
  66. } grpc_security_connector_vtable;
  67. struct grpc_security_connector {
  68. const grpc_security_connector_vtable *vtable;
  69. gpr_refcount refcount;
  70. int is_client_side;
  71. const char *url_scheme;
  72. grpc_auth_context *auth_context; /* Populated after the peer is checked. */
  73. };
  74. /* Refcounting. */
  75. #ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG
  76. #define GRPC_SECURITY_CONNECTOR_REF(p, r) \
  77. grpc_security_connector_ref((p), __FILE__, __LINE__, (r))
  78. #define GRPC_SECURITY_CONNECTOR_UNREF(p, r) \
  79. grpc_security_connector_unref((p), __FILE__, __LINE__, (r))
  80. grpc_security_connector *grpc_security_connector_ref(
  81. grpc_security_connector *policy, const char *file, int line,
  82. const char *reason);
  83. void grpc_security_connector_unref(grpc_security_connector *policy,
  84. const char *file, int line,
  85. const char *reason);
  86. #else
  87. #define GRPC_SECURITY_CONNECTOR_REF(p, r) grpc_security_connector_ref((p))
  88. #define GRPC_SECURITY_CONNECTOR_UNREF(p, r) grpc_security_connector_unref((p))
  89. grpc_security_connector *grpc_security_connector_ref(
  90. grpc_security_connector *policy);
  91. void grpc_security_connector_unref(grpc_security_connector *policy);
  92. #endif
  93. /* Handshake. */
  94. void grpc_security_connector_do_handshake(grpc_security_connector *connector,
  95. grpc_endpoint *nonsecure_endpoint,
  96. grpc_security_handshake_done_cb cb,
  97. void *user_data);
  98. /* Check the peer.
  99. Implementations can choose to check the peer either synchronously or
  100. asynchronously. In the first case, a successful call will return
  101. GRPC_SECURITY_OK. In the asynchronous case, the call will return
  102. GRPC_SECURITY_PENDING unless an error is detected early on.
  103. Ownership of the peer is transfered.
  104. */
  105. grpc_security_status grpc_security_connector_check_peer(
  106. grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb,
  107. void *user_data);
  108. /* Util to encapsulate the connector in a channel arg. */
  109. grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc);
  110. /* Util to get the connector from a channel arg. */
  111. grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg);
  112. /* Util to find the connector from channel args. */
  113. grpc_security_connector *grpc_find_security_connector_in_args(
  114. const grpc_channel_args *args);
  115. /* --- channel_security_connector object. ---
  116. A channel security connector object represents away to configure the
  117. underlying transport security mechanism on the client side. */
  118. typedef struct grpc_channel_security_connector grpc_channel_security_connector;
  119. struct grpc_channel_security_connector {
  120. grpc_security_connector base; /* requires is_client_side to be non 0. */
  121. grpc_credentials *request_metadata_creds;
  122. grpc_security_status (*check_call_host)(grpc_channel_security_connector *sc,
  123. const char *host,
  124. grpc_security_check_cb cb,
  125. void *user_data);
  126. };
  127. /* Checks that the host that will be set for a call is acceptable.
  128. Implementations can choose do the check either synchronously or
  129. asynchronously. In the first case, a successful call will return
  130. GRPC_SECURITY_OK. In the asynchronous case, the call will return
  131. GRPC_SECURITY_PENDING unless an error is detected early on. */
  132. grpc_security_status grpc_channel_security_connector_check_call_host(
  133. grpc_channel_security_connector *sc, const char *host,
  134. grpc_security_check_cb cb, void *user_data);
  135. /* --- Creation security connectors. --- */
  136. /* For TESTING ONLY!
  137. Creates a fake connector that emulates real channel security. */
  138. grpc_channel_security_connector *grpc_fake_channel_security_connector_create(
  139. grpc_credentials *request_metadata_creds, int call_host_check_is_async);
  140. /* For TESTING ONLY!
  141. Creates a fake connector that emulates real server security. */
  142. grpc_security_connector *grpc_fake_server_security_connector_create(void);
  143. /* Config for ssl clients. */
  144. typedef struct {
  145. unsigned char *pem_private_key;
  146. size_t pem_private_key_size;
  147. unsigned char *pem_cert_chain;
  148. size_t pem_cert_chain_size;
  149. unsigned char *pem_root_certs;
  150. size_t pem_root_certs_size;
  151. } grpc_ssl_config;
  152. /* Creates an SSL channel_security_connector.
  153. - request_metadata_creds is the credentials object which metadata
  154. will be sent with each request. This parameter can be NULL.
  155. - config is the SSL config to be used for the SSL channel establishment.
  156. - is_client should be 0 for a server or a non-0 value for a client.
  157. - secure_peer_name is the secure peer name that should be checked in
  158. grpc_channel_security_connector_check_peer. This parameter may be NULL in
  159. which case the peer name will not be checked. Note that if this parameter
  160. is not NULL, then, pem_root_certs should not be NULL either.
  161. - sc is a pointer on the connector to be created.
  162. This function returns GRPC_SECURITY_OK in case of success or a
  163. specific error code otherwise.
  164. */
  165. grpc_security_status grpc_ssl_channel_security_connector_create(
  166. grpc_credentials *request_metadata_creds, const grpc_ssl_config *config,
  167. const char *target_name, const char *overridden_target_name,
  168. grpc_channel_security_connector **sc);
  169. /* Gets the default ssl roots. */
  170. size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs);
  171. /* Config for ssl servers. */
  172. typedef struct {
  173. unsigned char **pem_private_keys;
  174. size_t *pem_private_keys_sizes;
  175. unsigned char **pem_cert_chains;
  176. size_t *pem_cert_chains_sizes;
  177. size_t num_key_cert_pairs;
  178. unsigned char *pem_root_certs;
  179. size_t pem_root_certs_size;
  180. int force_client_auth;
  181. } grpc_ssl_server_config;
  182. /* Creates an SSL server_security_connector.
  183. - config is the SSL config to be used for the SSL channel establishment.
  184. - sc is a pointer on the connector to be created.
  185. This function returns GRPC_SECURITY_OK in case of success or a
  186. specific error code otherwise.
  187. */
  188. grpc_security_status grpc_ssl_server_security_connector_create(
  189. const grpc_ssl_server_config *config, grpc_security_connector **sc);
  190. /* Util. */
  191. const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer,
  192. const char *name);
  193. /* Exposed for testing only. */
  194. grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer);
  195. #endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H */