crypto.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*************************************************************************/
  2. /* crypto.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/reference.h"
  33. #include "core/resource.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/io/resource_saver.h"
  36. class CryptoKey : public Resource {
  37. GDCLASS(CryptoKey, Resource);
  38. protected:
  39. static void _bind_methods();
  40. static CryptoKey *(*_create)();
  41. public:
  42. static CryptoKey *create();
  43. virtual Error load(String p_path) = 0;
  44. virtual Error save(String p_path) = 0;
  45. };
  46. class X509Certificate : public Resource {
  47. GDCLASS(X509Certificate, Resource);
  48. protected:
  49. static void _bind_methods();
  50. static X509Certificate *(*_create)();
  51. public:
  52. static X509Certificate *create();
  53. virtual Error load(String p_path) = 0;
  54. virtual Error load_from_memory(const uint8_t *p_buffer, int p_len) = 0;
  55. virtual Error save(String p_path) = 0;
  56. };
  57. class Crypto : public Reference {
  58. GDCLASS(Crypto, Reference);
  59. protected:
  60. static void _bind_methods();
  61. static Crypto *(*_create)();
  62. static void (*_load_default_certificates)(String p_path);
  63. public:
  64. static Crypto *create();
  65. static void load_default_certificates(String p_path);
  66. virtual PackedByteArray generate_random_bytes(int p_bytes);
  67. virtual Ref<CryptoKey> generate_rsa(int p_bytes);
  68. virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after);
  69. Crypto();
  70. };
  71. class ResourceFormatLoaderCrypto : public ResourceFormatLoader {
  72. GDCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader);
  73. public:
  74. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL, bool p_use_sub_threads = false, float *r_progress = nullptr);
  75. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  76. virtual bool handles_type(const String &p_type) const;
  77. virtual String get_resource_type(const String &p_path) const;
  78. };
  79. class ResourceFormatSaverCrypto : public ResourceFormatSaver {
  80. GDCLASS(ResourceFormatSaverCrypto, ResourceFormatSaver);
  81. public:
  82. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  83. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  84. virtual bool recognize(const RES &p_resource) const;
  85. };
  86. #endif // CRYPTO_H