You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.0 KiB

  1. From d6ec605d2059191163cad27b7d7b215ed8d3725b Mon Sep 17 00:00:00 2001
  2. From: Dave McCowan <11235david@gmail.com>
  3. Date: Wed, 30 Jul 2014 10:39:13 -0400
  4. Subject: [PATCH 6/6] MEDIUM: connection: add new bit in Proxy Protocol V2
  5. There are two sample commands to get information about the presence of a
  6. client certificate.
  7. ssl_fc_has_crt is true if there is a certificate present in the current
  8. connection
  9. ssl_c_used is true if there is a certificate present in the session.
  10. If a session has stopped and resumed, then ssl_c_used could be true, while
  11. ssl_fc_has_crt is false.
  12. In the client byte of the TLS TLV of Proxy Protocol V2, there is only one
  13. bit to indicate whether a certificate is present on the connection. The
  14. attached patch adds a second bit to indicate the presence for the session.
  15. This maintains backward compatibility.
  16. [wt: this should be backported to 1.5 to help maintain compatibility
  17. between versions]
  18. (cherry picked from commit 328fb58d745c03a0dc706da9e2fcd4e9f860a14b)
  19. ---
  20. include/proto/ssl_sock.h | 3 ++-
  21. include/types/connection.h | 5 +++--
  22. src/connection.c | 6 ++++--
  23. src/ssl_sock.c | 21 +++++++++++++++++++--
  24. 4 files changed, 28 insertions(+), 7 deletions(-)
  25. diff --git a/include/proto/ssl_sock.h b/include/proto/ssl_sock.h
  26. index 3e111cd..10541ed 100644
  27. --- a/include/proto/ssl_sock.h
  28. +++ b/include/proto/ssl_sock.h
  29. @@ -51,7 +51,8 @@ void ssl_sock_free_all_ctx(struct bind_conf *bind_conf);
  30. const char *ssl_sock_get_cipher_name(struct connection *conn);
  31. const char *ssl_sock_get_proto_version(struct connection *conn);
  32. char *ssl_sock_get_version(struct connection *conn);
  33. -int ssl_sock_get_cert_used(struct connection *conn);
  34. +int ssl_sock_get_cert_used_sess(struct connection *conn);
  35. +int ssl_sock_get_cert_used_conn(struct connection *conn);
  36. int ssl_sock_get_remote_common_name(struct connection *conn, struct chunk *out);
  37. unsigned int ssl_sock_get_verify_result(struct connection *conn);
  38. #ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
  39. diff --git a/include/types/connection.h b/include/types/connection.h
  40. index 2ae16d7..b317007 100644
  41. --- a/include/types/connection.h
  42. +++ b/include/types/connection.h
  43. @@ -345,8 +345,9 @@ struct tlv_ssl {
  44. uint8_t sub_tlv[0];
  45. }__attribute__((packed));
  46. -#define PP2_CLIENT_SSL 0x01
  47. -#define PP2_CLIENT_CERT 0x02
  48. +#define PP2_CLIENT_SSL 0x01
  49. +#define PP2_CLIENT_CERT_CONN 0x02
  50. +#define PP2_CLIENT_CERT_SESS 0x04
  51. #endif /* _TYPES_CONNECTION_H */
  52. diff --git a/src/connection.c b/src/connection.c
  53. index 2dd2c02..3af6d9a 100644
  54. --- a/src/connection.c
  55. +++ b/src/connection.c
  56. @@ -678,9 +678,11 @@ int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connec
  57. tlv_len = make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_TYPE_SSL_VERSION, strlen(value), value);
  58. ssl_tlv_len += tlv_len;
  59. }
  60. - if (ssl_sock_get_cert_used(remote)) {
  61. - tlv->client |= PP2_CLIENT_CERT;
  62. + if (ssl_sock_get_cert_used_sess(remote)) {
  63. + tlv->client |= PP2_CLIENT_CERT_SESS;
  64. tlv->verify = htonl(ssl_sock_get_verify_result(remote));
  65. + if (ssl_sock_get_cert_used_conn(remote))
  66. + tlv->client |= PP2_CLIENT_CERT_CONN;
  67. }
  68. if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
  69. cn_trash = get_trash_chunk();
  70. diff --git a/src/ssl_sock.c b/src/ssl_sock.c
  71. index cf8adc7..da99a30 100644
  72. --- a/src/ssl_sock.c
  73. +++ b/src/ssl_sock.c
  74. @@ -2720,8 +2720,25 @@ out:
  75. return result;
  76. }
  77. -/* returns 1 if client passed a certificate, 0 if not */
  78. -int ssl_sock_get_cert_used(struct connection *conn)
  79. +/* returns 1 if client passed a certificate for this session, 0 if not */
  80. +int ssl_sock_get_cert_used_sess(struct connection *conn)
  81. +{
  82. + X509 *crt = NULL;
  83. +
  84. + if (!ssl_sock_is_ssl(conn))
  85. + return 0;
  86. +
  87. + /* SSL_get_peer_certificate, it increase X509 * ref count */
  88. + crt = SSL_get_peer_certificate(conn->xprt_ctx);
  89. + if (!crt)
  90. + return 0;
  91. +
  92. + X509_free(crt);
  93. + return 1;
  94. +}
  95. +
  96. +/* returns 1 if client passed a certificate for this connection, 0 if not */
  97. +int ssl_sock_get_cert_used_conn(struct connection *conn)
  98. {
  99. if (!ssl_sock_is_ssl(conn))
  100. return 0;
  101. --
  102. 1.8.5.5