crypto.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**************************************************************************/
  2. /* crypto.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CRYPTO_H
  31. #define CRYPTO_H
  32. #include "core/crypto/hashing_context.h"
  33. #include "core/io/resource.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/object/ref_counted.h"
  37. class CryptoKey : public Resource {
  38. GDCLASS(CryptoKey, Resource);
  39. protected:
  40. static void _bind_methods();
  41. static CryptoKey *(*_create)();
  42. public:
  43. static CryptoKey *create();
  44. virtual Error load(String p_path, bool p_public_only = false) = 0;
  45. virtual Error save(String p_path, bool p_public_only = false) = 0;
  46. virtual String save_to_string(bool p_public_only = false) = 0;
  47. virtual Error load_from_string(String p_string_key, bool p_public_only = false) = 0;
  48. virtual bool is_public_only() const = 0;
  49. };
  50. class X509Certificate : public Resource {
  51. GDCLASS(X509Certificate, Resource);
  52. protected:
  53. static void _bind_methods();
  54. static X509Certificate *(*_create)();
  55. public:
  56. static X509Certificate *create();
  57. virtual Error load(String p_path) = 0;
  58. virtual Error load_from_memory(const uint8_t *p_buffer, int p_len) = 0;
  59. virtual Error save(String p_path) = 0;
  60. };
  61. class TLSOptions : public RefCounted {
  62. GDCLASS(TLSOptions, RefCounted);
  63. public:
  64. enum TLSVerifyMode {
  65. TLS_VERIFY_NONE = 0,
  66. TLS_VERIFY_CERT = 1,
  67. TLS_VERIFY_FULL = 2,
  68. };
  69. private:
  70. bool server_mode = false;
  71. String common_name;
  72. TLSVerifyMode verify_mode = TLS_VERIFY_FULL;
  73. Ref<X509Certificate> trusted_ca_chain;
  74. Ref<X509Certificate> own_certificate;
  75. Ref<CryptoKey> private_key;
  76. protected:
  77. static void _bind_methods();
  78. public:
  79. static Ref<TLSOptions> client(Ref<X509Certificate> p_trusted_chain = Ref<X509Certificate>(), const String &p_common_name_override = String());
  80. static Ref<TLSOptions> client_unsafe(Ref<X509Certificate> p_trusted_chain);
  81. static Ref<TLSOptions> server(Ref<CryptoKey> p_own_key, Ref<X509Certificate> p_own_certificate);
  82. TLSVerifyMode get_verify_mode() const { return verify_mode; }
  83. String get_common_name() const { return common_name; }
  84. Ref<X509Certificate> get_trusted_ca_chain() const { return trusted_ca_chain; }
  85. Ref<X509Certificate> get_own_certificate() const { return own_certificate; }
  86. Ref<CryptoKey> get_private_key() const { return private_key; }
  87. bool is_server() const { return server_mode; }
  88. };
  89. class HMACContext : public RefCounted {
  90. GDCLASS(HMACContext, RefCounted);
  91. protected:
  92. static void _bind_methods();
  93. static HMACContext *(*_create)();
  94. public:
  95. static HMACContext *create();
  96. virtual Error start(HashingContext::HashType p_hash_type, PackedByteArray p_key) = 0;
  97. virtual Error update(PackedByteArray p_data) = 0;
  98. virtual PackedByteArray finish() = 0;
  99. HMACContext() {}
  100. virtual ~HMACContext() {}
  101. };
  102. class Crypto : public RefCounted {
  103. GDCLASS(Crypto, RefCounted);
  104. protected:
  105. static void _bind_methods();
  106. static Crypto *(*_create)();
  107. static void (*_load_default_certificates)(String p_path);
  108. public:
  109. static Crypto *create();
  110. static void load_default_certificates(String p_path);
  111. virtual PackedByteArray generate_random_bytes(int p_bytes) = 0;
  112. virtual Ref<CryptoKey> generate_rsa(int p_bytes) = 0;
  113. virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) = 0;
  114. virtual Vector<uint8_t> sign(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Ref<CryptoKey> p_key) = 0;
  115. virtual bool verify(HashingContext::HashType p_hash_type, Vector<uint8_t> p_hash, Vector<uint8_t> p_signature, Ref<CryptoKey> p_key) = 0;
  116. virtual Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_plaintext) = 0;
  117. virtual Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_ciphertext) = 0;
  118. PackedByteArray hmac_digest(HashingContext::HashType p_hash_type, PackedByteArray p_key, PackedByteArray p_msg);
  119. // Compares two PackedByteArrays for equality without leaking timing information in order to prevent timing attacks.
  120. // @see: https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy
  121. bool constant_time_compare(PackedByteArray p_trusted, PackedByteArray p_received);
  122. Crypto() {}
  123. };
  124. class ResourceFormatLoaderCrypto : public ResourceFormatLoader {
  125. public:
  126. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  127. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  128. virtual bool handles_type(const String &p_type) const;
  129. virtual String get_resource_type(const String &p_path) const;
  130. };
  131. class ResourceFormatSaverCrypto : public ResourceFormatSaver {
  132. public:
  133. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
  134. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
  135. virtual bool recognize(const Ref<Resource> &p_resource) const;
  136. };
  137. #endif // CRYPTO_H