fingerprint.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <s2n.h>
  17. /**
  18. * @file fingerprint.h
  19. *
  20. * The following APIs enable applications to calculate fingerprints to
  21. * identify ClientHellos.
  22. *
  23. * The fingerprinting APIs are currently considered unstable. They will be finalized
  24. * and marked as stable after an initial customer integration and feedback.
  25. */
  26. typedef enum {
  27. /*
  28. * The current standard open source fingerprinting method.
  29. * See https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967.
  30. */
  31. S2N_FINGERPRINT_JA3,
  32. } s2n_fingerprint_type;
  33. /**
  34. * Calculates a fingerprint hash for a given ClientHello.
  35. *
  36. * Currently the only type supported is S2N_FINGERPRINT_JA3, which uses MD5 and
  37. * requires at least 16 bytes of memory.
  38. *
  39. * @param ch The ClientHello to fingerprint.
  40. * @param type The algorithm to use for the fingerprint. Currently only JA3 is supported.
  41. * @param max_hash_size The maximum size of data that may be written to `hash`.
  42. * If too small for the requested hash, an S2N_ERR_T_USAGE error will occur.
  43. * @param hash The location that the requested hash will be written to.
  44. * @param hash_size The actual size of the data written to `hash`.
  45. * @param str_size The actual size of the full string associated with this hash.
  46. * This size can be used to ensure that sufficient memory is provided for the
  47. * output of `s2n_client_hello_get_fingerprint_string`.
  48. * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
  49. */
  50. S2N_API int s2n_client_hello_get_fingerprint_hash(struct s2n_client_hello *ch,
  51. s2n_fingerprint_type type, uint32_t max_hash_size,
  52. uint8_t *hash, uint32_t *hash_size, uint32_t *str_size);
  53. /**
  54. * Calculates a full, variable-length fingerprint string for a given ClientHello.
  55. *
  56. * Because the length of the string is variable and unknown until the string is
  57. * calculated, `s2n_client_hello_get_fingerprint_hash` can be called first to
  58. * determine `max_size` and ensure `output` is sufficiently large.
  59. *
  60. * @param ch The ClientHello to fingerprint.
  61. * @param type The algorithm to use for the fingerprint. Currently only JA3 is supported.
  62. * @param max_size The maximum size of data that may be written to `output`.
  63. * If too small for the requested string, an S2N_ERR_T_USAGE error will occur.
  64. * @param output The location that the requested string will be written to.
  65. * @param output_size The actual size of the data written to `output`.
  66. * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
  67. */
  68. S2N_API int s2n_client_hello_get_fingerprint_string(struct s2n_client_hello *ch,
  69. s2n_fingerprint_type type, uint32_t max_size,
  70. uint8_t *output, uint32_t *output_size);
  71. /**
  72. * Creates an s2n_client_hello from bytes representing a ClientHello message.
  73. *
  74. * Unlike s2n_connection_get_client_hello, the s2n_client_hello returned by this
  75. * method is owned by the application and must be freed with s2n_client_hello_free.
  76. *
  77. * This method does not support SSLv2 ClientHellos.
  78. *
  79. * @param bytes The raw bytes representing the ClientHello.
  80. * @param size The size of raw_message.
  81. * @returns A new s2n_client_hello on success, or NULL on failure.
  82. */
  83. S2N_API struct s2n_client_hello *s2n_client_hello_parse_message(const uint8_t *bytes, uint32_t size);
  84. /**
  85. * Frees an s2n_client_hello structure.
  86. *
  87. * This method should be called to free s2n_client_hellos returned by
  88. * s2n_client_hello_parse_message. It will error if passed an s2n_client_hello
  89. * returned by s2n_connection_get_client_hello and owned by the connection.
  90. *
  91. * @param ch The structure to be freed.
  92. * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
  93. */
  94. S2N_API int s2n_client_hello_free(struct s2n_client_hello **ch);